Skip to content

Commit

Permalink
test_imaging_confocal_old: remove reworked downsampling tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rpauszek committed Oct 27, 2023
1 parent 4147b13 commit 47a15ae
Showing 1 changed file with 0 additions and 234 deletions.
234 changes: 0 additions & 234 deletions lumicks/pylake/tests/test_imaging_confocal_old/test_kymo.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,27 +156,6 @@ def test_deprecated_plotting(test_kymos):
kymo.plot("rgb", None, axes=None)


def test_plotting_with_force_downsampling(kymo_h5_file):
f = pylake.File.from_h5py(kymo_h5_file)
kymo = f.kymos["Kymo1"]
ranges = kymo.line_timestamp_ranges(include_dead_time=False)

# Check timestamps for downsampled channel
# Note that if the kymo would have the same samples per pixel, a simple:
# np.testing.assert_allclose(np.mean(kymo.timestamps, axis=0)[:-1], ds.timestamps[:-1])
# would have sufficed. However, in this case we need the following solution:
ds = f.force2x.downsampled_over(ranges)
min_ts, max_ts = (reduce(kymo._timestamps(reduce), axis=0) for reduce in (np.min, np.max))
target_timestamps = np.array(
[
np.mean(kymo.infowave[int(start) : int(stop) + 1].timestamps)
for start, stop in zip(min_ts, max_ts)
]
)
np.testing.assert_allclose(ds.timestamps, target_timestamps)
np.testing.assert_allclose(ds.data, [30, 30, 10, 10])


def test_plotting_with_force(kymo_h5_file):
f = pylake.File.from_h5py(kymo_h5_file)
kymo = f.kymos["Kymo1"]
Expand All @@ -187,34 +166,6 @@ def test_plotting_with_force(kymo_h5_file):
np.testing.assert_allclose(np.sort(plt.ylim()), [10, 30])


def test_downsample_channel_downsampled_kymo(kymo_h5_file):
f = pylake.File.from_h5py(kymo_h5_file)
kymo = f.kymos["Kymo1"]
kymo_ds = kymo.downsampled_by(position_factor=2)

ds = f.force2x.downsampled_over(kymo_ds.line_timestamp_ranges(include_dead_time=False))
np.testing.assert_allclose(ds.data, [30, 30, 10, 10])

# Downsampling by a factor of two in position means that the last pixel will be dropped
# from this kymo when downsampling (as it is 5 pixels wide). This is why the before last
# sample is taken when determining the maxima.
mins = kymo._timestamp_factory(kymo, np.min)[0, :]
maxs = kymo._timestamp_factory(kymo, np.max)[-2, :]
np.testing.assert_allclose(ds.timestamps, (maxs + mins) / 2)

# Downsampling by a factor of five in position means no pixel will be dropped.
kymo_ds = kymo.downsampled_by(position_factor=5)
ds = f.force2x.downsampled_over(kymo_ds.line_timestamp_ranges(include_dead_time=False))
mins = kymo._timestamp_factory(kymo, np.min)[0, :]
maxs = kymo._timestamp_factory(kymo, np.max)[-1, :]
np.testing.assert_allclose(ds.timestamps, (maxs + mins) / 2)

# Down-sampling by time should invalidate plot_with_force as it would correspond to
# non-contiguous sampling
with pytest.raises(NotImplementedError, match="Line timestamp ranges are no longer available"):
kymo.downsampled_by(time_factor=2).plot_with_force("1x", "red")


def test_regression_plot_with_force(kymo_h5_file):
# Plot_with_force used to fail when the last line of a kymograph was incomplete. The reason for
# this was that the last few timestamps on the last line had zero as their timestamp. This meant
Expand Down Expand Up @@ -312,191 +263,6 @@ def test_export_tiff(tmp_path, test_kymos, grab_tiff_tags):
assert tags["ResolutionUnit"] == 3 # 3 = Centimeter


def test_downsampled_kymo():
image = np.array(
[
[0, 12, 0, 12, 0, 6, 0],
[0, 0, 0, 0, 0, 6, 0],
[12, 0, 0, 0, 12, 6, 0],
[0, 12, 12, 12, 0, 6, 0],
],
dtype=np.uint8,
)

kymo = generate_kymo(
"Mock",
image,
pixel_size_nm=1,
start=with_offset(0),
dt=7,
samples_per_pixel=5,
line_padding=2,
)

kymo_ds = kymo.downsampled_by(time_factor=2)
ds = np.array(
[
[12, 12, 6],
[0, 0, 6],
[12, 0, 18],
[12, 24, 6],
],
dtype=np.uint8,
)

assert kymo_ds.name == "Mock"
np.testing.assert_allclose(kymo_ds.get_image("red"), ds)
np.testing.assert_allclose(kymo_ds.shape, kymo_ds.get_image("rgb").shape)
np.testing.assert_allclose(kymo_ds.start, with_offset(0))
np.testing.assert_allclose(kymo_ds.pixelsize_um, 1 / 1000)
np.testing.assert_allclose(kymo_ds.pixelsize, 1 / 1000)
np.testing.assert_allclose(kymo_ds.line_time_seconds, 2 * 7 * (5 * 4 + 2 + 2) / 1e9)
assert not kymo_ds.contiguous

with pytest.raises(
NotImplementedError,
match=re.escape("Per-pixel timestamps are no longer available after downsampling"),
):
kymo_ds.timestamps

with pytest.raises(
NotImplementedError,
match=re.escape("No motion blur constant was defined for this kymograph"),
):
kymo_ds.motion_blur_constant

