Exemple #1
0
def computeLabelLayers(labelFile, surfaceAdjacency, borderFile):
    """
    Method to find level structures of vertices, where each structure is a set
    of vertices that are a distance k away from the border vertices.
    """

    label = loaded.load(labelFile, 0)
    surfAdj = loaded.load(surfaceAdjacency)
    borders = loaded.load(borderFile)

    # get set of non-zero labels in label file
    L = set(label) - set([0])

    layers = {}.fromkeys(L)

    """
    fullList = Parallel(n_jobs=NUM_CORES)(delayed(labelLayers)(lab,
                        np.where(label == lab)[0],
                        surfAdj,borders[lab]) for lab in L)
    """

    for i, labelValue in enumerate(L):
        if labelValue in borders:

            inds = np.where(label == labelValue)[0]
            bm = borders[labelValue]

            layers[labelValue] = labelLayers(labelValue, inds, surfAdj, bm)

    return layers
Exemple #2
0
def main(args):

    components = loaded.load(args.components)
    rest = loaded.load(args.rest)

    if args.mask:
        mask = loaded.load(args.mask)
        components = components[np.where(mask)]
        rest = rest[np.where(mask), :]
        rest = rest.squeeze()

    # instantiate dual regressor
    Regressor = dr.Regressor(standardize=args.standardize,
                             hdr_alpha=args.alpha,
                             tr=args.rep_time,
                             s_filter=args.filter)

    # fit spatial and temporal regression components
    Regressor.fit(rest, components)

    temporal = {'temporal': Regressor.temporal_}
    spatial = {'spatial': Regressor.spatial_}

    if args.mask:
        temp = np.zeros((mask.shape[0], spatial['spatial'].shape[1]))
        temp[np.where(mask), :] = spatial['spatial']
        spatial['spatial'] = temp

    sio.savemat(file_name='.'.join([args.output, 'Temporal.mat']),
                mdict=temporal)
    sio.savemat(file_name='.'.join([args.output, 'Spatial.mat']),
                mdict=spatial)
Exemple #3
0
def regionalizeStructures(timeSeries, levelStructures, midlines, level, R,
    measure='median'):

    """
    Method to regionalize the resting state connectivity, using only vertices
    included at a minimum level away from the border vertices.
    Parameters:
    - - - - -
        timeSeries : input resting state file
        levelStrucutres : levelStructures created by computeLabelLayers
                            ".RegionalLayers.p" file
        level : depth to constaint layers at
        midlines : path to midline indices
        measure : measure to apply to correlation values ['mean','median']
    """

    assert measure in ['median', 'mean']
    assert level >= 1

    resting = loaded.load(timeSeries)
    midlines = loaded.load(midlines)
    levelSets = loaded.load(levelStructures)

    resting[midlines, :] = 0

    condensedLevels = layerCondensation(levelSets, level)

    regionalized = np.zeros((resting.shape[0], R))

    # get the central vertices for region_id
    for rid in condensedLevels.keys():

        subregion = condensedLevels[rid]
        subregion = list(set(subregion).difference(set(midlines)))
        print('# subvertices: {:}'.format(len(subregion)))

        # if subregion has at least 1 vertex
        if len(subregion):

            subrest = resting[subregion, :]

            if np.ndim(subrest) == 1:
                subrest.shape += (1,)

            if subrest.shape[1] != resting.shape[1]:
                subrest = subrest.T

            corr = metrics.pairwise.pairwise_distances(
                resting, subrest, metric='correlation')

            if measure == 'median':
                regionalized[:, rid-1] = np.median(1-corr, axis=1)
            else:
                regionalized[:, rid-1] = np.mean(1-corr, axis=1)

    regionalized[midlines, :] = 0

    return regionalized
Exemple #4
0
def prep_data(subject_id, sreg, treg, region_map, dir_func, dir_dist,
              hemisphere, nsize):
    """
    Source and target distance and correlation data for modeling.

    Parameters:
    - - - - -
    subject_id: string
        Subject name
    sreg, treg: string
        source, target region pair
    region_map: dictioary
        mapping from region name to cortical indices
    dir_func: string
        Directory where Nearest Neighbor correlation maps exist
    dir_dist: string:
        Directory where Nearest Neighbor distance maps exist
    hemisphere: string
        Hemisphere to process, in ['L', 'R']
    nsize: int
        neighborhood size

    Returns:
    - - - -
    inds: int, array
        indices of source voxels
    x: float, array
        dispersion vector
    y: float, array
        correlation vector
    """

    base_knn = '%s.%s.knn_mean.2.%s.func.gii' % (subject_id, hemisphere, treg)
    in_knn = '%s%s/%s' % (dir_func, subject_id, base_knn)
    knn = loaded.load(in_knn)

    base_dist = '%s.%s.Distance.2.%s.func.gii' % (subject_id, hemisphere, treg)
    in_dist = '%s%s/%s' % (dir_dist, subject_id, base_dist)
    dist = loaded.load(in_dist)

    source_indices = region_map[sreg]
    nsize = nsize - 1

    x = dist[source_indices, nsize]
    y = knn[source_indices, nsize]

    inds = np.arange(len(source_indices))[~np.isnan(y)]
    inds = inds[y[inds] != 0]

    x = x[inds]
    y = y[inds]

    return [inds, x, y]
