示例#1
0
def analyze_km(times_path, output_path=''):
    f = open(output_path, 'wb')
    f.write(
        '0 1 calls mu_na_nomin va_na_nomin bu_na_nomin mu_na_min va_na_min bu_na_min mu_km_nomin va_km_nomin bu_km_nomin mu_km_min va_km_min bu_km_min\n'
    )
    with open(times_path, 'r') as r:
        row = r.readline()
        while row:
            n1, n2, times = utils.parse_time_line(row)
            if len(times) > 1:
                s = '{} {} {} '.format(n1, n2, len(times))
                m, v, b = _km_versions(times, 'naive', None)
                s += '{} {} {} '.format(m, v, b)
                try:
                    m, v, b = _km_versions(times, 'naive')
                except:
                    import pdb
                    pdb.set_trace()
                s += '{} {} {} '.format(m, v, b)
                m, v, b = _km_versions(times, 'km', None)
                s += '{} {} {} '.format(m, v, b)
                m, v, b = _km_versions(times, 'km')
                s += '{} {} {}\n'.format(m, v, b)
                f.write(s)
            row = r.readline()
    f.close()
示例#2
0
def net_residual_times(dic=None,
                       path=times_path,
                       output_path='',
                       kaplan='naive'):
    """
    Dic must be an edge dictionary, where values are a list of call times
    Otherwise, write a .txt file containing the edges and values
    """
    net = netio.pynet.SymmNet()
    if dic:
        for k, v in dic.iteritems():
            n1, n2 = k
            if len(v) > 1:
                net[n1, n2] = residual_intervals(v, kaplan)
        return net
    else:
        f = open(output_path, "a")
        with open(path, 'r') as r:
            row = r.readline()
            while row:
                n1, n2, times = utils.parse_time_line(row)
                if len(times) > 1:
                    w = mean_inter_event_km(times, kaplan) / 86400
                    s = n1 + " " + n2 + " " + str(w) + "\n"
                    f.write(s)
                row = r.readline()
        f.close()
示例#3
0
def read_timesdic(path, extra=False):
    dic = {}
    extr = {}
    with open(path, 'r') as r:
        row = r.readline()
        while row:
            if extra:
                n1, n2, a, b = utils.parse_time_line(row, True)
                dic[(n1, n2)] = a
                extr[(n1, n2)] = b
            else:
                n1, n2, a = utils.parse_time_line(row, False)
                # NOTE: if causes problems remove array.array
                dic[(n1, n2)] = array('i', a)
            row = r.readline()
    if extra:
        return dic, extr
    else:
        return dic
示例#4
0
def bursty_trains_to_file(path, output_path, delta=3600):
    f = open(output_path, "a")
    with open(path, 'r') as r:
        row = r.readline()
        while row:
            n1, n2, times = utils.parse_time_line(row)
            if len(times) > 1:
                w = number_of_bursty_trains(times, delta)
                s = n1 + " " + n2 + " " + str(w) + "\n"
                f.write(s)
            row = r.readline()
    f.close()
示例#5
0
def total_calls_times(times_path, output_path):
    """
    Create net with total number of calls using times of calls
    """
    net = netio.pynet.SymmNet()
    w = open(output_path, 'wb')
    with open(times_path, 'r') as r:
        row = r.readline()
        while row:
            n1, n2, times = utils.parse_time_line(row)
            f = ' '.join(n1, n2, str(len(times)))
            w.write(f)
            #net[int(n1), int(n2)] = len(times)
            row = r.readline()
    w.close()
示例#6
0
def net_tail(path=times_path, output_path=''):
    """
    Create net with tail probabilitys
    """
    f = open(output_path, "a")
    with open(path, 'r') as r:
        row = r.readline()
        while row:
            n1, n2, times = utils.parse_time_line(row)
            if len(times) > 1:
                w = tail_weight(times)
                s = n1 + " " + n2 + " " + str(w) + "\n"
                f.write(s)
            row = r.readline()
    f.close()
示例#7
0
def net_calltimes_mode(path=times_path, output_path=''):
    """
    Write edgelist of net with mode of each call time
    """
    f = open(output_path, "a")
    with open(path, 'r') as r:
        row = r.readline()
        while row:
            n1, n2, times = utils.parse_time_line(row)
            if len(times) > 1:
                w = main_call_time(times)
                s = n1 + " " + n2 + " " + str(w) + "\n"
                f.write(s)
            row = r.readline()
    f.close()
示例#8
0
def net_burstiness(path=times_path, output_path='', kaplan='naive'):
    """
    Create net with burstiness, kaplan is either 'km', 'km+', 'naive'
    """
    f = open(output_path, "a")
    with open(path, 'r') as r:
        row = r.readline()
        while row:
            n1, n2, times = utils.parse_time_line(row)
            if len(times) > 1:
                w = km_burstiness(times, kaplan)
                s = n1 + " " + n2 + " " + str(w) + "\n"
                f.write(s)
            row = r.readline()
    f.close()
示例#9
0
def net_overlap(net, output_path=None, alt_net_path=None):
    #if not return_net:
    #    dic = {}
    #    for edge in net.edges:
    #        e0, e1, w = edge
    #        e0, e1 = np.sort([e0, e1])
    #        dic[(e0, e1)] = netanalysis.overlap(net, e0, e1)
    #    return dic
    if output_path is not None:
        f = open(output_path, 'w+')
        if alt_net_path is not None:
            r = open(alt_net_path, 'r')
            row = r.readline()
            while row:
                e0, e1, _ = utils.parse_time_line(row)
                e0, e1 = np.sort([int(e0), int(e1)])
                try:
                    ov = round(netanalysis.overlap(net, e0, e1), 4)
                except:
                    ov = 0.0
                line = [str(e0), str(e1), str(ov)]
                f.write(' '.join(line) + "\n")
                row = r.readline()
            r.close()
        else:
            for edge in net.edges:
                e0, e1, _ = edge
                e0, e1 = np.sort([e0, e1])
                ov = round(netanalysis.overlap(net, e0, e1), 4)
                line = [str(e0), str(e1), str(ov)]
                f.write(' '.join(line) + "\n")
        f.close()
    else:
        ov_net = pynet.SymmNet()
        for edge in net.edges:
            e_0 = edge[0]
            e_1 = edge[1]
            o = netanalysis.overlap(net, e_0, e_1)
            if o > 0:
                ov_net[e_0, e_1] = o
        return ov_net
    return True
示例#10
0
def get_times(edges, times_path='../full_run/times_dict.txt'):
    """
    Given the edge set from get_edge_set, gets the timestamps for those edges
    """
    import utils
    times_dict = {}
    edge_set = set([
        tuple(x) for sublist in edges.values() for y in sublist.values()
        for x in y
    ])
    with open(times_path, 'r') as r:
        row = r.readline()
        while (row is not None) & (len(edge_set) > 0):
            e0, e1, times = utils.parse_time_line(row)
            edge = (e0, e1)
            if edge in edge_set:
                times_dict[edge] = times
                edge_set.remove(edge)
            row = r.readline()
    return times_dict