def evaluate_deep_features(features_file,
                           labels_file,
                           sulci_file='',
                           hemi='',
                           excludeIDs=[-1],
                           output_vtk_name='',
                           verbose=True):
    """
    Evaluate deep surface features by computing the minimum distance from each
    label border vertex to all of the feature vertices in the same sulcus,
    and from each feature vertex to all of the label border vertices in the
    same sulcus.  The label borders run along the deepest parts of sulci
    and correspond to fundi in the DKT cortical labeling protocol.

    Parameters
    ----------
    features_file : string
        VTK surface file with feature numbers for vertex scalars
    labels_file : string
        VTK surface file with label numbers for vertex scalars
    sulci_file : string
        VTK surface file with sulcus numbers for vertex scalars
    excludeIDs : list of integers
        feature/sulcus/label IDs to exclude (background set to -1)
    output_vtk_name : Boolean
        if not empty, output a VTK file beginning with output_vtk_name that
        contains a surface with mean distances as scalars
    verbose : Boolean
        print mean distances to standard output?

    Returns
    -------
    feature_to_border_mean_distances : numpy array [number of features x 1]
        mean distance from each feature to sulcus label border
    feature_to_border_sd_distances : numpy array [number of features x 1]
        standard deviations of feature-to-border distances
    feature_to_border_distances_vtk : string
        VTK surface file containing feature-to-border distances
    border_to_feature_mean_distances : numpy array [number of features x 1]
        mean distances from each sulcus label border to feature
    border_to_feature_sd_distances : numpy array [number of features x 1]
        standard deviations of border-to-feature distances
    border_to_feature_distances_vtk : string
        VTK surface file containing border-to-feature distances

    """
    import os
    import sys
    import numpy as np
    from mindboggle.mio.vtks import read_vtk, read_scalars, write_vtk
    from mindboggle.guts.mesh import find_neighbors, remove_faces
    from mindboggle.guts.segment import extract_borders
    from mindboggle.guts.compute import source_to_target_distances
    from mindboggle.mio.labels import DKTprotocol

    dkt = DKTprotocol()
    #-------------------------------------------------------------------------
    # Load labels, features, and sulci:
    #-------------------------------------------------------------------------
    faces, lines, indices, points, npoints, labels, scalar_names, \
        input_vtk = read_vtk(labels_file, True, True)
    features, name = read_scalars(features_file, True, True)
    if sulci_file:
        sulci, name = read_scalars(sulci_file, True, True)
        # List of indices to sulcus vertices:
        sulcus_indices = [i for i, x in enumerate(sulci) if x != -1]
        segmentIDs = sulci
        sulcus_faces = remove_faces(faces, sulcus_indices)
    else:
        sulcus_indices = range(len(labels))
        segmentIDs = []
        sulcus_faces = faces

    #-------------------------------------------------------------------------
    # Prepare neighbors, label pairs, border IDs, and outputs:
    #-------------------------------------------------------------------------
    # Calculate neighbor lists for all points:
    print('Find neighbors for all vertices...')
    neighbor_lists = find_neighbors(faces, npoints)

    # Find label border points in any of the sulci:
    print('Find label border points in any of the sulci...')
    border_indices, border_label_tuples, unique_border_label_tuples = \
        extract_borders(sulcus_indices, labels, neighbor_lists,
                        ignore_values=[], return_label_pairs=True)
    if not len(border_indices):
        sys.exit('There are no label border points!')

    # Initialize an array of label border IDs
    # (label border vertices that define sulci in the labeling protocol):
    print('Build an array of label border IDs...')
    label_borders = -1 * np.ones(npoints)

    if hemi == 'lh':
        nsulcus_lists = len(dkt.left_sulcus_label_pair_lists)
    else:
        nsulcus_lists = len(dkt.right_sulcus_label_pair_lists)
    feature_to_border_mean_distances = -1 * np.ones(nsulcus_lists)
    feature_to_border_sd_distances = -1 * np.ones(nsulcus_lists)
    border_to_feature_mean_distances = -1 * np.ones(nsulcus_lists)
    border_to_feature_sd_distances = -1 * np.ones(nsulcus_lists)
    feature_to_border_distances_vtk = ''
    border_to_feature_distances_vtk = ''

    #-------------------------------------------------------------------------
    # Loop through sulci:
    #-------------------------------------------------------------------------
    # For each list of sorted label pairs (corresponding to a sulcus):
    for isulcus, label_pairs in enumerate(dkt.sulcus_label_pair_lists):

        # Keep the border points with label pair labels:
        label_pair_border_indices = [
            x for i, x in enumerate(border_indices)
            if np.unique(border_label_tuples[i]).tolist() in label_pairs
        ]

        # Store the points as sulcus IDs in the border IDs array:
        if label_pair_border_indices:
            label_borders[label_pair_border_indices] = isulcus

    if len(np.unique(label_borders)) > 1:

        #---------------------------------------------------------------------
        # Construct a feature-to-border distance matrix and VTK file:
        #---------------------------------------------------------------------
        # Construct a distance matrix:
        print('Construct a feature-to-border distance matrix...')
        sourceIDs = features
        targetIDs = label_borders
        distances, distance_matrix = source_to_target_distances(
            sourceIDs, targetIDs, points, segmentIDs, excludeIDs)

        # Compute mean distances for each feature:
        nfeatures = min(np.shape(distance_matrix)[1], nsulcus_lists)
        for ifeature in range(nfeatures):
            feature_distances = [
                x for x in distance_matrix[:, ifeature] if x != -1
            ]
            feature_to_border_mean_distances[ifeature] = \
                np.mean(feature_distances)
            feature_to_border_sd_distances[ifeature] = \
                np.std(feature_distances)

        if verbose:
            print('Feature-to-border mean distances:')
            print(feature_to_border_mean_distances)
            print('Feature-to-border standard deviations of distances:')
            print(feature_to_border_sd_distances)

        # Write resulting feature-label border distances to VTK file:
        if output_vtk_name:
            feature_to_border_distances_vtk = os.path.join(
                os.getcwd(),
                output_vtk_name + '_feature_to_border_mean_distances.vtk')
            print('Write feature-to-border distances to {0}...'.format(
                feature_to_border_distances_vtk))
            write_vtk(feature_to_border_distances_vtk, points, [], [],
                      sulcus_faces, [distances],
                      ['feature-to-border_distances'], 'float')

        #---------------------------------------------------------------------
        # Construct a border-to-feature distance matrix and VTK file:
        #---------------------------------------------------------------------
        # Construct a distance matrix:
        print('Construct a border-to-feature distance matrix...')
        sourceIDs = label_borders
        targetIDs = features
        distances, distance_matrix = source_to_target_distances(
            sourceIDs, targetIDs, points, segmentIDs, excludeIDs)

        # Compute mean distances for each feature:
        nfeatures = min(np.shape(distance_matrix)[1], nsulcus_lists)
        for ifeature in range(nfeatures):
            border_distances = [
                x for x in distance_matrix[:, ifeature] if x != -1
            ]
            border_to_feature_mean_distances[ifeature] = \
                np.mean(border_distances)
            border_to_feature_sd_distances[ifeature] = \
                np.std(border_distances)

        if verbose:
            print('border-to-feature mean distances:')
            print(border_to_feature_mean_distances)
            print('border-to-feature standard deviations of distances:')
            print(border_to_feature_sd_distances)

        # Write resulting feature-label border distances to VTK file:
        if output_vtk_name:
            border_to_feature_distances_vtk = os.path.join(
                os.getcwd(),
                output_vtk_name + '_border_to_feature_mean_distances.vtk')
            print('Write border-to-feature distances to {0}...'.format(
                border_to_feature_distances_vtk))
            write_vtk(border_to_feature_distances_vtk, points, [], [],
                      sulcus_faces, [distances],
                      ['border-to-feature_distances'], 'float')

    #-------------------------------------------------------------------------
    # Return outputs:
    #-------------------------------------------------------------------------
    return feature_to_border_mean_distances, feature_to_border_sd_distances,\
           feature_to_border_distances_vtk,\
           border_to_feature_mean_distances, border_to_feature_sd_distances,\
           border_to_feature_distances_vtk
