Exemplo n.º 1
0
def plot_sliding_window(series, width, step):
    '''
    Plot a sliding-window graph
    
    Parameters
    ----------
    series:  time-series, array , 3-D
    width: window width
    step: window step size, in samples. If not provided, window and step size are equal.
    '''

    from nilearn import plotting
    import numpy as np
    from nilearn.connectome import sym_matrix_to_vec
    from nilearn.connectome import ConnectivityMeasure

    cut = sliding_window_2d(series, width, step)
    cut_matrix = np.zeros((cut.shape[0], cut.shape[2], cut.shape[2]))
    correlation_measure = ConnectivityMeasure(kind='correlation')

    for i in range(cut.shape[0]):
        matrix = correlation_measure.fit_transform([cut[i]])[0]
        cut_matrix[i, :, :] = matrix

    vectors = np.zeros(
        (cut_matrix.shape[0], sym_matrix_to_vec(cut_matrix[1]).shape[0]))

    for i in range(cut_matrix.shape[0]):
        vec = sym_matrix_to_vec(cut_matrix[i])
        vectors[i, :] = vec

    ax = np.corrcoef(vectors)
    plotting.plot_matrix(ax, title="width={} step={}".format(width, step))
Exemplo n.º 2
0
def plot_matrix(mat, labels, modules, outfile="", zero_diag=True):
    # plot group-mean matrix
    norm = MidpointNormalize(midpoint=0)

    if zero_diag:
        mat[range(mat.shape[0]), range(mat.shape[0])] = 0

    plotting.plot_matrix(mat,
                         labels=labels.tolist(),
                         auto_fit=True,
                         norm=norm,
                         cmap=ListedColormap(
                             sns.diverging_palette(220, 15, sep=1, n=31)),
                         figure=(10, 10))

    prev = ""
    idx = 0
    for i in range(len(labels)):
        if modules[i] != prev:
            plt.plot([-5, len(labels) + 0.5], [i - 0.5, i - 0.5],
                     linewidth=1,
                     color='gray')
            plt.plot([i - 0.5, i - 0.5], [-5, len(labels) + 0.5],
                     linewidth=1,
                     color='gray')

            idx = idx + 1
            prev = modules[i]

    if outfile:
        figure = plt.gcf()
        figure.savefig(outfile, bbox_inches='tight')
        plt.close(figure)
    else:
        plotting.show()
Exemplo n.º 3
0
def compute_correlation_matrix_from_hypergraph(hypergraph, time_series, delay=0, savefigure=None):
    correlation_matrix = np.zeros(hypergraph.shape)

    if delay == 0:
        k_circular = []
        k_circular.append(0)
    else:
        k_circular = range(-1 * delay, delay + 1, 1)

    hypergraph = hypergraph.astype(bool)

    for i in range(hypergraph.shape[0]):
        print(i)
        for j in range(i + 1, hypergraph.shape[0], 1):
            m = []
            for lag in k_circular:
                time_serie_lagged = np.roll(time_series[:, hypergraph[j, :]], lag)
                m.append(dcor.u_distance_correlation_sqr(time_series[:, hypergraph[i, :]], time_serie_lagged))

            correlation_matrix[i, j] = max(m)
            correlation_matrix[j, i] = correlation_matrix[i, j]

    if savefigure is not None:
        figure = plt.figure(figsize=(6, 6))
        plotting.plot_matrix(correlation_matrix, figure=figure, vmax=1., vmin=0.)
        figure.savefig(savefigure, dpi=200)
        plt.close(figure)

    return correlation_matrix
Exemplo n.º 4
0
def compute_hypergraph_elastic_net(time_series, savefigure=None, alpha=0.1, threshold=0):
    # 1 / (2 * n_samples) * ||y - Xw||^2_2 + alpha * l1_ratio * ||w||_1 + 0.5 * alpha * (1 - l1_ratio) * ||w||^2_2

    clf = ElasticNet(alpha=alpha, l1_ratio=0.25, max_iter=8000, tol=1e-2)

    hypergraph = np.zeros((time_series.shape[1], time_series.shape[1]))

    for i in range(time_series.shape[1]):
        X = time_series.copy()
        Y = X[:, i].copy()
        X[:, i] = 0

        hypergraph[i, :] = clf.fit(X, Y).coef_
        hypergraph[i, np.where(hypergraph[i, :] > threshold)] = 1
        hypergraph[i, np.where(hypergraph[i, :] < -threshold)] = 1
        hypergraph[i, np.where(hypergraph[i, :] != 1)] = 0

    if savefigure is not None:
        figure = plt.figure(figsize=(6, 6))
        plotting.plot_matrix(hypergraph, figure=figure, vmax=1., vmin=-1.)
        figure.savefig(savefigure, dpi=200)
        plt.close(figure)

    print('HyperGraph computing finished')
    return hypergraph
Exemplo n.º 5
0
def run(ts_path, output_path, savefigure, faster=False):
    x = np.loadtxt(ts_path, delimiter=',')
    alphas = np.arange(0.1, 0.8, 0.2)

    output_path = join(output_path, 'hypergraph_parcellation')
    makedir(output_path)

    hypergraph_list = []
    for alpha in alphas:
        alpha = round(alpha, 2)
        print('Computing a HyperGraph with ' + str(alpha) + ' sparse level')
        output_file = join(output_path, '_hypergraph_' + str(alpha) + '.txt')
        if not path.exists(output_file):
            hypergraph = compute_hypergraph_elastic_net(time_series=x, alpha=alpha, savefigure=savefigure + str(alpha) + '.png')
            np.savetxt(output_file, hypergraph, delimiter=',', fmt='%i')
        else:
            hypergraph = np.loadtxt(output_file, delimiter=',')

        hypergraph_list.append(hypergraph)
    np_hypergraph_list = np.asarray(hypergraph_list).astype(np.int8)

    print(np_hypergraph_list.shape)
    median_h = np.median(np_hypergraph_list, axis=0)

    figure = plt.figure(figsize=(6, 6))
    plotting.plot_matrix(median_h, figure=figure, vmax=1., vmin=-1.)
    figure.savefig(savefigure + 'median.png', dpi=200)
    plt.close(figure)

    return hypergraph_list