Exemple #5
0
    def _raw_fit(self, input_files):
        """

        :param input_files:
        :return:
        """

        W = []
        for temp_file in input_files[:self.s_init]:

            temp_matrix = loaded.load(temp_file)
            nans = np.isnan(temp_matrix).sum()
            infs = np.isinf(temp_matrix).sum()

            if nans > 0 or infs > 0:
                print('%s has %i NANs and %i INFs' % (temp_file, nans, infs))
                continue
            else:
                W.append(self._merge_and_reduce(temp_matrix))

        # Compute initial estimate of spatial eigenvectors
        print('Computing initial estimate for {:} subjects.'.format(
            self.s_init))
        W = np.row_stack(W).squeeze()
        print('Initialization matrix: {:}'.format(W.shape))
        W = self._estimate(W)

        print('Initial estimate shape: {:}'.format(W.shape))

        for k, temp_file in enumerate(input_files[self.s_init:]):

            print('Adding file # %i' % (k + 1 + self.s_init))

            temp_matrix = loaded.load(temp_file)
            nans = np.isnan(temp_matrix).sum()
            infs = np.isinf(temp_matrix).sum()

            if nans > 0 or infs > 0:
                print('%s has %i NANs and %i INFs' % (temp_file, nans, infs))
                continue
            else:
                print('Adding file: %s' % (temp_file))
                update_data = self._merge_and_reduce(temp_matrix)
                W = np.row_stack([W, update_data])

                temporal, variance, spatial = randomized_svd(W,
                                                             self.m_eigen,
                                                             n_iter=3)
                W = np.dot(np.diag(variance), spatial)

        self.components_ = W[0:self.n_components, :]
Exemple #6
0
def populationRegionSize(subjectList, labelDirectory, extension):
    """
    Method to compute the sizes of each region across the training set.

    Parameters:
    - - - - -
        subjectList : (list,.txt) list of subjects to include
        labelDirectory : directory containing the label files
        extension : label file extension
    """

    if isinstance(subjectList, str):
        with open(subjectList, 'r') as inFile:
            subjects = inFile.readlines()
        subjects = [x.strip() for x in subjects]
    elif isinstance(subjectList, list):
        subjects = subjectList
    else:
        print('Incorrect subject list type.')

    labelSizes = {k: [] for k in np.arange(1, 181)}

    for subj in subjects:

        inLabel = labelDirectory + str(subj) + extension

        if os.path.isfile(inLabel):
            pred = loaded.load(inLabel)

            for k in labelSizes.keys():
                if k in set(pred):
                    labelSizes[k].append(len(np.where(pred == k)[0]))

    return labelSizes
Exemple #7
0
    def _merge_and_reduce(self, input_files):

        """

        Clean, temporally reduce, and concatenate resting state matrix files.

        :param input_files: list of input resting state matrix files
        :return signals: concatenated resting state arrays
        """

        signals = []

        for inp in input_files:

            print('Loading {:}'.format(inp.split('/')[-1]))

            matrix = loaded.load(inp)

            try:
                z
            except NameError:
                z = np.zeros((matrix.shape[0],))
            else:
                pass
            finally:
                zinds = np.where(np.abs(matrix).sum(1) == 0)[0]
                if len(zinds) > 3000:
                    pass
                else:
                    z[zinds] += 1

                    matrix = clean(matrix,standardize=self.standardize,
                                low_pass=self.low_pass,high_pass=self.high_pass,
                                t_r=self.t_r)

                    if self.pca_filter:
                        matrix = self._reduce(matrix)

                    signals.append(matrix)

        self.mask = z.astype(np.bool)
        print(self.mask.sum())
        print(len(signals))
        signals = np.column_stack(signals)
        signals = signals[~self.mask, :]
        
        print(signals.shape)

        return signals.T
