stitch_tracklets crashes when not enough tracks are found by n-poulsen · Pull Request #2273 · DeepLabCut/DeepLabCut

When there aren't enough tracks found by the stitcher, the current implementation fails. By running

deeplabcut.analyze_videos(
    "project/config.yaml", ["/videos/"], videotype='.avi', auto_track=True, n_tracks=10,
)

The following error occurs as remaining_nodes is an empty list

deeplabcut/refine_training_dataset/stitch.py:690: UserWarning: No optimal solution found. Employing black magic...
Traceback (most recent call last):
  File "deeplabcut/refine_training_dataset/stitch.py", line 687, in stitch
    _, self.flow = nx.capacity_scaling(self.G)
  File "/lib/python3.9/site-packages/networkx/algorithms/flow/capacityscaling.py", line 394, in capacity_scaling
    raise nx.NetworkXUnfeasible("No flow satisfying all demands.")
networkx.exception.NetworkXUnfeasible: No flow satisfying all demands.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "deeplabcut/refine_training_dataset/stitch.py", line 755, in stitch
    self.build_graph(list(remaining_nodes), max_gap=np.inf)
  File "deeplabcut/refine_training_dataset/stitch.py", line 644, in build_graph
    nodes_in, nodes_out = zip(
ValueError: not enough values to unpack (expected 2, got 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "deeplabcut/pose_estimation_tensorflow/predict_videos.py", line 651, in analyze_videos
    stitch_tracklets(
  File "deeplabcut/refine_training_dataset/stitch.py", line 1201, in stitch_tracklets
    stitcher.stitch()
  File "deeplabcut/refine_training_dataset/stitch.py", line 766, in stitch
    raise ValueError(
ValueError: Could not reconstruct 10 tracks from the tracklets given.

This PR simply returns once there are no remaining nodes, even if the number of tracks found is smaller than the desired number of tracks (as if there are no remaining nodes, no new tracks can be found). To keep the shape of the output dataframe identical to when tracks are found for all individuals, NaN-filled columns are added for the missing tracks. A user warning is printed so the user is informed that not all tracks were found:

deeplabcut/refine_training_dataset/stitch.py:763: UserWarning: Only 4 tracks could be reconstructed.