Exemplo n.º 6
0
def savefig_connectome(connectivity_matrix,
                       outfile,
                       labels=None,
                       title=None,
                       reorder=True):
    """
    Save a connectome figure using nilearn's plot_matrix.
    """
    fake_labels = [str(i) for i in range(1, connectivity_matrix.shape[0] + 1)]
    atlas_labels = labels[1:] if labels is not None else fake_labels

    connectome = connectivity_matrix.copy()
    np.fill_diagonal(connectome, 0)

    fig = plt.figure(figsize=(10, 8))

    plotting.plot_matrix(connectome,
                         figure=fig,
                         labels=atlas_labels,
                         vmin=-0.8,
                         vmax=0.8,
                         reorder=reorder)

    if title is not None:
        fig.suptitle(title)

    plt.savefig(outfile)
Exemplo n.º 7
0
def plot_conn_mat(conn_matrix, label_names, out_path_fig):
    import matplotlib
    matplotlib.use('agg')
    from matplotlib import pyplot as plt
    #from pynets import thresholding
    from nilearn.plotting import plot_matrix

    dpi_resolution = 500

    #conn_matrix = np.array(np.array(thresholding.autofix(conn_matrix)))
    [z_min, z_max] = -np.abs(conn_matrix).max(), np.abs(conn_matrix).max()
    rois_num = conn_matrix.shape[0]
    if rois_num < 100:
        plot_matrix(conn_matrix,
                    figure=(10, 10),
                    labels=label_names,
                    vmax=z_max,
                    vmin=z_min,
                    reorder=True,
                    auto_fit=True,
                    grid=False,
                    colorbar=False)
    else:
        plot_matrix(conn_matrix,
                    figure=(10, 10),
                    vmax=z_max,
                    vmin=z_min,
                    auto_fit=True,
                    grid=False,
                    colorbar=False)
    plt.savefig(out_path_fig, dpi=dpi_resolution)
    plt.close()
    return
def plot_matrices(cov, prec, title):
    """Plot covariance and precision matrices, for a given processing. """
    import matplotlib.pyplot as plt

    prec = prec.copy()  # avoid side effects

    # Put zeros on the diagonal, for graph clarity.
    size = prec.shape[0]
    prec[list(range(size)), list(range(size))] = 0
    span = max(abs(prec.min()), abs(prec.max()))

    # Display covariance matrix
    plotting.plot_matrix(cov,
                         cmap=plotting.cm.bwr,
                         vmin=-1,
                         vmax=1,
                         colorbar=True)
    plt.title("%s / covariance" % title)

    # Display precision matrix
    plotting.plot_matrix(cov,
                         cmap=plotting.cm.bwr,
                         vmin=-span,
                         vmax=span,
                         colorbar=True)
    plt.title("%s / precision" % title)
Exemplo n.º 9
0
def plotMatrix(matrix, plot_path, labels, title, ticks, vmin=-1., vmax=1.):
    """Plot matrix. 
	param matrix: two dimensional array. The matrix to plot
	param plot_path : string. Full path and name of the plotting picture
	param labels: list. The labels 
	param title: string. The title of the plot
	param ticks: list. The ticks of the plot
	vmin: float. Minimum value
	vmax: float. Maximum value
	return: None
	"""
    ticks = list(map(lambda x: x - 0.5, ticks))
    ticks_middle = [(((ticks[i + 1] - ticks[i]) / 2) + ticks[i])
                    for i in range(0,
                                   len(ticks) - 1)]
    fig, ax = plt.subplots()
    fig.set_size_inches(16.5, 9.5)
    ax.xaxis.set_minor_locator(plt.FixedLocator(ticks[1:]))
    ax.yaxis.set_minor_locator(plt.FixedLocator(ticks[1:]))
    ax.grid(color='black', linestyle='-', linewidth=1.2, which='minor')
    plt.title(label=title, fontsize=20)
    plotting.plot_matrix(matrix, colorbar=True, axes=ax, vmin=vmin, vmax=vmax)
    plt.yticks(ticks_middle, list(labels))
    plt.xticks(ticks_middle,
               list(labels),
               rotation=55,
               horizontalalignment='right')
    for item in (ax.get_xticklabels() + ax.get_yticklabels()):
        item.set_color(net_dic.labelToColorDic[item.get_text()])
        item.set_fontsize(14)
    fig.savefig(plot_path)
    plt.close()
Exemplo n.º 10
0
def plot_conn_mat(conn_matrix, labels, out_path_fig):
    """

    :param conn_matrix:
    :param labels:
    :param out_path_fig:
    :return:
    """
    import matplotlib
    matplotlib.use('agg')
    from matplotlib import pyplot as plt
    #from pynets import thresholding
    from nilearn.plotting import plot_matrix

    dpi_resolution = 300

    # conn_matrix = np.array(np.array(thresholding.autofix(conn_matrix)))
    [z_min, z_max] = -np.abs(conn_matrix).max(), np.abs(conn_matrix).max()
    rois_num = conn_matrix.shape[0]
    if rois_num < 100:
        try:
            plot_matrix(conn_matrix, figure=(10, 10), labels=labels, vmax=z_max*0.5, vmin=z_min*0.5, reorder=True,
                        auto_fit=True, grid=False, colorbar=False)
        except RuntimeWarning:
            print('Connectivity matrix too sparse for plotting...')
    else:
        try:
            plot_matrix(conn_matrix, figure=(10, 10), vmax=z_max*0.5, vmin=z_min*0.5, auto_fit=True, grid=False,
                        colorbar=False)
        except RuntimeWarning:
            print('Connectivity matrix too sparse for plotting...')
    plt.savefig(out_path_fig, dpi=dpi_resolution)
    plt.close()
    return
Exemplo n.º 11
0
def plot_corr(time_series, labels):
    '''
    Parameters
    ----------
    time_series : array
        Time series of the signal in each region .
    labels : list
        Labels of all the regions in parcellation.

    Returns
    -------
    None.
    '''
    correlation_measure = ConnectivityMeasure(kind='correlation')
    correlation_matrix = correlation_measure.fit_transform([time_series])[0]

    # Mask the main diagonal for visualization:
    np.fill_diagonal(correlation_matrix, 0)
    # The labels we have start with the background (0), hence we skip the first label
    plotting.plot_matrix(correlation_matrix,
                         figure=(10, 8),
                         labels=labels[1:],
                         vmax=0.8,
                         vmin=-0.8,
                         reorder=True)
