Beispiel #1
0
def get_centers(n_cluster, **kwargs):

    conf = {
        'path': '/media/robbis/DATA/fmri/movie_viviana/',
        'filetype': 'masked',
        'fname': 'mat_corr_sub_%s.mat',
        'band': 'alpha',
        'conditions': ['movie', 'scramble', 'rest'],
        'state_res_fname': "clustering_labels_%s_maxk_%s_%s_%s.pyobj",
        'max_k': 15,
        'method': 'speed'
    }

    conditions = conf['conditions']
    conf.update(kwargs)

    centers = []

    _, clustering = get_results(**conf)
    data_fname = os.path.join(conf['path'], conf['band'], conf['filetype'],
                              conf['fname'])

    for c in conditions:

        data_, _ = get_data(data_fname % str(c))
        X = get_subsampler(conf['method']).fit_transform(data_)

        centroids = get_centroids(X, clustering[c][n_cluster - 2])

        centers.append(centroids)

    return centers, conditions
Beispiel #2
0
def demeaned():

    conf = {
        'path': '/media/robbis/DATA/fmri/movie_viviana/',
        'filetype': 'masked',
        'fname': 'mat_corr_sub_%s.mat',
        'conditions': ['movie', 'scramble', 'rest'],
        'state_res_fname': "clustering_labels_%s_maxk_%s_%s_%s.pyobj",
        'max_k': 15,
        'method': 'speed'
    }

    filename = os.path.join(conf['path'], conf['filetype'], conf['fname'])

    for condition in conf["conditions"]:
        data, n_roi = get_data(filename % (str(condition)))

        #data = remove_component(data, method="mean", argument="session")
        data = data - data.mean(1)[:, None, :]

        X, clustering_ = cluster_state(data, range(2, 8), method='variance')
        metrics, k_step, metric_names = calculate_metrics(X, clustering_)

        clustering_ = pickle.load(file(label_fname % (condition.lower()), 'r'))

        centroid_ = get_centroids(X, clustering_[3])  # Five centroids
        centroid_ = centroid_[order[condition], :]  # Similarity reorder

        dict_centroids[condition.lower()] = centroid_
    get_positions(dict_centroids, path)
Beispiel #3
0
def demeaned():
    
    conf = {'path':'/media/robbis/DATA/fmri/movie_viviana/',
              'filetype' : 'masked',
              'fname': 'mat_corr_sub_%s.mat',
              'conditions' : ['movie', 'scramble', 'rest'],
              'state_res_fname' : "clustering_labels_%s_maxk_%s_%s_%s.pyobj",
              'max_k':15,
              'method':'speed'                 
            }
 
    filename = os.path.join(conf['path'], conf['filetype'], conf['fname'])
    
    for condition in conf["conditions"]:
        data, n_roi = get_data(filename % (str(condition)))
        
        
        #data = remove_component(data, method="mean", argument="session")
        data = data - data.mean(1)[:,None,:]

        X, clustering_ = cluster_state(data, range(2,8), method='variance')
        metrics, k_step, metric_names = calculate_metrics(X, clustering_)
        
        
        
        clustering_ = pickle.load(file(label_fname % (condition.lower()), 'r'))
        
        centroid_ = get_centroids(X, clustering_[3]) # Five centroids
        centroid_ = centroid_[order[condition],:] # Similarity reorder
        
        dict_centroids[condition.lower()] = centroid_
    get_positions(dict_centroids, path)
    



    