Esempio n. 2
0
def write_shape_stats(labels_or_file=[],
                      sulci=[],
                      fundi=[],
                      affine_transform_files=[],
                      inverse_booleans=[],
                      transform_format='itk',
                      area_file='',
                      normalize_by_area=False,
                      mean_curvature_file='',
                      travel_depth_file='',
                      geodesic_depth_file='',
                      freesurfer_thickness_file='',
                      freesurfer_curvature_file='',
                      freesurfer_sulc_file='',
                      labels_spectra=[],
                      labels_spectra_IDs=[],
                      sulci_spectra=[],
                      sulci_spectra_IDs=[],
                      labels_zernike=[],
                      labels_zernike_IDs=[],
                      sulci_zernike=[],
                      sulci_zernike_IDs=[],
                      exclude_labels=[-1],
                      verbose=False):
    """
    Make tables of shape statistics per label, sulcus, and/or fundus.

    There can be thousands of vertices in a single feature such as a gyrus,
    sulcus, or fundus, and for per-vertex shape measures, it makes sense to
    characterize their collective shape as a distribution of shape values.
    Mindboggle's stats_per_label function generates tables of summary
    statistical measures for these distributions, and includes the shape
    measures computed on cortical features as well.

    Note ::
        This function is tailored for Mindboggle outputs.

    Parameters
    ----------
    labels_or_file : list or string
        label number for each vertex or name of VTK file with index scalars
    sulci : list of integers
        indices to sulci, one per vertex, with -1 indicating no sulcus
    fundi : list of integers
        indices to fundi, one per vertex, with -1 indicating no fundus
    affine_transform_files : list of strings
        affine transform files to standard space
    inverse_booleans : list of of zeros and ones
        for each transform, 1 to take the inverse, else 0
    transform_format : string
        format for transform file
        Ex: 'txt' for text, 'itk' for ITK, and 'mat' for Matlab format
    area_file :  string
        name of VTK file with surface area scalar values
    normalize_by_area : bool
        normalize all shape measures by area of label/feature? (UNTESTED)
    mean_curvature_file :  string
        name of VTK file with mean curvature scalar values
    travel_depth_file :  string
        name of VTK file with travel depth scalar values
    geodesic_depth_file :  string
        name of VTK file with geodesic depth scalar values
    freesurfer_thickness_file :  string
        name of VTK file with FreeSurfer thickness scalar values
    freesurfer_curvature_file :  string
        name of VTK file with FreeSurfer curvature (curv) scalar values
    freesurfer_sulc_file :  string
        name of VTK file with FreeSurfer convexity (sulc) scalar values
    labels_spectra : list of lists of floats
        Laplace-Beltrami spectra for each labeled region
    labels_spectra_IDs : list of integers
        unique labels for labels_spectra
    sulci_spectra : list of lists of floats
        Laplace-Beltrami spectra for each sulcus
    sulci_spectra_IDs : list of integers
        unique sulcus IDs for sulci_spectra
    labels_zernike : list of lists of floats
        Zernike moments for each labeled region
    labels_zernike_IDs : list of integers
        unique labels for labels_zernike
    sulci_zernike : list of lists of floats
        Zernike moments for each sulcus
    sulci_zernike_IDs : list of integers
        unique sulcus IDs for sulci_zernike
    exclude_labels : list of lists of integers
        indices to be excluded (in addition to -1)
    verbose : bool
        print statements?

    Returns
    -------
    label_table :  string
        output table filename for label shapes
    sulcus_table :  string
        output table filename for sulcus shapes
    fundus_table :  string
        output table filename for fundus shapes

    Examples
    --------
    >>> from mindboggle.mio.tables import write_shape_stats
    >>> from mindboggle.mio.vtks import read_scalars
    >>> from mindboggle.mio.fetch_data import prep_tests
    >>> urls, fetch_data = prep_tests()
    >>> label_file = fetch_data(urls['left_freesurfer_labels'], '', '.vtk')
    >>> sulci_file = fetch_data(urls['left_sulci'], '', '.vtk')
    >>> fundi_file = fetch_data(urls['left_fundus_per_sulcus'], '', '.vtk')
    >>> mean_curvature_file = fetch_data(urls['left_mean_curvature'], '', '.vtk')
    >>> travel_depth_file = fetch_data(urls['left_travel_depth'], '', '.vtk')
    >>> geodesic_depth_file = fetch_data(urls['left_geodesic_depth'], '', '.vtk')
    >>> area_file = fetch_data(urls['left_area'], '', '.vtk')
    >>> freesurfer_thickness_file = ''
    >>> freesurfer_curvature_file = ''
    >>> freesurfer_sulc_file = ''
    >>> sulci, name = read_scalars(sulci_file)
    >>> fundi, name = read_scalars(fundi_file)
    >>> affine_transform_files = []
    >>> inverse_booleans = []
    >>> transform_format = 'itk'
    >>> normalize_by_area = False
    >>> labels, name = read_scalars(label_file)
    >>> labels_spectra = []
    >>> labels_spectra_IDs = []
    >>> sulci_spectra = []
    >>> sulci_spectra_IDs = []
    >>> labels_zernike = []
    >>> labels_zernike_IDs = []
    >>> sulci_zernike = []
    >>> sulci_zernike_IDs = []
    >>> exclude_labels = [-1]
    >>> verbose = False
    >>> label_table, sulcus_table, fundus_table = write_shape_stats(label_file,
    ...     sulci, fundi, affine_transform_files, inverse_booleans,
    ...     transform_format, area_file, normalize_by_area,
    ...     mean_curvature_file, travel_depth_file, geodesic_depth_file,
    ...     freesurfer_thickness_file, freesurfer_curvature_file,
    ...     freesurfer_sulc_file, labels_spectra, labels_spectra_IDs,
    ...     sulci_spectra, sulci_spectra_IDs, labels_zernike,
    ...     labels_zernike_IDs, sulci_zernike, sulci_zernike_IDs,
    ...     exclude_labels, verbose)

    """
    import os
    import numpy as np
    import pandas as pd

    from mindboggle.guts.compute import stats_per_label
    from mindboggle.guts.compute import means_per_label
    from mindboggle.guts.compute import sum_per_label
    from mindboggle.mio.vtks import read_scalars, read_vtk
    from mindboggle.mio.vtks import apply_affine_transforms
    from mindboggle.mio.labels import DKTprotocol

    dkt = DKTprotocol()

    # Make sure inputs are lists:
    if isinstance(labels_or_file, np.ndarray):
        labels = [int(x) for x in labels_or_file]
    elif isinstance(labels_or_file, list):
        labels = labels_or_file
    elif isinstance(labels_or_file, str):
        labels, name = read_scalars(labels_or_file)
    if isinstance(sulci, np.ndarray):
        sulci = [int(x) for x in sulci]
    if isinstance(fundi, np.ndarray):
        fundi = [int(x) for x in fundi]

    if not labels and not sulci and not fundi:
        raise IOError('No feature data to tabulate in write_shape_stats().')

    spectrum_start = 1  # Store all columns of spectral components (0),
    # or start from higher frequency components (>=1)

    # ------------------------------------------------------------------------
    # Feature lists, shape names, and shape files:
    # ------------------------------------------------------------------------
    # Feature lists:
    feature_lists = [labels, sulci, fundi]
    feature_names = ['label', 'sulcus', 'fundus']
    spectra_lists = [labels_spectra, sulci_spectra]
    spectra_ID_lists = [labels_spectra_IDs, sulci_spectra_IDs]
    zernike_lists = [labels_zernike, sulci_zernike]
    zernike_ID_lists = [labels_zernike_IDs, sulci_zernike_IDs]
    table_names = [
        'label_shapes.csv', 'sulcus_shapes.csv', 'fundus_shapes.csv'
    ]

    # Shape names corresponding to shape files below:
    shape_names = [
        'area', 'travel depth', 'geodesic depth', 'mean curvature',
        'freesurfer curvature', 'freesurfer thickness',
        'freesurfer convexity (sulc)'
    ]

    # Load shape files as a list of numpy arrays of per-vertex shape values:
    shape_files = [
        area_file, travel_depth_file, geodesic_depth_file, mean_curvature_file,
        freesurfer_curvature_file, freesurfer_thickness_file,
        freesurfer_sulc_file
    ]
    shape_arrays = []
    first_pass = True
    area_array = []

    for ishape, shape_file in enumerate(shape_files):
        if os.path.exists(shape_file):
            if first_pass:
                points, indices, lines, faces, scalars_array, scalar_names, \
                    npoints, input_vtk = read_vtk(shape_file, True, True)
                points = np.array(points)
                first_pass = False
                if affine_transform_files and transform_format:
                    affine_points, \
                        foo1 = apply_affine_transforms(affine_transform_files,
                                    inverse_booleans, transform_format,
                                    points, vtk_file_stem='')
            else:
                scalars_array, name = read_scalars(shape_file, True, True)
            if scalars_array.size:
                shape_arrays.append(scalars_array)

                # Store area array:
                if ishape == 0:
                    area_array = scalars_array.copy()

    if normalize_by_area:
        use_area = area_array
    else:
        use_area = []

    # Initialize table file names:
    label_table = ''
    sulcus_table = ''
    fundus_table = ''

    # Loop through features / tables:
    for itable, feature_list in enumerate(feature_lists):
        column_names = []

        # ----------------------------------------------------------------
        # Label names:
        # ----------------------------------------------------------------
        label_title = 'name'
        if itable == 0:
            label_numbers = dkt.cerebrum_cortex_DKT31_numbers
            label_names = dkt.cerebrum_cortex_DKT31_names
        elif itable in [1, 2]:
            label_numbers = dkt.sulcus_numbers
            label_names = dkt.sulcus_names
        else:
            label_numbers = []
            label_names = []
        include_labels = label_numbers
        nlabels = len(label_numbers)

        # --------------------------------------------------------------------
        # For each feature, construct a table of average shape values:
        # --------------------------------------------------------------------
        if feature_list:
            feature_name = feature_names[itable]
            columns = []

            # ----------------------------------------------------------------
            # Loop through shape measures:
            # ----------------------------------------------------------------
            column_names.extend(column_names[:])
            for ishape, shape_array in enumerate(shape_arrays):
                shape = shape_names[ishape]
                if verbose:
                    print('  Compute statistics on {0} {1}...'.format(
                        feature_name, shape))
                # ------------------------------------------------------------
                # Append feature areas to columns:
                # ------------------------------------------------------------
                if ishape == 0 and np.size(area_array):
                    sums, label_list = sum_per_label(shape_array, feature_list,
                                                     include_labels,
                                                     exclude_labels)
                    column_names.append(shape)
                    columns.append(sums)
                # ------------------------------------------------------------
                # Append feature shape statistics to columns:
                # ------------------------------------------------------------
                else:
                    medians, mads, means, sdevs, skews, kurts, \
                    lower_quarts, upper_quarts, \
                    label_list = stats_per_label(shape_array, feature_list,
                                        include_labels, exclude_labels,
                                        area_array, precision=1)

                    column_names.append(shape + ': median')
                    column_names.append(shape + ': MAD')
                    column_names.append(shape + ': mean')
                    column_names.append(shape + ': SD')
                    column_names.append(shape + ': skew')
                    column_names.append(shape + ': kurtosis')
                    column_names.append(shape + ': 25%')
                    column_names.append(shape + ': 75%')
                    columns.append(medians)
                    columns.append(mads)
                    columns.append(means)
                    columns.append(sdevs)
                    columns.append(skews)
                    columns.append(kurts)
                    columns.append(lower_quarts)
                    columns.append(upper_quarts)

            # ----------------------------------------------------------------
            # Mean positions in the original space:
            # ----------------------------------------------------------------
            # Compute mean position per feature:
            positions, sdevs, label_list, foo = means_per_label(
                points, feature_list, include_labels, exclude_labels, use_area)

            # Append mean x,y,z position per feature to columns:
            xyz_positions = np.asarray(positions)
            for ixyz, xyz in enumerate(['x', 'y', 'z']):
                column_names.append('mean position: {0}'.format(xyz))
                columns.append(xyz_positions[:, ixyz].tolist())

            # ----------------------------------------------------------------
            # Mean positions in standard space:
            # ----------------------------------------------------------------
            if affine_transform_files and transform_format:
                # Compute standard space mean position per feature:
                standard_positions, sdevs, label_list, \
                foo = means_per_label(affine_points,
                    feature_list, include_labels, exclude_labels, use_area)

                # Append standard space x,y,z position per feature to columns:
                xyz_std_positions = np.asarray(standard_positions)
                for ixyz, xyz in enumerate(['x', 'y', 'z']):
                    column_names.append('mean position in standard space:'
                                        ' {0}'.format(xyz))
                    columns.append(xyz_std_positions[:, ixyz].tolist())

            # ----------------------------------------------------------------
            # Laplace-Beltrami spectra:
            # ----------------------------------------------------------------
            if itable in [0, 1]:
                spectra = spectra_lists[itable]
                if spectra:
                    spectra_IDs = spectra_ID_lists[itable]

                    # Construct a matrix of spectra:
                    len_spectrum = len(spectra[0])
                    spectrum_matrix = np.zeros((nlabels, len_spectrum))
                    for ilabel, label in enumerate(include_labels):
                        if label in spectra_IDs:
                            spectrum = spectra[spectra_IDs.index(label)]
                            spectrum_matrix[ilabel, 0:len_spectrum] = spectrum

                    # Append spectral shape name and values to columns:
                    for ispec in range(spectrum_start, len_spectrum):
                        columns.append(spectrum_matrix[:, ispec].tolist())
                        column_names.append('Laplace-Beltrami spectrum:'
                                            ' component {0}'.format(ispec + 1))

            # ----------------------------------------------------------------
            # Zernike moments:
            # ----------------------------------------------------------------
            if itable in [0, 1]:
                zernike = zernike_lists[itable]
                if zernike:
                    zernike_IDs = zernike_ID_lists[itable]

                    # Construct a matrix of Zernike moments:
                    len_moments = len(zernike[0])
                    moments_matrix = np.zeros((nlabels, len_moments))
                    for ilabel, label in enumerate(include_labels):
                        if label in zernike_IDs:
                            moments = zernike[zernike_IDs.index(label)]
                            moments_matrix[ilabel, 0:len_moments] = moments

                    # Append Zernike shape name and values to columns:
                    for imoment in range(0, len_moments):
                        columns.append(moments_matrix[:, imoment].tolist())
                        column_names.append(
                            'Zernike moments: component {0}'.format(imoment +
                                                                    1))

            # ----------------------------------------------------------------
            # Write labels/IDs and values to table:
            # ----------------------------------------------------------------
            # Write labels/IDs to table:
            output_table = os.path.join(os.getcwd(), table_names[itable])

            if columns:
                df1 = pd.DataFrame({'ID': label_numbers})
                df2 = pd.DataFrame(np.transpose(columns), columns=column_names)
                df = pd.concat([df1, df2], axis=1)
                if label_names:
                    df0 = pd.DataFrame({'name': label_names})
                    df = pd.concat([df0, df], axis=1)
                df.to_csv(output_table, index=False, encoding='utf-8')

            if not os.path.exists(output_table):
                raise IOError(output_table + " not found")

            # ----------------------------------------------------------------
            # Return correct table file name:
            # ----------------------------------------------------------------
            if itable == 0:
                label_table = output_table
            elif itable == 1:
                sulcus_table = output_table
            elif itable == 2:
                fundus_table = output_table

    return label_table, sulcus_table, fundus_table
