Java Code Examples for com.google.common.primitives.Floats#toArray()

The following examples show how to use com.google.common.primitives.Floats#toArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaClassProcessor.java    From ArchUnit with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"}) // NOTE: We assume the component type matches the list
private Object toArray(Class<?> componentType, List<Object> values) {
    if (componentType == boolean.class) {
        return Booleans.toArray((Collection) values);
    } else if (componentType == byte.class) {
        return Bytes.toArray((Collection) values);
    } else if (componentType == short.class) {
        return Shorts.toArray((Collection) values);
    } else if (componentType == int.class) {
        return Ints.toArray((Collection) values);
    } else if (componentType == long.class) {
        return Longs.toArray((Collection) values);
    } else if (componentType == float.class) {
        return Floats.toArray((Collection) values);
    } else if (componentType == double.class) {
        return Doubles.toArray((Collection) values);
    } else if (componentType == char.class) {
        return Chars.toArray((Collection) values);
    }
    return values.toArray((Object[]) Array.newInstance(componentType, values.size()));
}
 
Example 2
Source File: CpuViewMessageHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Turn the monitoring data history into a
 * data structure that can feed the CPU UI memory.
 *
 * @param deviceId the device ID being monitored
 * @param length the length of the array
 * @param cpuStats the CPU load per core
 * @return a map of CPU metrics to their arrays of values
 */
private Map<Integer, Float[]> populateCpuDataHistory(
        DeviceId deviceId, int length, List<CpuStatistics> cpuStats) {
    Map<Integer, Float[]> data = initializeMapDataHistory(MAX_COLUMNS_NB);

    for (CpuStatistics stats : cpuStats) {
        int index = stats.id();

        // Store it locally
        addToCache(deviceId, length, index, stats.load());

        LruCache<Float> loadCache = getDataHistory(deviceId, index);
        if (loadCache == null) {
            continue;
        }
        float[] floatArray = Floats.toArray(Arrays.asList(loadCache.values().toArray(new Float[0])));

        // Project the load array to the range of [0, 100]
        for (int j = 0; j < floatArray.length; j++) {
            floatArray[j] = floatArray[j] * (float) 100;
        }

        // Fill the missing points
        float[] filledLoadArray = fillData(floatArray, NUM_OF_DATA_POINTS);

        // Set the data
        data.put(index, ArrayUtils.toObject(filledLoadArray));
    }

    // Keep a timestamp
    timestamp = System.currentTimeMillis();

    return data;
}
 
Example 3
Source File: SupportVectorMachineModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Object toArray(SupportVectorMachineModel supportVectorMachineModel, List<? extends Number> values){
	MathContext mathContext = supportVectorMachineModel.getMathContext();

	switch(mathContext){
		case FLOAT:
			return Floats.toArray(values);
		case DOUBLE:
			return Doubles.toArray(values);
		default:
			throw new UnsupportedAttributeException(supportVectorMachineModel, mathContext);
	}
}
 
Example 4
Source File: CompilationUnitImpl.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
private Object toArrayOfType(final Iterable<?> iterable, final Class<?> componentType) {
  Collection<?> _xifexpression = null;
  if ((iterable instanceof Collection<?>)) {
    _xifexpression = ((Collection<?>)iterable);
  } else {
    _xifexpression = IterableExtensions.toList(iterable);
  }
  final Collection<?> collection = _xifexpression;
  Object _switchResult = null;
  boolean _matched = false;
  if (Objects.equal(componentType, int.class)) {
    _matched=true;
    _switchResult = Ints.toArray(((List<Integer>) collection));
  }
  if (!_matched) {
    if (Objects.equal(componentType, long.class)) {
      _matched=true;
      _switchResult = Longs.toArray(((List<Long>) collection));
    }
  }
  if (!_matched) {
    if (Objects.equal(componentType, char.class)) {
      _matched=true;
      _switchResult = Chars.toArray(((List<Character>) collection));
    }
  }
  if (!_matched) {
    if (Objects.equal(componentType, boolean.class)) {
      _matched=true;
      _switchResult = Booleans.toArray(((List<Boolean>) collection));
    }
  }
  if (!_matched) {
    if (Objects.equal(componentType, byte.class)) {
      _matched=true;
      _switchResult = Bytes.toArray(((List<Byte>) collection));
    }
  }
  if (!_matched) {
    if (Objects.equal(componentType, short.class)) {
      _matched=true;
      _switchResult = Shorts.toArray(((List<Short>) collection));
    }
  }
  if (!_matched) {
    if (Objects.equal(componentType, float.class)) {
      _matched=true;
      _switchResult = Floats.toArray(((List<Float>) collection));
    }
  }
  if (!_matched) {
    if (Objects.equal(componentType, double.class)) {
      _matched=true;
      _switchResult = Doubles.toArray(((List<Double>) collection));
    }
  }
  if (!_matched) {
    _switchResult = Iterables.<Object>toArray(collection, ((Class<Object>) componentType));
  }
  return _switchResult;
}
 
