Skip to content

Commit

Permalink
Fix min_time_point with link metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
rutgerkok committed Nov 25, 2024
1 parent 5f8aa54 commit 5f6faaa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
19 changes: 11 additions & 8 deletions organoid_tracker/imaging/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")
Expand Down
22 changes: 22 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

0 comments on commit 5f6faaa

Please sign in to comment.