Skip to content

Commit

Permalink
Merge pull request #469 from MouseLand/test-separation
Browse files Browse the repository at this point in the history
Test type separation
  • Loading branch information
carsen-stringer committed Jul 27, 2020
2 parents b60cbdb + 657a081 commit cc0322a
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 101 deletions.
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

@pytest.fixture()
def data_dir():
return Path(__file__).parent.parent.joinpath('data/test_data')
return Path('data/test_data')


@pytest.fixture()
Expand Down
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions tests/regression/test_io_pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@


import numpy as np
from pathlib import Path
from tifffile import imread
from suite2p import io

from utils import get_binary_file_data


def test_tiff_reconstruction_from_binary_file(test_ops):
"""
Tests to see if tif generated by tiff_to_binary and write_tiff matches test tif.
"""
test_ops['tiff_list'] = ['input.tif']
op = io.tiff_to_binary(test_ops)[0]
output_data = get_binary_file_data(op)
# Make sure data in matrix is nonnegative
assert np.all(output_data >= 0)
fname = io.generate_tiff_filename(
functional_chan=op['functional_chan'],
align_by_chan=op['align_by_chan'],
save_path=op['save_path'],
k=0,
ichan=True
)
io.save_tiff(output_data, fname=fname)
reconstructed_tiff_data = imread(
str(Path(test_ops['save_path0']).joinpath('suite2p', 'plane0', 'reg_tif', 'file000_chan0.tif'))
)
# Compare to our test data
prior_data = imread(
str(Path(test_ops['data_path'][0]).joinpath('1plane1chan', 'suite2p', 'test_write_tiff.tif'))
)
assert np.array_equal(reconstructed_tiff_data, prior_data)

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np
from pathlib import Path
from tifffile import imread
from suite2p.registration import bidiphase, utils, register_binary, get_pc_metrics
from suite2p.registration import register_binary


def prepare_for_registration(op, input_file_name, dimensions):
Expand Down Expand Up @@ -95,58 +95,3 @@ def test_register_binary_rigid_registration_only(test_ops):
assert num_col_lines == 16
assert num_row_lines == 16


def test_spatial_smooth_has_not_regressed_during_refactor():
frames = np.ones((2, 3, 3))
smoothed = utils.spatial_smooth(frames, 2)
expected = np.array([
[[1. , 1. , 0.5 ],
[1. , 1. , 0.5 ],
[0.5 , 0.5 , 0.25]],

[[1. , 1. , 0.5 ],
[1. , 1. , 0.5 ],
[0.5 , 0.5 , 0.25]]], dtype=np.float32)
assert np.allclose(smoothed, expected)


def test_positive_bidiphase_shift_shifts_every_other_line():
orig = np.array([
[[1, 2, 3, 4, 5, 6, 7],
[1, 2, 3, 4, 5, 6, 7],
[1, 2, 3, 4, 5, 6, 7],
[1, 2, 3, 4, 5, 6, 7],
[1, 2, 3, 4, 5, 6, 7]]
])
expected = np.array([
[[1, 2, 3, 4, 5, 6, 7],
[1, 2, 1, 2, 3, 4, 5],
[1, 2, 3, 4, 5, 6, 7],
[1, 2, 1, 2, 3, 4, 5],
[1, 2, 3, 4, 5, 6, 7]]
])

shifted = orig.copy()
bidiphase.shift(shifted, 2)
assert np.allclose(shifted, expected)


def test_negative_bidiphase_shift_shifts_every_other_line():
orig = np.array([
[[1, 2, 3, 4, 5, 6, 7],
[1, 2, 3, 4, 5, 6, 7],
[1, 2, 3, 4, 5, 6, 7],
[1, 2, 3, 4, 5, 6, 7],
[1, 2, 3, 4, 5, 6, 7]]
])
expected = np.array([
[[1, 2, 3, 4, 5, 6, 7],
[3, 4, 5, 6, 7, 6, 7],
[1, 2, 3, 4, 5, 6, 7],
[3, 4, 5, 6, 7, 6, 7],
[1, 2, 3, 4, 5, 6, 7]]
])

shifted = orig.copy()
bidiphase.shift(shifted, -2)
assert np.allclose(shifted, expected)
File renamed without changes.
File renamed without changes.
36 changes: 2 additions & 34 deletions tests/test_modules/test_io_module.py → tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@

import numpy as np
from pathlib import Path
from tifffile import imread
from suite2p import io
from pytest import fixture

from utils import get_binary_file_data


def get_binary_file_data(op):
# Read in binary file's contents as int16 np array
binary_file_data = np.fromfile(
str(Path(op['save_path0']).joinpath('suite2p', 'plane0', 'data.bin')),
np.int16
)
return np.reshape(binary_file_data, (-1, op['Ly'], op['Lx']))


@fixture()
Expand All @@ -27,32 +21,6 @@ def binfile1500(test_ops):
yield bin_file


