Esempio n. 1
0
	def trans_netattr(self,subject_scan, atlas_name, feature_name, value):
		if value.ndim == 1:  # 这里要改一下
			arr = netattr.Attr(value, atlas.get(atlas_name),subject_scan, feature_name)
			return arr
		else:
			net = netattr.Net(value, atlas.get(atlas_name), subject_scan, feature_name)
			return net
Esempio n. 2
0
	def trans_dynamic_netattr(self, subject_scan, atlas_name, feature_name, window_length, step_size, value):
		if value.ndim == 2:  # 这里要改一下
			arr = netattr.DynamicAttr(value.swapaxes(0,1), atlas.get(atlas_name), window_length, step_size, subject_scan, feature_name)
			return arr
		else:
			net = netattr.DynamicNet(value.swapaxes(0,2).swapaxes(0,1), atlas.get(atlas_name), window_length, step_size, subject_scan, feature_name)
			return net
Esempio n. 3
0
def calculate_real_healthy_range_threshold():
    """
	This function loads in the healthy networks, calculate graph similarities, and plot them in one graph.
	"""
    atlasobj = atlas.get('brodmann_lrce')
    healthy_group = group_manager.getHealthyGroup()
    healthy_scans = [scan.filename for scan in healthy_group.scans]
    healthy_nets = [
        loader.load_single_network(atlasobj, filename)
        for filename in healthy_scans
    ]
    thresh_list = list(range(60, 100))
    curve_mat = np.zeros((int(len(healthy_nets) * (len(healthy_nets) - 1) / 2),
                          len(thresh_list)))  # each row is a curve
    for count, threshold in enumerate(thresh_list):
        print('Threshold: %d' % threshold)
        threshold = threshold / 100.0
        counter = 0  # counter in curve_mat row index
        for i in range(len(healthy_nets)):
            for j in range(i + 1, len(healthy_nets)):
                # calculate similarities between each pair of networks
                curve_mat[counter, count] = compare_two_graphs(
                    healthy_nets[i].binarize(threshold).data,
                    healthy_nets[j].binarize(threshold).data, 4, 4)
                counter += 1
    mdb = mongodb_database.MongoDBDatabase(None)
    mdb.put_temp_data(
        curve_mat, 'graph kernel inter-healthy',
        'The inter-healthy network similarity calculated by graph kernel method with d = 4, h = 4. The binary threshold is varied range(60, 100). The data is of shape (cases, thresh_list length). Each row is a curve'
    )
Esempio n. 4
0
def test_directed_net_strongly_connected_components():
    atlasobj = atlas.get('aal')
    znet = DirectedNet(np.zeros((atlasobj.count, atlasobj.count)), atlasobj)
    znet.data[1, 0] = 1
    znet.data[0, 2] = 1
    znet.data[2, 1] = 1
    znet.data[0, 3] = 1
    znet.data[3, 4] = 1
    scc, lsize = znet.connected_components()
    print(scc, len(scc), lsize)
Esempio n. 5
0
def test_net_connected_components():
    atlasobj = atlas.get('aal')
    znet = zero_net(atlasobj)
    znet.data[2, 3] = 1
    znet.data[3, 2] = 1
    znet.data[3, 5] = 1
    znet.data[5, 3] = 1
    znet.data[6, 7] = 1
    znet.data[7, 6] = 1
    cc, lsize = znet.connected_components()
    print(cc)
    print(len(cc))
    print(lsize)
Esempio n. 6
0
def load_attrs(scans, atlasobj, attrname, rootFolder = rootconfig.path.feature_root, csvfilename = None):
	"""
	Load static attrs as a list
	:param rootFolder:
	:param scans:
	:param atlasobj:
	:return:
	"""
	if type(atlasobj) is str:
		atlasobj = atlas.get(atlasobj)
	# l = AttrLoader(atlasobj, rootFolder)
	if csvfilename is not None:
		return AttrLoader(atlasobj, rootFolder).load_multiple_attrs(scans, attrname, csvfilename)
	return AttrLoader(atlasobj, rootFolder).load_multiple_attrs(scans, attrname)
