Skip to content

Commit

Permalink
fix tests and remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
js51 committed Mar 30, 2023
1 parent 396f1eb commit 243cdd4
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 142 deletions.
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"python.formatting.provider": "black"
"python.formatting.provider": "black",
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
1 change: 0 additions & 1 deletion splitp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Python imports
from splitp.phylogeny import *
from splitp.enums import *
from splitp import alignment
from splitp import constants
from splitp import constructions
from splitp import model
Expand Down
4 changes: 0 additions & 4 deletions splitp/alignment.py

This file was deleted.

1 change: 0 additions & 1 deletion splitp/matrices.py → splitp/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
def is_sparse(matrix):
return scipy.sparse.issparse(matrix)


def frobenius_norm(matrix, data_table=None):
"""Calculates the Frobenius Norm for a given matrix"""
if data_table is not None:
Expand Down
126 changes: 0 additions & 126 deletions splitp/misc.py

This file was deleted.

9 changes: 8 additions & 1 deletion splitp/parsers/newick.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

def newick_to_json(
newick_string,
namestring="id",
Expand Down Expand Up @@ -113,4 +115,9 @@ def move_tree_edge_labels_to_nodes(tree, remove_edge_attributes=True):
if remove_edge_attributes:
tree[in_edge[0]][in_edge[1]].clear()

return tree
return tree

def strip_newick(newick):
regex = re.compile("(:|(?<=\)))[^,)]*?((?=(\)|,)|;))")
newick = regex.sub("", newick)
return newick
8 changes: 4 additions & 4 deletions splitp/phylogenetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from itertools import combinations
import networkx as nx
from networkx import dfs_postorder_nodes, bfs_successors
from splitp import splits, matrices
from splitp import matrix, splits
import scipy


Expand Down Expand Up @@ -272,7 +272,7 @@ def __dense_split_score(matrix, k=None, singularValues=False, force_frob_norm=Fa
return (
1
- (sum(val**2 for val in singular_values[0:4]))
/ (matrices.frobrenius_norm(matrix) ** 2)
/ (matrix.frobrenius_norm(matrix) ** 2)
) ** (1 / 2)
else:
min_shape = min(matrix.shape)
Expand All @@ -292,7 +292,7 @@ def __sparse_split_score(
matrix, 4, return_singular_vectors=False
)
squared_singular_values = [sigma**2 for sigma in largest_four_singular_values]
norm = matrices.frobenius_norm(matrix, data_table=data_table_for_frob_norm)
norm = matrix.frobenius_norm(matrix, data_table=data_table_for_frob_norm)
return (1 - (sum(squared_singular_values) / (norm**2))) ** (1 / 2)


Expand All @@ -302,7 +302,7 @@ def split_score(
force_frob_norm_on_dense=False,
data_table_for_frob_norm=None,
):
if matrices.is_sparse(matrix):
if matrix.is_sparse(matrix):
return __sparse_split_score(
matrix, return_singular_values, data_table_for_frob_norm
)
Expand Down
22 changes: 21 additions & 1 deletion splitp/phylogeny.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ def __str__(self):
json_graph.tree_data(self.networkx_graph, self.root(return_index=False))
)

def unrooted_networkx_graph(self):
"""Return the unrooted version of the tree"""
# Make a copy of the graph
unrooted_graph = self.networkx_graph.copy()
# Get the root node
root_node = self.root(return_index=False)
# If the root node has more than 2 children, return the original graph
if len(list(unrooted_graph.successors(root_node))) > 2:
return unrooted_graph.to_undirected()
# Get the root node children
root_children = list(unrooted_graph.successors(self.root(return_index=False)))
# Remove the root node
unrooted_graph.remove_node(self.root(return_index=False))
# Connect the two root node children
unrooted_graph.add_edge(root_children[0], root_children[1])
# Make undirected
unrooted_graph = unrooted_graph.to_undirected()
# Return the unrooted graph
return unrooted_graph

def reassign_transition_matrices(self, transition_matrix):
"""DEPRECATED: Reassign transition matrices to all nodes in the tree"""
for node in self.networkx_graph.nodes:
Expand Down Expand Up @@ -164,4 +184,4 @@ def draw(self, draw_format=DrawFormat.ASCII):
elif draw_format == DrawFormat.matplotlib:
draw(tree, branch_labels=lambda c: c.branch_length)
else:
raise ValueError("Invalid format.")
raise ValueError("Invalid format.")
10 changes: 7 additions & 3 deletions tests/test_phylogenetics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from splitp.parsers import newick, fasta
import networkx as nx
from splitp.phylogeny import Phylogeny
from splitp.misc import strip_newick
import splitp.phylogenetics

def test_newick_string_from_splits(test_cases_trees):
Expand All @@ -11,5 +11,9 @@ def test_newick_string_from_splits(test_cases_trees):
phylogeny = Phylogeny(true_newick_string)
splits = list(phylogeny.splits())
newick_string = splitp.phylogenetics.newick_string_from_splits(splits)
test_splits = list(Phylogeny(newick_string).splits())
assert set(test_splits) == set(splits)
# Now check if the two Phylogenies are isomorphic
ph1 = Phylogeny(newick.strip_newick(newick_string)).unrooted_networkx_graph()
ph2 = Phylogeny(newick.strip_newick(true_newick_string)).unrooted_networkx_graph()
print(ph1.edges())
print(ph2.edges())
assert nx.is_isomorphic(ph1, ph2)

0 comments on commit 243cdd4

Please sign in to comment.