Skip to content

Commit

Permalink
Merge pull request #295 from pyt-team/frantzen-shadow-builtins
Browse files Browse the repository at this point in the history
Do not shadow built-in names
  • Loading branch information
mhajij authored Oct 30, 2023
2 parents d5867de + ce1e5aa commit ada1f19
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 66 deletions.
42 changes: 21 additions & 21 deletions toponetx/algorithms/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@


def s_connected_components(
complex: Complex, s: int = 1, cells: bool = True, return_singletons: bool = False
domain: Complex, s: int = 1, cells: bool = True, return_singletons: bool = False
) -> Generator[set[Hashable] | set[tuple[Hashable, ...]], None, None]:
"""Return generator for the s-connected components.
Parameters
----------
complex : Complex
domain : Complex
Supported complexes are cell/combintorial and hypegraphs.
s : int, optional
The number of intersections between pairwise consecutive cells.
Expand Down Expand Up @@ -73,26 +73,26 @@ def s_connected_components(
>>> CCC = CC.to_combinatorial_complex()
>>> list(s_connected_components(CCC, s=1,cells=False))
"""
if not isinstance(complex, (CellComplex, ColoredHyperGraph, CombinatorialComplex)):
raise TypeError(f"Input complex {complex} is not supported.")
if not isinstance(domain, (CellComplex, ColoredHyperGraph, CombinatorialComplex)):
raise TypeError(f"Input complex {domain} is not supported.")

if cells:
cell_dict, A = complex.all_cell_to_node_coadjacnecy_matrix(s=s, index=True)
cell_dict, A = domain.all_cell_to_node_coadjacnecy_matrix(s=s, index=True)
cell_dict = {v: k for k, v in cell_dict.items()}
G = nx.from_scipy_sparse_array(A)

for c in nx.connected_components(G):
if not return_singletons and len(c) == 1:
continue
if isinstance(complex, CellComplex):
if isinstance(domain, CellComplex):
yield {cell_dict[n] for n in c}
else:
yield {tuple(cell_dict[n]) for n in c}

