Skip to content

Table subset - #2700

Merged
jeromekelleher merged 5 commits into
tskit-dev:mainfrom
jeromekelleher:table-subset
Feb 23, 2023
Merged

jeromekelleher merged 5 commits into
tskit-dev:mainfrom
jeromekelleher:table-subset

Conversation

@jeromekelleher

Copy link
Copy Markdown
Member

A start on #2666

The nice thing about the boolean mask is that it does let us do the subsetting efficiently in-place (because the output order is fixed) so I think we should probably do this more efficiently, avoiding the intermediate copy. I'll tip along at this as I get a chance.

@codecov

codecov Bot commented Jan 25, 2023 •

Copy link
Copy Markdown

Codecov Report

Merging #2700 (6c8c779) into main (1c2099e) will increase coverage by 0.05%.
The diff coverage is 98.48%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2700      +/-   ##
==========================================
+ Coverage   94.00%   94.05%   +0.05%     
==========================================
  Files          29       29              
  Lines       28080    28409     +329     
  Branches     1585     1586       +1     
==========================================
+ Hits        26396    26720     +324     
- Misses       1648     1653       +5     
  Partials       36       36              
Flag Coverage Δ
c-tests 92.36% <98.14%> (+0.09%) ⬆️
lwt-tests 89.05% <ø> (ø)
python-c-tests 71.38% <99.12%> (+0.20%) ⬆️
python-tests 99.02% <100.00%> (+<0.01%) ⬆️

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

Impacted Files Coverage Δ
c/tskit/core.h 100.00% <ø> (ø)
c/tskit/tables.c 90.58% <98.12%> (+0.21%) ⬆️
python/_tskitmodule.c 92.83% <99.04%> (+0.09%) ⬆️
c/tskit/core.c 97.92% <100.00%> (+<0.01%) ⬆️
python/tskit/tables.py 98.95% <100.00%> (+<0.01%) ⬆️

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 1c2099e...6c8c779. Read the comment docs.

@jeromekelleher
jeromekelleher force-pushed the table-subset branch 2 times, most recently from 628c157 to 6afacc6 Compare February 3, 2023 15:51
@jeromekelleher
jeromekelleher marked this pull request as ready for review February 3, 2023 15:54
@jeromekelleher

Copy link
Copy Markdown
Member Author

This is ready for a review now @benjeffery - I went with x_table_keep_rows rather than subset because the table_collection_subset does something a bit different, and it would have been confusing to use the same word.

Ping @petrelharp and @bhaller also, since this is a basic utility needed for breaking up the simplify operation into smaller bits for parallelisation

@jeromekelleher

Copy link
Copy Markdown
Member Author

Dammit, went copy-paste mad too soon, and didn't test it properly. Back to draft!

@jeromekelleher
jeromekelleher marked this pull request as draft February 3, 2023 16:55
@jeromekelleher

Copy link
Copy Markdown
Member Author

I think the core of this is more-or-less in place now, and it would be good to get some feedback.

The main thing that might raise some controversy is the semantics around mutation parent ID remapping (look at mutation_table_keep_rows). Basically, we want to make it easy to get rid of mutations, which is currently a major faff because of the parents column. This does involve making sure that reference integrity is maintained, though, and the approach I've taken is to do some basic checks and to error-out if someone tries to do something that doesn't appear to make sense. We could easily add some options to change this behaviour, but I think this is a good default.

If we agree I'll write up the details in the documentation, and apply the same logic to the individual table (which is the only other place we have self-referential tables.

@molpopgen - this is also adjacent to the "more modular simplify" work you're interested in, so good to see what you think of the proposed API also.

@molpopgen

Copy link
Copy Markdown
Member

If I'm following along correctly: after simplifying "chunks", the client aggregates a list of nodes to delete, and can get back a new node "global" node mapping for the start of the next recording epoch?

@jeromekelleher

Copy link
Copy Markdown
Member Author

If I'm following along correctly: after simplifying "chunks", the client aggregates a list of nodes to delete, and can get back a new node "global" node mapping for the start of the next recording epoch?

Yeah, that's one of the things that's made easier - you can efficiently repack the node table at the end of a simplification epoch to get rid of unused nodes. Also, I'm planning to use this internally within the simplifier to make updating the site, mutation and individual tables easier. We should be able to get to a point where we don't copy any of the tables during simplification (although will need to think about how to handle the input edges - but that's close to what you're thinking about too).

@jeromekelleher
jeromekelleher force-pushed the table-subset branch 2 times, most recently from 89a9f7c to 8ee4ff6 Compare February 5, 2023 20:14
@molpopgen

Copy link
Copy Markdown
Member

Sounds good. I'll try to take a closer look at this early this week. My "modular simplification" idea has the nice property of not interacting with the input edge table. Hopefully that'll mean things "just work" in the face of any refactoring.

@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.

Very nice, seems all the hairy details are under control.
One extra test that might be worth doing is adding rows to the table after this operation - seeing that it resizes by changing num_rows and the old memory getting reused. As it looks this should be all fine, as max_rows keeps track of that, but maybe worth testing? Fine if not, but it's the only extra thing I could think of!

Comment thread c/tests/test_tables.c Resolved
Comment thread c/tests/test_tables.c Resolved
Comment thread python/tests/test_lowlevel.py Resolved
@jeromekelleher

