Beispiel #1
0
 def load_from_redis(redis_data):
     arrival = ot_utils.unix_time_to_localtime(int(redis_data[1]))
     redis_data_0_split = redis_data[0].split('_')
     stop_id = int(redis_data_0_split[0])
     name = stops.all_stops[stop_id].name
     departure = ot_utils.unix_time_to_localtime(int(redis_data_0_split[1])) if redis_data_0_split[1] != '' else None    
     return DetectedStopTime(stop_id, arrival, departure)
Beispiel #2
0
def print_tracked_stop_times(tracker_id):
    #for tracked_stop_time in self.stop_times:
    #    print tracked_stop_time
    res = cl.zrange(get_train_tracker_tracked_stops_key(tracker_id), 0, -1, withscores=True)
    for cur in res:
        arrival = ot_utils.unix_time_to_localtime(int(cur[1]))
        cur_0_split = cur[0].split('_')
        name = stops.all_stops[cur_0_split[0]].name
        departure = ot_utils.unix_time_to_localtime(int(cur_0_split[1])) if cur_0_split[1] != '' else None
        print TrackedStopTime.get_str(arrival, departure, name)
Beispiel #3
0
 def load_from_redis(redis_data):
     arrival = ot_utils.unix_time_to_localtime(int(redis_data[1]))
     data = json.loads(redis_data[0])
     if data[0] == str(None):
         stop_id = None
     else:
         stop_id = int(data[0])
     departure = ot_utils.isoformat_to_localtime(data[1]) if data[1] else None
     return DetectedStopTime(stop_id, arrival, departure)
Beispiel #4
0
def get_possible_trips(tracker_id, print_debug_info=False):
    stop_times_redis = cl.zrange(get_train_tracker_tracked_stops_key(tracker_id), 0, -1, withscores=True)
    if len(stop_times_redis) == 0:
        return None
    #arrival = get_localtime(datetime.datetime.fromtimestamp(stop_times_redis[0][1]), self.db_timezone)
    
    #shape_matches_inds = self.get_shape_ids_with_high_probability()

    #trips = gtfs.models.Trip.objects.filter(shape_id__in=shape_matches_inds, service__in=relevant_service_ids)
    relevant_service_ids = load_by_key(get_train_tracker_relevant_services_key(tracker_id))
    trips = gtfs.models.Trip.objects.filter(service__in=relevant_service_ids)

    # filter by stop existence and its time frame:
    trips_filtered_by_stops_and_times = trips
    if print_debug_info:
        print len(trips_filtered_by_stops_and_times)
    stop_times = []
    for cur in stop_times_redis:
        arrival = ot_utils.unix_time_to_localtime(int(cur[1]))
        cur_0_split = cur[0].split('_')
        stop_id = cur_0_split[0]
        name = stops.all_stops[stop_id].name
        departure = ot_utils.unix_time_to_localtime(int(cur_0_split[1])) if cur_0_split[1] != '' else None
        stop_times.append(TrackedStopTime(stop_id))
        stop_times[-1].arrival = arrival
        stop_times[-1].departure = departure
        
        trip_stop_times = gtfs.models.StopTime.objects.filter(trip__in = trips_filtered_by_stops_and_times)
        arrival_time__greater_than = arrival-datetime.timedelta(seconds=config.late_arrival_max_seconds)
        arrival_time__less_than = arrival+datetime.timedelta(seconds=config.early_arrival_max_seconds)
        arrival_time__range = datetime_range_to_db_time(arrival_time__greater_than, arrival_time__less_than)

        if departure is not None:
            departure_time__greater_than = departure-datetime.timedelta(seconds=config.late_departure_max_seconds)
            departure_time__less_than = departure+datetime.timedelta(seconds=config.early_departure_max_seconds)
            departure_time__range = datetime_range_to_db_time(departure_time__greater_than, departure_time__less_than)
        
            trip_stop_times_for_specific_stop = trip_stop_times.filter(stop = stop_id, 
                                                                       arrival_time__range=arrival_time__range,
                                                                       departure_time__range=departure_time__range)
        
        else:
            trip_stop_times_for_specific_stop = trip_stop_times.filter(stop = stop_id, 
                                                                       arrival_time__range=arrival_time__range)                
            
        trips_filtered_by_stops_and_times = trip_stop_times_for_specific_stop.values_list('trip')

        if print_debug_info:
            print len(trips_filtered_by_stops_and_times)
    
    # filter by stop order:
    trip_in_right_direction = []
    for i, t in enumerate(trips_filtered_by_stops_and_times):
        trip_stop_times = gtfs.models.StopTime.objects.filter(trip = t).order_by('arrival_time').values_list('stop')
        trip_stop_times = [str(x[0]) for x in trip_stop_times]
        stop_inds_by_visit_order = [trip_stop_times.index(x) for x in [x.stop_id for x in stop_times]]
        if is_increasing(stop_inds_by_visit_order):
            trip_in_right_direction.append(i)
    
    trips_filtered_by_stops_and_times = [trips_filtered_by_stops_and_times[i][0] for i in trip_in_right_direction]
    
    arrival_delta_abs_sums_seconds = []
    #departure_delta_abs_sums = []
    for t in trips_filtered_by_stops_and_times:
        stop_times_redis = gtfs.models.StopTime.objects.filter(trip = t).order_by('arrival_time').values_list('stop', 'arrival_time')#, 'departure_time')
        arrival_delta_abs_sum = 0
        #departure_delta_abs_sum = 0
        for tracked_stop_time in stop_times:
            stop_time = [x for x in stop_times_redis if str(x[0]) == tracked_stop_time.stop_id][0]
            arrival_delta_seconds = stop_time[1] - datetime_to_db_time(tracked_stop_time.arrival)
            #departure_delta_seconds = stop_time[2] - datetime_to_db_time(tracked_stop_time.departure)
            arrival_delta_abs_sum += abs(arrival_delta_seconds)
            #departure_delta_abs_sum += abs(departure_delta_seconds)
        arrival_delta_abs_sums_seconds.append(arrival_delta_abs_sum)
        #departure_delta_abs_sums.append(departure_delta_abs_sum)
    
    # sort results by increasing arrival time 
    sorted_trips = sorted(zip(arrival_delta_abs_sums_seconds,trips_filtered_by_stops_and_times))
    trips_filtered_by_stops_and_times = [x for (y,x) in sorted_trips]
    arrival_delta_abs_sums_seconds = [y for (y,x) in sorted_trips]
    return trips_filtered_by_stops_and_times, arrival_delta_abs_sums_seconds