Exemple #8
0
def func2mat(in_func, out_mat):
    """
    Method to quickly convert between Matlab .mat and Gifti .func.gii files.

    Parameters:
    - - - - -
    in_func: str
        Gifti .func.gii or .label.gii file to convert
    out_mat: str
        Matlab .mat file to generate
    """

    func = loaded.load(in_func)
    func = func.squeeze()

    mat = {'data': func}
    sio.savemat(out_mat, mat)
Exemple #9
0
def mat2func(in_mat, out_func, hemisphere):
    """
    Method to quickly convert between Matlab .mat and Gifti .func.gii files.
    Generally, the Matlab file will be a 1-dimensional array.  If it is not,
    each column (dimension) will be saved as a DataArray object in the
    Gifti file.

    Parameters:
    - - - - -
    in_mat: str
        Matlab .mat file to convert
    out_func: str
        Gifti .func.gii to create
    """

    mat = loaded.load(in_mat)
    mat = mat.squeeze()

    if mat.ndim > 1:
        mat = mat.T.tolist()

    write.save(mat, out_func, hemisphere)
Exemple #10
0
    def _merge_and_reduce(self, input_file):
        """
        Clean, temporally reduce, and concatenate resting state matrix files.

        :param input_files: list of input resting state matrix files
        :return signals: concatenated resting state arrays
        """

        print('Loading {:}'.format(input_file.split('/')[-1]))

        matrix = loaded.load(input_file)
        if matrix.shape[0] < matrix.shape[1]:
            matrix = matrix.T
        matrix = clean(matrix,
                       standardize=self.standardize,
                       low_pass=self.low_pass,
                       high_pass=self.high_pass,
                       t_r=self.t_r)

        if self.pca_filter:
            matrix = self._reduce(matrix)

        return matrix.T
Exemple #11
0
                    default=None)

parser.add_argument('-d',
                    '--dir',
                    help='Output directory.',
                    required=True,
                    type=str)
parser.add_argument('-bo',
                    '--outbase',
                    help='Output base name.',
                    required=True,
                    type=str)

print('Generating region map...')
args = parser.parse_args()
label = loaded.load(args.label)
R = re.Extractor(args.label)
indices = R.indices(R.map_regions(), args.rois)
sort_inds = np.argsort(indices)

print('Loading eta matrix...')
eta = loaded.load(args.eta)
eta[np.isinf(eta)] = 0
eta[np.isnan(eta)] = 0

cmin = args.clusters[0]
cmax = args.clusters[1]

print('Generating adjacency matrix...')
surf = nb.load(args.surface)
vertices = surf.darrays[0].data
parser.add_argument('-d',
                    '--dir',
                    help='Output directory.',
                    required=True,
                    type=str)
parser.add_argument('-bo',
                    '--outbase',
                    help='Output base name.',
                    required=True,
                    type=str)

args = parser.parse_args()

print('Subject: {:}'.format(args.subject))

label = loaded.load(args.label)
R = re.Extractor(args.label)
region_map = R.map_regions()

indices = []
for r in args.rois:
    indices.append(region_map[r])
indices = np.concatenate(indices)

eta = loaded.load(args.eta)
eta[np.isinf(eta)] = 0
eta[np.isnan(eta)] = 0

cmin = args.clusters[0]
cmax = args.clusters[1]
Exemple #13
0
parser.add_argument('-e',
                    '--evecs',
                    help='Number of eigenvalues to compute.',
                    required=False,
                    default=5,
                    type=int)
parser.add_argument('-n',
                    '--normalize',
                    help='Normalize eigenvectors to [0,1].',
                    required=False,
                    default=True,
                    type=bool)

args = parser.parse_args()

clusters = loaded.load(args.cluster)

try:
    assert os.path.isfile(args.similarity)
except:
    raise ('Similarity file does not exist.')
else:
    print('Loading similarity file.')
    sims = h5py.File(name=args.similarity, mode='r')

labels = list(sims.keys())

outEvecs = ''.join([args.dir, args.outbase, '.Evecs.h5'])
evecs = h5py.File(name=outEvecs, mode='a')

z = np.zeros((clusters.shape[0], args.evecs * len(labels)))
Exemple #14
0
                    help='Base output name, without extension.', required=True, type=str)

args = parser.parse_args()

if not os.path.exists(args.dir):
    print('Output directory does not exist -- creating now.')
    os.mkdir(args.dir)

# Load region of interest
try:
    assert os.path.isfile(args.cluster)
except:
    raise('Mask ROI does not exist.')
else:
    print('Loading ROI.')
    cluster = loaded.load(args.cluster)

