def example1(): uinvite_network, ll = snafu.uinvite( fluencydata.lists[0], # provide fluency lists datamodel, # specify data model fitinfo=fitinfo, # specify fit info debug=True) # suppress print output to console when set to False return uinvite_network
def example3(): usf_network, usf_items = snafu.load_network( "../snet/USF_animal_subset.snet") # Here you can specify multiple networks as a prior; the first parameter is # a list of networks, the second parameter is a list of dictionaries that # map indices to items in each network usf_prior = snafu.genGraphPrior([usf_network], [usf_items]) uinvite_network, ll = snafu.uinvite(fluencydata.lists[0], prior=(usf_prior, fluencydata.items[0])) return uinvite_network
def network_properties(command, root_path): subj_props = command['data_parameters'] command = command['network_parameters'] # U-INVITE won't work with perseverations if command['network_method'] == "U-INVITE": removePerseverations=True else: removePerseverations=False if subj_props['factor_type'] == "subject": ids = str(subj_props['subject']) group = False elif subj_props['factor_type'] == "group": ids = str(subj_props['group']) # without str() causes unicode issues for "all" :( group = True filedata = snafu.readX(ids, subj_props['fullpath'], category=subj_props['category'], spellfile=label_to_filepath(subj_props['spellfile'], root_path, "spellfiles"), removePerseverations=removePerseverations, group=group) filedata.nonhierarchical() Xs = filedata.Xs items = filedata.items irts = filedata.irts numnodes = filedata.numnodes toydata=snafu.DataModel({ 'numx': len(Xs), 'trim': 1, 'jump': float(command['jump_probability']), 'jumptype': command['jump_type'], 'priming': float(command['priming_probability']), 'startX': command['first_item']}) fitinfo=snafu.Fitinfo({ 'prior_method': "zeroinflatedbetabinomial", 'prior_a': 1, 'prior_b': 2, 'zibb_p': 0.5, 'startGraph': command['starting_graph'], 'goni_size': int(command['goni_windowsize']), 'goni_threshold': int(command['goni_threshold']), 'followtype': "avg", 'prune_limit': 100, 'triangle_limit': 100, 'other_limit': 100}) if command['prior']=="None": prior=None elif command['prior']=="USF": usf_file_path = "/snet/USF_animal_subset.snet" filename = root_path + usf_file_path usf_graph, usf_items = snafu.read_graph(filename) usf_numnodes = len(usf_items) priordict = snafu.genGraphPrior([usf_graph], [usf_items], fitinfo=fitinfo) prior = (priordict, usf_items) if command['network_method']=="RW": bestgraph = snafu.noHidden(Xs, numnodes) elif command['network_method']=="Goni": bestgraph = snafu.goni(Xs, numnodes, td=toydata, valid=0, fitinfo=fitinfo) elif command['network_method']=="Chan": bestgraph = snafu.chan(Xs, numnodes) elif command['network_method']=="Kenett": bestgraph = snafu.kenett(Xs, numnodes) elif command['network_method']=="FirstEdge": bestgraph = snafu.firstEdge(Xs, numnodes) elif command['network_method']=="U-INVITE": bestgraph, ll = snafu.uinvite(Xs, toydata, numnodes, fitinfo=fitinfo, debug=False, prior=prior) nxg = nx.to_networkx_graph(bestgraph) nxg_json = jsonGraph(nxg, items) return graph_properties(nxg,nxg_json)
for sub in subs: Xs, items, irtdata, numnodes = snafu.readX( sub, category, filepath, removePerseverations=True, spellfile="spellfiles/zemla_spellfile.csv") if method == "rw": graph = snafu.nrw(Xs, numnodes) if method == "goni": graph = snafu.goni(Xs, numnodes, fitinfo=fitinfo) if method == "chan": graph = snafu.chan(Xs, numnodes) if method == "kenett": graph = snafu.kenett(Xs, numnodes) if method == "fe": graph = snafu.firstEdge(Xs, numnodes) if method == "uinvite_flat": graph, ll = snafu.uinvite(Xs, toydata, numnodes, fitinfo=fitinfo) for i in range(len(graph)): for j in range(len(graph)): if i > j: item1 = items[i] item2 = items[j] itempair = np.sort([item1, item2]) fo.write(sub + "," + method + "," + itempair[0] + "," + itempair[1] + "," + str(graph[i, j]) + "\n") fo.close()
graph = snafu.goni(Xs_flat, groupnumnodes, fitinfo=fitinfo) # Estimate the best network using Chan if method=="chan": graph = snafu.chan(Xs_flat, groupnumnodes) # Estimate the best network using Kenett if method=="kenett": graph = snafu.kenett(Xs_flat, groupnumnodes) # Estimate the best network using First-Edge if method=="fe": graph = snafu.firstEdge(Xs_flat, groupnumnodes) # Estimate the best network using a non-hierarchical U-INVITE if method=="uinvite_flat": graph, ll = snafu.uinvite(Xs_flat, toydata, groupnumnodes, fitinfo=fitinfo) # Estimate the best network using hierarchical U-INVITE if method=="uinvite_hierarchical": sub_graphs, priordict = snafu.hierarchicalUinvite(Xs_hier, items, numnodes, toydata, fitinfo=fitinfo) graph = snafu.priorToGraph(priordict, groupitems) # convert numpy matrix to networkx graph and replace indices with semantic labels graph = nx.to_networkx_graph(graph) nx.relabel_nodes(graph, groupitems, copy=False) graphs.append(graph) header=','.join(methods) snafu.write_graph(graphs, "human_graphs.csv",header=header)
# Generate Chan graph from data if 'chan' in methods: chan_graph = snafu.chan(flatdata, usf_numnodes) # Generate Kenett graph from data if 'kenett' in methods: kenett_graph = snafu.kenett(flatdata, usf_numnodes) # Generate First Edge graph from data if 'fe' in methods: fe_graph = snafu.firstEdge(flatdata, usf_numnodes) # Generate non-hierarchical U-INVITE graph from data if 'uinvite_flat' in methods: uinvite_flat_graph, ll = snafu.uinvite(flatdata, toydata, usf_numnodes, fitinfo=fitinfo) # Generate hierarchical U-INVITE graph from data if 'uinvite_hierarchical' in methods: uinvite_graphs, priordict = snafu.hierarchicalUinvite(data_hier[:ssnum], items[:ssnum], numnodes[:ssnum], toydata, fitinfo=fitinfo) # U-INVITE paper uses an added "threshold" such that at least 2 participants must have an edge for it to be in the group network # So rather than using the same prior as the one used during fitting, we have to generate a new one priordict = snafu.genGraphPrior(uinvite_graphs, items[:ssnum], fitinfo=fitinfo, mincount=2) # Generate group graph from the prior uinvite_group_graph = snafu.priorToGraph(priordict, usf_items) # Write data to file! for method in methods: if method=="rw": costlist = [snafu.costSDT(rw_graph, usf_graph), snafu.cost(rw_graph, usf_graph)]
# Generate Chan graph from data if 'chan' in methods: chan_graph = snafu.chan(flatdata, usf_numnodes) # Generate Kenett graph from data if 'kenett' in methods: kenett_graph = snafu.kenett(flatdata, usf_numnodes) # Generate First Edge graph from data if 'fe' in methods: fe_graph = snafu.firstEdge(flatdata, usf_numnodes) # Generate non-hierarchical U-INVITE graph from data if 'uinvite_flat' in methods: uinvite_flat_graph, ll = snafu.uinvite(flatdata, toydata, usf_numnodes, fitinfo=fitinfo) # Generate hierarchical U-INVITE graph from data if 'uinvite_hierarchical' in methods: uinvite_graphs, priordict = snafu.hierarchicalUinvite( data_hier[:ssnum], items[:ssnum], numnodes[:ssnum], toydata, fitinfo=fitinfo) # U-INVITE paper uses an added "threshold" such that at least 2 participants must have an edge for it to be in the group network # So rather than using the same prior as the one used during fitting, we have to generate a new one priordict = snafu.genGraphPrior(uinvite_graphs, items[:ssnum],
def network_properties(command, root_path): subj_props = command['data_parameters'] command = command['network_parameters'] # U-INVITE won't work with perseverations if command['network_method'] == "U-INVITE": removePerseverations = True else: removePerseverations = False if subj_props['factor_type'] == "subject": Xs, items, irts, numnodes = snafu.readX( subj_props['subject'], subj_props['category'], subj_props['fullpath'], spellfile=label_to_filepath(subj_props['spellfile'], root_path, "spellfiles"), removePerseverations=removePerseverations) elif subj_props['factor_type'] == "group": Xs, items, irts, numnodes = snafu.readX( subj_props['subject'], subj_props['category'], subj_props['fullpath'], spellfile=label_to_filepath(subj_props['spellfile'], root_path, "spellfiles"), removePerseverations=removePerseverations, group=subj_props['group'], flatten=True) toydata = snafu.Data({ 'numx': len(Xs), 'trim': 1, 'jump': float(command['jump_probability']), 'jumptype': command['jump_type'], 'priming': float(command['priming_probability']), 'startX': command['first_item'] }) fitinfo = snafu.Fitinfo({ 'prior_method': "betabinomial", 'prior_a': 1, 'prior_b': 1, 'startGraph': command['starting_graph'], 'goni_size': int(command['goni_windowsize']), 'goni_threshold': int(command['goni_threshold']), 'followtype': "avg", 'prune_limit': 100, 'triangle_limit': 100, 'other_limit': 100 }) if command['prior'] == "None": prior = None elif command['prior'] == "USF": usf_file_path = "/snet/USF_animal_subset.snet" filename = root_path + usf_file_path usf_graph, usf_items = snafu.read_graph(filename) usf_numnodes = len(usf_items) priordict = snafu.genGraphPrior([usf_graph], [usf_items], fitinfo=fitinfo) prior = (priordict, usf_items) if command['network_method'] == "RW": bestgraph = snafu.noHidden(Xs, numnodes) elif command['network_method'] == "Goni": bestgraph = snafu.goni(Xs, numnodes, td=toydata, valid=0, fitinfo=fitinfo) elif command['network_method'] == "Chan": bestgraph = snafu.chan(Xs, numnodes) elif command['network_method'] == "Kenett": bestgraph = snafu.kenett(Xs, numnodes) elif command['network_method'] == "FirstEdge": bestgraph = snafu.firstEdge(Xs, numnodes) elif command['network_method'] == "U-INVITE": bestgraph, ll = snafu.uinvite(Xs, toydata, numnodes, fitinfo=fitinfo, debug=False, prior=prior) nxg = nx.to_networkx_graph(bestgraph) node_degree = np.mean(dict(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 }