import tacoma as tc L = tc.edge_lists() L.N = 2 L.t = [0.,0.2,0.1] L.tmax = 0.11 L.edges = [ [ (0,1), (0,0), (1,0) ], [ (1,2) ], ] tc.verify(L) print C = tc.edge_changes() C.N = 2 C.t0 = 0 C.t = [0.2,0.1] C.tmax = 0.05 C.edges_initial = [ (5,4), (0,1), (0,1), ] C.edges_in = [ [(2,3), (0,0), (2,3)],[(1,0),(0,1)] ] C.edges_out = [ [(0,0), (1,0), (1,0)],[(1,0),(0,1) ] ] tc.verify(C)
t0 = 0.0 tmax = 100.0 t = [] this_time = t0 # Generate a new random network while this_time < tmax: G = nx.fast_gnp_random_graph(N, p) these_edges = list(G.edges()) t.append(this_time) edge_lists.append(these_edges) this_time += np.random.exponential(scale=1 / mean_tau) # save to _tacoma-object el = tc.edge_lists() el.N = N el.t = t el.edges = edge_lists el.tmax = tmax print("Number of mistakes:", tc.verify(el)) from tacoma.drawing import edge_activity_plot from bfmplot import pl edge_activity_plot(el, fit=True) pl.show()
tmax = time[-1] + 20.0 # get a new `edge_lists` instance el = tc.edge_lists() el.N = N el.tmax = tmax el.edges = edges el.t = time el.time_unit = 's' el.notes = """ This data is binned. In this data, t0 = 0.0 corresponds to 8am on Jun 29th 2009 (UNIX time 1246255200). For more info, please visit http://www.sociopatterns.org/datasets/hypertext-2009-dynamic-contact-network/ . If you use this data, please cite L. Isella et al., What's in a crowd? Analysis of face-to-face behavioral networks, Journal of Theoretical Biology 271, 166 (2011). """ el.int_to_node = int_to_node # verifying that this is a valid temporal network tc.verify(el) # save this edge_lists instance with open('ht09.taco', 'w') as f: tc.write_json_taco(el, f)
from tacoma.flockwork import flockwork_P # Define structural properties N = 100 k = 2.0 P = k / (k+1) # The disconnection rate per node is set # to gamma = 1, so the total run time is # in units of 1/gamma = 1 t_run_total = 100 # In order to start with any initial state, # pass an edge list to this function. # If you don't, the function assumes # you want to start with an equilibrium # configuration and generates one. fw = flockwork_P(N, P, t_run_total) # verify that the Flockwork is in a proper # format import tacoma as tc print("Number of rule violations in generated temporal network:", tc.verify(fw)) print(type(fw))
L.N = 3 L.t = [0.0,1.0,2.0] L.tmax = 3.0 L.edges = [ [ (0,1) ], [ (1,2), (0,2) ], [ (0,1) ], ] print("errors in temporal network:", tc.verify(L)) P_k = tc.degree_distribution_from_edge_lists(L) for k, P in enumerate(P_k): print(k, P) P_k = tc.degree_distribution(L) for k, P in enumerate(P_k): print(k, P) print("===== edge_changes => edge_changes =====") C = tc.edge_changes() C.N = 3 C.edges_initial = [ (0,1) ]
def download_and_convert_sociopatterns_high_school_2013( url="http://www.sociopatterns.org/wp-content/uploads/2015/07/High-School_data_2013.csv.gz", filename="~/.tacoma/hs13.taco", ): """Download the SocioPatterns 'High school 2013 dynamic contact network' data, extract it and save it as taco. This data is actually binned in intervals of `[t-20s, t]`. Parameters ---------- url : :obj:`str`, optional The url from which the tsv-data should be retrieved filename : :obj:`str`, optional this is the path where the taco will be saved to. default : "~/.tacoma/hs13.taco" Returns ------- edge_lists : :mod:`edge_lists` The temporal network of the 'High school 2013 dynamic contact network'. Notes ----- If you use this data, please cite :: [HS13] R. Mastrandrea, J. Fournet, A. Barrat, Contact patterns in a high school: a comparison between data collected using wearable sensors, contact diaries and friendship surveys. PLoS ONE 10(9): e0136497 (2015) """ # get directory name for download directory, _ = os.path.split(os.path.abspath(os.path.expanduser(filename))) mkdirp_customdir(directory) # download wget.download(url, out=directory) # open gzipped file gzip_file = os.path.join(directory, 'High-School_data_2013.csv.gz') with gzip.open(gzip_file, mode='rt') as f: reader = csv.reader(f, delimiter=' ') # mappings of nodes to integers node_to_int = {} int_to_node = {} # get an initial t_old # (this is done to detect changes in the tsv t_old = None # list of edge lists edges = [] # time points time = [] count = 0 for row in reader: if count == 0: t0 = int(row[0]) - 20 # this is to account for the interval choice [t-20s, t] t = float(int(row[0]) - 20 - t0) # if the time changed, we save the new time and # prepare to save new edges if t_old != t: if (t_old is not None) and (t - t_old > 20): edges.append([]) time.append(t_old + 20) edges.append([]) time.append(t) # get the edge i = int(row[1]) j = int(row[2]) # map the edge to integers if i not in node_to_int: this_int = len(node_to_int) node_to_int[i] = len(node_to_int) int_to_node[this_int] = str(i) if j not in node_to_int: this_int = len(node_to_int) node_to_int[j] = len(node_to_int) int_to_node[this_int] = str(j) # save the edge edges[-1].append(tuple(sorted([node_to_int[i], node_to_int[j]]))) t_old = t count += 1 N = len(node_to_int) tmax = time[-1] + 20.0 # get a new `edge_lists` instance el = tc.edge_lists() el.N = N el.tmax = tmax el.edges = edges el.t = time el.time_unit = 's' el.notes = """ This data is binned. In this data, t0 = 0.0 corresponds to UNIX time """ + str(t0) + """. For more info, please visit http://www.sociopatterns.org/datasets/high-school-contact-and-friendship-networks/ . If you use this data, please cite R. Mastrandrea, J. Fournet, A. Barrat, Contact patterns in a high school: a comparison between data collected using wearable sensors, contact diaries and friendship surveys. PLoS ONE 10(9): e0136497 (2015) """ el.int_to_node = int_to_node # verifying that this is a valid temporal network tc.verify(el) # save this edge_lists instance with open(os.path.abspath(os.path.expanduser(filename)), 'w') as f: write_json_taco(el, f) # remove the downloaded gzipped file os.remove(gzip_file) return el
def download_and_convert_sociopatterns_hypertext_2009( url="http://www.sociopatterns.org/files/datasets/003/ht09_contact_list.dat.gz", filename="~/.tacoma/ht09.taco", ): """Download the SocioPatterns 'Hypertext 2009 dynamic contact network' data, extract it and save it as taco. This data is actually binned in intervals of `[t-20s, t]`. Parameters ---------- url : :obj:`str`, optional The url from which the tsv-data should be retrieved filename : :obj:`str`, optional this is the path where the taco will be saved to. default : "~/.tacoma/ht09.taco" Returns ------- edge_lists : :mod:`edge_lists` The temporal network of the 'Hypertext 2009 dynamic contact network'. Notes ----- If you use this data, please cite :: L. Isella et al., What's in a crowd? Analysis of face-to-face behavioral networks, Journal of Theoretical Biology 271, 166 (2011). """ # get directory name for download directory, _ = os.path.split(os.path.abspath(os.path.expanduser(filename))) mkdirp_customdir(directory) # download wget.download(url, out=directory) # open gzipped file gzip_file = os.path.join(directory, 'ht09_contact_list.dat.gz') with gzip.open(gzip_file, mode='rt') as f: reader = csv.reader(f, delimiter='\t') # mappings of nodes to integers node_to_int = {} int_to_node = {} # get an initial t_old # (this is done to detect changes in the tsv t_old = None # list of edge lists edges = [] # time points time = [] for row in reader: # this is to account for the interval choice [t-20s, t] t = float(int(row[0]) - 20) # if the time changed, we save the new time and # prepare to save new edges if t_old != t: if (t_old is not None) and (t - t_old > 20): edges.append([]) time.append(t_old + 20) edges.append([]) time.append(t) # get the edge i = int(row[1]) j = int(row[2]) # map the edge to integers if i not in node_to_int: this_int = len(node_to_int) node_to_int[i] = len(node_to_int) int_to_node[this_int] = str(i) if j not in node_to_int: this_int = len(node_to_int) node_to_int[j] = len(node_to_int) int_to_node[this_int] = str(j) # save the edge edges[-1].append(tuple(sorted([node_to_int[i], node_to_int[j]]))) t_old = t N = len(node_to_int) tmax = time[-1] + 20.0 # get a new `edge_lists` instance el = tc.edge_lists() el.N = N el.tmax = tmax el.edges = edges el.t = time el.time_unit = 's' el.notes = """ This data is binned. In this data, t0 = 0.0 corresponds to 8am on Jun 29th 2009 (UNIX time 1246255200). For more info, please visit http://www.sociopatterns.org/datasets/hypertext-2009-dynamic-contact-network/ . If you use this data, please cite L. Isella et al., What's in a crowd? Analysis of face-to-face behavioral networks, Journal of Theoretical Biology 271, 166 (2011). """ el.int_to_node = int_to_node # verifying that this is a valid temporal network tc.verify(el) # save this edge_lists instance with open(os.path.abspath(os.path.expanduser(filename)), 'w') as f: write_json_taco(el, f) # remove the downloaded gzipped file os.remove(gzip_file) return el
import tacoma as tc L = tc.edge_lists() L.N = 2 L.t = [0.,0.2,0.1] L.tmax = 0.11 L.edges = [ [ (0,1), (0,0), (1,0) ], [ (1,2) ], ] L.int_to_node = { 0: '1', 1: '1' } print("number of errors =", tc.verify(L)) print() C = tc.edge_changes() C.N = 3 C.t0 = 0 C.t = [0.2,0.1] C.tmax = 0.05 C.edges_initial = [ (5,4), (0,1), (0,1), ] C.edges_in = [ [(2,3), (0,0), (2,3)],[(1,0),(0,1)] ] C.edges_out = [ [(0,0), (1,0), (1,0)],[(1,0),(0,1) ] ] C.int_to_node = { 0: '1', 1: '1' }