List of usage examples for java.util Map size
int size();
From source file:com.datumbox.framework.core.common.dataobjects.DataframeMatrix.java
/** * Parses a testing dataset and converts it to DataframeMatrix by using an already existing mapping between feature names and column ids. Typically used * to parse the testing or validation dataset. * //from ww w . j av a 2 s . co m * @param newData * @param recordIdsReference * @param featureIdsReference * @return */ public static DataframeMatrix parseDataset(Dataframe newData, Map<Integer, Integer> recordIdsReference, Map<Object, Integer> featureIdsReference) { if (featureIdsReference.isEmpty()) { throw new IllegalArgumentException("The featureIdsReference map should not be empty."); } setStorageEngine(newData); int n = newData.size(); int d = featureIdsReference.size(); DataframeMatrix m = new DataframeMatrix(new MapRealMatrix(n, d), new MapRealVector(n)); if (newData.isEmpty()) { return m; } boolean extractY = (newData.getYDataType() == TypeInference.DataType.NUMERICAL); boolean addConstantColumn = featureIdsReference.containsKey(Dataframe.COLUMN_NAME_CONSTANT); int rowId = 0; for (Map.Entry<Integer, Record> e : newData.entries()) { Integer rId = e.getKey(); Record r = e.getValue(); if (recordIdsReference != null) { recordIdsReference.put(rId, rowId); } if (extractY) { m.Y.setEntry(rowId, TypeInference.toDouble(r.getY())); } if (addConstantColumn) { m.X.setEntry(rowId, 0, 1.0); //add the constant column } for (Map.Entry<Object, Object> entry : r.getX().entrySet()) { Object feature = entry.getKey(); Double value = TypeInference.toDouble(entry.getValue()); if (value != null) { Integer featureId = featureIdsReference.get(feature); if (featureId != null) {//if the feature exists m.X.setEntry(rowId, featureId, value); } } //else the X matrix maintains the 0.0 default value } ++rowId; } return m; }
From source file:com.streamsets.pipeline.lib.jdbc.multithread.util.OffsetQueryUtil.java
/** * Validates whether offset names match in the stored offset with respect to table configuration * @param tableContext {@link TableContext} for table * @param offset Stored offset from the previous stage run * @return the actual offset map as deserialized from the offset param, if valid * @throws StageException if columns mismatch *//*from w w w . j a v a 2 s . c om*/ public static Map<String, String> validateStoredAndSpecifiedOffset(TableContext tableContext, String offset) throws StageException { Set<String> expectedColumns = Sets.newHashSet(tableContext.getOffsetColumns()); final Map<String, String> actualOffsets = getColumnsToOffsetMapFromOffsetFormat(offset); // only perform the actual validation below if there ARE stored offsets if (actualOffsets.size() == 0) { return actualOffsets; } Set<String> actualColumns = actualOffsets.keySet(); Set<String> expectedSetDifference = Sets.difference(expectedColumns, actualColumns); Set<String> actualSetDifference = Sets.difference(actualColumns, expectedColumns); if (expectedSetDifference.size() > 0 || actualSetDifference.size() > 0) { throw new StageException(JdbcErrors.JDBC_71, tableContext.getQualifiedName(), COMMA_SPACE_JOINER.join(actualColumns), COMMA_SPACE_JOINER.join(expectedColumns)); } return actualOffsets; }
From source file:playground.johannes.socialnets.GraphStatistics.java
static public Histogram1D createClusteringCoefficientsHistogram(Graph g, double min, double max, int ignoreWave) { Map<Vertex, Double> coeffs = edu.uci.ics.jung.statistics.GraphStatistics.clusteringCoefficients(g); DoubleArrayList values = new DoubleArrayList(coeffs.size()); for (Vertex v : coeffs.keySet()) { if (((Vertex) v).degree() == 1) coeffs.put(v, 0.0);// www. j av a2 s . c om if (((Vertex) v).getUserDatum(UserDataKeys.ANONYMOUS_KEY) == null) values.add(coeffs.get(v)); } if (min < 0 && max < 0) { values.sort(); min = values.get(0); max = values.get(values.size() - 1); } return createHistogram(values, min, max, "Clutering coefficients"); }
From source file:com.cloudera.oryx.rdf.common.pmml.DecisionForestPMML.java
private static TreeNode translateFromPMML(Node root, Map<Integer, BiMap<String, Integer>> columnToCategoryNameToIDMapping, InboundSettings settings) { List<String> columnNames = settings.getColumnNames(); int targetColumn = settings.getTargetColumn(); List<Node> children = root.getNodes(); if (children.isEmpty()) { // Terminal Collection<ScoreDistribution> scoreDistributions = root.getScoreDistributions(); Prediction prediction;//from w w w. j a v a2 s . c o m if (scoreDistributions != null && !scoreDistributions.isEmpty()) { // Categorical target Map<String, Integer> valueToID = columnToCategoryNameToIDMapping.get(targetColumn); int[] categoryCounts = new int[valueToID.size()]; for (ScoreDistribution dist : scoreDistributions) { int valueID = valueToID.get(dist.getValue()); categoryCounts[valueID] = (int) Math.round(dist.getRecordCount()); } prediction = new CategoricalPrediction(categoryCounts); } else { prediction = new NumericPrediction(Float.parseFloat(root.getScore()), (int) Math.round(root.getRecordCount())); } return new TerminalNode(prediction); } Preconditions.checkArgument(children.size() == 2); // Decision Node child1 = children.get(0); Node child2 = children.get(1); Node negativeLeftChild; Node positiveRightChild; if (child1.getPredicate().getClass().equals(True.class)) { negativeLeftChild = child1; positiveRightChild = child2; } else { Preconditions.checkArgument(child2.getPredicate().getClass().equals(True.class)); negativeLeftChild = child2; positiveRightChild = child1; } Decision decision; Predicate predicate = positiveRightChild.getPredicate(); boolean defaultDecision = positiveRightChild.getId().equals(root.getDefaultChild()); if (predicate instanceof SimplePredicate) { // Numeric decision SimplePredicate simplePredicate = (SimplePredicate) predicate; Preconditions.checkArgument(simplePredicate.getOperator() == SimplePredicate.Operator.GREATER_OR_EQUAL); float threshold = Float.parseFloat(simplePredicate.getValue()); int featureNumber = columnNames.indexOf(simplePredicate.getField().getValue()); decision = new NumericDecision(featureNumber, threshold, defaultDecision); } else { // Cateogrical decision Preconditions.checkArgument(predicate instanceof SimpleSetPredicate); SimpleSetPredicate simpleSetPredicate = (SimpleSetPredicate) predicate; Preconditions.checkArgument( simpleSetPredicate.getBooleanOperator() == SimpleSetPredicate.BooleanOperator.IS_IN); int featureNumber = columnNames.indexOf(simpleSetPredicate.getField().getValue()); Map<String, Integer> categoryNameToID = columnToCategoryNameToIDMapping.get(featureNumber); String[] categories = DelimitedDataUtils.decode(simpleSetPredicate.getArray().getValue(), ' '); BitSet activeCategories = new BitSet(categoryNameToID.size()); for (String category : categories) { int categoryID = categoryNameToID.get(category); activeCategories.set(categoryID); } decision = new CategoricalDecision(featureNumber, activeCategories, defaultDecision); } return new DecisionNode(decision, translateFromPMML(negativeLeftChild, columnToCategoryNameToIDMapping, settings), translateFromPMML(positiveRightChild, columnToCategoryNameToIDMapping, settings)); }
From source file:ezbake.data.graph.rexster.RexsterTestUtils.java
/** * Takes a property name and a map of base64 encoded visibility: value and converts it to a groovy formatted string, * suitable for gremlin queries.//w w w . j a va 2 s. c o m * * @param propName name of property * @param values a map of serialized visibility : string value * @return a groovy formatted string that represents a property key and value that can be used in request scripts. */ public static String makeEzPropertyString(String propName, Map<String, String> values) { final StringBuilder prop = new StringBuilder(); prop.append(propName).append(":["); int count = 1; for (final Map.Entry entry : values.entrySet()) { prop.append("[visibility:\"").append(entry.getKey()).append("\",").append("value:\"") .append(entry.getValue()).append("\"]"); if (count < values.size()) { prop.append(','); } count++; } prop.append(']'); return prop.toString(); }
From source file:com.spotify.heroic.metric.TagValues.java
public static List<TagValues> fromEntries(final Iterator<Map.Entry<String, String>> entries) { final Map<String, SortedSet<String>> key = new HashMap<>(); while (entries.hasNext()) { final Map.Entry<String, String> e = entries.next(); SortedSet<String> values = key.get(e.getKey()); if (values == null) { values = new TreeSet<String>(COMPARATOR); key.put(e.getKey(), values); }/*from w w w. j a v a 2 s. c om*/ values.add(e.getValue()); } final List<TagValues> group = new ArrayList<>(key.size()); for (final Map.Entry<String, SortedSet<String>> e : key.entrySet()) { group.add(new TagValues(e.getKey(), new ArrayList<>(e.getValue()))); } return group; }
From source file:com.opengamma.component.tool.ToolContextUtils.java
private static ComponentInfo getComponentInfo(ComponentServer componentServer, List<String> preferenceList, Class<?> type) {/* w ww. j av a 2 s . co m*/ Map<String, ComponentInfo> infos = componentServer.getComponentInfoMap(type); if (preferenceList != null) { for (String preference : preferenceList) { ComponentInfo componentInfo = infos.get(preference); if (componentInfo != null) { return componentInfo; } } } infos.remove("test"); if (infos.size() == 0) { return null; } if (infos.size() > 1) { s_logger.warn("Multiple remote components match: " + type.getSimpleName() + "::" + infos.keySet()); return null; } return infos.values().iterator().next(); }
From source file:eu.squadd.reflections.mapper.ServiceModelTranslator.java
private static void assignMixedTypesValue(Class sourceType, Method getter, Object sourceValue, Class destType, Method setter, Object destInstance) { System.out.println("Source collection doesn't match the one on destination side, resolve their types..."); if (sourceValue == null || destInstance == null) return;//from w w w .j a va 2 s . c o m if (Collection.class.isAssignableFrom(sourceType)) { if (((Collection) sourceValue).isEmpty()) return; Class sourceArgClass = detectSourceCollectionPayload(getter); if (sourceArgClass == null) { System.err.println("Failed to determine source Collection payload type, operation aborted !!!"); return; } // if source is a collection then destination must be a Map Map<Integer, Class> destMapTypes = detectDestMapPayload(setter); if (destMapTypes.isEmpty() || destMapTypes.size() != 2) { System.err.println("Failed to determine destination Map payload types, operation aborted !!!"); return; } Class firstDestArgClass = destMapTypes.get(0); Class secordDestArgsClass = destMapTypes.get(1); System.out.println("*** Both Collection types sorted, populating values..."); Map destItems = createMapOfTypes(firstDestArgClass, secordDestArgsClass); //for (Object key : sourceItems.entrySet()) { Collection sourceItems = (Collection) sourceValue; Iterator it = sourceItems.iterator(); Integer i = 0; while (it.hasNext()) { Object element = transposeModel(sourceArgClass, secordDestArgsClass, it.next()); destItems.put(++i, element); } try { setter.invoke(destInstance, destItems); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { Logger.getLogger(ServiceModelTranslator.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("*** done"); } else if (Map.class.isAssignableFrom(sourceType)) { if (((Map) sourceValue).isEmpty()) return; Map<Integer, Class> sourceMapTypes = detectSourceMapPayload(getter); if (sourceMapTypes.isEmpty() || sourceMapTypes.size() != 2) { System.err.println("Failed to determine source Map payload types, operation aborted !!!"); return; } //Class firstSourceArgClass = sourceMapTypes.get(0); // dummy, not used anywere Class secondSourceArgClass = sourceMapTypes.get(1); Map sourceItems = (Map) sourceValue; // if source is a Map then destination must be a Collection Class destArgClass = detectDestCollectionPayload(setter); if (destArgClass == null) { System.err .println("Failed to determine destination Collection payload type, operation aborted !!!"); return; } Collection destItems; switch (destType.getName()) { case "java.util.List": destItems = createListOfType(destArgClass); break; case "java.util.Set": destItems = createSetOfType(destArgClass); break; default: System.out.println("4: Unrecognized collection, can't populate values"); return; } for (Object value : sourceItems.values()) { Object destValue = transposeModel(secondSourceArgClass, destArgClass, value); destItems.add(destValue); } try { setter.invoke(destInstance, destItems); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { Logger.getLogger(ServiceModelTranslator.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("*** done"); } else { System.out.println("4: Unrecognized collection type, cannot proceed, type: " + sourceType.getName()); } }
From source file:Main.java
/** * Creates a tag from the given string values. Can be used for creating html or xml tags. * /*from w w w .j av a2s . c o m*/ * @param tagname * the tagname * @param value * the value from the tag. * @param attributtes * a map with the attributtes * @return the string */ public static String newTag(final String tagname, final String value, final Map<String, String> attributtes) { final StringBuilder xmlTag = new StringBuilder(); xmlTag.append("<").append(tagname); if (attributtes != null && !attributtes.isEmpty()) { xmlTag.append(" "); int count = 1; for (final Map.Entry<String, String> attributte : attributtes.entrySet()) { xmlTag.append(attributte.getKey()); xmlTag.append("="); xmlTag.append("\"").append(attributte.getValue()).append("\""); if (count != attributtes.size()) { xmlTag.append(" "); } count++; } } xmlTag.append(">"); xmlTag.append(value); xmlTag.append("</").append(tagname).append(">"); return xmlTag.toString(); }