Esempio n. 3
0
    def compare_surface_shape_measures_by_vertex():

        import os
        import pandas as pd
        import numpy as np

        from mindboggle.guts.compute import distcorr
        from mindboggle.mio.labels import DKTprotocol

        dkt = DKTprotocol()
        label_namesL = dkt.left_cerebrum_cortex_DKT31_names
        label_namesR = dkt.right_cerebrum_cortex_DKT31_names
        labelsL = dkt.left_cerebrum_cortex_DKT31_numbers
        labelsR = dkt.right_cerebrum_cortex_DKT31_numbers
        label_names_bilateral = dkt.DKT31_names

        subject_list = '/Users/arno/Data/subject_list_Mindboggle101.txt'
        fid = open(subject_list, 'r')
        subjects = [x.strip() for x in fid.readlines()]

        table_dir = '/Users/arno/Data/manual_tables'
        table_pathL = 'tables/left_cortical_surface/vertices.csv'
        table_pathR = 'tables/right_cortical_surface/vertices.csv'

        # --------------------------------------------------------------------
        # Loop through subjects and save distance correlations between
        # different curvature and between different depth shape measures:
        # --------------------------------------------------------------------
        dcors = np.zeros((len(subjects), len(labelsL), 4))
        for isubject, subject in enumerate(subjects):

            # Load shape tables:
            tableL = os.path.join(table_dir, subject, table_pathL)
            tableR = os.path.join(table_dir, subject, table_pathR)
            columnsL = pd.read_csv(tableL, sep=",", index_col="label ID")
            columnsR = pd.read_csv(tableR, sep=",", index_col="label ID")

            for ilabel, labelL in enumerate(labelsL):
                print(subject + ', ' + str(labelL))
                labelR = labelsR[ilabel]
                columnc1L = columnsL.loc[[labelL],
                                         ['mean curvature']].iloc[:, 0].values
                columnc2L = columnsL.loc[
                    [labelL], ['freesurfer curvature']].iloc[:, 0].values
                columnd1L = columnsL.loc[[labelL],
                                         ['travel depth']].iloc[:, 0].values
                columnd2L = columnsL.loc[[labelL],
                                         ['geodesic depth']].iloc[:, 0].values
                columnc1R = columnsR.loc[[labelR],
                                         ['mean curvature']].iloc[:, 0].values
                columnc2R = columnsR.loc[
                    [labelR], ['freesurfer curvature']].iloc[:, 0].values
                columnd1R = columnsR.loc[[labelR],
                                         ['travel depth']].iloc[:, 0].values
                columnd2R = columnsR.loc[[labelR],
                                         ['geodesic depth']].iloc[:, 0].values

                # Compute distance correlations:
                dcors[isubject, ilabel, 0] = distcorr(columnc1L, columnc2L)
                dcors[isubject, ilabel, 1] = distcorr(columnc1R, columnc2R)
                dcors[isubject, ilabel, 2] = distcorr(columnd1L, columnd2L)
                dcors[isubject, ilabel, 3] = distcorr(columnd1R, columnd2R)

        # --------------------------------------------------------------------
        # Save csv files:
        # --------------------------------------------------------------------
        data = pd.DataFrame(dcors[:, :, 0].transpose(),
                            index=label_names_bilateral,
                            columns=[x for x in range(101)])
        data.to_csv('mean_and_FS_curvature_distance_correlation_'
                    'per_left_label_vertices_Mindboggle101.csv')

        data = pd.DataFrame(dcors[:, :, 1].transpose(),
                            index=label_names_bilateral,
                            columns=[x for x in range(101)])
        data.to_csv('mean_and_FS_curvature_distance_correlation_'
                    'per_right_label_vertices_Mindboggle101.csv')

        data = pd.DataFrame(dcors[:, :, 2].transpose(),
                            index=label_names_bilateral,
                            columns=[x for x in range(101)])
        data.to_csv('geodesic_and_travel_depth_distance_correlation_'
                    'per_left_label_vertices_Mindboggle101.csv')

        data = pd.DataFrame(dcors[:, :, 3].transpose(),
                            index=label_names_bilateral,
                            columns=[x for x in range(101)])
        data.to_csv('geodesic_and_travel_depth_distance_correlation_'
                    'per_right_label_vertices_Mindboggle101.csv')

        data = dcors.mean(axis=0)
        data = pd.DataFrame(
            data,
            index=label_names_bilateral,
            columns=[
                'mean / freesurfer curvature distance correlation (left)',
                'mean / freesurfer curvature distance correlation (right)',
                'geodesic / travel depth distance correlation (left)',
                'geodesic / travel depth distance correlation (right)'
            ])
        data.to_csv(
            'mean_and_FS_curvature_geodesic_and_travel_depth_distance_correlations_'
            'per_label_vertices_avg_over_Mindboggle101.csv')
Esempio n. 4
0
    def compare_shapes_between_hemispheres():

        import os
        import numpy as np
        import pandas as pd

        # For plotting:
        from math import pi
        from bokeh.models import HoverTool
        from bokeh.plotting import ColumnDataSource, figure, show, save, output_file
        from mindboggle.mio.colors import viridis_colormap
        from mindboggle.mio.labels import DKTprotocol

        titles = [
            "Fractional difference between interhemispheric volumes",
            "Fractional difference between interhemispheric thickinthehead cortical thicknesses",
            "Fractional difference between interhemispheric cortical label median areas",
            "Fractional difference between interhemispheric cortical label median travel depths",
            "Fractional difference between interhemispheric cortical label median geodesic depths",
            "Fractional difference between interhemispheric cortical label median mean curvatures",
            "Fractional difference between interhemispheric cortical label median FreeSurfer curvatures",
            "Fractional difference between interhemispheric cortical label median FreeSurfer thicknesses"
        ]
        names = [
            "volume_for_each_freesurfer_label",
            "thickinthehead_per_freesurfer_cortex_label",
            "median_area_per_freesurfer_cortex_label",
            "median_travel_depth_per_freesurfer_cortex_label",
            "median_geodesic_depth_per_freesurfer_cortex_label",
            "median_mean_curvatures_per_freesurfer_cortex_label",
            "median_freesurfer_curvature_per_freesurfer_cortex_label",
            "median_freesurfer_thickness_per_freesurfer_cortex_label"
        ]
        table_dir = '/Users/arno/Data/manual_tables'
        tablesL = [
            os.path.join('tables', 'volume_for_each_freesurfer_label.csv'),
            os.path.join('tables',
                         'thickinthehead_per_freesurfer_cortex_label.csv'),
            os.path.join('tables', 'left_cortical_surface',
                         'label_shapes.csv'),
            os.path.join('tables', 'left_cortical_surface',
                         'label_shapes.csv'),
            os.path.join('tables', 'left_cortical_surface',
                         'label_shapes.csv'),
            os.path.join('tables', 'left_cortical_surface',
                         'label_shapes.csv'),
            os.path.join('tables', 'left_cortical_surface',
                         'label_shapes.csv'),
            os.path.join('tables', 'left_cortical_surface', 'label_shapes.csv')
        ]
        tablesR = [
            os.path.join('tables', 'volume_for_each_freesurfer_label.csv'),
            os.path.join('tables',
                         'thickinthehead_per_freesurfer_cortex_label.csv'),
            os.path.join('tables', 'right_cortical_surface',
                         'label_shapes.csv'),
            os.path.join('tables', 'right_cortical_surface',
                         'label_shapes.csv'),
            os.path.join('tables', 'right_cortical_surface',
                         'label_shapes.csv'),
            os.path.join('tables', 'right_cortical_surface',
                         'label_shapes.csv'),
            os.path.join('tables', 'right_cortical_surface',
                         'label_shapes.csv'),
            os.path.join('tables', 'right_cortical_surface',
                         'label_shapes.csv')
        ]
        column_indices = [1, 1, 1, 2, 10, 18, 26, 34]

        # --------------------------------------------------------------------
        # Alternating left, right cortex label numbers (for volume shapes):
        # --------------------------------------------------------------------
        dkt = DKTprotocol()
        labels_left = dkt.left_cerebrum_cortex_DKT31_numbers
        labels_right = dkt.right_cerebrum_cortex_DKT31_numbers
        label_names = dkt.DKT31_names
        #exclude_sulci = [20] # Sulcus 20 removed from protocol since initial run

        # --------------------------------------------------------------------
        # Colors:
        # --------------------------------------------------------------------
        colors = viridis_colormap()
        #from matplotlib import cm as cmaps
        #import matplotlib.pyplot as plt
        #plt.register_cmap(name='viridis', cmap=cmaps.viridis)
        #plt.set_cmap(cmaps.viridis)

        scale_rect = 20

        # --------------------------------------------------------------------
        # Subjects:
        # --------------------------------------------------------------------
        subject_list = '/Users/arno/Data/subject_list_Mindboggle101.txt'
        fid = open(subject_list, 'r')
        subjects = [x.strip() for x in fid.readlines()]

        # --------------------------------------------------------------------
        # Loop through tables:
        # --------------------------------------------------------------------
        data_means = np.zeros((len(labels_left), len(titles)))
        data_summaries = np.zeros((len(titles), 8))
        for ititle, title in enumerate(titles):
            tableL_file = tablesL[ititle]
            tableR_file = tablesR[ititle]
            name = names[ititle]
            index = column_indices[ititle]

            # ----------------------------------------------------------------
            # Loop through subjects:
            # ----------------------------------------------------------------
            subject_shapesL = np.zeros((len(subjects), len(labels_left)))
            subject_shapesR = np.zeros((len(subjects), len(labels_right)))
            for isubject, subject in enumerate(subjects):
                tableL = os.path.join(table_dir, subject, tableL_file)
                tableR = os.path.join(table_dir, subject, tableR_file)
                columnsL = pd.read_csv(tableL, sep=",", index_col='name')
                columnsR = pd.read_csv(tableR, sep=",", index_col='name')

                # ------------------------------------------------------------
                # Loop through labels:
                # ------------------------------------------------------------
                for ilabel, labelL in enumerate(labels_left):
                    for irow in range(columnsL.shape[0]):
                        if int(columnsL.iloc[irow][0]) == int(labelL):
                            valueL = columnsL.iloc[irow][index]
                            subject_shapesL[isubject, ilabel] = valueL
                for ilabel, labelR in enumerate(labels_right):
                    for irow in range(columnsR.shape[0]):
                        if int(columnsR.iloc[irow][0]) == int(labelR):
                            valueR = columnsR.iloc[irow][index]
                            subject_shapesR[isubject, ilabel] = valueR

            # ----------------------------------------------------------------
            # Save csv files:
            # ----------------------------------------------------------------
            data = pd.DataFrame(subject_shapesL,
                                index=subjects,
                                columns=labels_left)
            data.to_csv(name + '_left.csv')
            data_summary = data.describe(include='all')
            data_summary.to_csv(name + '_left_summary.csv')

            data = pd.DataFrame(subject_shapesR,
                                index=subjects,
                                columns=labels_right)
            data.to_csv(name + '_right.csv')
            data_summary = data.describe(include='all')
            data_summary.to_csv(name + '_right_summary.csv')

            subject_shape_diffs = subject_shapesL - subject_shapesR
            data = pd.DataFrame(subject_shape_diffs,
                                index=subjects,
                                columns=label_names)
            data.to_csv(name + '_differences.csv')
            data_summary = data.describe(include='all')
            data_summary.to_csv(name + '_differences_summary.csv')

            subject_shape_abs_diffs = np.abs(subject_shape_diffs)
            subject_shape_frac_diffs = subject_shape_diffs / subject_shapesL
            data = pd.DataFrame(subject_shape_frac_diffs,
                                index=subjects,
                                columns=label_names)
            data.to_csv(name + '_fractional_differences.csv')
            data_summary = data.describe(include='all')
            data_summary.to_csv(name + '_fractional_differences_summary.csv')

            subject_shape_frac_abs_diffs = np.abs(subject_shape_abs_diffs /
                                                  subject_shapesL)
            data = pd.DataFrame(subject_shape_frac_abs_diffs,
                                index=subjects,
                                columns=label_names)
            n50 = len(np.where(data.values > 0.5)[0])
            n25 = len(np.where(data.values > 0.25)[0])
            n10 = len(np.where(data.values > 0.1)[0])
            print(title)
            print("Fractional absolute differences above "
                  "0.5: {0}; 0.25: {1}; 0.1: {2}".format(n50, n25, n10))
            print("")
            data.to_csv(name + '_fractional_abs_differences.csv')
            data_summary = data.describe(include='all')
            data_summary.to_csv(name +
                                '_fractional_abs_differences_summary.csv')

            data_means[:, ititle] = data_summary.loc['mean'].values
            data_summaries[ititle, :] = data_summary.mean(axis=1)

            # ----------------------------------------------------------------
            # Plot heatmap for labels X subjects array for each table:
            # ----------------------------------------------------------------
            html_file = name + '_fractional_abs_differences.html'
            print(html_file)

            # Set up the data for plotting. We will need to have values for every
            # pair of subject/label names. Map the value to a color.
            subjectx = []
            label_namex = []
            value1x = []
            value2x = []
            differencex = []
            fractionx = []
            colorx = []
            for ilabel in range(len(labels_left)):
                for isubject, subject in enumerate(subjects):
                    label_namex.append(label_names[ilabel])
                    subjectx.append(subject)
                    value1 = subject_shapesL[isubject, ilabel]
                    value2 = subject_shapesR[isubject, ilabel]
                    difference = subject_shape_diffs[isubject, ilabel]
                    value1x.append(value1)
                    value2x.append(value2)
                    differencex.append(difference)
                    fraction = subject_shape_frac_diffs[isubject, ilabel]
                    fractionx.append(fraction)
                    abs_fraction = subject_shape_frac_abs_diffs[isubject,
                                                                ilabel]
                    if np.isnan(value1) or np.isnan(value2) or np.isnan(
                            abs_fraction):
                        rgb = [0, 0, 0]
                    elif abs_fraction > 1.0:
                        rgb = [1, 1, 1]
                    else:
                        rgb = [
                            np.int(255 * x)
                            for x in colors[np.int(255 * abs_fraction)]
                        ]
                    hex = "#%02x%02x%02x" % tuple(rgb)
                    colorx.append(hex)

            output_file(html_file, title=title)
            source = ColumnDataSource(
                dict(subject=subjectx,
                     label_name=label_namex,
                     color=colorx,
                     value1=value1x,
                     value2=value2x,
                     difference=differencex,
                     fraction=fractionx))
            TOOLS = "hover,save,pan,box_zoom,wheel_zoom"

            plot_width = len(subjects) * scale_rect
            plot_height = len(labels_left) * scale_rect
            p = figure(title=title,
                       x_range=subjects,
                       y_range=list(reversed(label_names)),
                       plot_width=plot_width,
                       plot_height=plot_height,
                       x_axis_location="above",
                       tools=TOOLS)
            p.grid.grid_line_color = None
            p.axis.axis_line_color = None
            p.axis.major_tick_line_color = None
            p.axis.major_label_text_font_size = "10pt"
            p.axis.major_label_standoff = 0
            p.xaxis.major_label_orientation = pi / 3
            p.rect(x="subject",
                   y="label_name",
                   width=1,
                   height=1,
                   source=source,
                   color="color",
                   line_color=None)

            p.select_one(HoverTool).tooltips = [
                ('subject', '@subject'),
                ('label', '@label_name'),
                ('value1', '@value1'),
                ('value2', '@value2'),
                ('difference', '@difference'),
                ('fraction', '@fraction'),
            ]

            #show(p)      # show the plot
            #import sys; sys.exit()
            save(p)  # save the plot

        data_means_df = pd.DataFrame(data_means,
                                     index=label_names,
                                     columns=names)
        data_means_df.to_csv(
            'means_of_interhemispheric_fractional_abs_shape_differences.csv')

        data_summaries_df = pd.DataFrame(data_summaries,
                                         index=names,
                                         columns=data_summary.index)
        data_summaries_df.to_csv(
            'summary_of_interhemispheric_fractional_abs_shape_differences.csv')