Exemplo n.º 12
0
def plot_community_conn_mat(conn_matrix, labels, out_path_fig_comm, community_aff):
    """

    :param conn_matrix:
    :param labels:
    :param out_path_fig_comm:
    :param community_aff:
    :return:
    """
    import matplotlib
    import matplotlib.pyplot as plt
    import matplotlib.patches as patches
    matplotlib.use('agg')
    #from pynets import thresholding
    from nilearn.plotting import plot_matrix

    dpi_resolution = 300

    #conn_matrix = np.array(np.array(thresholding.autofix(conn_matrix)))
    sorting_array = sorted(range(len(community_aff)), key=lambda k: community_aff[k])
    sorted_conn_matrix = conn_matrix[sorting_array, :]
    sorted_conn_matrix = sorted_conn_matrix[:, sorting_array]
    [z_min, z_max] = -np.abs(sorted_conn_matrix).max(), np.abs(sorted_conn_matrix).max()
    rois_num = sorted_conn_matrix.shape[0]
    if rois_num < 100:
        try:
            plot_matrix(conn_matrix, figure=(10, 10), labels=labels, vmax=z_max, vmin=z_min,
                        reorder=False, auto_fit=True, grid=False, colorbar=False)
        except RuntimeWarning:
            print('Connectivity matrix too sparse for plotting...')
    else:
        try:
            plot_matrix(conn_matrix, figure=(10, 10), vmax=z_max, vmin=z_min, auto_fit=True, grid=False, colorbar=False)
        except RuntimeWarning:
            print('Connectivity matrix too sparse for plotting...')

    ax = plt.gca()
    total_size = 0
    for community in np.unique(community_aff):
        size = sum(sorted(community_aff) == community)
        ax.add_patch(patches.Rectangle(
                (total_size, total_size),
                size,
                size,
                fill=False,
                edgecolor='black',
                alpha=None,
                linewidth=1
            )
        )
        total_size += size

    plt.savefig(out_path_fig_comm, dpi=dpi_resolution)
    plt.close()
    return
Exemplo n.º 13
0
def plot_conn_mat(conn_matrix,
                  labels,
                  out_path_fig,
                  cmap,
                  binarized=False,
                  dpi_resolution=300):
    """
    Plot a connectivity matrix.

    Parameters
    ----------
    conn_matrix : array
        NxN matrix.
    labels : list
        List of string labels corresponding to ROI nodes.
    out_path_fig : str
        File path to save the connectivity matrix image as a .png figure.
    """
    import matplotlib

    matplotlib.use("agg")
    from matplotlib import pyplot as plt
    from nilearn.plotting import plot_matrix
    from pynets.core import thresholding
    import matplotlib.ticker as mticker

    conn_matrix = thresholding.standardize(conn_matrix)
    conn_matrix_bin = thresholding.binarize(conn_matrix)
    conn_matrix_plt = np.nan_to_num(np.multiply(conn_matrix, conn_matrix_bin))

    try:
        plot_matrix(
            conn_matrix_plt,
            figure=(10, 10),
            labels=labels,
            vmax=np.percentile(conn_matrix_plt[conn_matrix_plt > 0], 95),
            vmin=np.min(conn_matrix_plt) - 0.000001,
            reorder="average",
            auto_fit=True,
            grid=False,
            colorbar=False,
            cmap=cmap,
        )
    except RuntimeWarning:
        print("Connectivity matrix too sparse for plotting...")

    if len(labels) > 40:
        tick_interval = int(np.around(len(labels) / 40))
    else:
        tick_interval = int(np.around(len(labels)))
    plt.axes().yaxis.set_major_locator(mticker.MultipleLocator(tick_interval))
    plt.axes().xaxis.set_major_locator(mticker.MultipleLocator(tick_interval))
    plt.savefig(out_path_fig, dpi=dpi_resolution)
    plt.close()
    return
Exemplo n.º 14
0
def plot_correlation_matrix(correlation_matrix, atlas_data):
    np.fill_diagonal(correlation_matrix,
                     0)  #mask the main diagonal for visualization:
    labels = np.unique(atlas_data)
    plotting.plot_matrix(correlation_matrix,
                         figure=(10, 8),
                         labels=labels,
                         vmax=1,
                         vmin=-1,
                         reorder=True)
    plotting.show()
def main(argv=None):

    args = get_parser().parse_args(argv)

    output_dir = op.join(args.dset, 'derivatives', args.deriv, args.sub,
                         args.ses)
    os.makedirs(output_dir, exist_ok=True)

    nii_files = glob(
        op.join(args.dset, 'derivatives',
                '3dTproject_denoise_acompcor_csfwm+12mo+0.35mm', args.sub,
                args.ses, '*.nii.gz'))

    for tmp_nii_file in nii_files:
        print(tmp_nii_file)
        imgs = nib.load(tmp_nii_file)

        fsaverage = fetch_surf_fsaverage(mesh='fsaverage5')

        mask = compute_background_mask(imgs)
        surf_lh = surface.vol_to_surf(imgs,
                                      fsaverage.pial_left,
                                      radius=24,
                                      interpolation='nearest',
                                      kind='ball',
                                      n_samples=None,
                                      mask_img=mask)
        surf_rh = surface.vol_to_surf(imgs,
                                      fsaverage.pial_right,
                                      radius=24,
                                      interpolation='nearest',
                                      kind='ball',
                                      n_samples=None,
                                      mask_img=mask)

        time_series = np.transpose(np.vstack((surf_lh, surf_rh)))
        correlation = ConnectivityMeasure(kind='correlation')
        time_series = correlation.fit_transform([time_series])[0]
        plotting.plot_matrix(time_series, figure=(10, 8))
        plt.savefig(
            op.join(
                output_dir, '{0}-correlation_matrix.png'.format(
                    op.basename(tmp_nii_file).split('.')[0])))
        plt.close()
        with open(
                op.join(
                    output_dir, '{0}-correlation_matrix.pkl'.format(
                        op.basename(tmp_nii_file).split('.')[0])), 'wb') as fo:
            pickle.dump(time_series, fo)
