import xml.etree.ElementTree as ET import os import re path_to_input_meta='./neuroglancer-scripts-input/metadata' path_to_metadata='./output/metadata' os.makedirs(path_to_metadata,exist_ok=True) def get_rgb_from_hex(hex=None): if hex is None: return None r=hex[1:3] g=hex[3:5] b=hex[5:7] return [int(r, 16), int(g, 16), int(b, 16)] def process_label(label, parent=None): name=label.attrib.get('name') color=label.attrib.get('color') abbreviation=label.attrib.get('abbreviation') id=int(label.attrib.get('id')) node={ 'name': name, 'rgb': get_rgb_from_hex(color), 'labelIndex': id, '_': { 'abbreviation': abbreviation, }, 'children': [] } if parent is not None: parent['children'].append(node) for l in label: process_label(l, node) def process_ilf_file(filename): stripped_name=re.sub(r'.ilf$', '', filename) path_to_ifl=f'{path_to_input_meta}/{filename}' ilf=ET.parse(path_to_ifl) root_node={ 'name': 'Root', 'rgb': None, 'labelIndex': None, 'children': [] } structure=ilf.find('structure') for label in structure: process_label(label, root_node) path_to_region=f"{path_to_metadata}/{stripped_name}.regions.json" with open(path_to_region, 'w') as fp: import json json.dump(root_node, fp, indent=2) pass def main(): is_ilf = re.compile(r'.ilf$') for f in [f for f in os.listdir(path_to_input_meta) if is_ilf.search(f)]: process_ilf_file(f) if __name__ == '__main__': main()