下面列出了怎么用com.google.common.collect.ArrayTable的API类实例代码及写法,或者点击链接到github查看源代码。
private MLDouble histoDataAsMLDouble(String name, ArrayTable<Integer, String, Float> histoData) {
int rowsSize = histoData.rowKeySet().size();
int colsSize = histoData.columnKeySet().size();
MLDouble mlDouble = new MLDouble(name, new int[] {rowsSize, colsSize});
int i = 0;
for (Integer rowKey : histoData.rowKeyList()) {
int j = 0;
for (String colkey : histoData.columnKeyList()) {
Float v = histoData.get(rowKey, colkey);
mlDouble.set(new Double(v), i, j);
j++;
}
i++;
}
return mlDouble;
}
public static double[][] histoDataAsDoubleMatrixNew(ArrayTable<Integer, String, Float> hdTable) {
int rowsSize = hdTable.rowKeySet().size();
int colsSize = hdTable.columnKeySet().size();
double[][] matFinal = new double[rowsSize][colsSize];
int i = 0;
for (Integer rowKey : hdTable.rowKeyList()) {
int j = 0;
for (String colkey : hdTable.columnKeyList()) {
Float v = hdTable.get(rowKey, colkey);
matFinal[i][j] = getCleanFloat(v);
j = j + 1;
}
i = i + 1;
}
return matFinal;
}
@Test
public void givenArrayTable_whenGet_returnsSuccessfully() {
final List<String> universityRowTable = Lists.newArrayList("Mumbai", "Harvard");
final List<String> courseColumnTables = Lists.newArrayList("Chemical", "IT", "Electrical");
final Table<String, String, Integer> universityCourseSeatTable = ArrayTable.create(universityRowTable, courseColumnTables);
universityCourseSeatTable.put("Mumbai", "Chemical", 120);
universityCourseSeatTable.put("Mumbai", "IT", 60);
universityCourseSeatTable.put("Harvard", "Electrical", 60);
universityCourseSeatTable.put("Harvard", "IT", 120);
final int seatCount = universityCourseSeatTable.get("Mumbai", "IT");
assertThat(seatCount).isEqualTo(60);
}
public ForecastErrorsHistoricalData(List<String> generatorsIds, List<String> loadsIds,
List<StochasticVariable> stochasticVariables,
ArrayTable<Integer, String, Float> forecastsData,
ArrayTable<Integer, String, Float> snapshotsData) {
this.generatorsIds = generatorsIds;
this.loadsIds = loadsIds;
this.stochasticVariables = stochasticVariables;
this.forecastsData = forecastsData;
this.snapshotsData = snapshotsData;
}
public Wp41HistoData(List<String> historicalGensIds,
List<String> historicalLoadsIds,
List<String> historicalDanglingLinesIds,
ArrayTable<Integer, String, Float> hdTable) {
this.historicalGensIds = historicalGensIds;
this.historicalLoadsIds = historicalLoadsIds;
this.historicalDanglingLinesIds = historicalDanglingLinesIds;
this.hdTable = hdTable;
}
private Wp41HistoData parseData(DataMiningFacadeParams dmParams) throws IOException, InterruptedException {
int rowCount = histoClient.queryCount(dmParams.getInterval(), HistoDbHorizon.SN);
Set<HistoDbAttributeId> attributeIds = new LinkedHashSet<>((dmParams.getGensIds().size() +
dmParams.getLoadsIds().size() +
dmParams.getDanglingLinesIds().size()) * 2); // gens P, Q loads P, Q danglingLines P0, Q0
for (String genId : dmParams.getGensIds()) {
attributeIds.add(new HistoDbNetworkAttributeId(genId, HistoDbAttr.P));
attributeIds.add(new HistoDbNetworkAttributeId(genId, HistoDbAttr.Q));
}
for (String loadId : dmParams.getLoadsIds()) {
attributeIds.add(new HistoDbNetworkAttributeId(loadId, HistoDbAttr.P));
attributeIds.add(new HistoDbNetworkAttributeId(loadId, HistoDbAttr.Q));
}
for (String dlId : dmParams.getDanglingLinesIds()) {
attributeIds.add(new HistoDbNetworkAttributeId(dlId, HistoDbAttr.P0));
attributeIds.add(new HistoDbNetworkAttributeId(dlId, HistoDbAttr.Q0));
}
List<Integer> rowIndexes;
try (IntStream intStream = IntStream.range(0, rowCount)) {
rowIndexes = intStream.boxed().collect(Collectors.toList());
}
List<String> colIndexes = attributeIds.stream().map(Object::toString).collect(Collectors.toList());
ArrayTable<Integer, String, Float> hdTable = ArrayTable.create(rowIndexes, colIndexes);
// parse csv generators
try (InputStream is = histoClient.queryCsv(HistoQueryType.data, attributeIds, dmParams.getInterval(), HistoDbHorizon.SN, false, false)) {
parseCsv(is, attributeIds, hdTable, rowCount);
}
return new Wp41HistoData(dmParams.getGensIds(), dmParams.getLoadsIds(), dmParams.getDanglingLinesIds(), hdTable);
}
public static double[][] histoDataAsDoubleMatrix(ArrayTable<Integer, String, Float> hdTable) {
int rowsSize = hdTable.rowKeySet().size();
int colsSize = hdTable.columnKeySet().size();
double[][] matFinal = new double[rowsSize][colsSize];
for (int i = 0; i < rowsSize; i++) {
for (int j = 0; j < colsSize; j++) {
Float v = hdTable.get(i, j);
matFinal[i][j] = getCleanFloat(v);
}
}
return matFinal;
}
private Table<String, ResourceType, Response<Map<String, String>>> getAllFromRedis(
Set<String> userIds) {
if (userIds.size() == 0) {
return HashBasedTable.create();
}
try {
final Table<String, ResourceType, Response<Map<String, String>>> responseTable =
ArrayTable.create(
userIds,
new ArrayIterator<>(
resources.stream().map(Resource::getResourceType).toArray(ResourceType[]::new)));
for (List<String> userIdSubset : Lists.partition(new ArrayList<>(userIds), 10)) {
redisClientDelegate.withMultiKeyPipeline(
p -> {
for (String userId : userIdSubset) {
resources.stream()
.map(Resource::getResourceType)
.forEach(
r -> {
responseTable.put(userId, r, p.hgetAll(userKey(userId, r)));
});
}
p.sync();
});
}
return responseTable;
} catch (Exception e) {
log.error("Storage exception reading all entries.", e);
}
return null;
}
@Test
public void getColumnTypes() {
ArrayTable<Integer, Integer, Result<?>> table = ArrayTable.create(INDICES, INDICES);
table.put(0, 0, Result.success(1));
table.put(0, 1, Result.success("abc"));
table.put(1, 0, Result.success(2));
table.put(1, 1, Result.success("def"));
List<Class<?>> columnTypes = TradeReportFormatter.INSTANCE.getColumnTypes(report(table));
assertThat(columnTypes).isEqualTo(ImmutableList.of(Integer.class, String.class));
}
@Test
public void getColumnTypesWithSomeFailures() {
ImmutableList<Integer> indices = ImmutableList.of(0, 1);
ArrayTable<Integer, Integer, Result<?>> table = ArrayTable.create(indices, indices);
table.put(0, 0, Result.failure(FailureReason.ERROR, "fail"));
table.put(0, 1, Result.failure(FailureReason.ERROR, "fail"));
table.put(1, 0, Result.success(2));
table.put(1, 1, Result.success("def"));
List<Class<?>> columnTypes = TradeReportFormatter.INSTANCE.getColumnTypes(report(table));
assertThat(columnTypes).isEqualTo(ImmutableList.of(Integer.class, String.class));
}
@Test
public void getColumnTypesWithAllFailures() {
ImmutableList<Integer> indices = ImmutableList.of(0, 1);
ArrayTable<Integer, Integer, Result<?>> table = ArrayTable.create(indices, indices);
table.put(0, 0, Result.failure(FailureReason.ERROR, "fail"));
table.put(0, 1, Result.failure(FailureReason.ERROR, "fail"));
table.put(1, 0, Result.failure(FailureReason.ERROR, "fail"));
table.put(1, 1, Result.failure(FailureReason.ERROR, "fail"));
List<Class<?>> columnTypes = TradeReportFormatter.INSTANCE.getColumnTypes(report(table));
assertThat(columnTypes).isEqualTo(ImmutableList.of(Object.class, Object.class));
}
private TradeReport report(ArrayTable<Integer, Integer, Result<?>> table) {
return TradeReport.builder()
.columns(
TradeReportColumn.builder().header("col0").build(),
TradeReportColumn.builder().header("col1").build())
.data(table)
.valuationDate(LocalDate.now(ZoneOffset.UTC))
.runInstant(Instant.now())
.build();
}
static
InlineTable createInlineTable(List<List<String>> rows, List<String> columns){
List<Integer> rowKeys = new ArrayList<>();
for(int i = 0; i < rows.size(); i++){
rowKeys.add(i + 1);
}
Table<Integer, String, String> table = ArrayTable.create(rowKeys, columns);
for(int i = 0; i < rows.size(); i++){
List<String> row = rows.get(i);
for(int j = 0; j < columns.size(); j++){
String column = columns.get(j);
String value = row.get(j);
if(value == null){
continue;
}
table.put(rowKeys.get(i), column, value);
}
}
return InlineTableUtil.format(table);
}
public ArrayTable<Integer, String, Float> getForecastsData() {
return forecastsData;
}
public ArrayTable<Integer, String, Float> getSnapshotsData() {
return snapshotsData;
}
public ArrayTable<Integer, String, Float> getHdTable() {
return hdTable;
}