Beispiel #1
0
def labcdefg():
    subway = nx.Graph()

    STATIONS = [tuple(['a', 0, 10, 0, 1, ['1']]), tuple(['b', 10, 10, 0, 1, ['1']]), tuple(['c', 20, 10, 0, 1, ['1','2']]), tuple(['d', 30, 10, 0, 1, ['1']]),tuple(['e', 20, 30, 0, 1, ['2']]), tuple(['f', 20, 20, 0, 1, ['2']]), tuple(['g', 20, 0, 0, 1, ['2']])]

    stations = list()

    for S in STATIONS:
        stations.append(tools.LineStation(*S))

    for s in stations:
        subway.add_node(s)

    lines = { '1': tools.Line('1',stations[0:4]),
              '2': tools.Line('2', [stations[4],stations[5],stations[2],stations[6]] )}

    for l in lines.values():
        for pair in pairwise(l.route):
            dist = np.linalg.norm([pair[0].xy,pair[1].xy])
            subway.add_edge(*pair, distance=dist)


    train1 = tools.Train('1-1', 30, lines['1'], stations[0], 1, 1.0, verbose=0)
    train2 = tools.Train('1-2', 30, lines['1'], stations[1], 1, 1.0, verbose=0)
    train3 = tools.Train('1-3', 30, lines['1'], stations[2], 1, 1.0, verbose=0)
    train4 = tools.Train('1-4', 30, lines['1'], stations[3], 1, 1.0, verbose=1)


    trains = [train1,train2,train3,train4]



    return subway,lines,stations,trains
Beispiel #2
0
def clean_db_gps(db, max_dist=200, min_dist=100, min_len=10, max_again=10, debug=False):
    dbs = dict()
    idx = 0
    for db_i in range(len(db)):
        traj = db[db_i]
        ranges = [0]
        for i in range(len(traj)-1):
            lat1, lng1 = traj[i][0], traj[i][1]
            lat2, lng2 = traj[i + 1][0], traj[i + 1][1]
            x1, y1, _, _ = utm.from_latlon(float(lat1), float(lng1))
            x2, y2, _, _ = utm.from_latlon(float(lat2), float(lng2))
            dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
            if dist > max_dist:
                ranges.append(i + 1)
        ranges.append(len(traj))
        out = 'Traj ID=%d\n' % db_i
        for a, b in tools.pairwise(ranges):
            bounding = bounding_box(traj, a, b)
            if b - a < min_len or bounding < min_dist:
                out += 'discard[%d:%d]\t' % (a, b-1)
                continue
            c, d, delta = trim_traj(traj, a, b)
            dbs[idx] = traj[c:d] if delta > max_again else traj[a:b]
            out += 'keep[%d:%d]as id=%d\t' % (c, d-1, idx)
            idx += 1
        if debug:
            print out
    return dbs
Beispiel #3
0
def abcd():
    subway = nx.Graph()

    STATIONS = [tuple(['a', 0, 10, 0, 1]), tuple(['b', 10, 10, 0, 1]), tuple(['c', 20, 10, 0, 1]), tuple(['d', 30, 10, 0, 1])]

    stations = list()

    for S in STATIONS:
        stations.append(tools.BasicStation(*S))

    for s in stations:
        subway.add_node(s)

    lines = { '1': tools.Line('1',stations[0:4]),
            }

    for l in lines.values():
        for pair in pairwise(l.route):
            dist = np.linalg.norm([pair[0].xy,pair[1].xy])
            subway.add_edge(*pair, distance=dist)

    train1 = tools.Train('1-1', 30, lines['1'], stations[0], 1, 1.0, verbose=0)
    train2 = tools.Train('1-2', 30, lines['1'], stations[1], -1, 1.0, verbose=0)
    trains = [train1,train2]

    return subway,lines,stations,trains
Beispiel #4
0
def clean_db(db, max_dist=200, min_dist=100, min_len=10, max_again=10, debug=False):
    dbs = dict()
    idx = 0
    for db_i in range(len(db)):
        traj = db[db_i]
        ranges = [0]
        out = 'Traj ID=%d\n' % db_i
        for i in range(len(traj)-1):
            x1, y1 = traj[i][0], traj[i][1]
            x2, y2 = traj[i + 1][0], traj[i + 1][1]
            dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
            if dist > max_dist:
                out += '%d->%d:%sm\t' % (i, i+1, int(dist))
                ranges.append(i + 1)
        ranges.append(len(traj))
        out += '\n'
        for a, b in tools.pairwise(ranges):
            bounding = bounding_box(traj, a, b)
            if b - a < min_len or bounding < min_dist:
                out += 'discard[%d:%d]=%dm\t' % (a, b-1, int(bounding))
                continue
            c, d, delta = trim_traj(traj, a, b)
            dbs[idx] = traj[c:d] if delta > max_again else traj[a:b]
            if delta > max_again:
                out += 'trimed[%d,%d] ' % (c - a, b - d)
                out += 'keep[%d:%d]as id=%d\t' % (c, d-1, idx)
            else:
                out += 'keep[%d:%d]as id=%d\t' % (a, b, idx)
            idx += 1
        if debug:
            print out
    return dbs
