|
2 | 2 | from iohub import open_ome_zarr |
3 | 3 | from pytest import mark |
4 | 4 |
|
5 | | -from viscy.data.triplet import TripletDataModule |
| 5 | +from viscy.data.triplet import TripletDataModule, TripletDataset |
6 | 6 |
|
7 | 7 |
|
8 | 8 | @mark.parametrize("include_wells", [None, ["A/1", "A/2", "B/1"]]) |
@@ -109,3 +109,244 @@ def test_datamodule_z_window_size( |
109 | 109 | expected_z_shape, |
110 | 110 | *yx_patch_size, |
111 | 111 | ) |
| 112 | + |
| 113 | + |
| 114 | +def test_filter_anchors_time_interval_any( |
| 115 | + preprocessed_hcs_dataset, tracks_with_gaps_dataset |
| 116 | +): |
| 117 | + """Test that time_interval='any' returns all tracks unchanged.""" |
| 118 | + with open_ome_zarr(preprocessed_hcs_dataset) as dataset: |
| 119 | + channel_names = dataset.channel_names |
| 120 | + positions = list(dataset.positions()) |
| 121 | + |
| 122 | + # Create dataset with time_interval="any" |
| 123 | + tracks_tables = [] |
| 124 | + for fov_name, _ in positions: |
| 125 | + tracks_df = pd.read_csv( |
| 126 | + next((tracks_with_gaps_dataset / fov_name).glob("*.csv")) |
| 127 | + ).astype(int) |
| 128 | + tracks_tables.append(tracks_df) |
| 129 | + |
| 130 | + total_tracks = sum(len(df) for df in tracks_tables) |
| 131 | + |
| 132 | + ds = TripletDataset( |
| 133 | + positions=[pos for _, pos in positions], |
| 134 | + tracks_tables=tracks_tables, |
| 135 | + channel_names=channel_names, |
| 136 | + initial_yx_patch_size=(64, 64), |
| 137 | + z_range=slice(4, 9), |
| 138 | + fit=True, |
| 139 | + time_interval="any", |
| 140 | + ) |
| 141 | + |
| 142 | + # Should return all tracks |
| 143 | + assert len(ds.valid_anchors) == total_tracks |
| 144 | + |
| 145 | + |
| 146 | +def test_filter_anchors_time_interval_1( |
| 147 | + preprocessed_hcs_dataset, tracks_with_gaps_dataset |
| 148 | +): |
| 149 | + """Test filtering with time_interval=1.""" |
| 150 | + with open_ome_zarr(preprocessed_hcs_dataset) as dataset: |
| 151 | + channel_names = dataset.channel_names |
| 152 | + positions = list(dataset.positions()) |
| 153 | + |
| 154 | + tracks_tables = [] |
| 155 | + for fov_name, _ in positions: |
| 156 | + tracks_df = pd.read_csv( |
| 157 | + next((tracks_with_gaps_dataset / fov_name).glob("*.csv")) |
| 158 | + ).astype(int) |
| 159 | + tracks_tables.append(tracks_df) |
| 160 | + |
| 161 | + ds = TripletDataset( |
| 162 | + positions=[pos for _, pos in positions], |
| 163 | + tracks_tables=tracks_tables, |
| 164 | + channel_names=channel_names, |
| 165 | + initial_yx_patch_size=(64, 64), |
| 166 | + z_range=slice(4, 9), |
| 167 | + fit=True, |
| 168 | + time_interval=1, |
| 169 | + ) |
| 170 | + |
| 171 | + # Check expected anchors per FOV/track |
| 172 | + valid_anchors = ds.valid_anchors |
| 173 | + |
| 174 | + # FOV A/1/0, Track 0: t=[0,1,2,3] -> valid anchors at t=[0,1,2] |
| 175 | + fov_a10_track0 = valid_anchors[ |
| 176 | + (valid_anchors["fov_name"] == "A/1/0") & (valid_anchors["track_id"] == 0) |
| 177 | + ] |
| 178 | + assert set(fov_a10_track0["t"]) == {0, 1, 2} |
| 179 | + |
| 180 | + # FOV A/1/0, Track 1: t=[0,1] -> valid anchor at t=[0] |
| 181 | + fov_a10_track1 = valid_anchors[ |
| 182 | + (valid_anchors["fov_name"] == "A/1/0") & (valid_anchors["track_id"] == 1) |
| 183 | + ] |
| 184 | + assert set(fov_a10_track1["t"]) == {0} |
| 185 | + |
| 186 | + # FOV A/1/1, Track 0: t=[0,1,3] -> valid anchor at t=[0] only (t=1 has no t+1=2) |
| 187 | + fov_a11_track0 = valid_anchors[ |
| 188 | + (valid_anchors["fov_name"] == "A/1/1") & (valid_anchors["track_id"] == 0) |
| 189 | + ] |
| 190 | + assert set(fov_a11_track0["t"]) == {0} |
| 191 | + |
| 192 | + # FOV A/1/1, Track 1: t=[0,2,4] -> no valid anchors (gaps of 2, no consecutive t+1) |
| 193 | + fov_a11_track1 = valid_anchors[ |
| 194 | + (valid_anchors["fov_name"] == "A/1/1") & (valid_anchors["track_id"] == 1) |
| 195 | + ] |
| 196 | + assert len(fov_a11_track1) == 0 |
| 197 | + |
| 198 | + # FOV A/2/0, Track 0: t=[0] -> no valid anchors (no t+1) |
| 199 | + fov_a20_track0 = valid_anchors[ |
| 200 | + (valid_anchors["fov_name"] == "A/2/0") & (valid_anchors["track_id"] == 0) |
| 201 | + ] |
| 202 | + assert len(fov_a20_track0) == 0 |
| 203 | + |
| 204 | + # FOV A/2/0, Track 1: t=[0,1,2] -> valid anchors at t=[0,1] |
| 205 | + fov_a20_track1 = valid_anchors[ |
| 206 | + (valid_anchors["fov_name"] == "A/2/0") & (valid_anchors["track_id"] == 1) |
| 207 | + ] |
| 208 | + assert set(fov_a20_track1["t"]) == {0, 1} |
| 209 | + |
| 210 | + |
| 211 | +def test_filter_anchors_time_interval_2( |
| 212 | + preprocessed_hcs_dataset, tracks_with_gaps_dataset |
| 213 | +): |
| 214 | + """Test filtering with time_interval=2.""" |
| 215 | + with open_ome_zarr(preprocessed_hcs_dataset) as dataset: |
| 216 | + channel_names = dataset.channel_names |
| 217 | + positions = list(dataset.positions()) |
| 218 | + |
| 219 | + tracks_tables = [] |
| 220 | + for fov_name, _ in positions: |
| 221 | + tracks_df = pd.read_csv( |
| 222 | + next((tracks_with_gaps_dataset / fov_name).glob("*.csv")) |
| 223 | + ).astype(int) |
| 224 | + tracks_tables.append(tracks_df) |
| 225 | + |
| 226 | + ds = TripletDataset( |
| 227 | + positions=[pos for _, pos in positions], |
| 228 | + tracks_tables=tracks_tables, |
| 229 | + channel_names=channel_names, |
| 230 | + initial_yx_patch_size=(64, 64), |
| 231 | + z_range=slice(4, 9), |
| 232 | + fit=True, |
| 233 | + time_interval=2, |
| 234 | + ) |
| 235 | + |
| 236 | + valid_anchors = ds.valid_anchors |
| 237 | + |
| 238 | + # FOV A/1/0, Track 0: t=[0,1,2,3] -> valid anchors at t=[0,1] (t+2 available) |
| 239 | + fov_a10_track0 = valid_anchors[ |
| 240 | + (valid_anchors["fov_name"] == "A/1/0") & (valid_anchors["track_id"] == 0) |
| 241 | + ] |
| 242 | + assert set(fov_a10_track0["t"]) == {0, 1} |
| 243 | + |
| 244 | + # FOV A/1/0, Track 1: t=[0,1] -> no valid anchors (no t+2) |
| 245 | + fov_a10_track1 = valid_anchors[ |
| 246 | + (valid_anchors["fov_name"] == "A/1/0") & (valid_anchors["track_id"] == 1) |
| 247 | + ] |
| 248 | + assert len(fov_a10_track1) == 0 |
| 249 | + |
| 250 | + # FOV A/1/1, Track 0: t=[0,1,3] -> valid anchor at t=[1] (t=1+2=3 exists) |
| 251 | + fov_a11_track0 = valid_anchors[ |
| 252 | + (valid_anchors["fov_name"] == "A/1/1") & (valid_anchors["track_id"] == 0) |
| 253 | + ] |
| 254 | + assert set(fov_a11_track0["t"]) == {1} |
| 255 | + |
| 256 | + # FOV A/1/1, Track 1: t=[0,2,4] -> valid anchors at t=[0,2] |
| 257 | + fov_a11_track1 = valid_anchors[ |
| 258 | + (valid_anchors["fov_name"] == "A/1/1") & (valid_anchors["track_id"] == 1) |
| 259 | + ] |
| 260 | + assert set(fov_a11_track1["t"]) == {0, 2} |
| 261 | + |
| 262 | + # FOV A/2/0, Track 1: t=[0,1,2] -> valid anchor at t=[0] |
| 263 | + fov_a20_track1 = valid_anchors[ |
| 264 | + (valid_anchors["fov_name"] == "A/2/0") & (valid_anchors["track_id"] == 1) |
| 265 | + ] |
| 266 | + assert set(fov_a20_track1["t"]) == {0} |
| 267 | + |
| 268 | + |
| 269 | +def test_filter_anchors_cross_fov_independence( |
| 270 | + preprocessed_hcs_dataset, tracks_with_gaps_dataset |
| 271 | +): |
| 272 | + """Test that same track_id in different FOVs are treated independently.""" |
| 273 | + with open_ome_zarr(preprocessed_hcs_dataset) as dataset: |
| 274 | + channel_names = dataset.channel_names |
| 275 | + positions = list(dataset.positions()) |
| 276 | + |
| 277 | + tracks_tables = [] |
| 278 | + for fov_name, _ in positions: |
| 279 | + tracks_df = pd.read_csv( |
| 280 | + next((tracks_with_gaps_dataset / fov_name).glob("*.csv")) |
| 281 | + ).astype(int) |
| 282 | + tracks_tables.append(tracks_df) |
| 283 | + |
| 284 | + ds = TripletDataset( |
| 285 | + positions=[pos for _, pos in positions], |
| 286 | + tracks_tables=tracks_tables, |
| 287 | + channel_names=channel_names, |
| 288 | + initial_yx_patch_size=(64, 64), |
| 289 | + z_range=slice(4, 9), |
| 290 | + fit=True, |
| 291 | + time_interval=1, |
| 292 | + ) |
| 293 | + |
| 294 | + # Check global_track_id format and uniqueness |
| 295 | + assert "global_track_id" in ds.tracks.columns |
| 296 | + global_track_ids = ds.tracks["global_track_id"].unique() |
| 297 | + |
| 298 | + # Verify format: should be "fov_name_track_id" |
| 299 | + for gid in global_track_ids: |
| 300 | + assert "_" in gid |
| 301 | + fov_part, track_id_part = gid.rsplit("_", 1) |
| 302 | + assert "/" in fov_part # FOV names contain slashes like "A/1/0" |
| 303 | + |
| 304 | + # Track 0 exists in multiple FOVs (A/1/0, A/1/1, A/2/0) but should have different global_track_ids |
| 305 | + track0_global_ids = ds.tracks[ds.tracks["track_id"] == 0][ |
| 306 | + "global_track_id" |
| 307 | + ].unique() |
| 308 | + assert len(track0_global_ids) >= 3 # At least 3 different FOVs with track_id=0 |
| 309 | + |
| 310 | + # Verify that filtering is independent per FOV |
| 311 | + # A/1/0 Track 0 (continuous) should have more valid anchors than A/1/1 Track 0 (with gap) |
| 312 | + valid_a10_track0 = ds.valid_anchors[ |
| 313 | + (ds.valid_anchors["fov_name"] == "A/1/0") & (ds.valid_anchors["track_id"] == 0) |
| 314 | + ] |
| 315 | + valid_a11_track0 = ds.valid_anchors[ |
| 316 | + (ds.valid_anchors["fov_name"] == "A/1/1") & (ds.valid_anchors["track_id"] == 0) |
| 317 | + ] |
| 318 | + # A/1/0 Track 0 has t=[0,1,2] valid (3 anchors) |
| 319 | + # A/1/1 Track 0 has t=[0] valid (1 anchor, gap at t=2) |
| 320 | + assert len(valid_a10_track0) == 3 |
| 321 | + assert len(valid_a11_track0) == 1 |
| 322 | + |
| 323 | + |
| 324 | +def test_filter_anchors_predict_mode( |
| 325 | + preprocessed_hcs_dataset, tracks_with_gaps_dataset |
| 326 | +): |
| 327 | + """Test that predict mode (fit=False) returns all tracks regardless of time_interval.""" |
| 328 | + with open_ome_zarr(preprocessed_hcs_dataset) as dataset: |
| 329 | + channel_names = dataset.channel_names |
| 330 | + positions = list(dataset.positions()) |
| 331 | + |
| 332 | + tracks_tables = [] |
| 333 | + for fov_name, _ in positions: |
| 334 | + tracks_df = pd.read_csv( |
| 335 | + next((tracks_with_gaps_dataset / fov_name).glob("*.csv")) |
| 336 | + ).astype(int) |
| 337 | + tracks_tables.append(tracks_df) |
| 338 | + |
| 339 | + total_tracks = sum(len(df) for df in tracks_tables) |
| 340 | + |
| 341 | + ds = TripletDataset( |
| 342 | + positions=[pos for _, pos in positions], |
| 343 | + tracks_tables=tracks_tables, |
| 344 | + channel_names=channel_names, |
| 345 | + initial_yx_patch_size=(64, 64), |
| 346 | + z_range=slice(4, 9), |
| 347 | + fit=False, # Predict mode |
| 348 | + time_interval=1, |
| 349 | + ) |
| 350 | + |
| 351 | + # Should return all tracks even with time_interval=1 |
| 352 | + assert len(ds.valid_anchors) == total_tracks |
0 commit comments