def network_properties(command): subj_props = command['data_parameters'] command = command['network_parameters'] Xs, items, irts, numnodes = rw.readX(subj_props['subject'], subj_props['category'], subj_props['fullpath']) def no_persev(x): seen = set() seen_add = seen.add return [i for i in x if not (i in seen or seen_add(i))] toydata = rw.Data({ 'numx': len(Xs), 'trim': 1, 'jump': 0.0, 'jumptype': "stationary", 'priming': 0.0, 'startX': "stationary" }) fitinfo = rw.Fitinfo({ 'startGraph': "goni_valid", 'goni_size': 2, 'goni_threshold': 2, 'followtype': "avg", 'prior_samplesize': 10000, 'recorddir': "records/", 'prune_limit': 100, 'triangle_limit': 100, 'other_limit': 100 }) if command['network_method'] == "RW": bestgraph = rw.noHidden(Xs, numnodes) elif command['network_method'] == "Goni": bestgraph = rw.goni(Xs, numnodes, td=toydata, valid=0, fitinfo=fitinfo) elif command['network_method'] == "U-INVITE": no_persev_Xs = [no_persev(x) for x in Xs ] # U-INVITE doesn't work with perseverations bestgraph, ll = rw.uinvite(no_persev_Xs, toydata, numnodes, fitinfo=fitinfo, debug=False) nxg = nx.to_networkx_graph(bestgraph) node_degree = np.mean(nxg.degree().values()) nxg_json = jsonGraph(nxg, items) clustering_coefficient = nx.average_clustering(nxg) try: aspl = nx.average_shortest_path_length(nxg) except: aspl = "disjointed graph" return { "type": "network_properties", "node_degree": node_degree, "clustering_coefficient": clustering_coefficient, "aspl": aspl, "graph": nxg_json }
category="animals" Xs, items, irts.data, numnodes=rw.readX(subj,category,'./Spring2015/results_cleaned.csv',ignorePerseverations=True) # uinvite prior priorgraphs=[] prioritems=[] for osub in subs: if osub != subj: g, i = rw.read_csv('Sdirected2015.csv',cols=("node1","node2"),header=True,filters={"subj": osub, "uinvite": "1"},undirected=False) priorgraphs.append(g) prioritems.append(i) priordict = rw.genGraphPrior(priorgraphs, prioritems) toydata.numx = len(Xs) prior = (priordict, items) # u-invite directed_prior_graph, bestval=rw.uinvite(Xs, toydata, numnodes, fitinfo=fitinfo, prior=prior) # rw rw_graph=rw.noHidden(Xs, numnodes) g=nx.DiGraph(directed_prior_graph) g2=nx.DiGraph(rw_graph) nx.relabel_nodes(g, items, copy=False) nx.relabel_nodes(g2, items, copy=False) rw.write_csv([g, g2],subj+"_directed_prior_nosparse.csv",subj,directed=True) # write multiple graphs
'exgauss_sigma': 6.58655566, 'irt_weight': 0.95, 'rcutoff': 20}) for subj in subs: print subj category="animals" Xs, items, irts.data, numnodes=rw.readX(subj,category,'./Spring2015/results_cleaned.csv',ignorePerseverations=True) toydata.numx = len(Xs) [mu, sig, lambd] = rw.mexgauss(rw.flatten_list(irts.data)) irts.exgauss_sigma = sig irts.exgauss_lambda = lambd # u-invite uinvite_graph, bestval=rw.uinvite(Xs, toydata, numnodes, fitinfo=fitinfo) # irt95 irts.irt_weight=0.95 irt95_graph, bestval=rw.uinvite(Xs, toydata, numnodes, irts=irts, fitinfo=fitinfo) # irt5 irts.irt_weight=0.5 irt5_graph, bestval=rw.uinvite(Xs, toydata, numnodes, irts=irts, fitinfo=fitinfo) # rw rw_graph=rw.noHidden(Xs, numnodes) # goni goni_graph=rw.goni(Xs, numnodes, td=toydata, valid=0, fitinfo=fitinfo)
'irttype': "exgauss", 'lambda': 0.721386887, 'sigma': 6.58655566, 'irt_weight': 0.5, 'rcutoff': 20}) subs=['S102','S103','S104','S105','S106','S107','S108','S109','S110', 'S111','S112','S113','S114','S115','S116','S117','S118','S119','S120'] # u-invite for subj in subs: category="animals" Xs, items, irts.data, numnodes=rw.readX(subj,category,'./Spring2015/results_cleaned.csv',ignorePerseverations=True) toydata.numx = len(Xs) uinvite_graph, bestval=rw.uinvite(Xs, toydata, numnodes, fitinfo=fitinfo) #orig=rw.probX(Xs, uinvite_graph, toydata) extra_data={} for inum, i in enumerate(uinvite_graph): for jnum, j in enumerate(i): if uinvite_graph[inum,jnum]==1: uinvite_graph[inum,jnum]=0 uinvite_graph[jnum,inum]=0 result=rw.probX(Xs, uinvite_graph, toydata, forceCompute=True) if items[inum] not in extra_data: extra_data[items[inum]]={} if items[jnum] not in extra_data: extra_data[items[jnum]]={}
'numnodes': 50, 'numlinks': 6 }) fh = open('priming_test.csv', 'w') seed = 15 for td in toydata: print "numx: ", td.numx # generate data with priming and fit best graph g, a = rw.genG(toygraph, seed=seed) [Xs, irts, alter_graph] = rw.genX(g, td, seed=seed) bestgraph_priming, ll = rw.uinvite(Xs, td, toygraph.numnodes, fitinfo=fitinfo, seed=seed, debug=True) priming_cost = rw.cost(bestgraph_priming, a) print priming_cost td.priming = 0.0 # fit best graph assuming no priming bestgraph_nopriming, ll = rw.uinvite(Xs, td, toygraph.numnodes, fitinfo=fitinfo, seed=seed, debug=True) nopriming_cost = rw.cost(bestgraph_nopriming, a) print nopriming_cost
irts = rw.Irts({ 'data': [], 'irttype': "exgauss", 'lambda': 0.721386887, 'sigma': 6.58655566, 'irt_weight': 0.9, 'rcutoff': 20 }) for subj in subs: category = "animals" Xs, items, irts.data, numnodes = rw.readX( subj, category, './Spring2015/results_cleaned.csv') uinvite_irt9, bestval = rw.uinvite(Xs, toydata, numnodes, fitinfo=fitinfo, irts=irts) irts.irt_weight = 0.95 uinvite_irt95, bestval = rw.uinvite(Xs, toydata, numnodes, fitinfo=fitinfo, irts=irts) irts.irt_weight = 0.5 uinvite_irt5, bestval = rw.uinvite(Xs, toydata, numnodes, fitinfo=fitinfo, irts=irts) g = nx.to_networkx_graph(uinvite_irt5)
rnd += 1 changesmade = 0 np.random.shuffle(subs) for subj in subs: print "ss: ", subj subj_idx = original_order.index(subj) toydata.numx = len(ss_Xs[subj_idx]) fitinfo.startGraph = prior_graphs[subj_idx] prior = (priordict, prior_items[subj_idx]) # find best graph uinvite_graph, bestval = rw.uinvite(ss_Xs[subj_idx], toydata, ss_numnodes[subj_idx], fitinfo=fitinfo, prior=prior) ## update prior if graph has changed if not np.array_equal(uinvite_graph, prior_graphs[subj_idx]): changesmade += 1 prior_graphs[subj_idx] = uinvite_graph priordict = rw.genGraphPrior(prior_graphs, prior_items) for subj in range(len(subs)): g = nx.to_networkx_graph(prior_graphs[subj]) g2 = nx.to_networkx_graph(rw.noHidden(ss_Xs[subj], ss_numnodes[subj])) nx.relabel_nodes(g, prior_items[subj], copy=False) nx.relabel_nodes(g2, prior_items[subj], copy=False) rw.write_csv([g, g2], subj + ".csv", subj) # write multiple graphs