Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Documentation, Test cases and CI for trx_file_memmap #13

Merged
merged 7 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python package

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.7", "3.8", "3.9"]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest pytest-cov
python -m pip install .
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest trx_file_memmap/tests
Binary file added trx_file_memmap/tests/data/small.trx
Binary file not shown.
Binary file added trx_file_memmap/tests/data/small_compressed.trx
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�5
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�5
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�5
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions trx_file_memmap/tests/data/small_fldr.trx/dps/algo.uint8
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
163 changes: 163 additions & 0 deletions trx_file_memmap/tests/data/small_fldr.trx/dpv/color_y.uint8

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions trx_file_memmap/tests/data/small_fldr.trx/header.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"DIMENSIONS": [117, 151, 115], "NB_STREAMLINES": 1000, "NB_VERTICES": 33886, "VOXEL_TO_RASMM": [[-1.25, 0.0, 0.0, 72.5], [0.0, 1.25, 0.0, -109.75], [0.0, 0.0, 1.25, -64.5], [0.0, 0.0, 0.0, 1.0]]}
Binary file not shown.
Binary file not shown.
226 changes: 226 additions & 0 deletions trx_file_memmap/tests/test_memmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
#!/usr/bin/env python
from numpy.ma.core import shape
import pytest
import numpy as np
import trx_file_memmap.trx_file_memmap as tmm

from tempfile import mkdtemp
import os.path as path


@pytest.mark.parametrize(
"arr,expected,value_error",
[
(np.ones((5, 5, 5), dtype=np.int16), None, True),
(np.ones((5, 4), dtype=np.int16), "mean_fa.4.int16", False),
(np.ones((5, 4), dtype=np.float64), "mean_fa.4.float64", False),
(np.ones((5, 1), dtype=np.float64), "mean_fa.float64", False),
(np.ones((1), dtype=np.float64), "mean_fa.float64", False),
],
)
def test__generate_filename_from_data(
arr, expected, value_error, filename="mean_fa.bit"
):

if value_error:
with pytest.raises(ValueError):
new_fn = tmm._generate_filename_from_data(arr=arr, filename=filename)
assert new_fn is None
else:
new_fn = tmm._generate_filename_from_data(arr=arr, filename=filename)
assert new_fn == expected


@pytest.mark.parametrize(
"filename,expected,value_error",
[
("mean_fa.float64", ("mean_fa", 1, ".float64"), False),
("mean_fa.5.int32", ("mean_fa", 5, ".int32"), False),
("mean_fa", None, True),
("mean_fa.5.4.int32", None, True),
pytest.param(
"mean_fa.fa", None, True, marks=pytest.mark.xfail, id="invalid extension"
),
],
)
def test__split_ext_with_dimensionality(filename, expected, value_error):
if value_error:
with pytest.raises(ValueError):
assert tmm._split_ext_with_dimensionality(filename) == expected
else:
assert tmm._split_ext_with_dimensionality(filename) == expected


@pytest.mark.parametrize(
"offsets,nb_vertices,expected",
[
(np.array(range(5), dtype=np.int16), 4, np.array([1, 1, 1, 1, 0])),
(np.array([0, 1, 0, 3, 4], dtype=np.int16), 4, np.array([1, 3, 0, 1, 0])),
(np.array(range(4), dtype=np.int16), 4, np.array([1, 1, 1, 1])),
],
)
def test__compute_lengths(offsets, nb_vertices, expected):

lengths = tmm._compute_lengths(offsets=offsets, nb_vertices=nb_vertices)
assert np.array_equal(lengths, expected)


@pytest.mark.parametrize(
"ext,expected",
[
(".bit", True),
(".int16", True),
(".float32", True),
(".ushort", True),
(".txt", False),
],
)
def test__is_dtype_valid(ext, expected):
assert tmm._is_dtype_valid(ext) == expected


@pytest.mark.parametrize(
"arr,l_bound,r_bound,expected",
[
(np.array(range(5), dtype=np.int16), None, None, 4),
(np.array([0, 1, 0, 3, 4], dtype=np.int16), None, None, 1),
(np.array([0, 1, 2, 0, 4], dtype=np.int16), None, None, 2),
(np.array(range(5), dtype=np.int16), 1, 2, 2),
(np.array(range(5), dtype=np.int16), 3, 3, 3),
(np.zeros((5), dtype=np.int16), 3, 3, -1),
],
)
def test__dichotomic_search(arr, l_bound, r_bound, expected):
end_idx = tmm._dichotomic_search(arr, l_bound=l_bound, r_bound=r_bound)
assert end_idx == expected


@pytest.mark.parametrize(
"basename, create, expected",
[
("offsets.int16", True, np.array(range(12), dtype=np.int16).reshape((3, 4))),
("offsets.float32", False, None),
],
)
def test__create_memmap(basename, create, expected):
if create:
# Need to create array before evaluating
filename = path.join(mkdtemp(), basename)
fp = np.memmap(filename, dtype=np.int16, mode="w+", shape=(3, 4))
fp[:] = expected[:]
mmarr = tmm._create_memmap(filename=filename, shape=(3, 4), dtype=np.int16)
assert np.array_equal(mmarr, expected)

else:
mmarr = tmm._create_memmap(filename=basename, shape=(0,), dtype=np.int16)
assert path.isfile(basename)
assert np.array_equal(mmarr, np.zeros(shape=(0,), dtype=np.float32))


# need dpg test with missing keys
@pytest.mark.parametrize(
"path,check_dpg,value_error",
[
("trx_file_memmap/tests/data/small_compressed.trx", False, False),
("trx_file_memmap/tests/data/small.trx", True, False),
("trx_file_memmap/tests/data/small_fldr.trx", False, False),
("trx_file_memmap/tests/data/dontexist.trx", False, True),
],
)
def test__load(path, check_dpg, value_error):

# Need to perhaps improve test
if value_error:
with pytest.raises(ValueError):
assert not isinstance(
tmm.load(input_obj=path, check_dpg=check_dpg), tmm.TrxFile
)
else:
assert isinstance(tmm.load(input_obj=path, check_dpg=check_dpg), tmm.TrxFile)


@pytest.mark.parametrize(
"path",
[
("trx_file_memmap/tests/data/small.trx"),
],
)
def test_load_zip(path):
assert isinstance(tmm.load_from_zip(path), tmm.TrxFile)


@pytest.mark.parametrize("path", [("trx_file_memmap/tests/data/small_fldr.trx")])
def test_load_directory(path):
assert isinstance(tmm.load_from_directory(path), tmm.TrxFile)


def test_concatenate():
pass


def test_save():
pass


def test_zip_from_folder():
pass


def test_trxfile_init():
pass


def test_trxfile_print():
pass


def test_trxfile_len():
pass


def test_trxfile_getitem():
pass


def test_trxfile_deepcopy():
pass


def test_get_real_len():
pass


def test_copy_fixed_arrays_from():
pass


def test_initialize_empty_trx():
pass


def test_create_trx_from_pointer():
pass


def test_trxfile_resize():
pass


def test_trxfile_append():
pass


def test_trxfile_getgroup():
pass


def test_trxfile_select():
pass


def test_trxfile_to_memory():
pass


def test_trxfile_close():
pass
Loading