Skip to content

Commit

Permalink
Fixes #2 and generates empty uns if asked for (#3)
Browse files Browse the repository at this point in the history
* Fixes #2 and generates empty uns if asked for

* Fix empty nested uns

* add Iterable import
  • Loading branch information
LouiseDck authored Nov 8, 2024
1 parent 3c1f276 commit 3c44df5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
13 changes: 12 additions & 1 deletion src/dummy_anndata/generate_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def generate_dataset(
obsp_types=None,
varp_types=None,
uns_types=None,
nested_uns_types=None,
):
"""
Generate a synthetic AnnData dataset with specified dimensions and data types.
Expand Down Expand Up @@ -48,6 +49,9 @@ def generate_dataset(
Types of matrices to generate for `varp`. Each type must be a key in `matrix_generators`.
uns_types : list of str, optional
Types of data to generate for `uns`. Each type must be a key in `vector_generators`, `matrix_generators`, or `scalar_generators`.
nested_uns_types : list of str, optional
Types of data to generate for the nested `uns` dictionary. They will be a new dictionary at the key `nested`.
Each type must be a key in `vector_generators`, `matrix_generators`, or `scalar_generators`.
Returns:
--------
Expand All @@ -70,6 +74,7 @@ def generate_dataset(
check_iterable_types(obsp_types, "obsp_types")
check_iterable_types(varp_types, "varp_types")
check_iterable_types(uns_types, "uns_types")
check_iterable_types(nested_uns_types, "nested_uns_types")

assert layer_types is None or all(
t in matrix_generators.keys() for t in layer_types
Expand Down Expand Up @@ -120,6 +125,12 @@ def generate_dataset(
+ list(matrix_generators.keys())
+ list(scalar_generators.keys())
)
if nested_uns_types is None:
nested_uns_types = (
list(vector_generators.keys())
+ list(matrix_generators.keys())
+ list(scalar_generators.keys())
)

X = matrix_generators[x_type](n_obs, n_vars)
layers = {t: matrix_generators[t](n_obs, n_vars) for t in layer_types}
Expand Down Expand Up @@ -149,7 +160,7 @@ def generate_dataset(
obsp = {t: matrix_generators[t](n_obs, n_obs) for t in obsp_types}
varp = {t: matrix_generators[t](n_vars, n_vars) for t in varp_types}

uns = generate_dict(n_obs, n_vars, uns_types)
uns = generate_dict(n_obs, n_vars, uns_types, nested_uns_types)

return ad.AnnData(
X,
Expand Down
22 changes: 17 additions & 5 deletions src/dummy_anndata/generate_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,27 @@ def generate_type(type, n_rows, n_cols):
return None


def generate_dict(n_rows, n_cols, types=None, nested=True):
def generate_dict(n_rows, n_cols, types=None, nested_uns_types=None):
if types is None: # types are all vectors and all matrices
scalar_types = list(scalar_generators.keys()) + [f"scalar_{t}" for t in vector_generators.keys()]
types = scalar_types + list(vector_generators.keys()) + list(matrix_generators.keys())
types = (
list(scalar_generators.keys())
+ [f"scalar_{t}" for t in vector_generators.keys()]
+ list(vector_generators.keys())
+ list(matrix_generators.keys())
)

if nested_uns_types is None:
nested_uns_types = (
list(scalar_generators.keys())
+ [f"scalar_{t}" for t in vector_generators.keys()]
+ list(vector_generators.keys())
+ list(matrix_generators.keys())
)

data = {}
if types: # types is not empty
data = {t: generate_type(t, n_rows, n_cols) for t in types}
if nested:
data["nested"] = generate_dict(n_rows, n_cols, types, False)
if nested_uns_types:
data["nested"] = generate_dict(n_rows, n_cols, types = nested_uns_types, nested_uns_types=[])

return data
10 changes: 7 additions & 3 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ def test_generating_dataset(tmp_path):
filename = tmp_path / "dummy.h5ad"
dummy.write_h5ad(filename)

def test_uns():
dummy_empty = dummy_anndata.generate_dataset(uns_types=[], nested_uns_types=[])
assert dummy_empty.uns == {}

def test_empty_uns():
dummy = dummy_anndata.generate_dataset(uns_types=[])
dummy_nested = dummy_anndata.generate_dataset(uns_types=[])
assert "nested" in dummy_nested.uns and dummy_nested.uns["nested"] != {}

assert dummy.uns == {}
dummy_no_nested = dummy_anndata.generate_dataset(nested_uns_types=[])
assert "nested" not in dummy_no_nested.uns

0 comments on commit 3c44df5

Please sign in to comment.