-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Statistical analysis working for generator runs
- Loading branch information
1 parent
e632477
commit 038142f
Showing
14 changed files
with
258 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 0 additions & 29 deletions
29
...olarthing/program/subprogram/analyze/analyzers/generator/entry/GeneratorFXStatistics.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 23 additions & 6 deletions
29
.../solarthing/program/subprogram/analyze/analyzers/generator/entry/GeneratorStatistics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,33 @@ | ||
package me.retrodaredevil.solarthing.program.subprogram.analyze.analyzers.generator.entry; | ||
|
||
import me.retrodaredevil.solarthing.program.subprogram.analyze.statistics.fx.FXStatisticCollection; | ||
import me.retrodaredevil.solarthing.solar.outback.OutbackIdentifier; | ||
|
||
import java.time.Instant; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
/** | ||
* Represents statistics of some time period of a generator run, not necessarily the entire generator run | ||
* | ||
* @deprecated Not yet implemented | ||
*/ | ||
@Deprecated | ||
public record GeneratorStatistics( | ||
Instant startTime, | ||
Instant endTime, | ||
float averageBatteryVoltage, | ||
float average | ||
) {} | ||
// float averageBatteryVoltage, | ||
// float batteryVoltageStandardDeviation, | ||
Map<Integer, FXStatisticCollection> fxStatistics | ||
) { | ||
public Collection<Integer> getFXAddresses() { | ||
return fxStatistics.keySet(); | ||
} | ||
public FXStatisticCollection getStatistics(int fxAddress) { | ||
FXStatisticCollection r = fxStatistics.get(fxAddress); | ||
if (r == null) { | ||
throw new IllegalArgumentException("Unknown FX address: " + fxAddress); | ||
} | ||
return r; | ||
} | ||
public FXStatisticCollection getStatistics(OutbackIdentifier outbackIdentifier) { | ||
return getStatistics(outbackIdentifier.getAddress()); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
.../java/me/retrodaredevil/solarthing/program/subprogram/analyze/analyzers/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
/** | ||
* Contains implementations of {@link me.retrodaredevil.solarthing.program.subprogram.analyze.Analyzer} | ||
* and contains data classes (usually records) to represent each Analyzer's entry/result. | ||
*/ | ||
package me.retrodaredevil.solarthing.program.subprogram.analyze.analyzers; |
7 changes: 7 additions & 0 deletions
7
...a/me/retrodaredevil/solarthing/program/subprogram/analyze/statistics/BasicStatistics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package me.retrodaredevil.solarthing.program.subprogram.analyze.statistics; | ||
|
||
public record BasicStatistics( | ||
float average, | ||
float maximum | ||
) { | ||
} |
22 changes: 22 additions & 0 deletions
22
...me/retrodaredevil/solarthing/program/subprogram/analyze/statistics/CommonPercentiles.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package me.retrodaredevil.solarthing.program.subprogram.analyze.statistics; | ||
|
||
import com.google.common.math.Quantiles; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public record CommonPercentiles( | ||
double p0_1, double p1, double p10, | ||
double p90, double p99, double p99_9 | ||
) { | ||
private static final Quantiles.ScaleAndIndexes PERCENTILE_1000 = Quantiles.scale(1000).indexes(1, 10, 100, 900, 990, 999); | ||
|
||
public static CommonPercentiles fromDataset(List<? extends Number> dataset) { | ||
Map<Integer, Double> result = PERCENTILE_1000.compute(dataset); | ||
return new CommonPercentiles( | ||
result.get(1), result.get(10), result.get(100), | ||
result.get(900), result.get(990), result.get(999) | ||
); | ||
} | ||
|
||
} |
94 changes: 94 additions & 0 deletions
94
...va/me/retrodaredevil/solarthing/program/subprogram/analyze/statistics/fx/FXStatistic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package me.retrodaredevil.solarthing.program.subprogram.analyze.statistics.fx; | ||
|
||
import java.util.function.BiConsumer; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Represents a collection of <em>some statistic</em> over some period of time for a single FX. | ||
* {@link me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket} | ||
* | ||
* Depending on what was happening during the time period this represents (AC Use, AC Drop, Silent, Bulk, the entire generator run time) | ||
* or the type of statistic this is, some properties may not be useful. | ||
* Usually many of these properties become more useful for the shorter periods of time that are focused on. | ||
* | ||
*/ | ||
public record FXStatistic<T>( | ||
T batteryVoltage, | ||
|
||
T inverterCurrentRaw, | ||
T inverterWattage, | ||
|
||
T chargerCurrentRaw, | ||
T chargerWattage, | ||
|
||
T buyCurrentRaw, | ||
T buyWattage, | ||
|
||
T sellCurrentRaw, | ||
T sellWattage, | ||
|
||
T inputVoltageRaw, | ||
T outputVoltageRaw | ||
) { | ||
|
||
public static <T> FXStatistic<T> initUsing(Supplier<T> initializer) { | ||
return new FXStatistic<>( | ||
initializer.get(), | ||
|
||
initializer.get(), | ||
initializer.get(), | ||
|
||
initializer.get(), | ||
initializer.get(), | ||
|
||
initializer.get(), | ||
initializer.get(), | ||
|
||
initializer.get(), | ||
initializer.get(), | ||
|
||
initializer.get(), | ||
initializer.get() | ||
); | ||
} | ||
|
||
public <U> FXStatistic<U> convert(Function<T, U> converter) { | ||
return new FXStatistic<>( | ||
converter.apply(batteryVoltage), | ||
|
||
converter.apply(inverterCurrentRaw), | ||
converter.apply(inverterWattage), | ||
|
||
converter.apply(chargerCurrentRaw), | ||
converter.apply(chargerWattage), | ||
|
||
converter.apply(buyCurrentRaw), | ||
converter.apply(buyWattage), | ||
|
||
converter.apply(sellCurrentRaw), | ||
converter.apply(sellWattage), | ||
|
||
converter.apply(inputVoltageRaw), | ||
converter.apply(outputVoltageRaw) | ||
); | ||
} | ||
public <U> void apply(FXStatistic<U> other, BiConsumer<T, U> function) { | ||
function.accept(batteryVoltage, other.batteryVoltage); | ||
|
||
function.accept(inverterCurrentRaw, other.inverterCurrentRaw); | ||
function.accept(inverterWattage, other.inverterWattage); | ||
|
||
function.accept(chargerCurrentRaw, other.chargerCurrentRaw); | ||
function.accept(chargerWattage, other.chargerWattage); | ||
|
||
function.accept(buyCurrentRaw, other.buyCurrentRaw); | ||
function.accept(buyWattage, other.buyWattage); | ||
|
||
function.accept(sellCurrentRaw, other.sellCurrentRaw); | ||
function.accept(sellWattage, other.sellWattage); | ||
|
||
function.accept(inputVoltageRaw, other.inputVoltageRaw); | ||
function.accept(outputVoltageRaw, other.outputVoltageRaw); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...trodaredevil/solarthing/program/subprogram/analyze/statistics/fx/FXStatisticAnalysis.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package me.retrodaredevil.solarthing.program.subprogram.analyze.statistics.fx; | ||
|
||
import com.google.common.math.Stats; | ||
import me.retrodaredevil.solarthing.packets.Packet; | ||
import me.retrodaredevil.solarthing.packets.collection.InstancePacketGroup; | ||
import me.retrodaredevil.solarthing.program.subprogram.analyze.statistics.CommonPercentiles; | ||
import me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public final class FXStatisticAnalysis { | ||
private FXStatisticAnalysis() { throw new UnsupportedOperationException(); } | ||
|
||
/** | ||
* | ||
* @param packets A list of packet groups, where each packet group must have at least one FX packet. | ||
* @return A map of Outback address to {@link FXStatisticCollection} for that given FX | ||
*/ | ||
public static Map<Integer, FXStatisticCollection> analyze(List<InstancePacketGroup> packets) { | ||
Map<Integer, FXStatistic<List<Number>>> accumulators = new HashMap<>(); | ||
for (InstancePacketGroup packetGroup : packets) { | ||
for (Packet packet : packetGroup.getPackets()) { | ||
if (packet instanceof FXStatusPacket fx) { | ||
int address = fx.getAddress(); | ||
FXStatistic<List<Number>> accumulator = accumulators.computeIfAbsent(address, _address -> FXStatistic.initUsing(ArrayList::new)); | ||
accumulator.apply(packetToStatistic(fx), List::add); | ||
} | ||
} | ||
} | ||
Map<Integer, FXStatisticCollection> statsMap = new HashMap<>(); | ||
for (Map.Entry<Integer, FXStatistic<List<Number>>> entry : accumulators.entrySet()) { | ||
FXStatistic<Stats> stats = entry.getValue().convert(Stats::of); | ||
FXStatistic<CommonPercentiles> percentiles = entry.getValue().convert(CommonPercentiles::fromDataset); | ||
|
||
statsMap.put(entry.getKey(), new FXStatisticCollection(stats, percentiles)); | ||
} | ||
return statsMap; | ||
} | ||
|
||
private static FXStatistic<Number> packetToStatistic(FXStatusPacket packet) { | ||
return new FXStatistic<>( | ||
packet.getBatteryVoltage(), | ||
|
||
packet.getInverterCurrentRaw(), | ||
packet.getInverterWattage(), | ||
|
||
packet.getChargerCurrentRaw(), | ||
packet.getChargerWattage(), | ||
|
||
packet.getBuyCurrentRaw(), | ||
packet.getBuyWattage(), | ||
|
||
packet.getSellCurrentRaw(), | ||
packet.getSellWattage(), | ||
|
||
packet.getInputVoltageRaw(), | ||
packet.getOutputVoltageRaw() | ||
); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...odaredevil/solarthing/program/subprogram/analyze/statistics/fx/FXStatisticCollection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package me.retrodaredevil.solarthing.program.subprogram.analyze.statistics.fx; | ||
|
||
import com.google.common.math.Stats; | ||
import me.retrodaredevil.solarthing.program.subprogram.analyze.statistics.CommonPercentiles; | ||
|
||
public record FXStatisticCollection( | ||
FXStatistic<Stats> stats, | ||
FXStatistic<CommonPercentiles> percentiles | ||
) { | ||
} |
4 changes: 4 additions & 0 deletions
4
...java/me/retrodaredevil/solarthing/program/subprogram/analyze/statistics/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Contains utilities to analyze a set of data and immutable data carriers representing the results of statistical analysis. | ||
*/ | ||
package me.retrodaredevil.solarthing.program.subprogram.analyze.statistics; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters