Search in sources :

Example 1 with ScheduleDeviationHistory

use of org.onebusaway.transit_data_federation.impl.realtime.history.ScheduleDeviationHistory in project onebusaway-application-modules by camsys.

the class BlockLocationHistoryTask method constructHistory.

private ScheduleDeviationHistory constructHistory(AgencyAndId tripId, BlockLocationArchiveRecordMap recordsByInstance) {
    List<SortedMap<Integer, Double>> traces = new ArrayList<SortedMap<Integer, Double>>();
    Range tRange = new Range();
    sortAndArrangeTraces(recordsByInstance, traces, tRange);
    int step = computeSamplingStep(traces);
    int from = (int) (Math.ceil(tRange.getMin() / step) * step);
    int to = (int) (Math.floor(tRange.getMax() / step) * step);
    SortedMap<Integer, Double> mus = new TreeMap<Integer, Double>();
    SortedMap<Integer, Double> sigmas = new TreeMap<Integer, Double>();
    computeMeanAndStandardDeviationForTraces(traces, from, to, step, mus, sigmas);
    removeOutlierTraces(traces, mus, sigmas);
    int numOfTraces = traces.size();
    DoubleArrayList scheduleTimes = new DoubleArrayList();
    List<DoubleArrayList> scheduleDeviations = new ArrayList<DoubleArrayList>();
    for (int i = 0; i < numOfTraces; i++) scheduleDeviations.add(new DoubleArrayList());
    for (int t = from; t <= to; t += step) {
        DoubleArrayList rawValues = new DoubleArrayList();
        DoubleArrayList values = new DoubleArrayList();
        for (SortedMap<Integer, Double> m : traces) {
            if (t < m.firstKey() || t > m.lastKey()) {
                rawValues.add(Double.NaN);
                continue;
            }
            double schedDev = InterpolationLibrary.interpolate(m, t);
            values.add(schedDev);
            rawValues.add(schedDev);
        }
        if (values.size() < Math.max(_minSampleSize, 2))
            continue;
        double mu = Descriptive.mean(values);
        double sigma = Descriptive.sampleStandardDeviation(values.size(), Descriptive.sampleVariance(values, mu));
        int goodValueCount = pruneOutlierValues(rawValues, mu, sigma);
        if (goodValueCount < _minSampleSize)
            continue;
        scheduleTimes.add(t);
        for (int traceIndex = 0; traceIndex < traces.size(); traceIndex++) scheduleDeviations.get(traceIndex).add(rawValues.get(traceIndex));
    }
    scheduleTimes.trimToSize();
    double[] scheduleTimesArray = scheduleTimes.elements();
    double[][] scheduleDeviationsArrays = new double[numOfTraces][];
    for (int traceIndex = 0; traceIndex < numOfTraces; traceIndex++) {
        DoubleArrayList list = scheduleDeviations.get(traceIndex);
        list.trimToSize();
        scheduleDeviationsArrays[traceIndex] = list.elements();
    }
    return new ScheduleDeviationHistory(tripId, scheduleTimesArray, scheduleDeviationsArrays);
}
Also used : DoubleArrayList(cern.colt.list.DoubleArrayList) ArrayList(java.util.ArrayList) ScheduleDeviationHistory(org.onebusaway.transit_data_federation.impl.realtime.history.ScheduleDeviationHistory) Range(org.onebusaway.collections.Range) TreeMap(java.util.TreeMap) DoubleArrayList(cern.colt.list.DoubleArrayList) SortedMap(java.util.SortedMap)

Example 2 with ScheduleDeviationHistory

use of org.onebusaway.transit_data_federation.impl.realtime.history.ScheduleDeviationHistory in project onebusaway-application-modules by camsys.

the class BlockLocationHistoryTask method processTrip.

private void processTrip(TripEntry trip) {
    List<BlockLocationArchiveRecord> records = _source.getRecordsForTrip(trip.getId());
    Map<AgencyAndId, BlockLocationArchiveRecordMap> recordsByTrip = loadRecords(records);
    List<ScheduleDeviationHistory> histories = new ArrayList<ScheduleDeviationHistory>();
    for (Map.Entry<AgencyAndId, BlockLocationArchiveRecordMap> entry : recordsByTrip.entrySet()) {
        AgencyAndId tripId = entry.getKey();
        BlockLocationArchiveRecordMap recordsByInstance = entry.getValue();
        /**
         * If we don't have enough samples, skip the trip
         */
        if (recordsByInstance.size() < _minSampleSize)
            continue;
        ScheduleDeviationHistory history = constructHistory(tripId, recordsByInstance);
        histories.add(history);
    }
    if (!histories.isEmpty())
        _scheduleDeviationHistoryDao.saveScheduleDeviationHistory(histories);
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) DoubleArrayList(cern.colt.list.DoubleArrayList) ArrayList(java.util.ArrayList) BlockLocationArchiveRecord(org.onebusaway.transit_data_federation.impl.realtime.history.BlockLocationArchiveRecord) ScheduleDeviationHistory(org.onebusaway.transit_data_federation.impl.realtime.history.ScheduleDeviationHistory) TreeMap(java.util.TreeMap) Map(java.util.Map) FactoryMap(org.onebusaway.collections.FactoryMap) SortedMap(java.util.SortedMap)

Aggregations

DoubleArrayList (cern.colt.list.DoubleArrayList)2 ArrayList (java.util.ArrayList)2 SortedMap (java.util.SortedMap)2 TreeMap (java.util.TreeMap)2 ScheduleDeviationHistory (org.onebusaway.transit_data_federation.impl.realtime.history.ScheduleDeviationHistory)2 Map (java.util.Map)1 FactoryMap (org.onebusaway.collections.FactoryMap)1 Range (org.onebusaway.collections.Range)1 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)1 BlockLocationArchiveRecord (org.onebusaway.transit_data_federation.impl.realtime.history.BlockLocationArchiveRecord)1