Beispiel #4
0
def analysis(**kwargs):
    """
    Method to fast analyze states from matrices.
  
    
    Parameters
    ----------
    kwargs : dictionary of several parameters
    
        path : string of the data path
        filetype : string ('masked', 'original')
                if you want to use masked matrix or full rank.
        fname : string. pattern of the mat file in input
            default ("mat_corr_sub_%s.mat")
        conditions : list of string of data conditions. 
                should cope with file pattern specified in fname.
        method : string ('speed', 'variance', 'variance+mean', 'speed+mean')
                Method used to subsample data. Using '+mean' suffix let 
                to select only points that are greater than mean.
        max_k : integer (default = 15).
                The maximum number of cluster to use
        filter : string ('none', 'normalize')
                Method used to normalize and filter data.
        state_res_fname : pattern of the output file 
            (default "clustering_labels_%s_maxk_%s_%s_%s.pyobj")
                File used to save labels after clustering.
    
    
    """
    from mvpa_itab.conn.states.plot import plot_metrics, plot_states_matrices

    configuration = {
        'path': '/media/robbis/DATA/fmri/movie_viviana/',
        'band': 'alpha',
        'filetype': 'masked',
        'fname': 'mat_corr_sub_%s.mat',
        'conditions': ['movie', 'scramble', 'rest'],
        'state_res_fname': "clustering_labels_%s_maxk_%s_%s_%s.pyobj",
        'max_k': 15,
        'method': 'variance+mean',
        'filter': 'none'
    }

    configuration.update(kwargs)

    logger.info(configuration)

    conditions = configuration['conditions']
    max_k = configuration['max_k']
    method = configuration['method']
    filetype = configuration['filetype']

    filter_type = configuration['filter']
    data_filter = get_filter(configuration['filter'])

    result = dict()

    path = os.path.join(configuration['path'], configuration['band'],
                        configuration['filetype'])

    for cond in conditions:

        data_, n_roi = get_data(
            os.path.join(path, "mat_corr_sub_%s.mat" % (str(cond))))

        data_ = data_filter.transform(data_)

        X = get_subsampler(method).fit_transform(data_)

        X, clustering_ = cluster_state(X, range(2, max_k))

        metrics_, k_step, metrics_keys = calculate_metrics(X, clustering_)

        fig = plot_metrics(metrics_, metrics_keys, k_step)

        directory = os.path.join(path, method, filter_type, cond)
        if not os.path.exists(directory):
            os.makedirs(directory)

        # Save results
        state_res_fname = configuration['state_res_fname']
        pickle.dump(
            clustering_,
            file(
                os.path.join(
                    directory,
                    state_res_fname % (cond, str(max_k), method, filetype),
                ), 'w'))

        # Save metrics
        fig_fname = os.path.join(directory, "metrics_k_%s.png" % (str(max_k)))
        fig.savefig(fig_fname)

        #save_data(X, 'metrics', metrics_)
        for i, labels in enumerate(clustering_):
            if not os.path.exists(os.path.join(directory, str(i + 2))):
                os.makedirs(os.path.join(directory, str(i + 2)))
            plot_states_matrices(X,
                                 labels,
                                 save_path=os.path.join(directory, str(i + 2)),
                                 save_name_condition=cond)

        # Save filtered data
        X_fname = os.path.join(directory, "filtered_data.mat")
        save_data(X, 'filtered_data', X_fname)

        result[cond] = {'X': X, 'clustering': clustering_, 'data': data_}

    return result
Beispiel #5
0
import itertools
from sklearn.manifold.mds import MDS

"""
distance_fname = os.path.join('/media/robbis/DATA/fmri/movie_viviana/',
                              'pairwise_distance_all_subj_all_pts_movie.npy')
clustering_ = pickle.load(file('/media/robbis/DATA/fmri/movie_viviana/clustering_labels_2to30k_movie_all_sub_all_pts.obj', 'r'))
distance_matrix = np.load(distance_fname, mmap_mode='r')
"""

path = "/media/robbis/DATA/fmri/movie_viviana/masked/"
conditions = ['rest', 'movie', 'scramble']
filetype = 'masked'

for cond in conditions:
    data_, n_roi = get_data(os.path.join(path,"mat_corr_sub_%s_masked.mat" % (str.upper(cond))))
    
    max_k = 15
    method = 'speed'
    
    X, clustering_ = cluster_state(data_, max_k, method)
    
    metrics_, k_step, metrics_keys = calculate_metrics(X, clustering_)
    pickle.dump(clustering_, file(os.path.join(path, 
                                               "clustering_labels_%s_maxk_%s_%s_%s.pyobj" %(cond,
                                                                                            str(max_k),
                                                                                            method,
                                                                                            filetype), 
                                                   ),
                                  'w'))
    
method = ['speed', 'variance']
conditions = ['movie', 'scramble', 'rest']

X_centers = dict()
prod = itertools.product(filetypes, method, conditions)

for t, m, c in prod:
    path_cluster = "%s/%s/%s" % (t, m, c)
    path_cluster = os.path.join(configuration['path'], path_cluster)
    fname = configuration['state_res_fname'] % (c, str(10), m, t)

    path_file = os.path.join(path_cluster, fname)
    clustering_ = pickle.load(file(path_file, 'r'))

    data_fname = os.path.join(configuration['path'], t, configuration['fname'])
    data_, _ = get_data(data_fname % str.upper(c))
    subj_min_speed, subj_speed = get_subsampling_measure(m)(data_)

    X = data_[subj_min_speed]

    metrics_, k_step, metric_names = calculate_metrics(X, clustering_)

    fig = plot_metrics(metrics_, metric_names, k_step)
    fig_fname = os.path.join(configuration['path'], t, m,
                             m + "_metric_evaluation.png")

    fig.savefig(fig_fname)

    #####################

centers, conditions = get_centers(n_cluster=5,