#3 -- F-Transform model of time series

This commit is contained in:
Anton Romanov 2022-04-19 22:13:50 +04:00
parent 35fedf4fec
commit 3e26083765
4 changed files with 45 additions and 29 deletions

View File

@ -55,31 +55,6 @@ public class IndexController {
return "index"; 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 { 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; int countForecastPoints = timeSeries.getLength() > 20 ? 10 : timeSeries.getLength() / 3;
TimeSeries timeSeriesModel; TimeSeries timeSeriesModel;
@ -95,7 +70,18 @@ public class IndexController {
TimeSeries testForecast = modelingResult.getTestForecast(); TimeSeries testForecast = modelingResult.getTestForecast();
model.addAttribute("dates", getDatesForChart(timeSeries, forecast)); model.addAttribute("dates", getDatesForChart(timeSeries, forecast));
model.addAttribute("timeSeries", timeSeries.getValues().stream().map(TimeSeriesValue::getValue).toArray()); 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); timeSeries.getValues().remove(timeSeries.getValues().size() - 1);
List<Double> forecastValues = timeSeries.getValues().stream().map(v -> (Double) null).collect(Collectors.toList()); List<Double> forecastValues = timeSeries.getValues().stream().map(v -> (Double) null).collect(Collectors.toList());
@ -121,4 +107,29 @@ public class IndexController {
.collect(Collectors.toList()); .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";
}
} }

View File

@ -35,6 +35,7 @@ public class DbFileService implements DbService {
createDbIfNotExists(); createDbIfNotExists();
return Arrays.stream(Objects.requireNonNull(new File(timeSeriesDbPath).listFiles(File::isDirectory))) return Arrays.stream(Objects.requireNonNull(new File(timeSeriesDbPath).listFiles(File::isDirectory)))
.map(TimeSeriesSet::new) .map(TimeSeriesSet::new)
.sorted()
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@ -47,7 +48,7 @@ public class DbFileService implements DbService {
.readValue(Paths.get(getSetPath(timeSeriesSet).getAbsolutePath(), file.getName()) .readValue(Paths.get(getSetPath(timeSeriesSet).getAbsolutePath(), file.getName())
.toFile(), TimeSeriesMeta.class)); .toFile(), TimeSeriesMeta.class));
} }
return list; return list.stream().sorted().collect(Collectors.toList());
} }
@Override @Override

View File

@ -17,7 +17,7 @@ public class FTransform extends Method {
List<MethodParamValue> parameters) { List<MethodParamValue> parameters) {
FTransformModel model = new FTransformModel(ts, parameters); FTransformModel model = new FTransformModel(ts, parameters);
List<AComponent> aComponents = generateAComponents(ts, model.getNumberOfCoveredPoints().getIntValue(), model.getAComponents()); List<AComponent> aComponents = generateAComponents(ts, model.getNumberOfCoveredPoints().getIntValue(), model.getAComponents());
;
TimeSeries piecewiseLinearTrend = model.getPiecewiseLinearTrend(); TimeSeries piecewiseLinearTrend = model.getPiecewiseLinearTrend();
TimeSeries tsModel = model.getTimeSeriesModel(); TimeSeries tsModel = model.getTimeSeriesModel();
@ -42,7 +42,7 @@ public class FTransform extends Method {
int startPoint = (currentPoint == 0) int startPoint = (currentPoint == 0)
? 0 ? 0
: piecewiseLinearTrend.get(piecewiseLinearTrend.size() - 1).getTop(); : 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) { if (bf.getStart() < 0) {
bf.setStart(0); bf.setStart(0);
} }

View File

@ -63,21 +63,25 @@
{ {
name: [[#{messages.time_series}]], name: [[#{messages.time_series}]],
color: 'rgba(119,152,191,0.5)', color: 'rgba(119,152,191,0.5)',
connectNulls: true,
data: [[${timeSeries}]] data: [[${timeSeries}]]
}, },
{ {
name: [[#{messages.time_series_model}]], name: [[#{messages.time_series_model}]],
color: 'rgba(255,200,0,0.5)', color: 'rgba(255,200,0,0.5)',
connectNulls: true,
data: [[${model}]] data: [[${model}]]
}, },
{ {
name: [[#{messages.time_series_test_forecast}]], name: [[#{messages.time_series_test_forecast}]],
color: 'rgba(255,0,0,0.5)', color: 'rgba(255,0,0,0.5)',
connectNulls: true,
data: [[${testForecast}]] data: [[${testForecast}]]
}, },
{ {
name: [[#{messages.time_series_forecast_short}]], name: [[#{messages.time_series_forecast_short}]],
color: 'rgba(255,0,0,0.5)', color: 'rgba(255,0,0,0.5)',
connectNulls: true,
data: [[${forecast}]] data: [[${forecast}]]
} }
]; ];