def warp_label(path_label, folder_label, file_label, fname_src, fname_transfo, path_out):
    # read label file and check if file exists
    sct.printv('\nRead label file...', param.verbose)
    template_label_ids, template_label_names, template_label_file = read_label_file(path_label+folder_label, file_label)
    # create output folder
    sct.run('mkdir '+path_out+folder_label, param.verbose)
    # Warp label
    for i in xrange(0, len(template_label_file)):
        fname_label = path_label+folder_label+template_label_file[i]
        # check if file exists
        # sct.check_file_exist(fname_label)
        # apply transfo
        sct.run('sct_apply_transfo -i '+fname_label+' -o '+path_out+folder_label+template_label_file[i] +' -d '+fname_src+' -w '+fname_transfo+' -x '+get_interp(template_label_file[i]), param.verbose)
    # Copy list.txt
    sct.run('cp '+path_label+folder_label+param.file_info_label+' '+path_out+folder_label, 0)
Beispiel #2
0
def get_fractional_volume_per_label(atlas_folder, file_label, nb_RL_labels=15):
    """This function takes as input the path to the folder containing an atlas and the name of the file gathering the
    labels' file name of this atlas.
    It returns, in the following order:
    - a list of the labels' ID,
    - a list of the labels' name,
    - a 1D-numpy array containing the fractional volume of each label in the same order as the previous lists."""

    import sct_extract_metric
    import nibabel
    import numpy

    [label_id, label_name,
     label_file] = sct_extract_metric.read_label_file(atlas_folder, file_label)
    nb_label = len(label_file)

    fract_volume_per_lab = numpy.zeros((nb_label))

    # compute fractional volume for each label
    for i_label in range(0, nb_label):
        fract_volume_per_lab[i_label] = numpy.sum(
            nibabel.load(atlas_folder + label_file[i_label]).get_data())

    # gather right and left sides
    # nb_non_RL_labels = nb_label - (2*nb_RL_labels) # number of labels that are not paired side-wise
    fract_volume_per_lab_RL_gatehered = numpy.zeros((nb_RL_labels))
    label_name_RL_gatehered = []

    for i_label in range(0, nb_RL_labels):
        ind_ID_first_side = label_id.index(i_label)
        ind_ID_other_side = label_id.index(i_label + nb_RL_labels)

        fract_volume_per_lab_RL_gatehered[i_label] = fract_volume_per_lab[
            ind_ID_first_side] + fract_volume_per_lab[ind_ID_other_side]
        label_name_RL_gatehered.append(label_name[ind_ID_first_side].replace(
            'left', '').replace('right', '').strip())

    # # add labels that are not paired side-wise
    # for i_label in range(0, nb_non_RL_labels):
    #     fract_volume_per_lab_RL_gatehered[nb_RL_labels+i_label] = fract_volume_per_lab[2 * nb_RL_labels + i_label]
    #     label_name_RL_gatehered.append(label_name[2 * nb_RL_labels + i_label].strip())

    return label_id, label_name, fract_volume_per_lab, label_name_RL_gatehered, fract_volume_per_lab_RL_gatehered
def get_nb_voxel_in_WM(atlas_folder, file_label):
    """This function takes as input the path to the folder containing an atlas and the name of the file gathering the
    labels' file name of this atlas. It returns the number of voxels including at least one label."""

    import sct_extract_metric
    import nibabel
    import numpy

    [label_id, label_name, label_file] = sct_extract_metric.read_label_file(atlas_folder, file_label)
    nb_label = len(label_file)

    # sum of all the labels
    sum_all_labels = nibabel.load(atlas_folder + label_file[0]).get_data()
    for i_label in range(1, nb_label):
        sum_all_labels = numpy.add(sum_all_labels, nibabel.load(atlas_folder + label_file[i_label]).get_data())

    # count the number of non-zero voxels
    nb_voxel_in_WM = numpy.count_nonzero(sum_all_labels)

    return nb_voxel_in_WM