# Verify that we can pass a different reduce function
np.testing.assert_allclose(
kymo.downsampled_by(time_factor=2, reduce=np.mean).get_image("red"), ds / 2
)

with pytest.raises(
NotImplementedError,
match="Per-pixel timestamps are no longer available after downsampling",
):
kymo_ds.pixel_time_seconds


def test_downsampled_kymo_position():
"""Test downsampling over the spatial axis"""
image = np.array(
[
[0, 12, 0, 12, 0, 6, 0],
[0, 0, 0, 0, 0, 6, 0],
[12, 0, 0, 0, 12, 6, 0],
[0, 12, 12, 12, 0, 6, 0],
[0, 12, 12, 12, 0, 6, 0],
],
dtype=np.uint8,
)

kymo = generate_kymo(
"Mock",
image,
pixel_size_nm=1,
start=with_offset(100),
dt=5,
samples_per_pixel=5,
line_padding=2,
)

kymo_ds = kymo.downsampled_by(position_factor=2)
ds = np.array([[0, 12, 0, 12, 0, 12, 0], [12, 12, 12, 12, 12, 12, 0]], dtype=np.uint8)
ds_ts = with_offset(
[
[132.5, 277.5, 422.5, 567.5, 712.5, 857.5, 1002.5],
[182.5, 327.5, 472.5, 617.5, 762.5, 907.5, 1052.5],
],
)

assert kymo_ds.name == "Mock"
np.testing.assert_allclose(kymo_ds.get_image("red"), ds)
np.testing.assert_equal(kymo_ds.timestamps, ds_ts)
np.testing.assert_allclose(kymo_ds.start, with_offset(100))
np.testing.assert_allclose(kymo_ds.pixelsize_um, 2 / 1000)
np.testing.assert_allclose(kymo_ds.pixelsize, 2 / 1000)
np.testing.assert_allclose(kymo_ds.line_time_seconds, kymo.line_time_seconds)
assert kymo_ds.contiguous

# We lost one line while downsampling
np.testing.assert_allclose(kymo_ds.size_um[0], kymo.size_um[0] - kymo.pixelsize_um[0])

np.testing.assert_allclose(kymo_ds.pixel_time_seconds, kymo.pixel_time_seconds * 2)


def test_downsampled_kymo_both_axes():
image = np.array(
[
[0, 12, 0, 12, 0, 6, 0],
[0, 0, 0, 0, 0, 6, 0],
[12, 0, 0, 0, 12, 6, 0],
[0, 12, 12, 12, 0, 6, 0],
[0, 12, 12, 12, 0, 6, 0],
],
dtype=np.uint8,
)

kymo = generate_kymo(
"Mock", image, pixel_size_nm=1, start=100, dt=5, samples_per_pixel=5, line_padding=2
)
ds = np.array([[12, 12, 12], [24, 24, 24]], dtype=np.uint8)

downsampled_kymos = [
kymo.downsampled_by(time_factor=2, position_factor=2),
# Test whether sequential downsampling works out correctly as well
kymo.downsampled_by(position_factor=2).downsampled_by(time_factor=2),
kymo.downsampled_by(time_factor=2).downsampled_by(position_factor=2),
]

for kymo_ds in downsampled_kymos:
assert kymo_ds.name == "Mock"
np.testing.assert_allclose(kymo_ds.get_image("red"), ds)
np.testing.assert_allclose(kymo_ds.get_image("green"), np.zeros(ds.shape)) # missing
np.testing.assert_allclose(kymo_ds.start, 100)
np.testing.assert_allclose(kymo_ds.pixelsize_um, 2 / 1000)
np.testing.assert_allclose(kymo_ds.pixelsize, 2 / 1000)
np.testing.assert_allclose(kymo_ds.line_time_seconds, 2 * 5 * (5 * 5 + 2 + 2) / 1e9)
assert not kymo_ds.contiguous
with pytest.raises(
NotImplementedError,
match=re.escape("Per-pixel timestamps are no longer available after downsampling"),
):
kymo_ds.timestamps


def test_side_no_side_effects_downsampling():
"""Test whether downsampling doesn't have side effects on the original kymo"""
image = np.array(
[
[0, 12, 0, 12, 0, 6, 0],
[0, 0, 0, 0, 0, 6, 0],
[12, 0, 0, 0, 12, 6, 0],
[0, 12, 12, 12, 0, 6, 0],
[0, 12, 12, 12, 0, 6, 0],
],
dtype=np.uint8,
)

kymo = generate_kymo(
"Mock",
image,
pixel_size_nm=1,
start=with_offset(100),
dt=5,
samples_per_pixel=5,
line_padding=2,
)
timestamps = kymo.timestamps.copy()
downsampled_kymos = kymo.downsampled_by(time_factor=2, position_factor=2)

np.testing.assert_allclose(kymo.get_image("red"), image)
np.testing.assert_allclose(kymo.start, with_offset(100))
np.testing.assert_allclose(kymo.pixelsize_um, 1 / 1000)
np.testing.assert_allclose(kymo.line_time_seconds, 5 * (5 * 5 + 2 + 2) / 1e9)
np.testing.assert_equal(kymo.timestamps, timestamps)
assert kymo.contiguous


def test_downsampled_slice():
"""There was a regression bug that if a Kymo was downsampled and then sliced, it would undo the
downsampling. For now, we just flag it as not implemented behaviour."""
Expand Down

0 comments on commit 47a15ae

Please sign in to comment.