Exemplo n.º 1
0
def generate_costop_matrix():
    start_date = ot_utils.get_localtime_now().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')]
    trips = gtfs.models.Trip.objects.filter(service__in=relevant_service_ids)
    trips = trips.prefetch_related('stoptime_set', 'stoptime_set__stop')    
    trips = list(trips)
    costops = np.zeros((len(stops.all_stops), len(stops.all_stops)))
    stop_id_to_ind_id = dict(zip(stops.all_stops.id_list, range(len(stops.all_stops.id_list))))
    stops.all_stops.id_list
    for trip in trips:
        trip_stop_times = trip.get_stop_times()
        for stop_time1 in trip_stop_times:
            for stop_time2 in trip_stop_times:
                stop_time1_ind = stop_id_to_ind_id[stop_time1.stop.stop_id];
                stop_time2_ind = stop_id_to_ind_id[stop_time2.stop.stop_id];
                costops[stop_time1_ind, stop_time2_ind] += 1
                costops[stop_time2_ind, stop_time1_ind] += 1
    names = [stops.all_stops[x].name for x in stops.all_stops.id_list[0:-1]]
    if (0):
        import matplotlib.pyplot as plt
        plt.imshow(costops > 0, interpolation="none")
        plt.yticks(range(costops.shape[0]), names, size='small')
        locs, labels = plt.xticks(range(costops.shape[0]), names, size='small')
        plt.setp(labels, rotation=90)

    return (costops > 0).astype(int);
Exemplo n.º 2
0
    def _stop_detector_on_real_trip(self, device_id='1cb87f1e', do_preload_reports=True, set_reports_to_same_weekday_last_week=True, do_show_fig=False):
        remove_from_redis([device_id])
        now = ot_utils.get_localtime_now()
        reports_queryset = get_device_id_reports(device_id)
        tracker_id = device_id
        if do_show_fig:
            display_utils.draw_map()

        fps_period_start = time.clock()
        fps_period_length = 100
        if do_preload_reports:
            reports_queryset = list(reports_queryset)
        count = len(reports_queryset) if isinstance(
            reports_queryset, list) else reports_queryset.count()
        for i in xrange(count):
            if i % fps_period_length == 0:
                elapsed = (time.clock() - fps_period_start)
                if elapsed > 0:
                    logger.debug('%d\t%.1f qps' %
                                 (i, fps_period_length / elapsed))
                else:
                    logger.debug(
                        'Elapsed time should be positive but is %d' % (elapsed))
                fps_period_start = time.clock()

            report = reports_queryset[i]

            if set_reports_to_same_weekday_last_week:
                # fix finding same weekday last week by
                # http://stackoverflow.com/questions/6172782/find-the-friday-of-previous-last-week-in-python
                day_fix = (now.weekday() - report.timestamp.weekday()) % 7
                day = now + datetime.timedelta(days=-day_fix)
                # move day and correct for DST (daylight savings time)
                dst_before = report.get_timestamp_israel_time().dst()
                report.timestamp = report.timestamp.replace(
                    year=day.year, month=day.month, day=day.day)
                dst_after = report.get_timestamp_israel_time().dst()
                report.timestamp -= dst_after - dst_before
            if do_show_fig:
                plt.scatter(report.my_loc.lat, report.my_loc.lon)
                plt.show()
            #print i, ot_utils.get_localtime(report.timestamp)
            is_updated_stop_time = add_report(tracker_id, report)
            if is_updated_stop_time:
                logger.debug('stop_time updated')

        stop_detector.print_tracked_stop_times(device_id)
        detected_stop_times = stop_detector.get_detected_stop_times(device_id)
        ground_truth_stops = stop_detector_ground_truth.data[device_id]
        for x, y in zip(detected_stop_times, ground_truth_stops):
            self.assertEquals(x.__str__(), y.__str__())

        remove_from_redis([device_id])
        print 'done'
        return tracker_id
Exemplo n.º 3
0
    def test_stop_detector_on_mock_trip(self, device_id='fake_device_1', trip_id='010714_00115'):
        remove_from_redis([device_id])
        day = datetime.datetime.strptime(trip_id.split('_')[0], '%d%m%y')
        # we want to get the correct timezone so we take it from
        # get_localtime_now()
        now = ot_utils.get_localtime_now()
        day = now.replace(year=day.year, month=day.month, day=day.day)
        reports = generate_mock_reports(
            device_id=device_id, trip_id=trip_id, nostop_percent=0.05, day=day)
        tracker_id = device_id
        for i, report in enumerate(reports):
            add_report(tracker_id, report=report)
            if (i % 100) == 0:
                print i
                stop_detector.print_tracked_stop_times(tracker_id)

        self.evaluate_detected_stop_times(tracker_id, trip_id)
        remove_from_redis([device_id])
        print 'done'