# Load feature matrix
try:
    assert os.path.isfile(args.features)
except:
    raise('Feature file for {:} does not exist.'.format(args.subject))
else:
    print('Loading feature data.')
    F = loaded.load(args.features)
    n, p = F.shape

    if n < p:
        F = F.T

F = (F-F.mean(1)[:, None]) / (F.std(1)[:, None])
Exemple #15
0
def neighborhoodErrorMap(labVal, labelAdjacency, truthLabFile,
                         predLabFile, labelLookup, outputColorMap):
    """
    Method to visualize the results of a prediction map, focusing in on a
    spefic core label.

    Parameters:
    - - - - -
        core : region of interest
        labelAdjacency : label adjacency list
        truthLabFile : ground truth label file
        predLabFile : predicted label file
        labelLookup : label color lookup table
        outputColorMap : new color map for label files
    """

    # load files
    labAdj = loaded.load(labelAdjacency)
    truth = loaded.load(truthLabFile, 0)
    pred = loaded.load(predLabFile, 0)

    # extract current colors from colormap
    parsedColors = parseColorLookUpFile(labelLookup)

    # initialize new color map file
    color_file = open(outputColorMap, "w")

    trueColors = ' '.join(map(str, [255, 255, 255]))
    trueName = 'Label {}'.format(labVal)
    trueRGBA = '{} {} {}\n'.format(labVal, trueColors, 255)

    trueStr = '\n'.join([trueName, trueRGBA])
    color_file.writelines(trueStr)

    # get labels that neighbor core
    neighbors = labAdj[labVal]
    # get indices of core label in true map
    truthInds = np.where(truth == labVal)[0]

    # initialize new map
    visualizeMap = np.zeros((truth.shape))
    visualizeMap[truthInds] = labVal

    # get predicted label values existing at truthInds
    predLabelsTruth = pred[truthInds]

    for n in neighbors:

        # get color code for label, adjust and write text to file
        oriName = 'Label {}'.format(n)
        oriCode = parsedColors[n]
        oriColors = ' '.join(map(str, oriCode))
        oriRGBA = '{} {} {}\n'.format(n, oriColors, 255)
        oriStr = '\n'.join([oriName, oriRGBA])

        color_file.writelines(oriStr)

        adjLabel = n+180
        adjName = 'Label {}'.format(adjLabel)
        adjColors = shiftColor(oriCode, mag=30)
        adjColors = ' '.join(map(str, adjColors))
        adjRGBA = '{} {} {}\n'.format(adjLabel, adjColors, 255)
        adjStr = '\n'.join([adjName, adjRGBA])

        color_file.writelines(adjStr)

        # find where true map == n and set this value
        n_inds = np.where(truth == n)[0]
        visualizeMap[n_inds] = n

        # find where prediction(core) == n, and set to adjusted value
        n_inds = np.where(predLabelsTruth == n)[0]
        visualizeMap[truthInds[n_inds]] = adjLabel

    color_file.close()

    return visualizeMap
Exemple #16
0
parser.add_argument('-bo', '--base_out', help='Base output name, without extension.', required=True, type=str)

args = parser.parse_args()

if not os.path.exists(args.dir):
    print('Output directory does not exist -- creating now.')
    os.mkdir(args.dir)

# Load region of interest
try:
    assert os.path.isfile(args.roi)
except:
    raise('Mask ROI does not exist.')
else:
    print('Loading ROI.')
    roi = loaded.load(args.roi)
    roi = (roi>0)

# Load target region
if args.target:
    try:
        assert os.path.isfile(args.target)
    except:
        raise('Target mask does not exist.')
    else:
        print('Loading target.')
        target_exists = True
        target = loaded.load(args.target)
        target = (target>0)
else:
    target_exists = False
Exemple #17
0
    print('Computing similarity matrix.')
    E2 = conmap.eta2(R)

    return [E2, R]


if not os.path.exists(args.dir):
    print('Output directory does not exist -- creating now.')
    os.mkdir(args.dir)

# Load region of interest
if not os.path.isfile(args.label):
    raise FileExistsError('Label file does not exist.')
else:
    label = loaded.load(args.label)
    R = re.Extractor(args.label)
    index_map = R.map_regions()

# get source and target indices
sinds = index_map[args.sroi]
if args.troi:
    tinds = index_map[args.troi]
else:
    tinds = list(set(np.arange(label.shape[0])).difference(set(sinds)))

# Load feature matrix
if not os.path.isfile(args.features):
    raise FileExistsError('Features file does not exist.')
else:
    print('Loading feature data.')
Exemple #18
0
try:
    assert os.path.isfile(args.label)
