Skip to content

1d stats - #2417

Merged
mergify[bot] merged 1 commit into
tskit-dev:mainfrom
gtsambos:1d-stats
Aug 4, 2022
Merged

mergify[bot] merged 1 commit into
tskit-dev:mainfrom
gtsambos:1d-stats

Conversation

@gtsambos

Copy link
Copy Markdown
Member

Single statistics computed with the general_stat method are now returned as numpy scalars.

Fixes #2308

It didn't break any existing tests!

It would be good for someone to check my changes to the documented return values. I don't know what all these statistics are, so it would be easy for me to make a mistake there.

@gtsambos
gtsambos requested a review from petrelharp July 18, 2022 06:22
@gtsambos

Copy link
Copy Markdown
Member Author

Hmm, it might be worth making a separate test for allele_frequency_spectrum since that one should return a 1D array now under this change.

@gtsambos

Copy link
Copy Markdown
Member Author

Hmm, not sure why the docs build is failing but it doesn't appear to be in the part of the API that I changed 🤷

@codecov

codecov Bot commented Jul 18, 2022 •

Copy link
Copy Markdown

Codecov Report

Merging #2417 (0fb2605) into main (46d3e4a) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2417   +/-   ##
=======================================
  Coverage   93.44%   93.44%           
=======================================
  Files          28       28           
  Lines       27380    27386    +6     
  Branches     1253     1255    +2     
=======================================
+ Hits        25584    25590    +6     
  Misses       1762     1762           
  Partials       34       34           
Flag Coverage Δ
c-tests 92.26% <ø> (ø)
lwt-tests 89.05% <ø> (ø)
python-c-tests 71.17% <0.00%> (-0.03%) ⬇️
python-tests 98.95% <100.00%> (+<0.01%) ⬆️

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

Impacted Files Coverage Δ
python/tskit/trees.py 98.72% <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 46d3e4a...0fb2605. Read the comment docs.

@gtsambos

Copy link
Copy Markdown
Member Author

I think this is ready to look at, btw -- it doesn't obviously look like these test errors are to do with the code I've changed here.

@jeromekelleher jeromekelleher 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. Let's move the description of the general rule for dropping to a scalar to the higher-level docs, and put in a signpost there in the places you've updated.

Comment thread python/CHANGELOG.rst Resolved
Comment thread python/tskit/trees.py Outdated
:param bool span_normalise: Whether to divide the result by the span of the
window (defaults to True).
:return: A numpy array whose length is equal to the number of sample sets.
If there is just one sample set and one window, a numpy scalar is returned.

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 not sure we want to update all these return values independently. How about we just update to note the change? We could also add a sentence like "Please see :ref:sec_stats_output_format for information on the dimensionality of the returned array, and the conditions for dropping dimensions." to all these return descriptions?

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 reason why I updated these is because the output is no longer always a NumPy array now. What about something like
"Please see :ref:sec_stats_output_format for information about the shape and type of the returned NumPy object"?
Although I can see that the existing output is quite repetitive (and a pain to update), I think there's a case to make for leaving it in its current explicit form s well. "Dimensionality" can be quite an off-putting word to non-mathy folks, and I think talking about "dropping dimensions" is likely to be even more confusing as that is something that happens entirely under the hood here.

@gtsambos

Copy link
Copy Markdown
Member Author

Thanks Jerome! I fixed the changelog issue, might wait to see if there are any further thoughts about the return values before I make any modifications there.

@petrelharp

Copy link
Copy Markdown
Contributor

Hm, I'm not sure about this yet (but maybe I'm confused) - looking at the rules in the docs I think that what we want to do is:

  • if the sample sets are None or are a single list (instead of a list of lists)
  • and the windows are None
  • then we drop dimensions, twice, so should get a scalar.

If I'm reading it right, though, this would drop down to a scalar even if you did windows=[0, ts.sequence_length]. What we want to avoid is having different shaped outputs from the same type of input, so for instance doing something like:

for windows in ([0, 100, 1000], [0, 1000]):
  x = ts.diversity(sample_sets=[0, 1], windows=windows)
  print(x.shape)

then currently we'd get first a length-2 vector and then a length-1 vector. This change would give us a scalar the second time around, requiring special-casing that in the code (e.g., if len(x.shape == 0): xxx).

So, my proposal is to make this behavior only happen if windows=None?

@jeromekelleher

Copy link
Copy Markdown
Member

So, my proposal is to make this behavior only happen if windows=None?

Agreed - well spotted @petrelharp

@gtsambos

gtsambos commented Jul 22, 2022 via email

Copy link
Copy Markdown
Member Author

@gtsambos

gtsambos commented Aug 2, 2022

Copy link
Copy Markdown
Member Author

Hopefully this addresses @petrelharp's comments above? I also edited the Changelog as this PR was not part of the 0.5.2 release on Friday.

