List of usage examples for java.lang Double isFinite
public static boolean isFinite(double d)
From source file:com.bytelightning.opensource.pokerface.PokerFace.java
/** * Utility method that allows us to be very flexible in how many reactor processors are allocated to a reactor. * @param num If not a finite number, returns 1. * If <= 0, returns the actual number of Runtime.getRuntime().availableProcessors(). * If >= 1, returns the minimum of <code>num</code> or (Runtime.getRuntime().availableProcessors(). * 2) * If > 0 && < 1, returns (num * Runtime.getRuntime().availableProcessors()). * @return The translated number of IOProcessors for a reactor. */// w w w . j av a2s .co m private static int ComputeReactorProcessors(double num) { if (!Double.isFinite(num)) num = 1d; if (num <= 0) return Runtime.getRuntime().availableProcessors(); if (num >= (1d - Double.MIN_VALUE)) return Math.min((int) Math.rint(num), (Runtime.getRuntime().availableProcessors() * 2)); return Math.max(1, (int) Math.rint(Runtime.getRuntime().availableProcessors() * num)); }
From source file:gov.noaa.pfel.erddap.dataset.EDDTableFromNcFiles.java
/** * This generates a ready-to-use datasets.xml entry for an EDDTableFromNcFiles. * The XML can then be edited by hand and added to the datasets.xml file. * * <p>This can't be made into a web service because it would allow any user * to looks at (possibly) private .nc files on the server. * * @param tFileDir the starting (parent) directory for searching for files * @param tFileNameRegex the regex that each filename (no directory info) must match * (e.g., ".*\\.nc") (usually only 1 backslash; 2 here since it is Java code). * If null or "", it is generated to catch the same extension as the sampleFileName * (usually ".*\\.nc").//from w w w . j a va 2 s.co m * @param sampleFileName the full file name of one of the files in the collection * @param useDimensionsCSV If null or "", this finds the group of variables sharing the * highest number of dimensions. Otherwise, it find the variables using * these dimensions (plus related char variables). * @param tReloadEveryNMinutes e.g., 10080 for weekly * @param tPreExtractRegex part of info for extracting e.g., stationName from file name. Set to "" if not needed. * @param tPostExtractRegex part of info for extracting e.g., stationName from file name. Set to "" if not needed. * @param tExtractRegex part of info for extracting e.g., stationName from file name. Set to "" if not needed. * @param tColumnNameForExtract part of info for extracting e.g., stationName from file name. Set to "" if not needed. * @param tSortedColumnSourceName use "" if not known or not needed. * @param tSortFilesBySourceNames This is useful, because it ultimately determines default results order. * @param tInfoUrl or "" if in externalAddGlobalAttributes or if not available * @param tInstitution or "" if in externalAddGlobalAttributes or if not available * @param tSummary or "" if in externalAddGlobalAttributes or if not available * @param tTitle or "" if in externalAddGlobalAttributes or if not available * @param externalAddGlobalAttributes These attributes are given priority. Use null in none available. * @return a suggested chunk of xml for this dataset for use in datasets.xml * @throws Throwable if trouble, e.g., if no Grid or Array variables are found. * If no trouble, then a valid dataset.xml chunk has been returned. */ public static String generateDatasetsXml(String tFileDir, String tFileNameRegex, String sampleFileName, String useDimensionsCSV, int tReloadEveryNMinutes, String tPreExtractRegex, String tPostExtractRegex, String tExtractRegex, String tColumnNameForExtract, String tSortedColumnSourceName, String tSortFilesBySourceNames, String tInfoUrl, String tInstitution, String tSummary, String tTitle, Attributes externalAddGlobalAttributes) throws Throwable { String2.log("\n*** EDDTableFromNcFiles.generateDatasetsXml" + "\nfileDir=" + tFileDir + " fileNameRegex=" + tFileNameRegex + "\nsampleFileName=" + sampleFileName + "\nuseDimensionsCSV=" + useDimensionsCSV + " reloadEveryNMinutes=" + tReloadEveryNMinutes + "\nextract pre=" + tPreExtractRegex + " post=" + tPostExtractRegex + " regex=" + tExtractRegex + " colName=" + tColumnNameForExtract + "\nsortedColumn=" + tSortedColumnSourceName + " sortFilesBy=" + tSortFilesBySourceNames + "\ninfoUrl=" + tInfoUrl + "\ninstitution=" + tInstitution + "\nsummary=" + tSummary + "\ntitle=" + tTitle + "\nexternalAddGlobalAttributes=" + externalAddGlobalAttributes); if (!String2.isSomething(tFileDir)) throw new IllegalArgumentException("fileDir wasn't specified."); tFileDir = File2.addSlash(tFileDir); //ensure it has trailing slash String[] useDimensions = StringArray.arrayFromCSV(useDimensionsCSV); tColumnNameForExtract = String2.isSomething(tColumnNameForExtract) ? tColumnNameForExtract.trim() : ""; tSortedColumnSourceName = String2.isSomething(tSortedColumnSourceName) ? tSortedColumnSourceName.trim() : ""; if (!String2.isSomething(sampleFileName)) String2.log("Found/using sampleFileName=" + (sampleFileName = FileVisitorDNLS.getSampleFileName(tFileDir, tFileNameRegex, true, ".*"))); //recursive, pathRegex //show structure of sample file String2.log("Let's see if netcdf-java can tell us the structure of the sample file:"); String2.log(NcHelper.dumpString(sampleFileName, false)); //*** basically, make a table to hold the sourceAttributes //and a parallel table to hold the addAttributes Table dataSourceTable = new Table(); Table dataAddTable = new Table(); //new way StringArray varNames = new StringArray(); double maxTimeES = Double.NaN; if (useDimensions.length > 0) { //find the varNames NetcdfFile ncFile = NcHelper.openFile(sampleFileName); try { Group rootGroup = ncFile.getRootGroup(); List rootGroupVariables = rootGroup.getVariables(); for (int v = 0; v < rootGroupVariables.size(); v++) { Variable var = (Variable) rootGroupVariables.get(v); boolean isChar = var.getDataType() == DataType.CHAR; if (var.getRank() + (isChar ? -1 : 0) == useDimensions.length) { boolean matches = true; for (int d = 0; d < useDimensions.length; d++) { if (!var.getDimension(d).getFullName().equals(useDimensions[d])) { matches = false; break; } } if (matches) varNames.add(var.getFullName()); } } ncFile.close(); } catch (Exception e) { //make sure ncFile is explicitly closed try { ncFile.close(); } catch (Exception e2) { //don't care } String2.log(MustBe.throwableToString(e)); } Test.ensureTrue(varNames.size() > 0, "The file has no variables with dimensions: " + useDimensionsCSV); } //then read the file dataSourceTable.readNDNc(sampleFileName, varNames.toStringArray(), null, 0, 0, true); //getMetadata for (int c = 0; c < dataSourceTable.nColumns(); c++) { String colName = dataSourceTable.getColumnName(c); Attributes sourceAtts = dataSourceTable.columnAttributes(c); PrimitiveArray pa = makeDestPAForGDX(dataSourceTable.getColumn(c), sourceAtts); dataAddTable.addColumn(c, colName, pa, makeReadyToUseAddVariableAttributesForDatasetsXml( dataSourceTable.globalAttributes(), sourceAtts, null, colName, true, true)); //addColorBarMinMax, tryToFindLLAT //if a variable has timeUnits, files are likely sorted by time //and no harm if files aren't sorted that way String tUnits = sourceAtts.getString("units"); if (tSortedColumnSourceName.length() == 0 && Calendar2.isTimeUnits(tUnits)) tSortedColumnSourceName = colName; if (!Double.isFinite(maxTimeES) && Calendar2.isTimeUnits(tUnits)) { try { if (Calendar2.isNumericTimeUnits(tUnits)) { double tbf[] = Calendar2.getTimeBaseAndFactor(tUnits); //throws exception maxTimeES = Calendar2.unitsSinceToEpochSeconds(tbf[0], tbf[1], pa.getDouble(pa.size() - 1)); } else { //string time units maxTimeES = Calendar2.tryToEpochSeconds(pa.getString(pa.size() - 1)); //NaN if trouble } } catch (Throwable t) { String2.log("caught while trying to get maxTimeES: " + MustBe.throwableToString(t)); } } } //String2.log("SOURCE COLUMN NAMES=" + dataSourceTable.getColumnNamesCSSVString()); //String2.log("DEST COLUMN NAMES=" + dataSourceTable.getColumnNamesCSSVString()); //globalAttributes if (externalAddGlobalAttributes == null) externalAddGlobalAttributes = new Attributes(); if (tInfoUrl != null && tInfoUrl.length() > 0) externalAddGlobalAttributes.add("infoUrl", tInfoUrl); if (tInstitution != null && tInstitution.length() > 0) externalAddGlobalAttributes.add("institution", tInstitution); if (tSummary != null && tSummary.length() > 0) externalAddGlobalAttributes.add("summary", tSummary); if (tTitle != null && tTitle.length() > 0) externalAddGlobalAttributes.add("title", tTitle); externalAddGlobalAttributes.setIfNotAlreadySet("sourceUrl", "(" + (String2.isRemote(tFileDir) ? "remote" : "local") + " files)"); //tryToFindLLAT tryToFindLLAT(dataSourceTable, dataAddTable); //externalAddGlobalAttributes.setIfNotAlreadySet("subsetVariables", "???"); //after dataVariables known, add global attributes in the dataAddTable dataAddTable.globalAttributes() .set(makeReadyToUseAddGlobalAttributesForDatasetsXml(dataSourceTable.globalAttributes(), //another cdm_data_type could be better; this is ok hasLonLatTime(dataAddTable) ? "Point" : "Other", tFileDir, externalAddGlobalAttributes, suggestKeywords(dataSourceTable, dataAddTable))); //subsetVariables if (dataSourceTable.globalAttributes().getString("subsetVariables") == null && dataAddTable.globalAttributes().getString("subsetVariables") == null) dataAddTable.globalAttributes().add("subsetVariables", suggestSubsetVariables(dataSourceTable, dataAddTable, false)); //add the columnNameForExtract variable if (tColumnNameForExtract.length() > 0) { Attributes atts = new Attributes(); atts.add("ioos_category", "Identifier"); atts.add("long_name", EDV.suggestLongName(null, tColumnNameForExtract, null)); //no units or standard_name dataSourceTable.addColumn(0, tColumnNameForExtract, new StringArray(), new Attributes()); dataAddTable.addColumn(0, tColumnNameForExtract, new StringArray(), atts); } //useMaxTimeES if (tReloadEveryNMinutes <= 0 || tReloadEveryNMinutes == Integer.MAX_VALUE) tReloadEveryNMinutes = 1440; //1440 works well with suggestedUpdateEveryNMillis String tTestOutOfDate = EDD.getAddOrSourceAtt(dataSourceTable.globalAttributes(), dataAddTable.globalAttributes(), "testOutOfDate", null); if (Double.isFinite(maxTimeES) && !String2.isSomething(tTestOutOfDate)) { tTestOutOfDate = suggestTestOutOfDate(maxTimeES); if (String2.isSomething(tTestOutOfDate)) dataAddTable.globalAttributes().set("testOutOfDate", tTestOutOfDate); } //write the information StringBuilder sb = new StringBuilder(); String suggestedRegex = (tFileNameRegex == null || tFileNameRegex.length() == 0) ? ".*\\" + File2.getExtension(sampleFileName) : tFileNameRegex; if (tSortFilesBySourceNames.length() == 0) { if (tColumnNameForExtract.length() > 0 && tSortedColumnSourceName.length() > 0 && !tColumnNameForExtract.equals(tSortedColumnSourceName)) tSortFilesBySourceNames = tColumnNameForExtract + ", " + tSortedColumnSourceName; else if (tColumnNameForExtract.length() > 0) tSortFilesBySourceNames = tColumnNameForExtract; else tSortFilesBySourceNames = tSortedColumnSourceName; } sb.append(directionsForGenerateDatasetsXml() + "-->\n\n" + "<dataset type=\"EDDTableFromNcFiles\" datasetID=\"" + suggestDatasetID(tFileDir + suggestedRegex) + //dirs can't be made public "\" active=\"true\">\n" + " <reloadEveryNMinutes>" + tReloadEveryNMinutes + "</reloadEveryNMinutes>\n" + " <updateEveryNMillis>" + suggestUpdateEveryNMillis(tFileDir) + "</updateEveryNMillis>\n" + " <fileDir>" + XML.encodeAsXML(tFileDir) + "</fileDir>\n" + " <fileNameRegex>" + XML.encodeAsXML(suggestedRegex) + "</fileNameRegex>\n" + " <recursive>true</recursive>\n" + " <pathRegex>.*</pathRegex>\n" + " <metadataFrom>last</metadataFrom>\n" + " <preExtractRegex>" + XML.encodeAsXML(tPreExtractRegex) + "</preExtractRegex>\n" + " <postExtractRegex>" + XML.encodeAsXML(tPostExtractRegex) + "</postExtractRegex>\n" + " <extractRegex>" + XML.encodeAsXML(tExtractRegex) + "</extractRegex>\n" + " <columnNameForExtract>" + XML.encodeAsXML(tColumnNameForExtract) + "</columnNameForExtract>\n" + " <sortedColumnSourceName>" + XML.encodeAsXML(tSortedColumnSourceName) + "</sortedColumnSourceName>\n" + " <sortFilesBySourceNames>" + XML.encodeAsXML(tSortFilesBySourceNames) + "</sortFilesBySourceNames>\n" + " <fileTableInMemory>false</fileTableInMemory>\n" + " <accessibleViaFiles>false</accessibleViaFiles>\n"); sb.append(writeAttsForDatasetsXml(false, dataSourceTable.globalAttributes(), " ")); sb.append(cdmSuggestion()); sb.append(writeAttsForDatasetsXml(true, dataAddTable.globalAttributes(), " ")); //last 2 params: includeDataType, questionDestinationName sb.append(writeVariablesForDatasetsXml(dataSourceTable, dataAddTable, "dataVariable", true, false)); sb.append("</dataset>\n" + "\n"); String2.log("\n\n*** generateDatasetsXml finished successfully.\n\n"); return sb.toString(); }
From source file:de.bund.bfr.knime.nls.fitting.FittingNodeModel.java
private static Pair<ListMultimap<String, Double>, ListMultimap<String, Double>> readDataTable( BufferedDataTable table, Function f) { ListMultimap<String, Double> timeValues = ArrayListMultimap.create(); ListMultimap<String, Double> targetValues = ArrayListMultimap.create(); for (DataRow row : table) { String id = IO.getString(row.getCell(table.getSpec().findColumnIndex(NlsUtils.ID_COLUMN))); Double time = IO.getDouble(row.getCell(table.getSpec().findColumnIndex(f.getTimeVariable()))); Double target = IO.getDouble(row.getCell(table.getSpec().findColumnIndex(f.getDependentVariable()))); if (id != null && time != null && target != null && Double.isFinite(time) && Double.isFinite(target)) { timeValues.put(id, time);/*from ww w . j av a 2s . c o m*/ targetValues.put(id, target); } } return new Pair<>(timeValues, targetValues); }
From source file:com.arpnetworking.metrics.mad.parsers.JsonToRecordParser.java
private static Quantity version2gSampleToQuantity(final Version2g.Sample sample) { if (sample != null) { if (Double.isFinite(sample.getValue())) { final CompositeUnit sampleUnit = sample.getUnit2g() != null ? Iterables.getFirst(sample.getUnit2g().getNumerators(), null) : null;//from w w w . j ava 2 s. c om return new Quantity.Builder().setValue(sample.getValue()).setUnit(getLegacyUnit(sampleUnit)) // TODO(vkoskela): Support compound units in Tsd Aggregator //.setNumerator(sampleNumerator) // same as sampleUnit above //.setDenominator(sampleDenominator) .build(); } else { // TODO(barp): Create a counter for invalid metrics INVALID_SAMPLE_LOGGER.warn().setMessage("Invalid sample for metric") .addData("value", sample.getValue()).log(); return null; } } else { return null; } }
From source file:de.bund.bfr.knime.nls.fitting.FittingNodeModel.java
private static Map<String, ListMultimap<String, Double>> readConditionTable(BufferedDataTable table, Function f) {//from w w w . ja v a 2s .c o m Map<String, ListMultimap<String, Double>> argumentValues = new LinkedHashMap<>(); for (String indep : f.getIndependentVariables()) { argumentValues.put(indep, ArrayListMultimap.create()); } loop: for (DataRow row : table) { String id = IO.getString(row.getCell(table.getSpec().findColumnIndex(NlsUtils.ID_COLUMN))); if (id == null) { continue loop; } Map<String, Double> values = new LinkedHashMap<>(); for (String var : f.getIndependentVariables()) { Double value = IO.getDouble(row.getCell(table.getSpec().findColumnIndex(var))); if (value == null || !Double.isFinite(value)) { continue loop; } values.put(var, value); } for (String indep : f.getIndependentVariables()) { argumentValues.get(indep).put(id, values.get(indep)); } } return argumentValues; }
From source file:com.simiacryptus.mindseye.lang.Tensor.java
/** * Set./* ww w .j a v a2s . c o m*/ * * @param coords the coords * @param value the value */ public void set(@Nonnull final Coordinate coords, final double value) { if (Double.isFinite(value)) set(coords.getIndex(), value); }
From source file:com.simiacryptus.mindseye.lang.Tensor.java
/** * Set.// ww w .jav a 2 s . c o m * * @param coord1 the coord 1 * @param coord2 the coord 2 * @param value the value */ public void set(final int coord1, final int coord2, final double value) { assert Double.isFinite(value); set(index(coord1, coord2), value); }
From source file:com.simiacryptus.mindseye.lang.Tensor.java
/** * Set.//from www . j a v a2 s.c o m * * @param coord1 the coord 1 * @param coord2 the coord 2 * @param coord3 the coord 3 * @param value the value */ public void set(final int coord1, final int coord2, final int coord3, final double value) { assert Double.isFinite(value); set(index(coord1, coord2, coord3), value); }
From source file:com.simiacryptus.mindseye.lang.Tensor.java
/** * Set.//from w w w .ja v a 2 s. com * * @param coord1 the coord 1 * @param coord2 the coord 2 * @param coord3 the coord 3 * @param coord4 the coord 4 * @param value the value */ public void set(final int coord1, final int coord2, final int coord3, final int coord4, final double value) { assert Double.isFinite(value); set(index(coord1, coord2, coord3, coord4), value); }
From source file:AnalysisModule.DataAnalysis.java
protected void bitmapAnalyse(List<Scenario> lstScenario) throws Exception { BitMap bitMap;//www. j ava 2 s .c om String bmpDir; int traffic; int numberOfFiles; FileInputStream fin = null; ObjectInputStream ois = null; for (Scenario scenario : lstScenario) { for (Topology topology : scenario.lstTopology) { for (Instance instance : topology.getLstInstance()) { instance.trafficMatrix = new double[topology.getNumberOfSwitches()][topology .getNumberOfSwitches()]; } } } for (Scenario scenario : lstScenario) { for (Topology topology : scenario.lstTopology) { System.out.println("Analisando Topologia: " + topology.getIdTopology()); for (Instance instance : topology.getLstInstance()) { System.out.println(" Instancia: " + instance.getId()); for (int i = 0; i < topology.getNumberOfSwitches(); i++) { for (int j = 0; j < topology.getNumberOfSwitches(); j++) { switch (instance.type) { case BITMAP: { if (j <= i) { break; } TreeMap<Long, BitMap> sourceBitMapTree = new TreeMap<>(); TreeMap<Long, BitMap> destinationBitMapTree = new TreeMap<>(); bmpDir = getSrcDir(instance, i); numberOfFiles = new File(bmpDir).listFiles().length - 1; int firstBitmap = -2; for (int numberOfBmp = 0; numberOfBmp < numberOfFiles; numberOfBmp++) { fin = new FileInputStream(bmpDir + "BitMap" + numberOfBmp + ".bmp"); ois = new ObjectInputStream(fin); bitMap = (BitMap) ois.readObject(); if ((bitMap.getStartEpoch() - 21600000000L > scenario.startTime) && (bitMap.getStartEpoch() - 21600000000L < scenario.endTime)) { if (firstBitmap == -2) { firstBitmap = numberOfBmp - 1; } sourceBitMapTree.put(bitMap.getStartEpoch() - 21600000000L, bitMap); } ois.close(); fin.close(); } //Add the first bitmap in the Measurement Interval if (firstBitmap >= 0) { fin = new FileInputStream(bmpDir + "BitMap" + firstBitmap + ".bmp"); ois = new ObjectInputStream(fin); bitMap = (BitMap) ois.readObject(); sourceBitMapTree.put(bitMap.getStartEpoch() - 21600000000L, bitMap); ois.close(); fin.close(); } bmpDir = getDestDir(instance, j); numberOfFiles = new File(bmpDir).listFiles().length - 1; firstBitmap = -2; for (int numberOfBmp = 0; numberOfBmp < numberOfFiles; numberOfBmp++) { fin = new FileInputStream(bmpDir + "BitMap" + numberOfBmp + ".bmp"); ois = new ObjectInputStream(fin); bitMap = (BitMap) ois.readObject(); if ((bitMap.getStartEpoch() - 21600000000L > scenario.startTime) && (bitMap.getStartEpoch() - 21600000000L < scenario.endTime)) { if (firstBitmap == -2) { firstBitmap = numberOfBmp - 1; } destinationBitMapTree.put(bitMap.getStartEpoch() - 21600000000L, bitMap); } ois.close(); fin.close(); } //Add the first bitmap in the Measurement Interval if (firstBitmap >= 0) { fin = new FileInputStream(bmpDir + "BitMap" + firstBitmap + ".bmp"); ois = new ObjectInputStream(fin); bitMap = (BitMap) ois.readObject(); destinationBitMapTree.put(bitMap.getStartEpoch() - 21600000000L, bitMap); ois.close(); fin.close(); } //Estimation int k1 = sourceBitMapTree.size(); int k2 = destinationBitMapTree.size(); Collection sourceEntrySet = sourceBitMapTree.entrySet(); Iterator sourceEntries = sourceEntrySet.iterator(); for (int q = 0; q < k1; q++) { Map.Entry entrySrc = (Map.Entry) sourceEntries.next(); BitMap bmpSrc = (BitMap) entrySrc.getValue(); Collection destinationEntrySet = destinationBitMapTree.entrySet(); Iterator destinationEntries = destinationEntrySet.iterator(); for (int r = 0; r < k2; r++) { Map.Entry entryDst = (Map.Entry) destinationEntries.next(); BitMap bmpDst = (BitMap) entryDst.getValue(); boolean overlap = bmpSrc.getStartEpoch() <= bmpDst.getEndEpoch() && bmpSrc.getEndEpoch() >= bmpDst.getStartEpoch(); if (overlap) { double sourceDTr = instance.getBitMapSize() * Math.log(((double) instance.getBitMapSize()) / (instance.getBitMapSize() - bmpSrc.occupancy())); double destinationDTr = instance.getBitMapSize() * Math.log(((double) instance.getBitMapSize()) / (instance.getBitMapSize() - bmpDst.occupancy())); BitSet orSrcDst = (BitSet) bmpSrc.getBitSet().clone(); //BitSet andSrcDst = (BitSet) bmpSrc.getBitSet().clone(); orSrcDst.or(bmpDst.getBitSet()); //andSrcDst.and(bmpDst.getBitSet()); double orDTr = instance.getBitMapSize() * Math.log(((double) instance.getBitMapSize()) / (instance.getBitMapSize() - orSrcDst.cardinality())); //double andDTr = instance.getBitMapSize() * Math.log(((double) instance.getBitMapSize()) / (instance.getBitMapSize() - andSrcDst.cardinality())); double estimation = 0D; if (Double.isFinite(orDTr)) { estimation = sourceDTr + destinationDTr - orDTr; //estimation = (bmpSrc.getNumberOfPackets()/sourceDTr) * estimation; //estimation = andDTr; } instance.trafficMatrix[i][j] += estimation; } } } break; } case COUNTER_ARRAY: { traffic = 0; traffic += instance.networkSwitch.get(i).arrayCounter[i][j]; //traffic += instance.networkSwitch.get(i).arrayCounter[j][i]; instance.doCalculateMatrixElem(i, j, topology, traffic); break; } case OPT_COUNTER_ARRAY: { traffic = 0; for (Integer node : topology.getPathNodes(i, j)) { if (instance.networkSwitch.get(node).isObserver) { traffic += instance.networkSwitch.get(node).arrayCounter[i][j]; //traffic += instance.networkSwitch.get(node).arrayCounter[j][i]; break; } } instance.doCalculateMatrixElem(i, j, topology, traffic); break; } } } } } } } }