Search in sources :

Example 1 with Range

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;
}
Also used : FrequencyBlockStopTimeIndex(org.onebusaway.transit_data_federation.services.blocks.FrequencyBlockStopTimeIndex) BlockStopTimeIndex(org.onebusaway.transit_data_federation.services.blocks.BlockStopTimeIndex) AbstractBlockStopTimeIndex(org.onebusaway.transit_data_federation.services.blocks.AbstractBlockStopTimeIndex) FrequencyBlockStopTimeIndex(org.onebusaway.transit_data_federation.services.blocks.FrequencyBlockStopTimeIndex) StopEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopEntry) Range(org.onebusaway.collections.Range)

Example 2 with Range

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);
}
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 3 with Range

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);
}
Also used : ScheduleDeviationHistogram(org.onebusaway.transit_data_federation.services.realtime.ScheduleDeviationHistogram) Range(org.onebusaway.collections.Range)

Example 4 with Range

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());
}
Also used : Range(org.onebusaway.collections.Range) NoSuchElementException(java.util.NoSuchElementException)

Example 5 with Range

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();
}
Also used : ArrayList(java.util.ArrayList) Range(org.onebusaway.collections.Range) VehicleLocationCacheElements(org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) FactoryMap(org.onebusaway.collections.FactoryMap) SortedMap(java.util.SortedMap) TreeMap(java.util.TreeMap)

Aggregations

Range (org.onebusaway.collections.Range)5 ArrayList (java.util.ArrayList)2 SortedMap (java.util.SortedMap)2 TreeMap (java.util.TreeMap)2 DoubleArrayList (cern.colt.list.DoubleArrayList)1 List (java.util.List)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 FactoryMap (org.onebusaway.collections.FactoryMap)1 ScheduleDeviationHistory (org.onebusaway.transit_data_federation.impl.realtime.history.ScheduleDeviationHistory)1 AbstractBlockStopTimeIndex (org.onebusaway.transit_data_federation.services.blocks.AbstractBlockStopTimeIndex)1 BlockStopTimeIndex (org.onebusaway.transit_data_federation.services.blocks.BlockStopTimeIndex)1 FrequencyBlockStopTimeIndex (org.onebusaway.transit_data_federation.services.blocks.FrequencyBlockStopTimeIndex)1 ScheduleDeviationHistogram (org.onebusaway.transit_data_federation.services.realtime.ScheduleDeviationHistogram)1 VehicleLocationCacheElements (org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements)1 StopEntry (org.onebusaway.transit_data_federation.services.transit_graph.StopEntry)1