@petrelharp petrelharp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I've suggested a few more tests to make sure we're hitting all the conditions. I'm also not sure about adding the boilerplate to the return values of all the functions - but, I guess you're right, that we shoudl do it. However, it'd be nice to uniformize them more - right now, the strings differ in minor ways.

Comment thread python/CHANGELOG.rst Outdated
**Changes**

- Single statistics computed with ``TreeSequence.general_stat`` are now returned
as numpy scalars if windows=None. (:user:`gtsambos`, :issue:`2308`)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
as numpy scalars if windows=None. (:user:`gtsambos`, :issue:`2308`)
as numpy scalars if windows=None and samples is a single list or None. (:user:`gtsambos`, :issue:`2308`)

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.

This is only true for one-way stats, right? (Since this is just the changelog, maybe these details don't matter so much here)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Right, good point. You're right, we don't have to give the complete specification here, but we should have something accurate (which it wasn't quite, previously). I'm not thinking of a nice concise way to say this at the moment...

Comment thread python/tests/test_tree_stats.py Resolved
Comment thread python/tests/test_tree_stats.py Resolved
Comment thread python/tests/test_tree_stats.py Resolved
Comment thread python/tskit/trees.py Resolved
Comment thread python/tskit/trees.py Resolved
@gtsambos

gtsambos commented Aug 3, 2022

Copy link
Copy Markdown
Member Author

I'm starting to lose track of what conditions we want to require to return a scalar stat, and why. Does this cover it?

One way stat:

  • windows=None
  • AND sample_sets is just a list of samples, or None

K-way stat:

  • windows=None
  • AND sample_sets is a list of k sets
  • AND indexes=None

?

@petrelharp

Copy link
Copy Markdown
Contributor

Hm, let's see. I think the guideline is that if we drop dimensions twice, we should get a scalar, not a 1D array. And, the conditions under which we drop dimensions are listed in the docs:

  1. if windows = None
  2. for a one-way stat, if sample_sets is None or a single list (instead of a list-of-lists)
  3. for a k-way stat, if indexes is None or a single list of length k (instead of a list of length-k lists)

... and, yes, this is exactly what you've got! I agree!

@petrelharp

Copy link
Copy Markdown
Contributor

Hm - codecov is telling us that some of the code isn't being hit.

@gtsambos
gtsambos force-pushed the 1d-stats branch 2 times, most recently from d13d885 to c5c928d Compare August 4, 2022 04:32
@gtsambos

gtsambos commented Aug 4, 2022

Copy link
Copy Markdown
Member Author

pending the code checks, I hope this is good to go now 💀

@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 one nit about the changelog.

Comment thread python/CHANGELOG.rst Outdated
- Single statistics computed with ``TreeSequence.general_stat`` are now
returned as numpy scalars if windows=None, AND; samples is a single
list or None (for a 1-way stat), OR indexes is None or a single list of
length k (instead of a list of length-k lists). (:user:`gtsambos`, :issue:`2308`)

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.

We usually add the PR number here too.

Comment thread python/CHANGELOG.rst Resolved
@petrelharp

Copy link
Copy Markdown
Contributor

I fixed that nit, so we should be good to go - but, will the automerge-requested bot squash commits? Or, will the "squash and merge" button also rebase?

@petrelharp

Copy link
Copy Markdown
Contributor

And, thanks for getting this through, @gtsambos - this is a good idea, and annoyingly tricky to get right.

@benjeffery

benjeffery commented Aug 4, 2022 •

Copy link
Copy Markdown
Member

I fixed that nit, so we should be good to go - but, will the automerge-requested bot squash commits? Or, will the "squash and merge" button also rebase?

Neither! But you can ask mergify to squash before adding the automerge label:

@Mergifyio squash first-commit

(Only triggers if it is the sole content in a comment)

@benjeffery

Copy link
Copy Markdown
Member

@Mergifyio squash first-commit

@tskit-dev tskit-dev deleted a comment from mergify Bot Aug 4, 2022
@mergify

mergify Bot commented Aug 4, 2022

Copy link
Copy Markdown
Contributor

squash first-commit

✅ Pull request squashed successfully

@benjeffery

Copy link
Copy Markdown
Member

Hmm, that removes @gtsambos's author credit! How rude of the bot. I'll fix it.

@benjeffery

Copy link
Copy Markdown
Member

@gtsambos 's honour is restored. I'll merge.

@mergify
mergify Bot merged commit e0f5843 into tskit-dev:main Aug 4, 2022
@gtsambos

gtsambos commented Aug 5, 2022

Copy link
Copy Markdown
Member Author

thanks for defending my honour @benjeffery (and @petrelharp) ⚔️

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.

Don't return array-types for 1x1 statistics?

5 participants