Esempio n. 5
0
    def compare_thickness_measures():

        import os
        import pandas as pd
        import numpy as np

        from mindboggle.guts.compute import distcorr
        from mindboggle.mio.labels import DKTprotocol

        dkt = DKTprotocol()
        label_names = dkt.cerebrum_cortex_DKT31_names

        subject_list = '/Users/arno/Data/subject_list_Mindboggle101.txt'
        fid = open(subject_list, 'r')
        subjects = [x.strip() for x in fid.readlines()]

        table_dir = '/Users/arno/Data/manual_tables'
        table_path1a = 'tables/left_cortical_surface/label_shapes.csv'
        table_path1b = 'tables/right_cortical_surface/label_shapes.csv'
        table_path2 = 'tables/thickinthehead_per_freesurfer_cortex_label.csv'

        # --------------------------------------------------------------------
        # Loop through subjects and table columns:
        # --------------------------------------------------------------------
        subjects_by_labels1 = np.zeros((len(subjects), len(label_names)))
        subjects_by_labels2 = np.zeros((len(subjects), len(label_names)))
        for isubject, subject in enumerate(subjects):
            # Load shape tables:
            table1a = os.path.join(table_dir, subject, table_path1a)
            table1b = os.path.join(table_dir, subject, table_path1b)
            table2 = os.path.join(table_dir, subject, table_path2)
            columns1a = pd.read_csv(table1a, sep=",", index_col='name')
            columns1b = pd.read_csv(table1b, sep=",", index_col='name')
            column1 = columns1a['freesurfer thickness: median'] + \
                      columns1b['freesurfer thickness: median']
            column1index = column1.index
            columns2 = pd.read_csv(table2, sep=",", index_col='name')
            column2 = columns2.iloc[:, 1]
            column2_match = []
            for icolumn2, column2_index in enumerate(column2.index):
                if column2_index in column1index:
                    column2_match.append(column2[icolumn2])
            subjects_by_labels1[isubject, :] = column1
            subjects_by_labels2[isubject, :] = column2_match

        dcors = []
        for ilabel in range(len(label_names)):
            dcors.append(
                distcorr(subjects_by_labels1[:, ilabel],
                         subjects_by_labels2[:, ilabel]))

        # --------------------------------------------------------------------
        # Save csv files:
        # --------------------------------------------------------------------
        data = pd.DataFrame(
            dcors,
            index=label_names,  #index=columns1.columns)
            columns=[
                'freesurfer / thickinthehead cortical thickness distance correlation'
            ])
        data.to_csv('thickinthehead_FSthickness_distance_correlations_'
                    'per_label_Mindboggle101.csv')