Exemplo n.º 16
0
def plotMatrix(matrix, plot_path, labels, title, ticks, vmin, vmax):
    """Plot matrix. 
	param matrix: two dimensional array. The matrix to plot
	param plot_path : string. Full path and name of the plotting picture
    param labels: list. The labels 
    param title: string. The title of the plot
    param ticks: list. The ticks of the plot
    vmin: float. Minimum value
    vmax: float. Maximum value
	return: None
    """
    ticks = list(map(lambda x: x - 0.5, ticks))
    labelToColorDic = {
        "Uncertain": "olive",
        "SSH": "cyan",
        "SSM": "orange",
        "CO": "purple",
        "Auditory": "m",
        "DMN": "red",
        "Memory": "grey",
        "Visual": "blue",
        "FP": "gold",
        "Salience": "black",
        "Subcortical": "brown",
        "VAN": "teal",
        "DAN": "green",
        "Cerebellum": "purple"
    }
    ticks_middle = [(((ticks[i + 1] - ticks[i]) / 2) + ticks[i])
                    for i in range(0,
                                   len(ticks) - 1)]
    fig, ax = plt.subplots()
    fig.set_size_inches(16.5, 9.5)
    plt.yticks(ticks_middle, labels)
    plt.xticks(ticks_middle, labels, rotation=55, horizontalalignment='right')
    ax.xaxis.set_minor_locator(plt.FixedLocator(ticks[1:]))
    ax.yaxis.set_minor_locator(plt.FixedLocator(ticks[1:]))
    ax.grid(color='black', linestyle='-', linewidth=1.2, which='minor')
    plt.title(label=title, fontsize=20)
    for item in (ax.get_xticklabels() + ax.get_yticklabels()):
        item.set_color(labelToColorDic[item.get_text()])
        item.set_fontsize(14)
    plotting.plot_matrix(matrix,
                         colorbar=True,
                         figure=fig,
                         vmin=vmin,
                         vmax=vmax)
    fig.savefig(plot_path)
    plt.close()
Exemplo n.º 17
0
def plot_cor_matrix(correlation_matrix, title, labels=None):
  ## plot the correlation matrix
  
    
  np.fill_diagonal(correlation_matrix, 0)
  if labels:
    plot_matrix(correlation_matrix, figure=(10, 8), 
              labels=labels,
                       vmax=0.8, vmin=-0.8, reorder=True)
  else:
    plot_matrix(correlation_matrix, figure=(10, 8), 
              labels=range(correlation_matrix.shape[1]),
                       vmax=0.8, vmin=-0.8, reorder=True)
  plt.title(title)
  plt.show()
def plot_conn_matrices(sc_mat, fc_mat):
    fig = plt.figure(figsize=(12, 8))
    ax1 = plt.subplot(1, 2, 1)
    plotting.plot_matrix(sc_mat[0:78, 0:78],
                         colorbar=False,
                         cmap="Reds",
                         axes=ax1)
    ax1.set_title("Structural Connectivity", fontsize=20)
    ax2 = plt.subplot(1, 2, 2)
    plotting.plot_matrix(fc_mat[0:78, 0:78],
                         colorbar=True,
                         cmap="Reds",
                         axes=ax2)
    ax2.set_title("Functional Connectivity", fontsize=20)
    plt.savefig("../../figures/conn_matrices_plot.png")
Exemplo n.º 19
0
def plot_conn_mat(conn_matrix, labels, out_path_fig):
    """
    Plot a connectivity matrix.

    Parameters
    ----------
    conn_matrix : array
        NxN matrix.
    labels : list
        List of string labels corresponding to ROI nodes.
    out_path_fig : str
        File path to save the connectivity matrix image as a .png figure.
    """
    import matplotlib
    matplotlib.use('agg')
    from matplotlib import pyplot as plt
    from nilearn.plotting import plot_matrix

    dpi_resolution = 300
    [z_min, z_max] = -np.abs(conn_matrix).max(), np.abs(conn_matrix).max()
    rois_num = conn_matrix.shape[0]
    if rois_num < 100:
        try:
            plot_matrix(conn_matrix,
                        figure=(10, 10),
                        labels=labels,
                        vmax=z_max * 0.5,
                        vmin=z_min * 0.5,
                        reorder=False,
                        auto_fit=True,
                        grid=False,
                        colorbar=False)
        except RuntimeWarning:
            print('Connectivity matrix too sparse for plotting...')
    else:
        try:
            plot_matrix(conn_matrix,
                        figure=(10, 10),
                        auto_fit=True,
                        vmax=z_max * 0.5,
                        vmin=z_min * 0.5,
                        grid=False,
                        colorbar=False)
        except RuntimeWarning:
            print('Connectivity matrix too sparse for plotting...')
    plt.savefig(out_path_fig, dpi=dpi_resolution)
    plt.close()
    return
def plot_matrices(cov, prec, title):
    """Plot covariance and precision matrices, for a given processing. """

    prec = prec.copy()  # avoid side effects

    # Put zeros on the diagonal, for graph clarity.
    size = prec.shape[0]
    prec[list(range(size)), list(range(size))] = 0
    span = max(abs(prec.min()), abs(prec.max()))

    # Display covariance matrix
    plotting.plot_matrix(cov, cmap=plotting.cm.bwr,
                         vmin=-1, vmax=1, title="%s / covariance" % title)
    # Display precision matrix
    plotting.plot_matrix(cov, cmap=plotting.cm.bwr,
                         vmin=-span, vmax=span, title="%s / precision" % title)
