List of usage examples for java.text DecimalFormat setGroupingUsed
@Override public void setGroupingUsed(boolean newValue)
From source file:org.matsim.contrib.drt.analysis.DynModeTripsAnalyser.java
public static String summarizeTrips(List<DynModeTrip> trips, String delimiter) { DescriptiveStatistics waitStats = new DescriptiveStatistics(); DescriptiveStatistics rideStats = new DescriptiveStatistics(); DescriptiveStatistics distanceStats = new DescriptiveStatistics(); DescriptiveStatistics directDistanceStats = new DescriptiveStatistics(); DescriptiveStatistics traveltimes = new DescriptiveStatistics(); DecimalFormat format = new DecimalFormat(); format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US)); format.setMinimumIntegerDigits(1);/* www . ja v a 2 s .c o m*/ format.setMaximumFractionDigits(2); format.setGroupingUsed(false); for (DynModeTrip trip : trips) { if (trip.getToLinkId() == null) { continue; } waitStats.addValue(trip.getWaitTime()); rideStats.addValue(trip.getInVehicleTravelTime()); distanceStats.addValue(trip.getTravelDistance()); directDistanceStats.addValue(trip.getUnsharedDistanceEstimate_m()); traveltimes.addValue(trip.getInVehicleTravelTime() + trip.getWaitTime()); } String value = format.format(waitStats.getValues().length) + delimiter + format.format(waitStats.getMean()) + delimiter + format.format(waitStats.getMax()) + delimiter + format.format(waitStats.getPercentile(95)) + delimiter + format.format(waitStats.getPercentile(75)) + delimiter + format.format(waitStats.getPercentile(50)) + delimiter + format.format(rideStats.getMean()) + delimiter + format.format(distanceStats.getMean()) + delimiter + format.format(directDistanceStats.getMean()) + delimiter + format.format(traveltimes.getMean()); return value; }
From source file:org.onebusaway.nyc.presentation.impl.realtime.SiriSupport.java
private static OnwardCallStructure getOnwardCallStructure(StopBean stopBean, PresentationService presentationService, double distanceOfCallAlongTrip, double distanceOfVehicleFromCall, int visitNumber, int index, TimepointPredictionRecord prediction, long responseTimestamp) { OnwardCallStructure onwardCallStructure = new OnwardCallStructure(); onwardCallStructure.setVisitNumber(BigInteger.valueOf(visitNumber)); StopPointRefStructure stopPointRef = new StopPointRefStructure(); stopPointRef.setValue(stopBean.getId()); onwardCallStructure.setStopPointRef(stopPointRef); NaturalLanguageStringStructure stopPoint = new NaturalLanguageStringStructure(); stopPoint.setValue(stopBean.getName()); onwardCallStructure.setStopPointName(stopPoint); if (prediction != null) { if (prediction.getTimepointPredictedTime() < responseTimestamp) { onwardCallStructure.setExpectedArrivalTime(new Date(responseTimestamp)); onwardCallStructure.setExpectedDepartureTime(new Date(responseTimestamp)); } else {//from w ww .j a v a 2 s. c o m onwardCallStructure.setExpectedArrivalTime(new Date(prediction.getTimepointPredictedTime())); onwardCallStructure.setExpectedDepartureTime(new Date(prediction.getTimepointPredictedTime())); } } // siri extensions SiriExtensionWrapper wrapper = new SiriExtensionWrapper(); ExtensionsStructure distancesExtensions = new ExtensionsStructure(); SiriDistanceExtension distances = new SiriDistanceExtension(); DecimalFormat df = new DecimalFormat(); df.setMaximumFractionDigits(2); df.setGroupingUsed(false); distances.setStopsFromCall(index); distances.setCallDistanceAlongRoute(Double.valueOf(df.format(distanceOfCallAlongTrip))); distances.setDistanceFromCall(Double.valueOf(df.format(distanceOfVehicleFromCall))); distances.setPresentableDistance(presentationService.getPresentableDistance(distances)); wrapper.setDistances(distances); distancesExtensions.setAny(wrapper); onwardCallStructure.setExtensions(distancesExtensions); return onwardCallStructure; }
From source file:org.onebusaway.nyc.presentation.impl.realtime.SiriSupport.java
private static MonitoredCallStructure getMonitoredCallStructure(StopBean stopBean, PresentationService presentationService, double distanceOfCallAlongTrip, double distanceOfVehicleFromCall, int visitNumber, int index, TimepointPredictionRecord prediction, long responseTimestamp) { MonitoredCallStructure monitoredCallStructure = new MonitoredCallStructure(); monitoredCallStructure.setVisitNumber(BigInteger.valueOf(visitNumber)); StopPointRefStructure stopPointRef = new StopPointRefStructure(); stopPointRef.setValue(stopBean.getId()); monitoredCallStructure.setStopPointRef(stopPointRef); NaturalLanguageStringStructure stopPoint = new NaturalLanguageStringStructure(); stopPoint.setValue(stopBean.getName()); monitoredCallStructure.setStopPointName(stopPoint); if (prediction != null) { // do not allow predicted times to be less than ResponseTimestamp if (prediction.getTimepointPredictedTime() < responseTimestamp) { /*/*from ww w. j ava 2 s. c o m*/ * monitoredCall has less precision than onwardCall (date vs. timestamp) * which results in a small amount of error when converting back to timestamp. * Add a second here to prevent negative values from showing up in the UI * (actual precision of the value is 1 minute, so a second has little influence) */ monitoredCallStructure.setExpectedArrivalTime(new Date(responseTimestamp + 1000)); monitoredCallStructure.setExpectedDepartureTime(new Date(responseTimestamp + 1000)); } else { monitoredCallStructure.setExpectedArrivalTime(new Date(prediction.getTimepointPredictedTime())); monitoredCallStructure.setExpectedDepartureTime(new Date(prediction.getTimepointPredictedTime())); } } // siri extensions SiriExtensionWrapper wrapper = new SiriExtensionWrapper(); ExtensionsStructure distancesExtensions = new ExtensionsStructure(); SiriDistanceExtension distances = new SiriDistanceExtension(); DecimalFormat df = new DecimalFormat(); df.setMaximumFractionDigits(2); df.setGroupingUsed(false); distances.setStopsFromCall(index); distances.setCallDistanceAlongRoute(Double.valueOf(df.format(distanceOfCallAlongTrip))); distances.setDistanceFromCall(Double.valueOf(df.format(distanceOfVehicleFromCall))); distances.setPresentableDistance(presentationService.getPresentableDistance(distances)); wrapper.setDistances(distances); distancesExtensions.setAny(wrapper); monitoredCallStructure.setExtensions(distancesExtensions); return monitoredCallStructure; }
From source file:org.matsim.contrib.drt.analysis.DynModeTripsAnalyser.java
public static void analyseWaitTimes(String fileName, List<DynModeTrip> trips, int binsize_s) { Collections.sort(trips);// ww w . j a v a 2 s .co m if (trips.size() == 0) return; int startTime = ((int) (trips.get(0).getDepartureTime() / binsize_s)) * binsize_s; int endTime = ((int) (trips.get(trips.size() - 1).getDepartureTime() / binsize_s) + binsize_s) * binsize_s; Map<Double, List<DynModeTrip>> splitTrips = splitTripsIntoBins(trips, startTime, endTime, binsize_s); DecimalFormat format = new DecimalFormat(); format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US)); format.setMinimumIntegerDigits(1); format.setMaximumFractionDigits(2); format.setGroupingUsed(false); SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss"); BufferedWriter bw = IOUtils.getBufferedWriter(fileName + ".csv"); TimeSeriesCollection dataset = new TimeSeriesCollection(); TimeSeriesCollection datasetrequ = new TimeSeriesCollection(); TimeSeries averageWaitC = new TimeSeries("average"); TimeSeries medianWait = new TimeSeries("median"); TimeSeries p_5Wait = new TimeSeries("5th percentile"); TimeSeries p_95Wait = new TimeSeries("95th percentile"); TimeSeries requests = new TimeSeries("Ride requests"); try { bw.write("timebin;trips;average_wait;min;p_5;p_25;median;p_75;p_95;max"); for (Entry<Double, List<DynModeTrip>> e : splitTrips.entrySet()) { long rides = 0; double averageWait = 0; double min = 0; double p_5 = 0; double p_25 = 0; double median = 0; double p_75 = 0; double p_95 = 0; double max = 0; if (!e.getValue().isEmpty()) { DescriptiveStatistics stats = new DescriptiveStatistics(); for (DynModeTrip t : e.getValue()) { stats.addValue(t.getWaitTime()); } rides = stats.getN(); averageWait = stats.getMean(); min = stats.getMin(); p_5 = stats.getPercentile(5); p_25 = stats.getPercentile(25); median = stats.getPercentile(50); p_75 = stats.getPercentile(75); p_95 = stats.getPercentile(95); max = stats.getMax(); } Minute h = new Minute(sdf2.parse(Time.writeTime(e.getKey()))); medianWait.addOrUpdate(h, Double.valueOf(median)); averageWaitC.addOrUpdate(h, Double.valueOf(averageWait)); p_5Wait.addOrUpdate(h, Double.valueOf(p_5)); p_95Wait.addOrUpdate(h, Double.valueOf(p_95)); requests.addOrUpdate(h, rides * 3600. / binsize_s);// normalised [req/h] bw.newLine(); bw.write(Time.writeTime(e.getKey()) + ";" + rides + ";" + format.format(averageWait) + ";" + format.format(min) + ";" + format.format(p_5) + ";" + format.format(p_25) + ";" + format.format(median) + ";" + format.format(p_75) + ";" + format.format(p_95) + ";" + format.format(max)); } bw.flush(); bw.close(); dataset.addSeries(averageWaitC); dataset.addSeries(medianWait); dataset.addSeries(p_5Wait); dataset.addSeries(p_95Wait); datasetrequ.addSeries(requests); JFreeChart chart = chartProfile(splitTrips.size(), dataset, "Waiting times", "Wait time (s)"); JFreeChart chart2 = chartProfile(splitTrips.size(), datasetrequ, "Ride requests per hour", "Requests per hour (req/h)"); ChartSaveUtils.saveAsPNG(chart, fileName, 1500, 1000); ChartSaveUtils.saveAsPNG(chart2, fileName + "_requests", 1500, 1000); } catch (IOException | ParseException e) { e.printStackTrace(); } }
From source file:com.itude.mobile.android.util.StringUtil.java
/** * Format {@link String} as a Volume/*from ww w .j ava2 s.co m*/ * * WARNING: Only use this method to present data to the screen * * @param locale {@link Locale} * @param stringToFormat {@link String} to format * @return a string formatted as a volume with group separators (eg, 131.224.000) assuming the receiver is an int string read from XML */ public static String formatVolume(Locale locale, String stringToFormat) { if (stringToFormat == null || stringToFormat.length() == 0) { return null; } String result = null; DecimalFormat formatter = new DecimalFormat(); formatter.setDecimalFormatSymbols(new DecimalFormatSymbols(locale)); formatter.setGroupingUsed(true); formatter.setGroupingSize(3); formatter.setMaximumFractionDigits(0); result = formatter.format(Double.parseDouble(stringToFormat)); return result; }
From source file:org.mitre.ccv.mapred.CompleteCompositionVectorUtils.java
/** * Writes out the {@link SequenceFile} feature vectors in row major (packed) order. No labels are outputed. * * @param jobConf//from w w w . ja v a 2s . c o m * @param input top level SequenceFile directory path * @param output path to output the matrix * @param digits the maximum number of fraction digits * @throws IOException */ public static void featureVectors2RowMajorMatrix(JobConf jobConf, String input, String output, int digits) throws IOException { JobConf conf = new JobConf(jobConf, CalculateCosineDistanceMatrix.class); DecimalFormat format = new DecimalFormat(); format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US)); format.setMinimumIntegerDigits(1); format.setMaximumFractionDigits(digits); //format.setMinimumFractionDigits(fractionDigits); format.setGroupingUsed(false); final Path inputPath = new Path(input); final FileSystem fs = inputPath.getFileSystem(conf); final Path qInputPath = fs.makeQualified(inputPath); final Path outputPath = new Path(output); Path[] paths = FileUtils.ls(conf, qInputPath.toString() + Path.SEPARATOR + "part-*"); FSDataOutputStream fos = fs.create(outputPath, true); // throws nothing! final Writer writer = new OutputStreamWriter(fos); final Text key = new Text(); final SparseVectorWritable value = new SparseVectorWritable(); for (int idx = 0; idx < paths.length; idx++) { SequenceFile.Reader reader = new SequenceFile.Reader(fs, paths[idx], conf); boolean hasNext = reader.next(key, value); while (hasNext) { final SparseVector vector = value.get(); final StringBuilder sb = new StringBuilder(); for (int i = 0; i < vector.getCardinality(); i++) { final String s = format.format(vector.get(i)); // format the number sb.append(s); sb.append(' '); } writer.write(sb.toString()); hasNext = reader.next(key, value); } try { writer.flush(); reader.close(); } catch (IOException ioe) { // closing the SequenceFile.Reader will throw an exception if the file is over some unknown size LOG.debug("Probably caused by closing the SequenceFile.Reader. All is well", ioe); } } try { writer.close(); fos.flush(); fos.close(); } catch (IOException ioe) { LOG.debug("Caused by distributed cache output stream.", ioe); } }
From source file:org.onebusaway.presentation.impl.realtime.SiriSupport.java
private static OnwardCallStructure getOnwardCallStructure(StopBean stopBean, PresentationService presentationService, double distanceOfCallAlongTrip, double distanceOfVehicleFromCall, int visitNumber, int index, TimepointPredictionRecord prediction, boolean hasRealtimeData, long responseTimestamp, long scheduledArrivalTime) { OnwardCallStructure onwardCallStructure = new OnwardCallStructure(); onwardCallStructure.setVisitNumber(BigInteger.valueOf(visitNumber)); StopPointRefStructure stopPointRef = new StopPointRefStructure(); stopPointRef.setValue(stopBean.getId()); onwardCallStructure.setStopPointRef(stopPointRef); if (stopBean.getCode() != null) { // Agency's prefer stop code display in UI, so override platform name for this use NaturalLanguageStringStructure platform = new NaturalLanguageStringStructure(); platform.setValue(stopBean.getCode()); onwardCallStructure.setArrivalPlatformName(platform); }//from w w w . java 2 s. co m NaturalLanguageStringStructure stopPoint = new NaturalLanguageStringStructure(); stopPoint.setValue(stopBean.getName()); onwardCallStructure.setStopPointName(stopPoint); if (prediction != null) { if (prediction.getTimepointPredictedArrivalTime() < responseTimestamp) { onwardCallStructure.setExpectedArrivalTime(new Date(responseTimestamp)); onwardCallStructure.setExpectedDepartureTime(new Date(responseTimestamp)); } else { onwardCallStructure.setExpectedArrivalTime(new Date(prediction.getTimepointPredictedArrivalTime())); onwardCallStructure .setExpectedDepartureTime(new Date(prediction.getTimepointPredictedDepartureTime())); } } else if (!hasRealtimeData) { _log.debug("using arrival time of " + new Date(scheduledArrivalTime)); onwardCallStructure.setExpectedArrivalTime(new Date(scheduledArrivalTime)); onwardCallStructure.setExpectedDepartureTime(new Date(scheduledArrivalTime)); } // siri extensions SiriExtensionWrapper wrapper = new SiriExtensionWrapper(); ExtensionsStructure distancesExtensions = new ExtensionsStructure(); SiriDistanceExtension distances = new SiriDistanceExtension(); DecimalFormat df = new DecimalFormat(); df.setMaximumFractionDigits(2); df.setGroupingUsed(false); distances.setStopsFromCall(index); distances.setCallDistanceAlongRoute(NumberUtils.toDouble(df.format(distanceOfCallAlongTrip))); distances.setDistanceFromCall(NumberUtils.toDouble(df.format(distanceOfVehicleFromCall))); distances.setPresentableDistance(presentationService.getPresentableDistance(distances)); wrapper.setDistances(distances); distancesExtensions.setAny(wrapper); onwardCallStructure.setExtensions(distancesExtensions); return onwardCallStructure; }
From source file:org.mitre.ccv.mapred.CalculateCosineDistanceMatrix.java
/** * Writes out the matrix in row major (packed) order. No labels are outputed. * * @param jobConf//from w ww . j a v a2 s . c o m * @param input * @param output * @param digits * @throws IOException */ public static void printRowMajorMatrix(JobConf jobConf, String input, String output, int digits) throws IOException { JobConf conf = new JobConf(jobConf, CalculateCosineDistanceMatrix.class); DecimalFormat format = new DecimalFormat(); format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US)); format.setMinimumIntegerDigits(1); format.setMaximumFractionDigits(digits); //format.setMinimumFractionDigits(fractionDigits); format.setGroupingUsed(false); final Path inputPath = new Path(input); final FileSystem fs = inputPath.getFileSystem(conf); final Path qInputPath = fs.makeQualified(inputPath); final Path outputPath = new Path(output); Path[] paths = FileUtils.ls(conf, qInputPath.toString() + Path.SEPARATOR + "part-*"); FSDataOutputStream fos = fs.create(outputPath, true); // throws nothing! final Writer writer = new OutputStreamWriter(fos); final Text key = new Text(); final DenseVectorWritable value = new DenseVectorWritable(); for (int idx = 0; idx < paths.length; idx++) { SequenceFile.Reader reader = new SequenceFile.Reader(fs, paths[idx], conf); boolean hasNext = reader.next(key, value); while (hasNext) { final DenseVector vector = value.get(); final StringBuilder sb = new StringBuilder(); for (int i = 0; i < vector.getCardinality(); i++) { final String s = format.format(vector.get(i)); // format the number sb.append(s); sb.append(' '); } writer.write(sb.toString()); hasNext = reader.next(key, value); } try { writer.flush(); reader.close(); } catch (IOException ioe) { // closing the SequenceFile.Reader will throw an exception if the file is over some unknown size LOG.debug("Probably caused by closing the SequenceFile.Reader. All is well", ioe); } } try { writer.close(); fos.flush(); fos.close(); } catch (IOException ioe) { LOG.debug("Caused by distributed cache output stream.", ioe); } }
From source file:org.onebusaway.presentation.impl.realtime.SiriSupport.java
private static MonitoredCallStructure getMonitoredCallStructure(StopBean stopBean, PresentationService presentationService, double distanceOfCallAlongTrip, double distanceOfVehicleFromCall, int visitNumber, int index, TimepointPredictionRecord prediction, boolean hasRealtimeData, long responseTimestamp, long scheduledArrivalTime) { MonitoredCallStructure monitoredCallStructure = new MonitoredCallStructure(); monitoredCallStructure.setVisitNumber(BigInteger.valueOf(visitNumber)); StopPointRefStructure stopPointRef = new StopPointRefStructure(); stopPointRef.setValue(stopBean.getId()); monitoredCallStructure.setStopPointRef(stopPointRef); NaturalLanguageStringStructure stopPoint = new NaturalLanguageStringStructure(); stopPoint.setValue(stopBean.getName()); monitoredCallStructure.setStopPointName(stopPoint); if (prediction != null) { if (!hasRealtimeData) { monitoredCallStructure.setExpectedArrivalTime(new Date(prediction.getTimepointScheduledTime())); monitoredCallStructure.setExpectedDepartureTime(new Date(prediction.getTimepointScheduledTime())); }/* w w w . j a v a 2s . c om*/ // do not allow predicted times to be less than ResponseTimestamp else if (prediction.getTimepointPredictedArrivalTime() < responseTimestamp) { /* * monitoredCall has less precision than onwardCall (date vs. timestamp) * which results in a small amount of error when converting back to timestamp. * Add a second here to prevent negative values from showing up in the UI * (actual precision of the value is 1 minute, so a second has little influence) */ monitoredCallStructure.setExpectedArrivalTime(new Date(responseTimestamp + 1000)); monitoredCallStructure.setExpectedDepartureTime(new Date(responseTimestamp + 1000)); } else { monitoredCallStructure .setExpectedArrivalTime(new Date(prediction.getTimepointPredictedArrivalTime())); monitoredCallStructure .setExpectedDepartureTime(new Date(prediction.getTimepointPredictedArrivalTime())); } } else if (!hasRealtimeData) { monitoredCallStructure.setExpectedArrivalTime(new Date(scheduledArrivalTime)); monitoredCallStructure.setExpectedDepartureTime(new Date(scheduledArrivalTime)); } //setting the scheduled arrival time. if (monitoredCallStructure.getExpectedArrivalTime() != null) { monitoredCallStructure.setAimedArrivalTime(new Date(scheduledArrivalTime)); } // siri extensions SiriExtensionWrapper wrapper = new SiriExtensionWrapper(); ExtensionsStructure distancesExtensions = new ExtensionsStructure(); SiriDistanceExtension distances = new SiriDistanceExtension(); DecimalFormat df = new DecimalFormat(); df.setMaximumFractionDigits(2); df.setGroupingUsed(false); distances.setStopsFromCall(index); distances.setCallDistanceAlongRoute(NumberUtils.toDouble(df.format(distanceOfCallAlongTrip))); distances.setDistanceFromCall(NumberUtils.toDouble(df.format(distanceOfVehicleFromCall))); distances.setPresentableDistance(presentationService.getPresentableDistance(distances)); wrapper.setDistances(distances); distancesExtensions.setAny(wrapper); monitoredCallStructure.setExtensions(distancesExtensions); return monitoredCallStructure; }
From source file:org.mitre.ccv.mapred.CalculateCosineDistanceMatrix.java
/** * Outputs the distance matrix (DenseVectors) in Phylip Square format. Names/labels are limited to 10-characters! * * @param jobConf//from w ww .j a v a 2s. com * @param input input directory name containing DenseVectors (as generated by this class). * @param output output file name * @param fractionDigits number of digits after decimal point * @throws IOException */ public static void printPhylipSquare(JobConf jobConf, String input, String output, int fractionDigits) throws IOException { JobConf conf = new JobConf(jobConf, CalculateCosineDistanceMatrix.class); DecimalFormat format = new DecimalFormat(); format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US)); format.setMinimumIntegerDigits(1); format.setMaximumFractionDigits(fractionDigits); //format.setMinimumFractionDigits(fractionDigits); format.setGroupingUsed(false); final Path inputPath = new Path(input); final FileSystem fs = inputPath.getFileSystem(conf); final Path qInputPath = fs.makeQualified(inputPath); final Path outputPath = new Path(output); Path[] paths = FileUtils.ls(conf, qInputPath.toString() + Path.SEPARATOR + "part-*"); FSDataOutputStream fos = fs.create(outputPath, true); // throws nothing! Writer writer = new OutputStreamWriter(fos); Text key = new Text(); DenseVectorWritable value = new DenseVectorWritable(); Boolean wroteHeader = false; for (int idx = 0; idx < paths.length; idx++) { SequenceFile.Reader reader = new SequenceFile.Reader(fs, paths[idx], conf); boolean hasNext = reader.next(key, value); while (hasNext) { final DenseVector vector = value.get(); if (!wroteHeader) { writer.write(String.format("\t%d\n", vector.getCardinality())); wroteHeader = true; } final StringBuilder sb = new StringBuilder(); final String name = key.toString(); sb.append(name.substring(0, (name.length() > 10 ? 10 : name.length()))); final int padding = Math.max(1, 10 - name.length()); for (int k = 0; k < padding; k++) { sb.append(' '); } sb.append(' '); for (int i = 0; i < vector.getCardinality(); i++) { final String s = format.format(vector.get(i)); // format the number sb.append(s); sb.append(' '); } sb.append("\n"); writer.write(sb.toString()); hasNext = reader.next(key, value); } try { writer.flush(); reader.close(); } catch (IOException ioe) { // closing the SequenceFile.Reader will throw an exception if the file is over some unknown size LOG.debug("Probably caused by closing the SequenceFile.Reader. All is well", ioe); } } try { writer.close(); fos.flush(); fos.close(); } catch (IOException ioe) { LOG.debug("Caused by distributed cache output stream.", ioe); } }