Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix track matching regression #5571

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Fix track matching regression #5571

wants to merge 3 commits into from

Conversation

snejus
Copy link
Member

@snejus snejus commented Dec 30, 2024

Problem

A regression was introduced when adjusting the track matching logic to use lapjv instead of munkres. The lapjv algorithm returns -1 for unmatched items, which wasn't being handled correctly in the matching logic. This caused incorrect track assignments when importing new music.

Solution

  • Modified the mapping creation to filter out unmatched items (where index is -1)
  • Updated test case to properly catch this scenario

@snejus snejus self-assigned this Dec 30, 2024
@snejus snejus requested review from wisp3rwind and JOJ0 December 30, 2024 00:08
Copy link

Thank you for the PR! The changelog has not been updated, so here is a friendly reminder to check if you need to add an entry.

Copy link
Member

@wisp3rwind wisp3rwind left a comment

Choose a reason for hiding this comment

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

Thanks for the quick follow-up fix!

This looks fine in principle, I left a few suggestions that might make this more robust.

beets/autotag/match.py Show resolved Hide resolved
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]}
Copy link
Member

Choose a reason for hiding this comment

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

I think I understand why this change was made (the original code didn't fail the test due to indexing the arrays from the back with the spurious -1 index), and this made the test fail.

However, the original test case seems to cover something slightly different (track assignment with missing tracks with gaps, i.e. just zip(track_infos, items) (stopping when the shortest argument ends) wouldn't give the correct assignment.

Maybe, it would make sense to enhance the test case such that it tests missing tracks at various positions:

    def test_order_works_with_missing_tracks(self):
    	all_items = [
            self.item("one", 1),
            self.item("two", 2),
            self.item("three", 3),
        ]
        all_trackinfos = [
            TrackInfo(title="one"),
            TrackInfo(title="two"),
            TrackInfo(title="three"),
        ]
    	for missing_idx in range(3):
            items = all_items[:]
            items.pop(missing_idx)
            trackinfos = all_trackinfos[:]
            trackinfos.pop(missing_idx)
            mapping, extra_items, extra_tracks = match.assign_items(
                items, all_trackinfos
            )
            assert extra_items == []
            assert extra_tracks == [trackinfo[missing_idx]]
            assert mapping == dict(zip(items, trackinfos))

I didn't test this, but this should contain both the original and your modified test case.

Copy link
Member Author

Choose a reason for hiding this comment

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

I refactored the tests and added the test cases that you mentioned above

I had previously tested the `munkres` -> `lapjv` replacement
extensively, so I was today surprised to find that nothing gets matched
correctly when I tried importing some new tracks.

On the other hand I now remember making a small adjustment in the logic
to make autotagging tests pass which is when I introduced a bug: I did
not realize that `lapjv` returns index '-1' for each unmatched item.

This issue did not get caught by tests because this 'unmatched' item
index '-1' anecdotally ended up pointing to the last (expected) item in
the test making it pass.

This commit adjusts the aforementioned test to catch this issue and
fixes the logic to correctly identify unmatched tracks.
These tests depend on certain `track_length_grace` and
`track_length_max` configuration which was set by other tests in this
module.

I discovered this issue when I tried to run
`test_order_works_when_track_names_are_entirely_wrong` test only
- I found that my local configuration was read and the test failed.
@snejus
Copy link
Member Author

snejus commented Dec 31, 2024

@wisp3rwind note that I 'borrowed' the ConfigMixin implementation (see the last commit) from #5452 where it was introduced for the same purpose - to remove tests dependency on local configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants