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);
}
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);
}
Aggregations