Skip to content

Commit 6b5978b

Browse files
authored
BUG: Fix T-DaTing labels when continuing tracking (#280)
* BUG: Fix T-DaTing labels when continuing tracking When continuing tracking from previous list, the track labels could be duplicated because the labeling started always from 0. Instead, start from maximum label of previous cells. * Add test for multi-step t-dating * Update variable name
1 parent 99c79d8 commit 6b5978b

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

pysteps/tests/test_tracking_tdating.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,56 @@
1515
("mch", True),
1616
]
1717

18+
arg_names_multistep = ("source", "len_timesteps")
19+
arg_values_multistep = [
20+
("mch", 6),
21+
]
22+
23+
24+
@pytest.mark.parametrize(arg_names_multistep, arg_values_multistep)
25+
def test_tracking_tdating_dating_multistep(source, len_timesteps):
26+
pytest.importorskip("skimage")
27+
28+
input_fields, metadata = get_precipitation_fields(
29+
0, len_timesteps, True, True, 4000, source
30+
)
31+
input_fields, __ = to_reflectivity(input_fields, metadata)
32+
33+
timelist = metadata["timestamps"]
34+
35+
# First half of timesteps
36+
tracks_1, cells, labels = dating(
37+
input_fields[0 : len_timesteps // 2],
38+
timelist[0 : len_timesteps // 2],
39+
mintrack=1,
40+
)
41+
# Second half of timesteps
42+
tracks_2, cells, _ = dating(
43+
input_fields[len_timesteps // 2 - 2 :],
44+
timelist[len_timesteps // 2 - 2 :],
45+
mintrack=1,
46+
start=2,
47+
cell_list=cells,
48+
label_list=labels,
49+
)
50+
51+
# Since we are adding cells, number of tracks should increase
52+
assert len(tracks_1) <= len(tracks_2)
53+
54+
# Tracks should be continuous in time so time difference should not exceed timestep
55+
max_track_step = max([t.time.diff().max().seconds for t in tracks_2 if len(t) > 1])
56+
timestep = np.diff(timelist).max().seconds
57+
assert max_track_step <= timestep
58+
59+
# IDs of unmatched cells should increase in every timestep
60+
for prev_df, cur_df in zip(cells[:-1], cells[1:]):
61+
prev_ids = set(prev_df.ID)
62+
cur_ids = set(cur_df.ID)
63+
new_ids = list(cur_ids - prev_ids)
64+
prev_unmatched = list(prev_ids - cur_ids)
65+
if len(prev_unmatched):
66+
assert np.all(np.array(new_ids) > max(prev_unmatched))
67+
1868

1969
@pytest.mark.parametrize(arg_names, arg_values)
2070
def test_tracking_tdating_dating(source, dry_input):

pysteps/tracking/tdating.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ def dating(
152152
raise ValueError("start > len(timelist)")
153153

154154
oflow_method = motion.get_method("LK")
155-
max_ID = 0
155+
if len(label_list) == 0:
156+
max_ID = 0
157+
else:
158+
max_ID = np.nanmax([np.nanmax(np.unique(label_list)), 0])
156159
for t in range(start, len(timelist)):
157160
cells_id, labels = tstorm_detect.detection(
158161
input_video[t, :, :],

0 commit comments

Comments
 (0)