Beispiel #5
0
def save_stop_times_to_db(tracker_id, arrival_unix_timestamp, stop_id_and_departure_time):
    stop_id, departure_unix_timestamp = stop_id_and_departure_time.split('_')
    name = stops.all_stops[stop_id].name
    departure = ot_utils.unix_time_to_localtime(int(departure_unix_timestamp)) if departure_unix_timestamp else None 
    arrival = ot_utils.unix_time_to_localtime(int(arrival_unix_timestamp))
Beispiel #6
0
def add_report_to_tracker(tracker_id, report):
    if not load_by_key(get_train_tracker_relevant_services_key(tracker_id)):
        start_date = report.timestamp.strftime("%Y-%m-%d")
        relevant_services = gtfs.models.Service.objects.filter(start_date = start_date)
        relevant_service_ids = [x[0] for x in relevant_services.all().values_list('service_id')]
        save_by_key(get_train_tracker_relevant_services_key(tracker_id), relevant_service_ids)
         
    # update train position
    if report.get_my_loc():
        try_update_coords(report, tracker_id)
        
        ##commented out below is code that filters trips based on shape
        #coords_updated = False
        #p = get_redis_pipeline()
        #p.zincrby("train_tracker:%s:counters" % (tracker_id), res_shape_ids[i], inc_by)
        #p.incr("train_tracker:%s:total" % (tracker_id), inc_by)
        #p.execute()        

        #for i in xrange(len(res_shape_point_ids)):
            #cl = get_redis_client()
            #if cl.sadd("train_tracker:%s:visited_shape_point_ids" % (tracker_id), res_shape_point_ids[i]) == 0:
                #if not coords_updated: #update if report adds new points on tracks
                    #self.coords = coords
                    #coords_updated = True
                #p = get_redis_pipeline()
                #p.zincrby("train_tracker:%s:counters" % (tracker_id), res_shape_ids[i], 1)
                #p.incr("train_tracker:%s:total" % (tracker_id))
                #p.execute()
                ##self.shape_counts[res_shape_ids[i]] = self.shape_counts[res_shape_ids[i]] + 1
        
        # 1) add stop or non-stop to prev_stops and prev_stops_timestamps     
        # 2) set calc_hmm to true if according to wifis and/or location, our state changed from stop to non-stop or vice versa
    prev_current_stop_id_by_hmm = cl.get(get_train_tracker_current_stop_id_key(tracker_id))
      
    if not prev_current_stop_id_by_hmm:
        prev_state = tracker_states.INITIAL
    elif prev_current_stop_id_by_hmm == stops.NOSTOP:
        prev_state = tracker_states.NOSTOP
    else:
        prev_state = tracker_states.STOP

    stop_id = try_get_stop_id(report)
    if not stop_id:
        current_state = tracker_states.UNKNOWN
    elif stop_id == nostop_id:
        current_state = tracker_states.NOSTOP
    else:
        current_state = tracker_states.STOP

    if current_state != tracker_states.UNKNOWN:
        timestamp = report.get_timestamp_israel_time()
        prev_stop_id = add_prev_stop(tracker_id, stop_id, timestamp)
        
    # calculate hmm to get state_sequence, update stop_times and current_stop if needed
    if  current_state != tracker_states.UNKNOWN and prev_state != current_state:

        prev_stops_and_timestamps = cl.zrange(get_train_tracker_timestamp_sorted_stop_ids_key(tracker_id), 0, -1, withscores=True)
        prev_stop_ids_order = [int(x[0].split("_")[0]) for x in prev_stops_and_timestamps]
        prev_stops_and_timestamps = [x for (y,x) in sorted(zip(prev_stop_ids_order,prev_stops_and_timestamps))]
        
        prev_stop_ids = [x[0].split("_")[1] for x in prev_stops_and_timestamps]
        
        prev_stop_int_ids = np.array([stops.all_stops.id_list.index(x) for x in prev_stop_ids])
        #assert np.array_equal(prev_stop_int_ids, np.array(self.prev_stops))
        prev_stop_hmm_logprob, prev_stop_int_ids_by_hmm = hmm.decode(prev_stop_int_ids)
        prev_stop_int_ids_by_hmm_for_debug = prev_stop_int_ids_by_hmm
        
        # update current_stop_id_by_hmm and current_state by hmm:        
        current_stop_id_by_hmm = stops.all_stops.id_list[prev_stop_int_ids_by_hmm[-1]]
        cl.set(get_train_tracker_current_stop_id_key(tracker_id), current_stop_id_by_hmm)
        if current_stop_id_by_hmm == stops.NOSTOP:
            current_state = tracker_states.NOSTOP
        else:
            current_state = tracker_states.STOP

        if prev_state != current_state: # change in state
            prev_stops_by_hmm = [stops.all_stops.id_list[x] for x in prev_stop_int_ids_by_hmm]
            prev_stops_timestamps = [ot_utils.unix_time_to_localtime((x[1])) for x in prev_stops_and_timestamps]
            index_of_oldest_current_state = max(0, find_index_of_first_consecutive_value(prev_stops_by_hmm, len(prev_stops_by_hmm)-1))
            index_of_most_recent_previous_state = index_of_oldest_current_state-1
              
            if current_state == tracker_states.NOSTOP:
                stop_id = prev_stops_by_hmm[index_of_most_recent_previous_state]
                unix_timestamp = ot_utils.dt_time_to_unix_time(prev_stops_timestamps[index_of_most_recent_previous_state])

                if prev_state == tracker_states.INITIAL:
                    pass #do nothing
                else: # previous_state == tracker_states.STOP - need to set stop_time departure
                    stop_time = cl.zrange(get_train_tracker_tracked_stops_key(tracker_id), -1, -1, withscores=True)
                    departure_unix_timestamp = unix_timestamp
                    stop_id_and_departure_time = "%s_%d" % (prev_current_stop_id_by_hmm, departure_unix_timestamp)
                    update_stop_time(tracker_id, prev_stop_id, stop_time[0][1], stop_id_and_departure_time)
                    update_trips(tracker_id)
            else: # current_state == tracker_states.STOP
                stop_id = prev_stops_by_hmm[index_of_oldest_current_state]
                unix_timestamp = ot_utils.dt_time_to_unix_time(prev_stops_timestamps[index_of_oldest_current_state])
                
                arrival_unix_timestamp = unix_timestamp
                stop_id_and_departure_time = "%s_" % (current_stop_id_by_hmm)
                update_stop_time(tracker_id, prev_stop_id, arrival_unix_timestamp, stop_id_and_departure_time)
                update_trips(tracker_id)
                    
            prev_timestamp = unix_timestamp
                
        print_tracked_stop_times(tracker_id)
