Skip to content

Add fancy indexing to tables #1348

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

Merged
merged 1 commit into from
May 12, 2021
Merged

Conversation

benjeffery
Copy link
Member

Fixes #1221

@codecov
Copy link

codecov bot commented May 12, 2021

Codecov Report

Merging #1348 (457d50d) into main (f6b917d) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #1348   +/-   ##
=======================================
  Coverage   93.89%   93.89%           
=======================================
  Files          26       26           
  Lines       22545    22561   +16     
  Branches     1066     1071    +5     
=======================================
+ Hits        21168    21184   +16     
  Misses       1343     1343           
  Partials       34       34           
Flag Coverage Δ
c-tests 92.43% <ø> (ø)
lwt-tests 92.97% <ø> (ø)
python-c-tests 95.27% <100.00%> (+<0.01%) ⬆️
python-tests 98.80% <100.00%> (+<0.01%) ⬆️

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

Impacted Files Coverage Δ
python/tskit/tables.py 98.90% <100.00%> (+0.02%) ⬆️

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 f6b917d...457d50d. Read the comment docs.

@benjeffery benjeffery marked this pull request as ready for review May 12, 2021 15:48
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.

Awesome! Minor comments.

if hasattr(table1, "metadata_schema"):
assert table1.metadata_schema == tskit.MetadataSchema(None)
table2 = table1.copy()
def test_metadata_schema(self, table_5row):
Copy link
Member

Choose a reason for hiding this comment

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

I like the abstract idea of fixtures (i.e., not making up test data over and over again), but I have to say I find this idiom of changing all the variables in a test to (effectively) a global variable really nasty. These tests are now objectively less readable that they were before. Wouldn't a parametrize with a set of locally-defined example tables be better?

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I see, you've made it a 5row table so that you know in later tests that you can do things like table[:4] or whatever. Maybe disregard above comment then.

Copy link
Member Author

Choose a reason for hiding this comment

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

The main change is moving the fixture to where it can be used by AssertEqualsMixin and FancyIndexingMixin. I agree the non-locality of fixtures can be a readability issue, but generally think it is worth the trade off.

if it is present. Supports negative indexing, e.g. ``table[-5]``.
If passed a slice, iterable or array return a new table containing the specified
rows. Similar to numpy fancy indexing, if the array or iterables contains
booleans then in acts as a mask, returning those rows for which are True.
Copy link
Member

Choose a reason for hiding this comment

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

s/in/it

Copy link
Member

Choose a reason for hiding this comment

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

The sentence isn't quite grammatical, something like "returning those rows for which the mask is True."

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

"""

if isinstance(index, numbers.Integral):
# Single row by integer
Copy link
Member

Choose a reason for hiding this comment

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

Out of curiosity, could you do index = slice(index).indices(len(self))[0]?

More obscure, I guess, but saves a bit of fiddly code?

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately not:

>>> slice(5).indices(10)
(0, 5, 1)

@mergify mergify bot merged commit 38e2229 into tskit-dev:main May 12, 2021
@benjeffery benjeffery deleted the table-indexing branch May 13, 2021 08:49
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.

Allow tables to be subset using numpy indexing
2 participants