Exemplo n.º 21
0
    def _run_interface(self, runtime):
        fname = self.inputs.fmri_denoised
        entities = parse_file_entities(fname)
        bold_img = nb.load(fname)
        parcellation_file = get_parcellation_file_path(entities['space'])
        masker = NiftiLabelsMasker(labels_img=parcellation_file,
                                   standardize=True)
        time_series = masker.fit_transform(bold_img, confounds=None)

        corr_measure = ConnectivityMeasure(kind='correlation')
        corr_mat = corr_measure.fit_transform([time_series])[0]
        entities['pipeline'] = extract_pipeline_from_path(fname)
        conn_file = join(self.inputs.output_dir,
                         build_path(entities, self.conn_file_pattern, False))
        carpet_plot_file = join(
            self.inputs.output_dir,
            build_path(entities, self.carpet_plot_pattern, False))
        matrix_plot_file = join(
            self.inputs.output_dir,
            build_path(entities, self.matrix_plot_pattern, False))

        make_carpetplot(time_series, carpet_plot_file)
        mplot = plot_matrix(corr_mat, vmin=-1, vmax=1)
        mplot.figure.savefig(matrix_plot_file)

        np.save(conn_file, corr_mat)

        self._results['corr_mat'] = conn_file
        self._results['carpet_plot'] = carpet_plot_file
        self._results['matrix_plot'] = matrix_plot_file

        return runtime
Exemplo n.º 22
0
    def _run_interface(self, runtime):
        fname = self.inputs.fmri_denoised
        bold_img = nb.load(fname)
        masker = NiftiLabelsMasker(labels_img=self.inputs.parcellation,
                                   standardize=True)
        time_series = masker.fit_transform(bold_img, confounds=None)

        corr_measure = ConnectivityMeasure(kind='correlation')
        corr_mat = corr_measure.fit_transform([time_series])[0]
        _, base, _ = split_filename(fname)

        conn_file = f'{self.inputs.output_dir}/{base}_conn_mat.npy'

        carpet_plot_file = join(self.inputs.output_dir,
                                f'{base}_carpet_plot.png')
        matrix_plot_file = join(self.inputs.output_dir,
                                f'{base}_matrix_plot.png')

        create_carpetplot(time_series, carpet_plot_file)
        mplot = plot_matrix(corr_mat, vmin=-1, vmax=1)
        mplot.figure.savefig(matrix_plot_file)

        np.save(conn_file, corr_mat)

        self._results['corr_mat'] = conn_file
        self._results['carpet_plot'] = carpet_plot_file
        self._results['matrix_plot'] = matrix_plot_file

        return runtime
def getConnectome(imgPath=None,
                  atlasPath=None,
                  viewInBrowser=False,
                  displayCovMatrix=False):
    """
    Gets the connectome of a functional MRI scan
    imgPath -> absolute or relative path to the .nii file
    atlasPath -> download path for the reference MSDL atlas
    viewInBrowser (optional, default=False) -> if True, opens up an interactive viewer in the browser
    displayCovMatrix (optional, default=False) -> display the inverse covariance matrix
    Returns a tuple of shape (estimator, atlas)
    """
    # Download the reference atlas
    atlas = datasets.fetch_atlas_msdl(data_dir=atlasPath)
    # Loading atlas image stored in 'maps'
    atlasFilename = atlas['maps']
    # Get the time series for the fMRI scan
    masker = NiftiMapsMasker(maps_img=atlasFilename,
                             standardize=True,
                             memory='nilearn_cache',
                             verbose=5)
    timeSeries = masker.fit_transform(imgPath)
    # Compute the connectome using sparse inverse covariance
    estimator = GraphicalLassoCV()
    estimator.fit(timeSeries)
    if (displayCovMatrix):
        labels = atlas['labels']
        plotting.plot_matrix(estimator.covariance_,
                             labels=labels,
                             figure=(9, 7),
                             vmax=1,
                             vmin=-1,
                             title='Covariance')
        plotting.plot_matrix(estimator.precision_,
                             labels=labels,
                             figure=(9, 7),
                             vmax=1,
                             vmin=-1,
                             title='Inverse covariance (Precision)')
        #covPlot.get_figure().savefig('Covariance.png')
        # precPlot.get_figure().savefig('Inverse Covariance.png')
    if (viewInBrowser):
        coords = atlas.region_coords
        view = plotting.view_connectome(-estimator.precision_, coords, '60.0%')
        #view.save_as_html(file_name='Connectome Test.html')
        view.open_in_browser()
    return (estimator, atlas)
Exemplo n.º 24
0
def plot_matrices(matrices, matrix_kind):
    n_matrices = len(matrices)
    fig = plt.figure(figsize=(n_matrices * 4, 4))
    for n_subject, matrix in enumerate(matrices):
        plt.subplot(1, n_matrices, n_subject + 1)
        matrix = matrix.copy()  # avoid side effects
        # Set diagonal to zero, for better visualization
        np.fill_diagonal(matrix, 0)
        vmax = np.max(np.abs(matrix))
        title = '{0}, subject {1}'.format(matrix_kind, n_subject)
        plotting.plot_matrix(matrix,
                             vmin=-vmax,
                             vmax=vmax,
                             cmap='RdBu_r',
                             title=title,
                             figure=fig,
                             colorbar=False)
    def _run_interface(self, runtime):
        import numpy as np
        from nilearn import plotting
        import matplotlib.pyplot as plt
        import dcor
        from datetime import datetime
        from utils.utils import absmax

        hypergraph = np.loadtxt(self.inputs.hypergraph_path, delimiter=',')
        time_series = np.loadtxt(self.inputs.time_series_path, delimiter=',')

        k_circular = [0] if self.inputs.lag == 0 else range(-1 * self.inputs.lag, self.inputs.lag + 1, 1)

        correlation_matrix = np.zeros(hypergraph.shape)

        threshold = 0.3

        hypergraph[np.where(hypergraph > threshold)] = 1
        hypergraph[np.where(hypergraph != 1)] = 0
        hypergraph = hypergraph.astype(bool)

        then = datetime.now()

        for i in range(hypergraph.shape[0]):
            print(i)
            for j in range(i + 1, hypergraph.shape[0], 1):
                correlation_values_from_laggeds = []
                for lag in k_circular:
                    correlation_values_from_laggeds.append(
                        dcor.u_distance_correlation_sqr(time_series[:, hypergraph[i, :]],
                                                        np.roll(time_series[:, hypergraph[j, :]], lag)))

                    # correlation_matrix[i, j] = dcor.u_distance_correlation_sqr(time_series[:, hypergraph[i, :]],
                    #                                                           time_series[:, hypergraph[j, :]])
                correlation_matrix[i, j] = np.max(correlation_values_from_laggeds)
                # correlation_matrix[j, i] = correlation_matrix[i, j]

        figure = plt.figure(figsize=(6, 6))
        plotting.plot_matrix(correlation_matrix, figure=figure, vmax=1., vmin=0.)
        figure.savefig(self.inputs.correlation_matrix_plot_out_file, dpi=300)

        np.savetxt(self.inputs.correlation_matrix_out_file, correlation_matrix, delimiter=',', fmt='%10.2f')

        print('Total time: ', (datetime.now() - then).total_seconds())
        return runtime
