Skip to content

Commit

Permalink
Merge pull request #159 from pyt-team/frantzen-complex-shape
Browse files Browse the repository at this point in the history
Unify `shape` properties of complexes
  • Loading branch information
mhajij authored Jun 13, 2023
2 parents e1d1e9e + 1cc92c9 commit f1b89ae
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 30 deletions.
7 changes: 2 additions & 5 deletions test/classes/test_simplicial_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ def test_iterable_simplices(self):

def test_shape_property(self):
"""Test shape property."""
# Test the shape property of the SimplicialComplex class
sc = SimplicialComplex([[1, 2, 3], [2, 3, 4], [0, 1]])
self.assertEqual(sc.shape, [5, 6, 2])
self.assertEqual(sc.shape, (5, 6, 2))

def test_shape_empty(self):
"""Test shape property when Simplicial Complex is Empty."""
sc = SimplicialComplex()
self.assertEqual(sc.shape, None)
self.assertEqual(sc.shape, tuple())

def test_dim_property(self):
"""Test dim property."""
Expand Down
4 changes: 2 additions & 2 deletions toponetx/classes/combinatorial_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,15 @@ def incidence_dict(self):
return self._complex_set.hyperedge_dict

@property
def shape(self):
def shape(self) -> tuple[int, ...]:
"""Return shape.
This is:
(number of cells[i], for i in range(0,dim(CC)) )
Returns
-------
tuple
tuple of ints
"""
return self._complex_set.shape

Expand Down
10 changes: 8 additions & 2 deletions toponetx/classes/complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ def dim(self) -> int:

@property
@abc.abstractmethod
def shape(self) -> tuple:
"""Return number of cells for each rank."""
def shape(self) -> tuple[int, ...]:
"""Return number of cells for each rank.
Returns
-------
tuple of ints
The number of elements for each rank. If the complex is empty, an empty tuple is returned.
"""
pass

@abc.abstractmethod
Expand Down
18 changes: 6 additions & 12 deletions toponetx/classes/reportviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,9 @@ def __getitem__(self, hyperedge):
return self.hyperedge_dict[rank][hyperedge_]

@property
def shape(self):
def shape(self) -> tuple[int, ...]:
"""Compute shape."""
if len(self.hyperedge_dict) == 0:
print("Complex is empty.")
else:
return [len(self.hyperedge_dict[i]) for i in self.allranks]
return tuple(len(self.hyperedge_dict[i]) for i in self.allranks)

def __len__(self):
"""Compute number of nodes."""
Expand Down Expand Up @@ -521,18 +518,15 @@ def __getitem__(self, simplex):
return self.faces_dict[0][frozenset({simplex})]

@property
def shape(self):
def shape(self) -> tuple[int, ...]:
"""Return the number of simplices in each dimension.
Returns
-------
list
A list of integers representing the number of simplices in each dimension.
tuple of ints
A tuple of integers representing the number of simplices in each dimension.
"""
if len(self.faces_dict) == 0:
print("Complex is empty.")
else:
return [len(self.faces_dict[i]) for i in range(len(self.faces_dict))]
return tuple(len(self.faces_dict[i]) for i in range(len(self.faces_dict)))

def __len__(self):
"""Return the number of simplices in the SimplexView instance."""
Expand Down
15 changes: 6 additions & 9 deletions toponetx/classes/simplicial_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,19 @@ def __init__(self, simplices=None, name=None, **attr):
self._add_simplices_from(simplices)

@property
def shape(self):
def shape(self) -> tuple[int, ...]:
"""Shape of simplicial complex.
(number of simplices[i], for i in range(0,dim(Sc)) )
Returns
-------
tuple
tuple of ints
"""
if len(self._simplex_set.faces_dict) == 0:
print("Simplicial Complex is empty.")
else:
return [
len(self._simplex_set.faces_dict[i])
for i in range(len(self._simplex_set.faces_dict))
]
return tuple(
len(self._simplex_set.faces_dict[i])
for i in range(len(self._simplex_set.faces_dict))
)

@property
def dim(self) -> int:
Expand Down

0 comments on commit f1b89ae

Please sign in to comment.