Skip to content

Commit

Permalink
remove old tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rpauszek committed Nov 17, 2023
1 parent 45e49e3 commit 03249be
Showing 1 changed file with 0 additions and 312 deletions.
312 changes: 0 additions & 312 deletions lumicks/pylake/tests/test_imaging_confocal_old/test_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,6 @@
from ..data.mock_confocal import generate_scan


def test_slicing(test_scans):
scan0 = test_scans["multiframe_poisson"]
assert scan0.num_frames == 10

def compare_frames(original_frames, new_scan):
assert new_scan.num_frames == len(original_frames)
for new_frame_index, index in enumerate(original_frames):
frame = scan0.get_image("red")[index]
new_frame = (
new_scan.get_image("red")[new_frame_index]
if new_scan.num_frames > 1
else new_scan.get_image("red")
)
np.testing.assert_equal(frame, new_frame)

compare_frames([0], scan0[0]) # first frame
compare_frames([9], scan0[-1]) # last frame
compare_frames([3], scan0[3]) # single frame
compare_frames([2, 3, 4], scan0[2:5]) # slice
compare_frames([0, 1, 2], scan0[:3]) # from beginning
compare_frames([8, 9], scan0[8:]) # until end
compare_frames([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], scan0[:]) # all
compare_frames([7], scan0[-3]) # negative index
compare_frames([0, 1, 2, 3], scan0[:-6]) # until negative index
compare_frames([5, 6, 7], scan0[5:-2]) # mixed sign indices
compare_frames([6, 7], scan0[-4:-2]) # full negative slice
compare_frames([2], scan0[2:3]) # slice to single frame
compare_frames([3, 4], scan0[2:6][1:3]) # iterative slicing

compare_frames([1, 2, 3, 4, 5, 6, 7, 8, 9], scan0[1:100]) # test clamping past the end
compare_frames([0, 1, 2, 3, 4, 5, 6, 7, 8], scan0[-100:9]) # test clamping past the beginning
compare_frames([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], scan0[-100:100]) # test clamping both dirs

# reverse slice (leads to empty scan)
assert not scan0[5:3]
assert not scan0[-2:-4]

# empty slice
assert not scan0[5:5]
assert not scan0[15:16]

# Verify no side effects
scan0[0]
assert scan0.num_frames == 10


def test_plotting(test_scans):
scan = test_scans["fast Y slow X multiframe"]
scan.plot(channel="blue")
Expand Down Expand Up @@ -248,269 +202,3 @@ def test_plot_single_channel_percentile_color_adjustment(test_scans):
np.testing.assert_allclose(plotted_image.get_array(), image[0])
np.testing.assert_allclose(plotted_image.get_clim(), [lb_abs, ub_abs])
plt.close(fig)


@pytest.mark.parametrize(
"x_min, x_max, y_min, y_max",
[
(None, None, None, None),
(None, 3, None, 2),
(0, None, 1, None),
(1, 4, 1, 5),
(1, 3, None, None),
(None, None, 1, 3),
(1, 2, 1, 2), # Single pixel
(1, 2, None, None),
(None, None, 1, 2),
],
)
def test_scan_cropping(x_min, x_max, y_min, y_max, test_scans):
valid_scans = [
"fast Y slow X multiframe",
"fast Y slow X",
"fast X slow Z multiframe",
"fast Y slow Z multiframe",
"red channel missing",
"rb channels missing",
]

for key in valid_scans:
scan = test_scans[key]

# Slice how we would with numpy
all_slices = tuple([slice(None), slice(y_min, y_max), slice(x_min, x_max)])
numpy_slice = all_slices[1:] if scan.num_frames == 1 else all_slices

cropped_scan = scan.crop_by_pixels(x_min, x_max, y_min, y_max)
np.testing.assert_allclose(cropped_scan.timestamps, scan.timestamps[numpy_slice])
np.testing.assert_allclose(cropped_scan.shape, scan.get_image("rgb")[numpy_slice].shape)
np.testing.assert_allclose(scan[all_slices].timestamps, scan.timestamps[numpy_slice])

for channel in ("rgb", "green"):
ref_img = scan.get_image(channel)[numpy_slice]
np.testing.assert_allclose(cropped_scan.get_image(channel), ref_img)
# Numpy array is given as Y, X, while number of pixels is given sorted by spatial axis
# i.e. X, Y
np.testing.assert_allclose(
np.flip(cropped_scan._num_pixels), cropped_scan.get_image("green").shape[-2:]
)