def plot_matrices(cov, prec, title, labels):
    """Plot covariance and precision matrices, for a given processing. """

    prec = prec.copy()  # avoid side effects

    # Put zeros on the diagonal, for graph clarity.
    size = prec.shape[0]
    prec[list(range(size)), list(range(size))] = 0
    span = max(abs(prec.min()), abs(prec.max()))

    # Display covariance matrix
    plotting.plot_matrix(cov, cmap=plotting.cm.bwr,
                         vmin=-1, vmax=1, title="%s / covariance" % title,
                         labels=labels)
    # Display precision matrix
    plotting.plot_matrix(prec, cmap=plotting.cm.bwr,
                         vmin=-span, vmax=span, title="%s / precision" % title,
                         labels=labels)
Exemplo n.º 27
0
    def forward(self, batch):
        if type(batch) == list:  # Data list
            batch = Batch.from_data_list(batch)

        all_x = [
            batch.x.to(self.device),
            batch.adj_statistics.to(self.device),
            batch.raw_adj.to(self.device),
        ]

        edge_index, edge_attr = batch.edge_index.to(
            self.device), batch.edge_attr
        edge_attr = edge_attr.to(
            self.device) if edge_attr is not None else edge_attr
        num_graphs, batch_mask = batch.num_graphs, batch.batch.to(self.device)

        x = torch.cat([op(x) for op, x in zip(self.first_fc, all_x)], dim=-1)

        # domain_x_out = self.first_domain_fc(x.reshape(num_graphs, -1))

        # CNP
        cnp1_out_all, p1_x, p1_ei, p1_ea, p1_batch, p1_loss, p1_assignment = self.cnp1(
            x, edge_index, edge_attr, batch)
        reg = p1_loss.unsqueeze(0)
        # reg = torch.tensor(0.).to(self.device)

        # domain
        # domain_out = self.domain_conv(torch.cat([x for _ in range(self.alpha_dim)], dim=-1),
        #                               self.cnp1.alpha_index, self.cnp1.alpha, batch_mask)
        # domain_out = torch.cat([torch.cat(d, dim=1) for d in domain_out], dim=-1)
        # domain_out = domain_out.reshape(num_graphs, self.in_nodes, self.hidden_dim * self.conv_depth, self.alpha_dim)
        # domain_out = torch.cat([domain_out.max(dim=1)[0], domain_out.mean(dim=1)], dim=-1)  # readout

        p1_x = p1_x.reshape(num_graphs, self.pool1_nodes, self.hidden_dim,
                            self.alpha_dim)
        p1_x = p1_x.max(dim=1)[0]  # max pooling
        self.fc_in = p1_x.reshape(num_graphs, -1)

        fc_out = self.final_fc(self.fc_in)
        self.fc_out = fc_out
        # domain_fc_out = self.domain_fc(domain_out.reshape(num_graphs, -1))
        domain_fc_out = None

        if self.logging_hist:
            self.writer.add_histogram('alpha1',
                                      self.cnp1.alpha.detach().cpu().flatten())
            self.writer.add_histogram('p1_ea', p1_ea.detach().cpu().flatten())
            self.writer.add_histogram('p1_assignment',
                                      p1_assignment.detach().cpu().flatten())
            adj_1 = batch_to_adj(self.cnp1.alpha_index, self.cnp1.alpha, 360,
                                 num_graphs)
            torch.save(adj_1, 'adj_1')
            fig_1 = plot_matrix(adj_1[0, 0].detach().cpu())
            fig_1.show()
            self.writer.add_figure('alpha1', fig_1)

        return fc_out, domain_fc_out, reg
Exemplo n.º 28
0
def plot_conn_mat(conn_matrix, label_names):
    import matplotlib as mpl
    import seaborn as sns
    from matplotlib import colors
    from matplotlib import pyplot as plt
    from nilearn.plotting import plot_matrix
    plt.rcParams['axes.facecolor'] = 'black'
    plt.rcParams['figure.facecolor'] = 'black'

    colors.Normalize(vmin=0, vmax=1)
    clust_pal = mpl.colors.ListedColormap(sns.color_palette("RdBu_r", 40))

    rois_num = conn_matrix.shape[0]
    if rois_num < 100:
        try:
            fig = plot_matrix(conn_matrix, figure=(10, 10), labels=label_names, vmax=1, vmin=0, reorder=True,
                        auto_fit=True, grid=False, colorbar=False, cmap=clust_pal)
        except RuntimeWarning:
            print('Connectivity matrix too sparse for plotting...')
    else:
        try:
            fig = plot_matrix(conn_matrix, figure=(10, 10), vmax=1, vmin=0, auto_fit=True, grid=False,
                        colorbar=False, cmap=clust_pal)
        except RuntimeWarning:
            print('Connectivity matrix too sparse for plotting...')

    cur_axes = plt.gca()
    for spine in cur_axes.axes.spines.values():
        spine.set_visible(False)
    fig.figure.tight_layout(pad=0)
    fig.figure.patch.set_facecolor('black')
    cur_axes.axes.get_xaxis().set_visible(False)
    cur_axes.axes.get_yaxis().set_visible(False)
    cur_axes.axes.get_xaxis().set_ticks([])
    cur_axes.axes.get_yaxis().set_ticks([])
    cur_axes.axes.get_xaxis().set_ticklabels([])
    cur_axes.axes.get_yaxis().set_ticklabels([])
    cur_axes.axes.get_xaxis().set_major_locator(plt.NullLocator())
    cur_axes.axes.get_yaxis().set_major_locator(plt.NullLocator())
    cur_axes.set_frame_on(False)
    cur_axes.margins(0,0, tight=True)
    cur_axes.set_axis_off()

    return fig
