Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding tutorial #26

Merged
merged 5 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/classes/test_cell2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_fit_and_get_embedding(self):
dc = Cell2Vec(dimensions=5)

# Fit the Cell2Vec object to the graph and get embedding for nodes (using adjacency matrix A0)
dc.fit(cx, neighborhood_type="adj", neighborhood_dim={"adj": 0, "coadj": -1})
dc.fit(cx, neighborhood_type="adj", neighborhood_dim={"dim": 0, "codim": -1})

# Check that the shape of the embedding is correct
assert dc.get_embedding().shape == (len(cx.nodes), 5)
Expand Down
2 changes: 1 addition & 1 deletion test/classes/test_deepcell.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_fit_and_get_embedding(self):
dc = DeepCell(walk_number=5, walk_length=10, dimensions=2)

# Fit the DeepCell object to the graph and get embedding for edges (using adjacency matrix A1)
dc.fit(cx, neighborhood_type="adj", neighborhood_dim={"adj": 1, "coadj": -1})
dc.fit(cx, neighborhood_type="adj", neighborhood_dim={"dim": 1, "codim": -1})

# Check that the shape of the embedding is correct
assert dc.get_embedding().shape == (len(cx.edges), 2)
Expand Down
2 changes: 1 addition & 1 deletion test/classes/test_higher_order_laplacian_eigenmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_fit_and_get_embedding(self):
dc = HigherOrderLaplacianEigenmaps(dimensions=5)

# Fit the Cell2Vec object to the graph and get embedding for nodes (using adjacency matrix A0)
dc.fit(cx, neighborhood_type="adj", neighborhood_dim={"adj": 0, "coadj": -1})
dc.fit(cx, neighborhood_type="adj", neighborhood_dim={"dim": 0, "codim": -1})

# Check that the shape of the embedding is correct
assert dc.get_embedding().shape == (len(cx.nodes), 5)
Expand Down
2 changes: 1 addition & 1 deletion test/classes/test_hoglee.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_fit_and_get_embedding(self):
dc = HOGLEE(dimensions=5)

# Fit the Cell2Vec object to the graph and get embedding for nodes (using adjacency matrix A0)
dc.fit(cx, neighborhood_type="adj", neighborhood_dim={"adj": 0, "coadj": -1})
dc.fit(cx, neighborhood_type="adj", neighborhood_dim={"dim": 0, "codim": -1})

# Check that the shape of the embedding is correct
assert dc.get_embedding().shape == (len(cx.nodes), 5 + 1)
Expand Down
2 changes: 1 addition & 1 deletion test/test_neighborhood.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_neighborhood_from_complex_raise_error(self):

assert (
str(e.value)
== """Input Complex can only be a Simplicial, Cell or Combinatorial Complex."""
== """Input Complex can only be a SimplicialComplex, CellComplex, PathComplex ColoredHyperGraph or CombinatorialComplex."""
)

