Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 42 additions & 34 deletions deeplabcut/refine_training_dataset/stitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,40 +723,41 @@ def stitch(self, add_back_residuals=True):
for node in self.G
if node not in ("source", "sink")
)
if (
incomplete_tracks == 1
): # All remaining nodes must belong to the same track
# Verify whether there are overlapping tracklets
for t1, t2 in combinations(remaining_nodes, 2):
if t1 in t2:
# Pick the segment that minimizes "smoothness", computed here
# with the coefficient of variation of the differences.
if t1 in remaining_nodes:
remaining_nodes.remove(t1)
if t2 in remaining_nodes:
remaining_nodes.remove(t2)
track = sum(remaining_nodes)
hyp1 = track + t1
hyp2 = track + t2
dx1 = np.diff(hyp1.centroid, axis=0)
cv1 = dx1.std() / np.abs(dx1).mean()
dx2 = np.diff(hyp2.centroid, axis=0)
cv2 = dx2.std() / np.abs(dx2).mean()
if cv1 < cv2:
remaining_nodes.add(t1)
self.residuals.append(t2)
else:
remaining_nodes.add(t2)
self.residuals.append(t1)
paths.append(list(remaining_nodes))
elif incomplete_tracks > 1:
# Rebuild a full graph from the remaining nodes without
# temporal constraint on what tracklets can be stitched together.
self.build_graph(list(remaining_nodes), max_gap=np.inf)
self.G.nodes["source"]["demand"] = -incomplete_tracks
self.G.nodes["sink"]["demand"] = incomplete_tracks
_, self.flow = nx.capacity_scaling(self.G)
paths += self.reconstruct_paths()
if len(remaining_nodes) > 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! By the way, I checked, and we could also have:

if not remaining_nodes:
    return

as the finally clause is executed before the function exits, no matter what.
But that's a detail 😃

if (
incomplete_tracks == 1
): # All remaining nodes must belong to the same track
# Verify whether there are overlapping tracklets
for t1, t2 in combinations(remaining_nodes, 2):
if t1 in t2:
# Pick the segment that minimizes "smoothness", computed here
# with the coefficient of variation of the differences.
if t1 in remaining_nodes:
remaining_nodes.remove(t1)
if t2 in remaining_nodes:
remaining_nodes.remove(t2)
track = sum(remaining_nodes)
hyp1 = track + t1
hyp2 = track + t2
dx1 = np.diff(hyp1.centroid, axis=0)
cv1 = dx1.std() / np.abs(dx1).mean()
dx2 = np.diff(hyp2.centroid, axis=0)
cv2 = dx2.std() / np.abs(dx2).mean()
if cv1 < cv2:
remaining_nodes.add(t1)
self.residuals.append(t2)
else:
remaining_nodes.add(t2)
self.residuals.append(t1)
paths.append(list(remaining_nodes))
elif incomplete_tracks > 1:
# Rebuild a full graph from the remaining nodes without
# temporal constraint on what tracklets can be stitched together.
self.build_graph(list(remaining_nodes), max_gap=np.inf)
self.G.nodes["source"]["demand"] = -incomplete_tracks
self.G.nodes["sink"]["demand"] = incomplete_tracks
_, self.flow = nx.capacity_scaling(self.G)
paths += self.reconstruct_paths()
self.paths = paths
if len(self.paths) != self.n_tracks:
warnings.warn(f"Only {len(self.paths)} tracks could be reconstructed.")
Expand Down Expand Up @@ -867,6 +868,13 @@ def concatenate_data(self):
temp = np.full((self.n_frames, flat_data.shape[1]), np.nan)
temp[track.inds - self._first_frame] = flat_data
data.append(temp)

# If there isn't a track for each animal, fill in the dataframe with NaNs
missing_tracks = self.n_tracks - len(self.tracks)
if missing_tracks > 0:
track_shape = self.tracks[0].flat_data.shape[1]
data += missing_tracks * [np.full((self.n_frames, track_shape), np.nan)]

return np.hstack(data)

def format_df(self, animal_names=None):
Expand Down