-
Notifications
You must be signed in to change notification settings - Fork 4
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
Update uns to precomp stats to take a list as parameter #21
base: rc/v1.4.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -555,7 +555,7 @@ def precomputed_stats_to_uns( | |
|
||
def uns_to_precomputed_stats( | ||
h5ad_path, | ||
uns_key, | ||
uns_keys_list, | ||
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. Please update the docstring to reflect the new argument. |
||
tmp_dir=None): | ||
""" | ||
Read a serialized precomputed stats file from the uns element | ||
|
@@ -593,7 +593,10 @@ def uns_to_precomputed_stats( | |
['n_cells', 'sum', 'sumsq', 'ge1', 'gt1', 'gt0'] | ||
) | ||
|
||
serialized_data = read_uns_from_h5ad(h5ad_path)[uns_key] | ||
serialized_data = read_uns_from_h5ad(h5ad_path) | ||
for uns_key in uns_keys_list: | ||
serialized_data = serialized_data[uns_key] | ||
|
||
with h5py.File(h5_path, 'w') as dst: | ||
for dataset_name in serialized_data: | ||
data = serialized_data[dataset_name] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -468,9 +468,11 @@ def test_re_order_blob(tmp_dir_fixture): | |
assert len(new_blob) == len(results_lookup) | ||
|
||
|
||
@pytest.mark.parametrize('nested_uns', [True, False]) | ||
def test_precomputed_stats_to_uns( | ||
tmp_dir_fixture, | ||
precomputed_stats_fixture): | ||
precomputed_stats_fixture, | ||
nested_uns): | ||
""" | ||
Test utility functions to move precomputed stats data from HDF5 to | ||
the uns element of an h5ad file and back. | ||
|
@@ -507,16 +509,26 @@ def test_precomputed_stats_to_uns( | |
a_data.write_h5ad(h5ad_path) | ||
|
||
uns_key = 'serialization_test' | ||
uns_key_nested = 'serialization_test_nested' | ||
|
||
precomputed_stats_to_uns( | ||
precomputed_stats_path=precomputed_stats_fixture, | ||
h5ad_path=h5ad_path, | ||
uns_key=uns_key) | ||
|
||
roundtrip_path = uns_to_precomputed_stats( | ||
uns_key=uns_key, | ||
h5ad_path=h5ad_path, | ||
tmp_dir=tmp_dir_fixture) | ||
if not nested_uns: | ||
roundtrip_path = uns_to_precomputed_stats( | ||
uns_keys_list=[uns_key], | ||
h5ad_path=h5ad_path, | ||
tmp_dir=tmp_dir_fixture) | ||
else: | ||
a_data = anndata.read_h5ad(h5ad_path) | ||
a_data.uns[uns_key] = {uns_key_nested: a_data.uns[uns_key]} | ||
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. Just being paranoid: The way this code is constructed, the file at Can you write a new anndata object at a different location that only has the expected data at your nested location. I am imagining something like this:
|
||
a_data.write_h5ad(h5ad_path) | ||
roundtrip_path = uns_to_precomputed_stats( | ||
uns_keys_list=[uns_key, uns_key_nested], | ||
h5ad_path=h5ad_path, | ||
tmp_dir=tmp_dir_fixture) | ||
|
||
with h5py.File(precomputed_stats_fixture, 'r') as expected_src: | ||
with h5py.File(roundtrip_path, 'r') as actual_src: | ||
|
@@ -540,3 +552,4 @@ def test_precomputed_stats_to_uns( | |
original_uns['maybe'], | ||
roundtrip_h5ad.uns['maybe'] | ||
) | ||
|
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.
Please add a unit test for this function. I know it's trivial, I just want to make sure that this is actually solving the problem we want it to solve.