Esempio n. 6
0
    def compare_shapes_between_scans():
        import os
        import numpy as np
        import pandas as pd

        # For plotting:
        from math import pi
        from bokeh.models import HoverTool
        from bokeh.plotting import ColumnDataSource, figure, show, save, output_file
        from mindboggle.mio.colors import viridis_colormap
        from mindboggle.mio.labels import DKTprotocol
        #from mindboggle.mio.plots import histograms_of_lists

        titles = [
            "Fractional difference between re/scan volumes",
            "Fractional difference between re/scan thickinthehead cortical thicknesses",
            "Fractional difference between re/scan left cortical label median areas",
            "Fractional difference between re/scan left cortical label median travel depths",
            "Fractional difference between re/scan left cortical label median geodesic depths",
            "Fractional difference between re/scan left cortical label median mean curvatures",
            "Fractional difference between re/scan left cortical label median FreeSurfer curvatures",
            "Fractional difference between re/scan left cortical label median FreeSurfer thicknesses"
        ]
        # "Fractional difference between re/scan right cortical label median areas",
        # "Fractional difference between re/scan right cortical label median travel depths",
        # "Fractional difference between re/scan right cortical label median geodesic depths",
        # "Fractional difference between re/scan right cortical label median mean curvatures",
        # "Fractional difference between re/scan right cortical label median FreeSurfer curvatures",
        # "Fractional difference between re/scan right cortical label median FreeSurfer thicknesses"]
        # "Fractional difference between re/scan right cortical label median FreeSurfer convexities"]
        names = [
            "volume_for_each_freesurfer_label",
            "thickinthehead_per_freesurfer_cortex_label",
            "median_area_per_freesurfer_left_cortex_label",
            "median_travel_depth_per_freesurfer_left_cortex_label",
            "median_geodesic_depth_per_freesurfer_left_cortex_label",
            "median_mean_curvatures_per_freesurfer_left_cortex_label",
            "median_freesurfer_curvature_per_freesurfer_left_cortex_label",
            "median_freesurfer_thickness_per_freesurfer_left_cortex_label"
        ]
        # "median_area_per_freesurfer_right_cortex_label",
        # "median_travel_depth_per_freesurfer_right_cortex_label",
        # "median_geodesic_depth_per_freesurfer_right_cortex_label",
        # "median_mean_curvatures_per_freesurfer_right_cortex_label",
        # "median_freesurfer_curvature_per_freesurfer_right_cortex_label",
        # "median_freesurfer_thickness_per_freesurfer_right_cortex_label"]
        # "median_freesurfer_convexity_per_freesurfer_right_cortex_label"]
        table_dir = '/Users/arno/Data/shape_tables_for_auto_labels_of_Mindboggle101_rescans'
        tables = [
            os.path.join('tables', 'volume_for_each_freesurfer_label.csv'),
            os.path.join('tables',
                         'thickinthehead_per_freesurfer_cortex_label.csv'),
            os.path.join('tables', 'left_cortical_surface',
                         'label_shapes.csv'),
            os.path.join('tables', 'left_cortical_surface',
                         'label_shapes.csv'),
            os.path.join('tables', 'left_cortical_surface',
                         'label_shapes.csv'),
            os.path.join('tables', 'left_cortical_surface',
                         'label_shapes.csv'),
            os.path.join('tables', 'left_cortical_surface',
                         'label_shapes.csv'),
            os.path.join('tables', 'left_cortical_surface', 'label_shapes.csv')
        ]
        # os.path.join('tables', 'right_cortical_surface', 'label_shapes.csv'),
        # os.path.join('tables', 'right_cortical_surface', 'label_shapes.csv'),
        # os.path.join('tables', 'right_cortical_surface', 'label_shapes.csv'),
        # os.path.join('tables', 'right_cortical_surface', 'label_shapes.csv'),
        # os.path.join('tables', 'right_cortical_surface', 'label_shapes.csv'),
        # os.path.join('tables', 'right_cortical_surface', 'label_shapes.csv')]
        column_indices = [1, 1, 1, 2, 10, 18, 26,
                          34]  #, 1, 2, 10, 18, 26, 34] #, 42]

        # --------------------------------------------------------------------
        # Alternating left, right cortex label numbers (for volume shapes):
        # --------------------------------------------------------------------
        dkt = DKTprotocol()
        labels_left = dkt.left_cerebrum_cortex_DKT31_numbers
        labels_right = dkt.right_cerebrum_cortex_DKT31_numbers
        DKT31_names = dkt.DKT31_names
        # label_list = []
        # label_name_list = []
        # for ilabel, label_left in enumerate(labels_left):
        #     label_list.append(label_left)
        #     label_list.append(labels_right[ilabel])
        #     label_name_list.append(DKT31_names[ilabel] + ' (left)')
        #     label_name_list.append(DKT31_names[ilabel] + ' (right)')
        #label_list = [str(x) for x in label_list]
        ##exclude_sulci = [20] # Sulcus 20 removed from protocol since initial run

        #label_lists = [label_list,
        #               label_list,
        label_lists = [
            labels_left, labels_left, labels_left, labels_left, labels_left,
            labels_left, labels_left, labels_left, labels_left, labels_right,
            labels_right, labels_right, labels_right, labels_right,
            labels_right, labels_right
        ]
        label_name_lists = [DKT31_names for x in range(len(titles))]
        #label_name_lists[0] = label_name_list
        #label_name_lists[1] = label_name_list

        # --------------------------------------------------------------------
        # Colors:
        # --------------------------------------------------------------------
        colors = viridis_colormap()
        #from matplotlib import cm as cmaps
        #import matplotlib.pyplot as plt
        #plt.register_cmap(name='viridis', cmap=cmaps.viridis)
        #plt.set_cmap(cmaps.viridis)

        scale_rect = 40

        # --------------------------------------------------------------------
        # Subjects with second scans:
        # --------------------------------------------------------------------
        groups = ['OASIS-TRT-20', 'MMRR-21']
        numbers = [20, 21]
        nsubjects = sum(numbers)
        subjects = []
        subjects2 = []
        for igroup, group in enumerate(groups):
            for n in range(1, numbers[igroup] + 1):
                subjects.append(group + '-' + str(n))
                subjects2.append(group + '-rescan-' + str(n))

        # --------------------------------------------------------------------
        # Loop through tables:
        # --------------------------------------------------------------------
        data_means = np.zeros((len(labels_left), len(titles)))
        data_summaries = np.zeros((len(titles), 8))
        for ititle, title in enumerate(titles):
            table = tables[ititle]
            name = names[ititle]
            index = column_indices[ititle]
            labels = label_lists[ititle]
            label_names = label_name_lists[ititle]

            # ----------------------------------------------------------------
            # Loop through subjects:
            # ----------------------------------------------------------------
            subject_shapes = np.zeros((nsubjects, len(labels)))
            subject2_shapes = np.zeros((nsubjects, len(labels)))
            for isubject, subject in enumerate(subjects):
                subject2 = subjects2[isubject]
                table_file = os.path.join(table_dir, subject, table)
                table_file2 = os.path.join(table_dir, subject2, table)
                columns = pd.read_csv(table_file, sep=",", index_col='name')
                columns2 = pd.read_csv(table_file2, sep=",", index_col='name')

                # ------------------------------------------------------------
                # Loop through labels:
                # ------------------------------------------------------------
                for ilabel, label in enumerate(labels):
                    for irow in range(columns.shape[0]):
                        if int(columns.iloc[irow][0]) == int(label):
                            value = columns.iloc[irow][index]
                            value2 = columns2.iloc[irow][index]
                            subject_shapes[isubject, ilabel] = value
                            subject2_shapes[isubject, ilabel] = value2

            # ----------------------------------------------------------------
            # Save csv files:
            # ----------------------------------------------------------------
            data = pd.DataFrame(subject_shapes, index=subjects, columns=labels)
            data.to_csv(name + '_scans.csv')
            data_summary = data.describe(include='all')
            data_summary.to_csv(name + '_scans_summary.csv')

            data = pd.DataFrame(subject2_shapes,
                                index=subjects,
                                columns=labels)
            data.to_csv(name + '_rescans.csv')
            data_summary = data.describe(include='all')
            data_summary.to_csv(name + '_rescans_summary.csv')

            subject_shape_diffs = subject2_shapes - subject_shapes
            data = pd.DataFrame(subject_shape_diffs,
                                index=subjects,
                                columns=labels)
            data.to_csv(name + '_differences.csv')
            data_summary = data.describe(include='all')
            data_summary.to_csv(name + '_differences_summary.csv')

            subject_shape_abs_diffs = np.abs(subject_shape_diffs)
            max_diffs = subject_shape_abs_diffs.max(axis=0)
            subject_shape_frac_diffs = subject_shape_diffs / subject_shapes
            data = pd.DataFrame(subject_shape_frac_diffs,
                                index=subjects,
                                columns=labels)
            #iInf, jInf = np.where(data.values == np.inf)
            #data.iloc[iInf, jInf] = 'NaN'
            data.to_csv(name + '_fractional_differences.csv')
            data_summary = data.describe(include='all')
            data_summary.to_csv(name + '_fractional_differences_summary.csv')

            # max_array = np.zeros((nsubjects, len(labels), 2))
            # max_array[:, :, 0] = subject_shapes
            # max_array[:, :, 1] = subject2_shapes
            # max_diff = np.abs(np.max(max_array, axis=2))

            # maxs = np.max(np.max([subject_shapes, subject2_shapes], axis=0), axis=0)
            # mins = np.min(np.min([subject_shapes, subject2_shapes], axis=0), axis=0)
            # max_diff = np.abs(maxs - mins)

            # max_diff = np.max([subject_shapes, subject2_shapes]) -\
            #           np.min([subject_shapes, subject2_shapes])

            subject_shape_frac_abs_diffs = np.abs(subject_shape_abs_diffs /
                                                  subject_shapes)
            data = pd.DataFrame(subject_shape_frac_abs_diffs,
                                index=subjects,
                                columns=labels)
            n50 = len(np.where(data.values > 0.5)[0])
            n25 = len(np.where(data.values > 0.25)[0])
            n10 = len(np.where(data.values > 0.1)[0])
            print(title)
            print("Fractional absolute differences above "
                  "0.5: {0}; 0.25: {1}; 0.1: {2}".format(n50, n25, n10))
            print("")
            #iInf, jInf = np.where(data.values == np.inf)
            #data.iloc[iInf, jInf] = 'NaN'
            data.to_csv(name + '_fractional_abs_differences.csv')
            data_summary = data.describe(include='all')
            data_summary.to_csv(name +
                                '_fractional_abs_differences_summary.csv')

            data_means[:, ititle] = data_summary.loc['mean'].values
            data_summaries[ititle, :] = data_summary.mean(axis=1)

            # ignore_columns = []
            # nbins = 100
            # axis_limits = []
            # histograms_of_lists(subject_shape_diffs, title, ignore_columns,
            #                     nbins, axis_limits, [title])

            # ----------------------------------------------------------------
            # Plot heatmap for labels X subjects array for each table:
            # ----------------------------------------------------------------
            html_file = name + '_fractional_abs_differences.html'
            print(html_file)

            # Set up the data for plotting. We will need to have values for every
            # pair of subject/label names. Map the value to a color.
            subjectx = []
            labelx = []
            label_namex = []
            value1x = []
            value2x = []
            differencex = []
            fractionx = []
            colorx = []
            for ilabel, label in enumerate(labels):
                for isubject, subject in enumerate(subjects):
                    labelx.append(label)
                    label_namex.append(label_names[ilabel])
                    subjectx.append(subject)
                    value1 = subject_shapes[isubject, ilabel]
                    value2 = subject2_shapes[isubject, ilabel]
                    difference = subject_shape_diffs[isubject, ilabel]
                    value1x.append(value1)
                    value2x.append(value2)
                    differencex.append(difference)
                    fraction = subject_shape_frac_diffs[isubject, ilabel]
                    fractionx.append(fraction)
                    abs_fraction = subject_shape_frac_abs_diffs[isubject,
                                                                ilabel]
                    if np.isnan(value) or np.isnan(abs_fraction):
                        rgb = [0, 0, 0]
                    elif abs_fraction > 1.0:
                        rgb = [1, 1, 1]
                    else:
                        rgb = [
                            np.int(255 * x)
                            for x in colors[np.int(255 * abs_fraction)]
                        ]
                    hex = "#%02x%02x%02x" % tuple(rgb)
                    colorx.append(hex)

            output_file(html_file, title=title)
            source = ColumnDataSource(
                dict(subject=subjectx,
                     label=labelx,
                     label_name=label_namex,
                     color=colorx,
                     value1=value1x,
                     value2=value2x,
                     difference=differencex,
                     fraction=fractionx))
            TOOLS = "hover,save,pan,box_zoom,wheel_zoom"

            plot_width = len(subjects) * scale_rect
            plot_height = len(labels) * scale_rect
            p = figure(title=title,
                       x_range=subjects,
                       y_range=list(reversed(label_names)),
                       plot_width=plot_width,
                       plot_height=plot_height,
                       x_axis_location="above",
                       tools=TOOLS)
            p.grid.grid_line_color = None
            p.axis.axis_line_color = None
            p.axis.major_tick_line_color = None
            p.axis.major_label_text_font_size = "10pt"
            p.axis.major_label_standoff = 0
            p.xaxis.major_label_orientation = pi / 3
            p.rect(x="subject",
                   y="label_name",
                   width=1,
                   height=1,
                   source=source,
                   color="color",
                   line_color=None)

            p.select_one(HoverTool).tooltips = [
                ('subject', '@subject'),
                ('label', '@label'),
                ('label name', '@label_name'),
                ('value1', '@value1'),
                ('value2', '@value2'),
                ('difference', '@difference'),
                ('fraction', '@fraction'),
            ]

            #show(p)      # show the plot
            #import sys; sys.exit()
            save(p)  # save the plot

        data_means_df = pd.DataFrame(data_means,
                                     index=label_names,
                                     columns=names)
        data_means_df.to_csv(
            'means_of_rescan_fractional_abs_shape_differences.csv')

        data_summaries_df = pd.DataFrame(data_summaries,
                                         index=names,
                                         columns=data_summary.index)
        data_summaries_df.to_csv(
            'summary_of_rescan_fractional_abs_shape_differences.csv')
