-
Notifications
You must be signed in to change notification settings - Fork 1
Left right multiplication #99
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ccadc80
left multiplication
e399d7b
left multiplication
f4ee56a
remove stray breakpoint
311da65
add right-multiplication
aaa8106
Merge branch 'main' of https://github.com/PalamaraLab/threads into le…
8dbbd85
replace .at() with brackets
e0692be
Merge remote-tracking branch 'origin/main' into left_right_multiplica…
alexallmont 7b77a6b
docs: update changelog left/right multiplication (#99)
alexallmont 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
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
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,117 @@ | ||
| # This file is part of the Threads software suite. | ||
| # Copyright (C) 2024-2025 Threads Developers. | ||
| # | ||
| # This program is free software: you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License as published by | ||
| # the Free Software Foundation, either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # This program is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| import numpy as np | ||
| import pgenlib | ||
| import pytest | ||
|
|
||
| from threads_arg.serialization import load_instructions | ||
|
|
||
| from snapshot_runners import ( | ||
| TEST_DATA_DIR | ||
| ) | ||
|
|
||
| def _col_normalize(x): | ||
| z = x.copy() | ||
| mu = z.mean(axis=0, keepdims=True) | ||
| std = z.std(axis=0, keepdims=True) | ||
| return (z - mu) / std | ||
|
|
||
| def test_left_multiply(): | ||
| # Read ground truth genotypes | ||
| pgen_path = str(TEST_DATA_DIR / "panel.pgen") | ||
| reader = pgenlib.PgenReader(str(pgen_path).encode()) | ||
| expected_num_variants = reader.get_variant_ct() | ||
| num_samples = reader.get_raw_sample_ct() | ||
| expected_gt = np.empty((expected_num_variants, 2 * num_samples), dtype=np.int32) | ||
| reader.read_alleles_range(0, expected_num_variants, expected_gt) | ||
| gt_matrix = expected_gt.transpose() | ||
| gt_matrix_dip = gt_matrix[::2] + gt_matrix[1::2] | ||
| gt_matrix_norm = _col_normalize(gt_matrix) | ||
| gt_matrix_dip_norm = _col_normalize(gt_matrix_dip) | ||
|
|
||
| # Read threading instructions | ||
| threads_path = str(TEST_DATA_DIR / "expected_infer_snapshot.threads") | ||
| instructions = load_instructions(threads_path) | ||
|
|
||
| # Random vector to multiply with | ||
| rng = np.random.default_rng(130222) | ||
| x_hap = rng.normal(0, 1, 2 * num_samples) | ||
| x_dip = rng.normal(0, 1, num_samples) | ||
|
|
||
| # Make sure length checks are performed | ||
| with pytest.raises(RuntimeError): | ||
| instructions.left_multiply(x_dip) | ||
| with pytest.raises(RuntimeError): | ||
| instructions.left_multiply(x_hap, diploid=True) | ||
|
|
||
| # Do normal left-multiplication | ||
| expected = x_hap @ gt_matrix | ||
| expected_norm = x_hap @ gt_matrix_norm | ||
| expected_dip = x_dip @ gt_matrix_dip | ||
| expected_dip_norm = x_dip @ gt_matrix_dip_norm | ||
|
|
||
| # Do threads left-multiplication and confirm results are correct | ||
| found = instructions.left_multiply(x_hap) | ||
| assert np.allclose(expected, found) | ||
| found_norm = instructions.left_multiply(x_hap, normalize=True) | ||
| assert np.allclose(expected_norm, found_norm) | ||
| found_dip = instructions.left_multiply(x_dip, diploid=True) | ||
| assert np.allclose(expected_dip, found_dip) | ||
| found_dip_norm = instructions.left_multiply(x_dip, normalize=True, diploid=True) | ||
| assert np.allclose(expected_dip_norm, found_dip_norm) | ||
|
|
||
| def test_right_multiply(): | ||
| # Read ground truth genotypes | ||
| pgen_path = str(TEST_DATA_DIR / "panel.pgen") | ||
| reader = pgenlib.PgenReader(str(pgen_path).encode()) | ||
| expected_num_variants = reader.get_variant_ct() | ||
| num_samples = reader.get_raw_sample_ct() | ||
| expected_gt = np.empty((expected_num_variants, 2 * num_samples), dtype=np.int32) | ||
| reader.read_alleles_range(0, expected_num_variants, expected_gt) | ||
| gt_matrix = expected_gt.transpose() | ||
| gt_matrix_dip = gt_matrix[::2] + gt_matrix[1::2] | ||
| gt_matrix_norm = _col_normalize(gt_matrix) | ||
| gt_matrix_dip_norm = _col_normalize(gt_matrix_dip) | ||
|
|
||
| # Read threading instructions | ||
| threads_path = str(TEST_DATA_DIR / "expected_infer_snapshot.threads") | ||
| instructions = load_instructions(threads_path) | ||
|
|
||
| # Random vector to multiply with | ||
| rng = np.random.default_rng(130222) | ||
| x = rng.normal(0, 1, expected_num_variants) | ||
| x_wrong_length = rng.normal(0, 1, expected_num_variants + 1) | ||
|
|
||
| # Make sure length check is performed | ||
| with pytest.raises(RuntimeError): | ||
| instructions.left_multiply(x_wrong_length) | ||
|
|
||
| # Do normal right-multiplication | ||
| expected = gt_matrix @ x | ||
| expected_norm = gt_matrix_norm @ x | ||
| expected_dip = gt_matrix_dip @ x | ||
| expected_dip_norm = gt_matrix_dip_norm @ x | ||
|
|
||
| # Do threads right-multiplication and confirm results are correct | ||
| found = instructions.right_multiply(x) | ||
| assert np.allclose(expected, found) | ||
| found_norm = instructions.right_multiply(x, normalize=True) | ||
| assert np.allclose(expected_norm, found_norm) | ||
| found_dip = instructions.right_multiply(x, diploid=True) | ||
| assert np.allclose(expected_dip, found_dip) | ||
| found_dip_norm = instructions.right_multiply(x, normalize=True, diploid=True) | ||
| assert np.allclose(expected_dip_norm, found_dip_norm) |
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.
Uh oh!
There was an error while loading. Please reload this page.