Skip to content

Commit 9779e5c

Browse files
refactor!: rename NiiVue._meshes and NiiVue._volumes
These attributes are now public, and the underscore prefix has been removed. This is a breaking change; accessing `nv._meshes` and `nv._volumes` will no longer work.
1 parent a87a924 commit 9779e5c

File tree

6 files changed

+32
-70
lines changed

6 files changed

+32
-70
lines changed

examples/draw.ui.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@
741741
" # Update the previous_script_value\n",
742742
" previous_script_value[0] = value\n",
743743
"\n",
744-
" nv._meshes = []\n",
744+
" nv.meshes = []\n",
745745
" if value == \"FLAIR\":\n",
746746
" volume_list1 = [{\"path\": DATA_FOLDER / \"FLAIR.nii.gz\"}]\n",
747747
" nv.load_volumes(volume_list1)\n",

js/mesh.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ export async function render_meshes(
351351
) {
352352
const mmodels = await lib.gather_models<MeshModel>(
353353
model,
354-
model.get("_meshes"),
354+
model.get("meshes"),
355355
);
356356

357357
const backend_meshes = mmodels;

js/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ export type MeshLayerModel = AnyModel<{
8080
export type Model = AnyModel<{
8181
id: string;
8282
height: number;
83-
_volumes: Array<string>;
84-
_meshes: Array<string>;
83+
volumes: Array<string>;
84+
meshes: Array<string>;
8585
opts: Partial<Record<keyof NVConfigOptions, unknown>>;
8686

8787
background_masks_overlays: number;

js/volume.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export async function render_volumes(
161161
) {
162162
const vmodels = await lib.gather_models<VolumeModel>(
163163
model,
164-
model.get("_volumes"),
164+
model.get("volumes"),
165165
);
166166

167167
const backend_volumes = vmodels;

js/widget.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ function attachModelEventHandlers(
4545
model: Model,
4646
disposer: Disposer,
4747
) {
48-
model.on("change:_volumes", () => {
48+
model.on("change:volumes", () => {
4949
if (nv.canvas) {
5050
render_volumes(nv, model, disposer);
5151
}
5252
});
53-
model.on("change:_meshes", () => {
53+
model.on("change:meshes", () => {
5454
if (nv.canvas) {
5555
render_meshes(nv, model, disposer);
5656
}
@@ -211,7 +211,7 @@ function attachNiivueEventHandlers(nv: niivue.Niivue, model: Model) {
211211
const volumeID = volume.id;
212212
const volumeModels = await gather_models<VolumeModel>(
213213
model,
214-
model.get("_volumes"),
214+
model.get("volumes"),
215215
);
216216

217217
const backendVolumeIds = volumeModels.map(
@@ -256,7 +256,7 @@ function attachNiivueEventHandlers(nv: niivue.Niivue, model: Model) {
256256
const meshID = mesh.id;
257257
const meshModels = await gather_models<MeshModel>(
258258
model,
259-
model.get("_meshes"),
259+
model.get("meshes"),
260260
);
261261

262262
const backendMeshIds = meshModels.map((mmodel) => mmodel?.get("id") || "");
@@ -542,8 +542,8 @@ export default {
542542
return () => {
543543
disposer.disposeAll();
544544

545-
model.off("change:_volumes");
546-
model.off("change:_meshes");
545+
model.off("change:volumes");
546+
model.off("change:meshes");
547547
model.off("change:opts");
548548
model.off("change:height");
549549
model.off("msg:custom");

src/ipyniivue/widget.py

Lines changed: 21 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,10 @@ class NiiVue(anywidget.AnyWidget):
337337
opts = t.Instance(ConfigOptions).tag(
338338
sync=True, to_json=serialize_options, from_json=deserialize_options
339339
)
340-
_volumes = t.List(t.Instance(Volume), default_value=[]).tag(
340+
volumes = t.List(t.Instance(Volume), default_value=[]).tag(
341341
sync=True, **ipywidgets.widget_serialization
342342
)
343-
_meshes = t.List(t.Instance(Mesh), default_value=[]).tag(
343+
meshes = t.List(t.Instance(Mesh), default_value=[]).tag(
344344
sync=True, **ipywidgets.widget_serialization
345345
)
346346

@@ -378,7 +378,7 @@ def __init__(self, height: int = 300, **options): # noqa: D417
378378
"""
379379
# convert to JS camelCase options
380380
opts = ConfigOptions(parent=self, **options)
381-
super().__init__(height=height, opts=opts, _volumes=[], _meshes=[])
381+
super().__init__(height=height, opts=opts, volumes=[], meshes=[])
382382

383383
# handle messages coming from frontend
384384
self._event_handlers = {}
@@ -441,17 +441,17 @@ def _handle_custom_msg(self, content, buffers):
441441
elif event == "frame_change":
442442
idx = self.get_volume_index_by_id(data["id"])
443443
if idx != -1:
444-
handler(self._volumes[idx], data["frame_index"])
444+
handler(self.volumes[idx], data["frame_index"])
445445
elif event in {"image_loaded", "intensity_change"}:
446446
idx = self.get_volume_index_by_id(data["id"])
447447
if idx != -1:
448-
handler(self._volumes[idx])
448+
handler(self.volumes[idx])
449449
else:
450450
handler(data)
451451
elif event == "mesh_loaded":
452452
idx = self.get_mesh_index_by_id(data["id"])
453453
if idx != -1:
454-
handler(self._meshes[idx])
454+
handler(self.meshes[idx])
455455
else:
456456
handler(data)
457457
elif event == "mesh_added_from_url":
@@ -466,20 +466,20 @@ def _handle_custom_msg(self, content, buffers):
466466
def _add_volume_from_frontend(self, volume_data):
467467
index = volume_data.pop("index", None)
468468
volume = Volume(**volume_data)
469-
if index is not None and 0 <= index <= len(self._volumes):
470-
self._volumes = [*self._volumes[:index], volume, *self._volumes[index:]]
469+
if index is not None and 0 <= index <= len(self.volumes):
470+
self.volumes = [*self.volumes[:index], volume, *self.volumes[index:]]
471471
else:
472-
self._volumes = [*self._volumes, volume]
472+
self.volumes = [*self.volumes, volume]
473473

474474
def _add_mesh_from_frontend(self, mesh_data):
475475
index = mesh_data.pop("index", None)
476476
layers_data = mesh_data.pop("layers", [])
477477
mesh = Mesh(**mesh_data)
478478
mesh.layers = [MeshLayer(**layer_data) for layer_data in layers_data]
479-
if index is not None and 0 <= index <= len(self._meshes):
480-
self._meshes = [*self._meshes[:index], mesh, *self._meshes[index:]]
479+
if index is not None and 0 <= index <= len(self.meshes):
480+
self.meshes = [*self.meshes[:index], mesh, *self.meshes[index:]]
481481
else:
482-
self._meshes = [*self._meshes, mesh]
482+
self.meshes = [*self.meshes, mesh]
483483

484484
def close(self):
485485
"""
@@ -506,7 +506,7 @@ def get_volume_index_by_id(self, volume_id: str) -> int:
506506
volume_id : str
507507
The id of the volume.
508508
"""
509-
for idx, vol in enumerate(self._volumes):
509+
for idx, vol in enumerate(self.volumes):
510510
if vol.id == volume_id:
511511
return idx
512512
return -1
@@ -519,7 +519,7 @@ def get_mesh_index_by_id(self, mesh_id: str) -> int:
519519
mesh_id : str
520520
The id of the mesh.
521521
"""
522-
for idx, mesh in enumerate(self._meshes):
522+
for idx, mesh in enumerate(self.meshes):
523523
if mesh.id == mesh_id:
524524
return idx
525525
return -1
@@ -546,7 +546,7 @@ def load_volumes(self, volumes: list):
546546
547547
"""
548548
volumes = [Volume(**item) for item in volumes]
549-
self._volumes = volumes
549+
self.volumes = volumes
550550

551551
def add_volume(self, volume: dict):
552552
"""
@@ -569,26 +569,7 @@ def add_volume(self, volume: dict):
569569
nv.add_volume({"path": "mni152.nii.gz"})
570570
571571
"""
572-
self._volumes = [*self._volumes, Volume(**volume)]
573-
574-
@property
575-
def volumes(self):
576-
"""
577-
Returns the list of volumes.
578-
579-
Returns
580-
-------
581-
list
582-
A list of dictionairies containing the volume information.
583-
584-
Examples
585-
--------
586-
::
587-
588-
print(nv.volumes)
589-
590-
"""
591-
return list(self._volumes)
572+
self.volumes = [*self.volumes, Volume(**volume)]
592573

593574
def load_meshes(self, meshes: list):
594575
"""
@@ -612,7 +593,7 @@ def load_meshes(self, meshes: list):
612593
613594
"""
614595
meshes = [Mesh(**item) for item in meshes]
615-
self._meshes = meshes
596+
self.meshes = meshes
616597

617598
def add_mesh(self, mesh: Mesh):
618599
"""
@@ -635,26 +616,7 @@ def add_mesh(self, mesh: Mesh):
635616
nv.add_mesh({"path": "BrainMesh_ICBM152.lh.mz3"})
636617
637618
"""
638-
self._meshes = [*self._meshes, mesh]
639-
640-
@property
641-
def meshes(self):
642-
"""
643-
Returns the list of meshes.
644-
645-
Returns
646-
-------
647-
list
648-
A list of dictionairies containing the mesh information.
649-
650-
Examples
651-
--------
652-
::
653-
654-
print(nv.meshes)
655-
656-
"""
657-
return list(self._meshes)
619+
self.meshes = [*self.meshes, mesh]
658620

659621
"""
660622
Other functions
@@ -804,7 +766,7 @@ def set_mesh_property(self, mesh_id: str, attribute: str, value: typing.Any):
804766
if idx == -1:
805767
raise ValueError(f"Mesh with id '{mesh_id}' not found.")
806768

807-
mesh = self._meshes[idx]
769+
mesh = self.meshes[idx]
808770
setattr(mesh, attribute, value)
809771

810772
def set_mesh_layer_property(
@@ -854,7 +816,7 @@ def set_mesh_layer_property(
854816
if idx == -1:
855817
raise ValueError(f"Mesh with id '{mesh_id}' not found.")
856818

857-
mesh = self._meshes[idx]
819+
mesh = self.meshes[idx]
858820
if layer_index < 0 or layer_index >= len(mesh.layers):
859821
raise IndexError(f"Layer index {layer_index} out of range.")
860822

@@ -955,7 +917,7 @@ def set_colormap(self, imageID: str, colormap: str):
955917
"""
956918
idx = self.get_volume_index_by_id(imageID)
957919
if idx != -1:
958-
self._volumes[idx].colormap = colormap
920+
self.volumes[idx].colormap = colormap
959921
else:
960922
raise ValueError(f"Volume with ID '{imageID}' not found")
961923

0 commit comments

Comments
 (0)