Skip to content

Commit

Permalink
Test: Fix tests in test_network.py except "test_init"
Browse files Browse the repository at this point in the history
  • Loading branch information
dileep-kishore committed Jan 22, 2021
1 parent 7c26550 commit f4a989c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 47 deletions.
18 changes: 10 additions & 8 deletions micone/validation/otu_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class HeaderType(BaseType):
"""
DataType that describes the expected structure and format for the sample headers
DataType that describes the expected structure and format for the sample headers
"""

def validate_header(self, value):
Expand Down Expand Up @@ -128,15 +128,17 @@ def validate_obsmeta_data(self, value):
query = filt_data[
~filt_data.str.contains(r"^[a-zA-Z0-9-._ ]+(?<! )$")
].any()
# TODO: FIXME:
# if query:
# raise ValidationError(
# "Invalid observation metadata. "
# f"Taxonomy names are not standard: {query} is not allowed in {level}"
# )
else:
elif level in ["Kingdom", "Phylum", "Class", "Order", "Family", "Genus"]:
query = filt_data[
~filt_data.str.contains(r"^[a-zA-Z0-9-._ ]+(?<! )$")
].any()
# TODO: FIXME:
# if query:
# raise ValidationError(
# "Invalid observation metadata. "
Expand All @@ -146,13 +148,13 @@ def validate_obsmeta_data(self, value):

class BiomType(BaseType):
"""
DataType that describes the expected structure and format for the `biom.Table`
DataType that describes the expected structure and format for the `biom.Table`
Parameters
----------
norm : bool, optional
True if abundances are normalized
Default value is False
Parameters
----------
norm : bool, optional
True if abundances are normalized
Default value is False
"""

def __init__(self, norm=False, *args, **kwargs):
Expand Down
85 changes: 46 additions & 39 deletions tests/main/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ def test_init(self, correlation_data):
obsmeta_data,
cmeta_data,
) in correlation_data["good"]:
# TODO: FIXME: __init__ was changed to take in nodes and links
network = Network(
corr_data, meta_data, cmeta_data, obsmeta_data, pval_data, child_data
)
n_nodes = corr_data.shape[0]
assert n_nodes == len(network.nodes)
assert (n_nodes ** 2 - n_nodes) // 2 == len(network.links)
assert len(network.filtered_links) <= len(network.links)
assert len(network.filter_links(True, True)) <= len(network.links)
assert all(key in network.metadata for key in meta_data)

def test_load_data(self, correlation_files):
Expand All @@ -52,66 +53,70 @@ def test_load_data(self, correlation_files):
n_nodes = corr_data.shape[0]
assert n_nodes == len(network.nodes)
assert (n_nodes ** 2 - n_nodes) // 2 == len(network.links)
assert len(network.filtered_links) <= len(network.links)
assert len(network.filter_links(True, True)) <= len(network.links)
assert all(key in network.metadata for key in meta_data)

def test_graph(self, correlation_data):
def test_graph(self, correlation_files):
for (
corr_data,
pval_data,
meta_data,
child_data,
obsmeta_data,
cmeta_data,
) in correlation_data["good"]:
network = Network(
corr_data, meta_data, cmeta_data, obsmeta_data, pval_data, child_data
corr_file,
pval_file,
meta_file,
child_file,
obsmeta_file,
cmeta_file,
) in correlation_files["good"]:
network = Network.load_data(
corr_file, meta_file, cmeta_file, obsmeta_file, pval_file, child_file
)
graph = network.graph
assert isinstance(graph, nx.Graph)
if network.metadata["directionality"] == "directed":
assert isinstance(graph, nx.DiGraph)
assert len(network.nodes) == graph.number_of_nodes()
assert len(network.links) == graph.number_of_edges()
with open(meta_file) as fid:
meta_data = json.load(fid)
assert all(key in graph.graph for key in meta_data)

def test_json(self, correlation_data):
def test_json(self, correlation_files):
for (
corr_data,
pval_data,
meta_data,
child_data,
obsmeta_data,
cmeta_data,
) in correlation_data["good"]:
network = Network(
corr_data, meta_data, cmeta_data, obsmeta_data, pval_data, child_data
corr_file,
pval_file,
meta_file,
child_file,
obsmeta_file,
cmeta_file,
) in correlation_files["good"]:
network = Network.load_data(
corr_file, meta_file, cmeta_file, obsmeta_file, pval_file, child_file
)
net_loaded = json.loads(network.json(threshold=False))
net_loaded = json.loads(network.json())
assert net_loaded["nodes"] == network.nodes
assert net_loaded["links"] == network.links
net_loaded_thres = json.loads(network.json(threshold=True))
net_loaded_thres = json.loads(network.json(True, True))
assert net_loaded_thres["nodes"] == network.nodes
assert net_loaded_thres["links"] == network.filtered_links
assert net_loaded_thres["links"] == network.filter_links(True, True)

def test_write_load_network(self, correlation_data, tmpdir):
def test_write_load_network(self, correlation_files, tmpdir):
for (
corr_data,
pval_data,
meta_data,
child_data,
obsmeta_data,
cmeta_data,
) in correlation_data["good"]:
network = Network(
corr_data, meta_data, cmeta_data, obsmeta_data, pval_data, child_data
corr_file,
pval_file,
meta_file,
child_file,
obsmeta_file,
cmeta_file,
) in correlation_files["good"]:
network = Network.load_data(
corr_file, meta_file, cmeta_file, obsmeta_file, pval_file, child_file
)
network_file = tmpdir.mkdir("test_write_load_network").join("network.json")
network.write(network_file, threshold=True)
network.write(network_file, pvalue_filter=True, interaction_filter=True)
network_loaded = Network.load_json(network_file)
assert network.metadata == network_loaded.metadata
assert network.nodes == network_loaded.nodes
assert network.filtered_links == network_loaded.filtered_links
assert network.filter_links(True, True) == network_loaded.filter_links(
True, True
)

def test_load_elist(self, network_elist_files):
for (
Expand Down Expand Up @@ -142,9 +147,11 @@ def fun(x: dict):
return frozenset([x["source"], x["target"]])

links1 = {
fun(x): (x["pvalue"], x["weight"]) for x in network_elist.filtered_links
fun(x): (f"{x['pvalue']:.5f}", f"{x['weight']:.5f}")
for x in network_elist.filter_links(True, True)
}
links2 = {
fun(x): (x["pvalue"], x["weight"]) for x in network_json.filtered_links
fun(x): (f"{x['pvalue']:.5f}", f"{x['weight']:.5f}")
for x in network_json.filter_links(True, True)
}
assert links1 == links2

0 comments on commit f4a989c

Please sign in to comment.