Exemplo n.º 29
0
def compute_faster_correlation_matrix_from_hypergraph(hypergraph, time_series, savefigure=None):
    hypergraph_shape = hypergraph.shape
    correlation_matrix = np.zeros(hypergraph_shape)
    hypergraph = hypergraph.astype(bool)

    for i in range(hypergraph_shape[0]):
        print(i)
        for j in range(i + 1, hypergraph_shape[0], 1):
            correlation_matrix[i, j] = dcor.u_distance_correlation_sqr(time_series[:, hypergraph[i, :]],
                                                                 time_series[:, hypergraph[j, :]])
            #correlation_matrix[j, i] = correlation_matrix[i, j]

    if savefigure is not None:
        figure = plt.figure(figsize=(6, 6))
        plotting.plot_matrix(correlation_matrix, figure=figure, vmax=1., vmin=0.)
        figure.savefig(savefigure, dpi=200)
        plt.close(figure)

    return correlation_matrix
Exemplo n.º 30
0
def plot_conn_mat(conn_matrix, labels, out_path_fig, cmap, dpi_resolution=300):
    """
    Plot a connectivity matrix.

    Parameters
    ----------
    conn_matrix : array
        NxN matrix.
    labels : list
        List of string labels corresponding to ROI nodes.
    out_path_fig : str
        File path to save the connectivity matrix image as a .png figure.
    """
    import matplotlib

    matplotlib.use("agg")
    from matplotlib import pyplot as plt
    from nilearn.plotting import plot_matrix
    from pynets.core import thresholding

    conn_matrix_bin = thresholding.binarize(conn_matrix)
    conn_matrix = thresholding.standardize(conn_matrix)
    conn_matrix_plt = np.nan_to_num(np.multiply(conn_matrix, conn_matrix_bin))

    try:
        plot_matrix(
            conn_matrix_plt,
            figure=(10, 10),
            labels=labels,
            vmax=np.abs(np.max(conn_matrix_plt)),
            vmin=-np.abs(np.max(conn_matrix_plt)),
            reorder="average",
            auto_fit=True,
            grid=False,
            colorbar=False,
            cmap=cmap,
        )
    except RuntimeWarning:
        print("Connectivity matrix too sparse for plotting...")
    plt.savefig(out_path_fig, dpi=dpi_resolution)
    plt.close()
    return
Exemplo n.º 31
0
def plot_connectome_mixture(data=None, index=None, metric="correlation", save_as=None, show=False, **kwargs):
   
    # extract time series from all subjects and concatenate them
    mm = data.X[index, :].copy()
    time_series = [np.vstack(mm)]

    # calculate correlation matrices across indexed frames in data 
    connectome_measure = ConnectivityMeasure(kind=metric)
    connectome_measure.fit_transform(time_series)
    connectivity = connectome_measure.mean_
    np.fill_diagonal(connectivity, 0)
    #connectivity[np.abs(connectivity) < 0.2] = 0.0 


    # grab center coordinates for atlas labels
    atlas = kwargs.pop('atlas', data.atlas)
    coords = plotting.find_parcellation_cut_coords(labels_img=atlas)

    # assign node colors
    cmap = kwargs.pop('cmap', 'jet')
    node_cmap = plt.get_cmap('bone')
    node_norm = mpl.colors.Normalize(vmin=-0.8, vmax=1.2)
    node_colors = np.ravel([_[-1] for i,_ in enumerate(coords)])
    node_colors = [_ / np.max(node_colors) for _ in node_colors]
    node_colors = node_cmap(node_norm(node_colors))

    # plot connectome matrix
    fig = plt.figure(figsize=(12,5))
    ax = plt.subplot2grid((1, 2), (0, 1),  rowspan=1, colspan=1) 
    display = plotting.plot_matrix(
        connectivity,
        vmin=-.5, vmax=.5, colorbar=True, cmap=cmap,
        axes=ax, #title='{} Matrix'.format(metric.title()),
        )

    # plot connectome with 99.7% edge strength in the connectivity
    ax = plt.subplot2grid((1, 2), (0, 0), rowspan=1, colspan=1)
    display = plotting.plot_connectome(
        connectivity, coords,
        edge_threshold="99.9%", display_mode='z',
        node_color=node_colors, node_size=20, edge_kwargs=dict(lw=4),
        edge_vmin=-.8, edge_vmax=.8, edge_cmap=cmap,
        colorbar=False, black_bg=not True, alpha=0.5,
        annotate=False,
        axes=ax,
        )
    if show is True:
        plt.show()
    plt.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95)
    if save_as:
        fig.savefig(save_as, transparent=True)#, facecolor='slategray', edgecolor='white')
    plt.close(fig)
    return display
# `time_series` is now a 2D matrix, of shape (number of time points x
# number of regions)
print(time_series.shape)

############################################################################
# Build and display a correlation matrix
# ---------------------------------------
from nilearn.connectome import ConnectivityMeasure
correlation_measure = ConnectivityMeasure(kind='correlation')
correlation_matrix = correlation_measure.fit_transform([time_series])[0]

# Display the correlation matrix
import numpy as np
from nilearn import plotting
# Mask out the major diagonal
np.fill_diagonal(correlation_matrix, 0)
plotting.plot_matrix(correlation_matrix, labels=labels, colorbar=True,
                     vmax=0.8, vmin=-0.8)
############################################################################
# And now display the corresponding graph
# ----------------------------------------
from nilearn import plotting
coords = atlas.region_coords

# We threshold to keep only the 20% of edges with the highest value
# because the graph is very dense
plotting.plot_connectome(correlation_matrix, coords,
                         edge_threshold="80%", colorbar=True)

plotting.show()
Exemplo n.º 33
0
##############################################################################
# Compute and display a correlation matrix
# -----------------------------------------
from nilearn.connectome import ConnectivityMeasure
correlation_measure = ConnectivityMeasure(kind='correlation')
correlation_matrix = correlation_measure.fit_transform([time_series])[0]