Copy link
Copy Markdown
Member Author

Good call @benjeffery - I'll add some tests for adding more rows

@jeromekelleher
jeromekelleher force-pushed the table-subset branch 2 times, most recently from f9ef394 to 4db41e5 Compare February 10, 2023 06:26
@jeromekelleher
jeromekelleher marked this pull request as ready for review February 16, 2023 17:21
@jeromekelleher

Copy link
Copy Markdown
Member Author

OK, I think this is ready for final review now. Jeez that was a lot of code for a simple operation, but I'm hoping this (and a few similar ops like #2707) will help us trim down the code as well as making things more efficient over time.

@molpopgen

Copy link
Copy Markdown
Member

Great -- I was feeling guilty for not looking yet. We have a holiday weekend coming up, but I'll definitely look next week.

@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.

LGTM!

@molpopgen molpopgen 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.

This LGTM. There's one technical issue that can only be handled via documentation.

Comment thread c/tskit/tables.h Outdated
@return Return 0 on success or a negative value on failure.
*/
int tsk_node_table_keep_rows(
tsk_node_table_t *self, const bool *keep, tsk_flags_t options, tsk_id_t *id_map);

@molpopgen molpopgen Feb 21, 2023 •

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.

Overall, the API makes sense. There's one technical issue that should be documented:

  • When interacting with C functions, C++ code will tend to store things in a vector and obtain the pointer via the data member function. For example, x.data() yields a pointer to the first element, which is then passed to a C API.
  • C++ vector<bool> totally breaks all of what I just said. Due to a grave lapse of judgement, whether bool is stored or individual elements are bit fields is implementation defined.

There's nothing we can do except to note that "the pointer from a c++ vector<bool> is not the right thing to pass in here".

EDIT: fix formatting in the last sentence.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Or tell people to use std::vector<uint8_t> instead of std::vector<bool>, right? As I understand it, that avoids the problem.

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.

Yes, so long as uint8_t and bool have the same sizeof everywhere. That's true for all platforms that I expect to encounter, but I'd still use a static_assert to check it at compile time.

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.

Yet another reason why I'm drifting away from C++. From cppreference:

bool - type, capable of holding one of the two values: [true](https://en.cppreference.com/w/cpp/language/bool_literal) or [false](https://en.cppreference.com/w/cpp/language/bool_literal). The value of sizeof(bool) is implementation defined and might differ from 1.

And the first passes but the second fails:

#include <cstdint>
#include <type_traits>

int
main(int, char**)
{
    static_assert(sizeof(std::uint8_t) == sizeof(bool), "sizeof failure");
    static_assert(std::is_same<std::uint8_t, bool>::value,
                  "uint8_t and bool aren't the same");
}

I think we have to say "be careful here" when using C++. Too much is platform-dependent.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Technically, true that they could be different – but as you said, on all platforms that we expect to encounter, they are in fact the same. A matter of taste how much one wants to worry about that, I guess. :->

@jeromekelleher

jeromekelleher commented Feb 21, 2023 •

Copy link
Copy Markdown
Member Author

That's a really good point @molpopgen that I struggled a bit with at the Python C API level as well - the C99 bool type is frustratingly vague and I think it could end up being a problem in the future.

How about we follow numpy and do this:

typedef uint8_t tsk_bool_t;

and then change the signature of these functions to use tsk_bool_t rather than bool.

That should make things a bit easier to do portably?

For C++ we put in documentation warning saying "make sure you use std::vector<tsk_bool_t> and not std::vector<bool>?

@bhaller

bhaller commented Feb 21, 2023

Copy link
Copy Markdown

typedef uint8_t tsk_bool_t;

That's what I do in SLiM/Eidos:

typedef uint8_t eidos_logical_t;

@molpopgen

Copy link
Copy Markdown
Member

That's a really good point @molpopgen that I struggled a bit with at the Python C API level as well - the C99 bool type is frustratingly vague and I think it could end up being a problem in the future.

How about we follow numpy and do this:

typedef uint8_t tsk_bool_t;

and then change the signature of these functions to use tsk_bool_t rather than bool.

That should make things a bit easier to do portably?

For C++ we put in documentation warning saying "make sure you use std::vector<tsk_bool_t> and not std::vector<bool>?

Yes, this is the way to go.

@jeromekelleher

Copy link
Copy Markdown
Member Author

OK, great, I'll make that change.

@jeromekelleher

Copy link
Copy Markdown
Member Author

OK, I think this is ready to go now!

@molpopgen

Copy link
Copy Markdown
Member

LGTM. (This codecov thing is annoying!)

@jeromekelleher

Copy link
Copy Markdown
Member Author

@Mergifyio rebase

@mergify

mergify Bot commented Feb 23, 2023

Copy link
Copy Markdown
Contributor

rebase

✅ Branch has been successfully rebased

@jeromekelleher

Copy link
Copy Markdown
Member Author

I'm going to merge this manually, as the only thing that's failing is one codecov upload, which has occurred successfully on other builds.

@jeromekelleher
jeromekelleher merged commit ae7859d into tskit-dev:main Feb 23, 2023
@jeromekelleher
jeromekelleher deleted the table-subset branch February 23, 2023 12:10
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.

4 participants