<jupyter_start><jupyter_text>Load data<jupyter_code>nm_data, nm_pixdim, nm_header = load_nifti(neuromorphometrics_file)<jupyter_output><empty_output><jupyter_text>Visualize<jupyter_code>#%gui qt5
#spimagine_show_mni_volume_numpy(nm_data, stackUnits=nm_pixdim)<jupyter_output><empty_output><jupyter_text>Count number of regions<jupyter_code>region_values = np.unique(nm_data)<jupyter_output><empty_output><jupyter_text>Get region names from the xml file and remove region values from region_values that are not found in the xml file. This is only non-brain region if original Neuromorphometrics file is used<jupyter_code>import xml.etree.ElementTree as ET
tree = ET.parse(neuromorphometrics_xml_file)
region_names = []
region_values_temp = []
for i in tree.iter():
    index = i.find("index")
    if index != None:
        index_int = np.int(index.text)
        if index_int in region_values:
            name = i.find("name")
            region_values_temp += [index_int]
            region_names += [name.text]
region_values = np.array(region_values_temp) # Update region_values. Removes 0 (outside of the brain) region from region_values, since it does not have a name
region_names = np.array(region_names)
#region_names<jupyter_output><empty_output><jupyter_text>Verify that we have the same amount of regions with name and values<jupyter_code>region_values.shape == region_names.shape<jupyter_output><empty_output><jupyter_text>Create empty array to place the merged data<jupyter_code>nm_data_lrmerged = np.zeros_like(nm_data)<jupyter_output><empty_output><jupyter_text>Create empty array to place the merged names<jupyter_code>region_names_merged = []<jupyter_output><empty_output><jupyter_text>Perform the merge<jupyter_code>value = 1
for name in region_names:
    if "Left" in name:
        left_name = name
        right_name = region_names[region_names == "Right" + name[len("Left"):]][0]
        left_name_value = region_values[np.where(region_names == left_name)][0]
        right_name_value = region_values[np.where(region_names == right_name)][0]
        """
        print(left_name)
        print(left_name_value)
        print(right_name)
        print(right_name_value)
        """
        # Set left and right regions to the value of the left region,
        # thereby merging left and right region into one region.
        #nm_data_lrmerged[nm_data == left_name_value] = left_name_value
        #nm_data_lrmerged[nm_data == right_name_value] = left_name_value
        
        nm_data_lrmerged[nm_data == left_name_value] = value
        nm_data_lrmerged[nm_data == right_name_value] = value
        
        # Append region names list
        region_names_merged += ["Left & right" + name[len("Left"):]]
        
        # Increment value
        value += 1
    elif not "Right" in name:
        name_value = region_values[np.where(region_names == name)][0]
        #nm_data_lrmerged[nm_data == name_value] = name_value
        nm_data_lrmerged[nm_data == name_value] = value
        region_names_merged += [name]
        
        # Incremnt value
        value += 1
region_names_merged = np.array(region_names_merged)<jupyter_output><empty_output><jupyter_text>Visualize the merged volume<jupyter_code>#spimagine_show_mni_volume_numpy(nm_data_lrmerged)<jupyter_output><empty_output><jupyter_text>Verify that we have the same amount of regions with merged name and values<jupyter_code>region_names_merged.shape == np.unique(nm_data_lrmerged)[1:].shape
np.unique(nm_data_lrmerged)[1:]
region_names_merged
print("Went from %i to %i regions by merging left and right" % (region_names.shape[0], region_names_merged.shape[0]))<jupyter_output>Went from 136 to 72 regions by merging left and right<jupyter_text>Save merged file using existing header and affine information<jupyter_code># Need affine information
nm_affine = nib.load(neuromorphometrics_file).affine

# Create Nifti image class instance
#nm_lrmerged_img = nib.spatialimages.SpatialImage(nm_data_lrmerged, affine=nm_affine, header=nm_header)
nm_lrmerged_img = nib.Nifti1Image(nm_data_lrmerged.astype(np.uint8), nm_affine, header=nm_header)

# Save cbv by overwriting original cbv file
nib.save(nm_lrmerged_img, corrections_base_directory + "/labels_Neuromorphometrics_lrmerged.nii")
# https://stackoverflow.com/questions/44397617/change-data-type-in-numpy-and-nibabel
#nm_header['sizeof_hdr']
#image.get_data_dtype()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09271822704658525, 'fraction_numerical': 0.006331976481230212, 'mean_word_length': 5.06721536351166, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 44, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '85656', 'license': 'Intel', 'n_tokens_mistral': 1527, 'n_tokens_neox': 1477, 'n_words': 298, 'path': 'notebooks/merge-l-r-Neuromorphometrics-labels.ipynb', 'repo_name': 'ivartz/epi_corrections', 'size': '4422'}