Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fratajcz committed Oct 11, 2024
1 parent ddd6640 commit 094227d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ jobs:
pip install .
- name: Test with unittest
run: |
python -m unittest speos/tests/test_*.py
NODATA=1 python -m unittest speos/tests/test_*.py
111 changes: 60 additions & 51 deletions speos/tests/test_postprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
from speos.postprocessing.postprocessor import PostProcessor
from speos.utils.config import Config
from speos.tests.utils import TestSetup
import os

NODATA = int(os.getenv('NODATA', '1'))
print(NODATA)

class PostProcessorTest(TestSetup):

class PostProcessorTestNoData(TestSetup):
def setUp(self):
super().setUp()
self.config.name = "TestPostProcessor"

self.pp = PostProcessor(self.config)
self.pp = PostProcessor(self.config, translation_table=self.translation_table_path)

self.test_outer_results = "speos/tests/files/dummy_outer_results.json"
self.results_file = "speos/tests/files/dummy_inner_results.tsv"
Expand All @@ -23,7 +25,7 @@ def test_random_overlap_descriptive_algorithm(self):
config = self.config.copy()
config.crossval.n_folds = 5

pp = PostProcessor(config)
pp = PostProcessor(config, translation_table=self.translation_table_path)
pp.num_runs_for_random_experiments = 100

eligible_genes = np.asarray([str(x) for x in range(0, 100)])
Expand All @@ -41,7 +43,7 @@ def test_random_overlap_fast_algorithm(self):
config = self.config.copy()
config.crossval.n_folds = 5

pp = PostProcessor(config)
pp = PostProcessor(config, translation_table=self.translation_table_path)
pp.num_runs_for_random_experiments = 100

eligible_genes = np.asarray([str(x) for x in range(0, 100)])
Expand All @@ -59,7 +61,7 @@ def test_random_overlap_both_algorithms_identical_results(self):
config = self.config.copy()
config.crossval.n_folds = 5

pp = PostProcessor(config)
pp = PostProcessor(config, translation_table=self.translation_table_path)
pp.num_runs_for_random_experiments = 100

eligible_genes = np.asarray([str(x) for x in range(0, 100)])
Expand All @@ -83,7 +85,7 @@ def test_random_overlap_fast_faster_than_descriptive(self):
config = self.config.copy()
config.crossval.n_folds = 10

pp = PostProcessor(config)
pp = PostProcessor(config, translation_table=self.translation_table_path)
pp.num_runs_for_random_experiments = 100

eligible_genes = np.asarray([str(x) for x in range(0, 1000)])
Expand All @@ -96,8 +98,47 @@ def test_random_overlap_fast_faster_than_descriptive(self):
descriptive = timeit.timeit(lambda: pp.get_random_overlap(eligible_genes, kept_genes, algorithm="descriptive"), number=3)
self.assertLess(fast, descriptive)

def test_drugtarget(self):
def test_dge_missing_phenotype(self):

config = self.config.copy()
config.input.tag = "autism"

pp = PostProcessor(config, translation_table=self.translation_table_path)

with open(self.test_outer_results, "r") as file:
outer_results = json.load(file)

pp.outer_result = outer_results

self.assertIsNone(pp.dge(self.results_file))

def test_contingency_table(self):
import numpy as np
full_set = set((1, 2, 3, 4, 5, 6))
A = set((1, 2))
B = set((2, 3, 4))

testarray = np.array([[1, 2], [1, 2]])

array = self.pp.make_contingency_table(full_set, A, B)

self.assertTrue(np.equal(array, testarray).all())


@unittest.skipIf(NODATA, "nodata")
class PostProcessorTest(TestSetup):

def setUp(self):
super().setUp()
self.config.name = "TestPostProcessor"

self.pp = PostProcessor(self.config, translation_table=self.translation_table_path)

self.test_outer_results = "speos/tests/files/dummy_outer_results.json"
self.results_file = "speos/tests/files/dummy_inner_results.tsv"

def test_drugtarget(self):
# cant do without data
with open(self.test_outer_results, "r") as file:
outer_results = json.load(file)

Expand All @@ -106,7 +147,7 @@ def test_drugtarget(self):
self.pp.drugtarget(self.results_file)

def test_druggable(self):

# cant do without data
with open(self.test_outer_results, "r") as file:
outer_results = json.load(file)

Expand All @@ -115,7 +156,7 @@ def test_druggable(self):
self.pp.druggable(self.results_file)

def test_mouseKO(self):

# cant do without data
with open(self.test_outer_results, "r") as file:
outer_results = json.load(file)

Expand All @@ -124,7 +165,7 @@ def test_mouseKO(self):
self.pp.mouseKO(self.results_file)

def test_mouseKO_missing_phenotype(self):

# cant do without data
config = self.config.copy()
config.input.tag = "autism"