def get_fractional_volume_per_label(atlas_folder, file_label, nb_RL_labels=15):
    """This function takes as input the path to the folder containing an atlas and the name of the file gathering the
    labels' file name of this atlas.
    It returns, in the following order:
    - a list of the labels' ID,
    - a list of the labels' name,
    - a 1D-numpy array containing the fractional volume of each label in the same order as the previous lists."""

    import sct_extract_metric
    import nibabel
    import numpy

    [label_id, label_name, label_file] = sct_extract_metric.read_label_file(atlas_folder, file_label)
    nb_label = len(label_file)

    fract_volume_per_lab = numpy.zeros((nb_label))

    # compute fractional volume for each label
    for i_label in range(0, nb_label):
        fract_volume_per_lab[i_label] = numpy.sum(nibabel.load(atlas_folder + label_file[i_label]).get_data())

    # gather right and left sides
    # nb_non_RL_labels = nb_label - (2*nb_RL_labels) # number of labels that are not paired side-wise
    fract_volume_per_lab_RL_gatehered = numpy.zeros((nb_RL_labels))
    label_name_RL_gatehered = []

    for i_label in range(0, nb_RL_labels):
        ind_ID_first_side = label_id.index(i_label)
        ind_ID_other_side = label_id.index(i_label + nb_RL_labels)

        fract_volume_per_lab_RL_gatehered[i_label] = fract_volume_per_lab[ind_ID_first_side] + fract_volume_per_lab[ind_ID_other_side]
        label_name_RL_gatehered.append(label_name[ind_ID_first_side].replace('left', '').replace('right', '').strip())

    # # add labels that are not paired side-wise
    # for i_label in range(0, nb_non_RL_labels):
    #     fract_volume_per_lab_RL_gatehered[nb_RL_labels+i_label] = fract_volume_per_lab[2 * nb_RL_labels + i_label]
    #     label_name_RL_gatehered.append(label_name[2 * nb_RL_labels + i_label].strip())

    return label_id, label_name, fract_volume_per_lab, label_name_RL_gatehered, fract_volume_per_lab_RL_gatehered
def compute_fract_vol_per_lab(atlas_folder, file_label):

    atlas_folder = '/Users/slevy_local/spinalcordtoolbox/dev/atlas/validate_atlas/cropped_atlas/'
    file_label = 'info_label.txt'

    [label_id, label_name,
     label_file] = sct_extract_metric.read_label_file(atlas_folder, file_label)
    nb_label = len(label_file)

    fract_volume_per_lab = numpy.zeros((nb_label))

    # compute fractional volume for each label
    for i_label in range(0, nb_label):
        fract_volume_per_lab[i_label] = numpy.sum(
            nibabel.load(atlas_folder + label_file[i_label]).get_data())

    print 'Labels\'name:'
    print label_name
    print '\nCorresponding fractional volume:'
    print fract_volume_per_lab

    return label_name, fract_volume_per_lab
def compute_fract_vol_per_lab(atlas_folder, file_label):

    atlas_folder = '/Users/slevy_local/spinalcordtoolbox/dev/atlas/validate_atlas/cropped_atlas/'
    file_label = 'info_label.txt'


    [label_id, label_name, label_file] = sct_extract_metric.read_label_file(atlas_folder, file_label)
    nb_label = len(label_file)

    fract_volume_per_lab = numpy.zeros((nb_label))

    # compute fractional volume for each label
    for i_label in range(0, nb_label):
        fract_volume_per_lab[i_label] = numpy.sum(nibabel.load(os.path.join(atlas_folder, label_file[i_label)]).get_data())


    print 'Labels\'name:'
    print label_name
    print '\nCorresponding fractional volume:'
    print fract_volume_per_lab

    return label_name, fract_volume_per_lab
Beispiel #7
0
def get_nb_voxel_in_WM(atlas_folder, file_label):
    """This function takes as input the path to the folder containing an atlas and the name of the file gathering the
    labels' file name of this atlas. It returns the number of voxels including at least one label."""

    import sct_extract_metric
    import nibabel
    import numpy

    [label_id, label_name,
     label_file] = sct_extract_metric.read_label_file(atlas_folder, file_label)
    nb_label = len(label_file)

    # sum of all the labels
    sum_all_labels = nibabel.load(atlas_folder + label_file[0]).get_data()
    for i_label in range(1, nb_label):
        sum_all_labels = numpy.add(
            sum_all_labels,
            nibabel.load(atlas_folder + label_file[i_label]).get_data())

    # count the number of non-zero voxels
    nb_voxel_in_WM = numpy.count_nonzero(sum_all_labels)

    return nb_voxel_in_WM