# Plot the correlation matrix
import numpy as np
from nilearn import plotting
# Make a large figure
# Mask the main diagonal for visualization:
np.fill_diagonal(correlation_matrix, 0)
# The labels we have start with the background (0), hence we skip the
# first label
plotting.plot_matrix(correlation_matrix, figure=(10, 8), labels=labels[1:],
                     vmax=0.8, vmin=-0.8)

###############################################################################
# Same thing without confounds, to stress the importance of confounds
# --------------------------------------------------------------------

time_series = masker.fit_transform(fmri_filenames)
# Note how we did not specify confounds above. This is bad!

correlation_matrix = correlation_measure.fit_transform([time_series])[0]

# Mask the main diagonal for visualization:
np.fill_diagonal(correlation_matrix, 0)

plotting.plot_matrix(correlation_matrix, figure=(10, 8), labels=labels[1:],
                     vmax=0.8, vmin=-0.8, title='No confounds')
Exemplo n.º 34
0
# ----------------------------------------
from matplotlib import pyplot as plt
plt.figure(figsize=(4, 3))
plt.boxplot([cv_scores_ova, cv_scores_ovo])
plt.xticks([1, 2], ['One vs All', 'One vs One'])
plt.title('Prediction: accuracy score')

##############################################################################
# Plot a confusion matrix
# ------------------------
# We fit on the the first 10 sessions and plot a confusion matrix on the
# last 2 sessions
from sklearn.metrics import confusion_matrix
from nilearn.plotting import plot_matrix

svc_ovo.fit(X[session < 10], y[session < 10])
y_pred_ovo = svc_ovo.predict(X[session >= 10])

plot_matrix(confusion_matrix(y_pred_ovo, y[session >= 10]),
            labels=unique_conditions,
            title='Confusion matrix: One vs One', cmap='hot_r')

svc_ova.fit(X[session < 10], y[session < 10])
y_pred_ova = svc_ova.predict(X[session >= 10])

plot_matrix(confusion_matrix(y_pred_ova, y[session >= 10]),
            labels=unique_conditions,
            title='Confusion matrix: One vs All', cmap='hot_r')

plt.show()
covariance_estimator.fit(timeseries)

###############################################################################
# and get the ROI-to-ROI covariance matrix.
matrix = covariance_estimator.covariance_
print('Covariance matrix has shape {0}.'.format(matrix.shape))

###############################################################################
# Plot matrix and graph
# ---------------------
#
# We use nilearn.plotting.plot_matrix to visualize our correlation matrix
# and display the graph of connections with `nilearn.plotting.plot_connectome`.
from nilearn import plotting

plotting.plot_matrix(matrix, vmin=-1., vmax=1., colorbar=True,
                     title='Power correlation matrix')

# Tweak edge_threshold to keep only the strongest connections.
plotting.plot_connectome(matrix, coords, title='Power correlation graph',
                         edge_threshold='99.8%', node_size=20, colorbar=True)

###############################################################################
# Note the 1. on the matrix diagonal: These are the signals variances, set to
# 1. by the `spheres_masker`. Hence the covariance of the signal is a
# correlation matrix

###############################################################################
# Connectome extracted from Dosenbach's atlas
# -------------------------------------------
#
# We repeat the same steps for Dosenbach's atlas.
Exemplo n.º 36
0
# Generate synthetic data
from nilearn._utils.testing import generate_group_sparse_gaussian_graphs

n_subjects = 20  # number of subjects
n_displayed = 3  # number of subjects displayed
subjects, precisions, topology = generate_group_sparse_gaussian_graphs(
    n_subjects=n_subjects, n_features=10, min_n_samples=30, max_n_samples=50,
    density=0.1)

from nilearn import plotting
fig = plt.figure(figsize=(10, 7))
plt.subplots_adjust(hspace=0.4)
for n in range(n_displayed):
    ax = plt.subplot(n_displayed, 4, 4 * n + 1)
    max_precision = precisions[n].max()
    plotting.plot_matrix(precisions[n], vmin=-max_precision,
                         vmax=max_precision, axes=ax, colorbar=False)

    if n == 0:
        plt.title("ground truth")
    plt.ylabel("subject %d" % n)


# Run group-sparse covariance on all subjects
from nilearn.connectome import GroupSparseCovarianceCV
gsc = GroupSparseCovarianceCV(max_iter=50, verbose=1)
gsc.fit(subjects)

for n in range(n_displayed):
    ax = plt.subplot(n_displayed, 4, 4 * n + 2)
    max_precision = gsc.precisions_[..., n].max()
    plotting.plot_matrix(gsc.precisions_[..., n], axes=ax, vmin=-max_precision,
    # saving each subject correlation to correlations
    correlations.append(correlation)

# Mean of all correlations
import numpy as np
mean_correlations = np.mean(correlations, axis=0).reshape(n_regions_extracted,
                                                          n_regions_extracted)

###############################################################################
# Plot resulting connectomes
# ----------------------------

title = 'Correlation between %d regions' % n_regions_extracted

# First plot the matrix
display = plotting.plot_matrix(mean_correlations, vmax=1, vmin=-1,
                               colorbar=True, title=title)

# Then find the center of the regions and plot a connectome
regions_img = regions_extracted_img
coords_connectome = plotting.find_probabilistic_atlas_cut_coords(regions_img)

plotting.plot_connectome(mean_correlations, coords_connectome,
                         edge_threshold='90%', title=title)

################################################################################
# Plot regions extracted for only one specific network
# ----------------------------------------------------

# First, we plot a network of index=4 without region extraction (left plot)
from nilearn import image
# Compute the sparse inverse covariance
# --------------------------------------
from sklearn.covariance import GraphLassoCV
estimator = GraphLassoCV()

estimator.fit(time_series)

##############################################################################
# Display the connectome matrix
# ------------------------------
from nilearn import plotting
# Display the covariance

# The covariance can be found at estimator.covariance_
plotting.plot_matrix(estimator.covariance_, labels=labels,
                     figure=(9, 7), vmax=1, vmin=-1,
                     title='Covariance')

##############################################################################
# And now display the corresponding graph
# ----------------------------------------
coords = atlas.region_coords

plotting.plot_connectome(estimator.covariance_, coords,
                         title='Covariance')


##############################################################################
# Display the sparse inverse covariance
# --------------------------------------
# we negate it to get partial correlations