import anywidget
import traitlets
from traitlets import List, Any
from pathlib import Path
import numpy as np
import scipy
class HelloWidget(anywidget.AnyWidget):
_esm = Path("widget.js")
voxel_data = Any().tag(sync=True)
counter = HelloWidget()
# Create the NumPy array
voxelarray = np.full((11, 11, 11), 0)
voxelarray[5, 3, 5] = 1
voxelarray[5, 7, 5] = 1
# Convert the NumPy array to a nested list
voxel_data_list = voxelarray.tolist()
# Assign the values to the widget
counter.count = 42
counter.voxel_data = voxel_data_list
#page = counter
def ball(radius, dtype=np.uint8):
n = 2 * radius + 1
Z, Y, X = np.mgrid[
-radius: radius: n * 1j,
-radius: radius: n * 1j,
-radius: radius: n * 1j
]
s = X ** 2 + Y ** 2 + Z ** 2
return np.array(s <= radius * radius, dtype=dtype)
# Create the NumPy array
voxelarray = np.full((15, 15, 15), 0)
voxelarray[5, 3, 5] = 1
voxelarray[5, 9, 5] = 1
# apply dilation with scipy
img_morphed = scipy.ndimage.binary_dilation(voxelarray, ball(2)).astype(int)
img_morphed2 = scipy.ndimage.binary_dilation(voxelarray, ball(3)).astype(int)
img_morphed3 = scipy.ndimage.binary_dilation(voxelarray, ball(4)).astype(int)
img_morphed4 = scipy.ndimage.binary_dilation(voxelarray, ball(5)).astype(int)
sequence = []
sequence += [voxelarray.tolist()]
sequence += [img_morphed.tolist()]
sequence += [img_morphed2.tolist()]
sequence += [img_morphed3.tolist()]
sequence += [img_morphed4.tolist()]
w = HelloWidget()
w.voxel_data = sequence
page = w