@pytest.mark.parametrize(
"all_slices",
[
[slice(None), slice(None), slice(None)],
[slice(None), slice(None, 3), slice(None, 2)],
[slice(None), slice(0, None), slice(1, None)],
[slice(None), slice(1, 4), slice(1, 5)],
[slice(None), slice(1, 3), slice(None)],
[slice(None), slice(None), slice(1, 3)],
[slice(None), slice(1, 2), slice(1, 2)], # Single pixel
[slice(None), slice(1, 2), slice(None)],
[slice(None), slice(None), slice(1, 2)],
[0], # Only indexing scans
[0, slice(1, 2), slice(1, 2)],
[-1, slice(1, 3), slice(None)],
[0, slice(None), slice(1, 2)],
[0, slice(1, 3)], # This tests a very specific corner case where _num_pixels could fail
],
)
def test_scan_get_item_slicing(all_slices, test_scans):
"""Test slicing, slicing is given as image, y, x"""

valid_scans = [
"fast Y slow X multiframe",
"fast Y slow X",
"fast X slow Z multiframe",
"fast Y slow Z multiframe",
"red channel missing",
"rb channels missing",
]

for key in valid_scans:
scan = test_scans[key]

# Slice how we would with numpy
slices = tuple(all_slices if scan.num_frames > 1 else all_slices[1:])
cropped_scan = scan[all_slices]
np.testing.assert_allclose(cropped_scan.timestamps, scan.timestamps[slices])
np.testing.assert_allclose(cropped_scan.shape, scan.get_image("rgb")[slices].shape)

for channel in ("rgb", "green"):
ref_img = scan.get_image(channel)[slices]
np.testing.assert_allclose(cropped_scan.get_image(channel), ref_img)

# Numpy array is given as Y, X, while number of pixels is given sorted by spatial axis
# i.e. X, Y
np.testing.assert_allclose(
np.flip(cropped_scan._num_pixels), cropped_scan.get_image("green").shape[-2:]
)


def test_slicing_cropping_separate_actions(test_scans):
"""Test whether cropping works in a sequential fashion"""
multi_frame, single_frame = (
generate_scan(
"test",
np.random.rand(num_frames, 5, 7),
[1, 1],
start=0,
dt=100,
samples_per_pixel=1,
line_padding=1,
)
for num_frames in (8, 1)
)

def assert_equal(first, second):
np.testing.assert_allclose(first.get_image("red"), second.get_image("red"))
np.testing.assert_allclose(first.get_image("rgb"), second.get_image("rgb"))
np.testing.assert_allclose(first.get_image("rgb").shape, second.get_image("rgb").shape)
assert first.num_frames == second.num_frames
assert first._num_pixels == second._num_pixels

assert_equal(multi_frame[1:3][:, 1:3, 2:3], multi_frame[1:3, 1:3, 2:3])
assert_equal(multi_frame[1][:, 1:3, 2:3], multi_frame[1, 1:3, 2:3])
assert_equal(multi_frame[:, 1:3, 2:3][3], multi_frame[3, 1:3, 2:3])
assert_equal(multi_frame[:, 1:, 2:][3], multi_frame[3, 1:, 2:])
assert_equal(multi_frame[:, :3, :3][3], multi_frame[3, :3, :3])
assert_equal(single_frame[0][:, 1:5, 2:6][:, 1:3, 2:8], single_frame[0, 2:4, 4:6])


def test_error_handling_multidim_indexing():
multi_frame, single_frame = (
generate_scan(
"test",
np.random.rand(num_frames, 5, 7),
[1, 1],
start=0,
dt=100,
samples_per_pixel=1,
line_padding=1,
)
for num_frames in (8, 1)
)

with pytest.raises(IndexError, match="Frame index out of range"):
multi_frame[8]
with pytest.raises(IndexError, match="Frame index out of range"):
multi_frame[-9]
with pytest.raises(IndexError, match="Frame index out of range"):
single_frame[1]
with pytest.raises(
IndexError, match="Scalar indexing is not supported for spatial coordinates"
):
multi_frame[0, 4, 3]


@pytest.mark.parametrize(
"name",
[
"fast Y slow X multiframe",
"fast Y slow X",
"fast X slow Z multiframe",
"fast Y slow Z multiframe",
"red channel missing",
"rb channels missing",
],
)
def test_empty_slices_due_to_out_of_bounds(name, test_scans):
shape = test_scans[name][0].get_image("green").shape
with pytest.raises(NotImplementedError, match="Slice is empty."):
test_scans[name][0][0, shape[0] : shape[0] + 10, 0:2]
with pytest.raises(NotImplementedError, match="Slice is empty."):
test_scans[name][0][0, shape[0] : shape[0] + 10]
with pytest.raises(NotImplementedError, match="Slice is empty."):
test_scans[name][0][0, :, shape[1] : shape[1] + 10]
with pytest.raises(NotImplementedError, match="Slice is empty."):
test_scans[name][0][0, :, -10:0]


