Repository: KIT-IAI/pyWATTS

Issue:
RollingRMSE: Extraction time index from the wrong data
**Describe the bug**
In Line 79 we extract the time index from kwargs and use this time index on y. This leads to an error if the name of the time index in kwargs and y differs.

Similiar issue in line 85 and 86 where we set the name of the time_index to time. However, in general that would not match the name of the time dimension of the result or of y.

**Expected behavior**
In all three cases `_get_time_indeces(y)[0]` is used.



Discussion:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 707f3cf..e3de7bd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -32,6 +32,7 @@
 * Fix save and load of keras Wrapper  ([#91](https://github.com/KIT-IAI/pyWATTS/issues/91))
 * Import of pytorchWrapper via wrapper.__init__
 * Fix the mask for weekends and public holidays in rolling_base  ([#107](https://github.com/KIT-IAI/pyWATTS/issues/107))
+* Fix the extracted time index in rolling RMSE  ([#124](https://github.com/KIT-IAI/pyWATTS/issues/124))
 * Use raise from if an exception is raise because an other is raised before for retaining the original stack trace  ([#107](https://github.com/KIT-IAI/pyWATTS/issues/123))
 
 
diff --git a/pywatts/modules/rolling_metric_base.py b/pywatts/modules/rolling_metric_base.py
index a842a30..8827f60 100644
--- a/pywatts/modules/rolling_metric_base.py
+++ b/pywatts/modules/rolling_metric_base.py
@@ -74,13 +74,13 @@ class RollingMetricBase(BaseTransformer, ABC):
         for key, y_hat in kwargs.items():
             p = y_hat.values
             p_, t_ = p.reshape((len(p), -1)), t.reshape((len(t), -1))
-            index = y.indexes[_get_time_indeces(kwargs)[0]]
+            index = y.indexes[_get_time_indeces(y)[0]]
             results[key] = self._apply_rolling_metric(p_, t_, index)
         time = y.indexes[_get_time_indeces(y)[0]]
 
         return xr.DataArray(np.concatenate(list(results.values()), axis=1),
-                            coords={"time": time, "predictions": list(results.keys())},
-                            dims=["time", "predictions"])
+                            coords={_get_time_indeces(y)[0]: time, "predictions": list(results.keys())},
+                            dims=[_get_time_indeces(y)[0], "predictions"])
 
     @abstractmethod
     def _apply_rolling_metric(self, p_, t_, index):


Test patch:
diff --git a/tests/unit/modules/test_rolling_rmse.py b/tests/unit/modules/test_rolling_rmse.py
index 22aaa22..18a76e4 100644
--- a/tests/unit/modules/test_rolling_rmse.py
+++ b/tests/unit/modules/test_rolling_rmse.py
@@ -1,14 +1,13 @@
 import unittest
 
+import numpy as np
+import pandas as pd
 import pytest
 import xarray as xr
-import pandas as pd
+
 from pywatts.core.exceptions.input_not_available import InputNotAvailable
 from pywatts.modules.rolling_rmse import RollingRMSE
 
-from pywatts.modules.root_mean_squared_error import RmseCalculator
-import numpy as np
-
 
 class TestRollingRMSE(unittest.TestCase):
 
@@ -34,9 +33,13 @@ class TestRollingRMSE(unittest.TestCase):
 
         test_data = xr.Dataset({"testCol": ("time", xr.DataArray([-2, -1, 0, 1, 2]).data),
                                 "predictCol1": ("time", xr.DataArray([2, -3, 3, 1, -2]).data),
-                                "predictCol2": ("time", xr.DataArray([4, 4, 3, -2, 1]).data), "time": time})
+                                "predictCol2": ("time", xr.DataArray([4, 4, 3, -2, 1]).data), "time": time, "timeTest": time})
+
+        y = xr.DataArray(np.array([-2, -1, 0, 1, 2]),
+                                       coords={"timeTest": time},
+                                       dims=["timeTest"])
 
-        test_result = self.rolling_rmse.transform(y=test_data['testCol'], gt=test_data['testCol'],
+        test_result = self.rolling_rmse.transform(y=y, gt=test_data['testCol'],
                                                   pred1=test_data['predictCol1'],
                                                   pred2=test_data['predictCol2'])
         expected_result = xr.DataArray(np.array([[0.0, 4, 6],
@@ -44,8 +47,8 @@ class TestRollingRMSE(unittest.TestCase):
                                                  [0.0, np.sqrt(6.5), np.sqrt(17)],
                                                  [0.0, np.sqrt(4.5), 3],
                                                  [0.0, np.sqrt(8), np.sqrt(5)], ]),
-                                       coords={"time": time, "predictions": ["gt", "pred1", "pred2"]},
-                                       dims=["time", "predictions"])
+                                       coords={"timeTest": time, "predictions": ["gt", "pred1", "pred2"]},
+                                       dims=["timeTest", "predictions"])
 
         xr.testing.assert_allclose(test_result, expected_result)