def get_edge_probability_and_rate(temporal_network): r""" For each edge compute the probability that it is active and the rate with which it is activated. Parameters ========== temporal_network : :class:`_tacoma.edge_lists`, :class:`_tacoma.edge_changes` or :class:`_tacoma.edge_trajectories` Returns ======= p : numpy.ndarray The probability to be switched on for each observed edge of the network (the remaining un-observed edges have probability p = 0). omega : numpy.ndarray The rate with which the observed edges are switched on :math:`\omega = \left(\frac{1}{\tau^+} + \frac{1}{\tau^{-}}\right)^{-1}` (the remaining un-observed edges have rate :math:`\omega = 0`). """ if type(temporal_network) in [ec, el, el_h, ec_h]: traj = tc.get_edge_trajectories(temporal_network) tmax = temporal_network.tmax if type(temporal_network) in [ec, ec_h]: t0 = temporal_network.t0 else: t0 = temporal_network.t[0] elif type(temporal_network) == edge_trajectories: traj = temporal_network.trajectories tmax = temporal_network.tmax t0 = temporal_network.t0 else: raise ValueError("Unknown type for temporal network:", type(temporal_network)) T = tmax - t0 connection_probability = np.empty(len(traj)) activity_rate = np.empty(len(traj)) for iedge, entry in enumerate(traj): N_events = len(entry.time_pairs) if entry.time_pairs[0][0] == t0: N_events -= 1 activity_rate[iedge] = N_events / T t_on = 0.0 for interval in entry.time_pairs: t_on += interval[1] - interval[0] connection_probability[iedge] = t_on / T return connection_probability, activity_rate
def get_edge_life_times(temporal_network): r""" For each edge compute the probability that it is active and the rate with which it is activated. Parameters ========== temporal_network : :class:`_tacoma.edge_lists`, :class:`_tacoma.edge_changes` or :class:`_tacoma.edge_trajectories` Returns ======= p : numpy.ndarray The probability to be switched on for each observed edge of the network (the remaining un-observed edges have probability p = 0). omega : numpy.ndarray The rate with which the observed edges are switched on :math:`\omega = \left(\frac{1}{\tau^+} + \frac{1}{\tau^{-}}\right)^{-1}` (the remaining un-observed edges have rate :math:`\omega = 0`). """ if type(temporal_network) in [ec, el, el_h, ec_h]: traj = tc.get_edge_trajectories(temporal_network) tmax = temporal_network.tmax if type(temporal_network) in [ec, ec_h]: t0 = temporal_network.t0 else: t0 = temporal_network.t[0] elif type(temporal_network) == edge_trajectories: traj = temporal_network.trajectories tmax = temporal_network.tmax t0 = temporal_network.t0 else: raise ValueError("Unknown type for temporal network:", type(temporal_network)) T = tmax - t0 connection_probability = np.empty(len(traj)) activity_rate = np.empty(len(traj)) life_times = [] for iedge, entry in enumerate(traj): for pair in entry.time_pairs: if pair[0] != t0 and pair[0] != tmax: life_times.append(pair[1] - pair[0]) return np.array(life_times)
def contact_coverage(temporal_network): """Get the total number of discovered unique edges C(t), i.e. the contact coverage. Parameters ========== temporal_network : :class:`_tacoma.edge_trajectories`, :class:`_tacoma.edge_lists`, :class:`_tacoma.edge_changes` or :obj:`list` of :class:`_tacoma.edge_trajectory_entry` Returns ======= t : numpy.ndarray Time points at which new edges have been discovered C : numpy.ndarray total number of edges discovered up to time t. """ if type(temporal_network) in [ec, el, el_h, ec_h]: traj = tc.get_edge_trajectories(temporal_network) elif type(temporal_network) == list and type( temporal_network[0]) == tc.edge_trajectory_entry: traj = temporal_network elif type(temporal_network) == edge_trajectories: traj = temporal_network.trajectories else: raise ValueError("Unknown type for temporal network:", type(temporal_network)) t = [] count = [] for iedge, entry in enumerate(traj): t0 = entry.time_pairs[0][0] if len(t) > 0: if t[-1] == t0: count[-1] = iedge + 1 else: count.append(iedge + 1) t.append(t0) else: count.append(iedge + 1) t.append(t0) return np.array(t), np.array(count, dtype=float)
def write_edge_trajectory_coordinates(temporal_network, filename, filter_for_duration=0.0): """Write the coordinates of the edge activation periods to a json-file such that each entry corresponds to a line to be drawn. Parameters ---------- temporal_network : :mod:`edge_changes`, :mod:`edge_lists`, :mod:`edge_changes_with_histograms`, or :mod:`edge_lists_with_histograms` An instance of a temporal network. filename : :obj:`str` Write to this file. """ traj = tc.get_edge_trajectories(temporal_network) try: t0 = temporal_network.t[0] except: t0 = temporal_network.t0 tmax = temporal_network.tmax coords = [] for i_edge, entry in enumerate(traj): for time_pair in entry.time_pairs: t_i = time_pair[0] t_f = time_pair[1] if t_f - t_i > filter_for_duration: coords.append((i_edge, t_i, t_f)) data = { 'xlim': (t0, tmax), 'ylim': (0, len(traj)), 'data': coords, } filename = os.path.abspath(os.path.expanduser(filename)) with open(filename, 'w') as f: json.dump(data, f)
import matplotlib.pyplot as pl import tacoma as tc from tacoma.drawing import draw_edges from tacoma.drawing import get_edge_order import numpy as np #tn = tc.load_sociopatterns_hypertext_2009() tn = tc.load_json_taco('~/.tacoma/dtu_1_weeks.taco') tn = tc.convert(tn) edge_traj = tc.get_edge_trajectories(tn, return_edge_similarities=True) print(edge_traj.edge_similarities[:10]) edge_order = get_edge_order(edge_traj, threshold=3600.) print(edge_order) print(np.all(edge_order == np.sort(edge_order))) draw_edges(edge_traj.trajectories, edge_order=edge_order) draw_edges(edge_traj.trajectories) pl.show()
import matplotlib.pyplot as pl from tacoma.model_conversions import estimate_ZSBB_args from tacoma.model_conversions import estimate_flockwork_P_args from tacoma.model_conversions import estimate_dynamic_RGG_args # ======== get original network ============= socio = tc.load_sociopatterns_hypertext_2009() socio_binned = tc.bin(socio, dt=20.) # ========= plot properties ============== socio_result = tc.measure_group_sizes_and_durations(socio) fig, ax, data = temporal_network_group_analysis(socio_result, time_unit=socio.time_unit) fig.tight_layout() traj = tc.get_edge_trajectories(socio) draw_edges(traj.trajectories, ax=ax[3]) # ========== generate surrogate from ZSBB model ====== ZSBB_params = estimate_ZSBB_args(socio, group_sizes_and_durations=socio_result) ZSBB_params['b0'] = 0.51 ZSBB_params['b1'] = 1.0 ZSBB_params['lambda'] = 0.9 zsbb = tc.ZSBB_model(**ZSBB_params) this_t0 = zsbb.t0 zsbb.t0 = 0. zsbb.tmax -= this_t0 zsbb.t = [(t - this_t0) / zsbb.tmax * (socio.tmax - socio.t[0]) for t in zsbb.t] zsbb.tmax = socio.tmax - socio.t[0]
def edge_activity_plot( temporal_network, time_normalization_factor=1., time_unit=None, ax=None, fit=False, edge_order=None, color=None, alpha=0.5, linewidth=1, intervals_to_discard_for_fit=[], fit_color=None, return_fit_params=False, ): """ Draw an edge activity plot for the given temporal network. This is a wrapper for :func:`tacoma.drawing.draw_edges`. Parameters ---------- temporal_network : :class:`_tacoma.edge_lists` or :class:`_tacoma.edge_changes`. A temporal network. time_normalization_factor, float, default : 1.0 Rescale time by this factor. time_unit : str, default : None Unit of time to put on the axis. ax : matplotlib.Axes, default : None Axis to draw an, will create new one if none provided. fit : bool, default : False Fit a curve to the number of uniquely observed edges. edge_order : list of int, default : None Reorder the edges according to this list before drawing. color : a matplotlib color, default : None Color in which to draw the edges in alpha : float, default : 0.5 Line opacity of edges linewidth : float, default : 1.0 Line width of edges intervals_to_discard_for_fit : list of tuple of float a list of time intervals which have to be discarded for the fit fit_color : a matplotlib color, default : 'k' color in which to draw the fit in return_fit_params : bool, default : False Switch this on if you want to obtain the fit parameters. Returns ------- fig : matplotlib.Figure If an axes was provided, this is `None`. ax : matplotlib.Axes The axes the plot was drawn on. popt : tuple of float Fit parameters, will only be returned if `return_fit_params` is `True`. """ traj = tc.get_edge_trajectories(temporal_network) return draw_edges( traj, time_normalization_factor=time_normalization_factor, time_unit=time_unit, ax=ax, fit=fit, edge_order=edge_order, color=color, alpha=alpha, linewidth=linewidth, intervals_to_discard_for_fit=intervals_to_discard_for_fit, fit_color=fit_color, return_fit_params=return_fit_params, )
L.t = [0.0, 1.0, 2.0] L.tmax = 3.0 L.edges = [ [(0, 1)], [(1, 2), (0, 2)], [(0, 1)], ] L = _tacoma.dynamic_RGG(100, 100, mean_link_duration=10) #F = tc.flockwork_P_varying_rates([],100,[0.5],100,[(0.0,1.0)],tmax=100) F = L FBIN = tc.bin(F, dt=1) # draw_rows(FBIN) start = time.time() traj, similarities = tc.get_edge_trajectories( FBIN, return_edge_similarities=True) end = time.time() print(similarities) print("needed ", end - start, "seconds") draw_edges(traj, fit=True) start = time.time() result = tc.get_edge_trajectories(F) end = time.time() print("needed ", end - start, "seconds") draw_edges(traj) # draw_edge_lists(L) pl.show()