diff --git a/Bachelor_application.iml b/Bachelor_application.iml index bcd84db39f631518a3f8fc961e3ff70eb08138ab..cde0e4ea36b288c8f56e3bcc95d2a8443a0b7115 100644 --- a/Bachelor_application.iml +++ b/Bachelor_application.iml @@ -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 diff --git a/pom.xml b/pom.xml index 3126ade87e22757652cbda929a5fcb0fc37529cf..014584e6a9919ce414793a105fa695baaabac1ce 100644 --- a/pom.xml +++ b/pom.xml @@ -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> diff --git a/src/main/java/com/application/GUI/LineChartFunctionality.java b/src/main/java/com/application/GUI/LineChartFunctionality.java index f4c249ba613cebc05d069c411f1b08b223a37378..29d61ae03de5354e6c3756e79ba94e41603aa299 100644 --- a/src/main/java/com/application/GUI/LineChartFunctionality.java +++ b/src/main/java/com/application/GUI/LineChartFunctionality.java @@ -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(); } } diff --git a/target/classes/com/application/GUI/LineChartFunctionality.class b/target/classes/com/application/GUI/LineChartFunctionality.class index 8427e84169ee5bbc8f1c49de41a658e1d8cf6745..a7c6a67ecc6871b6999f49cf884d5f375c5ac204 100644 Binary files a/target/classes/com/application/GUI/LineChartFunctionality.class and b/target/classes/com/application/GUI/LineChartFunctionality.class differ