train: Fix NaN for unobserved, respect envvar for dataset
Browse files- batteryswap_example/train.py +21 -7
batteryswap_example/train.py
CHANGED
|
@@ -123,28 +123,35 @@ class DummyRULModel(RULModel):
|
|
| 123 |
return out[self.quantile_cols]
|
| 124 |
|
| 125 |
|
| 126 |
-
def train_rul_model(locations, timeseries, eol_times, scenarios):
|
| 127 |
|
| 128 |
# Collect training data
|
| 129 |
# FIXME: train/validate/test split to estimate generalized predictive performance
|
| 130 |
gen = iterate_scenarios(locations, timeseries, eol_times, scenarios)
|
| 131 |
cut_dfs = []
|
| 132 |
cut_ruls = []
|
|
|
|
| 133 |
for scenario, locs, cut, eol in gen:
|
| 134 |
-
|
| 135 |
cut = cut.reset_index()
|
| 136 |
cut['device_id'] = cut['device_id'].astype(str) + scenario['name']
|
| 137 |
cut = cut.set_index(['device_id', 'end_time'])
|
| 138 |
cut_dfs.append(cut)
|
| 139 |
plan_start = pandas.Timestamp(scenario['start_time'])
|
|
|
|
| 140 |
|
| 141 |
# Convert EOL datetimes to RUL in days relative to planning time
|
| 142 |
rul_days = (eol - plan_start) / pandas.Timedelta(days=1)
|
| 143 |
-
rul_days.index = pandas.Series(rul_days.index) + scenario['name']
|
|
|
|
| 144 |
cut_ruls.append(rul_days)
|
| 145 |
|
| 146 |
assert set(rul_days.index) == set(cut.index.get_level_values('device_id'))
|
| 147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
|
| 150 |
# Train a RUL prediction model
|
|
@@ -172,13 +179,18 @@ class Config(BaseSettings):
|
|
| 172 |
cli_ignore_unknown_args=True,
|
| 173 |
)
|
| 174 |
|
| 175 |
-
dataset_path: Path =
|
| 176 |
split : str = 'train'
|
| 177 |
|
| 178 |
def main():
|
| 179 |
cfg = Config()
|
| 180 |
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
split_path = dataset_path / cfg.split
|
| 184 |
locations, timeseries, eol_times, scenarios = load_dataset(split_path)
|
|
@@ -187,9 +199,11 @@ def main():
|
|
| 187 |
|
| 188 |
|
| 189 |
# Prediction model training
|
| 190 |
-
rul_model =
|
| 191 |
-
|
|
|
|
| 192 |
|
|
|
|
| 193 |
# Evaluate on the planning scenarios
|
| 194 |
gen = iterate_scenarios(locations, timeseries, eol_times, scenarios)
|
| 195 |
for scenario, locs, cut, eol in gen:
|
|
|
|
| 123 |
return out[self.quantile_cols]
|
| 124 |
|
| 125 |
|
| 126 |
+
def train_rul_model(locations, timeseries, eol_times, scenarios, limit_scenarios=None):
|
| 127 |
|
| 128 |
# Collect training data
|
| 129 |
# FIXME: train/validate/test split to estimate generalized predictive performance
|
| 130 |
gen = iterate_scenarios(locations, timeseries, eol_times, scenarios)
|
| 131 |
cut_dfs = []
|
| 132 |
cut_ruls = []
|
| 133 |
+
scenarios_loaded = 0
|
| 134 |
for scenario, locs, cut, eol in gen:
|
| 135 |
+
print('load-scenario')
|
| 136 |
cut = cut.reset_index()
|
| 137 |
cut['device_id'] = cut['device_id'].astype(str) + scenario['name']
|
| 138 |
cut = cut.set_index(['device_id', 'end_time'])
|
| 139 |
cut_dfs.append(cut)
|
| 140 |
plan_start = pandas.Timestamp(scenario['start_time'])
|
| 141 |
+
unobserved_rul = 120
|
| 142 |
|
| 143 |
# Convert EOL datetimes to RUL in days relative to planning time
|
| 144 |
rul_days = (eol - plan_start) / pandas.Timedelta(days=1)
|
| 145 |
+
rul_days.index = pandas.Series(rul_days.index) + scenario['name']
|
| 146 |
+
rul_days = rul_days.fillna(unobserved_rul)
|
| 147 |
cut_ruls.append(rul_days)
|
| 148 |
|
| 149 |
assert set(rul_days.index) == set(cut.index.get_level_values('device_id'))
|
| 150 |
|
| 151 |
+
if limit_scenarios is not None:
|
| 152 |
+
if scenarios_loaded > limit_scenarios:
|
| 153 |
+
break
|
| 154 |
+
scenarios_loaded += 1
|
| 155 |
|
| 156 |
|
| 157 |
# Train a RUL prediction model
|
|
|
|
| 179 |
cli_ignore_unknown_args=True,
|
| 180 |
)
|
| 181 |
|
| 182 |
+
dataset_path: Optional[Path] = None
|
| 183 |
split : str = 'train'
|
| 184 |
|
| 185 |
def main():
|
| 186 |
cfg = Config()
|
| 187 |
|
| 188 |
+
if cfg.dataset_path is None:
|
| 189 |
+
dataset_path = os.environ.get('BATTERYSWAP_DATASET_PATH', None)
|
| 190 |
+
assert dataset_path
|
| 191 |
+
dataset_path = Path(dataset_path)
|
| 192 |
+
else:
|
| 193 |
+
dataset_path = cfg.dataset_path
|
| 194 |
|
| 195 |
split_path = dataset_path / cfg.split
|
| 196 |
locations, timeseries, eol_times, scenarios = load_dataset(split_path)
|
|
|
|
| 199 |
|
| 200 |
|
| 201 |
# Prediction model training
|
| 202 |
+
#rul_model = DummyRULModel()
|
| 203 |
+
rul_model = train_rul_model(locations, timeseries, eol_times, scenarios, limit_scenarios=1)
|
| 204 |
+
log.info('train-done')
|
| 205 |
|
| 206 |
+
log.info('evaluate')
|
| 207 |
# Evaluate on the planning scenarios
|
| 208 |
gen = iterate_scenarios(locations, timeseries, eol_times, scenarios)
|
| 209 |
for scenario, locs, cut, eol in gen:
|