constriction 0.3.0

Entropy coders for research and production (Rust and Python).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
name: release

on:
  release:
    types: [created]
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always

jobs:
  rust-test:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]

    steps:
      - name: Print event name ("${{ github.event_name }}")
        run: |
          echo "GITHUB_EVENT_NAME: $GITHUB_EVENT_NAME"

      - name: Checkout
        uses: actions/checkout@v1

      - name: Install latest stable Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
          components: rustfmt, clippy

      - name: Lint with rustfmt
        uses: actions-rs/cargo@v1
        with:
          command: fmt
          args: -- --check

      - name: Lint with clippy
        uses: actions-rs/cargo@v1
        env:
          RUSTFLAGS: "-D warnings"
        with:
          command: clippy
          args: --all-targets --all-features

      - name: Test in development mode
        uses: actions-rs/cargo@v1.0.1
        env:
          RUSTFLAGS: "-D warnings"
        with:
          command: test
          toolchain: stable

      - name: Test in release mode
        uses: actions-rs/cargo@v1.0.1
        env:
          RUSTFLAGS: "-D warnings"
        with:
          command: test
          toolchain: stable
          args: --release

  miri-test:
    name: no_std and miri
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]

    steps:
      - name: Checkout
        uses: actions/checkout@v1

      - name: Get latest rust nightly version that has miri
        id: versions
        shell: bash
        run: |
          case ${{ matrix.os }} in
            ubuntu-latest)
              arch="x86_64-unknown-linux-gnu"
              ;;

            macos-latest)
              arch="x86_64-apple-darwin"
              ;;

            windows-latest)
              arch="x86_64-pc-windows-msvc"
              ;;
          esac

          version="nightly-$(curl -s https://rust-lang.github.io/rustup-components-history/$arch/miri)"
          echo "Found version: $version"
          echo "rustc=$version" >> $GITHUB_OUTPUT

      - name: Install rust ${{ steps.versions.outputs.rustc }}
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ steps.versions.outputs.rustc }}
          profile: minimal
          components: miri, rust-src
          override: true

      - name: Test `no_std` compatibility
        shell: bash
        working-directory: ensure_no_std
        run: cargo +${{ steps.versions.outputs.rustc }} build

      - name: Run tests in miri
        env:
          RUSTFLAGS: "-Zrandomize-layout"
          MIRIFLAGS: "-Zmiri-symbolic-alignment-check -Zmiri-disable-isolation"
        run: |
          cargo miri test --no-fail-fast --all-targets

  python-test:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
        os: [ubuntu-latest, macos-latest, windows-latest]

    steps:
      - name: Checkout
        uses: actions/checkout@v1

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install Python dependencies
        run: |
          python -m pip install --upgrade pip
          python -m pip install poetry
          poetry install

      - name: Install latest stable Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      - name: Build Python package
        run: poetry run maturin develop --release --features pybindings

      - name: pytest
        shell: bash
        run: |
          poetry run pytest tests/python | tee pytest.out
          exit ${PIPESTATUS[0]}

      - name: Verify that pytest used correct python version (unix)
        if: matrix.os != 'windows-latest'
        run: |
          USED_PYTHON_VERSION=$(grep -A1 -iE '^=+ test session starts =+$' pytest.out | perl -ne 'if ($_ =~ / -- Python (\d+\.\d+)\.\d+/i) { print "$1"; }')
          echo "Expected python version: ${{ matrix.python-version }}"
          echo "Found python version:    $USED_PYTHON_VERSION"
          if [ "x${{ matrix.python-version }}y" == "x${USED_PYTHON_VERSION}y" ]; then
            echo "Versions match."
          else
            echo "ERROR: versions don't match."
            exit 1
          fi

      - name: Verify that pytest used correct python version (windows)
        if: matrix.os == 'windows-latest'
        run: |
          $USED_PYTHON_VERSION = grep -A1 -iE '^=+ test session starts =+$' pytest.out | perl -ne 'if ($_ =~ / -- Python (\d+\.\d+)\.\d+/i) { print "$1"; }'
          echo "Expected python version: ${{ matrix.python-version }}"
          echo "Found python version:    $USED_PYTHON_VERSION"
          if ( "x${{ matrix.python-version }}y" -eq "x${USED_PYTHON_VERSION}y" )
          {
            echo "Versions match."
          }
          else
          {
            throw "ERROR: versions don't match."
          }

  testall:
    runs-on: ubuntu-latest
    name: Meta job for all tests
    needs: [rust-test, miri-test, python-test]
    steps:
      - name: Done
        run: echo "All tests successful."

  python-doc:
    needs: testall
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1

      - name: Set up Python 3.11
        uses: actions/setup-python@v2
        with:
          python-version: "3.11"

      - name: Install Python dependencies
        run: |
          python -m pip install --upgrade pip
          python -m pip install poetry
          poetry install

      - name: Install latest stable Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      - name: checkout website template
        run: |
          git clone https://github.com/bamler-lab/constriction.git --branch website-template --single-branch website
          rm -rf website/.git
          CONSTRICTION_VERSION=`poetry run python get_version.py`
          echo "Found constriction version $CONSTRICTION_VERSION."
          sed -i "s/<\!-- CONSTRICTION_VERSION -->/$CONSTRICTION_VERSION/g" website/index.html

      - name: generate license file
        run: |
          cargo install cargo-about
          cargo about generate --features=pybindings about.hbs > website/license.html
          wc -l website/license.html

      - name: generate python API reference
        run: |
          poetry run maturin develop --features pybindings
          poetry run python pythondoc.py website/apidoc/python
          mv website/apidoc/python/constriction/* website/apidoc/python/
          rmdir website/apidoc/python/constriction

      - name: Save artifact with website
        uses: actions/upload-artifact@v2
        with:
          name: website
          path: ./website

      - name: Deploy website to gh-pages branch
        if: github.event_name == 'release'
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./website
          commit_message: Deploy

  rust-publish:
    needs: testall
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1

      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      - name: Publish on crates.io
        if: github.event_name == 'release'
        run: |
          cargo login ${{ secrets.CARGO }}
          cargo publish

  python-publish:
    needs: testall
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    steps:
      - uses: actions/checkout@v1

      - name: Install latest stable Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      - name: Set up Python 3.7
        id: setup-python-3-7
        uses: actions/setup-python@v2
        with:
          python-version: 3.7

      - name: Set up Python 3.8
        id: setup-python-3-8
        uses: actions/setup-python@v2
        with:
          python-version: 3.8

      - name: Set up Python 3.9
        id: setup-python-3-9
        uses: actions/setup-python@v2
        with:
          python-version: 3.9

      - name: Set up Python 3.10
        id: setup-python-3-10
        uses: actions/setup-python@v2
        with:
          python-version: "3.10"

      - name: Set up Python 3.11
        id: setup-python-3-11
        uses: actions/setup-python@v2
        with:
          python-version: "3.11"

      - name: Build wheels (unix)
        if: matrix.os != 'windows-latest'
        uses: messense/maturin-action@v1
        with:
          command: build
          manylinux: 2014
          args: --release --strip --features pybindings -i python3.7 python3.8 python3.9 python3.10 python3.11

      - name: Build wheels (windows)
        if: matrix.os == 'windows-latest'
        uses: messense/maturin-action@v1
        with:
          command: build
          args: --release --strip --features pybindings -i C:\hostedtoolcache\windows\Python\${{ steps.setup-python-3-7.outputs.python-version }}\x64\python3.exe C:\hostedtoolcache\windows\Python\${{ steps.setup-python-3-8.outputs.python-version }}\x64\python3.exe C:\hostedtoolcache\windows\Python\${{ steps.setup-python-3-9.outputs.python-version }}\x64\python3.exe C:\hostedtoolcache\windows\Python\${{ steps.setup-python-3-10.outputs.python-version }}\x64\python3.exe C:\hostedtoolcache\windows\Python\${{ steps.setup-python-3-11.outputs.python-version }}\x64\python3.exe

      # For some reason, manylinux builds create wheels owned by the root user,
      # which breaks our attempts to add the license file below.
      - name: Fix wheel ownership on linux
        if: matrix.os == 'ubuntu-latest'
        run: sudo chown -R runner:docker target

      - name: Cross-build wheels for Apple Silicon
        if: matrix.os == 'macos-latest'
        uses: messense/maturin-action@v1
        with:
          target: aarch64-apple-darwin
          command: build
          args: --release --strip --features pybindings -i python3.7 python3.8 python3.9 python3.10 python3.11

      - name: Build universal2 wheels
        if: matrix.os == 'macos-latest'
        uses: messense/maturin-action@v1
        with:
          command: build
          args: --release --strip --features pybindings --universal2 -i python3.7 python3.8 python3.9 python3.10 python3.11

      - name: List wheels (unix)
        if: matrix.os != 'windows-latest'
        run: ls -l ./target/wheels/

      - name: List wheels (windows)
        if: matrix.os == 'windows-latest'
        run: dir target\wheels\

      - name: generate license file
        run: |
          cargo install cargo-about
          cargo about generate --features=pybindings about.hbs > LICENSE.html
          ls -l LICENSE.html
          wc -l LICENSE.html

      - name: Add LICENSE.html to all wheels (unix)
        if: matrix.os != 'windows-latest'
        run: |
          for wheel in target/wheels/*.whl; do
            zip -ur $wheel LICENSE.html
          done

      - name: Add LICENSE.html to all wheels (windows)
        if: matrix.os == 'windows-latest'
        run: |
          [Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | Out-Null
          Get-ChildItem "target\wheels\" -Filter *.whl |
          Foreach-Object {
            $zip = [System.IO.Compression.ZipFile]::Open($_.FullName, "Update")
            $licenseFile = [System.IO.Path]::GetFileName("LICENSE.html")
            [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, "LICENSE.html", $licenseFile, "Optimal") | Out-Null
            $zip.Dispose()
          }

      - name: Save artifact with wheels for ${{ matrix.os }}
        uses: actions/upload-artifact@v2
        with:
          name: wheels-${{ matrix.os }}
          path: ./target/wheels/

      - name: Test install wheels (unix)
        if: matrix.os != 'windows-latest'
        run: |
          for i in target/wheels/*.whl; do
            echo "Running: pip install $i ..."
            pip install "$i" || echo "WARNING: unable to install $i"

            echo "Testing if we can import constriction and numpy ..."
            python -c 'import constriction; import numpy; print(constriction.__file__)' || echo "WARNING: unable to import constriction or numpy ($i)"

            echo "Running: pip uninstall -y constriction numpy"
            pip uninstall -y constriction numpy
            echo
          done

      - name: Test install wheels (windows)
        if: matrix.os == 'windows-latest'
        run: |
          $wheels = Get-ChildItem "target\wheels\"
          foreach ($wheel in $wheels){
            echo "Running: pip install $($wheel.FullName) ..."
            try {
              pip install "$($wheel.FullName)"
            } catch {
              echo "WARNING: unable to install $($wheel.FullName)"
            }

            echo "Testing if we can import constriction and numpy ..."
            try {
              python -c 'import constriction; import numpy; print(constriction.__file__)'
            } catch {
              echo "WARNING: unable to import constriction or numpy ($($wheel.FullName))"
            }

            echo "Running: pip uninstall -y constriction numpy ..."
            pip uninstall -y constriction numpy
            echo ""
          }

      - name: Install Python dependencies (for twine)
        run: |
          python -m pip install --upgrade pip
          python -m pip install poetry
          poetry install

      - name: Publish wheels (unix)
        if: matrix.os != 'windows-latest' && github.event_name == 'release'
        env:
          TWINE_USERNAME: __token__
          TWINE_PASSWORD: ${{ secrets.PYPI }}
        run: |
          poetry run twine upload target/wheels/*.whl

      - name: Publish wheels (windows)
        if: matrix.os == 'windows-latest' && github.event_name == 'release'
        env:
          TWINE_USERNAME: __token__
          TWINE_PASSWORD: ${{ secrets.PYPI }}
        run: |
          $wheels = Get-ChildItem "target\wheels\"
          foreach ($wheel in $wheels){
            echo "Uploading $($wheel.FullName)"
            poetry run twine upload "$($wheel.FullName)"
          }

      - name: Add wheels to Github release
        if: github.event_name == 'release'
        uses: softprops/action-gh-release@v1
        with:
          files: target/wheels/*.whl
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}