Ejemplo n.º 1
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("gc_clustering")
    parser.add_argument("other_clustering")
    opts = parser.parse_args()

    gc_clusters = io.read_clusters(opts.gc_clustering)
    other_clusters = io.read_clusters(opts.other_clustering)

    clusters = resolve_clusters(gc_clusters, other_clusters)
    io.output_clusters(clusters, '')
Ejemplo n.º 2
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("network_file", help="Network file used for initial\
                                              clustering")
    parser.add_argument("cluster_file", help="Clustering results file")
    parser.add_argument("-c", "--no_conversion", action="store_true")
    parser.add_argument("-d", "--directed", action="store_true",
                        help="Flag specifying if the input represents\
                              a directed graph. Defaults to false.")
    parser.add_argument("-n", "--node_list", nargs="?",
                        help="Optionally specify a list of the nodes in\
                              the DSD file. Default is all the nodes in the\
                              graph.")
    parser.add_argument("-s", "--simple_conversion", action="store_true")
    opts = parser.parse_args()

    if opts.node_list:
        node_list = io.get_node_list(opts.node_list)
    clusters = io.read_clusters(opts.cluster_file)
    if opts.node_list:
        G = io.build_ig_graph_from_matrix(opts.network_file, False, node_list)
    else:
        G = ig.Graph.Read_Ncol(opts.network_file, directed=opts.directed)

    clusters_to_process, final_clusters = [], []
    for cluster in clusters:
        if len(cluster) > MAX_CL_SIZE:
            clusters_to_process.append(cluster)
        else:
            final_clusters.append(cluster)

    # if all nodes have been clustered, stop looping, otherwise continue to
    # recurse on each large cluster
    step = 1
    while clusters_to_process:
        processing = clusters_to_process
        clusters_to_process = []

        for cluster in processing:
            id_cluster = names_to_ids(G, cluster)
            SG = G.subgraph(cluster)

            cluster_size = len(cluster)
            num_clusters = 2
            '''
            num_clusters = (int(cluster_size / float(100)) if cluster_size > 200
                                                           else 2)
            '''
            clusters = cl.spectral_clustering(SG, num_clusters,
                                              no_conversion=opts.no_conversion,
                                              simple_conversion=opts.simple_conversion)
            for cluster in clusters:
                if len(cluster) > MAX_CL_SIZE:
                    clusters_to_process.append([SG.vs[i]['name'] for i in cluster])
                else:
                    final_clusters.append([SG.vs[i]['name'] for i in cluster])
        step += 1

    io.output_clusters(final_clusters, '')
Ejemplo n.º 3
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("dsd_file",
                        help="Distance (i.e. DSD) matrix for network")
    parser.add_argument("cluster_file", help="Clustering results file")
    parser.add_argument("-n",
                        "--node_list",
                        nargs="?",
                        help="Optionally specify a list of the nodes in\
                              the DSD file. Default is all the nodes in the\
                              graph.")
    opts = parser.parse_args()

    node_list = io.get_node_list(opts.node_list)
    clusters = io.read_clusters(opts.cluster_file)
    G = io.build_ig_graph_from_matrix(opts.dsd_file, False, node_list)

    clusters_to_process, final_clusters = [], []
    for cluster in clusters:
        if len(cluster) > MAX_CL_SIZE:
            clusters_to_process.append(cluster)
        else:
            final_clusters.append(cluster)

    # if all nodes have been clustered, stop looping, otherwise continue to
    # recurse on each large cluster
    step = 1
    while clusters_to_process:
        processing = clusters_to_process
        clusters_to_process = []

        for cluster in processing:
            id_cluster = names_to_ids(G, cluster)
            SG = G.subgraph(cluster)

            cluster_size = len(cluster)
            num_clusters = (int(cluster_size /
                                float(100)) if cluster_size > 200 else 2)
            mat = SG.get_adjacency(attribute='weight')
            dist_matrix = np.array(mat.data)
            del mat
            clusters = cl.spectral_clustering(dist_matrix, num_clusters)
            del dist_matrix
            for cluster in clusters:
                if len(cluster) > MAX_CL_SIZE:
                    clusters_to_process.append(
                        [SG.vs[i]['name'] for i in cluster])
                else:
                    final_clusters.append([SG.vs[i]['name'] for i in cluster])
        step += 1

    io.output_clusters(final_clusters, '')
Ejemplo n.º 4
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("input_file", help="Original clusters input file")
    parser.add_argument("-c",
                        "--cutoff",
                        nargs="?",
                        default=DEFAULT_MIN_SIZE,
                        help="Cutoff for filtering cluster size")
    opts = parser.parse_args()

    clusters = io.read_clusters(opts.input_file)
    filtered_clusters = [c for c in clusters if len(c) >= int(opts.cutoff)]
    io.output_clusters(filtered_clusters, '')
Ejemplo n.º 5
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("gc_file", help='GeneCentric cluster file')
    parser.add_argument("cluster_file",
                        help="File containing cluster filepaths")
    opts = parser.parse_args()

    cluster_nodes = get_cluster_nodes(opts.cluster_file)
    gc_nodes = get_cluster_nodes(opts.gc_file)
    gc_clusters = io.read_clusters(opts.gc_file)
    difference_nodes = list(set(cluster_nodes) - set(gc_nodes))
    gc_clusters.append(difference_nodes)
    io.output_clusters(gc_clusters, '')
