Example #1
0
def zoomout_query(query_results, resolution):
    """
    this is trying to decrease the time resolution of desc ordered query
    results with datetime objects to the specified resolution, which should
    be a datetime.timedelta object;

    for each item, the index number of the datetime object must be 1;

    the non-datetime column will be normalized by the value of norm to a percentage
    """
    zoomouted = []
    last_index = len(query_results) - 1

    # starting from the second item in query_results, since the first and last
    # one will be used to indicate the actually timestamp, shouldn't be
    # averaged
    beg_item = query_results[1]
    ref_t = beg_item[1]
    # ts: used to collect time for average;
    # cs for cores initial reference time
    cs, ts = [beg_item[0]], [util.dat2time(ref_t)]
    for k, item in enumerate(query_results):
        item_c, item_t = item
        item_c = item_c
        if k == 0 or k == last_index:  # not do anything with the first and last item
            item = list(item)  # tuple => list
            zoomouted.append([item_c, item_t])
        else:
            if item_t - ref_t <= resolution:
                ts.append(util.dat2time(item_t))
                cs.append(item_c)
            else:
                zoomouted.append([
                    np.average(cs),
                    datetime.datetime.fromtimestamp(np.average(ts))
                ])
                ref_t = item_t
                cs, ts = [item_c], [util.dat2time(item_t)
                                    ]  # reinitialize ts, cs
    return zoomouted
Example #2
0
def zoomout_query(query_results, resolution):
    """
    this is trying to decrease the time resolution of desc ordered query
    results with datetime objects to the specified resolution, which should
    be a datetime.timedelta object;

    for each item, the index number of the datetime object must be 1;

    the non-datetime column will be normalized by the value of norm to a percentage
    """
    zoomouted = []
    last_index = len(query_results) - 1

    # starting from the second item in query_results, since the first and last
    # one will be used to indicate the actually timestamp, shouldn't be
    # averaged
    beg_item = query_results[1]
    ref_t = beg_item[1]
    # ts: used to collect time for average;
    # cs for cores initial reference time
    cs, ts = [beg_item[0]], [util.dat2time(ref_t)]
    for k, item in enumerate(query_results):
        item_c, item_t = item
        item_c = item_c
        if k == 0 or k == last_index: # not do anything with the first and last item
            item = list(item)         # tuple => list
            zoomouted.append([item_c, item_t])
        else:
            if item_t - ref_t <= resolution:
                ts.append(util.dat2time(item_t))
                cs.append(item_c)
            else:
                zoomouted.append([
                        np.average(cs),
                        datetime.datetime.fromtimestamp(np.average(ts))
                        ])
                ref_t = item_t
                cs, ts = [item_c], [util.dat2time(item_t)] # reinitialize ts, cs
    return zoomouted
Example #3
0
def inte_coresec(xs, ys):
    # inte: integrate core seconds
    # xs: usually time along the x axis
    # ys: core seconds along the y axis
    # The math is just like calculating the area of tiny trapezoids, and then
    # sum it up.
    xs = [util.dat2time(i) for i in xs]
    ref_x = xs[0]
    ref_y = ys[0]
    area = 0
    for x, y in zip(xs[1:], ys[1:]):
        area = area + (.5 * (y + ref_y) * (x - ref_x))
        ref_x, ref_y = x, y
    return area  # core seconds
Example #4
0
def inte_coresec(xs, ys):
    # inte: integrate core seconds
    # xs: usually time along the x axis
    # ys: core seconds along the y axis
    # The math is just like calculating the area of tiny trapezoids, and then
    # sum it up.
    xs = [util.dat2time(i) for i in xs]
    ref_x = xs[0]
    ref_y = ys[0]
    area = 0
    for x, y in zip(xs[1:], ys[1:]):
        area = area + (.5 * (y + ref_y) * (x - ref_x))
        ref_x, ref_y = x, y
    return area                                             # core seconds