Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
10 changes: 7 additions & 3 deletions neurocollage/collage.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ def plot_3d_collage(
centerline,
sample=10,
filename=None,
show=False,
show=True,
):
"""Plot 3d collage with trimesh."""
mesh_helper = MeshHelper(atlas_path, region, hemisphere)
Expand All @@ -470,9 +470,13 @@ def plot_3d_collage(
mesh_path = Path(atlas_path["structure"]).parent / boundary["path"]
if mesh_path.is_dir():
for _mesh_path in mesh_path.iterdir():
boundary_meshes.append(trimesh.load_mesh(_mesh_path))
_mesh = trimesh.load_mesh(_mesh_path)
_mesh.visual.face_colors = [100, 100, 100, 100]
boundary_meshes.append(_mesh)
else:
boundary_meshes.append(trimesh.load_mesh(mesh_path))
_mesh = trimesh.load_mesh(mesh_path)
_mesh.visual.face_colors = [100, 100, 100, 100]
boundary_meshes.append(_mesh)

data = boundary_meshes + plane_data + cell_data + centerline_data
mesh_helper.render(data=data, filename=filename)
Expand Down
36 changes: 8 additions & 28 deletions neurocollage/mesh_helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""3D collage module."""

from copy import deepcopy

import matplotlib
Expand Down Expand Up @@ -35,7 +36,6 @@ def __init__(self, atlas_path, region, hemisphere=None):
self.hemisphere = hemisphere

self._layer_annotation = None
self._boundary_mask = None
self._depths = None

@property
Expand Down Expand Up @@ -98,38 +98,18 @@ def get_pia_mesh(self, cutoff=3):
data[data > cutoff * np.mean(abs(self.depths.voxel_dimensions))] = 0
data[np.isnan(data)] = 0

mesh = self._get_mesh(VoxelGrid(data), self.boundary_mask)
mesh = self._get_mesh(VoxelGrid(data))
mesh.visual.face_colors = [0, 0, 255, 100]
return mesh

@staticmethod
def _get_mesh(vg, mask=None):
"""Get a mesh."""
def _get_mesh(self, vg):
"""Get a mesh within a region."""
mesh = vg.marching_cubes
if mask is not None:
tri_indices = vg.points_to_indices(mesh.triangles_center)
mesh.update_faces(~mask[tuple(tri_indices.T)])
mask = VoxelGrid(self.annotation.raw).matrix
tri_indices = vg.points_to_indices(mesh.triangles_center)
mesh.update_faces(mask[tuple(tri_indices.T)])
return mesh

@property
def boundary_mask(self):
"""Get a mask of inner and boundary voxel of a region."""
if self._boundary_mask is None:
m = VoxelGrid(self.annotation.raw).matrix
outer_vg = VoxelGrid(self.annotation.raw)
outer_vg.encoding.data[m == 0] = -1000
outer_vg.encoding.data[m > 0] = 0
d1 = outer_vg.matrix
d2 = outer_vg.matrix
d1[:-1] += d2[1:]
d1[1:] += d2[:-1]
d1[:, :-1] += d2[:, 1:]
d1[:, 1:] += d2[:, :-1]
d1[:, :, :-1] += d2[:, :, 1:]
d1[:, :, 1:] += d2[:, :, :-1]
self._boundary_mask = d1 > 0
return self._boundary_mask

def get_layer_meshes(self, alpha=0.5, colors=None):
"""Get layer meshes."""
if colors is None:
Expand All @@ -140,7 +120,7 @@ def get_layer_meshes(self, alpha=0.5, colors=None):
data = self.annotation.raw
vg = VoxelGrid(data)
vg.encoding.data[data != layer] = False
mesh = self._get_mesh(vg, self.boundary_mask)
mesh = self._get_mesh(vg)
color = [int(255 * v) for v in matplotlib.colors.to_rgb(colors[i - 1])]
color.append(255 * alpha)
mesh.visual.face_colors = color
Expand Down
9 changes: 6 additions & 3 deletions neurocollage/planes.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ def slice_n_cells(cells, n_cells, random_state=0):

sampled_cells = []
for mtype in cells.mtype.unique():
samples = cells[cells.mtype == mtype].sample(
n=min(n_cells, len(cells[cells.mtype == mtype])), random_state=random_state
)
if n_cells > 1:
samples = cells[cells.mtype == mtype].sample(
n=min(n_cells, len(cells[cells.mtype == mtype])), random_state=random_state
)
if n_cells < 1:
samples = cells[cells.mtype == mtype].sample(frac=n_cells, random_state=random_state)
sampled_cells.append(samples)

if len(sampled_cells) > 0:
Expand Down
2 changes: 0 additions & 2 deletions neurocollage/planes_utils/planes.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,7 @@ def _smoothing(path, ctrl_point_count=10):
curve.ctrlpts = _split_path(path, ctrl_point_count).tolist()
# Auto-generate knot vector
curve.knotvector = utilities.generate_knot_vector(curve.degree, curve.ctrlpts_size)
print(curve.knotvector)
curve.delta = 0.01

step_count = 100
steps = np.linspace(0, 1, step_count, endpoint=True)
return np.asarray(curve.evaluate_list(steps))
Expand Down
4 changes: 2 additions & 2 deletions neurocollage/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ def _trans(p):
# pylint: disable=cell-var-from-loop
def trans(p):
return p + df.loc[gid, ["x", "y", "z"]].to_numpy().T

return m.transform(trans)
p = m.transform(trans)
return p
Loading