Ejemplo n.º 6
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("network_file", help="Original network input file")
    parser.add_argument("cluster_file", help="A cluster results file")
    parser.add_argument("-n",
                        "--node_list",
                        nargs="?",
                        default=[],
                        help="Optionally specify a list of the nodes in\
                              the graph.")
    parser.add_argument("-o",
                        "--output_prefix",
                        nargs="?",
                        default="",
                        help="Optionally specify a prefix for output files")
    opts = parser.parse_args()

    nodes = io.get_node_list(opts.node_list) if opts.node_list else []
    output_pref = opts.output_prefix if opts.output_prefix else './clusters_'
    G = io.build_ig_graph_from_matrix(opts.network_file,
                                      is_directed=False,
                                      node_list=nodes)
    if opts.node_list: G.vs['name'] = nodes
    clusters = io.read_clusters(opts.cluster_file)

    for idx, cluster in enumerate(clusters):
        try:
            output_file = '{}{}.txt'.format(output_pref, idx)
            output_fp = open(output_file, 'w')
        except IOError:
            sys.exit("Could not open file: {}".format(output_file))
        id_cluster = names_to_ids(G, cluster)
        SG = G.subgraph(id_cluster)
        for e in SG.es:
            output_fp.write('{}\t{}\t{}\n'.format(SG.vs[e.source]['name'],
                                                  SG.vs[e.target]['name'],
                                                  e['weight']))
        output_fp.close()
Ejemplo n.º 7
0
def plot_grand_averages_source_estimates_cluster_masked(
        name, save_dir_averages, lowpass, subjects_dir, method, time_window,
        save_plots, figures_path, independent_variable_1,
        independent_variable_2, mne_evoked_time, p_threshold):

    if mne_evoked_time < time_window[0] or mne_evoked_time > time_window[1]:
        raise ValueError('"mne_evoked_time" must be within "time_window"')
    n_subjects = 20  ## should be corrected
    independent_variables = [independent_variable_1, independent_variable_2]
    stcs = dict()
    for stc_type in independent_variables:
        filename = join(save_dir_averages,
                        stc_type + filter_string(lowpass) + \
                        '_morphed_data_' + method)
        stc = mne.read_source_estimate(filename)
        stc.comment = stc_type
        stcs[stc_type] = stc

    difference_stc = stcs[independent_variable_1] - stcs[independent_variable_2]

    ## load clusters

    cluster_dict = io.read_clusters(save_dir_averages, independent_variable_1,
                                    independent_variable_2, time_window,
                                    lowpass)
    cluster_p_values = cluster_dict['cluster_p_values']
    clusters = cluster_dict['clusters']
    T_obs = cluster_dict['T_obs']
    n_sources = T_obs.shape[0]

    cluster_p_threshold = 0.05
    indices = np.where(cluster_p_values <= cluster_p_threshold)[0]
    sig_clusters = []
    for index in indices:
        sig_clusters.append(clusters[index])

    cluster_T = np.zeros(n_sources)
    for sig_cluster in sig_clusters:
        # start = sig_cluster[0].start
        # stop = sig_cluster[0].stop
        sig_indices = np.unique(np.where(sig_cluster == 1)[0])
        cluster_T[sig_indices] = 1

    t_mask = np.copy(T_obs)
    t_mask[cluster_T == 0] = 0
    cutoff = stats.t.ppf(1 - p_threshold / 2, df=n_subjects - 1)

    time_index = mne_evoked_time * 1e3 + 200
    time_window_times = np.linspace(
        time_window[0], time_window[1],
        int((time_window[1] - time_window[0]) * 1e3) + 2)
    time_index_mask = np.where(time_window_times == mne_evoked_time)[0]
    difference_stc._data[:,
                         time_index] = np.reshape(t_mask[:, time_index_mask],
                                                  n_sources)

    mayavi.mlab.close(None, True)
    clim = dict(kind='value', lims=[cutoff, 2 * cutoff, 4 * cutoff])

    mayavi.mlab.figure(figure=0, size=(800, 800))
    brain = difference_stc.plot(subject='fsaverage',
                                subjects_dir=subjects_dir,
                                time_viewer=False,
                                hemi='both',
                                figure=0,
                                clim=clim,
                                views='dorsal')
    time = mne_evoked_time
    brain.set_time(time)
    message = independent_variable_1 + ' vs ' + independent_variable_2
    brain.add_text(0.01, 0.9, message, str(0), font_size=14)

    if save_plots:
        save_path = join(figures_path, 'grand_averages',
                            'source_space/statistics',
                            message + '_' + name + \
                            filter_string(lowpass) + '.jpg' + '_' + str(time * 1e3) + \
                                '_msec.jpg')
        brain.save_single_image(save_path)
        print 'figure: ' + save_path + ' has been saved'

    else:
        print 'Not saving plots; set "save_plots" to "True" to save'