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

Improve batch-match coverage #998

Merged
merged 6 commits into from
Feb 17, 2025
Merged

Conversation

benjeffery
Copy link
Member

@benjeffery benjeffery commented Feb 13, 2025

Fixes #972

Copy link

codecov bot commented Feb 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.34%. Comparing base (fbff408) to head (a063456).
Report is 9 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #998      +/-   ##
==========================================
+ Coverage   93.16%   93.34%   +0.18%     
==========================================
  Files          18       18              
  Lines        6462     6458       -4     
  Branches     1097     1095       -2     
==========================================
+ Hits         6020     6028       +8     
+ Misses        300      292       -8     
+ Partials      142      138       -4     
Flag Coverage Δ
C 93.34% <100.00%> (+0.18%) ⬆️
python 95.71% <100.00%> (+0.25%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@benjeffery benjeffery marked this pull request as ready for review February 14, 2025 12:22
@benjeffery
Copy link
Member Author

Ended up tweaking a few things here, for example ancestors are now packed into partitions using a greedy bin packing algorithm and the logic about how many partitions to use simplified.

Copy link
Member

@jeromekelleher jeromekelleher left a comment

Choose a reason for hiding this comment

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

Looks good, small suggested implementation improvement.

if group_index == 0:
partitions.append(group_ancestors)
partitions = [
group_ancestors,
Copy link
Member

Choose a reason for hiding this comment

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

stray comma causing profligate whitespace

current_partition.append(ancestor)
current_partition_work += ancestor_lengths[ancestor]
partitions.append(current_partition)
parition_count = math.ceil(total_work / min_work_per_job)
Copy link
Member

Choose a reason for hiding this comment

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

typo "paritition" -> partition

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed. I'm sprinkling these in now to prove that a free-range human wrote the code.

sorted_ancestors = sorted(
group_ancestors, key=lambda x: ancestor_lengths[x], reverse=True
)
partitions = []
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
partitions = []
partitions = [[] for _ in range(partition_count)]
partition_lengths = [0 for _ in range(partition_count)]

Copy link
Member Author

Choose a reason for hiding this comment

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

Superseded by the heap code.


# Use greedy bin packing - place each ancestor in the bin with
# lowest total length
for ancestor in sorted_ancestors:
Copy link
Member

Choose a reason for hiding this comment

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

How about we use a heapq for this?

heap = [(0, []) for _ range(partition_count]
for ancestor in sorted_ancestors:
      sum_len, partition = heapq.heappop(heap)
      partition.append(ancestor)
      sum_len += ancestor_lengths[ancestor]
      heapq.heappush(heap, (sum_len, partition))

I think this does the same thing, but avoids the quadratic time complexity here.

Copy link
Member Author

@benjeffery benjeffery Feb 14, 2025

Choose a reason for hiding this comment

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

Very nice, I should have thought of this!

@benjeffery
Copy link
Member Author

Fixed up in a063456

@benjeffery benjeffery merged commit 1aa0233 into tskit-dev:main Feb 17, 2025
12 checks passed
@benjeffery benjeffery deleted the batch-coverage branch February 17, 2025 13:15
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.

Improve test coverage for batch matching
2 participants