Skip to content

Commit

Permalink
Fixing style in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
handwerkerd committed Feb 7, 2024
1 parent ddf89d9 commit 30a07b8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 37 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# version file autogenerated with pip install -e .'[all]'
_version.py

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
14 changes: 11 additions & 3 deletions mapca/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Tests for mapca configuration."""

import os
from urllib.request import urlretrieve

Expand All @@ -6,7 +8,7 @@

def fetch_file(osf_id, path, filename):
"""
Fetches file located on OSF and downloads to `path`/`filename`1
Fetch file located on OSF and downloads to `path`/`filename`1.
Parameters
----------
Expand All @@ -25,7 +27,7 @@ def fetch_file(osf_id, path, filename):
full_path : str
Full path to downloaded `filename`
"""
url = "https://osf.io/{}/download".format(osf_id)
url = f"https://osf.io/{osf_id}/download"
full_path = os.path.join(path, filename)
if not os.path.isfile(full_path):
urlretrieve(url, full_path)
Expand All @@ -34,35 +36,41 @@ def fetch_file(osf_id, path, filename):

@pytest.fixture(scope="session")
def testpath(tmp_path_factory):
"""Test path that will be used to download all files"""
"""Test path that will be used to download all files."""
return tmp_path_factory.getbasetemp()


@pytest.fixture
def test_img(testpath):
"""Fetch data file."""
return fetch_file("jw43h", testpath, "data.nii.gz")


@pytest.fixture
def test_mask(testpath):
"""Fetch mask file."""
return fetch_file("9u2m5", testpath, "mask.nii.gz")


@pytest.fixture
def test_ts(testpath):
"""Fetch comp_ts file."""
return fetch_file("gz2hb", testpath, "comp_ts.npy")


@pytest.fixture
def test_varex(testpath):
"""Fetch varex file."""
return fetch_file("7xj5k", testpath, "varex.npy")


@pytest.fixture
def test_varex_norm(testpath):
"""Fetch varex_norm file."""
return fetch_file("jrd9c", testpath, "varex_norm.npy")


@pytest.fixture
def test_weights(testpath):
"""Fetch weights file."""
return fetch_file("t94m8", testpath, "voxel_comp_weights.npy")
4 changes: 1 addition & 3 deletions mapca/tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
Integration test for mapca.
"""
"""Integration tests for mapca."""

import shutil
from os.path import split
Expand Down
22 changes: 9 additions & 13 deletions mapca/tests/test_mapca.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
Tests for mapca
"""
"""Tests for mapca."""

import nibabel as nib
import numpy as np
Expand All @@ -10,8 +8,7 @@


def test_ma_pca():
"""Check that ma_pca runs correctly with all three options"""

"""Check that ma_pca runs correctly with all three options."""
n_timepoints = 200
n_voxels = 20
n_vox_total = n_voxels**3
Expand Down Expand Up @@ -57,22 +54,21 @@ def test_ma_pca():
assert v.shape[0] == n_timepoints


def test_MovingAveragePCA():
def test_moving_average_pca():
"""Check that MovingAveragePCA runs correctly with "aic" option."""

N_TIMEPOINTS = 200
N_VOXELS = 20 # number of voxels in each dimension
n_timepoints = 200
n_voxels = 20 # number of voxels in each dimension

# Create fake data to test with
test_data = np.random.random((N_VOXELS, N_VOXELS, N_VOXELS, N_TIMEPOINTS))
time = np.linspace(0, 400, N_TIMEPOINTS)
test_data = np.random.random((n_voxels, n_voxels, n_voxels, n_timepoints))
time = np.linspace(0, 400, n_timepoints)
freq = 1
test_data = test_data + np.sin(2 * np.pi * freq * time)
xform = np.eye(4) * 2
test_img = nib.nifti1.Nifti1Image(test_data, xform)

# Create mask
test_mask = np.zeros((N_VOXELS, N_VOXELS, N_VOXELS), dtype=int)
test_mask = np.zeros((n_voxels, n_voxels, n_voxels), dtype=int)
test_mask[5:-5, 5:-5, 5:-5] = 1
test_mask_img = nib.nifti1.Nifti1Image(test_mask, xform, dtype=np.int16)
n_voxels_in_mask = np.sum(test_mask)
Expand All @@ -85,7 +81,7 @@ def test_MovingAveragePCA():
assert pca.u_.shape[0] == n_voxels_in_mask
assert pca.explained_variance_.shape[0] == 1
assert pca.explained_variance_ratio_.shape[0] == 1
assert pca.components_.T.shape[0] == N_TIMEPOINTS
assert pca.components_.T.shape[0] == n_timepoints

# Test other stuff
pca2 = MovingAveragePCA(criterion="mdl", normalize=True)
Expand Down
27 changes: 9 additions & 18 deletions mapca/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@


def test_autocorr():
"""
Unit test on _autocorr function
"""
"""Unit test on _autocorr function."""
test_data = np.array([1, 2, 3, 4])
test_result = np.array([30, 20, 11, 4])
autocorr = _autocorr(test_data)
assert np.array_equal(autocorr, test_result)


def test_parzen_win():
"""Test parzen gives expected output."""
test_npoints = 3
test_result = np.array([0.07407407, 1, 0.07407407])
win = parzen(test_npoints)
Expand All @@ -31,9 +30,7 @@ def test_parzen_win():


def test_ent_rate_sp():
"""
Check that ent_rate_sp runs correctly, i.e. returns a float
"""
"""Check that ent_rate_sp runs correctly, i.e. returns a float."""
test_data = np.random.rand(200, 10, 10)
ent_rate = ent_rate_sp(test_data, 0)
ent_rate = ent_rate_sp(test_data, 1)
Expand All @@ -55,9 +52,7 @@ def test_ent_rate_sp():


def test_subsampling():
"""
Unit test for subsampling function
"""
"""Unit test for subsampling function."""
# 1D input
test_data = np.array([1])
sub_data = _subsampling(test_data, sub_depth=2)
Expand All @@ -76,18 +71,14 @@ def test_subsampling():


def test_icatb_svd():
"""
Unit test for icatb_svd function.
"""
"""Unit test for icatb_svd function."""
test_data = np.diag(np.random.rand(5))
V, Lambda = _icatb_svd(test_data)
assert np.allclose(np.sum(V, axis=0), np.ones((5,)))
v, lambda_var = _icatb_svd(test_data)
assert np.allclose(np.sum(v, axis=0), np.ones((5,)))


def test_eigensp_adj():
"""
Unit test for eigensp_adj function
"""
"""Unit test for eigensp_adj function."""
test_eigen = np.array([0.9, 0.5, 0.2, 0.1, 0])
n_effective = 2
test_result = np.array([0.13508894, 0.11653465, 0.06727316, 0.05211424, 0.0])
Expand All @@ -96,7 +87,7 @@ def test_eigensp_adj():


def test_kurtosis():
# Generate data
"""Generate data."""
test_data = np.array([[-10, 2, 500, 0, -0.4], [-4, -200, -40, 0.1, 90]]).T

# Run scipy function
Expand Down

0 comments on commit 30a07b8

Please sign in to comment.