def get_moves_by_day(data):
    moves = []
    cur_move = []
    last_endtime = None
    H1 = 30
    H2 = 15

    for record in data:
        if record['duration'] > H1:
            if len(cur_move) > 1:
                moves.append(cur_move)
            cur_move = []
            last_endtime = None
        else:
            start_time = str2date(record['start_time'])
            end_time = str2date(record['end_time'])
            if last_endtime is None:
                cur_move.append(record)
                last_endtime = end_time
            else:
                delta = (start_time - last_endtime).total_seconds() / 60
                if delta < H2:
                    cur_move.append(record)
                    last_endtime = end_time
                else:
                    if len(cur_move) > 1:
                        moves.append(cur_move)
                    cur_move = []
                    last_endtime = None
    return moves
예제 #2
0
def get_location_in_range(move, start, end):
    result = []
    for location in move:
        cur_start = str2date(location['start_time'])
        cur_end = str2date(location['end_time'])
        if cur_start < end and (cur_start >= start or cur_end <= end):
            result.append(location)
    return result
def get_location_in_range(move, start, end):
    result = []
    for location in move:
        cur_start = str2date(location["start_time"])
        cur_end = str2date(location["end_time"])
        if cur_start < end and (cur_start >= start or cur_end <= end):
            result.append(location)
    return result
예제 #4
0
def entropy(move, delta_t, time_range):
    if len(move) <= 1:
        return 0

    location_time_ranges = {}

    move_start = str2date(move[0]['start_time'])
    move_end = str2date(move[-1]['end_time'])

    if move_start > time_range[0]:
        delta_t -= (move_start - time_range[0]).total_seconds() / 60.0
    if move_end < time_range[1]:
        delta_t -= (time_range[1] - move_end).total_seconds() / 60.0

    if delta_t <= 0:
        return 0

    last_end_time = None
    last_location = None
    last_duration = None
    last_start_time = None
    for location in move:
        cur_location = location['location']
        cur_start_time = str2date(location['start_time'])
        cur_end_time = str2date(location['end_time'])
        cur_duration = location['duration']
        location_time_ranges.setdefault(cur_location, 0)

        if last_end_time:
            location_time_ranges[last_location] += (
                cur_start_time - last_end_time).total_seconds() / 60.0
            from_ts = max(time_range[0], last_start_time)
            to_ts = min(time_range[1], last_end_time)
            location_time_ranges[last_location] += (
                to_ts - from_ts).total_seconds() / 60.0

        last_start_time = cur_start_time
        last_end_time = cur_end_time
        last_location = cur_location
        last_duration = cur_duration

    if location:
        from_ts = max(time_range[0], last_start_time)
        to_ts = min(time_range[1], last_end_time)
        location_time_ranges[last_location] += (to_ts -
                                                from_ts).total_seconds() / 60.0

    for location in list(location_time_ranges.keys()):
        location_time_ranges[location] /= delta_t

    entropy = sum([
        -1 * proba * math.log(proba, 2)
        for proba in list(location_time_ranges.values()) if proba > 0
    ])
    return entropy
def transient_entropy(location, move):
    delta_t = 10

    # set start_time
    start_time = str2date(location["start_time"])
    # get - t/2 and + t/2
    time_range = [
        start_time - datetime.timedelta(minutes=delta_t / 2),
        start_time + datetime.timedelta(minutes=delta_t / 2),
    ]
    location_in_ranges = get_location_in_range(move, time_range[0], time_range[1])
    pprint.pprint(location_in_ranges)
    location_time_ranges = {}
    move_start = str2date(move[0]["start_time"])
    move_end = str2date(move[-1]["end_time"])

    if move_start > time_range[0]:
        location_time_ranges["before"] = (move_start - time_range[0]).total_seconds() / 60.0
    if move_end < time_range[1]:
        location_time_ranges["after"] = (time_range[1] - move_end).total_seconds() / 60.0
        # delta_t -= (time_range[1] - move_end).total_seconds() / 60.0

    last_end_time = None
    last_location = None
    last_duration = None
    last_start_time = None
    for location in location_in_ranges:
        cur_location = location["location"]
        cur_start_time = str2date(location["start_time"])
        cur_end_time = str2date(location["end_time"])
        cur_duration = location["duration"]
        location_time_ranges.setdefault(cur_location, 0)

        if last_end_time:
            location_time_ranges[last_location] += (cur_start_time - last_end_time).total_seconds() / 60.0
            from_ts = max(time_range[0], last_start_time)
            to_ts = min(time_range[1], last_end_time)
            location_time_ranges[last_location] += (to_ts - from_ts).total_seconds() / 60.0

        last_start_time = cur_start_time
        last_end_time = cur_end_time
        last_location = cur_location
        last_duration = cur_duration

    if location:
        from_ts = max(time_range[0], last_start_time)
        to_ts = min(time_range[1], last_end_time)
        location_time_ranges[last_location] += (to_ts - from_ts).total_seconds() / 60.0

    for location in list(location_time_ranges.keys()):
        location_time_ranges[location] /= delta_t

    entropy = sum([-1 * proba * math.log(proba, 2) for proba in list(location_time_ranges.values()) if proba > 0])
    return entropy
예제 #6
0
def to_dist(row):
    result = {}
    for day in row:
        date = day["date"]
        result.setdefault(date, {})
        for location in day["locations"]:
            loc = location["location"]
            result[date].setdefault(loc, 0)
            st = str2date(location["start_time"])
            dt = str2date(location["end_time"])
            duration = (dt - st).total_seconds() / 60.0
            result[date][loc] += duration
    return result