Beispiel #7
0
def add_report(tracker_id, report):
    # 1) add stop or non-stop to prev_stops and prev_stops_timestamps     
    # 2) set calc_hmm to true if according to wifis and/or location, our
    #    state changed from stop to non-stop or vice versa
    prev_current_stop_id_by_hmm = cl.get(\
        get_train_tracker_current_stop_id_key(tracker_id))
    prev_current_stop_id_by_hmm = int(prev_current_stop_id_by_hmm) if\
        prev_current_stop_id_by_hmm else None
      
    if not prev_current_stop_id_by_hmm:
        prev_state = tracker_states.INITIAL
    else:
        prev_state = prev_current_stop_id_by_hmm

    stop_id = try_get_stop_id(report)
    if not stop_id:
        current_state = tracker_states.UNKNOWN
    else:
        current_state = stop_id

    if current_state != tracker_states.UNKNOWN:
        timestamp = report.get_timestamp_israel_time()
        prev_stop_id = add_prev_stop(tracker_id, stop_id, timestamp)
        
    # calculate hmm to get state_sequence, update stop_times and current_stop if needed
    if  current_state != tracker_states.UNKNOWN and prev_state != current_state:

        prev_stops_and_timestamps = cl.zrange(get_train_tracker_timestamp_sorted_stop_ids_key(tracker_id), 0, -1, withscores=True)
        prev_stop_ids_order = [int(x[0].split("_")[0]) for x in prev_stops_and_timestamps]
        prev_stops_and_timestamps = [x for (y,x) in sorted(zip(prev_stop_ids_order,prev_stops_and_timestamps))]
        
        prev_stop_ids = [int(x[0].split("_")[1]) for x in prev_stops_and_timestamps]
        
        prev_stop_int_ids = np.array([stops.all_stops.id_list.index(x) for x in prev_stop_ids])
        #assert np.array_equal(prev_stop_int_ids, np.array(self.prev_stops))
        prev_stop_hmm_logprob, prev_stop_int_ids_by_hmm = hmm.decode(prev_stop_int_ids)
        prev_stop_int_ids_by_hmm_for_debug = prev_stop_int_ids_by_hmm
        
        # update current_stop_id_by_hmm and current_state by hmm:        
        current_stop_id_by_hmm = stops.all_stops.id_list[prev_stop_int_ids_by_hmm[-1]]
        cl.set(get_train_tracker_current_stop_id_key(tracker_id), current_stop_id_by_hmm)
        current_state = current_stop_id_by_hmm

        if prev_state != current_state: # change in state
            prev_stops_by_hmm = [stops.all_stops.id_list[x] for x in prev_stop_int_ids_by_hmm]
            prev_stops_timestamps = [ot_utils.unix_time_to_localtime((x[1])) for x in prev_stops_and_timestamps]
            index_of_oldest_current_state = max(0, find_index_of_first_consecutive_value(prev_stops_by_hmm, len(prev_stops_by_hmm)-1))
            index_of_most_recent_previous_state = index_of_oldest_current_state-1
              
            if current_state == stops.NOSTOP:
                stop_id = prev_stops_by_hmm[index_of_most_recent_previous_state]
                unix_timestamp = ot_utils.dt_time_to_unix_time(prev_stops_timestamps[index_of_most_recent_previous_state])

                if prev_state == tracker_states.INITIAL:
                    pass #do nothing
                else: # previous_state == tracker_states.STOP - need to set stop_time departure
                    stop_time = cl.zrange(get_train_tracker_tracked_stops_key(tracker_id), -1, -1, withscores=True)
                    departure_unix_timestamp = unix_timestamp
                    stop_id_and_departure_time = "%s_%d" % (prev_current_stop_id_by_hmm, departure_unix_timestamp)
                    update_stop_time(tracker_id, prev_stop_id, stop_time[0][1], stop_id_and_departure_time)
            else: # current_state == tracker_states.STOP
                arrival_unix_timestamp_prev_stop = None
                stop_id_and_departure_time_prev_stop = None
                if (prev_state != tracker_states.INITIAL and prev_state != stops.NOSTOP):
                    last_prev_end_ind = len(prev_stops_by_hmm) - prev_stops_by_hmm[::-1].index(prev_state) - 1
                    stop_time = cl.zrange(get_train_tracker_tracked_stops_key(tracker_id), -1, -1, withscores=True)
                    departure_unix_timestamp = ot_utils.dt_time_to_unix_time(prev_stops_timestamps[last_prev_end_ind])
                    stop_id_and_departure_time = "%s_%d" % (prev_current_stop_id_by_hmm, departure_unix_timestamp)
                    arrival_unix_timestamp_prev_stop = stop_time[0][1]
                    stop_id_and_departure_time_prev_stop = stop_id_and_departure_time
                    
                stop_id = prev_stops_by_hmm[index_of_oldest_current_state]
                unix_timestamp = ot_utils.dt_time_to_unix_time(prev_stops_timestamps[index_of_oldest_current_state])
                
                arrival_unix_timestamp = unix_timestamp
                stop_id_and_departure_time = "%s_" % (current_stop_id_by_hmm)
                update_stop_time(tracker_id, prev_stop_id, arrival_unix_timestamp, stop_id_and_departure_time, arrival_unix_timestamp_prev_stop, stop_id_and_departure_time_prev_stop)
                    
            prev_timestamp = unix_timestamp

    stop_times = get_detected_stop_times(tracker_id)
    is_stops_updated = (prev_state != current_state) and len(stop_times) > 0
    return stop_times, is_stops_updated