diff options
author | Frederick Muriuki Muriithi | 2022-02-10 11:36:10 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2022-02-17 06:37:30 +0300 |
commit | 67f517aa0f44f55dc691ffd791bf22ef7af0b02c (patch) | |
tree | ddb4b7fd8f354cba2a09d8b0314a1032f1ec975f /gn3 | |
parent | 00cd52204646f283cfaf413e67755cd9f0acfff0 (diff) | |
download | genenetwork3-67f517aa0f44f55dc691ffd791bf22ef7af0b02c.tar.gz |
Add property tests for `dictify_by_samples`
Add property tests using pytest and hypothesis to test that the expected
properties hold for the
`gn3.computations.partial_correlations.dictify_by_samples`
function.
Diffstat (limited to 'gn3')
-rw-r--r-- | gn3/computations/partial_correlations.py | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/gn3/computations/partial_correlations.py b/gn3/computations/partial_correlations.py index f1757f9..85e3c11 100644 --- a/gn3/computations/partial_correlations.py +++ b/gn3/computations/partial_correlations.py @@ -6,6 +6,7 @@ GeneNetwork1. """ import math +import warnings from functools import reduce, partial from typing import Any, Tuple, Union, Sequence @@ -78,11 +79,24 @@ def dictify_by_samples(samples_vals_vars: Sequence[Sequence]) -> Sequence[dict]: This implementation extracts code that will find common use, and that will find use in more than one place. """ + def __build_key_value_pairs__( + sample: str, value: Union[float, None], + variance: Union[float, None]) -> dict[ + str, dict[str, Union[str, float, None]]]: + smp = sample.strip() + if smp == "": + warnings.warn( + "Empty strings for sample names is not allowed. Returning None", + category=RuntimeWarning) + return None + return (smp, {"sample_name": smp, "value": value, "variance": variance}) + return tuple( - { - sample: {"sample_name": sample, "value": val, "variance": var} - for sample, val, var in zip(*trait_line) - } for trait_line in zip(*(samples_vals_vars[0:3]))) + dict(item for item in + (__build_key_value_pairs__(sample, val, var) + for sample, val, var in zip(*trait_line)) + if item is not None) + for trait_line in zip(*(samples_vals_vars[0:3]))) def fix_samples(primary_trait: dict, control_traits: Sequence[dict]) -> Sequence[Sequence[Any]]: """ |