Skip to content

Ibd result all pairs - #1660

Merged
mergify[bot] merged 3 commits into
tskit-dev:mainfrom
jeromekelleher:ibd_result_all_pairs
Sep 10, 2021
Merged

mergify[bot] merged 3 commits into
tskit-dev:mainfrom
jeromekelleher:ibd_result_all_pairs

Conversation

@jeromekelleher

Copy link
Copy Markdown
Member

Change the IBD result object to allow us find IBD among all pairs.

Stacked on #1659, #1655, #1637

Closes #1639

@jeromekelleher

Copy link
Copy Markdown
Member Author

This is working pretty well:

import resource
import tskit
import time

# ts = tskit.load("benchmark-n1M-m100M.trees")
ts = tskit.load("benchmark-n100k-m1M.trees")
print(ts)

before = time.perf_counter()
result = ts.find_ibd(max_time=100)
duration = time.perf_counter() - before

info = resource.getrusage(resource.RUSAGE_SELF)
# Result is in KiB
gb = info.ru_maxrss / (1024**2)

print(result)

print("time = ", duration)
print("mem  = ", gb)

Gives

╔═════════════════════════╗
║TreeSequence             ║
╠═══════════════╤═════════╣
║Trees          │     4518║
╟───────────────┼─────────╢
║Sequence Length│1000000.0║
╟───────────────┼─────────╢
║Sample Nodes   │   100000║
╟───────────────┼─────────╢
║Total Size     │ 14.0 MiB║
╚═══════════════╧═════════╝
╔═══════════╤══════╤═════════╤════════════╗
║Table      │Rows  │Size     │Has Metadata║
╠═══════════╪══════╪═════════╪════════════╣
║Edges      │217239│  6.6 MiB│          No║
╟───────────┼──────┼─────────┼────────────╢
║Individuals│     0│ 24 Bytes│          No║
╟───────────┼──────┼─────────┼────────────╢
║Migrations │     0│  8 Bytes│          No║
╟───────────┼──────┼─────────┼────────────╢
║Mutations  │  4943│178.6 KiB│          No║
╟───────────┼──────┼─────────┼────────────╢
║Nodes      │203674│  5.4 MiB│          No║
╟───────────┼──────┼─────────┼────────────╢
║Populations│     1│ 16 Bytes│          No║
╟───────────┼──────┼─────────┼────────────╢
║Provenances│     1│597 Bytes│          No║
╟───────────┼──────┼─────────┼────────────╢
║Sites      │  4943│120.7 KiB│          No║
╚═══════════╧══════╧═════════╧════════════╝

IBD Result:
max_time       = 100
min_length     = 0
total segments = 86966219
num node pairs = 46006545

time =  26.705644345027395
mem  =  5.849822998046875

So, we 're finding the IBD segments for a tree sequence with 100K samples in 26 seconds and ~6G of RAM. There's a lot more work to be done, but I think we're definitely on the right track. There's 46 million pairs that are IBD by 100 generations ago.

@jeromekelleher

jeromekelleher commented Sep 5, 2021 •

Copy link
Copy Markdown
Member Author

Here's the perf report:

 72.27%  python3  _tskit.cpython-38-x86_64-linux-gnu.so         [.] tsk_avl_tree_int_search         
14.45%  python3  _tskit.cpython-38-x86_64-linux-gnu.so         [.] tsk_avl_tree_int_insert                                                                                                 
   4.89%  python3  _tskit.cpython-38-x86_64-linux-gnu.so         [.] tsk_table_collection_find_ibd                                                                                           
   1.21%  python3  _tskit.cpython-38-x86_64-linux-gnu.so         [.] tsk_blkalloc_get                                                                                                        
   1.03%  python3  _tskit.cpython-38-x86_64-linux-gnu.so         [.] tsk_ibd_result_alloc_new_pair                                                                                           
   0.71%  python3  [unknown]                                     [k] 0xffffffffa74727e7                                                                                                      ▒
   0.44%  python3  [unknown]                                     [k] 0xffffffffa7600ae7                                                                                                      ▒
   0.40%  python3  [unknown]                                     [k] 0xffffffffa6a308be                                                                                                      ▒
   0.24%  python3  [unknown]                                     [k] 0xffffffffa6c6d1ae                           

So - basically all the time is spent in the AVL tree (which is fine)

@gtsambos

gtsambos commented Sep 7, 2021

Copy link
Copy Markdown
Member

Awesome. This looks like a huge step up. Pair-lookup was also taking most of the time beforehand, so it's not surprising that the AVL tree is where most of the time is spent here.