Esempio n. 7
0
def extract_sulci(labels_file,
                  folds_or_file,
                  hemi,
                  min_boundary=1,
                  sulcus_names=[],
                  save_file=False,
                  output_file='',
                  background_value=-1,
                  verbose=False):
    """
    Identify sulci from folds in a brain surface according to a labeling
    protocol that includes a list of label pairs defining each sulcus.

    Since folds are defined as deep, connected areas of a surface, and since
    folds may be connected to each other in ways that differ across brains,
    there usually does not exist a one-to-one mapping between folds of one
    brain and those of another.  To address the correspondence problem then,
    we need to find just those portions of the folds that correspond across
    brains. To accomplish this, Mindboggle segments folds into sulci, which
    do have a one-to-one correspondence across non-pathological brains.
    Mindboggle defines a sulcus as a folded portion of cortex whose opposing
    banks are labeled with one or more sulcus label pairs in the DKT labeling
    protocol, where each label pair is unique to one sulcus and represents
    a boundary between two adjacent gyri, and each vertex has one gyrus label.

    This function assigns vertices in a fold to a sulcus in one of two cases.
    In the first case, vertices whose labels are in only one label pair in
    the fold are assigned to the label pair’s sulcus if they are connected
    through similarly labeled vertices to the boundary between the two labels.
    In the second case, the segment_regions function propagates labels from
    label borders to vertices whose labels are in multiple label pairs in the
    fold.

    Steps for each fold ::

        1. Remove fold if it has fewer than two labels.
        2. Remove fold if its labels do not contain a sulcus label pair.
        3. Find vertices with labels that are in only one of the fold's
           label boundary pairs. Assign the vertices the sulcus with the label
           pair if they are connected to the label boundary for that pair.
        4. If there are remaining vertices, segment into sets of vertices
           connected to label boundaries, and assign a unique ID to each set.

    Parameters
    ----------
    labels_file : string
        file name for surface mesh VTK containing labels for all vertices
    folds_or_file : numpy array, list or string
        fold number for each vertex / name of VTK file containing fold scalars
    hemi : string
        hemisphere abbreviation in {'lh', 'rh'} for sulcus labels
    min_boundary : integer
        minimum number of vertices for a sulcus label boundary segment
    sulcus_names : list of strings
        names of sulci
    save_file : bool
        save output VTK file?
    output_file : string
        name of output file in VTK format
    background_value : integer or float
        background value
    verbose : bool
        print statements?

    Returns
    -------
    sulci : list of integers
        sulcus numbers for all vertices (-1 for non-sulcus vertices)
    n_sulci : integers
        number of sulci
    sulci_file : string
        output VTK file with sulcus numbers (-1 for non-sulcus vertices)

    Examples
    --------
    >>> # Example 1: Extract sulcus from a fold with one sulcus label pair:
    >>> import numpy as np
    >>> from mindboggle.features.sulci import extract_sulci
    >>> from mindboggle.mio.vtks import read_scalars
    >>> from mindboggle.mio.fetch_data import prep_tests
    >>> urls, fetch_data = prep_tests()
    >>> # Load labels, folds, neighbor lists, and sulcus names and label pairs
    >>> labels_file = fetch_data(urls['left_freesurfer_labels'], '', '.vtk')
    >>> folds_file = fetch_data(urls['left_folds'], '', '.vtk')
    >>> folds_or_file, name = read_scalars(folds_file, True, True)
    >>> save_file = True
    >>> output_file = 'extract_sulci_fold4_1sulcus.vtk'
    >>> background_value = -1
    >>> # Limit number of folds to speed up the test:
    >>> limit_folds = True
    >>> if limit_folds:
    ...     fold_numbers = [4] #[4, 6]
    ...     i0 = [i for i,x in enumerate(folds_or_file) if x not in fold_numbers]
    ...     folds_or_file[i0] = background_value
    >>> hemi = 'lh'
    >>> min_boundary = 10
    >>> sulcus_names = []
    >>> verbose = False
    >>> sulci, n_sulci, sulci_file = extract_sulci(labels_file, folds_or_file,
    ...     hemi, min_boundary, sulcus_names, save_file, output_file,
    ...     background_value, verbose)
    >>> n_sulci  # 23 # (if not limit_folds)
    1
    >>> lens = [len([x for x in sulci if x==y])
    ...         for y in np.unique(sulci) if y != -1]
    >>> lens[0:10]  # [6358, 3288, 7612, 5205, 4414, 6251, 3493, 2566, 4436, 739] # (if not limit_folds)
    [1151]

    View result without background (skip test):

    >>> from mindboggle.mio.plots import plot_surfaces # doctest: +SKIP
    >>> from mindboggle.mio.vtks import rewrite_scalars # doctest: +SKIP
    >>> output = 'extract_sulci_fold4_1sulcus_no_background.vtk'
    >>> rewrite_scalars(sulci_file, output, sulci,
    ...                 'sulci', sulci) # doctest: +SKIP
    >>> plot_surfaces(output) # doctest: +SKIP

    Example 2:  Extract sulcus from a fold with multiple sulcus label pairs:

    >>> folds_or_file, name = read_scalars(folds_file, True, True)
    >>> output_file = 'extract_sulci_fold7_2sulci.vtk'
    >>> # Limit number of folds to speed up the test:
    >>> limit_folds = True
    >>> if limit_folds:
    ...     fold_numbers = [7] #[4, 6]
    ...     i0 = [i for i,x in enumerate(folds_or_file) if x not in fold_numbers]
    ...     folds_or_file[i0] = background_value
    >>> sulci, n_sulci, sulci_file = extract_sulci(labels_file, folds_or_file,
    ...     hemi, min_boundary, sulcus_names, save_file, output_file,
    ...     background_value, verbose)
    >>> n_sulci  # 23 # (if not limit_folds)
    2
    >>> lens = [len([x for x in sulci if x==y])
    ...         for y in np.unique(sulci) if y != -1]
    >>> lens[0:10]  # [6358, 3288, 7612, 5205, 4414, 6251, 3493, 2566, 4436, 739] # (if not limit_folds)
    [369, 93]

    View result without background (skip test):

    >>> from mindboggle.mio.plots import plot_surfaces # doctest: +SKIP
    >>> from mindboggle.mio.vtks import rewrite_scalars # doctest: +SKIP
    >>> output = 'extract_sulci_fold7_2sulci_no_background.vtk'
    >>> rewrite_scalars(sulci_file, output, sulci,
    ...                 'sulci', sulci) # doctest: +SKIP
    >>> plot_surfaces(output) # doctest: +SKIP

    """
    import os
    from time import time
    import numpy as np

    from mindboggle.mio.vtks import read_scalars, read_vtk, rewrite_scalars
    from mindboggle.guts.mesh import find_neighbors
    from mindboggle.guts.segment import extract_borders, propagate, segment_regions
    from mindboggle.mio.labels import DKTprotocol

    # Load fold numbers if folds_or_file is a string:
    if isinstance(folds_or_file, str):
        folds, name = read_scalars(folds_or_file)
    elif isinstance(folds_or_file, list):
        folds = folds_or_file
    elif isinstance(folds_or_file, np.ndarray):
        folds = folds_or_file.tolist()

    dkt = DKTprotocol()

    if hemi == 'lh':
        pair_lists = dkt.left_sulcus_label_pair_lists
    elif hemi == 'rh':
        pair_lists = dkt.right_sulcus_label_pair_lists
    else:
        raise IOError(
            "Warning: hemisphere not properly specified ('lh' or 'rh').")

    # Load points, faces, and neighbors:
    points, indices, lines, faces, labels, scalar_names, npoints, \
            input_vtk = read_vtk(labels_file)
    neighbor_lists = find_neighbors(faces, npoints)

    # Array of sulcus IDs for fold vertices, initialized as -1.
    # Since we do not touch gyral vertices and vertices whose labels
    # are not in the label list, or vertices having only one label,
    # their sulcus IDs will remain -1:
    sulci = background_value * np.ones(npoints)

    # ------------------------------------------------------------------------
    # Loop through folds
    # ------------------------------------------------------------------------
    fold_numbers = [int(x) for x in np.unique(folds) if x != background_value]
    n_folds = len(fold_numbers)
    if verbose:
        print("Extract sulci from {0} folds...".format(n_folds))
    t0 = time()
    for n_fold in fold_numbers:
        fold_indices = [i for i, x in enumerate(folds) if x == n_fold]
        len_fold = len(fold_indices)

        # List the labels in this fold:
        fold_labels = [labels[x] for x in fold_indices]
        unique_fold_labels = [
            int(x) for x in np.unique(fold_labels) if x != background_value
        ]

        # --------------------------------------------------------------------
        # NO MATCH -- fold has fewer than two labels
        # --------------------------------------------------------------------
        if verbose and len(unique_fold_labels) < 2:
            # Ignore: sulci already initialized with -1 values:
            if not unique_fold_labels:
                print("  Fold {0} ({1} vertices): "
                      "NO MATCH -- fold has no labels".format(
                          n_fold, len_fold))
            else:
                print("  Fold {0} ({1} vertices): "
                      "NO MATCH -- fold has only one label ({2})".format(
                          n_fold, len_fold, unique_fold_labels[0]))
            # Ignore: sulci already initialized with -1 values

        else:
            # Find all label boundary pairs within the fold:
            indices_fold_pairs, fold_pairs, unique_fold_pairs = \
                extract_borders(fold_indices, labels, neighbor_lists,
                                ignore_values=[], return_label_pairs=True)

            # Find fold label pairs in the protocol (pairs are already sorted):
            fold_pairs_in_protocol = [
                x for x in unique_fold_pairs
                if x in dkt.unique_sulcus_label_pairs
            ]

            if verbose and unique_fold_labels:
                print("  Fold {0} labels: {1} ({2} vertices)".format(
                    n_fold, ', '.join([str(x) for x in unique_fold_labels]),
                    len_fold))
            # ----------------------------------------------------------------
            # NO MATCH -- fold has no sulcus label pair
            # ----------------------------------------------------------------
            if verbose and not fold_pairs_in_protocol:
                print("  Fold {0}: NO MATCH -- fold has no sulcus label pair".
                      format(n_fold, len_fold))

            # ----------------------------------------------------------------
            # Possible matches
            # ----------------------------------------------------------------
            else:
                if verbose:
                    print("  Fold {0} label pairs in protocol: {1}".format(
                        n_fold,
                        ', '.join([str(x) for x in fold_pairs_in_protocol])))

                # Labels in the protocol (includes repeats across label pairs):
                labels_in_pairs = [
                    x for lst in fold_pairs_in_protocol for x in lst
                ]

                # Labels that appear in one or more sulcus label boundary:
                unique_labels = []
                nonunique_labels = []
                for label in np.unique(labels_in_pairs):
                    if len([x for x in labels_in_pairs if x == label]) == 1:
                        unique_labels.append(label)
                    else:
                        nonunique_labels.append(label)

                # ------------------------------------------------------------
                # Vertices whose labels are in only one sulcus label pair
                # ------------------------------------------------------------
                # Find vertices with a label that is in only one of the fold's
                # label pairs (the other label in the pair can exist in other
                # pairs). Assign the vertices the sulcus with the label pair
                # if they are connected to the label boundary for that pair.
                # ------------------------------------------------------------
                if unique_labels:

                    for pair in fold_pairs_in_protocol:

                        # If one or both labels in label pair is/are unique:
                        unique_labels_in_pair = [
                            x for x in pair if x in unique_labels
                        ]
                        n_unique = len(unique_labels_in_pair)
                        if n_unique:

                            ID = None
                            for i, pair_list in enumerate(pair_lists):
                                if not isinstance(pair_list, list):
                                    pair_list = [pair_list]
                                if pair in pair_list:
                                    ID = i
                                    break
                            if ID:
                                # Seeds from label boundary vertices
                                # (fold_pairs and pair already sorted):
                                indices_pair = [
                                    x for i, x in enumerate(indices_fold_pairs)
                                    if fold_pairs[i] == pair
                                ]

                                # Vertices with unique label(s) in pair:
                                indices_unique_labels = [
                                    fold_indices[i]
                                    for i, x in enumerate(fold_labels)
                                    if x in unique_labels_in_pair
                                ]
                                #dkt.unique_sulcus_label_pairs]

                                # Propagate sulcus ID from seeds to vertices
                                # with "unique" labels (only exist in one
                                # label pair in a fold); propagation ensures
                                # that sulci consist of contiguous vertices
                                # for each label boundary:
                                sulci2 = segment_regions(
                                    indices_unique_labels,
                                    neighbor_lists,
                                    min_region_size=1,
                                    seed_lists=[indices_pair],
                                    keep_seeding=False,
                                    spread_within_labels=True,
                                    labels=labels,
                                    label_lists=[],
                                    values=[],
                                    max_steps='',
                                    background_value=background_value,
                                    verbose=False)

                                sulci[sulci2 != background_value] = ID

                                # Print statement:
                                if verbose:
                                    if n_unique == 1:
                                        ps1 = 'One label'
                                    else:
                                        ps1 = 'Both labels'
                                    if len(sulcus_names):
                                        ps2 = sulcus_names[ID]
                                    else:
                                        ps2 = ''
                                    print("    {0} unique to one fold pair: "
                                          "{1} {2}".format(
                                              ps1, ps2, unique_labels_in_pair))

                # ------------------------------------------------------------
                # Vertex labels shared by multiple label pairs
                # ------------------------------------------------------------
                # Propagate labels from label borders to vertices with labels
                # that are shared by multiple label pairs in the fold.
                # ------------------------------------------------------------
                if len(nonunique_labels):
                    # For each label shared by different label pairs:
                    for label in nonunique_labels:
                        # Print statement:
                        if verbose:
                            print(
                                "    Propagate sulcus borders with label {0}".
                                format(int(label)))

                        # Construct seeds from label boundary vertices:
                        seeds = background_value * np.ones(npoints)

                        for ID, pair_list in enumerate(pair_lists):
                            if not isinstance(pair_list, list):
                                pair_list = [pair_list]
                            label_pairs = [x for x in pair_list if label in x]
                            for label_pair in label_pairs:
                                indices_pair = [
                                    x for i, x in enumerate(indices_fold_pairs)
                                    if np.sort(fold_pairs[i]).tolist() ==
                                    label_pair
                                ]
                                if indices_pair:

                                    # Do not include short boundary segments:
                                    if min_boundary > 1:
                                        indices_pair2 = []
                                        seeds2 = segment_regions(
                                            indices_pair, neighbor_lists, 1,
                                            [], False, False, [], [], [], '',
                                            background_value, verbose)

                                        useeds2 = [
                                            x for x in np.unique(seeds2)
                                            if x != background_value
                                        ]
                                        for seed2 in useeds2:
                                            iseed2 = [
                                                i for i, x in enumerate(seeds2)
                                                if x == seed2
                                            ]
                                            if len(iseed2) >= min_boundary:
                                                indices_pair2.extend(iseed2)
                                            elif verbose:
                                                if len(iseed2) == 1:
                                                    print("    Remove "
                                                          "assignment "
                                                          "of ID {0} from "
                                                          "1 vertex".format(
                                                              seed2))
                                                else:
                                                    print(
                                                        "    Remove "
                                                        "assignment "
                                                        "of ID {0} from "
                                                        "{1} vertices".format(
                                                            seed2,
                                                            len(iseed2)))
                                        indices_pair = indices_pair2

                                    # Assign sulcus IDs to seeds:
                                    seeds[indices_pair] = ID

                        # Identify vertices with the label:
                        indices_label = [
                            fold_indices[i] for i, x in enumerate(fold_labels)
                            if x == label
                        ]
                        if len(indices_label):

                            # Propagate sulcus ID from seeds to vertices
                            # with a given shared label:
                            seg_vs_prop = False
                            if seg_vs_prop:
                                indices_seeds = []
                                for seed in [
                                        x for x in np.unique(seeds)
                                        if x != background_value
                                ]:
                                    indices_seeds.append([
                                        i for i, x in enumerate(seeds)
                                        if x == seed
                                    ])

                                sulci2 = segment_regions(
                                    indices_label, neighbor_lists, 50,
                                    indices_seeds, False, True, labels, [], [],
                                    '', background_value, verbose)
                            else:
                                label_array = background_value * \
                                              np.ones(npoints)
                                label_array[indices_label] = 1
                                sulci2 = propagate(
                                    points,
                                    faces,
                                    label_array,
                                    seeds,
                                    sulci,
                                    max_iters=10000,
                                    tol=0.001,
                                    sigma=5,
                                    background_value=background_value,
                                    verbose=verbose)
                            sulci[sulci2 != background_value] = \
                                sulci2[sulci2 != background_value]

    sulcus_numbers = [
        int(x) for x in np.unique(sulci) if x != background_value
    ]
    n_sulci = len(sulcus_numbers)

    # ------------------------------------------------------------------------
    # Print statements
    # ------------------------------------------------------------------------
    if verbose:
        if n_sulci == 1:
            sulcus_str = 'sulcus'
        else:
            sulcus_str = 'sulci'
        if n_folds == 1:
            folds_str = 'fold'
        else:
            folds_str = 'folds'
        print("Extracted {0} {1} from {2} {3} ({4:.1f}s):".format(
            n_sulci, sulcus_str, n_folds, folds_str,
            time() - t0))
        if sulcus_names:
            for sulcus_number in sulcus_numbers:
                print("  {0}: {1}".format(sulcus_number,
                                          sulcus_names[sulcus_number]))
        elif sulcus_numbers:
            print("  " + ", ".join([str(x) for x in sulcus_numbers]))

        unresolved = [
            i for i in range(len(pair_lists)) if i not in sulcus_numbers
        ]
        if len(unresolved) == 1:
            print("The following sulcus is unaccounted for:")
        else:
            print("The following {0} sulci are unaccounted for:".format(
                len(unresolved)))
        if sulcus_names:
            for sulcus_number in unresolved:
                print("  {0}: {1}".format(sulcus_number,
                                          sulcus_names[sulcus_number]))
        else:
            print("  " + ", ".join([str(x) for x in unresolved]))

    # ------------------------------------------------------------------------
    # Return sulci, number of sulci, and file name
    # ------------------------------------------------------------------------
    sulci = [int(x) for x in sulci]

    sulci_file = os.path.join(os.getcwd(), 'sulci.vtk')
    rewrite_scalars(labels_file, sulci_file, sulci, 'sulci', [],
                    background_value)

    if not os.path.exists(sulci_file):
        raise IOError(sulci_file + " not found")

    return sulci, n_sulci, sulci_file
