def save_by_slice(dic_dir):
    """
    from a dictionary containing for each subject a 3D image crop around the spinal cord,
     a graymatter segmentation 3D image, and a level image (from the registration of the template to the T2star image)

     save an image per slice including the level in the image name

    :param dic_dir: dictionary directory
    """
    if dic_dir[-1] == '/':
        dic_by_slice_dir = dic_dir[:-1] + '_by_slice/'
    else:
        dic_by_slice_dir = dic_dir + '_by_slice/'

    sct.run('mkdir ' + dic_by_slice_dir)

    for subject_dir in os.listdir(dic_dir):
        subject_path = dic_dir + '/' + subject_dir
        if os.path.isdir(subject_path):
            sct.run('mkdir ' + dic_by_slice_dir + subject_dir)
            # getting the level file
            path_file_levels = None
            label_by_slice = {}
            level_label = {0: '', 1: 'C1', 2: 'C2', 3: 'C3', 4: 'C4', 5: 'C5', 6: 'C6', 7: 'C7', 8: 'T1', 9: 'T2',
                           10: 'T3', 11: 'T4', 12: 'T5', 13: 'T6'}
            for file_name in os.listdir(subject_path):
                if 'level' in file_name:
                    path_file_levels = subject_path + '/' + file_name

                    if 'IRP' not in file_name:
                        path_file_levels_IRP = sct.add_suffix(path_file_levels, '_IRP')
                        sct.run('sct_image -i ' + subject_path + '/' + file_name + ' -setorient IRP -o '+path_file_levels_IRP)
                        path_file_levels = path_file_levels_IRP # subject_path + '/' + sct.extract_fname(file_name)[1] + '_IRP.nii.gz'

            if path_file_levels is None and 'label' in os.listdir(subject_path):
                if 'MNI-Poly-AMU_level_IRP.nii.gz' not in sct.run('ls ' + subject_path + '/label/template')[1]:
                    sct.run('sct_image -i ' + subject_path + '/label/template/MNI-Poly-AMU_level.nii.gz -setorient IRP')
                path_file_levels = subject_path + '/label/template/MNI-Poly-AMU_level_IRP.nii.gz'
            elif path_file_levels is not None:
                im_levels = Image(path_file_levels)
                nz_coord = im_levels.getNonZeroCoordinates()
                for i_level_slice, level_slice in enumerate(im_levels.data):
                    nz_val = []
                    for coord in nz_coord:
                        if coord.x == i_level_slice:
                            nz_val.append(level_slice[coord.y, coord.z])
                    try:
                        label_by_slice[i_level_slice] = int(round(sum(nz_val)/len(nz_val)))
                    except ZeroDivisionError:
                        sct.printv('No level label for slice ' + str(i_level_slice) + ' of subject ' + subject_dir)
                        label_by_slice[i_level_slice] = 0

            for file_name in os.listdir(subject_path):
                if 'seg_in' in file_name and 'croped' in file_name and 'IRP' in file_name:
                    im = Image(subject_path + '/' + file_name)
                    im_zooms = im.hdr.get_zooms()
                    slice_zoom = (im_zooms[1], im_zooms[2], im_zooms[0])
                    if path_file_levels is None:
                        for i_slice, im_slice in enumerate(im.data):
                            if i_slice < 10:
                                i_slice_str = str(i_slice)
                                i_slice_str = '0' + i_slice_str
                            else:
                                i_slice_str = str(i_slice)
                            fname_slice = dic_by_slice_dir + subject_dir + '/' + subject_dir + '_slice' + i_slice_str + '_im.nii.gz'
                            im_slice = Image(param=im_slice, absolutepath=fname_slice, hdr=im.hdr)

                            if len(im_slice.hdr.get_zooms()) == 3:
                                im_slice.hdr.set_zooms(slice_zoom)
                            im_slice.save()

                    else:
                        for i_slice, im_slice in enumerate(im.data):
                            if i_slice < 10:
                                i_slice_str = str(i_slice)
                                i_slice_str = '0' + i_slice_str
                            else:
                                i_slice_str = str(i_slice)
                            fname_slice = dic_by_slice_dir + subject_dir + '/' + subject_dir + '_slice' + i_slice_str + '_' + level_label[label_by_slice[i_slice]] + '_im.nii.gz'
                            im_slice = Image(param=im_slice, absolutepath=fname_slice, hdr=im.hdr)

                            if len(im_slice.hdr.get_zooms()) == 3:
                                im_slice.hdr.set_zooms(slice_zoom)
                            im_slice.save()

                if 'manual_gmseg' in file_name and 'croped' in file_name and 'IRP' in file_name:
                    seg = Image(dic_dir + '/' + subject_dir + '/' + file_name)
                    seg_zooms = seg.hdr.get_zooms()
                    slice_zoom = (seg_zooms[1], seg_zooms[2], seg_zooms[0])
                    if path_file_levels is None:
                        for i_slice, seg_slice in enumerate(seg.data):
                            if i_slice < 10:
                                i_slice_str = str(i_slice)
                                i_slice_str = '0' + i_slice_str
                            else:
                                i_slice_str = str(i_slice)
                            seg_slice = Image(param=seg_slice, absolutepath=dic_by_slice_dir + subject_dir + '/' + subject_dir + '_slice' + i_slice_str + '_' + level_label[label_by_slice[i_slice]] + '_manual_gmseg.nii.gz', hdr=seg.hdr)

                            if len(seg_slice.hdr.get_zooms()) == 3:
                                seg_slice.hdr.set_zooms(slice_zoom)
                            seg_slice.save()

                    else:
                        for i_slice, seg_slice in enumerate(seg.data):
                            if i_slice < 10:
                                i_slice_str = str(i_slice)
                                i_slice_str = '0' + i_slice_str
                            else:
                                i_slice_str = str(i_slice)

                            seg_slice = Image(param=seg_slice, absolutepath=dic_by_slice_dir + subject_dir + '/' + subject_dir +
                                  '_slice' + i_slice_str + '_' + level_label[label_by_slice[i_slice]] + '_seg.nii.gz', hdr=seg.hdr)

                            if len(seg_slice.hdr.get_zooms()) == 3:
                                seg_slice.hdr.set_zooms(slice_zoom)
                            seg_slice.save()


# ------------------------------------------------------------------------------------------------------------------
dataset/ETHPy150Open neuropoly/spinalcordtoolbox/scripts/msct_gmseg_utils.py/save_by_slice/original