-
Notifications
You must be signed in to change notification settings - Fork 35
Cohort subsets #394
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
Cohort subsets #394
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
92cbb3c
Factor out a test method to create cohorts
tomwhite 8078bf8
Generalise Fst and PBS tests to test variable numbers of cohorts
tomwhite 55e8e89
Cohort utilities
tomwhite 74a9229
Cohort subsets for Garud H
tomwhite a97b043
Cohort subsets for PBS
tomwhite 2332fe7
Add example to doc for _cohorts_to_array
tomwhite 189c964
Merge branch 'master' into cohort-subsets
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from typing import Optional, Sequence, Tuple, Union | ||
|
||
import numpy as np | ||
import pandas as pd | ||
|
||
from sgkit.typing import ArrayLike | ||
|
||
|
||
def _tuple_len(t: Union[int, Tuple[int, ...], str, Tuple[str, ...]]) -> int: | ||
"""Return the length of a tuple, or 1 for an int or string value.""" | ||
if isinstance(t, int) or isinstance(t, str): | ||
return 1 | ||
return len(t) | ||
|
||
|
||
def _cohorts_to_array( | ||
cohorts: Sequence[Union[int, Tuple[int, ...], str, Tuple[str, ...]]], | ||
index: Optional[pd.Index] = None, | ||
) -> ArrayLike: | ||
"""Convert cohorts or cohort tuples specified as a sequence of values or | ||
tuples to an array of ints used to match samples in ``sample_cohorts``. | ||
|
||
Cohorts can be specified by index (as used in ``sample_cohorts``), or a label, in | ||
which case an ``index`` must be provided to find index locations for cohorts. | ||
|
||
Parameters | ||
---------- | ||
cohorts | ||
A sequence of values or tuple representing cohorts or cohort tuples. | ||
index | ||
An index to turn labels into index locations, by default None. | ||
|
||
Returns | ||
------- | ||
An array of shape ``(len(cohorts), tuple_len)``, where ``tuple_len`` is the length | ||
of the tuples, or 1 if ``cohorts`` is a sequence of values. | ||
|
||
Raises | ||
------ | ||
ValueError | ||
If the cohort tuples are not all the same length. | ||
|
||
Examples | ||
-------- | ||
|
||
>>> import pandas as pd | ||
>>> from sgkit.cohorts import _cohorts_to_array | ||
>>> _cohorts_to_array([(0, 1), (2, 1)]) # doctest: +SKIP | ||
array([[0, 1], | ||
[2, 1]], dtype=int32) | ||
>>> _cohorts_to_array([("c0", "c1"), ("c2", "c1")], pd.Index(["c0", "c1", "c2"])) # doctest: +SKIP | ||
array([[0, 1], | ||
[2, 1]], dtype=int32) | ||
""" | ||
if len(cohorts) == 0: | ||
return np.array([], np.int32) | ||
|
||
tuple_len = _tuple_len(cohorts[0]) | ||
if not all(_tuple_len(cohort) == tuple_len for cohort in cohorts): | ||
raise ValueError("Cohort tuples must all be the same length") | ||
|
||
# convert cohort IDs using an index | ||
if index is not None: | ||
if isinstance(cohorts[0], str): | ||
cohorts = [index.get_loc(id) for id in cohorts] | ||
elif tuple_len > 1 and isinstance(cohorts[0][0], str): # type: ignore | ||
cohorts = [tuple(index.get_loc(id) for id in t) for t in cohorts] # type: ignore | ||
|
||
ct = np.empty((len(cohorts), tuple_len), np.int32) | ||
for n, t in enumerate(cohorts): | ||
ct[n, :] = t | ||
return ct |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
|
||
from sgkit.cohorts import _cohorts_to_array, _tuple_len | ||
|
||
|
||
def test_tuple_len(): | ||
assert _tuple_len(tuple()) == 0 | ||
assert _tuple_len(1) == 1 | ||
assert _tuple_len("a") == 1 | ||
assert _tuple_len("ab") == 1 | ||
assert _tuple_len((1,)) == 1 | ||
assert _tuple_len(("a",)) == 1 | ||
assert _tuple_len(("ab",)) == 1 | ||
assert _tuple_len((1, 2)) == 2 | ||
assert _tuple_len(("a", "b")) == 2 | ||
assert _tuple_len(("ab", "cd")) == 2 | ||
|
||
|
||
def test_cohorts_to_array__indexes(): | ||
with pytest.raises(ValueError, match="Cohort tuples must all be the same length"): | ||
_cohorts_to_array([(0, 1), (0, 1, 2)]) | ||
|
||
np.testing.assert_equal(_cohorts_to_array([]), np.array([])) | ||
np.testing.assert_equal(_cohorts_to_array([0, 1]), np.array([[0], [1]])) | ||
np.testing.assert_equal( | ||
_cohorts_to_array([(0, 1), (2, 1)]), np.array([[0, 1], [2, 1]]) | ||
) | ||
np.testing.assert_equal( | ||
_cohorts_to_array([(0, 1, 2), (3, 1, 2)]), np.array([[0, 1, 2], [3, 1, 2]]) | ||
) | ||
|
||
|
||
def test_cohorts_to_array__ids(): | ||
with pytest.raises(ValueError, match="Cohort tuples must all be the same length"): | ||
_cohorts_to_array([("c0", "c1"), ("c0", "c1", "c2")]) | ||
|
||
np.testing.assert_equal(_cohorts_to_array([]), np.array([])) | ||
np.testing.assert_equal( | ||
_cohorts_to_array(["c0", "c1"], pd.Index(["c0", "c1"])), | ||
np.array([[0], [1]]), | ||
) | ||
np.testing.assert_equal( | ||
_cohorts_to_array([("c0", "c1"), ("c2", "c1")], pd.Index(["c0", "c1", "c2"])), | ||
np.array([[0, 1], [2, 1]]), | ||
) | ||
np.testing.assert_equal( | ||
_cohorts_to_array( | ||
[("c0", "c1", "c2"), ("c3", "c1", "c2")], pd.Index(["c0", "c1", "c2", "c3"]) | ||
), | ||
np.array([[0, 1, 2], [3, 1, 2]]), | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any chance of a couple of simple examples here and the return values? I'm finding it a bit abstract and a concrete example would help understand what the function does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review @jeromekelleher. Added two examples in pystatgen/sgkit@f4b45ea