napistu 0.4.2__py3-none-any.whl → 0.4.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
tests/test_source.py CHANGED
@@ -5,6 +5,8 @@ import os
5
5
  import pandas as pd
6
6
  from napistu import indices
7
7
  from napistu import source
8
+ from napistu.network import ng_utils
9
+ from napistu.constants import SBML_DFS
8
10
 
9
11
  test_path = os.path.abspath(os.path.join(__file__, os.pardir))
10
12
  test_data = os.path.join(test_path, "test_data")
@@ -58,10 +60,40 @@ def test_source_w_pwindex():
58
60
  assert source_obj.source.shape == (2, 8)
59
61
 
60
62
 
61
- ################################################
62
- # __main__
63
- ################################################
63
+ def test_get_minimal_source_edges(sbml_dfs_metabolism):
64
+ vertices = sbml_dfs_metabolism.reactions.reset_index().rename(
65
+ columns={SBML_DFS.R_ID: "node"}
66
+ )
67
+
68
+ minimal_source_edges = ng_utils.get_minimal_sources_edges(
69
+ vertices, sbml_dfs_metabolism
70
+ )
71
+ # print(minimal_source_edges.shape)
72
+ assert minimal_source_edges.shape == (87, 3)
73
+
74
+
75
+ def test_source_set_coverage(sbml_dfs_metabolism):
76
+
77
+ source_df = source.unnest_sources(sbml_dfs_metabolism.reactions)
78
+
79
+ # print(source_df.shape)
80
+ assert source_df.shape == (111, 7)
81
+
82
+ set_coverage = source.source_set_coverage(source_df)
83
+ # print(set_coverage.shape)
84
+ assert set_coverage.shape == (87, 6)
85
+
86
+
87
+ def test_source_set_coverage_enrichment(sbml_dfs_metabolism):
88
+
89
+ source_total_counts = source.get_source_total_counts(
90
+ sbml_dfs_metabolism, "reactions"
91
+ )
92
+
93
+ source_df = source.unnest_sources(sbml_dfs_metabolism.reactions).head(40)
94
+
95
+ set_coverage = source.source_set_coverage(
96
+ source_df, source_total_counts=source_total_counts, sbml_dfs=sbml_dfs_metabolism
97
+ )
64
98
 