def warp_label(path_label, folder_label, file_label, fname_src, fname_transfo,
               path_out):
    # read label file and check if file exists
    sct.printv('\nRead label file...', param.verbose)
    template_label_ids, template_label_names, template_label_file = read_label_file(
        path_label + folder_label, file_label)
    # create output folder
    sct.run('mkdir ' + path_out + folder_label, param.verbose)
    # Warp label
    for i in xrange(0, len(template_label_file)):
        fname_label = path_label + folder_label + template_label_file[i]
        # check if file exists
        # sct.check_file_exist(fname_label)
        # apply transfo
        sct.run(
            'sct_apply_transfo -i ' + fname_label + ' -o ' + path_out +
            folder_label + template_label_file[i] + ' -d ' + fname_src +
            ' -w ' + fname_transfo + ' -x ' +
            get_interp(template_label_file[i]), param.verbose)
    # Copy list.txt
    sct.run(
        'cp ' + path_label + folder_label + param.file_info_label + ' ' +
        path_out + folder_label, 0)
Beispiel #9
0
def warp_label(path_label, folder_label, file_label, fname_src, fname_transfo, path_out):
    """
    Warp label files according to info_label.txt file
    :param path_label:
    :param folder_label:
    :param file_label:
    :param fname_src:
    :param fname_transfo:
    :param path_out:
    :return:
    """
    # read label file and check if file exists
    sct.printv('\nRead label file...', param.verbose)
    try:
        template_label_ids, template_label_names, template_label_file, combined_labels_ids, combined_labels_names, combined_labels_id_groups, clusters_apriori = read_label_file(path_label + folder_label, file_label)
    except Exception as error:
        sct.printv('\nWARNING: Cannot warp label ' + folder_label + ': ' + str(error), 1, 'warning')
        # raise
    # try:
    #     template_label_ids, template_label_names, template_label_file, combined_labels_ids, combined_labels_names, combined_labels_id_groups = read_label_file(path_label+folder_label, file_label)
    # except Exception:
    #     import traceback
    #     sct.printv('\nERROR: ' + traceback.format_exc(), 1, 'error')
    else:
        # create output folder
        if not os.path.exists(path_out + folder_label):
            os.makedirs(path_out + folder_label)
        # Warp label
        for i in xrange(0, len(template_label_file)):
            fname_label = path_label + folder_label + template_label_file[i]
            # check if file exists
            # sct.check_file_exist(fname_label)
            # apply transfo
            sct.run('sct_apply_transfo -i ' + fname_label + ' -o ' + path_out + folder_label + template_label_file[i] + ' -d ' + fname_src + ' -w ' + fname_transfo + ' -x ' + get_interp(template_label_file[i]), param.verbose)
        # Copy list.txt
        sct.run('cp ' + path_label + folder_label + param.file_info_label + ' ' + path_out + folder_label, 0)
Beispiel #10
0
def warp_label(path_label, folder_label, file_label, fname_src, fname_transfo, path_out):
    """
    Warp label files according to info_label.txt file
    :param path_label:
    :param folder_label:
    :param file_label:
    :param fname_src:
    :param fname_transfo:
    :param path_out:
    :return:
    """
    # read label file and check if file exists
    sct.printv('\nRead label file...', param.verbose)
    try:
        template_label_ids, template_label_names, template_label_file, combined_labels_ids, combined_labels_names, combined_labels_id_groups, clusters_apriori = read_label_file(path_label + folder_label, file_label)
    except Exception as error:
        sct.printv('\nWARNING: Cannot warp label '+folder_label+': '+str(error), 1, 'warning')
        # raise
    # try:
    #     template_label_ids, template_label_names, template_label_file, combined_labels_ids, combined_labels_names, combined_labels_id_groups = read_label_file(path_label+folder_label, file_label)
    # except Exception:
    #     import traceback
    #     sct.printv('\nERROR: ' + traceback.format_exc(), 1, 'error')
    else:
        # create output folder
        sct.run('mkdir '+path_out+folder_label, param.verbose)
        # Warp label
        for i in xrange(0, len(template_label_file)):
            fname_label = path_label+folder_label+template_label_file[i]
            # check if file exists
            # sct.check_file_exist(fname_label)
            # apply transfo
            sct.run('sct_apply_transfo -i '+fname_label+' -o '+path_out+folder_label+template_label_file[i] +' -d '+fname_src+' -w '+fname_transfo+' -x '+get_interp(template_label_file[i]), param.verbose)
        # Copy list.txt
        sct.run('cp '+path_label+folder_label+param.file_info_label+' '+path_out+folder_label, 0)