diff --git a/lumicks/pylake/kymo.py b/lumicks/pylake/kymo.py index a5bce449c..35c9aa947 100644 --- a/lumicks/pylake/kymo.py +++ b/lumicks/pylake/kymo.py @@ -334,7 +334,7 @@ def duration(self): def pixelsize(self): """Returns a `List` of axes dimensions in calibrated units. The length of the list corresponds to the number of scan axes.""" - return [self._calibration.value] + return [self._calibration.pixelsize] def plot( self, @@ -386,7 +386,7 @@ def plot( image = self._get_plot_data(channel, adjustment) - size_calibrated = self._calibration.value * self._num_pixels[0] + size_calibrated = self._calibration.pixelsize * self._num_pixels[0] default_kwargs = dict( # With origin set to upper (default) bounds should be given as (0, n, n, 0) @@ -1016,12 +1016,11 @@ def calibrate_to_kbp(self, length_kbp, *, start=None, end=None): if (start is None) ^ (end is None): raise ValueError("Both start and end points of the tether must be supplied.") - if start is not None: - if end < start: - raise ValueError("end must be larger than start.") - kbp_per_pixel = length_kbp / (end - start) * self.pixelsize_um[0] - else: - kbp_per_pixel = length_kbp / self._num_pixels[0] + kbp_per_pixel = ( + length_kbp / (end - start) * self.pixelsize_um[0] + if start is not None + else length_kbp / self._num_pixels[0] + ) pixel_origin = start / self.pixelsize_um[0] if start is not None else 0.0 result = copy(self) @@ -1123,7 +1122,7 @@ def label(self): @dataclass(frozen=True) class PositionCalibration: unit: PositionUnit = PositionUnit.pixel - value: float = 1.0 + scale: float = 1.0 origin: float = 0.0 def __post_init__(self): @@ -1132,11 +1131,15 @@ def __post_init__(self): def from_pixels(self, pixels): """Convert coordinates from pixel values to calibrated values""" - return self.value * (np.array(pixels) - self.origin) + return self.scale * (np.array(pixels) - self.origin) def to_pixels(self, calibrated): """Convert coordinates from calibrated values to pixel values""" - return np.array(calibrated) / self.value + self.origin + return np.array(calibrated) / self.scale + self.origin + + @property + def pixelsize(self): + return np.abs(self.scale) @property def unit_label(self): @@ -1146,7 +1149,7 @@ def downsample(self, factor): return ( self if self.unit == PositionUnit.pixel - else PositionCalibration(self.unit, self.value * factor) + else PositionCalibration(self.unit, self.scale * factor) ) diff --git a/lumicks/pylake/kymotracker/tests/test_greedy_algorithm.py b/lumicks/pylake/kymotracker/tests/test_greedy_algorithm.py index 86515f415..5245d4173 100644 --- a/lumicks/pylake/kymotracker/tests/test_greedy_algorithm.py +++ b/lumicks/pylake/kymotracker/tests/test_greedy_algorithm.py @@ -171,3 +171,42 @@ def test_default_parameters(kymo_pixel_calibrations): with np.testing.assert_raises(AssertionError): for ref, track in zip(ref_tracks, tracks): np.testing.assert_allclose(ref.position, track.position) + + +@pytest.mark.parametrize("tether_len,start,end", [(10, 0.18, 0.62), (48.502, 0.03, 0.94)]) +def test_track_calibrated_flipped(tether_len, start, end): + """test that tracking a calibrated kymo with tether end < tether start yields the same + coordinates as tracking the flipped tether with end > start. + """ + + image = np.zeros((20, 25)) + + # ([time, position], ...) + ref_tracks = [ + ([1, 5], [2, 6], [3, 7]), + ([5, 15], [6, 15], [7, 14], [8, 15], [9, 16]), + ] + for coords in ref_tracks: + for t, p in coords: + image[p, t] = 10 + + kymo = _kymo_from_array(image, "g", line_time_seconds=0.1, pixel_size_um=0.050) + kymo_flipped = kymo.flip() + + # start, end = 0.18, 0.62 + len_um = kymo._calibration.from_pixels(kymo._num_pixels[0] - 1) + end_flipped = len_um - end + start_flipped = len_um - start + + # tether_len = 10 + kymo = kymo.calibrate_to_kbp(tether_len, start=start, end=end) + kymo_flipped = kymo_flipped.calibrate_to_kbp(tether_len, start=start_flipped, end=end_flipped) + + params = dict(pixel_threshold=5, window=3) + tracks = track_greedy(kymo, "green", track_width=3 * kymo._calibration.pixelsize, **params) + tracks_flipped = track_greedy( + kymo_flipped, "green", track_width=3 * kymo_flipped._calibration.pixelsize, **params + ) + + for track, track_flipped in zip(tracks, tracks_flipped): + np.testing.assert_allclose(track.position, track_flipped.position) diff --git a/lumicks/pylake/tests/test_imaging_confocal/test_kymo_transforms.py b/lumicks/pylake/tests/test_imaging_confocal/test_kymo_transforms.py index 1a6867170..9bc9f8df3 100644 --- a/lumicks/pylake/tests/test_imaging_confocal/test_kymo_transforms.py +++ b/lumicks/pylake/tests/test_imaging_confocal/test_kymo_transforms.py @@ -13,19 +13,19 @@ def test_calibrate_to_kbp(test_kymo): # test that default calibration is in microns assert kymo._calibration.unit == PositionUnit.um - assert kymo._calibration.value == 0.1 + assert kymo._calibration.pixelsize == 0.1 # test that calibration is stored as kilobase-pairs assert kymo_bp._calibration.unit == PositionUnit.kbp - np.testing.assert_allclose(kymo_bp._calibration.value, length_kbp / n_pixels) + np.testing.assert_allclose(kymo_bp._calibration.pixelsize, length_kbp / n_pixels) # test conversion from microns to calibration units np.testing.assert_allclose( - kymo._calibration.value * n_pixels, + kymo._calibration.pixelsize * n_pixels, ref.metadata.pixelsize_um[0] * n_pixels, ) np.testing.assert_allclose(kymo.pixelsize, ref.metadata.pixelsize_um[0]) - np.testing.assert_allclose(kymo_bp._calibration.value * n_pixels, length_kbp) + np.testing.assert_allclose(kymo_bp._calibration.pixelsize * n_pixels, length_kbp) np.testing.assert_allclose(kymo_bp.pixelsize, length_kbp / n_pixels) start = 0.12 @@ -33,12 +33,9 @@ def test_calibrate_to_kbp(test_kymo): n_pixels_tether = (end - start) / ref.metadata.pixelsize_um[0] kymo_bp = kymo.calibrate_to_kbp(length_kbp, start=start, end=end) - np.testing.assert_allclose(kymo_bp._calibration.value * n_pixels_tether, length_kbp) + np.testing.assert_allclose(kymo_bp._calibration.pixelsize * n_pixels_tether, length_kbp) np.testing.assert_allclose(kymo_bp.pixelsize, length_kbp / n_pixels_tether) - with pytest.raises(ValueError, match="end must be larger than start."): - kymo.calibrate_to_kbp(length_kbp, start=end, end=start) - with pytest.raises(RuntimeError, match="kymo is already calibrated in base pairs."): kymo_bp.calibrate_to_kbp(10) @@ -87,8 +84,8 @@ def test_calibrate_sliced_cropped(test_kymo): np.testing.assert_allclose(kymo_bp.pixelsize[0], cropped_kymo_bp.pixelsize[0]) # but will change total length np.testing.assert_allclose( - kymo_bp._calibration.value * n_cropped_pixels, - cropped_kymo_bp._calibration.value * cropped_kymo_bp._num_pixels[0], + kymo_bp._calibration.pixelsize * n_cropped_pixels, + cropped_kymo_bp._calibration.pixelsize * cropped_kymo_bp._num_pixels[0], ) @@ -143,23 +140,31 @@ def test_position_unit(): def test_enum_in_calibration(): with pytest.raises(TypeError, match="`unit` must be a PositionUnit instance"): - PositionCalibration("kbp", value=0.42) + PositionCalibration("kbp", scale=0.42) - c = PositionCalibration(PositionUnit.um, value=0.42) + c = PositionCalibration(PositionUnit.um, scale=0.42) assert c.unit_label == PositionUnit.um.label def test_coordinate_transforms(): px_coord = [0, 1.2, 3.14, 85] - c = PositionCalibration(PositionUnit.kbp, value=0.42) + c = PositionCalibration(PositionUnit.kbp, scale=0.42) kbp_coord = [0, 0.504, 1.3188, 35.7] transformed = c.from_pixels(px_coord) np.testing.assert_allclose(kbp_coord, transformed) np.testing.assert_allclose(px_coord, c.to_pixels(transformed)) - c = PositionCalibration(PositionUnit.kbp, value=0.42, origin=2.0) - kbp_coord = [-0.84, -0.336, 0.4788, 34.86] + c = PositionCalibration(PositionUnit.kbp, scale=0.42, origin=2.0) + kbp_coord = np.array([-0.84, -0.336, 0.4788, 34.86]) transformed = c.from_pixels(px_coord) np.testing.assert_allclose(kbp_coord, transformed) np.testing.assert_allclose(px_coord, c.to_pixels(transformed)) + + c_flipped = PositionCalibration(PositionUnit.kbp, scale=-0.42, origin=2.0) + transformed = c_flipped.from_pixels(px_coord) + np.testing.assert_allclose(-kbp_coord, transformed) + np.testing.assert_allclose(px_coord, c_flipped.to_pixels(transformed)) + + assert c.scale == -c_flipped.scale + assert c.pixelsize == c_flipped.pixelsize