Skip to content

Commit

Permalink
Fix bug fetching gii-label map. Satisfy Flake8. vertices_labels->`v…
Browse files Browse the repository at this point in the history
…ertex_labels`
  • Loading branch information
AhmetNSimsek committed Dec 5, 2023
1 parent c78ab0a commit e39fadf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
7 changes: 3 additions & 4 deletions examples/02_maps_and_templates/005_access_surface_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@
# first one from the list. Now let us fetch a specific variant and also the
# hemisphere fragment
mesh = mp.fetch(variant="inflated", fragment="left")

# The returned structure is a dictionary of three numpy arrays representing the
# vertices, faces, and labels respectively. Each vertex defines a 3D surface
mesh
# The returned structure is a Surface consiting of three numpy arrays representing the
# vertices, faces, and vertex_labels respectively. Each vertex defines a 3D surface
# point, while the faces are triplets of indices into the list of vertices,
# defining surface triangles. The labels provide the label index associated with
# each vertex.
print(mesh.keys())

# %%
# Most meshes are shipped with a color map which we can fetch from the map
Expand Down
50 changes: 32 additions & 18 deletions siibra/surface/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,43 @@
from io import BytesIO
from functools import wraps


class Surface:
def __init__(self, vertices: List[List[float]]=None,
faces: List[List[int]]=None,
vertices_labels: List[List[Union[float, int]]]=None,
**kwargs) -> None:
"""Describes a common interface to surface, where vertices
def __init__(
self,
vertices: List[List[float]] = None,
faces: List[List[int]] = None,
vertex_labels: List[List[Union[float, int]]] = None,
**kwargs,
) -> None:
"""
Describes a common interface to surface, where vertices
faces are defined, and optionally, vertices labels, face labels
can be defined.
Args:
vertices (List[List[float]] or ndarray): verticies triplet
faces: (List[List[int]] or ndarray): faces triplet
vertices_labels: (Labels)"""

self.vertices = np.array(kwargs.get("verts", []) if vertices is None else vertices)
Parameters
----------
vertices (List[List[float]] or ndarray): verticies triplet
faces: (List[List[int]] or ndarray): faces triplet
vertex_labels: (Labels)
"""

self.vertices = np.array(
kwargs.get("verts", []) if vertices is None else vertices
)
self.faces = np.array([] if faces is None else faces, dtype=np.uint64)

vertices_labels = kwargs.get("labels", []) if vertices_labels is None else vertices_labels
self.vertices_labels = np.array(vertices_labels)
vertex_labels = (
kwargs.get("labels", []) if vertex_labels is None else vertex_labels
)
self.vertex_labels = np.array(vertex_labels)

def __contains__(self, spec):
self.faces
if spec in ("verts", "faces"):
return True
if spec == "labels":
return len(self.vertices_labels) > 0
return len(self.vertex_labels) > 0
return False

def __getitem__(self, spec: str):
Expand All @@ -38,10 +49,10 @@ def __getitem__(self, spec: str):
if spec == "faces":
return self.faces
if spec == "labels":
if len(self.vertices_labels) > 0:
return self.vertices_labels
if len(self.vertex_labels) > 0:
return self.vertex_labels
raise IndexError(f"spec {spec!r} not found in class surface")

def export(self, export_dest: Union[str, IO]):
mesh = trimesh.Trimesh(self.vertices, self.faces)
mesh.export(export_dest)
Expand All @@ -53,11 +64,14 @@ def to_bytes(self):
io.seek(0)
return io.read()


def wrap_return_surface():
def outer(fn):
@wraps(fn)
def inner(*args, **kwargs):
return_val = fn(*args, **kwargs)
return Surface(**return_val)

return inner

return outer
5 changes: 2 additions & 3 deletions siibra/volumes/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,8 @@ def fetch(
try:
if fmt == "gii-label":
tpl = self.space.get_template(variant=kwargs.get('variant'))
mesh = tpl.fetch(**kwargs)
labels = self._providers[fmt].fetch(**fwd_args)
result = dict(**mesh, **labels)
result = tpl.fetch(format='mesh', **kwargs)
result.vertex_labels = self._providers[fmt].fetch(**fwd_args).get('labels')
else:
result = self._providers[fmt].fetch(**fwd_args)
except requests.SiibraHttpRequestError as e:
Expand Down

0 comments on commit e39fadf

Please sign in to comment.