예제 #7
0
def to_dist(row):
    result = {}
    for day in row:
        date = day['date']
        result.setdefault(date, {})
        for location in day['locations']:
            loc = location['location']
            result[date].setdefault(loc, 0)
            st = str2date(location['start_time'])
            dt = str2date(location['end_time'])
            duration = (dt - st).total_seconds() / 60.0
            result[date][loc] += duration
    return result
def entropy(move, delta_t, time_range):
    if len(move) <= 1:
        return 0

    location_time_ranges = {}

    move_start = str2date(move[0]["start_time"])
    move_end = str2date(move[-1]["end_time"])

    if move_start > time_range[0]:
        delta_t -= (move_start - time_range[0]).total_seconds() / 60.0
    if move_end < time_range[1]:
        delta_t -= (time_range[1] - move_end).total_seconds() / 60.0

    if delta_t <= 0:
        return 0

    last_end_time = None
    last_location = None
    last_duration = None
    last_start_time = None
    for location in move:
        cur_location = location["location"]
        cur_start_time = str2date(location["start_time"])
        cur_end_time = str2date(location["end_time"])
        cur_duration = location["duration"]
        location_time_ranges.setdefault(cur_location, 0)

        if last_end_time:
            location_time_ranges[last_location] += (cur_start_time - last_end_time).total_seconds() / 60.0
            from_ts = max(time_range[0], last_start_time)
            to_ts = min(time_range[1], last_end_time)
            location_time_ranges[last_location] += (to_ts - from_ts).total_seconds() / 60.0

        last_start_time = cur_start_time
        last_end_time = cur_end_time
        last_location = cur_location
        last_duration = cur_duration

    if location:
        from_ts = max(time_range[0], last_start_time)
        to_ts = min(time_range[1], last_end_time)
        location_time_ranges[last_location] += (to_ts - from_ts).total_seconds() / 60.0

    for location in list(location_time_ranges.keys()):
        location_time_ranges[location] /= delta_t

    entropy = sum([-1 * proba * math.log(proba, 2) for proba in list(location_time_ranges.values()) if proba > 0])
    return entropy
예제 #9
0
def get_speed_by_day_at_change_point(all_rows, day):
    cols = ['start_time', 'location']
    speeds = []
    results = merge_locations_by_date([dict(list(zip(cols, row))) for row in all_rows])

    points = set()
    for location in results:
        points.add(location['start_time'])
        points.add(location['end_time'])
    points = [str2date(x) for x in sorted(points)]

    if len(all_rows) == 0:
        return speeds

    delta_t = 60
    for i in range(len(points)):
        start_time = points[i] - datetime.timedelta(minutes=delta_t / 2)
        end_time = points[i] + datetime.timedelta(minutes=delta_t / 2)
        rows = [x for x in all_rows if date2str(start_time) <= x[0] <= date2str(end_time)]
        if len(rows) == 0:
            speeds.append({
                'time': date2str(points[i]),
                'speed': 0
            })
            continue
        rows = merge_locations_by_date([dict(list(zip(cols, row))) for row in rows])
        get_delta_by_day(rows)
        speed = entropy(rows, delta_t, [start_time, end_time])
        speeds.append({
            'time': date2str(points[i]),
            'speed': speed
        })
    return speeds
예제 #10
0
def transient_entropy(location, move):
    delta_t = 10

    # set start_time
    start_time = str2date(location['start_time'])
    # get - t/2 and + t/2
    time_range = [
        start_time - datetime.timedelta(minutes=delta_t / 2),
        start_time + datetime.timedelta(minutes=delta_t / 2)
    ]
    location_in_ranges = get_location_in_range(move, time_range[0],
                                               time_range[1])
    pprint.pprint(location_in_ranges)
    location_time_ranges = {}
    move_start = str2date(move[0]['start_time'])
    move_end = str2date(move[-1]['end_time'])

    if move_start > time_range[0]:
        location_time_ranges['before'] = (move_start -
                                          time_range[0]).total_seconds() / 60.0
    if move_end < time_range[1]:
        location_time_ranges['after'] = (time_range[1] -
                                         move_end).total_seconds() / 60.0
        # delta_t -= (time_range[1] - move_end).total_seconds() / 60.0

    last_end_time = None
    last_location = None
    last_duration = None
    last_start_time = None
    for location in location_in_ranges:
        cur_location = location['location']
        cur_start_time = str2date(location['start_time'])
        cur_end_time = str2date(location['end_time'])
        cur_duration = location['duration']
        location_time_ranges.setdefault(cur_location, 0)

        if last_end_time:
            location_time_ranges[last_location] += (
                cur_start_time - last_end_time).total_seconds() / 60.0
            from_ts = max(time_range[0], last_start_time)
            to_ts = min(time_range[1], last_end_time)
            location_time_ranges[last_location] += (
                to_ts - from_ts).total_seconds() / 60.0

        last_start_time = cur_start_time
        last_end_time = cur_end_time
        last_location = cur_location
        last_duration = cur_duration

    if location:
        from_ts = max(time_range[0], last_start_time)
        to_ts = min(time_range[1], last_end_time)
        location_time_ranges[last_location] += (to_ts -
                                                from_ts).total_seconds() / 60.0

    for location in list(location_time_ranges.keys()):
        location_time_ranges[location] /= delta_t

    entropy = sum([
        -1 * proba * math.log(proba, 2)
        for proba in list(location_time_ranges.values()) if proba > 0
    ])
    return entropy