diff --git a/beets/autotag/match.py b/beets/autotag/match.py index db4a35b132..a7121fd347 100644 --- a/beets/autotag/match.py +++ b/beets/autotag/match.py @@ -127,15 +127,21 @@ def assign_items( objects. These "extra" objects occur when there is an unequal number of objects of the two types. """ + log.debug("Computing track assignment...") # Construct the cost matrix. costs = [[float(track_distance(i, t)) for t in tracks] for i in items] - # Find a minimum-cost bipartite matching. - log.debug("Computing track assignment...") - cost, _, assigned_idxs = lap.lapjv(np.array(costs), extend_cost=True) + # Assign items to tracks + _, _, assigned_item_idxs = lap.lapjv(np.array(costs), extend_cost=True) log.debug("...done.") - # Produce the output matching. - mapping = {items[i]: tracks[t] for (t, i) in enumerate(assigned_idxs)} + # Each item in `assigned_item_idxs` list corresponds to a track in the + # `tracks` list. Each value is either an index into the assigned item in + # `items` list, or -1 if that track has no match. + mapping = { + items[iidx]: t + for iidx, t in zip(assigned_item_idxs, tracks) + if iidx != -1 + } extra_items = list(set(items) - mapping.keys()) extra_items.sort(key=lambda i: (i.disc, i.track, i.title)) extra_tracks = list(set(tracks) - set(mapping.values())) diff --git a/test/test_autotag.py b/test/test_autotag.py index e131a6be17..d6f4606e16 100644 --- a/test/test_autotag.py +++ b/test/test_autotag.py @@ -551,7 +551,7 @@ def test_order_works_with_invalid_track_numbers(self): def test_order_works_with_missing_tracks(self): items = [] items.append(self.item("one", 1)) - items.append(self.item("three", 3)) + items.append(self.item("two", 2)) trackinfo = [] trackinfo.append(TrackInfo(title="one")) trackinfo.append(TrackInfo(title="two")) @@ -560,8 +560,8 @@ def test_order_works_with_missing_tracks(self): items, trackinfo ) assert extra_items == [] - assert extra_tracks == [trackinfo[1]] - assert mapping == {items[0]: trackinfo[0], items[1]: trackinfo[2]} + assert extra_tracks == [trackinfo[2]] + assert mapping == {items[0]: trackinfo[0], items[1]: trackinfo[1]} def test_order_works_with_extra_tracks(self): items = []