Example 5
Source File: OnnxGraphMapper.java    From nd4j with Apache License 2.0 4 votes vote down vote up
/**
 * Init a function's attributes
 * @param mappedTfName the onnx name to pick (sometimes ops have multiple names
 * @param on the function to map
 * @param attributesForNode the attributes for the node
 * @param node
 * @param graph
 */
public void initFunctionFromProperties(String mappedTfName, DifferentialFunction on, Map<String, OnnxProto3.AttributeProto> attributesForNode, OnnxProto3.NodeProto node, OnnxProto3.GraphProto graph) {
    val properties = on.mappingsForFunction();
    val tfProperties = properties.get(mappedTfName);
    val fields = DifferentialFunctionClassHolder.getInstance().getFieldsForFunction(on);
    val attributeAdapters = on.attributeAdaptersForFunction();
    for(val entry : tfProperties.entrySet()) {
        val tfAttrName = entry.getValue().getTfAttrName();
        val currentField = fields.get(entry.getKey());

        AttributeAdapter adapter = null;
        if(tfAttrName != null) {
            if(currentField == null) {
                continue;
            }
            if(attributeAdapters != null && !attributeAdapters.isEmpty()) {
                val mappers = attributeAdapters.get(on.tensorflowName());
                val adapterFor = mappers.get(entry.getKey());
                adapter = adapterFor;
            }


            if(attributesForNode.containsKey(tfAttrName)) {
                val attr = attributesForNode.get(tfAttrName);
                switch (attr.getType()) {
                    case STRING:
                        val setString = attr.getS().toStringUtf8();
                        if(adapter != null) {
                            adapter.mapAttributeFor(setString,currentField,on);
                        }
                        else
                            on.setValueFor(currentField,setString);
                        break;
                    case INT:
                        val setInt = (int) attr.getI();
                        if(adapter != null) {
                            adapter.mapAttributeFor(setInt,currentField,on);
                        }
                        else
                            on.setValueFor(currentField,setInt);
                        break;
                    case INTS:
                        val setList = attr.getIntsList();
                        if(!setList.isEmpty()) {
                            val intList = Ints.toArray(setList);
                            if(adapter != null) {
                                adapter.mapAttributeFor(intList,currentField,on);
                            }
                            else
                                on.setValueFor(currentField,intList);
                        }
                        break;
                    case FLOATS:
                        val floatsList = attr.getFloatsList();
                        if(!floatsList.isEmpty()) {
                            val floats = Floats.toArray(floatsList);
                            if(adapter != null) {
                                adapter.mapAttributeFor(floats,currentField,on);
                            }

                            else
                                on.setValueFor(currentField,floats);
                            break;
                        }
                        break;
                    case TENSOR:
                        val tensorToGet = mapTensorProto(attr.getT());
                        if(adapter != null) {
                            adapter.mapAttributeFor(tensorToGet,currentField,on);
                        }
                        else
                            on.setValueFor(currentField,tensorToGet);
                        break;

                }
            }
        }


    }
}
 
Example 6
Source File: ThroughputViewMessageHandler.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Turn the monitoring data history into a
 * data structure that can feed the Throughput UI memory.
 *
 * @param deviceId the device ID being monitored
 * @param length the length of the array
 * @param monStats a MonitoringStatistics object
 * @return a map of throughput metrics to their arrays of values
 */