Esempio n. 8
0
def concatenate_sulcus_scalars(scalar_files,
                               fold_files,
                               label_files,
                               background_value=-1):
    """
    Prepare data for estimating scalar distributions along and outside fundi.

    Extract (e.g., depth, curvature) scalar values in folds, along sulcus
    label boundaries as well as outside the sulcus label boundaries.
    Concatenate these scalar values across multiple files.

    Parameters
    ----------
    scalar_files : list of strings
        names of surface mesh VTK files with scalar values to concatenate
    fold_files : list of strings (corr. to each list in scalar_files)
        VTK files with fold numbers as scalars (-1 for non-fold vertices)
    label_files : list of strings (corr. to fold_files)
        VTK files with label numbers (-1 for unlabeled vertices)
    background_value : integer or float
        background value

    Returns
    -------
    border_scalars : list of floats
        concatenated scalar values within folds along sulcus label boundaries
    nonborder_scalars : list of floats
        concatenated scalar values within folds outside sulcus label boundaries

    Examples
    --------
    >>> # Concatenate (duplicate) depth scalars:
    >>> import numpy as np
    >>> from mindboggle.shapes.likelihood import concatenate_sulcus_scalars
    >>> from mindboggle.mio.fetch_data import prep_tests
    >>> urls, fetch_data = prep_tests()
    >>> depth_file = fetch_data(urls['left_travel_depth'], '', '.vtk')
    >>> labels_file = fetch_data(urls['left_freesurfer_labels'], '', '.vtk')
    >>> folds_file = fetch_data(urls['left_folds'], '', '.vtk')
    >>> scalar_files = [depth_file, depth_file]
    >>> fold_files = [folds_file, folds_file]
    >>> label_files = [labels_file, labels_file]
    >>> background_value = -1
    >>> border, nonborder = concatenate_sulcus_scalars(scalar_files,
    ...     fold_files, label_files, background_value)
    >>> print(np.array_str(np.array(border[0:5]),
    ...       precision=5, suppress_small=True))
    [ 3.48284  2.57157  4.27596  4.56549  3.84881]
    >>> print(np.array_str(np.array(nonborder[0:5]),
    ...       precision=5, suppress_small=True))
    [ 2.87204  2.89388  3.55364  2.81681  3.70736]

    """
    import numpy as np

    from mindboggle.mio.vtks import read_scalars
    from mindboggle.guts.mesh import find_neighbors_from_file
    from mindboggle.guts.segment import extract_borders
    from mindboggle.mio.labels import DKTprotocol

    dkt = DKTprotocol()

    # Prepare (non-unique) list of sulcus label pairs:
    protocol_label_pairs = [
        x for lst in dkt.sulcus_label_pair_lists for x in lst
    ]

    border_scalars = []
    nonborder_scalars = []

    # Loop through files with the scalar values:
    for ifile, scalar_file in enumerate(scalar_files):
        #print(scalar_file)

        # Load scalars, folds, and labels:
        folds_file = fold_files[ifile]
        labels_file = label_files[ifile]
        scalars, name = read_scalars(scalar_file, True, True)
        if scalars.shape:
            folds, name = read_scalars(folds_file)
            labels, name = read_scalars(labels_file)
            indices_folds = [
                i for i, x in enumerate(folds) if x != background_value
            ]
            neighbor_lists = find_neighbors_from_file(labels_file)

            # Find all label border pairs within the folds:
            indices_label_pairs, label_pairs, unique_pairs = extract_borders(
                indices_folds,
                labels,
                neighbor_lists,
                ignore_values=[-1],
                return_label_pairs=True)
            indices_label_pairs = np.array(indices_label_pairs)

            # Find vertices with label pairs in the sulcus labeling protocol:
            Ipairs_in_protocol = [
                i for i, x in enumerate(label_pairs)
                if x in protocol_label_pairs
            ]
            indices_label_pairs = indices_label_pairs[Ipairs_in_protocol]
            indices_outside_pairs = list(
                frozenset(indices_folds).difference(indices_label_pairs))

            # Store scalar values in folds along label border pairs:
            border_scalars.extend(scalars[indices_label_pairs].tolist())

            # Store scalar values in folds outside label border pairs:
            nonborder_scalars.extend(scalars[indices_outside_pairs].tolist())

    return border_scalars, nonborder_scalars
Esempio n. 9
0
    return output_file


#-----------------------------------------------------------------------------
# Run evaluate_labels.py on Mindboggle-101 data
# to compare manual and automated volume labels and surface labels.
#-----------------------------------------------------------------------------
if __name__ == "__main__":

    import os

    from mindboggle.mio.labels import DKTprotocol
    from mindboggle.evaluate.evaluate_labels import evaluate_volume_overlaps
    from mindboggle.evaluate.evaluate_labels import evaluate_surface_overlaps

    dkt = DKTprotocol()

    #-------------------------------------------------------------------------
    # Settings:
    #-------------------------------------------------------------------------
    label_method = 'freesurfer'  # 'ants'
    use_ants_segmentation = True  # False

    #-------------------------------------------------------------------------
    # File names, paths:
    #-------------------------------------------------------------------------
    if use_ants_segmentation:
        ants_str = ''
    else:
        ants_str = '_no_ants'
    if label_method == 'ants':