65
- if __name__ == "__main__":
66
- test_source()
67
- test_source_w_pwindex()
99
+ assert set_coverage.shape == (30, 6)
@@ -0,0 +1,62 @@
1
+ import numpy as np
2
+ from scipy.stats import fisher_exact
3
+
4
+ from napistu.statistics import hypothesis_testing
5
+
6
+
7
+ def test_fisher_exact_vectorized_basic_and_vectorized():
8
+
9
+ # Classic Fisher's test example: [[1, 9], [11, 3]]
10
+ # a=1, b=9, c=11, d=3
11
+ odds, p = hypothesis_testing.fisher_exact_vectorized([1], [9], [11], [3])
12
+ # Odds ratio: (1*3)/(9*11) = 3/99 = 0.0303...
13
+ assert np.allclose(odds, [3 / 99])
14
+ assert p.shape == (1,)
15
+ assert (p >= 0).all() and (p <= 1).all()
16
+
17
+ # Vectorized: two tables
18
+ odds, p = hypothesis_testing.fisher_exact_vectorized(
19
+ [1, 2], [9, 8], [11, 10], [3, 4]
20
+ )
21
+ assert odds.shape == (2,)
22
+ assert p.shape == (2,)
23
+ # Check that odds ratios are correct
24
+ expected_odds = np.array([(1 * 3) / (9 * 11), (2 * 4) / (8 * 10)])
25
+ assert np.allclose(odds, expected_odds)
26
+ # P-values should be between 0 and 1
27
+ assert (p >= 0).all() and (p <= 1).all()
28
+
29
+
30
+ def test_fisher_exact_vectorized_vs_scipy():
31
+
32
+ # Define several 2x2 tables
33
+ tables = [
34
+ ([1], [9], [11], [3]),
35
+ ([5], [2], [8], [7]),
36
+ ([10], [10], [10], [10]),
37
+ ([0], [5], [5], [10]),
38
+ ([3], [7], [2], [8]),
39
+ ]
40
+ for a, b, c, d in tables:
41
+ odds_vec, p_vec = hypothesis_testing.fisher_exact_vectorized(a, b, c, d)
42
+ # Build the table for scipy
43
+ table = np.array([[a[0], b[0]], [c[0], d[0]]])
44
+ odds_scipy, p_scipy = fisher_exact(table, alternative="greater")
45
+ # Odds ratios should be nearly identical
46
+ assert np.allclose(odds_vec, [odds_scipy], rtol=1e-6, atol=1e-8)
47
+ # P-values should be close (normal approx vs exact)
48
+ assert np.allclose(
49
+ p_vec, [p_scipy], rtol=0.15, atol=1e-3
50
+ ) # allow some tolerance
51
+
52
+ # Also test vectorized input
53
+ a = [1, 5, 10, 0, 3]
54
+ b = [9, 2, 10, 5, 7]
55
+ c = [11, 8, 10, 5, 2]
56
+ d = [3, 7, 10, 10, 8]
57
+ odds_vec, p_vec = hypothesis_testing.fisher_exact_vectorized(a, b, c, d)
58
+ for i in range(len(a)):
59
+ table = np.array([[a[i], b[i]], [c[i], d[i]]])
60
+ odds_scipy, p_scipy = fisher_exact(table, alternative="greater")
61
+ assert np.allclose(odds_vec[i], odds_scipy, rtol=1e-6, atol=1e-8)
62
+ assert np.allclose(p_vec[i], p_scipy, rtol=0.15, atol=1e-3)
@@ -1,50 +0,0 @@
1
- from __future__ import annotations
2
-
3
- from napistu import source
4
- from napistu.network import ng_utils
5
-
6
-
7
- def test_get_minimal_source_edges(sbml_dfs_metabolism):
8
- vertices = sbml_dfs_metabolism.reactions.reset_index().rename(
9
- columns={"r_id": "node"}
10
- )
11
-
12
- minimal_source_edges = ng_utils.get_minimal_sources_edges(
13
- vertices, sbml_dfs_metabolism
14
- )
15
- # print(minimal_source_edges.shape)
16
- assert minimal_source_edges.shape == (87, 3)
17
-
18
-
19
- def test_greedy_set_coverge_of_sources(sbml_dfs_metabolism):
20
- table_schema = sbml_dfs_metabolism.schema["reactions"]
21
-
22
- source_df = source.unnest_sources(
23
- sbml_dfs_metabolism.reactions, source_var="r_Source"
24
- )
25
- # print(source_df.shape)
26
- assert source_df.shape == (111, 7)
27
-
28
- set_coverage = source.greedy_set_coverge_of_sources(source_df, table_schema)
29
- # print(set_coverage.shape)
30
- assert set_coverage.shape == (87, 6)
31
-
32
-
33
- ################################################
34
- # __main__
35
- ################################################
36
-
37
- if __name__ == "__main__":
38
- import os
39
- from napistu import indices
40
- from napistu import consensus
41
-
42
- test_path = os.path.abspath(os.path.join(__file__, os.pardir))
43
- test_data = os.path.join(test_path, "test_data")
44
-
45
- pw_index = indices.PWIndex(os.path.join(test_data, "pw_index_metabolism.tsv"))
46
- sbml_dfs_dict = consensus.construct_sbml_dfs_dict(pw_index)
47
- sbml_dfs_metabolism = consensus.construct_consensus_model(sbml_dfs_dict, pw_index)
48
-
49
- test_get_minimal_source_edges(sbml_dfs_metabolism)
50
- test_greedy_set_coverge_of_sources(sbml_dfs_metabolism)