def test_slice_by_list_disallowed(test_scans):
with pytest.raises(IndexError, match="Indexing by list is not allowed"):
test_scans["fast Y slow X multiframe"][[0, 1], :, :]

with pytest.raises(IndexError, match="Indexing by list is not allowed"):
test_scans["fast Y slow X multiframe"][:, [0, 1], :]

class Dummy:
pass

with pytest.raises(IndexError, match="Indexing by Dummy is not allowed"):
test_scans["fast Y slow X multiframe"][Dummy(), :, :]

with pytest.raises(IndexError, match="Slicing by Dummy is not supported"):
test_scans["fast Y slow X multiframe"][Dummy() : Dummy(), :, :]


def test_crop_missing_channel(test_scans):
"""Make sure that missing channels are handled appropriately when cropping"""
np.testing.assert_equal(
test_scans["rb channels missing"][:, 0:2, 1:3].get_image("red"), np.zeros((2, 2))
)


def test_scan_slicing_by_time():
start = _FIRST_TIMESTAMP + 100
num_frames = 10
line_padding = 10
dt = int(1e7)
multi_frame = generate_scan(
"test",
np.random.randint(0, 10, size=(num_frames, 2, 2)),
[1, 1],
start=start - line_padding * dt,
dt=dt,
samples_per_pixel=15,
line_padding=line_padding,
)

# We deliberately shift the start to the start of the first frame (post padding). This makes
# the tests much more readable.
multi_frame = multi_frame[:]
ts = multi_frame.frame_timestamp_ranges()

def compare_frames(frames, new_scan):
assert new_scan.num_frames == len(frames)
for new_frame_index, index in enumerate(frames):
frame = multi_frame[index].get_image("rgb")
new_frame = new_scan[new_frame_index].get_image("rgb")
np.testing.assert_equal(frame, new_frame)

compare_frames([2], multi_frame["2s":"2.81s"])
compare_frames([2], multi_frame["2s":"3.8s"])
compare_frames([2, 3], multi_frame["2s":"3.81s"])
compare_frames([1, 2], multi_frame["1s":"3s"])
compare_frames([2, 3, 4, 5, 6, 7, 8, 9], multi_frame["2s":]) # until end
compare_frames([3, 4, 5, 6, 7, 8, 9], multi_frame["2.1s":]) # from beginning
compare_frames([0, 1], multi_frame[:"1.81s"]) # until end
compare_frames([0], multi_frame[:"1.8s"]) # from beginning

compare_frames([3, 4, 5], multi_frame["2s":"5.81s"]["1s":"3.81s"]) # iterative
compare_frames([3, 4], multi_frame["2s":"5.81s"]["1s":"3.80s"]) # iterative
compare_frames([3, 4, 5], multi_frame["2s":"5.81s"]["1s":]) # iterative

# Note that the from-end tests are different than the ones on correlatedstacks because the mock
# scan has the dead time half on the end of this frame, and half before this frame.
# The stop time of this scan is at 10 seconds, the last frame runs from 9 to 9.8 seconds, this
# means that to chop off the last two, we need to go -1.2 from end.
compare_frames([0, 1, 2, 3, 4, 5, 6, 7, 8], multi_frame[:"-1.199s"]) # negative indexing
compare_frames([0, 1, 2, 3, 4, 5, 6, 7], multi_frame[:"-1.2s"]) # negative indexing with time
compare_frames([8, 9], multi_frame["-2s":]) # negative indexing with time
compare_frames([9], multi_frame["-1.79s":]) # negative indexing with time
compare_frames([2, 3, 4], multi_frame["2s":"5.81s"][:"-1.199s"]) # iterative with from end
compare_frames([2, 3], multi_frame["2s":"5.81s"][:"-1.2s"]) # iterative with from end

# Slice by timestamps
compare_frames([2, 3], multi_frame[start + int(2e9) : start + int(4e9)])
compare_frames([2, 3], multi_frame[start + int(2e9) : start + int(4.8e9)])
compare_frames([2, 3, 4], multi_frame[start + int(2e9) : start + int(4.81e9)])
compare_frames([0, 1, 2, 3, 4], multi_frame[: (start + int(4.81e9))])
compare_frames([5, 6, 7, 8, 9], multi_frame[(start + int(5e9)) :])
compare_frames(
[2, 3, 4], multi_frame[start + int(2e9) : start + int(4.81e9)][start : start + int(100e9)]
)
compare_frames(
[3],
multi_frame[start + int(2e9) : start + int(4.81e9)][start + int(3e9) : start + int(3.81e9)],
)

0 comments on commit 03249be

Please sign in to comment.