Skip to content
Snippets Groups Projects
Commit dbf7850c authored by Eilert Tunheim's avatar Eilert Tunheim
Browse files

Merge branch 'Eilerts_branch' into 'master'

Added confidence interval and reset the x-axis back to index instead of hours

See merge request mesji/bacheloroppgave_2022!40
parents 143d9a7d 7fd96b46
No related branches found
No related tags found
No related merge requests found
......@@ -52,5 +52,6 @@
<orderEntry type="library" name="Maven: com.google.cloud:google-cloud-storage:2.4.0" level="project" />
<orderEntry type="library" name="Maven: com.google.apis:google-api-services-storage:v1-rev20211201-1.32.1" level="project" />
<orderEntry type="library" name="Maven: com.google.auto.value:auto-value-annotations:1.9" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-math3:3.6.1" level="project" />
</component>
</module>
\ No newline at end of file
......@@ -29,6 +29,13 @@
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-math3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
</dependencies>
<properties>
......
......@@ -5,9 +5,11 @@ import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.apache.commons.math3.distribution.TDistribution;
import org.apache.commons.math3.exception.MathIllegalArgumentException;
import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
import java.util.*;
public class LineChartFunctionality {
......@@ -15,17 +17,18 @@ public class LineChartFunctionality {
private static CategoryAxis xAxis;
private static NumberAxis yAxis;
private static final double CONFIDENCE_INTERVAL = 0.90;
public LineChartFunctionality() {
xAxis = new CategoryAxis();
yAxis = new NumberAxis();
lineChart = new LineChart<>(xAxis, yAxis);
xAxis.setLabel("Hours");
xAxis.setLabel("Data Points");
xAxis.setAnimated(false);
yAxis.setLabel("Kwh");
yAxis.setAnimated(false);
lineChart.setTitle("Drying Processes");
lineChart.setCreateSymbols(false);
}
public static void setLineChart(LineChart<String, Number> lineChart) {
......@@ -44,99 +47,81 @@ public class LineChartFunctionality {
lineChart.getData().clear();
}
private static double findDifference(String start_date, String end_date) {
// Defining a simple date format
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static Map<Integer, ArrayList<Double>> statistics(Map<Integer, ArrayList<Double>> multiMap){
try{
// try to convert the string to Date datatype
Date dateStart = dateFormat.parse(start_date);
Date dateEnd = dateFormat.parse(end_date);
System.out.println("\n\nMultimap: \n");
for (Map.Entry<Integer, ArrayList<Double>> entry : multiMap.entrySet()) {
System.out.printf("\nIndex: \t%s\t\t\tkWh: \t%s\n", entry.getKey(), entry.getValue());
// Finds the difference in millis
double differenceMillis = dateEnd.getTime() - dateStart.getTime();
if(entry.getValue().size()>1){
SummaryStatistics stats = new SummaryStatistics();
for (double val : entry.getValue()) {
stats.addValue(val);
}
// Finds the difference in minutes
return (differenceMillis / (1000 * 60 )) % 60;
// Calculate 95% confidence interval
double ci = calcMeanCI(stats, CONFIDENCE_INTERVAL);
System.out.println(String.format("Mean: %f", stats.getMean()));
double lower = stats.getMean() - ci;
double upper = stats.getMean() + ci;
System.out.println(String.format("Confidence Interval 95%%: %f, %f", lower, upper));
} catch (Exception e) {
System.out.println(e.getMessage());
// Deletes entries if they are out of bounds with the confidence interval
entry.getValue().removeIf(value -> Double.compare(value, lower) < 0 || Double.compare(value, upper) > 0);
}
}
return multiMap;
}
private static double calcMeanCI(SummaryStatistics stats, double level) {
try {
// Create T Distribution with N-1 degrees of freedom
TDistribution tDist = new TDistribution(stats.getN() - 1);
// Calculate critical value
double critVal = tDist.inverseCumulativeProbability(1.0 - (1 - level) / 2);
// Calculate confidence interval
return critVal * stats.getStandardDeviation() / Math.sqrt(stats.getN());
} catch (MathIllegalArgumentException e) {
return Double.NaN;
}
return 0;
}
public static LineChart<String, Number> loadSingleSeries(Map<Integer, Map<String, Number>> userInput) throws Exception {
clearLineChart();
//Map<Integer, Map<String, Number>> kWh = userInput;
//Map<Integer, Map<String, Number>> kWh = DB.setInputParameters();
//System.out.println(kWh.size());
Map<Integer, ArrayList<Double>> multiMap = new HashMap<>();
for (Map.Entry<Integer, Map<String, Number>> entryKwh : userInput.entrySet()) {
Map data = entryKwh.getValue();
//System.out.println(data.size());
XYChart.Series<String, Number> newSeries = new XYChart.Series<String, Number>();
long minutes = 0;
long hours;
String previouseDate = "";
int index = 0;
for (Object entryData : data.entrySet()) {
//System.out.println("data: \t"+entryData);
String entryString = entryData.toString();
String[] arr = entryString.split("=");
String currentDate = arr[0];
int kwhValue = Integer.parseInt(arr[1]);
//System.out.printf("previouse date: \t%s\n",previouseDate);
//System.out.printf("Current date: \t\t%s\n",currentDate);
//System.out.printf("is prev empty?: \t%s\n",previouseDate.isEmpty());
minutes += findDifference(previouseDate, currentDate);
hours = minutes/60;
System.out.println(hours);
previouseDate = currentDate;
String date = arr[0];
Double kwhValue = Double.parseDouble(arr[1]);
//System.out.printf("Date: \t%s\t\t\tkWh: \t%s\n",date,kwhValue);
//System.out.printf("Hours: \t\t%s\n",hours);
// Checks if the index already got an arraylist, if not one is created
multiMap.computeIfAbsent(index, k -> new ArrayList<Double>());
multiMap.get(index).add(kwhValue);
// Connect the data to a series
newSeries.getData().add(new XYChart.Data<String, Number>(String.valueOf(hours), kwhValue));
newSeries.getData().add(new XYChart.Data<String, Number>(String.valueOf(index), kwhValue));
index += 1;
}
updateLineChart(newSeries);
}
return getLineChart();
}
public static LineChart<String, Number> loadMultipleSeries(Map<Integer, Map<String, Number>> userInput) throws Exception {
//Map<Integer, Map<String, Number>> kWh = DB.setInputParameters();
//System.out.println(kWh.size());
for (Map.Entry<Integer, Map<String, Number>> entryKwh : userInput.entrySet()) {
Map data = entryKwh.getValue();
//System.out.println(data.size());
Map<Integer, ArrayList<Double>> confidenceIntervalData = statistics(multiMap);
XYChart.Series<String, Number> newSeries = new XYChart.Series<String, Number>();
for (Object entryData : data.entrySet()) {
//System.out.println("data: \t"+entryData);
String entryString = entryData.toString();
String[] arr = entryString.split("=");
String date = arr[0];
int kwhValue = Integer.parseInt(arr[1]);
System.out.println(confidenceIntervalData);
//System.out.printf("Date: \t%s\t\t\tkWh: \t%s\n",date,kwhValue);
// Connect the data to a series
newSeries.getData().add(new XYChart.Data<String, Number>(date, kwhValue));
}
updateLineChart(newSeries);
}
return getLineChart();
}
}
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment