-
Notifications
You must be signed in to change notification settings - Fork 74
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
randomized svd draft #3008
Draft
hanbin973
wants to merge
32
commits into
tskit-dev:main
Choose a base branch
from
hanbin973:rsvd
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
randomized svd draft #3008
Changes from 11 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
1f45245
randomized svd draft
hanbin973 e408ab3
modified api remove scipy
hanbin973 a176132
remove scipy
hanbin973 8c662c8
correct docstring and comments
hanbin973 aa13613
space remove
hanbin973 5bf405a
rng to random seed
hanbin973 6e415e5
add windows feature
hanbin973 45ac61e
output shape change when windows=wholegnome
hanbin973 2cdb9dd
make centre work with nodes
hanbin973 1f194aa
remove redundant options from internal functions
hanbin973 fdc5842
start at testing
petrelharp c0a2854
change variable name to n_* to num_*
hanbin973 667d60f
return range sketch matrix Q
hanbin973 36c10e9
fix random sketch option to handle None
hanbin973 0ac88b8
random_sketch needs windows specified
hanbin973 a6fa8ab
input checking for range_sketch to align with windows
hanbin973 791c7da
docstring change to reflect range_sketch
hanbin973 2f4ce2b
linting has a bug; when converting lambda to ordinary function defini…
hanbin973 be2f736
now output is a dataclass
hanbin973 bcbbcf6
docstring change
hanbin973 1a8ff7a
change variable name of PCAResult class
hanbin973 af16340
move internal function of PCA out
hanbin973 e81db15
function rearrangement
hanbin973 277cfc2
terminology change loaindgs -> factor
hanbin973 be715de
time resolved feature
hanbin973 d3e6c89
Update python/tskit/trees.py
hanbin973 49fe716
Update python/tskit/trees.py
hanbin973 d217fc7
Update python/tskit/trees.py
hanbin973 89e3b27
Update python/tskit/trees.py
hanbin973 81cbecf
remove comments; eigen_values -> eigenvalues
hanbin973 7ca6452
support for subset of samples and individuals in a tree
hanbin973 587409b
add time assertion
hanbin973 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 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 |
---|---|---|
|
@@ -460,7 +460,7 @@ def check_relatedness_vector( | |
return R | ||
|
||
|
||
class TestExamples: | ||
class TestRelatednessVector: | ||
|
||
def test_bad_weights(self): | ||
n = 5 | ||
|
@@ -737,3 +737,147 @@ def test_disconnected_non_sample_topology(self, centre): | |
ts2, internal_checks=True, centre=centre, do_nodes=False | ||
) | ||
np.testing.assert_array_almost_equal(D1, D2) | ||
|
||
|
||
def pca(ts, windows, centre): | ||
drop_dimension = windows is None | ||
if drop_dimension: | ||
windows = [0, ts.sequence_length] | ||
Sigma = relatedness_matrix(ts=ts, windows=windows, centre=centre) | ||
U, S, _ = np.linalg.svd(Sigma, hermitian=True) | ||
if drop_dimension: | ||
U = U[0] | ||
S = S[0] | ||
return U, S | ||
|
||
|
||
def allclose_up_to_sign(x, y, **kwargs): | ||
# check if two vectors are the same up to sign | ||
x_const = np.isclose(np.std(x), 0) | ||
y_const = np.isclose(np.std(y), 0) | ||
if x_const or y_const: | ||
if np.allclose(x, 0): | ||
r = 1.0 | ||
else: | ||
r = np.mean(x / y) | ||
else: | ||
r = np.sign(np.corrcoef(x, y)[0, 1]) | ||
return np.allclose(x, r * y, **kwargs) | ||
|
||
|
||
def assert_pcs_equal(U, D, U_full, D_full, rtol=1e-05, atol=1e-08): | ||
# check that the PCs in U, D occur in U_full, D_full | ||
# accounting for sign and ordering | ||
assert len(D) <= len(D_full) | ||
assert U.shape[0] == U_full.shape[0] | ||
assert U.shape[1] == len(D) | ||
for k in range(len(D)): | ||
u = U[:, k] | ||
d = D[k] | ||
(ii,) = np.where(np.isclose(D_full, d, rtol=rtol, atol=atol)) | ||
assert len(ii) > 0, f"{k}th singular value {d} not found in {D_full}." | ||
found_it = False | ||
for i in ii: | ||
if allclose_up_to_sign(u, U_full[:, i], rtol=rtol, atol=atol): | ||
found_it = True | ||
break | ||
assert found_it, f"{k}th singular vector {u} not found in {U_full}." | ||
|
||
|
||
class TestPCA: | ||
|
||
def verify_pca(self, ts, num_windows, n_components, centre): | ||
if num_windows == 0: | ||
windows = None | ||
elif num_windows % 2 == 0: | ||
windows = np.linspace( | ||
0.2 * ts.sequence_length, 0.8 * ts.sequence_length, num_windows + 1 | ||
) | ||
else: | ||
windows = np.linspace(0, ts.sequence_length, num_windows + 1) | ||
ts_U, ts_D = ts.pca( | ||
windows=windows, n_components=n_components, centre=centre, random_seed=123 | ||
) | ||
num_rows = ts.num_samples | ||
if windows is None: | ||
assert ts_U.shape == (num_rows, n_components) | ||
assert ts_D.shape == (n_components,) | ||
else: | ||
assert ts_U.shape == (num_windows, num_rows, n_components) | ||
assert ts_D.shape == (num_windows, n_components) | ||
U, D = pca(ts=ts, windows=windows, centre=centre) | ||
if windows is None: | ||
np.testing.assert_allclose(ts_D, D[:n_components], atol=1e-8) | ||
assert_pcs_equal(ts_U, ts_D, U, D) | ||
else: | ||
for w in range(num_windows): | ||
np.testing.assert_allclose(ts_D[w], D[w, :n_components], atol=1e-8) | ||
assert_pcs_equal(ts_U[w], ts_D[w], U[w], D[w]) | ||
|
||
def test_bad_windows(self): | ||
ts = msprime.sim_ancestry( | ||
3, | ||
ploidy=2, | ||
sequence_length=10, | ||
random_seed=123, | ||
) | ||
for bad_w in ([], [1]): | ||
with pytest.raises(ValueError, match="Number of windows"): | ||
ts.pca(n_components=2, windows=bad_w) | ||
for bad_w in ([1, 0], [-3, 10]): | ||
with pytest.raises(tskit.LibraryError, match="TSK_ERR_BAD_WINDOWS"): | ||
ts.pca(n_components=2, windows=bad_w) | ||
|
||
def test_bad_num_components(self): | ||
ts = msprime.sim_ancestry( | ||
3, | ||
ploidy=2, | ||
sequence_length=10, | ||
random_seed=123, | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe a test for |
||
with pytest.raises(ValueError, match="Number of components"): | ||
ts.pca(n_components=ts.num_samples + 1) | ||
with pytest.raises(ValueError, match="Number of components"): | ||
ts.pca(n_components=4, samples=[0, 1, 2]) | ||
with pytest.raises(ValueError, match="Number of components"): | ||
ts.pca(n_components=4, individuals=[0, 1]) | ||
|
||
def test_indivs_and_samples(self): | ||
ts = msprime.sim_ancestry( | ||
3, | ||
ploidy=2, | ||
sequence_length=10, | ||
random_seed=123, | ||
) | ||
with pytest.raises(ValueError, match="Samples and individuals"): | ||
ts.pca(n_components=2, samples=[0, 1, 2, 3], individuals=[0, 1, 2]) | ||
|
||
def test_modes(self): | ||
ts = msprime.sim_ancestry( | ||
3, | ||
ploidy=2, | ||
sequence_length=10, | ||
random_seed=123, | ||
) | ||
for bad_mode in ("site", "node"): | ||
with pytest.raises( | ||
tskit.LibraryError, match="TSK_ERR_UNSUPPORTED_STAT_MODE" | ||
): | ||
ts.pca(n_components=2, mode=bad_mode) | ||
|
||
@pytest.mark.parametrize("n", [2, 3, 5, 15]) | ||
@pytest.mark.parametrize("centre", (True, False)) | ||
@pytest.mark.parametrize("num_windows", (0, 1, 2, 3)) | ||
@pytest.mark.parametrize("n_components", (1, 3)) | ||
def test_simple_sims(self, n, centre, num_windows, n_components): | ||
ploidy = 1 | ||
nc = min(n_components, n * ploidy) | ||
ts = msprime.sim_ancestry( | ||
n, | ||
ploidy=ploidy, | ||
population_size=20, | ||
sequence_length=100, | ||
recombination_rate=0.01, | ||
random_seed=12345, | ||
) | ||
self.verify_pca(ts, num_windows=num_windows, n_components=nc, centre=centre) |
This file contains 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
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.
This is not right, as here we want
r
to be +/-1, I think?