From 757d9b68ba085259b0148bbd16d7de091fde8cc4 Mon Sep 17 00:00:00 2001 From: ZhengyanZhu Date: Wed, 20 Mar 2024 19:32:20 +0100 Subject: [PATCH] Add docstrings --- rul_datasets/reader/ncmapss.py | 15 +++++++++------ tests/reader/test_ncmapps.py | 0 tests/reader/test_ncmapss.py | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 tests/reader/test_ncmapps.py diff --git a/rul_datasets/reader/ncmapss.py b/rul_datasets/reader/ncmapss.py index 1b98f6f..9a24801 100644 --- a/rul_datasets/reader/ncmapss.py +++ b/rul_datasets/reader/ncmapss.py @@ -123,10 +123,11 @@ def __init__( truncate_degraded_only: bool = False, resolution_seconds: int = 1, padding_value: float = 0.0, + scaling_range: [float, float] = None, ) -> None: """ Create a new reader for the New C-MAPSS dataset. The maximum RUL value is set - to 65 by default. The default channels are the four operating conditions, + to 65 by default. The default channels are the four operating conditions, the 14 physical, and 14 virtual sensors in this order. The default window size is, by default, the longest flight cycle in the @@ -172,6 +173,7 @@ def __init__( self.run_split_dist = run_split_dist or self._get_default_split(self.fd) self.resolution_seconds = resolution_seconds self.padding_value = padding_value + self.scaling_range = scaling_range if self.resolution_seconds > 1 and window_size is None: warnings.warn( @@ -189,6 +191,7 @@ def hparams(self): "run_split_dist": self.run_split_dist, "feature_select": self.feature_select, "padding_value": self.padding_value, + "scaling_range": self.scaling_range, } ) @@ -214,10 +217,10 @@ def prepare_data(self) -> None: """ if not os.path.exists(self._NCMAPSS_ROOT): _download_ncmapss(self._NCMAPSS_ROOT) - if not os.path.exists(self._get_scaler_path()): - features, _, _ = self._load_data("dev") - scaler = scaling.fit_scaler(features, MinMaxScaler()) - scaling.save_scaler(scaler, self._get_scaler_path()) + #if not os.path.exists(self._get_scaler_path()): + features, _, _ = self._load_data("dev") + scaler = scaling.fit_scaler(features, MinMaxScaler()) + scaling.save_scaler(scaler, self._get_scaler_path()) def _get_scaler_path(self): file_name = f"scaler_{self.fd}_{self.run_split_dist['dev']}.pkl" @@ -301,7 +304,7 @@ def _select_units(self, units, split): return [units[i] for i in self.run_split_dist[split]] def _window_by_cycle( - self, features: np.ndarray, targets: np.ndarray, auxiliary: np.ndarray + self, features: np.ndarray, targets: np.ndarray, auxiliary: np.ndarray ) -> Tuple[np.ndarray, np.ndarray]: cycle_end_idx = self._get_end_idx(auxiliary[:, 1]) split_features = np.split(features, cycle_end_idx[:-1]) diff --git a/tests/reader/test_ncmapps.py b/tests/reader/test_ncmapps.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/reader/test_ncmapss.py b/tests/reader/test_ncmapss.py index 9bfc618..499e51c 100644 --- a/tests/reader/test_ncmapss.py +++ b/tests/reader/test_ncmapss.py @@ -47,6 +47,22 @@ def test_prepare_data(should_run, mocker): mock_save_scaler.assert_not_called() + +@pytest.mark.needs_data +@pytest.mark.parametrize("scaling_range", [(-1.0, 1.0), (0.0, 2.0)]) +def test_scaling_range(scaling_range): + reader = NCmapssReader(fd=1, scaling_range=scaling_range) + reader.prepare_data() + features, _ = reader.load_split("dev") + + reader = NCmapssReader(fd=1, scaling_range=(0, 1)) + reader.prepare_data() + features_default, _ = reader.load_split("dev") + + assert not np.array_equal(features[0][:, :, 1], features_default[0][:, :, 1]) + + + @pytest.mark.needs_data @pytest.mark.parametrize("fd", list(range(1, 8))) @pytest.mark.parametrize("split", ["dev", "val", "test"])