def test_tiff_reconstruction_from_binary_file(test_ops):
"""
Tests to see if tif generated by tiff_to_binary and write_tiff matches test tif.
"""
test_ops['tiff_list'] = ['input.tif']
op = io.tiff_to_binary(test_ops)[0]
output_data = get_binary_file_data(op)
# Make sure data in matrix is nonnegative
assert np.all(output_data >= 0)
fname = io.generate_tiff_filename(
functional_chan=op['functional_chan'],
align_by_chan=op['align_by_chan'],
save_path=op['save_path'],
k=0,
ichan=True
)
io.save_tiff(output_data, fname=fname)
reconstructed_tiff_data = imread(
str(Path(test_ops['save_path0']).joinpath('suite2p', 'plane0', 'reg_tif', 'file000_chan0.tif'))
)
# Compare to our test data
prior_data = imread(
str(Path(test_ops['data_path'][0]).joinpath('1plane1chan', 'suite2p', 'test_write_tiff.tif'))
)
assert np.array_equal(reconstructed_tiff_data, prior_data)


def test_h5_to_binary_produces_nonnegative_output_data(test_ops):
test_ops['h5py'] = Path(test_ops['data_path'][0]).joinpath('input.h5')
Expand Down
58 changes: 58 additions & 0 deletions tests/test_registration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import numpy as np
from suite2p.registration import bidiphase, utils


def test_spatial_smooth_has_not_regressed_during_refactor():
frames = np.ones((2, 3, 3))
smoothed = utils.spatial_smooth(frames, 2)
expected = np.array([
[[1. , 1. , 0.5 ],
[1. , 1. , 0.5 ],
[0.5 , 0.5 , 0.25]],

[[1. , 1. , 0.5 ],
[1. , 1. , 0.5 ],
[0.5 , 0.5 , 0.25]]], dtype=np.float32)
assert np.allclose(smoothed, expected)


def test_positive_bidiphase_shift_shifts_every_other_line():
orig = np.array([
[[1, 2, 3, 4, 5, 6, 7],
[1, 2, 3, 4, 5, 6, 7],
[1, 2, 3, 4, 5, 6, 7],
[1, 2, 3, 4, 5, 6, 7],
[1, 2, 3, 4, 5, 6, 7]]
])
expected = np.array([
[[1, 2, 3, 4, 5, 6, 7],
[1, 2, 1, 2, 3, 4, 5],
[1, 2, 3, 4, 5, 6, 7],
[1, 2, 1, 2, 3, 4, 5],
[1, 2, 3, 4, 5, 6, 7]]
])

shifted = orig.copy()
bidiphase.shift(shifted, 2)
assert np.allclose(shifted, expected)


def test_negative_bidiphase_shift_shifts_every_other_line():
orig = np.array([
[[1, 2, 3, 4, 5, 6, 7],
[1, 2, 3, 4, 5, 6, 7],
[1, 2, 3, 4, 5, 6, 7],
[1, 2, 3, 4, 5, 6, 7],
[1, 2, 3, 4, 5, 6, 7]]
])
expected = np.array([
[[1, 2, 3, 4, 5, 6, 7],
[3, 4, 5, 6, 7, 6, 7],
[1, 2, 3, 4, 5, 6, 7],
[3, 4, 5, 6, 7, 6, 7],
[1, 2, 3, 4, 5, 6, 7]]
])

shifted = orig.copy()
bidiphase.shift(shifted, -2)
assert np.allclose(shifted, expected)
25 changes: 15 additions & 10 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@
r_tol, a_tol = 1e-4, 5e-2


def get_plane_dir(op, plane):
suite_dir = Path(op['save_path0']).joinpath('suite2p')
suite_dir.mkdir(exist_ok=True)
plane_dir = Path(op['save_path0']).joinpath('suite2p').joinpath('plane{}'.format(plane))
plane_dir.mkdir(exist_ok=True)
def get_plane_dir(op, plane, mkdir=True):
plane_dir = Path(op['save_path0']).joinpath('suite2p/plane{}'.format(plane))
if mkdir:
plane_dir.mkdir(exist_ok=True, parents=True)
return plane_dir


def get_binary_file_data(op):
# Read in binary file's contents as int16 np array
mov = np.fromfile(str(Path(op['save_path0']).joinpath('suite2p/plane0/data.bin')), np.int16)
return mov.reshape(-1, op['Ly'], op['Lx'])


def write_data_to_binary(binary_path, data_path):
input_data = np.load(data_path)
with open(binary_path, 'wb') as f:
Expand All @@ -25,8 +30,8 @@ def write_data_to_binary(binary_path, data_path):


def check_lists_of_arr_all_close(list1, list2):
for i in range(len(list1)):
assert np.allclose(list1[i], list2[i], rtol=r_tol, atol=a_tol)
for l1, l2 in zip(list1, list2):
assert np.allclose(l1, l2, rtol=r_tol, atol=a_tol)


def check_dict_dicts_all_close(first_dict, second_dict):
Expand All @@ -50,9 +55,9 @@ def get_list_of_test_data(outputs_to_check, test_data_dir, nplanes, nchannels, a
np.concatenate([imread(tif) for tif in glob.glob(str(test_plane_dir.joinpath(output)) + '/*.tif')])
)
else:
test_data_list.append(np.load(
str(test_plane_dir.joinpath("{}.npy".format(output))), allow_pickle=True
))
test_data_list.append(
np.load(str(test_plane_dir.joinpath("{}.npy".format(output))), allow_pickle=True)
)
return test_data_list


Expand Down

0 comments on commit cc0322a

Please sign in to comment.