@jeromekelleher
jeromekelleher force-pushed the ibd_result_all_pairs branch 3 times, most recently from 60b1a78 to 9b6b531 Compare September 8, 2021 05:23
@codecov

codecov Bot commented Sep 8, 2021 •

Copy link
Copy Markdown

Codecov Report

Merging #1660 (a3dbf31) into main (a8b42a1) will increase coverage by 0.10%.
The diff coverage is 96.89%.

❗ Current head a3dbf31 differs from pull request most recent head 9adfce7. Consider uploading reports for the commit 9adfce7 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1660      +/-   ##
==========================================
+ Coverage   93.68%   93.79%   +0.10%     
==========================================
  Files          27       27              
  Lines       23483    23578      +95     
  Branches     1084     1085       +1     
==========================================
+ Hits        22000    22114     +114     
+ Misses       1449     1429      -20     
- Partials       34       35       +1     
Flag Coverage Δ
c-tests 92.04% <97.52%> (+0.24%) ⬆️
lwt-tests 93.49% <ø> (ø)
python-c-tests 95.46% <95.83%> (-0.03%) ⬇️
python-tests 98.76% <100.00%> (-0.04%) ⬇️

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

Impacted Files Coverage Δ
c/tskit/core.h 100.00% <ø> (ø)
python/_tskitmodule.c 92.36% <94.00%> (-0.02%) ⬇️
c/tskit/tables.c 90.09% <97.22%> (+0.42%) ⬆️
c/tskit/core.c 97.77% <100.00%> (+0.04%) ⬆️
python/tskit/tables.py 98.91% <100.00%> (+0.01%) ⬆️
python/tskit/trees.py 97.82% <100.00%> (-0.13%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a8b42a1...9adfce7. Read the comment docs.

Comment thread c/tests/test_tables.c
tsk_ibd_result_t result;
double true_left[2][2] = { { 0.0, 0.7 }, { 0.7, 0.0 } };
double true_right[2][2] = { { 0.7, 1.0 }, { 1.0, 0.7 } };
double true_left[2][2] = { { 0.0, 0.75 }, { 0.75, 0.0 } };

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In case anyone is wondering, this is for 32 bit tests. 0.7 isn't a round number, and it's troublesome for comparing equality on some hardware.

@jeromekelleher

Copy link
Copy Markdown
Member Author

This is ready for review now I think. I've filed a bunch of issues to follow up on, so there's still stuff to be cleaned up. It'd be good to get this much in though.

@jeromekelleher
jeromekelleher marked this pull request as ready for review September 9, 2021 12:36

@benjeffery benjeffery left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Been through, most closely checked the python C API code but all seems good.

@gtsambos

Copy link
Copy Markdown
Member

Just getting on to this now; hope to be done by tonight!

Comment thread python/tskit/tables.py Resolved
Comment thread c/tests/test_tables.c Resolved
Comment thread c/tests/test_tables.c
Comment on lines +4825 to +4833
ret = tsk_ibd_result_get_items(result, pairs2, lists);
CU_ASSERT_EQUAL_FATAL(ret, 0);
for (j = 0; j < num_pairs; j++) {
CU_ASSERT_EQUAL_FATAL(pairs[2 * j], pairs2[2 * j]);
CU_ASSERT_EQUAL_FATAL(pairs[2 * j + 1], pairs2[2 * j + 1]);
verify_ibd_segment_list(lists[j], result->num_nodes);
total_segments += lists[j]->num_segments;
}
CU_ASSERT_EQUAL_FATAL(result->total_segments, total_segments);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm a bit unclear on what this is testing for. Is this some way of checking that there are only keys for pairs with non-empty sets of IBD segments or something?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's mainly check that the "keys" we return are correct. Just making sure that the two different ways we have of getting the same thing (they pairs) are the same.

Comment thread c/tskit/tables.h

typedef struct {
tsk_size_t num_segments;
double total_span;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What's this for?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(the total_span attribute)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The sum of the right - left values in the segments. I figured this was a handy summary which would be useful when we optionally skip storing the segments.

@gtsambos gtsambos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice one @jeromekelleher --you've been so busy 🤯 👍 only really minor questions on this. I didn't comment in great detail on the AVL tree stuff as I only have a general idea of what they are needed for here.

@mergify
mergify Bot merged commit 06d7617 into tskit-dev:main Sep 10, 2021
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.

Remove mandatory sample_pairs argument in find_ibd

3 participants