Esempio n. 7
0
 def get_net(self, scan, atlas_name, feature):
     #return to an net object directly
     if self.exist_static(scan, atlas_name, feature):
         binary_data = self.query_static(scan, atlas_name, feature)['value']
         netdata = pickle.loads(binary_data)
         atlasobj = atlas.get(atlas_name)
         net = netattr.Net(netdata, atlasobj, scan, feature)
         return net
     else:
         print(
             "can't find the document you look for. scan: %s, atlas: %s, feature: %s."
             % (scan, atlas_name, feature))
         raise NoRecordFoundException(scan)
         return None
Esempio n. 8
0
 def get_attr(self, scan, atlas_name, feature, comment_dict={}):
     #return to an attr object  directly
     if self.exist_static(scan, atlas_name, feature, comment_dict):
         binary_data = self.query_static(scan, atlas_name, feature,
                                         comment_dict)[0]['value']
         attrdata = pickle.loads(binary_data)
         atlasobj = atlas.get(atlas_name)
         attr = netattr.Attr(attrdata, atlasobj, scan, feature)
         return attr
     else:
         print(
             "can't find the document you look for. scan: %s, atlas: %s, feature: %s."
             % (scan, atlas_name, feature))
         raise NoRecordFoundException(scan)
         return None
def func(args):
    subject = args[0]
    atlasname = args[1]
    windowLength = args[2]
    stepsize = args[3]
    volumename = '3mm'
    atlasobj = atlas.get(atlasname)
    work_path = 'D:/Research/xuquan_FMRI/Dynamic_tfMRI_work/'
    outfolder = os.path.join(work_path, subject, atlasname, 'bold_net',
                             'dynamic_%d_%d' % (stepsize, windowLength))
    img = load_nii(os.path.join(work_path, subject, 'pBOLD.nii'))
    os.makedirs(outfolder, exist_ok=True)
    c = CalcDynamic(atlasobj, volumename, img, outfolder, windowLength,
                    stepsize)
    c.run()
Esempio n. 10
0
def plot_range_threshold_curve():
    atlasobj = atlas.get('brodmann_lrce')
    mdb = mongodb_database.MongoDBDatabase(None)
    threshold_list = range(5, 100, 5)
    h_list = list(range(1, int(atlasobj.count / 2)))
    d_list = list(range(2, 10))
    plt.figure()
    for threshold in threshold_list:
        curve = mdb.get_temp_data('gk chen range h threshold %d' %
                                  threshold)['value']
        plt.plot(h_list, curve, label='threshold %d' % threshold)
    plt.xticks(range(1, int(atlasobj.count / 2), 3),
               range(1, int(atlasobj.count / 2), 3))
    plt.grid(True)
    plt.legend(loc='upper right')
    plt.xlabel('Depth (h)')
    plt.ylabel('Similarity')
    plt.title('Chen Network similarity d = 4')
    plt.savefig(WORK_PREFIX + 'Chen range h.png', dpi=300)
    plt.close()