private Map<Integer, Float[]> populateThroughputDataHistory(
        DeviceId deviceId, int length, MonitoringStatistics monStats) {
    Map<Integer, Float[]> data = initializeMapDataHistory(MAX_COLUMNS_NB);

    for (CpuStatistics stats : monStats.cpuStatisticsAll()) {
        int index = stats.id();

        Float value = null;
        if ((stats.averageThroughput().isPresent()) && (stats.load() > MIN_CPU_LOAD)) {
            value = stats.averageThroughput().get();
        } else {
            value = new Float(0);
        }

        // Unit conversion
        ThroughputUnit throughputUnit = null;
        if (stats.throughputUnit().isPresent()) {
            throughputUnit = (ThroughputUnit) stats.throughputUnit().get();
        } else {
            throughputUnit = ThroughputUnit.BPS;
        }
        value = ThroughputUnit.toGbps(value, throughputUnit);

        // Store it locally
        addToCache(deviceId, length, index, value);

        LruCache<Float> loadCache = getDataHistory(deviceId, index);
        if (loadCache == null) {
            continue;
        }
        float[] floatArray = Floats.toArray(Arrays.asList(loadCache.values().toArray(new Float[0])));

        // Fill the missing points
        float[] filledLoadArray = fillData(floatArray, NUM_OF_DATA_POINTS);

        // Set the data
        data.put(index, ArrayUtils.toObject(filledLoadArray));
    }

    // Keep a timestamp
    timestamp = System.currentTimeMillis();

    return data;
}
 
Example 7
Source File: MemoryViewMessageHandler.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Turn the monitoring data history into a
 * data structure that can feed the Memory UI memory.
 *
 * @param deviceId the device ID being monitored
 * @param length the length of the array
 * @param monStats a MonitoringStatistics object
 * @return a map of memory metrics to their arrays of values
 */
private Map<Integer, Float[]> populateMemoryDataHistory(
        DeviceId deviceId, int length, MonitoringStatistics monStats) {
    Map<Integer, Float[]> data = initializeMapDataHistory(MEM_COLUMNS_NB);

    MemoryStatistics memStats = monStats.memoryStatistics();

    CapacityUnit capacityUnit = (CapacityUnit) memStats.unit();
    Float used = new Float(memStats.used());
    Float free = new Float(memStats.free());
    Float total = new Float(memStats.total());

    // Unit conversions
    used = CapacityUnit.toGigaBytes(used, capacityUnit);
    free = CapacityUnit.toGigaBytes(free, capacityUnit);
    total = CapacityUnit.toGigaBytes(total, capacityUnit);

    // Store them locally
    addToCache(deviceId, length, 0, used);
    addToCache(deviceId, length, 1, free);
    addToCache(deviceId, length, 2, total);

    for (int i = 0; i < length; i++) {
        LruCache<Float> loadCache = getDataHistory(deviceId, i);
        if (loadCache == null) {
            continue;
        }
        float[] floatArray = Floats.toArray(Arrays.asList(loadCache.values().toArray(new Float[0])));

        // Fill the missing points
        float[] filledLoadArray = fillData(floatArray, NUM_OF_DATA_POINTS);

        // Set the data
        data.put(i, ArrayUtils.toObject(filledLoadArray));
    }

    // Keep a timestamp
    timestamp = System.currentTimeMillis();

    return data;
}
 
Example 8
Source File: LatencyViewMessageHandler.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Turn the monitoring data history into a
 * data structure that can feed the Latency UI memory.
 *
 * @param deviceId the device ID being monitored
 * @param length the length of the array
 * @param monStats a MonitoringStatistics object
 * @return a map of latency metrics to their arrays of values
 */
private Map<Integer, Float[]> populateLatencyDataHistory(
        DeviceId deviceId, int length, MonitoringStatistics monStats) {
    Map<Integer, Float[]> data = initializeMapDataHistory(MAX_COLUMNS_NB);

    for (CpuStatistics stats : monStats.cpuStatisticsAll()) {
        int index = stats.id();

        // TODO: Use min and max latency to plot bars plots with error bars
        Float value = null;
        if ((stats.averageLatency().isPresent()) && (stats.load() > MIN_CPU_LOAD)) {
            value = stats.averageLatency().get();
        } else {
            value = new Float(0);
        }

        // Unit conversion
        LatencyUnit latencyUnit = null;
        if (stats.latencyUnit().isPresent()) {
            latencyUnit = (LatencyUnit) stats.latencyUnit().get();
        } else {
            latencyUnit = LatencyUnit.NANO_SECOND;
        }
        value = LatencyUnit.toNano(value, latencyUnit);

        // Store it locally
        addToCache(deviceId, length, index, value);

        LruCache<Float> loadCache = getDataHistory(deviceId, index);
        if (loadCache == null) {
            continue;
        }
        float[] floatArray = Floats.toArray(Arrays.asList(loadCache.values().toArray(new Float[0])));

        // Fill the missing points
        float[] filledLoadArray = fillData(floatArray, NUM_OF_DATA_POINTS);

        // Set the data
        data.put(index, ArrayUtils.toObject(filledLoadArray));
    }

    // Keep a timestamp
    timestamp = System.currentTimeMillis();

    return data;
}