<jupyter_start><jupyter_text>Update_AO_according_to_gene_expression- input    - AObase_c.json    - AVbase_c_g.nrrd- output    - AObase_c_g_woVC.json<jupyter_code>ExpID = 74881161
Target_ROI_ID = 382

dir_data = 'data'
fn_input_AO = 'AObase_c.json'
fn_input_AV = 'AVbase_c_g.nrrd'
fn_output_AO = 'AObase_c_g_woVC.json' # without voxel count
import os
import nrrd
import numpy as np
import pandas as pd
import json
import copy
from collections import OrderedDict
from jsonpath_rw import jsonpath, parse<jupyter_output><empty_output><jupyter_text>Load data<jupyter_code>with open(os.path.join(dir_data, fn_input_AO)) as f:
    df_ori = json.load(f, object_pairs_hook=OrderedDict)
AV_ori, header = nrrd.read(os.path.join(dir_data, fn_input_AV))
ID_high_gene_expression = AV_ori.max()<jupyter_output><empty_output><jupyter_text>Segment target ROI with high gene expression<jupyter_code>def Segment_target_ROI_with_high_gene_expression_in_AV(match_id, match_fullpath):
    if match_id == 0: return # Children is not defined for a root node.
    # id_offset = 10**9 # ID of segmented node = original ID + this offset
    global df_gene
    global Target_ROI_ID
    global ID_high_gene_expression
    global ExpID
    print(match_fullpath)
    print(match_id)
    if match_id == Target_ROI_ID:
        source_acronym =  eval("df_gene"+\
                str(match_fullpath).replace('.','')\
                .replace('msg', "['msg']").replace('children', "['children']")\
                .replace('id', "['acronym']")) # acronym for matched ID
        source_name =  eval("df_gene"+\
                str(match_fullpath).replace('.','')\
                .replace('msg', "['msg']").replace('children', "['children']")\
                .replace('id', "['name']")) # name for matched ID
        source_original_content =  eval("copy.deepcopy(df_gene"+\
                str(match_fullpath).replace('.','')\
                .replace('msg', "['msg']")\
                .replace('children', "['children']").replace('id', "")+")") # OrderedDict
        source_child_index = int(str(match_fullpath)\
            [str(match_fullpath).rfind("[")+1:str(match_fullpath).rfind("]")])
        source_parent_path = "df_gene"+str(match_fullpath)\
            [0:str(match_fullpath).rfind("[")-1].replace('.','')\
            .replace('msg', "['msg']")\
            .replace('children', "['children']") # ./children[x]→./children
        source_original_voxelcount = eval(("df_gene"+\
                str(match_fullpath).replace('.','').replace('msg', "['msg']")\
                .replace('children', "['children']").replace('id','')+"['voxel_count']"))
        exec(source_parent_path + "[" +str(source_child_index) +"]['voxel_count'] = None")
        exec(source_parent_path + "[" + str(source_child_index) + "]['acronym'] = '"\
             + source_acronym + "_geneL'")
        exec(source_parent_path+\
                ".insert(" + str(source_child_index + 1) + ", source_original_content)") 
        exec(source_parent_path + "[" + str(source_child_index + 1) + "]['acronym'] = '"\
             + source_acronym + "_geneH'")
        exec(source_parent_path + "[" + str(source_child_index + 1) + "]['name'] = '"\
             + source_name + "_gene'+str(ExpID)")
        exec(source_parent_path + "[" + str(source_child_index + 1) + "]['children'] = []")
        exec(source_parent_path + "[" + str(source_child_index + 1) + "]['id'] = "\
             + str(ID_high_gene_expression))
        exec(source_parent_path + "[" + str(source_child_index + 1) +\
             "]['voxel_count'] = " + str(source_original_voxelcount))
        return [match_id, source_acronym, source_name, source_original_voxelcount, ID_high_gene_expression] # Leafed ID and its acronym
jsonpath_expr = parse('$..id')
df_gene = copy.deepcopy(df_ori)
temp_list = []
temp_list = [Segment_target_ROI_with_high_gene_expression_in_AV(match.value, match.full_path)\
            for match in reversed(jsonpath_expr.find(df_gene))]
ID_gene_high = np.array(temp_list)<jupyter_output>msg.[1].id
1000000028
msg.[0].children.[3].id
73
msg.[0].children.[2].id
1000000012
msg.[0].children.[1].id
1009
msg.[0].children.[0].children.[3].id
1000000013
msg.[0].children.[0].children.[2].children.[1].children.[3].id
589508455
msg.[0].children.[0].children.[2].children.[1].children.[2].id
846
msg.[0].children.[0].children.[2].children.[1].children.[1].id
91
msg.[0].children.[0].children.[2].children.[1].children.[0].id
989
msg.[0].children.[0].children.[2].children.[1].id
519
msg.[0].children.[0].children.[2].children.[0].children.[1].children.[5].id
1049
msg.[0].children.[0].children.[2].children.[0].children.[1].children.[4].id
1041
msg.[0].children.[0].children.[2].children.[0].children.[1].children.[3].id
1033
msg.[0].children.[0].children.[2].children.[0].children.[1].children.[2].id
1025
msg.[0].children.[0].children.[2].children.[0].children.[1].children.[1].children.[1].id
1064
msg.[0].children.[0].children.[2].children.[0].children.[1].children.[1].children.[0].id
1056
[...]<jupyter_text>Save<jupyter_code>with open(os.path.join(dir_data, fn_output_AO), mode='w') as fw:
    json.dump(df_gene, fw, indent=4)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1856702619414484, 'fraction_numerical': 0.03409090909090909, 'mean_word_length': 3.8172541743970316, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '637073', 'license': 'MIT', 'n_tokens_mistral': 1926, 'n_tokens_neox': 1810, 'n_words': 256, 'path': 'notebooks/Update_AO_according_to_gene_expression.ipynb', 'repo_name': 'ntakata/flexible-annotation-atlas', 'size': '5192'}