Esempio n. 11
0
def calculate_SCI_vs_healthy():
    """
	This function loads in healthy networks and SCI patients networks, calculate each
	patients network similarity to each healthy controls, and plot each person's similarity
	in one figure.
	"""
    atlasobj = atlas.get('brodmann_lrce')
    healthy_group = group_manager.getHealthyGroup()
    healthy_scans = [scan.filename for scan in healthy_group.scans]
    healthy_nets = [
        loader.load_single_network(atlasobj, filename)
        for filename in healthy_scans
    ]

    assistant = analysis_report.GroupAnalysisAssistant('jisuizhenjiaciji',
                                                       atlasobj)
    study = assistant.study
    group1 = study.getGroup('control 1')
    scans = [scan.filename for scan in group1.scans]
    group2 = study.getGroup('treatment 1')
    scans += [scan.filename for scan in group2.scans]
    SCI_nets = [loader.load_single_network(atlasobj, scan) for scan in scans]

    curve_mat = np.zeros((len(scans) * len(healthy_scans),
                          len(thresh_list)))  # each row is a curve
    column_counter = 0
    for subj_num, SCI_net in enumerate(SCI_nets):
        for healthy_net in healthy_nets:
            for count, threshold in enumerate(thresh_list):
                print('Threshold: %d, pair: %d/%d' %
                      (threshold, column_counter, curve_mat.shape[0]))
                threshold = threshold / 100.0
                curve_mat[column_counter, count] = compare_two_graphs(
                    SCI_net.binarize(threshold).data,
                    healthy_net.binarize(threshold).data, 4, 4)
            column_counter += 1
    mdb = mongodb_database.MongoDBDatabase(None)
    mdb.put_temp_data(
        curve_mat, 'graph kernel SCI 1 x HC',
        'The network similarity of each pair of SCI 1 and HC calculated by graph kernel method with d = 4, h = 4. The binary threshold is varied range(60, 100). The data is of shape (cases, thresh_list length). Each row is a curve.'
    )
Esempio n. 12
0
def calculate_real_range_parameters():
    mdb = mongodb_database.MongoDBDatabase(None)
    atlasobj = atlas.get('brodmann_lrce')
    subject_1 = loader.load_single_network(atlasobj, 'caochangsheng_20161027')
    subject_2 = loader.load_single_network(atlasobj, 'caochangsheng_20161114')
    subject_3 = loader.load_single_network(atlasobj, 'chenyifan_20150612')
    subject_4 = loader.load_single_network(atlasobj, 'chenyifan_20150629')

    threshold_list = range(40, 60, 5)
    h_list = list(range(1, int(atlasobj.count / 2)))
    d_list = list(range(2, 10))
    for thresh_count, threshold in enumerate(threshold_list, 1):
        threshold = threshold / 100.0
        sim_list_1 = np.zeros(len(h_list))
        sim_list_2 = np.zeros(len(h_list))
        sim_list_3 = np.zeros(len(h_list))
        for idx, h in enumerate(h_list):
            print('threshold: %d/%d, depth: %d/%d' %
                  (thresh_count, len(threshold_list), h, h_list[-1]))
            sim_list_1[idx] = compare_two_graphs(
                subject_1.binarize(threshold).data,
                subject_2.binarize(threshold).data, h, 4)
            sim_list_2[idx] = compare_two_graphs(
                subject_3.binarize(threshold).data,
                subject_4.binarize(threshold).data, h, 4)
            sim_list_3[idx] = compare_two_graphs(
                subject_1.binarize(threshold).data,
                subject_3.binarize(threshold).data, h, 4)
        threshold = int(threshold * 100.0)
        mdb.put_temp_data(
            sim_list_1, 'gk cao range h threshold %d' % threshold,
            'caochangsheng_20161027 vs caochangsheng_20161114 network similarity measured by graph kernel with brodmann_lrce, threshold = %d, d = 4, h range(1, int(atlasobj.count/2))'
            % threshold)
        mdb.put_temp_data(
            sim_list_2, 'gk chen range h threshold %d' % threshold,
            'chenyifan_20150612 vs chenyifan_20150629 network similarity measured by graph kernel with brodmann_lrce, threshold = %d, d = 4, h range(1, int(atlasobj.count/2))'
            % threshold)
        mdb.put_temp_data(
            sim_list_3, 'gk cao vs chen range h threshold %d' % threshold,
            'caochangsheng_20161027 vs chenyifan_20150612 network similarity measured by graph kernel with brodmann_lrce, threshold = %d, d = 4, h range(1, int(atlasobj.count/2))'
            % threshold)