use of org.onebusaway.collections.Range in project onebusaway-application-modules by camsys.
the class StopTimeServiceImpl method getDepartureForStopAndServiceDate.
@Override
public Range getDepartureForStopAndServiceDate(AgencyAndId stopId, ServiceDate serviceDate) {
StopEntry stop = _graph.getStopEntryForId(stopId, true);
List<BlockStopTimeIndex> indices = _blockIndexService.getStopTimeIndicesForStop(stop);
Range interval = new Range();
for (BlockStopTimeIndex index : indices) {
extendIntervalWithIndex(serviceDate, interval, index);
}
List<FrequencyBlockStopTimeIndex> freqIndices = _blockIndexService.getFrequencyStopTimeIndicesForStop(stop);
for (FrequencyBlockStopTimeIndex index : freqIndices) extendIntervalWithIndex(serviceDate, interval, index);
return interval;
}
use of org.onebusaway.collections.Range 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.collections.Range in project onebusaway-application-modules by camsys.
the class RealTimeHistoryServiceImpl method createHistogramFromValues.
private ScheduleDeviationHistogram createHistogramFromValues(double[] values, int stepSizeInSeconds) {
values = noNans(values);
if (values.length == 0)
return new ScheduleDeviationHistogram(new int[0], new int[0]);
Range r = new Range();
for (double v : values) r.addValue(v);
if (r.getRange() == 0)
return new ScheduleDeviationHistogram(new int[] { (int) values[0] }, new int[] { values.length });
int halfStep = stepSizeInSeconds / 2;
int from = (int) (Math.floor((r.getMin() - halfStep) / stepSizeInSeconds) * stepSizeInSeconds) + halfStep;
int to = (int) (Math.ceil((r.getMax() + halfStep) / stepSizeInSeconds) * stepSizeInSeconds) - halfStep;
int columns = (to - from) / stepSizeInSeconds;
int[] scheduleDeviations = new int[columns];
int[] counts = new int[columns];
for (int i = 0; i < columns; i++) scheduleDeviations[i] = from + stepSizeInSeconds * i + halfStep;
for (double value : values) {
int index = (int) ((value - from) / stepSizeInSeconds);
counts[index]++;
}
return new ScheduleDeviationHistogram(scheduleDeviations, counts);
}
use of org.onebusaway.collections.Range in project onebusaway-application-modules by camsys.
the class VehicleLocationCacheElements method getTimeRange.
public Range getTimeRange() {
if (_elements.isEmpty())
throw new NoSuchElementException();
VehicleLocationCacheElement first = _elements.get(0);
VehicleLocationCacheElement last = _elements.get(_elements.size() - 1);
return new Range(first.getRecord().getTimeOfRecord(), last.getRecord().getTimeOfRecord());
}
use of org.onebusaway.collections.Range in project onebusaway-application-modules by camsys.
the class BlockLocationServiceImpl method getBlockLocationRecordCollections.
private List<VehicleLocationCacheElements> getBlockLocationRecordCollections(RecordStrategy strategy, TargetTime time) {
List<VehicleLocationCacheElements> entries = strategy.getRecordsFromCache();
if (!entries.isEmpty()) {
List<VehicleLocationCacheElements> inRange = new ArrayList<VehicleLocationCacheElements>();
long offset = _predictionCacheMaxOffset * 1000;
for (VehicleLocationCacheElements elements : entries) {
if (elements.isEmpty())
continue;
Range range = elements.getTimeRange();
long tFrom = (long) (range.getMin() - offset);
long tTo = (long) (range.getMax() + offset);
if (tFrom <= time.getCurrentTime() && time.getCurrentTime() <= tTo)
inRange.add(elements);
}
if (!inRange.isEmpty())
return inRange;
}
long offset = _blockLocationRecordCacheWindowSize * 1000 / 2;
// We only consult persisted cache entries if the requested target time is
// not within our current cache window
boolean outOfRange = time.getTargetTime() + offset < time.getCurrentTime() || time.getCurrentTime() < time.getTargetTime() - offset;
if (outOfRange && _persistBlockLocationRecords) {
_blockLocationRecordPersistentStoreAccessCount.incrementAndGet();
long fromTime = time.getTargetTime() - offset;
long toTime = time.getTargetTime() + offset;
List<BlockLocationRecord> predictions = strategy.getRecordsFromDao(fromTime, toTime);
if (!predictions.isEmpty()) {
Map<BlockLocationRecordKey, List<BlockLocationRecord>> recordsByKey = groupRecord(predictions);
List<VehicleLocationCacheElements> allCollections = new ArrayList<VehicleLocationCacheElements>();
for (Map.Entry<BlockLocationRecordKey, List<BlockLocationRecord>> entry : recordsByKey.entrySet()) {
BlockLocationRecordKey key = entry.getKey();
List<BlockLocationRecord> blockLocationRecords = entry.getValue();
List<VehicleLocationCacheElements> someRecords = getBlockLocationRecordsAsVehicleLocationRecords(key.getBlockInstance(), blockLocationRecords);
allCollections.addAll(someRecords);
}
return allCollections;
}
}
return Collections.emptyList();
}
Aggregations