aboutsummaryrefslogtreecommitdiff
path: root/tests/unit
diff options
context:
space:
mode:
authorBonfaceKilz2021-10-26 08:48:30 +0300
committerGitHub2021-10-26 08:48:30 +0300
commit0cfff99e22155b6b15e23cbeff596f5f8f08709c (patch)
treec831f28534ebf6432972e107dbd6da5daef81088 /tests/unit
parent5440bfcd6940db08c4479a39ba66dbc802b2c426 (diff)
parentc13afb3af166d2b01e4f9fd9b09bb231f0a63cb1 (diff)
downloadgenenetwork3-0cfff99e22155b6b15e23cbeff596f5f8f08709c.tar.gz
Merge pull request #46 from genenetwork/partial-correlations
Partial correlations
Diffstat (limited to 'tests/unit')
-rw-r--r--tests/unit/test_data_helpers.py37
-rw-r--r--tests/unit/test_partial_correlations.py64
2 files changed, 99 insertions, 2 deletions
diff --git a/tests/unit/test_data_helpers.py b/tests/unit/test_data_helpers.py
new file mode 100644
index 0000000..1eec3cc
--- /dev/null
+++ b/tests/unit/test_data_helpers.py
@@ -0,0 +1,37 @@
+"""
+Test functions in gn3.data_helpers
+"""
+
+from unittest import TestCase
+
+from gn3.data_helpers import partition_all
+
+class TestDataHelpers(TestCase):
+ """
+ Test functions in gn3.data_helpers
+ """
+
+ def test_partition_all(self):
+ """
+ Test that `gn3.data_helpers.partition_all` partitions sequences as expected.
+
+ Given:
+ - `num`: The number of items per partition
+ - `items`: A sequence of items
+ When:
+ - The arguments above are passed to the `gn3.data_helpers.partition_all`
+ Then:
+ - Return a new sequence with partitions, each of which has `num`
+ items in the same order as those in `items`, save for the last
+ partition which might have fewer items than `num`.
+ """
+ for count, items, expected in (
+ (1, [0, 1, 2, 3], ((0,), (1,), (2,), (3,))),
+ (3, (0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
+ ((0, 1, 2), (3, 4, 5), (6, 7, 8), (9, ))),
+ (4, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
+ ((0, 1, 2, 3), (4, 5, 6, 7), (8, 9))),
+ (13, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
+ ((0, 1, 2, 3, 4, 5, 6, 7, 8, 9), ))):
+ with self.subTest(n=count, items=items):
+ self.assertEqual(partition_all(count, items), expected)
diff --git a/tests/unit/test_partial_correlations.py b/tests/unit/test_partial_correlations.py
index 7631a71..60e54c1 100644
--- a/tests/unit/test_partial_correlations.py
+++ b/tests/unit/test_partial_correlations.py
@@ -4,7 +4,8 @@ from unittest import TestCase
from gn3.partial_correlations import (
fix_samples,
control_samples,
- dictify_by_samples)
+ dictify_by_samples,
+ find_identical_traits)
sampleslist = ["B6cC3-1", "BXD1", "BXD12", "BXD16", "BXD19", "BXD2"]
control_traits = (
@@ -106,6 +107,8 @@ class TestPartialCorrelations(TestCase):
def test_dictify_by_samples(self):
"""
+ Test that `dictify_by_samples` generates the appropriate dict
+
Given:
a sequence of sequences with sample names, values and variances, as
in the output of `gn3.partial_correlations.control_samples` or
@@ -133,7 +136,34 @@ class TestPartialCorrelations(TestCase):
dictified_control_samples)
def test_fix_samples(self):
- """Test that fix_samples fixes the values"""
+ """
+ Test that `fix_samples` returns only the common samples
+
+ Given:
+ - A primary trait
+ - A sequence of control samples
+ When:
+ - The two arguments are passed to `fix_samples`
+ Then:
+ - Only the names of the samples present in the primary trait that
+ are also present in ALL the control traits are present in the
+ return value
+ - Only the values of the samples present in the primary trait that
+ are also present in ALL the control traits are present in the
+ return value
+ - ALL the values for ALL the control traits are present in the
+ return value
+ - Only the variances of the samples present in the primary trait
+ that are also present in ALL the control traits are present in the
+ return value
+ - ALL the variances for ALL the control traits are present in the
+ return value
+ - The return value is a tuple of the above items, in the following
+ order:
+ ((sample_names, ...), (primary_trait_values, ...),
+ (control_traits_values, ...), (primary_trait_variances, ...)
+ (control_traits_variances, ...))
+ """
self.assertEqual(
fix_samples(
{"B6cC3-1": {"sample_name": "B6cC3-1", "value": 7.51879,
@@ -149,3 +179,33 @@ class TestPartialCorrelations(TestCase):
(None,),
(None, None, None, None, None, None, None, None, None, None, None,
None, None)))
+
+ def test_find_identical_traits(self):
+ """
+ Test `gn3.partial_correlations.find_identical_traits`.
+
+ Given:
+ - the name of a primary trait
+ - the value of a primary trait
+ - a sequence of names of control traits
+ - a sequence of values of control traits
+ When:
+ - the arguments above are passed to the `find_identical_traits`
+ function
+ Then:
+ - Return ALL trait names that have the same value when up to three
+ decimal places are considered
+ """
+ for primn, primv, contn, contv, expected in (
+ ("pt", 12.98395, ("ct0", "ct1", "ct2"),
+ (0.1234, 2.3456, 3.4567), tuple()),
+ ("pt", 12.98395, ("ct0", "ct1", "ct2"),
+ (12.98354, 2.3456, 3.4567), ("pt", "ct0")),
+ ("pt", 12.98395, ("ct0", "ct1", "ct2", "ct3"),
+ (0.1234, 2.3456, 0.1233, 4.5678), ("ct0", "ct2"))
+ ):
+ with self.subTest(
+ primary_name=primn, primary_value=primv,
+ control_names=contn, control_values=contv):
+ self.assertEqual(
+ find_identical_traits(primn, primv, contn, contv), expected)