Skip to content

Commit f209b52

Browse files
committed
preserve query ordering
1 parent 9b2787b commit f209b52

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

viscy/data/triplet.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def _sample_positives(self, anchor_rows: pd.DataFrame) -> pd.DataFrame:
210210
"""Select a positive sample from the same track in the next time point."""
211211
query = anchor_rows[["global_track_id", "t"]].copy()
212212
query["t"] += self.time_interval
213-
return self.tracks.merge(query, on=["global_track_id", "t"], how="inner")
213+
return query.merge(self.tracks, on=["global_track_id", "t"], how="inner")
214214

215215
def _sample_negative(self, anchor_row: pd.Series) -> pd.Series:
216216
"""Select a negative sample from a different track in the next time point
@@ -275,7 +275,12 @@ def __getitems__(self, indices: list[int]) -> list[TripletSample]:
275275
positive_norms = anchor_norms
276276
else:
277277
positive_rows = self._sample_positives(anchor_rows)
278-
assert len(positive_rows) == len(anchor_rows)
278+
anchor_ids = anchor_rows["global_track_id"]
279+
pos_ids = positive_rows["global_track_id"]
280+
assert np.all(anchor_ids.values == pos_ids.values), (
281+
anchor_ids,
282+
pos_ids,
283+
)
279284
positive_patches, positive_norms = self._slice_patches(positive_rows)
280285
if self.positive_transform:
281286
positive_patches = _transform_channel_wise(
@@ -447,11 +452,20 @@ def _align_tracks_tables_with_positions(
447452
images_plate = open_ome_zarr(self.data_path)
448453
for well in _filter_wells(images_plate, include_wells=self._include_wells):
449454
for fov in _filter_fovs(well, exclude_fovs=self._exclude_fovs):
450-
positions.append(fov)
451-
tracks_df = pd.read_csv(
452-
next((self.tracks_path / fov.zgroup.name.strip("/")).glob("*.csv"))
453-
).astype(int)
454-
tracks_tables.append(tracks_df)
455+
try:
456+
tracks_df = pd.read_csv(
457+
next(
458+
(self.tracks_path / fov.zgroup.name.strip("/")).glob(
459+
"*.csv"
460+
)
461+
)
462+
).astype(int)
463+
positions.append(fov)
464+
tracks_tables.append(tracks_df)
465+
except StopIteration:
466+
_logger.warning(
467+
f"No tracks found for FOV {fov.zgroup.name}, skipping."
468+
)
455469

456470
return positions, tracks_tables
457471

0 commit comments

Comments
 (0)