diff --git a/neurocollage/collage.py b/neurocollage/collage.py index c9d5071..0fc4f56 100644 --- a/neurocollage/collage.py +++ b/neurocollage/collage.py @@ -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) @@ -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) diff --git a/neurocollage/mesh_helper.py b/neurocollage/mesh_helper.py index e3ee1e5..8abf9ba 100644 --- a/neurocollage/mesh_helper.py +++ b/neurocollage/mesh_helper.py @@ -1,4 +1,5 @@ """3D collage module.""" + from copy import deepcopy import matplotlib @@ -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 @@ -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: @@ -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 diff --git a/neurocollage/planes.py b/neurocollage/planes.py index e5b16f8..bb87ed7 100644 --- a/neurocollage/planes.py +++ b/neurocollage/planes.py @@ -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: diff --git a/neurocollage/planes_utils/planes.py b/neurocollage/planes_utils/planes.py index 8827ac7..e378139 100644 --- a/neurocollage/planes_utils/planes.py +++ b/neurocollage/planes_utils/planes.py @@ -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)) diff --git a/neurocollage/utils.py b/neurocollage/utils.py index 0988bdc..0f97b62 100644 --- a/neurocollage/utils.py +++ b/neurocollage/utils.py @@ -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