else:
node_dict, A = complex.node_to_all_cell_adjacnecy_matrix(s=s, index=True)
if isinstance(complex, ColoredHyperGraph) and not isinstance(
complex, CombinatorialComplex
node_dict, A = domain.node_to_all_cell_adjacnecy_matrix(s=s, index=True)
if isinstance(domain, ColoredHyperGraph) and not isinstance(
domain, CombinatorialComplex
):
node_dict = {v: k[0] for k, v in node_dict.items()}
else:
Expand All @@ -102,22 +102,22 @@ def s_connected_components(
if not return_singletons:
if len(c) == 1:
continue
if isinstance(complex, CellComplex):
if isinstance(domain, CellComplex):
yield {node_dict[n] for n in c}
else:
yield {tuple(node_dict[n])[0] for n in c}


def s_component_subcomplexes(
complex: Complex, s: int = 1, cells: bool = True, return_singletons: bool = False
domain: Complex, s: int = 1, cells: bool = True, return_singletons: bool = False
) -> Generator[Complex, None, None]:
"""Return a generator for the induced subcomplexes of s_connected components.
Removes singletons unless return_singletons is set to True.
Parameters
----------
complex : Complex
domain : Complex
Supported complexes are cell/combintorial and hypegraphs.
s : int, optional
The number of intersections between pairwise consecutive cells.
Expand Down Expand Up @@ -149,25 +149,25 @@ def s_component_subcomplexes(
"""
for idx, c in enumerate(
s_connected_components(
complex, s=s, cells=cells, return_singletons=return_singletons
domain, s=s, cells=cells, return_singletons=return_singletons
)
):
if cells:
yield complex.restrict_to_cells(list(c))
yield domain.restrict_to_cells(list(c))
else:
yield complex.restrict_to_nodes(list(c))
yield domain.restrict_to_nodes(list(c))


def connected_components(
complex: Complex, cells: bool = False, return_singletons: bool = True
domain: Complex, cells: bool = False, return_singletons: bool = True
) -> Generator[set[Hashable] | set[tuple[Hashable, ...]], None, None]:
"""Compute s-connected components with s=1.
Same as s_connected_component` with s=1, but nodes returned.
Parameters
----------
complex : Complex
domain : Complex
Supported complexes are cell/combintorial and hypegraphs.
cells : bool, optional
If True will return cell components, if False will return node components.
Expand All @@ -193,19 +193,19 @@ def connected_components(
>>> CC.add_cell([4,5],rank=1)
>>> list(CC.connected_components(CC,cells=False))
"""
return s_connected_components(complex, s=1, cells=cells, return_singletons=True)
return s_connected_components(domain, s=1, cells=cells, return_singletons=True)


def connected_component_subcomplexes(
complex: Complex, return_singletons: bool = True
domain: Complex, return_singletons: bool = True
) -> Generator[Complex, None, None]:
"""Compute connected component subcomplexes with s=1.
Same as :meth:`s_component_subcomplexes` with s=1.
Parameters
----------
complex : Complex
domain : Complex
Supported complexes are cell/combintorial and hypegraphs.
return_singletons : bool, optional
When True, returns singletons connected components
Expand All @@ -228,4 +228,4 @@ def connected_component_subcomplexes(
>>> CC.add_cell([4,5],rank=1)
>>> list(connected_component_subcomplexes(CC))
"""
return s_component_subcomplexes(complex, return_singletons=return_singletons)
return s_component_subcomplexes(domain, return_singletons=return_singletons)
24 changes: 12 additions & 12 deletions toponetx/algorithms/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
__all__ = ["distance", "cell_distance"]


def distance(complex: Complex, source: Hashable, target: Hashable, s: int = 1) -> int:
def distance(domain: Complex, source: Hashable, target: Hashable, s: int = 1) -> int:
"""Return shortest s-walk distance between two nodes in the cell complex.
Parameters
----------
complex : Complex.
domain : Complex.
Supported complexes are cell/combintorial and hypegraphs.
source : Hashable
A node in the input complex.
Expand All @@ -31,7 +31,7 @@ def distance(complex: Complex, source: Hashable, target: Hashable, s: int = 1) -
Returns
-------
s_walk_distance : int
int
See Also
--------
Expand All @@ -58,8 +58,8 @@ def distance(complex: Complex, source: Hashable, target: Hashable, s: int = 1) -
>>> CHG = CC.to_colored_hypergraph()
>>> list(node_diameters(CHG))
"""
if not isinstance(complex, (CellComplex, CombinatorialComplex, ColoredHyperGraph)):
raise ValueError(f"Input complex {complex} is not supported.")
if not isinstance(domain, (CellComplex, CombinatorialComplex, ColoredHyperGraph)):
raise ValueError(f"Input complex {domain} is not supported.")
if isinstance(source, Cell):
source = source.elements
if isinstance(target, Cell):
Expand All @@ -68,7 +68,7 @@ def distance(complex: Complex, source: Hashable, target: Hashable, s: int = 1) -
source = tuple(source)
if isinstance(target, Iterable):
target = tuple(target)
rowdict, A = complex.node_to_all_cell_adjacnecy_matrix(index=True)
rowdict, A = domain.node_to_all_cell_adjacnecy_matrix(index=True)
G = nx.from_scipy_sparse_array(A)
try:
path = nx.shortest_path_length(G, rowdict[source], rowdict[target])
Expand All @@ -79,7 +79,7 @@ def distance(complex: Complex, source: Hashable, target: Hashable, s: int = 1) -


def cell_distance(
complex: Complex,
domain: Complex,
source: Iterable | HyperEdge | Cell,
target: Iterable | HyperEdge | Cell,
s: int = 1,
Expand All @@ -88,7 +88,7 @@ def cell_distance(
Parameters
----------
complex : Complex
domain : Complex
Supported complexes are cell/combintorial and hypegraphs.
source : Iterable or HyperEdge or Cell
An Iterable representing a cell in the input complex cell complex.
Expand Down Expand Up @@ -131,18 +131,18 @@ def cell_distance(
>>> CCC = CC.to_combinatorial_complex()
>>> cell_distance(CCC, frozenset({2, 3}) ,frozenset({6, 7}))
"""
if not isinstance(complex, (CellComplex, CombinatorialComplex, ColoredHyperGraph)):
raise ValueError(f"Input complex {complex} is not supported.")
if not isinstance(domain, (CellComplex, CombinatorialComplex, ColoredHyperGraph)):
raise ValueError(f"Input complex {domain} is not supported.")
if isinstance(source, (Cell, HyperEdge)):
source = source.elements
if isinstance(target, (Cell, HyperEdge)):
target = target.elements
if isinstance(complex, CellComplex):
if isinstance(domain, CellComplex):
if isinstance(source, Iterable):
source = tuple(source)
if isinstance(target, Iterable):
target = tuple(target)
cell_dict, A = complex.all_cell_to_node_coadjacnecy_matrix(s=s, index=True)
cell_dict, A = domain.all_cell_to_node_coadjacnecy_matrix(s=s, index=True)
G = nx.from_scipy_sparse_array(A)
try:
path_distance = nx.shortest_path_length(G, cell_dict[source], cell_dict[target])
Expand Down
38 changes: 19 additions & 19 deletions toponetx/algorithms/distance_measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
__all__ = ["node_diameters", "cell_diameters", "diameter", "cell_diameter"]


def node_diameters(complex: Complex) -> tuple[list[int], list[set[Hashable]]]:
def node_diameters(domain: Complex) -> tuple[list[int], list[set[Hashable]]]:
"""Return the node diameters of the connected components in cell complex.
Returns
Expand All @@ -32,9 +32,9 @@ def node_diameters(complex: Complex) -> tuple[list[int], list[set[Hashable]]]:
>>> CHG = CC.to_colored_hypergraph()
>>> list(node_diameters(CHG))
"""
node_dict, A = complex.node_to_all_cell_adjacnecy_matrix(index=True)
if isinstance(complex, ColoredHyperGraph) and not isinstance(
complex, CombinatorialComplex
node_dict, A = domain.node_to_all_cell_adjacnecy_matrix(index=True)
if isinstance(domain, ColoredHyperGraph) and not isinstance(
domain, CombinatorialComplex
):
node_dict = {v: k[0] for k, v in node_dict.items()}
else:
Expand All @@ -53,12 +53,12 @@ def node_diameters(complex: Complex) -> tuple[list[int], list[set[Hashable]]]:
return diams, comps


def cell_diameters(complex: Complex, s: int = 1) -> tuple[list[int], list[set[int]]]:
def cell_diameters(domain: Complex, s: int = 1) -> tuple[list[int], list[set[int]]]:
"""Return the cell diameters of the s_cell_connected component subgraphs.
Parameters
----------
complex : Complex
domain : Complex
Supported complexes are cell/combintorial and hypegraphs.
s : int, optional
The number of intersections between pairwise consecutive cells.
Expand All @@ -82,9 +82,9 @@ def cell_diameters(complex: Complex, s: int = 1) -> tuple[list[int], list[set[in
>>> CHG = CC.to_colored_hypergraph()
>>> list(cell_diameters(CHG))
"""
if not isinstance(complex, (CellComplex, CombinatorialComplex, ColoredHyperGraph)):
raise TypeError(f"Input complex {complex} is not supported.")
coldict, A = complex.all_cell_to_node_coadjacnecy_matrix(index=True)
if not isinstance(domain, (CellComplex, CombinatorialComplex, ColoredHyperGraph)):
raise TypeError(f"Input complex {domain} is not supported.")
coldict, A = domain.all_cell_to_node_coadjacnecy_matrix(index=True)
coldict = {v: k for k, v in coldict.items()}

G = nx.from_scipy_sparse_array(A)
Expand All @@ -100,12 +100,12 @@ def cell_diameters(complex: Complex, s: int = 1) -> tuple[list[int], list[set[in
return diams, comps


def diameter(complex: Complex) -> int:
def diameter(domain: Complex) -> int:
"""Return length of the longest shortest s-walk between nodes.
Parameters
----------
complex : Complex
domain : Complex
Supported complexes are cell/combintorial and hypegraphs.
Returns
Expand Down Expand Up @@ -136,21 +136,21 @@ def diameter(complex: Complex) -> int:
>>> CHG = CC.to_colored_hypergraph()
>>> diameter(CHG)
"""
if not isinstance(complex, (CellComplex, CombinatorialComplex, ColoredHyperGraph)):
raise TypeError(f"Input complex {complex} is not supported.")
A = complex.node_to_all_cell_adjacnecy_matrix()
if not isinstance(domain, (CellComplex, CombinatorialComplex, ColoredHyperGraph)):
raise TypeError(f"Input complex {domain} is not supported.")
A = domain.node_to_all_cell_adjacnecy_matrix()
G = nx.from_scipy_sparse_array(A)
if nx.is_connected(G):
return nx.diameter(G)
raise RuntimeError("cc is not connected.")


def cell_diameter(complex: Complex, s: int = None) -> int:
def cell_diameter(domain: Complex, s: int = None) -> int:
"""Return the length of the longest shortest s-walk between cells.
Parameters
----------
complex : Complex
domain : Complex
Supported complexes are cell/combintorial and hypegraphs.
s : int, optional
The number of intersections between pairwise consecutive cells.
Expand Down Expand Up @@ -183,9 +183,9 @@ def cell_diameter(complex: Complex, s: int = None) -> int:
>>> CHG = CC.to_colored_hypergraph()
>>> cell_diameter(CHG)
"""
if not isinstance(complex, (CellComplex, CombinatorialComplex, ColoredHyperGraph)):
raise TypeError(f"Input complex {complex} is not supported.")
A = complex.all_cell_to_node_coadjacnecy_matrix()
if not isinstance(domain, (CellComplex, CombinatorialComplex, ColoredHyperGraph)):
raise TypeError(f"Input complex {domain} is not supported.")
A = domain.all_cell_to_node_coadjacnecy_matrix()
G = nx.from_scipy_sparse_array(A)
if nx.is_connected(G):
return nx.diameter(G)
Expand Down
Loading

0 comments on commit ada1f19

Please sign in to comment.