Skip to content

Commit

Permalink
adding tests for explicit network plannings
Browse files Browse the repository at this point in the history
  • Loading branch information
RiesBen committed Sep 4, 2024
1 parent 2787cdd commit cef72f4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def generate_network_from_indices(
Parameters
----------
components : list of SmallMoleculeComponent
components : list of Components
the small molecules to place into the network
mapper: AtomMapper
the atom mapper to use to construct edges
Expand Down Expand Up @@ -119,15 +119,15 @@ def generate_network_from_indices(

def generate_network_from_names(
self,
ligands: list[Component],
components: list[Component],
names: list[tuple[str, str]],
) -> LigandNetwork:
"""
Generate a :class:`.LigandNetwork` by specifying edges as tuples of names.
Parameters
----------
ligands : list of SmallMoleculeComponent
components : list of Components
the small molecules to place into the network
mapper: AtomMapper
the atom mapper to use to construct edges
Expand All @@ -148,10 +148,10 @@ def generate_network_from_names(
if multiple molecules have the same name (this would otherwise be
problematic)
"""
nm2comp = {l.name: l for l in enumerate(ligands)}
nm2comp = {l.name: l for l in components}

if len(nm2comp) < len(ligands):
dupes = Counter((l.name for l in ligands))
if len(nm2comp) < len(components):
dupes = Counter((l.name for l in components))
dupe_names = [k for k, v in dupes.items() if v > 1]
raise ValueError(f"Duplicate names: {dupe_names}")

Check warning on line 156 in src/konnektor/network_planners/generators/explicit_network_generator.py

View check run for this annotation

Codecov / codecov/patch

src/konnektor/network_planners/generators/explicit_network_generator.py#L154-L156

Added lines #L154 - L156 were not covered by tests

Expand All @@ -165,7 +165,7 @@ def generate_network_from_names(
for nm in itertools.chain.from_iterable(names)
if nm not in nm2comp
]
available = [ligand.name for ligand in ligands]
available = [ligand.name for ligand in components]
raise KeyError(

Check warning on line 169 in src/konnektor/network_planners/generators/explicit_network_generator.py

View check run for this annotation

Codecov / codecov/patch

src/konnektor/network_planners/generators/explicit_network_generator.py#L168-L169

Added lines #L168 - L169 were not covered by tests
f"Invalid name(s) requested {badnames}. " f"Available: {available}"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,39 @@ def test_explicit_network_planner():
assert len(ligand_network.nodes) == n_compounds
assert len(ligand_network.edges) == (n_compounds * (n_compounds - 1)) // 2
assert get_is_connected(ligand_network)


def test_explicit_network_planner_with_indices():
n_compounds = 20
components, genMapper, genScorer = build_random_dataset(n_compounds=n_compounds)
indices = [(1, 2), (2, 3), (3, 4), (2, 5), (2, 6)]
unique_indices = set([i for e in indices for i in e])
planner = ExplicitNetworkGenerator(genMapper, genScorer, n_processes=1)

ligand_network = planner.generate_network_from_indices(
components=components, indices=indices
)

assert isinstance(ligand_network, LigandNetwork)
assert len(ligand_network.nodes) == len(unique_indices)
assert len(ligand_network.edges) == len(indices)
assert get_is_connected(ligand_network)


def test_explicit_network_planner_with_names():
n_compounds = 20
components, genMapper, genScorer = build_random_dataset(n_compounds=n_compounds)
print(components[0].name)
names = [("0", "1"), ("1", "2"), ("2", "3"), ("1", "5"), ("1", "6")]
unique_names = set([i for e in names for i in e])

planner = ExplicitNetworkGenerator(genMapper, genScorer, n_processes=1)

ligand_network = planner.generate_network_from_names(
components=components, names=names
)

assert isinstance(ligand_network, LigandNetwork)
assert len(ligand_network.nodes) == len(unique_names)
assert len(ligand_network.edges) == len(names)
assert get_is_connected(ligand_network)

0 comments on commit cef72f4

Please sign in to comment.