Esempio n. 10
0
def extract_sulci(labels_file, folds_or_file, hemi, min_boundary=1,
                  sulcus_names=[], verbose=False):
    """
    Identify sulci from folds in a brain surface according to a labeling
    protocol that includes a list of label pairs defining each sulcus.

    A fold is a group of connected, deep vertices.

    Steps for each fold ::

        1. Remove fold if it has fewer than two labels.
        2. Remove fold if its labels do not contain a sulcus label pair.
        3. Find vertices with labels that are in only one of the fold's
           label boundary pairs. Assign the vertices the sulcus with the label
           pair if they are connected to the label boundary for that pair.
        4. If there are remaining vertices, segment into sets of vertices
           connected to label boundaries, and assign a unique ID to each set.

    Parameters
    ----------
    labels_file : string
        file name for surface mesh VTK containing labels for all vertices
    folds_or_file : list or string
        fold number for each vertex / name of VTK file containing fold scalars
    hemi : string
        hemisphere abbreviation in {'lh', 'rh'} for sulcus labels
    min_boundary : integer
        minimum number of vertices for a sulcus label boundary segment
    sulcus_names : list of strings
        names of sulci
    verbose : bool
        print statements?

    Returns
    -------
    sulci : list of integers
        sulcus numbers for all vertices (-1 for non-sulcus vertices)
    n_sulci : integers
        number of sulci
    sulci_file : string
        output VTK file with sulcus numbers (-1 for non-sulcus vertices)

    Examples
    --------
    >>> from mindboggle.features.sulci import extract_sulci
    >>> from mindboggle.mio.vtks import read_scalars
    >>> from mindboggle.mio.fetch_data import prep_tests
    >>> urls, fetch_data = prep_tests()
    >>> # Load labels, folds, neighbor lists, and sulcus names and label pairs
    >>> labels_file = fetch_data(urls['left_freesurfer_labels'])
    >>> folds_file = fetch_data(urls['left_folds'])
    >>> folds_or_file, name = read_scalars(folds_file)
    >>> hemi = 'lh'
    >>> min_boundary = 10
    >>> sulcus_names = []
    >>> verbose = False
    >>> sulci, n_sulci, sulci_file = extract_sulci(labels_file, folds_or_file,
    ...     hemi, min_boundary, sulcus_names, verbose)
    >>> n_sulci
    23
    >>> lens = [len([x for x in sulci if x == y]) for y in range(n_sulci)]
    >>> lens[0:10]
    [0, 6573, 3366, 6689, 5358, 4049, 6379, 3551, 2632, 4225]
    >>> lens[10::]
    [754, 3724, 2197, 5823, 1808, 5122, 513, 2153, 1445, 418, 0, 3556, 1221]

    View result (skip test):

    >>> from mindboggle.mio.plots import plot_surfaces
    >>> plot_surfaces('sulci.vtk') # doctest: +SKIP

    """
    import os
    from time import time
    import numpy as np

    from mindboggle.mio.vtks import read_scalars, read_vtk, rewrite_scalars
    from mindboggle.guts.mesh import find_neighbors
    from mindboggle.guts.segment import extract_borders, propagate, segment
    from mindboggle.mio.labels import DKTprotocol


    # Load fold numbers if folds_or_file is a string:
    if isinstance(folds_or_file, str):
        folds, name = read_scalars(folds_or_file)
    elif isinstance(folds_or_file, list):
        folds = folds_or_file

    dkt = DKTprotocol()

    if hemi == 'lh':
        pair_lists = dkt.left_sulcus_label_pair_lists
    elif hemi == 'rh':
        pair_lists = dkt.right_sulcus_label_pair_lists
    else:
        raise IOError("Warning: hemisphere not properly specified ('lh' or 'rh').")

    # Load points, faces, and neighbors:
    points, indices, lines, faces, labels, scalar_names, npoints, \
            input_vtk = read_vtk(labels_file)
    neighbor_lists = find_neighbors(faces, npoints)

    # Array of sulcus IDs for fold vertices, initialized as -1.
    # Since we do not touch gyral vertices and vertices whose labels
    # are not in the label list, or vertices having only one label,
    # their sulcus IDs will remain -1:
    sulci = -1 * np.ones(npoints)

    #-------------------------------------------------------------------------
    # Loop through folds
    #-------------------------------------------------------------------------
    fold_numbers = [int(x) for x in np.unique(folds) if x != -1]
    n_folds = len(fold_numbers)
    if verbose:
        print("Extract sulci from {0} folds...".format(n_folds))
    t0 = time()
    for n_fold in fold_numbers:
        fold = [i for i,x in enumerate(folds) if x == n_fold]
        len_fold = len(fold)

        # List the labels in this fold:
        fold_labels = [labels[x] for x in fold]
        unique_fold_labels = [int(x) for x in np.unique(fold_labels)
                              if x != -1]

        #---------------------------------------------------------------------
        # NO MATCH -- fold has fewer than two labels
        #---------------------------------------------------------------------
        if verbose and len(unique_fold_labels) < 2:
            # Ignore: sulci already initialized with -1 values:
            if not unique_fold_labels:
                print("  Fold {0} ({1} vertices): "
                      "NO MATCH -- fold has no labels".
                      format(n_fold, len_fold))
            else:
                print("  Fold {0} ({1} vertices): "
                  "NO MATCH -- fold has only one label ({2})".
                  format(n_fold, len_fold, unique_fold_labels[0]))
            # Ignore: sulci already initialized with -1 values

        else:
            # Find all label boundary pairs within the fold:
            indices_fold_pairs, fold_pairs, unique_fold_pairs = \
                extract_borders(fold, labels, neighbor_lists,
                                ignore_values=[], return_label_pairs=True)

            # Find fold label pairs in the protocol (pairs are already sorted):
            fold_pairs_in_protocol = [x for x in unique_fold_pairs
                                      if x in dkt.unique_sulcus_label_pairs]

            if verbose and unique_fold_labels:
                print("  Fold {0} labels: {1} ({2} vertices)".format(n_fold,
                      ', '.join([str(x) for x in unique_fold_labels]),
                      len_fold))
            #-----------------------------------------------------------------
            # NO MATCH -- fold has no sulcus label pair
            #-----------------------------------------------------------------
            if verbose and not fold_pairs_in_protocol:
                print("  Fold {0}: NO MATCH -- fold has no sulcus label pair".
                      format(n_fold, len_fold))

            #-----------------------------------------------------------------
            # Possible matches
            #-----------------------------------------------------------------
            else:
                if verbose:
                    print("  Fold {0} label pairs in protocol: {1}".format(n_fold,
                          ', '.join([str(x) for x in fold_pairs_in_protocol])))

                # Labels in the protocol (includes repeats across label pairs):
                labels_in_pairs = [x for lst in fold_pairs_in_protocol
                                   for x in lst]

                # Labels that appear in one or more sulcus label boundary:
                unique_labels = []
                nonunique_labels = []
                for label in np.unique(labels_in_pairs):
                    if len([x for x in labels_in_pairs if x == label]) == 1:
                        unique_labels.append(label)
                    else:
                        nonunique_labels.append(label)

                #-------------------------------------------------------------
                # Vertices whose labels are in only one sulcus label pair
                #-------------------------------------------------------------
                # Find vertices with a label that is in only one of the fold's
                # label pairs (the other label in the pair can exist in other
                # pairs). Assign the vertices the sulcus with the label pair
                # if they are connected to the label boundary for that pair.
                #-------------------------------------------------------------
                if unique_labels:

                    for pair in fold_pairs_in_protocol:

                        # If one or both labels in label pair is/are unique:
                        unique_labels_in_pair = [x for x in pair
                                                 if x in unique_labels]
                        n_unique = len(unique_labels_in_pair)
                        if n_unique:

                            ID = None
                            for i, pair_list in enumerate(pair_lists):
                                if not isinstance(pair_list, list):
                                    pair_list = [pair_list]
                                if pair in pair_list:
                                    ID = i
                                    break
                            if ID:
                                # Seeds from label boundary vertices
                                # (fold_pairs and pair already sorted):
                                indices_pair = [x for i,x
                                    in enumerate(indices_fold_pairs)
                                    if fold_pairs[i] == pair]

                                # Vertices with unique label(s) in pair:
                                indices_unique_labels = [fold[i]
                                     for i,x in enumerate(fold_labels)
                                     if x in dkt.unique_sulcus_label_pairs]

                                # Propagate from seeds to labels in label pair:
                                sulci2 = segment(indices_unique_labels,
                                                 neighbor_lists,
                                                 min_region_size=1,
                                                 seed_lists=[indices_pair],
                                                 keep_seeding=False,
                                                 spread_within_labels=True,
                                                 labels=labels)
                                sulci[sulci2 != -1] = ID

                                # Print statement:
                                if verbose:
                                    if n_unique == 1:
                                        ps1 = '1 label'
                                    else:
                                        ps1 = 'Both labels'
                                    if len(sulcus_names):
                                        ps2 = sulcus_names[ID]
                                    else:
                                        ps2 = ''
                                    print("    {0} unique to one fold pair: "
                                          "{1} {2}".
                                          format(ps1, ps2,
                                                 unique_labels_in_pair))

                #-------------------------------------------------------------
                # Vertex labels shared by multiple label pairs
                #-------------------------------------------------------------
                # Propagate labels from label borders to vertices with labels
                # that are shared by multiple label pairs in the fold.
                #-------------------------------------------------------------
                if len(nonunique_labels):
                    # For each label shared by different label pairs:
                    for label in nonunique_labels:
                        # Print statement:
                        if verbose:
                            print("    Propagate sulcus borders with label {0}".
                                  format(int(label)))

                        # Construct seeds from label boundary vertices:
                        seeds = -1 * np.ones(len(points))

                        for ID, pair_list in enumerate(pair_lists):
                            if not isinstance(pair_list, list):
                                pair_list = [pair_list]
                            label_pairs = [x for x in pair_list if label in x]
                            for label_pair in label_pairs:
                                indices_pair = [x for i,x
                                    in enumerate(indices_fold_pairs)
                                    if np.sort(fold_pairs[i]).
                                    tolist() == label_pair]
                                if indices_pair:

                                    # Do not include short boundary segments:
                                    if min_boundary > 1:
                                        indices_pair2 = []
                                        seeds2 = segment(indices_pair,
                                                         neighbor_lists)
                                        useeds2 = [x for x in
                                                   np.unique(seeds2)
                                                   if x != -1]
                                        for seed2 in useeds2:
                                            iseed2 = [i for i,x
                                                      in enumerate(seeds2)
                                                      if x == seed2]
                                            if len(iseed2) >= min_boundary:
                                                indices_pair2.extend(iseed2)
                                            elif verbose:
                                                if len(iseed2) == 1:
                                                    print("    Remove "
                                                          "assignment "
                                                          "of ID {0} from "
                                                          "1 vertex".
                                                          format(seed2))
                                                else:
                                                    print("    Remove "
                                                          "assignment "
                                                          "of ID {0} from "
                                                          "{1} vertices".
                                                          format(seed2,
                                                                 len(iseed2)))
                                        indices_pair = indices_pair2

                                    # Assign sulcus IDs to seeds:
                                    seeds[indices_pair] = ID

                        # Identify vertices with the label:
                        label_array = -1 * np.ones(len(points))
                        indices_label = [fold[i] for i,x
                                         in enumerate(fold_labels)
                                         if x == label]
                        if len(indices_label):
                            label_array[indices_label] = 1

                            # Propagate from seeds to vertices with label:
                            #indices_seeds = []
                            #for seed in range(int(max(seeds))+1):
                            #    indices_seeds.append([i for i,x
                            #                          in enumerate(seeds)
                            #                          if x == seed])
                            #sulci2 = segment(indices_label, neighbor_lists,
                            #                 50, indices_seeds, False, True,
                            #                 labels)
                            sulci2 = propagate(points, faces,
                                               label_array, seeds, sulci,
                                               max_iters=10000,
                                               tol=0.001, sigma=5)
                            sulci[sulci2 != -1] = sulci2[sulci2 != -1]

    sulcus_numbers = [int(x) for x in np.unique(sulci) if x != -1]
                      # if not np.isnan(x)]
    n_sulci = len(sulcus_numbers)

    #-------------------------------------------------------------------------
    # Print statements
    #-------------------------------------------------------------------------
    if verbose:
        print("Extracted {0} sulci from {1} folds ({2:.1f}s):".
                  format(n_sulci, n_folds, time()-t0))
        if sulcus_names:
            for sulcus_number in sulcus_numbers:
                print("  {0}: {1}".format(sulcus_number,
                                          sulcus_names[sulcus_number]))
        elif sulcus_numbers:
            print("  " + ", ".join([str(x) for x in sulcus_numbers]))

        unresolved = [i for i in range(len(pair_lists))
                      if i not in sulcus_numbers]
        if len(unresolved) == 1:
            print("The following sulcus is unaccounted for:")
        else:
            print("The following {0} sulci are unaccounted for:".
                  format(len(unresolved)))
        if sulcus_names:
            for sulcus_number in unresolved:
                print("  {0}: {1}".format(sulcus_number,
                                          sulcus_names[sulcus_number]))
        else:
            print("  " + ", ".join([str(x) for x in unresolved]))

    #-------------------------------------------------------------------------
    # Return sulci, number of sulci, and file name
    #-------------------------------------------------------------------------
    sulci = [int(x) for x in sulci]
    sulci_file = os.path.join(os.getcwd(), 'sulci.vtk')
    rewrite_scalars(labels_file, sulci_file, sulci, 'sulci', sulci)

    if not os.path.exists(sulci_file):
        raise IOError(sulci_file + " not found")

    return sulci, n_sulci, sulci_file
Esempio n. 11
0
def concatenate_sulcus_scalars(scalar_files, fold_files, label_files):
    """
    Prepare data for estimating scalar distributions along and outside fundi.

    Extract (e.g., depth, curvature) scalar values in folds, along sulcus
    label boundaries as well as outside the sulcus label boundaries.
    Concatenate these scalar values across multiple files.

    Parameters
    ----------
    scalar_files : list of strings
        names of surface mesh VTK files with scalar values to concatenate
    fold_files : list of strings (corr. to each list in scalar_files)
        VTK files with fold numbers as scalars (-1 for non-fold vertices)
    label_files : list of strings (corr. to fold_files)
        VTK files with label numbers (-1 for unlabeled vertices)

    Returns
    -------
    border_scalars : list of floats
        concatenated scalar values within folds along sulcus label boundaries
    nonborder_scalars : list of floats
        concatenated scalar values within folds outside sulcus label boundaries

    Examples
    --------
    >>> # Concatenate (duplicate) depth scalars:
    >>> import os
    >>> from mindboggle.shapes.likelihood import concatenate_sulcus_scalars
    >>> path = os.environ['MINDBOGGLE_DATA']
    >>> depth_file = os.path.join(path, 'arno', 'shapes', 'depth_rescaled.vtk')
    >>> folds_file = os.path.join(path, 'arno', 'features', 'folds.vtk')
    >>> labels_file = os.path.join(path, 'arno', 'labels', 'lh.labels.DKT25.manual.vtk')
    >>> scalar_files = [depth_file, depth_file]
    >>> fold_files = [folds_file, folds_file]
    >>> label_files = [labels_file, labels_file]
    >>> #
    >>> S = concatenate_sulcus_scalars(scalar_files, fold_files, label_files)

    """
    import numpy as np

    from mindboggle.mio.vtks import read_scalars
    from mindboggle.guts.mesh import find_neighbors_from_file
    from mindboggle.guts.segment import extract_borders
    from mindboggle.mio.labels import DKTprotocol

    dkt = DKTprotocol()

    # Prepare (non-unique) list of sulcus label pairs:
    protocol_label_pairs = [
        x for lst in dkt.sulcus_label_pair_lists for x in lst
    ]

    border_scalars = []
    nonborder_scalars = []

    # Loop through files with the scalar values:
    for ifile, scalar_file in enumerate(scalar_files):
        print(scalar_file)

        # Load scalars, folds, and labels:
        folds_file = fold_files[ifile]
        labels_file = label_files[ifile]
        scalars, name = read_scalars(scalar_file, True, True)
        if scalars.shape:
            folds, name = read_scalars(folds_file)
            labels, name = read_scalars(labels_file)
            indices_folds = [i for i, x in enumerate(folds) if x != -1]
            neighbor_lists = find_neighbors_from_file(labels_file)

            # Find all label border pairs within the folds:
            indices_label_pairs, label_pairs, unique_pairs = extract_borders(
                indices_folds,
                labels,
                neighbor_lists,
                ignore_values=[-1],
                return_label_pairs=True)
            indices_label_pairs = np.array(indices_label_pairs)

            # Find vertices with label pairs in the sulcus labeling protocol:
            Ipairs_in_protocol = [
                i for i, x in enumerate(label_pairs)
                if x in protocol_label_pairs
            ]
            indices_label_pairs = indices_label_pairs[Ipairs_in_protocol]
            indices_outside_pairs = list(
                frozenset(indices_folds).difference(indices_label_pairs))

            # Store scalar values in folds along label border pairs:
            border_scalars.extend(scalars[indices_label_pairs].tolist())

            # Store scalar values in folds outside label border pairs:
            nonborder_scalars.extend(scalars[indices_outside_pairs].tolist())

    return border_scalars, nonborder_scalars