Expand All @@ -138,7 +179,7 @@ def test_mouseKO_missing_phenotype(self):
self.assertIsNone(pp.mouseKO(self.results_file))

def test_lof(self):

# cant do without data
with open(self.test_outer_results, "r") as file:
outer_results = json.load(file)

Expand All @@ -147,7 +188,7 @@ def test_lof(self):
lof, tukey = self.pp.lof_intolerance(self.results_file)

def test_pathwayea(self):

# cant do without data
with open(self.test_outer_results, "r") as file:
outer_results = json.load(file)

Expand All @@ -156,7 +197,7 @@ def test_pathwayea(self):
self.pp.pathway(self.results_file)

def test_hpoea(self):

# cant do without data
with open(self.test_outer_results, "r") as file:
outer_results = json.load(file)

Expand All @@ -165,50 +206,18 @@ def test_hpoea(self):
self.pp.hpo_enrichment(self.results_file)

def test_goea(self):

# cant do without data
with open(self.test_outer_results, "r") as file:
outer_results = json.load(file)

self.pp.outer_result = outer_results

self.pp.go_enrichment(self.results_file)

def test_dge_cad(self):

with open(self.test_outer_results, "r") as file:
outer_results = json.load(file)

self.pp.config.input.tag = "Cardiovascular_Disease"
self.pp.outer_result = outer_results

self.pp.dge(self.results_file)

def test_dge_missing_phenotype(self):

config = self.config.copy()
config.input.tag = "autism"

pp = PostProcessor(config)

with open(self.test_outer_results, "r") as file:
outer_results = json.load(file)

pp.outer_result = outer_results

self.assertIsNone(pp.dge(self.results_file))

def test_contingency_table(self):
import numpy as np
full_set = set((1, 2, 3, 4, 5, 6))
A = set((1, 2))
B = set((2, 3, 4))

testarray = np.array([[1, 2], [1, 2]])

array = self.pp.make_contingency_table(full_set, A, B)

self.assertTrue(np.equal(array, testarray).all())



if __name__ == '__main__':
unittest.main(warnings='ignore')
suite = unittest.TestSuite()
suite.addTest(PostProcessorTest('test_random_overlap_both_algorithms_identical_results'))
suite.run("result")
9 changes: 7 additions & 2 deletions speos/tests/test_preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,14 +691,19 @@ def test_directed_graph(self):
preprocessor.build_graph()

data = preprocessor.get_data()


edges_sender = [preprocessor.hgnc2id[sender] for sender in edges["geneA"]]
edges_receiver = [preprocessor.hgnc2id[receiver] for receiver in edges["geneB"]]

edge_index = data[2]["DummyDirectedGraph"]

self.assertEqual(edge_index[0, :].tolist(), edges_sender)
self.assertEqual(edge_index[1, :].tolist(), edges_receiver)
edges_sender, edges_receiver = (list(x) for x in zip(*sorted(zip(edges_sender, edges_receiver), key=lambda pair: pair[0])))

edges_sender_prepro, edges_receiver_prepro = (list(x) for x in zip(*sorted(zip(edge_index[0, :].tolist(), edge_index[1, :].tolist()), key=lambda pair: pair[0])))

self.assertEqual(edges_sender_prepro, edges_sender)
self.assertEqual(edges_receiver_prepro, edges_receiver)


if __name__ == '__main__':
Expand Down
12 changes: 5 additions & 7 deletions speos/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,17 @@ def setUp(self):
self.config.model.mp.n_layers = 1
self.config.model.mp.dim = 5
self.config.model.post_mp.n_layers = 0
self.config.model.post_mp.dim = 5


self.config.model.post_mp.dim = 5

self.setup_dirs = [self.purge_dir + "logs/", self.purge_dir + "results/", self.purge_dir + "plots", self.purge_dir + "models/", self.purge_dir + "data/"]



translation_table_path = "speos/tests/files/dummy_graph/dummy_translation_table.tsv"
expression_file_paths = ["speos/tests/files/dummy_graph/dummy_gtex_file.tsv", "speos/tests/files/dummy_graph/dummy_human_protein_atlas_file.tsv"]
self.translation_table_path = "speos/tests/files/dummy_graph/dummy_translation_table.tsv"
self.expression_file_paths = ["speos/tests/files/dummy_graph/dummy_gtex_file.tsv", "speos/tests/files/dummy_graph/dummy_human_protein_atlas_file.tsv"]

self.prepro_kwargs = {"translation_table": translation_table_path,
"expression_files": expression_file_paths}
self.prepro_kwargs = {"translation_table": self.translation_table_path,
"expression_files": self.expression_file_paths}

for dir in self.setup_dirs:
pathlib.Path(dir).mkdir(parents=True, exist_ok=True)
Expand Down

0 comments on commit 094227d

Please sign in to comment.