<jupyter_start><jupyter_text>Based on: https://github.com/BICCN/cell-locator/blob/ac84ce35e5cecdacd063009ce955bbd8cc489a11/Modules/Scripted/Home/CMakeLists.txt<jupyter_code>annotation = itk.imread('annotation_50_contiguous_itk.nrrd')
view(label_map=annotation)
with open('allen_id_to_label.json', 'r') as fp:
    allen_id_to_label = json.load(fp)
allen_id_to_label = {int(k): v for k, v in allen_id_to_label.items()}
binary_for_id = itk.image_duplicator(annotation)
binary_for_id.FillBuffer(0)

mask_input = itk.image_duplicator(annotation)
mask_input.FillBuffer(0)
with open('1.json', 'r') as fp:
    structure_graph = json.load(fp)['msg'][0]['children']
print(structure_graph)
with open('label_to_allen_id.json', 'r') as fp:
    label_to_allen_id = json.load(fp)
label_to_allen_id = {int(k): v for k, v in label_to_allen_id.items()}
class TreeNode(object):
    children = []
    allen_id = 0

    def __init__(self, allen_id):
        self.allen_id = allen_id
        self.children = []

allen_id_to_tree_node = dict()
acronym_to_allen_id = dict()

def _node_to_tree_node(node):
    allen_id = int(node['id'])
    tree_node = TreeNode(allen_id)
    allen_id_to_tree_node[allen_id] = tree_node

    acronym = node['acronym']
    acronym_to_allen_id[acronym] = allen_id

    return tree_node

def _process_node(tree_parent, node):
    tree_node = _node_to_tree_node(node)
    tree_parent.children.append(tree_node)
    for child in node['children']:
        _process_node(tree_node, child)

tree = TreeNode(None)
for node in structure_graph:
    _process_node(tree, node)
def allen_id_to_labels(allen_id):
    labels = set()
    if allen_id in allen_id_to_label:
        labels.add(allen_id_to_label[allen_id])
    node = allen_id_to_tree_node[allen_id]
    for child in node.children:
        child_labels = allen_id_to_labels(child.allen_id)
        labels.update(child_labels)
    return labels
labels = allen_id_to_labels(567)
labels
for label in labels:
    label_image = itk.mask_image_filter(mask_input, mask_image=annotation, masking_value=label, outside_value=1)
    updated = itk.add_image_filter(label_image, binary_for_id)
    binary_for_id = updated
view(label_map=binary_for_id)
binary_float = itk.cast_image_filter(binary_for_id, ttype=(type(binary_for_id), itk.Image[itk.F,3]))
aliased = itk.anti_alias_binary_image_filter(binary_float,
                                             number_of_layers=6,
                                             iso_surface_value=1)
view(aliased)
interpolator = itk.BSplineInterpolateImageFunction.New(aliased, spline_order=3)
mesh = itk.cuberille_image_to_mesh_filter(aliased,
                                          interpolator=interpolator,
                                          iso_surface_value=0.0,
                                          generate_triangle_faces=False,
                                          project_vertices_to_iso_surface=True,
                                          project_vertex_surface_distance_threshold=0.01)
#mesh = itk.cuberille_image_to_mesh_filter(aliased,
#                                          project_vertices_to_iso_surface=True,
#                                          iso_surface_value=0.0)
view(label_map=annotation, geometries=mesh)
!wget http://download.alleninstitute.org/informatics-archive/current-release/mouse_ccf/annotation/ccf_2017/structure_meshes/567.obj

allen_mesh = itk.meshread('567.obj', itk.F)
view(label_map=annotation, geometries=allen_mesh, geometries_opacities=0.5)
from itkwidgets._transform_types import to_geometry, vtkjs_to_zarr
geometry = to_geometry(allen_mesh)
geometry.pop('pointData')
geometry
!mkdir -p meshes
url = 'http://download.alleninstitute.org/informatics-archive/current-release/mouse_ccf/annotation/ccf_2017/structure_meshes'
ext = 'obj'

def list_dir(url, ext=''):
    page = requests.get(url).text
    soup = BeautifulSoup(page, 'html.parser')
    return [(node.get('href'), url + '/' + node.get('href')) for node in soup.find_all('a') if node.get('href').endswith(ext)]

for obj, obj_url in list_dir(url, ext):
    print(obj)
    if not os.path.exists(obj): 
        urlretrieve(obj_url, obj)
    mesh = itk.meshread(obj, itk.F)
    geometry = to_geometry(mesh)
    geometry.pop('pointData')
    allen_id = obj.split('.')[0]
    path = 'meshes/' + allen_id + '.zarr'
    store = zarr.open_group(path, 'w')
    vtkjs_to_zarr(geometry, store)
    zarr.consolidate_metadata(path)
from itkwidgets._transform_types import zarr_to_vtkjs
vtkjs = zarr_to_vtkjs(store)
vtkjs
zarr.consolidate_metadata<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10633187772925765, 'fraction_numerical': 0.012882096069868995, 'mean_word_length': 3.5993975903614457, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '549868', 'license': 'Apache-2.0', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1605, 'n_words': 241, 'path': 'AllenAnnotations50LabelMapSurfaceMeshes.ipynb', 'repo_name': 'dzenanz/allen-ccf-itk-vtk-zarr', 'size': '4580'}