def test_neighborhood_from_complex_matrix_dimension_cell_complex(self):
Expand Down
23 changes: 13 additions & 10 deletions topoembedx/classes/cell2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def __init__(
self.ind = []

def fit(
self, complex, neighborhood_type="adj", neighborhood_dim={"adj": 0, "coadj": -1}
self, complex, neighborhood_type="adj", neighborhood_dim={"dim": 0, "codim": -1}
):
"""Fit a Cell2Vec model.

Expand All @@ -100,18 +100,21 @@ def fit(
- "adj" for adjacency matrix.
- "coadj" for coadjacency matrix.
neighborhood_dim : dict
The dimensions of the neighborhood to use.
If `neighborhood_type` is "adj", dimension is `neighborhood_dim["adj"]`.
If `neighborhood_type` is "coadj", dimension is `neighborhood_dim["coadj"]`
and `neighborhood_dim["adj"]` specifies the dimension of the ambient space.
The integer parmaters needed to specify the neighborhood of the cells to generate the embedding.
In TopoNetX (co)adjacency neighborhood matrices are specified via one or two parameters.
- For Cell/Simplicial/Path complexes (co)adjacency matrix is specified by a single parameter, this is precisely
neighborhood_dim["dim"]
- For Combinatorial/ColoredHyperGraph the (co)adjacency matrix is specified by a single parameter, this is precisely
neighborhood_dim["dim"] and neighborhood_dim["codim"]

Notes
-----
Here, neighborhood_dim={"adj": 1, "coadj": -1} specifies the dimension for
which the cell embeddings are going to be computed.
The integer "adj": 1 means that the embeddings will be computed for the first dim.
The integer "coadj": -1 is ignored and only considered,
when the input complex is a combinatorial complex.
Here neighborhood_dim={"dim": 1, "codim": -1} specifies the dimension for
which the cell embeddings are going to be computed.
"dim": 1 means that the embeddings will be computed for the first dimension.
The integer "codim": -1 is ignored when the input is cell/simplicial complex
and must be specified when the input complex is a combinatorial complex or
colored hypergraph.

Returns
-------
Expand Down
20 changes: 12 additions & 8 deletions topoembedx/classes/cell_diff2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,21 @@ def fit(
neighborhood_type : str
The type of neighborhood to compute. "adj" for adjacency matrix, "coadj" for coadjacency matrix.
neighborhood_dim : dict
The dimensions of the neighborhood to use. If `neighborhood_type` is "adj", the dimension is
`neighborhood_dim["adj"]`. If `neighborhood_type` is "coadj", the dimension is `neighborhood_dim["coadj"]`
and `neighborhood_dim["adj"]` specifies the dimension of the ambient space.
The integer parmaters needed to specify the neighborhood of the cells to generate the embedding.
In TopoNetX (co)adjacency neighborhood matrices are specified via one or two parameters.
- For Cell/Simplicial/Path complexes (co)adjacency matrix is specified by a single parameter, this is precisely
neighborhood_dim["dim"]
- For Combinatorial/ColoredHyperGraph the (co)adjacency matrix is specified by a single parameter, this is precisely
neighborhood_dim["dim"] and neighborhood_dim["codim"]

Notes
-----
Here, neighborhood_dim={"adj": 1, "coadj": -1} specifies the dimension for
which the cell embeddings are going to be computed.
The integer "adj": 1 means that the embeddings will be computed for the first dimension.
The integer "coadj": -1 is ignored and only considered
when the input complex is a combinatorial complex.
Here neighborhood_dim={"dim": 1, "codim": -1} specifies the dimension for
which the cell embeddings are going to be computed.
"dim": 1 means that the embeddings will be computed for the first dimension.
The integer "codim": -1 is ignored when the input is cell/simplicial complex
and must be specified when the input complex is a combinatorial complex or
colored hypergraph.

Returns
-------
Expand Down
36 changes: 29 additions & 7 deletions topoembedx/classes/higher_order_laplacian_eigenmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,35 @@ def fit(

Parameters
----------
complex : SimplicialComplex, CellComplex, CombinatorialComplex, or CombinatorialComplex
The complex to be embedded.
neighborhood_type : str, optional
The type of neighborhood to use, by default "adj".
neighborhood_dim : dict, optional
The dimension of the neighborhood to use.
Default: {"adj": 0, "coadj": -1}
complex : TopoNetX object
A complex object. The complex object can be one of the following:
- CellComplex
- CombinatorialComplex
- CombinatorialComplex
mhajij marked this conversation as resolved.
Show resolved Hide resolved
- SimplicialComplex
- DynamicSimplicialComplex
neighborhood_type : str
The type of neighborhood to compute. "adj" for adjacency matrix, "coadj" for coadjacency matrix.
neighborhood_dim : dict
The integer parmaters needed to specify the neighborhood of the cells to generate the embedding.
In TopoNetX (co)adjacency neighborhood matrices are specified via one or two parameters.
- For Cell/Simplicial/Path complexes (co)adjacency matrix is specified by a single parameter, this is precisely
neighborhood_dim["dim"]
- For Combinatorial/ColoredHyperGraph the (co)adjacency matrix is specified by a single parameter, this is precisely
neighborhood_dim["dim"] and neighborhood_dim["codim"]

Notes
-----
Here neighborhood_dim={"dim": 1, "codim": -1} specifies the dimension for
which the cell embeddings are going to be computed.
"dim": 1 means that the embeddings will be computed for the first dimension.
The integer "codim": -1 is ignored when the input is cell/simplicial complex
and must be specified when the input complex is a combinatorial complex or
colored hypergraph.

Returns
-------
None
"""
self.ind, self.A = neighborhood_from_complex(
complex, neighborhood_type, neighborhood_dim
Expand Down
22 changes: 13 additions & 9 deletions topoembedx/classes/hoglee.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, dimensions: int = 3, seed: int = 42):
self.ind = []

def fit(
self, complex, neighborhood_type="adj", neighborhood_dim={"adj": 0, "coadj": -1}
self, complex, neighborhood_type="adj", neighborhood_dim={"dim": 0, "codim": -1}
):
"""Fit a Higher Order Geometric Laplacian EigenMaps model.

Expand All @@ -40,17 +40,21 @@ def fit(
neighborhood_type : str
The type of neighborhood to compute. "adj" for adjacency matrix, "coadj" for coadjacency matrix.
neighborhood_dim : dict
The dimensions of the neighborhood to use. If `neighborhood_type` is "adj", the dimension is
`neighborhood_dim["adj"]`. If `neighborhood_type` is "coadj", the dimension is `neighborhood_dim["coadj"]`
and `neighborhood_dim["adj"]` specifies the dimension of the ambient space.
The integer parmaters needed to specify the neighborhood of the cells to generate the embedding.
In TopoNetX (co)adjacency neighborhood matrices are specified via one or two parameters.
- For Cell/Simplicial/Path complexes (co)adjacency matrix is specified by a single parameter, this is precisely
neighborhood_dim["dim"]
- For Combinatorial/ColoredHyperGraph the (co)adjacency matrix is specified by a single parameter, this is precisely
neighborhood_dim["dim"] and neighborhood_dim["codim"]

Notes
-----
Here, neighborhood_dim={"adj": 1, "coadj": -1} specifies the dimension for
which the cell embeddings are going to be computed.
The integer "adj": 1 means that the embeddings will be computed for the first dimension.
The integer "coadj": -1 is ignored and only considered
when the input complex is a combinatorial complex.
Here neighborhood_dim={"dim": 1, "codim": -1} specifies the dimension for
which the cell embeddings are going to be computed.
"dim": 1 means that the embeddings will be computed for the first dimension.
The integer "codim": -1 is ignored when the input is cell/simplicial complex
and must be specified when the input complex is a combinatorial complex or
colored hypergraph.

Returns
-------
Expand Down
50 changes: 30 additions & 20 deletions topoembedx/neighborhood.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
"""Functions for computing neighborhoods of a complex."""

from toponetx.classes import CellComplex, CombinatorialComplex, SimplicialComplex
from toponetx.classes import (
CellComplex,
ColoredHyperGraph,
CombinatorialComplex,
PathComplex,
SimplicialComplex,
)


def neighborhood_from_complex(
complex, neighborhood_type="adj", neighborhood_dim={"adj": 0, "coadj": -1}
complex, neighborhood_type="adj", neighborhood_dim={"dim": 0, "codim": -1}
):
"""Compute the neighborhood of a complex.

Expand All @@ -14,22 +20,26 @@ def neighborhood_from_complex(

Parameters
----------
complex : SimplicialComplex or CellComplex or CombinatorialComplex
complex : CellComplex, CombinatorialComplex, SimplicialComplex, ColoredHyperGraph, PathComplex
The complex to compute the neighborhood for.
neighborhood_type : str
The type of neighborhood to compute. "adj" for adjacency matrix, "coadj" for coadjacency matrix.
neighborhood_dim : dict
The dimensions of the neighborhood to use.
If `neighborhood_type` is "adj", the dimension is `neighborhood_dim["adj"]`.
If `neighborhood_type` is "coadj", the dimension is `neighborhood_dim["coadj"]`
and `neighborhood_dim["adj"]` specifies the dimension of the ambient space.
The integer parmaters needed to specify the neighborhood of the cells to generate the embedding.
In TopoNetX (co)adjacency neighborhood matrices are specified via one or two parameters.
- For Cell/Simplicial/Path complexes (co)adjacency matrix is specified by a single parameter, this is precisely
neighborhood_dim["dim"]
- For Combinatorial/ColoredHyperGraph the (co)adjacency matrix is specified by a single parameter, this is precisely
neighborhood_dim["dim"] and neighborhood_dim["codim"]

Note:
here neighborhood_dim={"adj": 1, "coadj": -1} specifies the dimension for
which the cell embeddings are going to be computed.
"adj": 1 means that the embeddings will be computed for the first dimension.
The integer "coadj": -1 is ignored and only considered
when the input complex is a combinatorial complex.
Notes
-----
here neighborhood_dim={"dim": 1, "codim": -1} specifies the dimension for
which the cell embeddings are going to be computed.
"dim": 1 means that the embeddings will be computed for the first dimension.
The integer "codim": -1 is ignored when the input is cell/simplicial complex
and must be specified when the input complex is a combinatorial complex or
colored hypergraph.

Returns
-------
Expand All @@ -43,24 +53,24 @@ def neighborhood_from_complex(
ValueError
If the input `complex` is not a SimplicialComplex, CellComplex or CombinatorialComplex
"""
if isinstance(complex, SimplicialComplex) or isinstance(complex, CellComplex):
if isinstance(complex, (SimplicialComplex, CellComplex, PathComplex)):
if neighborhood_type == "adj":
ind, A = complex.adjacency_matrix(neighborhood_dim["adj"], index=True)
ind, A = complex.adjacency_matrix(neighborhood_dim["dim"], index=True)

else:
ind, A = complex.coadjacency_matrix(neighborhood_dim["adj"], index=True)
elif isinstance(complex, CombinatorialComplex):
ind, A = complex.coadjacency_matrix(neighborhood_dim["dim"], index=True)
elif isinstance(complex, (CombinatorialComplex, ColoredHyperGraph)):
if neighborhood_type == "adj":
ind, A = complex.adjacency_matrix(
neighborhood_dim["adj"], neighborhood_dim["coadj"], index=True
neighborhood_dim["dim"], neighborhood_dim["codim"], index=True
)
else:
ind, A = complex.coadjacency_matrix(
neighborhood_dim["coadj"], neighborhood_dim["adj"], index=True
neighborhood_dim["codim"], neighborhood_dim["codim"], index=True
)
else:
raise TypeError(
"""Input Complex can only be a Simplicial, Cell or Combinatorial Complex."""
"""Input Complex can only be a SimplicialComplex, CellComplex, PathComplex ColoredHyperGraph or CombinatorialComplex."""
)

return ind, A
Loading