From 3e2608376576951f9f9af32939e67f90d5c9fd08 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 19 Apr 2022 22:13:50 +0400 Subject: [PATCH] #3 -- F-Transform model of time series --- .../ru/ulstu/controller/IndexController.java | 63 +++++++++++-------- src/main/java/ru/ulstu/db/DbFileService.java | 3 +- .../ulstu/method/ftransform/FTransform.java | 4 +- src/main/resources/templates/method.html | 4 ++ 4 files changed, 45 insertions(+), 29 deletions(-) diff --git a/src/main/java/ru/ulstu/controller/IndexController.java b/src/main/java/ru/ulstu/controller/IndexController.java index 05814e0..4a329cc 100644 --- a/src/main/java/ru/ulstu/controller/IndexController.java +++ b/src/main/java/ru/ulstu/controller/IndexController.java @@ -55,31 +55,6 @@ public class IndexController { return "index"; } - @GetMapping("/method") - public String method(Model model) throws IOException { - model.addAttribute("sets", dbService.getSets()); - model.addAttribute("methods", timeSeriesService.getAvailableMethods()); - model.addAttribute("chartForm", new ChartForm()); - return "method"; - } - - @GetMapping("chartMethod") - public String chartMethod(@ModelAttribute ChartForm chartForm, Model model) throws IOException, ModelingException, ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { - model.addAttribute("sets", dbService.getSets()); - model.addAttribute("methods", timeSeriesService.getAvailableMethods()); - if (chartForm.getSet() != null && !chartForm.getSet().getKey().equals("")) { - model.addAttribute("listTimeSeries", dbService.getTimeSeriesMeta(chartForm.getSet())); - } - if (chartForm.getTimeSeriesMeta() != null - && chartForm.getTimeSeriesMeta().getKey() != null - && !chartForm.getTimeSeriesMeta().getKey().isEmpty() - && chartForm.getMethodClassName() != null - && !chartForm.getMethodClassName().equals("")) { - addChartToModel(dbService.getTimeSeries(chartForm.getSet(), chartForm.getTimeSeriesMeta().getKey()), chartForm.getMethodClassName(), model); - } - return "method"; - } - private void addChartToModel(TimeSeries timeSeries, String method, Model model) throws ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, ModelingException { int countForecastPoints = timeSeries.getLength() > 20 ? 10 : timeSeries.getLength() / 3; TimeSeries timeSeriesModel; @@ -95,7 +70,18 @@ public class IndexController { TimeSeries testForecast = modelingResult.getTestForecast(); model.addAttribute("dates", getDatesForChart(timeSeries, forecast)); model.addAttribute("timeSeries", timeSeries.getValues().stream().map(TimeSeriesValue::getValue).toArray()); - model.addAttribute("model", timeSeriesModel.getValues().stream().map(TimeSeriesValue::getValue).toArray()); + // если временной ряд был сжат моделью, то для графика нужно вставить пустые значения + TimeSeries modelWithSkips = new TimeSeries(timeSeriesModel.getKey()); + int j = 0; + for (int i = 0; i < timeSeries.getLength(); i++) { + if (timeSeries.getValue(i).getDate().equals(timeSeriesModel.getValue(j).getDate())) { + modelWithSkips.addValue(timeSeriesModel.getValue(j)); + j++; + } else { + modelWithSkips.addValue(new TimeSeriesValue((Double) null)); + } + } + model.addAttribute("model", modelWithSkips.getValues().stream().map(TimeSeriesValue::getValue).toArray()); timeSeries.getValues().remove(timeSeries.getValues().size() - 1); List forecastValues = timeSeries.getValues().stream().map(v -> (Double) null).collect(Collectors.toList()); @@ -121,4 +107,29 @@ public class IndexController { .collect(Collectors.toList()); } + + @GetMapping("/method") + public String method(Model model) throws IOException { + model.addAttribute("sets", dbService.getSets()); + model.addAttribute("methods", timeSeriesService.getAvailableMethods()); + model.addAttribute("chartForm", new ChartForm()); + return "method"; + } + + @GetMapping("chartMethod") + public String chartMethod(@ModelAttribute ChartForm chartForm, Model model) throws IOException, ModelingException, ExecutionException, InterruptedException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { + model.addAttribute("sets", dbService.getSets()); + model.addAttribute("methods", timeSeriesService.getAvailableMethods()); + if (chartForm.getSet() != null && !chartForm.getSet().getKey().equals("")) { + model.addAttribute("listTimeSeries", dbService.getTimeSeriesMeta(chartForm.getSet())); + } + if (chartForm.getTimeSeriesMeta() != null + && chartForm.getTimeSeriesMeta().getKey() != null + && !chartForm.getTimeSeriesMeta().getKey().isEmpty() + && chartForm.getMethodClassName() != null + && !chartForm.getMethodClassName().equals("")) { + addChartToModel(dbService.getTimeSeries(chartForm.getSet(), chartForm.getTimeSeriesMeta().getKey()), chartForm.getMethodClassName(), model); + } + return "method"; + } } diff --git a/src/main/java/ru/ulstu/db/DbFileService.java b/src/main/java/ru/ulstu/db/DbFileService.java index 90a7e9d..aa322d4 100644 --- a/src/main/java/ru/ulstu/db/DbFileService.java +++ b/src/main/java/ru/ulstu/db/DbFileService.java @@ -35,6 +35,7 @@ public class DbFileService implements DbService { createDbIfNotExists(); return Arrays.stream(Objects.requireNonNull(new File(timeSeriesDbPath).listFiles(File::isDirectory))) .map(TimeSeriesSet::new) + .sorted() .collect(Collectors.toList()); } @@ -47,7 +48,7 @@ public class DbFileService implements DbService { .readValue(Paths.get(getSetPath(timeSeriesSet).getAbsolutePath(), file.getName()) .toFile(), TimeSeriesMeta.class)); } - return list; + return list.stream().sorted().collect(Collectors.toList()); } @Override diff --git a/src/main/java/ru/ulstu/method/ftransform/FTransform.java b/src/main/java/ru/ulstu/method/ftransform/FTransform.java index 3981e97..e533c9c 100644 --- a/src/main/java/ru/ulstu/method/ftransform/FTransform.java +++ b/src/main/java/ru/ulstu/method/ftransform/FTransform.java @@ -17,7 +17,7 @@ public class FTransform extends Method { List parameters) { FTransformModel model = new FTransformModel(ts, parameters); List aComponents = generateAComponents(ts, model.getNumberOfCoveredPoints().getIntValue(), model.getAComponents()); - ; + TimeSeries piecewiseLinearTrend = model.getPiecewiseLinearTrend(); TimeSeries tsModel = model.getTimeSeriesModel(); @@ -42,7 +42,7 @@ public class FTransform extends Method { int startPoint = (currentPoint == 0) ? 0 : piecewiseLinearTrend.get(piecewiseLinearTrend.size() - 1).getTop(); - AComponent bf = new AComponent(startPoint, currentPoint, currentPoint + numberOfCoveredPoints / 2); + AComponent bf = new AComponent(startPoint, currentPoint, (int) (currentPoint + Math.round(numberOfCoveredPoints / 2.0))); if (bf.getStart() < 0) { bf.setStart(0); } diff --git a/src/main/resources/templates/method.html b/src/main/resources/templates/method.html index ba39def..9ee0aa4 100644 --- a/src/main/resources/templates/method.html +++ b/src/main/resources/templates/method.html @@ -63,21 +63,25 @@ { name: [[#{messages.time_series}]], color: 'rgba(119,152,191,0.5)', + connectNulls: true, data: [[${timeSeries}]] }, { name: [[#{messages.time_series_model}]], color: 'rgba(255,200,0,0.5)', + connectNulls: true, data: [[${model}]] }, { name: [[#{messages.time_series_test_forecast}]], color: 'rgba(255,0,0,0.5)', + connectNulls: true, data: [[${testForecast}]] }, { name: [[#{messages.time_series_forecast_short}]], color: 'rgba(255,0,0,0.5)', + connectNulls: true, data: [[${forecast}]] } ];