From 5f6faaa5d5e08ec1a8deb7edf2111ab82fd60276 Mon Sep 17 00:00:00 2001 From: Rutger Kok Date: Mon, 25 Nov 2024 11:43:59 +0100 Subject: [PATCH] Fix min_time_point with link metadata --- organoid_tracker/imaging/io.py | 19 +++++++++++-------- tests/test_io.py | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/organoid_tracker/imaging/io.py b/organoid_tracker/imaging/io.py index 05ac93cd..13f512c5 100644 --- a/organoid_tracker/imaging/io.py +++ b/organoid_tracker/imaging/io.py @@ -310,22 +310,23 @@ def _parse_tracks_and_meta_format(experiment: Experiment, tracks_json: List[Dict coords_xyz_px = track_json["coords_xyz_px"] positions_of_track = list() - for i, raw_position in enumerate(coords_xyz_px): - time_point_number = time_point_number_start + i - if time_point_number < min_time_point or time_point_number > max_time_point: - continue - position_previous_track = Position(*raw_position, time_point_number=time_point_number_start + i) - positions_of_track.append(position_previous_track) + min_index = max(0, min_time_point - time_point_number_start) + max_index = min(len(coords_xyz_px), max_time_point - time_point_number_start) + for i in range(min_index, max_index): + position = Position(*coords_xyz_px[i], time_point_number=time_point_number_start + i) + positions_of_track.append(position) track = LinkingTrack(positions_of_track) links.add_track(track) # Handle link metadata if "link_meta" in track_json: for metadata_key, metadata_values in track_json["link_meta"].items(): - for i, value in enumerate(metadata_values): + for i in range(min_index, max_index - 1): + value = metadata_values[i] if value is None: continue - link_data.set_link_data(positions_of_track[i], positions_of_track[i + 1], metadata_key, value) + + link_data.set_link_data(positions_of_track[i - min_index], positions_of_track[i - min_index + 1], metadata_key, value) # Handle lineage metadata if "lineage_meta" in track_json: @@ -337,6 +338,8 @@ def _parse_tracks_and_meta_format(experiment: Experiment, tracks_json: List[Dict if "coords_xyz_px_before" not in track_json: continue time_point_number_start = track_json["time_point_start"] + if min_time_point >= time_point_number_start: + continue # Ignore tracks that start at/after the minimum time point - can't add metadata to before position_first = Position(*track_json["coords_xyz_px"][0], time_point_number=time_point_number_start) metadata = track_json.get("link_meta_before") diff --git a/tests/test_io.py b/tests/test_io.py index 181c28a1..bd2ceedb 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -160,3 +160,25 @@ def test_loading_old_format(self): # Link meta self.assertEqual(4, experiment.link_data.get_link_data(Position(4, 5, 6, time_point_number=2), Position(7, 8, 9, time_point_number=3), "test_key")) + + def test_loading_with_min_time_point(self): + experiment = Experiment() + position_1 = Position(1, 2, 3, time_point_number=1) + position_2 = Position(4, 5, 6, time_point_number=2) + position_3 = Position(7, 8, 9, time_point_number=3) + experiment.positions.add(position_1) + experiment.positions.add(position_2) + experiment.positions.add(position_3) + experiment.links.add_link(position_1, position_2) + experiment.links.add_link(position_2, position_3) + experiment.link_data.set_link_data(position_1, position_2, "test_key", 1) + experiment.link_data.set_link_data(position_2, position_3, "test_key", 2) + + with TemporaryDirectory() as directory: + file = os.path.join(directory, "test." + io.FILE_EXTENSION) + io.save_data_to_json(experiment, file, write_new_format=True) + + experiment = io.load_data_file(file, min_time_point=2) + self.assertEqual(2, len(experiment.positions)) + self.assertEqual(1, len(experiment.links)) + self.assertEqual(2, experiment.link_data.get_link_data(position_2, position_3, "test_key"))