except:
    raise ('Label file does not exist.')
else:
    R = re.Extractor(args.label)
    regions = R.map_regions()
    inds = R.indices(regions, args.sroi)

try:
    assert os.path.isfile(args.similarity)
except:
    raise ('Similarity matrix does not exist.')
else:
    sim = loaded.load(args.similarity)

sim[np.isnan(sim)] = 0
sim[np.isinf(sim)] = 0

row_sums = np.where(np.abs(sim).sum(1) != 0)[0]
col_sums = np.where(np.abs(sim).sum(0) != 0)[0]

assert np.all(row_sums == col_sums)
sim = sim[:, row_sums][col_sums, :]
inds = inds[row_sums]

if not os.path.exists(outEvecs):

    print('Computing distance matrix.')
    distance = conmap.norm(sim)
Exemple #19
0
def connectopyBy(subject_id, hemisphere, labeldir, conndir, nsize=5):
    """
    Generate DataFrame objects for each source region.  Contains the connectopy
    data for the first two eigenvectors of the source region, along with the 
    pairwise correlation / dispersion data.

    Parameters:
    - - - - -
    subject_id: string
        Subject name
    hemisphere: string
        Hemisphere to process ['L', 'R']
    labeldir: string
        Directory where label files exist
    conndir: string
        Directory where connectopy maps exist
    nsize: int
        Mapping neighborhood size
    """

    dir_out = '%sPlots/%s/' % (conndir, subject_id)
    dir_evec = '%sDesikan/%s/' % (conndir, subject_id)
    dir_func = '%sNeighborFunctional/%s/' % (conndir, subject_id)
    dir_disp = '%sNeighborDistances/%s/' % (conndir, subject_id)

    label_file = '%s%s.%s.aparc.32k_fs_LR.label.gii' % (labeldir, subject_id,
                                                        hemisphere)
    R = re.Extractor(label_file)
    region_map = R.map_regions()

    regions = list(region_map.keys())
    regions.sort()
    regions = [x for x in regions if x not in ['corpuscallosum']]

    col_names = ['e1', 'e2'] + regions

    for i, source_region in enumerate(regions):

        func_df = pd.DataFrame(columns=col_names, index=None)
        disp_df = pd.DataFrame(columns=col_names, index=None)

        sinds = region_map[source_region]

        evec_file = '%s%s.%s.%s.2.brain.Evecs.func.gii' % (
            dir_evec, subject_id, hemisphere, source_region)

        evecs = loaded.load(evec_file)
        func_df['e1'] = evecs[sinds, 0]
        func_df['e2'] = evecs[sinds, 1]
        disp_df['e1'] = evecs[sinds, 0]
        disp_df['e2'] = evecs[sinds, 1]

        for j, target_region in enumerate(regions):

            if source_region != target_region:

                func_file = '%s%s.%s.%s.2.%s.mean_knn.func.gii' % (
                    dir_func, subject_id, hemisphere, source_region,
                    target_region)

                func = loaded.load(func_file)
                func = func[sinds, nsize - 1]
                func_df[target_region] = func

                disp_file = '%s%s.%s.Distance.2.%s.func.gii' % (
                    dir_disp, subject_id, hemisphere, target_region)

                disp = loaded.load(disp_file)
                disp = disp[sinds, nsize - 1]
                disp_df[target_region] = disp

            else:
                disp_df[target_region] = np.nan
                func_df[target_region] = np.nan

        out_func = '%s%s.%s.%s.Functional.csv' % (dir_out, subject_id,
                                                  hemisphere, source_region)
        out_dist = '%s%s.%s.%s.Dispersion.csv' % (dir_out, subject_id,
                                                  hemisphere, source_region)
        func_df.to_csv(out_func, index=False)
        disp_df.to_csv(out_dist, index=False)
Exemple #20
0
                    help='Downsample the number of files.',
                    required=False,
                    type=int,
                    default=None)

args = parser.parse_args()

with open(args.file_list, 'r') as f:
    files = f.read().split()
np.random.shuffle(files)

if args.size:
    files = files[:args.size]

if args.mask:
    mask = loaded.load(args.mask)

print('Fitting MIGP with {:} components...'.format(args.number_components))
M = migp.MIGP(n_components=args.number_components, 
              low_pass=args.low_pass,
              m_eigen=args.eigens,
              s_init=args.number_subjects,
              t_r=args.rep_time,
              mask=mask)

M.fit(files)
components = M.components_

if args.mask:
    C = np.zeros((mask.shape[0], components.shape[1]))
    C[np.where(mask),:] = components)