Beispiel #5
0
def _compute_scores(gradients, f, m, **kwargs):
  """ Multi-Krum score computation.
  Args:
    gradients Non-empty list of gradients to aggregate
    f         Number of Byzantine gradients to tolerate
    m         Optional number of averaged gradients for Multi-Krum
    ...       Ignored keyword-arguments
  Returns:
    List of (gradient, score) by sorted (increasing) scores
  """
  n = len(gradients)
  # Compute all pairwise distances
  distances = [0] * (n * (n - 1) // 2)
  for i, (x, y) in enumerate(tools.pairwise(tuple(range(n)))):
    dist = gradients[x].sub(gradients[y]).norm().item()
    if not math.isfinite(dist):
      dist = math.inf
    distances[i] = dist
  # Compute the scores
  scores = list()
  for i in range(n):
    # Collect the distances
    grad_dists = list()
    for j in range(i):
      grad_dists.append(distances[(2 * n - j - 3) * j // 2 + i - 1])
    for j in range(i + 1, n):
      grad_dists.append(distances[(2 * n - i - 3) * i // 2 + j - 1])
    # Select the n - f - 1 smallest distances
    grad_dists.sort()
    scores.append((sum(grad_dists[:n - f - 1]), gradients[i]))
  # Sort the gradients by increasing scores
  scores.sort(key=lambda x: x[0])
  return scores
Beispiel #6
0
def paper_draw_traj(traj, out_fname):
    c_lat, c_lng = traj[0][:2]
    html = const_prefix % (c_lat, c_lng)

    # cut points
    last_id = -1
    ranges = []
    for i in range(len(traj)):
        cur_id = traj[i][2]
        if last_id != cur_id:
            ranges.append(i)
        last_id = cur_id
    ranges.append(len(traj))
    rid = 0
    for a, b in tools.pairwise(ranges):
        p_str = []
        coors = '['
        for idx, point in enumerate(traj[a:b]):
            lat, lng = float(point[0]), float(point[1])
            marker = '[%s, %s]' % (lat, lng)
            p_str.append(marker)
        coors += ', '.join(p_str) + ']'
        html += const_coor % (rid, coors)
        html += const_path % ('coor_' + str(rid), colors[rid % len(colors)])
        rid += 1

    for idx in ranges:
        point = traj[idx] if idx < len(traj) else traj[idx - 1]
        lat, lng = float(point[0]), float(point[1])
        marker = '[%s, %s]' % (lat, lng)
        html += const_circle % (marker, 'red', 5)
    html += const_suffix
    with open(out_fname, 'w') as o_file:
        o_file.write(html)
Beispiel #7
0
def clean_data(df, max_dist, min_dist, min_len, max_again, debug=False):
    max_trid = max(df['TrajID'])
    dbs = dict()
    idx = 0
    for i in range(max_trid + 1):
        df_i_idx = df[df['TrajID'] == i].index
        ranges = [df_i_idx[0]]
        out = 'Traj ID=%d\n' % i
        for p, q in tools.pairwise(df_i_idx):
            x1, y1 = df.loc[p]['UTM_X'], df.loc[p]['UTM_Y']
            x2, y2 = df.loc[q]['UTM_X'], df.loc[q]['UTM_Y']
            dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
            if dist > max_dist:
                out += '%d->%d:%sm\t' % (p, q, int(dist))
                ranges.append(q)
        ranges.append(df_i_idx[-1] + 1)
        out += '\n'
        remain = []
        for a, b in tools.pairwise(ranges):
            bounding = bounding_box(df, a, b)
            if b - a < min_len or bounding < min_dist:
                out += 'discard[%d:%d]=%dm\t' % (a, b - 1, int(bounding))
                continue
            c, d, delta = trim_traj(df, a, b)
            dbs[idx] = (c, d) if delta > max_again else (a, b)
            if delta > max_again:
                out += 'trimed[%d,%d] ' % (c - a, b - d)
                out += 'keep[%d:%d]as id=%d\t' % (c, d - 1, idx)
                remain.append(d - c)
            else:
                out += 'keep[%d:%d]as id=%d\t' % (a, b - 1, idx)
                remain.append(b - a)
            idx += 1
        if debug:
            print(out)
            print(remain)
    categories = []
    for idx, (a, b) in dbs.items():
        df_idx = df.loc[range(a, b)]
        df_idx['TrajID'] = idx
        categories.append(df_idx)
    return pd.concat(categories).reset_index(drop=True)
Beispiel #8
0
    def __call__(self,xs):
        """ Evaluate on some points, using np.piecewise """
        #local access
        edges, imps, chebfuns = self.edges, self.imps, self.chebs

        if len(edges) == 2:
            return chebfuns[0](xs)

        fullfuncs = list( tools.roundrobin( chebfuns, imps ))
        middleconds = ( ( (a<xs) + (xs > b) ) for (a,b) in tools.pairwise( edges[1:-1] ) )
        impulseconds = ( (xs==z) for z in edges[1:-1] )
        conds = [(xs<edges[1])] + list(tools.roundrobin(impulseconds,middleconds)) + [(xs>edges[-2])]
        return piecewise(xs, conds, fullfuncs)
Beispiel #9
0
def _compute_selection(gradients, f, **kwargs):
  """ Brute rule.
  Args:
    gradients Non-empty list of gradients to aggregate
    f         Number of Byzantine gradients to tolerate
    ...       Ignored keyword-arguments
  Returns:
    Selection index set
  """
  n = len(gradients)
  # Compute all pairwise distances
  distances = [0] * (n * (n - 1) // 2)
  for i, (x, y) in enumerate(tools.pairwise(tuple(range(n)))):
    distances[i] = gradients[x].sub(gradients[y]).norm().item()
  # Select the set of smallest diameter
  sel_iset = None
  sel_diam = None
  for cur_iset in itertools.combinations(range(n), n - f):
    # Compute the current diameter (max of pairwise distances)
    cur_diam = 0.
    for x, y in tools.pairwise(cur_iset):
      # Get distance between these two gradients ("magic" formula valid since x < y)
      cur_dist = distances[(2 * n - x - 3) * x // 2 + y - 1]
      # Check finite distance (non-Byzantine gradient must only contain finite coordinates), drop set if non-finite
      if not math.isfinite(cur_dist):
        break
      # Check if new maximum
      if cur_dist > cur_diam:
        cur_diam = cur_dist
    else:
      # Check if new selected diameter
      if sel_iset is None or cur_diam < sel_diam:
        sel_iset = cur_iset
        sel_diam = cur_diam
  # Return the selected gradients
  assert sel_iset is not None, "Too many non-finite gradients: a non-Byzantine gradient must only contain finite coordinates"
  return sel_iset
Beispiel #10
0
def circle():
    """
    This system has 4 lines.
    Line 1: a-b-c-d
    Line 2: e-f-c-g
    CircleLine 3: c-f-i-b-h-g
    Line 4: a-c-e
    """
    subway = nx.Graph()

    STATIONS = [tuple(['a', 0, 10, 0, 1, ['1','4']]), tuple(['b', 10, 10, 0, 1, ['1','3']]), tuple(['c', 20, 10, 0, 1, ['1','2','3','4']]), tuple(['d', 30, 10, 0, 1, ['1']]),tuple(['e', 20, 30, 0, 1, ['2','4']]), tuple(['f', 20, 20, 0, 1, ['2','3']]), tuple(['g', 20, 0, 0, 1, ['2','3']]),tuple(['h', 10, 0, 0, 1, ['3']]),tuple(['i', 10, 20, 0, 1, ['3']])]
    stations = list()

    for S in STATIONS:
        stations.append(tools.LineStation(*S))
    
    for s in stations:
        subway.add_node(s)

    lines = { '1': tools.Line('1',stations[0:4]),
              '2': tools.Line('2', [stations[4],stations[5],stations[2],stations[6]]),
              '3': tools.CircleLine('3', [stations[2], stations[5],stations[8], stations[1],stations[7],stations[6]]),
              '4': tools.Line('4', [stations[0], stations[2], stations[4]])
              }

    for l in lines.values():
        for pair in pairwise(l.route):
            dist = np.linalg.norm([pair[0].xy,pair[1].xy])
            subway.add_edge(*pair, distance=dist)


    train11 = tools.Train('1-1', 300, lines['1'], stations[0], 1, 1.0, verbose=0)
    train12 = tools.Train('1-2', 300, lines['1'], stations[3], -1, 1.0, verbose=0)
    train21 = tools.Train('2-1', 300, lines['2'], stations[4], 1, 1.0, verbose=0)
    train22 = tools.Train('2-2', 300, lines['2'], stations[2], -1, 1.0, verbose=0)
    train31 = tools.Train('3-1', 500, lines['3'], stations[2], 1, 1.0, verbose=0)
    train32 = tools.Train('3-2', 500, lines['3'], stations[6], -1, 1.0, verbose=0)
    train41 = tools.Train('4-1', 300, lines['4'], stations[0], 1, 1.0, verbose=0)
    train42 = tools.Train('4-2', 300, lines['4'], stations[4], -1, 1.0, verbose=0)

    trains = [train11,train12,train21,train22,train31, train32,train41,train42]



    return subway,lines,stations,trains
Beispiel #11
0
def get_trans_mat(match_res):
    A = dict()
    cnt = 0
    for tr_id, piece_data in match_res.iteritems():
        path = [x[3] for x in piece_data]
        for r1, r2 in tools.pairwise(path):
            if r1 not in A.keys():
                A[r1] = dict()
            if r2 not in A[r1].keys():
                A[r1][r2] = 0
            A[r1][r2] += 1
            cnt += 1
    print 'transition %d times' % cnt

    for r1 in A.iterkeys():
        total = sum(prob for prob in A[r1].itervalues()) * 1.0
        for r2 in A[r1].iterkeys():
            A[r1][r2] /= total
    return A
Beispiel #12
0
def draw_points_cells(traj, towers, ca, axis):
    # draw cell towers
    radius = 70
    for ids, info in towers.iteritems():
        rncid, cellid = ids
        index, lat, lng = info[:3]
        x, y, _, _ = utm.from_latlon(lat, lng)
        if tools.outside(x, y, axis):
            continue
        circle = Circle((x, y),
                        radius=radius,
                        color=colors[index % len(colors)],
                        alpha=0.5)
        ca.text(x - radius, y, '[%d]' % index, color='k', fontsize=16)
        ca.add_patch(circle)
    # draw gps points
    last_id = -1
    ranges = []
    for i in range(len(traj)):
        cur_id = traj[i][2]
        if last_id != cur_id:
            ranges.append(i)
        last_id = cur_id
    ranges.append(len(traj))

    for a, b in tools.pairwise(ranges):
        xs, ys = [], []
        for point in traj[a:b]:
            x, y, id_1 = point[:3]
            # print x, y, id_1
            xs.append(x)
            ys.append(y)
        if len(xs) == 1:
            continue
        line = lines.Line2D(xs,
                            ys,
                            linewidth=2,
                            color=colors[id_1 % len(colors)])
        mx, my = xs[len(xs) / 2], ys[len(xs) / 2]
        ca.text(mx, my, '%d' % id_1, color='k', fontsize=12)
        ca.add_line(line)
Beispiel #13
0
def aggregate(gradients, f, m=None, **kwargs):
    """ Multi-Krum rule.
  Args:
    gradients Non-empty list of gradients to aggregate
    f         Number of Byzantine gradients to tolerate
    m         Optional number of averaged gradients for Multi-Krum
    ...       Ignored keyword-arguments
  Returns:
    Aggregated gradient
  """
    n = len(gradients)
    # Defaults
    if m is None:
        m = n - f - 2
    # Compute all pairwise distances
    distances = [0] * (n * (n - 1) // 2)
    for i, (x, y) in enumerate(tools.pairwise(tuple(range(n)))):
        dist = gradients[x].sub(gradients[y]).norm().item()
        if not math.isfinite(dist):
            dist = math.inf
        distances[i] = dist
    # Compute the scores
    scores = list()
    for i in range(n):
        # Collect the distances
        grad_dists = list()
        for j in range(i):
            grad_dists.append(distances[(2 * n - j - 3) * j // 2 + i - 1])
        for j in range(i + 1, n):
            grad_dists.append(distances[(2 * n - i - 3) * i // 2 + j - 1])
        # Select the n - f - 1 smallest distances
        grad_dists.sort()
        scores.append((sum(grad_dists[:n - f - 1]), gradients[i]))
    # Compute the average of the selected gradients
    scores.sort(key=lambda x: x[0])
    return sum(grad for _, grad in scores[:m]).div_(m)
Beispiel #14
0
def plot_lag_energy(out_root, energies_tab, plot_ext, prefix, phase, err_phase,
        tlag, err_tlag, lo_freq, up_freq, detchans=64):
    """
    Plots the lag-energy spectrum.

    """
    font_prop = font_manager.FontProperties(size=18)
    energy_list = [np.mean([x, y]) for x,y in tools.pairwise(energies_tab)]
    energy_err = [np.abs(a-b) for (a,b) in zip(energy_list, energies_tab[0:-1])]
    e_chans = np.arange(0, detchans)
    name="lag-energy_%.2f-%.2fHz." %(lo_freq,up_freq)
    plot_file = out_root +"_" + name + plot_ext
    print "Lag-energy spectrum: %s" % plot_file

    ## Deleting the values at energy channel 10 for RXTE PCA event-mode data
    ## No counts detected in channel 10 in this data mode
    if detchans == 64 and len(phase) == 64:
        phase = np.delete(phase, 10)
        err_phase = np.delete(err_phase, 10)
        tlag = np.delete(tlag, 10)
        err_tlag = np.delete(err_tlag, 10)
        e_chans = np.delete(e_chans, 10)
        energy_list = np.delete(energy_list, 10)
        energy_err = np.delete(energy_err, 10)

		#####################
#	writes ascii file.dat with lag energy spectrum!
		#####################
    data_name=name +"dat" 
    data=[energy_list[2:26], tlag[2:26],energy_err[2:26],err_tlag[2:26]]
    print data
    ascii.write([energy_list[2:26], tlag[2:26],energy_err[2:26],err_tlag[2:26]],
            data_name,exclude_names=['y'])

#    data_name = data_name.replace("/lag_spectra/", "/lag_spectra/out_lags/GX339-4")

#    col1i = fits.Column(name='ENERGY',format= 'D', energy_list[2:26])
#    col2i = fits.Column(name='TIME_LAGS',format= 'D', array=tlag[2:26])
#    col3i = fits.Column(name='ERR_ENERGY',format= 'D', array=energy_err[2:26])
#    col4i = fits.Column(name='ERROR_TLAGS',format= 'D', array=err_tlag[2:26])
#    colsi = fits.ColDefs([col1i,col2i,col3i,col4I])
#    tbhdu = fits.BinTableHDU.from_columns(colsi)
#    thdulist = fits.HDUList([tbhdu])
#    thdulist.writeto('lag_energy.fits')

    #############
    ## Plotting!
    #############
    fig, ax = plt.subplots(1, 1, figsize=(10,7.5), dpi=300, tight_layout=True)

    ax.hlines(0.0, 3, 21, linestyle='dashed', lw=2, color='gray')
    ax.errorbar(energy_list[2:26], tlag[2:26], xerr=energy_err[2:26],
            yerr=err_tlag[2:26], ls='none', marker='o', ms=5, mew=2,
            mec='black', mfc='black', ecolor='black', elinewidth=2, capsize=0)

    # ax.errorbar(energy_list[5:200], tlag[5:200], xerr=energy_err[5:200], yerr=err_tlag[5:200], ls='none',
    #         marker='o', ms=5, mew=2, mec='black', mfc='black', ecolor='black',
    #         elinewidth=2, capsize=0)
    # ax.errorbar(energy_list, phase, xerr=energy_err, yerr=err_phase, ls='none',
    #         marker='+', ms=8, mew=2, mec='black', ecolor='black', elinewidth=2,
    #         capsize=0)

    ax.set_xlabel('Energy (keV)', fontproperties=font_prop)
    ax.set_xlim(3,21)
    # ax.set_xlim(0.3, 10)
    ax.set_xscale('log')
    x_maj_loc = [5,10,20]
    y_maj_loc = [-0.15, -0.1,-0.05, 0, 0.05, 0.1, 0.15]
    ax.set_xticks(x_maj_loc)
    ax.set_yticks(y_maj_loc)
    xLocator = MultipleLocator(1)  ## loc of minor ticks on x-axis
    yLocator = MultipleLocator(0.05)  ## loc of minor ticks on y-axis
    ax.xaxis.set_minor_locator(xLocator)
    ax.yaxis.set_minor_locator(yLocator)
    ax.xaxis.set_major_formatter(ScalarFormatter())

    ax.set_ylabel('Time lag (s)', fontproperties=font_prop)
    # ax.set_ylabel('Phase lag (radians)', fontproperties=font_prop)
    # ax.set_ylim(1.3 * np.min(tlag[2:25]), 1.30 * np.max(tlag[2:25]))
    ax.set_ylim(-0.17, 0.17)
    # ax.set_ylim(-0.4, 0.5)
    ax.tick_params(axis='x', labelsize=18)
    ax.tick_params(axis='y', labelsize=18)
    title = "Lag-energy spectrum, %s, %.2f - %.2f Hz" % (prefix, lo_freq,
                                                         up_freq)
    # ax.set_title(title, fontproperties=font_prop)

    plt.savefig(plot_file)
    # 	plt.show()
    plt.close()
Beispiel #15
0
def nyc(add=False, block="none"):
    subway = nx.Graph()

    # filling up stations
    STATIONS = []
    with open('../listOfStationsConverted.txt') as f:
        #skip header at the first row
        f.readline()
        rows = (line.strip().split("\t") for line in f)
        # row[0]: name key
        # row[5]: x position
        # row[4]: y position
        # row[3]: xy grid, determines +/- sign of x and y
        # min passenger: 0
        # max passenger: 1
        # row[0].split(";")[-1]: line
        for row in rows:
            STATIONS.append(
                tuple([
                    row[0],
                    (int(row[5]) if
                     (row[3][-2] == 'X') else int(row[5]) - 100000),
                    (int(row[4]) if
                     (row[3][-1] == 'L') else int(row[4]) - 100000), 0, 1,
                    list(row[0].split(";")[-1])
                ]))

    stations = dict()

    for S in STATIONS:
        stations[S[0]] = tools.LineStation(*S)

    for s in stations.itervalues():
        subway.add_node(s)

# filling up lines
    lines = dict()
    lines_information = {
        '1': [],
        '2': [],
        '3': [],
        '4': [],
        '5': [],
        '6': [],
        '7': [],
        'A': [],
        'B': [],
        'C': [],
        'D': [],
        'E': [],
        'F': [],
        'G': [],
        'H': [],
        'J': [],
        'L': [],
        'M': [],
        'N': [],
        'Q': [],
        'R': [],
        'S': [],
        's': [],
        'Z': []
    }

    with open('../listOfLines.txt') as f:
        rows = (line.strip().split("\t") for line in f)
        # row[0]: line
        # row[1:]: sequence of stations from north to south
        for row in rows:
            lines_information[row[0]] = row[1:]

# construct a new line
    if add:
        lines_information['8'] = [
            ';4567S', ';7BDFM', ';1237ACENQRS', '34th St;ACE', '34th St;123',
            ';BDFMNQR', '33rd St;6', '28th St;6', '23rd St;6', ';456LNQR',
            ';123FLM', ';ACEL', '23rd St;CE', '34th St;ACE', '34th St;123',
            ';BDFMNQR', '33rd St;6'
        ]

# block a line
    if block == "all":
        del lines_information['R']
    elif block == "nqr":
        lines_information['N'] = [
            ';2345BDNQR', '36th St;DNR', '59th St;NR', '8th Av;N',
            'Fort Hamilton Parkway;N', ';DN', '18th Av;N', '20th Av;N',
            'Bay Parkway-22nd Av;N', 'Kings Highway;N', 'Av U;N', '86th St;N',
            ';DFNQ'
        ]
        lines_information['Q'] = [
            'DeKalb Av;BQR', ';2345BDNQR', '7th Av;BQ', 'Prospect Park;BQs',
            'Parkside Av;Q', 'Church Av;BQ', 'Beverly Rd;Q', 'Cortelyou Rd;Q',
            'Newkirk Av;BQ', 'Av H;Q', 'Av J;Q', 'Av M;Q', 'Kings Highway;BQ',
            'Av U;Q', 'Neck Rd;Q', 'Sheepshead Bay;BQ', 'Brighton Beach;BQ',
            'Ocean Parkway;Q', 'West 8th St;FQ', ';DFNQ'
        ]
        lines_information['R'] = [
            ';2345R', ';ACFR', 'DeKalb Av;BQR', ';2345BDNQR', 'Union St;R',
            ';FGR', 'Prospect Av;R', '25th St;R', '36th St;DNR', '45th St;R',
            '53rd St;R', '59th St;NR', 'Bay Ridge Av;R', '77th St;R',
            '86th St;R', '95th St;R'
        ]
    else:
        pass

    for key, value in lines_information.iteritems():
        stations_list = []
        for j in value:
            stations_list.append(stations[j])
        # line '8' is a circle line
        if key == '8':
            lines[key] = tools.CircleLine(key, stations_list)
        else:
            lines[key] = tools.Line(key, stations_list)

    # add edges
    for l in lines.values():
        for pair in pairwise(l.route):
            dist = np.linalg.norm([pair[0].xy, pair[1].xy])
            subway.add_edge(*pair, distance=dist)

# filling up trains
    trains = []
    # forward direction
    for key in lines_information.iterkeys():
        iterating_group = range(0, (len(lines_information[key]) + 3) / 5)
        serial = 1
        for j in iterating_group:
            trains.append(
                tools.Train(name=key + '-' + str(serial),
                            capacity=30,
                            line=lines[key],
                            start=stations[lines_information[key][5 * j]],
                            direction=1,
                            velocity=100.0,
                            verbose=0))
            serial += 1
        # reverse direction
        iterating_group.reverse()
        for j in iterating_group:
            trains.append(
                tools.Train(name=key + '-' + str(serial),
                            capacity=30,
                            line=lines[key],
                            start=stations[lines_information[key][5 * j]],
                            direction=-1,
                            velocity=100.0,
                            verbose=0))
            serial += 1

    return subway, lines, stations, trains
Beispiel #16
0
def aggregate(gradients, f, m=None, **kwargs):
    """ Bulyan over Multi-Krum rule.
  Args:
    gradients Non-empty list of gradients to aggregate
    f         Number of Byzantine gradients to tolerate
    m         Optional number of averaged gradients for Multi-Krum
    ...       Ignored keyword-arguments
  Returns:
    Aggregated gradient
  """
    n = len(gradients)
    d = gradients[0].shape[0]
    # Defaults
    m_max = n - f - 2
    if m is None:
        m = m_max
    # Compute all pairwise distances
    distances = list([(math.inf, None)] * n for _ in range(n))
    for gid_x, gid_y in tools.pairwise(tuple(range(n))):
        dist = gradients[gid_x].sub(gradients[gid_y]).norm().item()
        if not math.isfinite(dist):
            dist = math.inf
        distances[gid_x][gid_y] = (dist, gid_y)
        distances[gid_y][gid_x] = (dist, gid_x)
    # Compute the scores
    scores = [None] * n
    for gid in range(n):
        dists = distances[gid]
        dists.sort(key=lambda x: x[0])
        dists = dists[:m]
        scores[gid] = (sum(dist for dist, _ in dists), gid)
        distances[gid] = dict(dists)
    # Selection loop
    selected = torch.empty(n - 2 * f - 2,
                           d,
                           dtype=gradients[0].dtype,
                           device=gradients[0].device)
    for i in range(selected.shape[0]):
        # Update 'm'
        m = min(m, m_max - i)
        # Compute the average of the selected gradients
        scores.sort(key=lambda x: x[0])
        selected[i] = sum(gradients[gid] for _, gid in scores[:m]).div_(m)
        # Remove the gradient from the distances and scores
        gid_prune = scores[0][1]
        scores[0] = (math.inf, None)
        for score, gid in scores[1:]:
            if gid == gid_prune:
                scores[gid] = (score - distance[gid][gid_prune], gid)
    # Coordinate-wise averaged median
    m = selected.shape[0] - 2 * f
    median = selected.median(dim=0).values
    closests = selected.clone().sub_(median).abs_().topk(m,
                                                         dim=0,
                                                         largest=False,
                                                         sorted=False).indices
    closests.mul_(d).add_(
        torch.arange(0, d, dtype=closests.dtype, device=closests.device))
    avgmed = selected.take(closests).mean(dim=0)
    # Return resulting gradient
    return avgmed
Beispiel #17
0
def plot_lag_energy(out_root,
                    energies_tab,
                    plot_ext,
                    prefix,
                    energy_lags,
                    detchans=64):
    """
    Plots the lag-energy spectrum.

    Parameters
    ----------
    out_root : str
        Dir+base name for plots generated, to be appended with '_lag-energy.
        (plot_ext)'.

    plot_ext : str
        File extension for the plots. Do not include the dot.

    prefix : str
        Identifying prefix of the data (object nickname or data ID).

    energy_lags : astropy Table

    detchans : int


    Returns
    -------
    Nothing, but saves a plot to '*_lag-energy.[plot_ext]'.

    """
    font_prop = font_manager.FontProperties(size=20)

    energy_list = []
    energy_err = []
    if energies_tab is not None:
        energy_list = [
            np.mean([x, y]) for x, y in tools.pairwise(energies_tab)
        ]
        energy_err = [
            np.abs(a - b) for (a, b) in zip(energy_list, energies_tab[0:-1])
        ]

    plot_file = out_root + "_lag-energy." + plot_ext
    print "Lag-energy spectrum: %s" % plot_file

    ## Deleting the values at energy channel 10 for RXTE PCA event-mode data
    ## No counts detected in channel 10 in this data mode
    if detchans == 64 and len(energy_lags['PHASE_LAG']) == 64:
        energy_lags.remove_row(10)
        energy_list = np.delete(energy_list, 10)
        energy_err = np.delete(energy_err, 10)

    #############
    ## Plotting!
    #############

    fig, ax = plt.subplots(1, 1, figsize=(10, 7.5), dpi=300, tight_layout=True)
    # ax.plot([0,detchans],[0,0], lw=1.5, ls='dashed', c='black')
    # 	ax.plot([0,detchans],[np.pi,np.pi], lw=1.5, ls='dashed', c='black')
    # 	ax.plot([0,detchans],[-np.pi,-np.pi], lw=1.5, ls='dashed', c='black')
    # 	ax.errorbar(e_chans, phase, yerr=err_phase, lw=3, c='red', \
    # 		ls="steps-mid", elinewidth=2, capsize=2)
    # ax.errorbar(e_chans, tlag, yerr=err_tlag, ls='none', marker='x', \
    #             ms=10, mew=2, mec='red', ecolor='red', \
    #             elinewidth=2, capsize=2)
    # ax.set_xlabel('Energy channel (0-%d)' % (detchans - 1), \
    #               fontproperties=font_prop)
    # ax.set_xlim(1.5, 25.5)

    # print energy_list
    if len(energy_list) > 0:
        ax.hlines(0.0, 3, 20.5, linestyle='dashed', lw=2, color='black')

        ax.errorbar(energy_list[2:26],
                    energy_lags['TIME_LAG'][2:26],
                    xerr=energy_err[2:26],
                    yerr=energy_lags['TIME_ERR'][2:26],
                    ls='none',
                    marker='o',
                    ms=10,
                    mew=2,
                    mec='black',
                    mfc='black',
                    ecolor='black',
                    elinewidth=2,
                    capsize=0)
        ax.set_xlim(3, 21)
        ax.set_xlabel('Energy (keV)', fontproperties=font_prop)
        ax.set_xscale('log')
        x_maj_loc = [5, 10, 20]
        ax.set_xticks(x_maj_loc)
        ax.xaxis.set_major_formatter(ScalarFormatter())

    else:
        ax.hlines(0.0,
                  energy_lags['CHANNEL'][0],
                  energy_lags['CHANNEL'][-1],
                  linestyle='dashed',
                  lw=2,
                  color='black')

        ax.errorbar(energy_lags['CHANNEL'],
                    energy_lags['TIME_LAG'],
                    yerr=energy_lags['TIME_ERR'],
                    ls='none',
                    marker='o',
                    ms=10,
                    mew=2,
                    mec='black',
                    mfc='black',
                    ecolor='black',
                    elinewidth=2,
                    capsize=0)
        ax.set_xlabel('Energy channel', fontproperties=font_prop)

    # ax.errorbar(energy_list[5:200], tlag[5:200], xerr=energy_err[5:200],
    #       yerr=err_tlag[5:200], ls='none',
    #         marker='o', ms=5, mew=2, mec='black', mfc='black', ecolor='black',
    #         elinewidth=2, capsize=0)
    # ax.errorbar(energy_list, phase, xerr=energy_err, yerr=err_phase, ls='none',
    #         marker='+', ms=8, mew=2, mec='black', ecolor='black', elinewidth=2,
    #         capsize=0)

    ax.set_ylim(-0.01, 0.017)
    # ax.set_ylim(1.3 * np.min(tlag[2:25]), 1.30 * np.max(tlag[2:25]))

    # y_maj_loc = [-0.005, 0, 0.005, 0.01, 0.015]
    # ax.set_yticks(y_maj_loc)
    xLocator = MultipleLocator(1)  ## loc of minor ticks on x-axis
    yLocator = MultipleLocator(0.001)  ## loc of minor ticks on y-axis
    ax.xaxis.set_minor_locator(xLocator)
    ax.yaxis.set_minor_locator(yLocator)
    ax.tick_params(which='major', width=1.5, length=7)
    ax.tick_params(which='minor', width=1.5, length=4)
    for axis in ['top', 'bottom', 'left', 'right']:
        ax.spines[axis].set_linewidth(1.5)

    ax.set_ylabel('Time lag (s)', fontproperties=font_prop)
    # ax.set_ylabel('Phase lag (radians)', fontproperties=font_prop)
    ax.tick_params(axis='x', labelsize=20)
    ax.tick_params(axis='y', labelsize=20)
    title = "Lag-energy spectrum, %s, %.2f - %.2f Hz" % (
        prefix, energy_lags.meta['LO_FREQ'], energy_lags.meta['UP_FREQ'])
    # ax.set_title(title, fontproperties=font_prop)

    plt.savefig(plot_file)
    # 	plt.show()
    plt.close()
Beispiel #18
0
def discard_pattern(pattern):
    if len(pattern) > 1:
        for dir1, dir2 in tools.pairwise(pattern):
            if abs(dir1 - dir2) == 4:
                return True
    return False
Beispiel #19
0
def pattern2label(patterns, grid, version=0):
    labels = []
    discard_idxs = []
    # local direction
    if version == 0:
        for pattern in patterns:
            t_vx, t_vy = 0, 0
            for c1, c2 in tools.pairwise(pattern):
                vx, vy = c2[0] - c1[0], c2[1] - c1[1]
                t_vx += vx
                t_vy += vy
            labels.append(decide_label(t_vx, t_vy))
        pattern_set = [0, 1, 2, 3, 4, 5, 6, 7, 8]
    # grid sequence
    elif version == 1:
        new_patterns = []
        for pattern in patterns:
            temp = [grid.cell2index(cid) for cid in pattern]
            xs = [idx[0] for idx in temp]
            ys = [idx[1] for idx in temp]
            x0, y0 = min(xs), min(ys)
            cid0 = grid.index2cell(x0, y0)
            new_pattern = []
            for idx in temp:
                if len(new_pattern) == 0 or new_pattern[-1] != (idx[0] - x0,
                                                                idx[1] - y0):
                    new_pattern.append((idx[0] - x0, idx[1] - y0))
            new_patterns.append(tuple(new_pattern))
        print('Origin Labels:', len(new_patterns))
        pattern_set = list(set(new_patterns))
        print('Merged labels:', len(pattern_set))
        labels = [pattern_set.index(pattern) for pattern in new_patterns]
    # direction change
    elif version == 2:
        new_patterns = []
        len_stat = []
        for idx, pattern in enumerate(patterns):
            new_pattern = []
            x0, x1, y0, y1 = tools.bounding_box(pattern)
            length = math.sqrt((x1 - x0)**2 + (y1 - y0)**2)
            len_stat.append(length)
            # stationary
            if length < 5.0:
                new_pattern.append(0)
            # moving
            else:
                for c1, c2 in tools.pairwise(pattern):
                    vx, vy = c2[0] - c1[0], c2[1] - c1[1]
                    local_dir = decide_label(vx, vy)
                    if local_dir == 0:
                        continue
                    if len(new_pattern) == 0 or new_pattern[-1] != local_dir:
                        new_pattern.append(local_dir)
                if len(new_pattern) == 0:
                    new_pattern.append(0)
                if discard_pattern(new_pattern):
                    discard_idxs.append(idx)
                    continue
            # if len(new_pattern) > 2:
            #     new_pattern = new_pattern[:2]
            new_patterns.append(tuple(new_pattern))
        print('Origin Labels:', len(new_patterns))
        pattern_set = list(set(new_patterns))
        print('Merged labels:', len(pattern_set))
        labels = [pattern_set.index(pattern) for pattern in new_patterns]
    return labels, pattern_set, discard_idxs, len_stat
Beispiel #20
0
def nyc(add=False, block="none"):
    subway = nx.Graph()
    
# filling up stations    
    STATIONS = []
    with open('../listOfStationsConverted.txt') as f:
        #skip header at the first row
        f.readline()
        rows = (line.strip().split("\t") for line in f)
        # row[0]: name key
        # row[5]: x position
        # row[4]: y position
        # row[3]: xy grid, determines +/- sign of x and y
        # min passenger: 0
        # max passenger: 1
        # row[0].split(";")[-1]: line
        for row in rows:
            STATIONS.append(tuple([
            row[0],
            (int(row[5]) if (row[3][-2] == 'X') else int(row[5]) - 100000),
            (int(row[4]) if (row[3][-1] == 'L') else int(row[4]) - 100000),
            0,
            1,
            list(row[0].split(";")[-1])
            ]))

    stations = dict()
    
    for S in STATIONS:
        stations[S[0]] = tools.LineStation(*S)

    for s in stations.itervalues():
        subway.add_node(s)

# filling up lines
    lines = dict()
    lines_information = {'1': [], '2': [], '3': [], '4': [], '5': [], '6': [],
                         '7': [], 'A': [], 'B': [], 'C': [], 'D': [], 'E': [],
                         'F': [], 'G': [], 'H': [], 'J': [], 'L': [], 'M': [],
                         'N': [], 'Q': [], 'R': [], 'S': [], 's': [], 'Z': []}

    with open('../listOfLines.txt') as f:
        rows = (line.strip().split("\t") for line in f)
        # row[0]: line
        # row[1:]: sequence of stations from north to south
        for row in rows:
            lines_information[row[0]] = row[1:]

# construct a new line
    if add:
        lines_information['8'] = [';4567S', ';7BDFM', ';1237ACENQRS', '34th St;ACE',
                                  '34th St;123', ';BDFMNQR', '33rd St;6', '28th St;6',
                                  '23rd St;6', ';456LNQR', ';123FLM', ';ACEL',
                                  '23rd St;CE', '34th St;ACE', '34th St;123', ';BDFMNQR',
                                  '33rd St;6']

# block a line
    if block == "all":
        del lines_information['R']
    elif block == "nqr":
        lines_information['N'] = [';2345BDNQR', '36th St;DNR', '59th St;NR', '8th Av;N',
                                  'Fort Hamilton Parkway;N', ';DN', '18th Av;N', '20th Av;N',
                                  'Bay Parkway-22nd Av;N', 'Kings Highway;N', 'Av U;N',
                                  '86th St;N', ';DFNQ']
        lines_information['Q'] = ['DeKalb Av;BQR', ';2345BDNQR', '7th Av;BQ',
                                  'Prospect Park;BQs', 'Parkside Av;Q', 'Church Av;BQ',
                                  'Beverly Rd;Q', 'Cortelyou Rd;Q', 'Newkirk Av;BQ', 'Av H;Q',
                                  'Av J;Q', 'Av M;Q', 'Kings Highway;BQ', 'Av U;Q',
                                  'Neck Rd;Q', 'Sheepshead Bay;BQ', 'Brighton Beach;BQ',
                                  'Ocean Parkway;Q', 'West 8th St;FQ', ';DFNQ']
        lines_information['R'] = [';2345R', ';ACFR', 'DeKalb Av;BQR', ';2345BDNQR',
                                  'Union St;R', ';FGR', 'Prospect Av;R', '25th St;R',
                                  '36th St;DNR', '45th St;R', '53rd St;R', '59th St;NR',
                                  'Bay Ridge Av;R', '77th St;R', '86th St;R', '95th St;R']
    else:
        pass

    for key, value in lines_information.iteritems():
        stations_list = []
        for j in value:
            stations_list.append(stations[j])
        # line '8' is a circle line
        if key == '8':
            lines[key] = tools.CircleLine(key, stations_list)
        else:
            lines[key] = tools.Line(key, stations_list)

    # add edges
    for l in lines.values():
        for pair in pairwise(l.route):
            dist = np.linalg.norm([pair[0].xy,pair[1].xy])
            subway.add_edge(*pair, distance=dist)

# filling up trains
    trains = []
    # forward direction
    for key in lines_information.iterkeys():
        iterating_group = range(0, (len(lines_information[key]) + 3) / 5)
        serial = 1
        for j in iterating_group:
            trains.append(tools.Train(name=key + '-' + str(serial),
                                      capacity=1000,
                                      line=lines[key],
                                      start=stations[lines_information[key][5 * j]],
                                      direction=1, velocity=100.0,
                                      verbose=0))
            serial += 1
        # reverse direction
        iterating_group.reverse()
        for j in iterating_group:
            trains.append(tools.Train(name=key + '-' + str(serial),
                                      capacity=1000,
                                      line=lines[key],
                                      start=stations[lines_information[key][5 * j]],
                                      direction=-1, velocity=100.0,
                                      verbose=0))
            serial += 1

    return subway,lines,stations,trains
Beispiel #21
0
def plot_lag_energy(out_root, energies_tab, plot_ext, prefix, energy_lags,
                    detchans=64):
    """
    Plots the lag-energy spectrum.

    Parameters
    ----------
    out_root : str
        Dir+base name for plots generated, to be appended with '_lag-energy.
        (plot_ext)'.

    plot_ext : str
        File extension for the plots. Do not include the dot.

    prefix : str
        Identifying prefix of the data (object nickname or data ID).

    energy_lags : astropy Table

    detchans : int


    Returns
    -------
    Nothing, but saves a plot to '*_lag-energy.[plot_ext]'.

    """
    font_prop = font_manager.FontProperties(size=20)

    energy_list = []
    energy_err = []
    if energies_tab is not None:
        energy_list = [np.mean([x, y]) for x,y in tools.pairwise(energies_tab)]
        energy_err = [np.abs(a-b) for (a,b) in zip(energy_list,
                                                   energies_tab[0:-1])]

    plot_file = out_root + "_lag-energy." + plot_ext
    print "Lag-energy spectrum: %s" % plot_file

    ## Deleting the values at energy channel 10 for RXTE PCA event-mode data
    ## No counts detected in channel 10 in this data mode
    if detchans == 64 and len(energy_lags['PHASE_LAG']) == 64:
        energy_lags.remove_row(10)
        energy_list = np.delete(energy_list, 10)
        energy_err = np.delete(energy_err, 10)

    #############
    ## Plotting!
    #############

    fig, ax = plt.subplots(1, 1, figsize=(10,7.5), dpi=300, tight_layout=True)
    # ax.plot([0,detchans],[0,0], lw=1.5, ls='dashed', c='black')
    # 	ax.plot([0,detchans],[np.pi,np.pi], lw=1.5, ls='dashed', c='black')
    # 	ax.plot([0,detchans],[-np.pi,-np.pi], lw=1.5, ls='dashed', c='black')
    # 	ax.errorbar(e_chans, phase, yerr=err_phase, lw=3, c='red', \
    # 		ls="steps-mid", elinewidth=2, capsize=2)
    # ax.errorbar(e_chans, tlag, yerr=err_tlag, ls='none', marker='x', \
    #             ms=10, mew=2, mec='red', ecolor='red', \
    #             elinewidth=2, capsize=2)
    # ax.set_xlabel('Energy channel (0-%d)' % (detchans - 1), \
    #               fontproperties=font_prop)
    # ax.set_xlim(1.5, 25.5)


    # print energy_list
    if len(energy_list) > 0:
        ax.hlines(0.0, 3, 20.5, linestyle='dashed', lw=2, color='black')

        ax.errorbar(energy_list[2:26], energy_lags['TIME_LAG'][2:26],
                    xerr=energy_err[2:26], yerr=energy_lags['TIME_ERR'][2:26],
                    ls='none', marker='o', ms=10, mew=2, mec='black',
                    mfc='black', ecolor='black', elinewidth=2, capsize=0)
        ax.set_xlim(3,21)
        ax.set_xlabel('Energy (keV)', fontproperties=font_prop)
        ax.set_xscale('log')
        x_maj_loc = [5,10,20]
        ax.set_xticks(x_maj_loc)
        ax.xaxis.set_major_formatter(ScalarFormatter())

    else:
        ax.hlines(0.0, energy_lags['CHANNEL'][0], energy_lags['CHANNEL'][-1],
                  linestyle='dashed', lw=2, color='black')

        ax.errorbar(energy_lags['CHANNEL'], energy_lags['TIME_LAG'],
                    yerr=energy_lags['TIME_ERR'], ls='none', marker='o', ms=10,
                    mew=2, mec='black', mfc='black', ecolor='black',
                    elinewidth=2, capsize=0)
        ax.set_xlabel('Energy channel', fontproperties=font_prop)

    # ax.errorbar(energy_list[5:200], tlag[5:200], xerr=energy_err[5:200],
    #       yerr=err_tlag[5:200], ls='none',
    #         marker='o', ms=5, mew=2, mec='black', mfc='black', ecolor='black',
    #         elinewidth=2, capsize=0)
    # ax.errorbar(energy_list, phase, xerr=energy_err, yerr=err_phase, ls='none',
    #         marker='+', ms=8, mew=2, mec='black', ecolor='black', elinewidth=2,
    #         capsize=0)

    ax.set_ylim(-0.01, 0.017)
    # ax.set_ylim(1.3 * np.min(tlag[2:25]), 1.30 * np.max(tlag[2:25]))

    # y_maj_loc = [-0.005, 0, 0.005, 0.01, 0.015]
    # ax.set_yticks(y_maj_loc)
    xLocator = MultipleLocator(1)  ## loc of minor ticks on x-axis
    yLocator = MultipleLocator(0.001)  ## loc of minor ticks on y-axis
    ax.xaxis.set_minor_locator(xLocator)
    ax.yaxis.set_minor_locator(yLocator)
    ax.tick_params(which='major', width=1.5, length=7)
    ax.tick_params(which='minor', width=1.5, length=4)
    for axis in ['top', 'bottom', 'left', 'right']:
        ax.spines[axis].set_linewidth(1.5)

    ax.set_ylabel('Time lag (s)', fontproperties=font_prop)
    # ax.set_ylabel('Phase lag (radians)', fontproperties=font_prop)
    ax.tick_params(axis='x', labelsize=20)
    ax.tick_params(axis='y', labelsize=20)
    title = "Lag-energy spectrum, %s, %.2f - %.2f Hz" % (prefix,
                                                         energy_lags.meta[
                                                             'LO_FREQ'],
                                                         energy_lags.meta[
                                                             'UP_FREQ'])
    # ax.set_title(title, fontproperties=font_prop)

    plt.savefig(plot_file)
    # 	plt.show()
    plt.close()
Beispiel #22
0
def make_plot(in_file_list, labels, plot_root):
    """
    Plots multiple lag plots in one figure.

    Parameters
    ----------
    in_file_list : list of str
        1-D list of the lag files to plot.

    labels : list of str
        1-D list of the line labels for each lag. Must be same length as
        in_file_list.

    plot_root : str
        Dir + base name of the plot file. _lag-energy.[PLOT_EXT] are appended to
        it.

    Returns
    -------
    nothing

    """

    colours=["black",
             "blue",
             "green",
             "magenta",
             "orange"]

    markers = ['o',
               'd'
               '*',
               '.',
               'x']

    #######################
    ## Setting up the plot
    #######################

    font_prop = font_manager.FontProperties(size=20)
    energies = np.loadtxt(HOME_DIR + "/Reduced_data/" + prefix +
                          "/energies.txt")
    energy_list = [np.mean([x, y]) for x,y in tools.pairwise(energies)]
    energy_err = [np.abs(a-b) for (a,b) in zip(energy_list, energies[0:-1])]

    plot_file = plot_root + "_lag-energy." + PLOT_EXT
    print "Lag-energy spectrum: %s" % plot_file

    e_chans = np.arange(0, DETCHANS)


    ###################
    ## Making the plot
    ###################

    fig, ax = plt.subplots(1, 1, figsize=(10,7.5), dpi=300, tight_layout=True)

    ax.hlines(0.0, 3, 21, linestyle='dashed', lw=2, color='gray')

    ## Deleting the values at energy channel 10 for RXTE PCA event-mode data
    if DETCHANS == 64:
        e_chans = np.delete(e_chans, 10)
        energy_list = np.delete(energy_list, 10)
        energy_err = np.delete(energy_err, 10)
    i = 0

    for in_file in in_file_list:
        try:
            fits_hdu = fits.open(in_file)
        except IOError:
            print "\tERROR: File does not exist: %s" % in_file
            exit()

        # print fits_hdu.info()

        tlag = fits_hdu[2].data.field('TIME_LAG')

        tlag_err = fits_hdu[2].data.field('TIME_LAG_ERR')

        ## Deleting the values at energy channel 10 for RXTE PCA event-mode data
        if DETCHANS == 64:
            tlag = np.delete(tlag, 10)
            tlag_err = np.delete(tlag_err, 10)

            ax.errorbar(energy_list[2:26], tlag[2:26], xerr=energy_err[2:26],
                    yerr=tlag_err[2:26], ls='none', marker=markers[i], ms=10,
                    mew=2, mec=colours[i], mfc=colours[i], ecolor=colours[i],
                    elinewidth=2, capsize=0, label=labels[i])
        else:
            ax.errorbar(energy_list, tlag, xerr=energy_err,
                    yerr=tlag_err, ls='none', marker=markers[i], ms=10, mew=2,
                    mec=colours[i], mfc=colours[i], ecolor=colours[i],
                    elinewidth=2, capsize=0, label=labels[i])
        i += 1

    # ax.plot([0,DETCHANS],[0,0], lw=1.5, ls='dashed', c='black')
    # 	ax.plot([0,DETCHANS],[np.pi,np.pi], lw=1.5, ls='dashed', c='black')
    # 	ax.plot([0,DETCHANS],[-np.pi,-np.pi], lw=1.5, ls='dashed', c='black')
    # 	ax.errorbar(e_chans, phase, yerr=err_phase, lw=3, c='red', \
    # 		ls="steps-mid", elinewidth=2, capsize=2)

    ax.set_xlabel('Energy (keV)', fontproperties=font_prop)
    ax.set_xlim(3, 21)
    # ax.set_xlim(0.3, 10)
    # ax.set_ylim(-0.009, 0.016)
    ax.set_xscale('log')
    x_maj_loc = [5, 10, 20]
    # y_maj_loc = [-0.005, 0, 0.005, 0.01, 0.015]
    ax.set_xticks(x_maj_loc)
    # ax.set_yticks(y_maj_loc)
    xLocator = MultipleLocator(1)  ## loc of minor ticks on x-axis
    yLocator = MultipleLocator(0.001)  ## loc of minor ticks on y-axis
    ax.xaxis.set_minor_locator(xLocator)
    ax.yaxis.set_minor_locator(yLocator)
    ax.xaxis.set_major_formatter(ScalarFormatter())

    ax.set_ylabel('Time lag (s)', fontproperties=font_prop)
    # ax.set_ylabel('Phase lag (radians)', fontproperties=font_prop)
    ax.set_ylim(-0.01, 0.017)
    # ax.set_ylim(-0.4, 0.5)
    ax.tick_params(axis='x', labelsize=18)
    ax.tick_params(axis='y', labelsize=18)
    # ax.set_title("Lag-energy spectrum", fontproperties=font_prop)

    ## The following legend code was found on stack overflow I think
    legend = ax.legend(loc='upper left')
    for label in legend.get_texts():
        label.set_fontsize(18)
    for label in legend.get_lines():
        label.set_linewidth(2)  # the legend line width

    plt.savefig(plot_file)
    # 	plt.show()
    plt.close()
    subprocess.call(['open', plot_file])