Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ Major release, not backwards compatible except for the AgentLib-MPC integration
- `PreprocessingMultiStep(eval_horizon=..., eval_stride=...)`: evaluation over a horizon longer than the training
horizon; `label_width` / `warmup_width` also as time delta strings ('12h'). RMSE, MAE and bias per step in
`td.horizon_metrics` (per output in `by_feature`).
- Metrics per output: `td.metrics.by_output[output][split]` holds MSE, RMSE, MAE, R2 and bias; `HorizonMetrics`
additionally the standard deviation of the error per step. Everything is part of the training data json.
- Plots: one report for all models (`physXAI.plotting.model_report`): per output the true vs. predicted values
with small transparent points, the binned median per split as bold line and its interquartile band, the sorted
predictions, the error over the horizon (RMSE, bias, bias ± σ), the training history (including a multi step
training) and a metrics table per output. Large point clouds are thinned out for display.
- Feature selection `physXAI.feature_selection.selection.FeatureSelection` (experimental): backward elimination
on the step weighted error of the recursive prediction (or the single step error), repeated on several block
splits (`n_repeats`, mean and spread), absolute bias at the MPC horizon, plausibility check of the sign of the
sensitivity to the controls (`controls={'u': 1}`), `tolerance` to prefer fewer inputs. Results as table, json
and plot.

### Changed
- Python >= 3.11 is required (3.9 and 3.10 are end of life).
Expand All @@ -67,8 +78,12 @@ Major release, not backwards compatible except for the AgentLib-MPC integration
holds sequences as views on it: `X_*` (warmup and prediction), `y_*` (true trajectories), `targets(split)`
and `initial(split)`.
- The batch size of recurrent models is set at the model (`RNNModel(batch_size=...)`), not the preprocessing.
- Feature selection on the multi step error uses `PreprocessingSingleStep(eval_horizon=...)` (mean RMSE over
the horizon on the validation data); the recursive feature is never removed.
- Feature selection on the multi step error uses `PreprocessingSingleStep(eval_horizon=...)`; the recursive
feature and the controls are never removed. `recursive_feature_elimination_pipeline` is a wrapper around
`FeatureSelection`: it no longer asks for input, does not change the preprocessing and saves its result as
json and html into the run directory.
- Plots are not opened anymore by default: `pipeline(..., plot=True)` saves them as `<model>.html` next to the
model (or into the run directory of the Logger); `show_plot=True` opens them. `Model.plot(td)` returns the figure.
- `random_state=None` draws a seed (or uses the global one) and stores it in the config.
- `PreprocessingData.from_config` restores the class from `__class_name__` and all settings.

Expand All @@ -91,6 +106,8 @@ Major release, not backwards compatible except for the AgentLib-MPC integration
- RNN: the summary was always printed, the verbosity ignored, the `Rescaling` layer held tensors instead of
floats, and the config validation of `init_layer` compared strings with `is not`.
- `PINNModel.pipeline` and `online_pipeline` returned None instead of the trained model.
- Metrics of several outputs were computed on the flattened outputs (mixing their units).
- Feature selection: `input()` prompts, the preprocessing was changed in place, lags were parsed from strings.

### Removed
- `physXAI.preprocessing.constructed` (`Feature`, `FeatureConstruction`): lags and `Change(X)` are derived
Expand Down
12 changes: 9 additions & 3 deletions executables/bestest_hydronic_heat_pump/P_hp_FeatureSelection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from physXAI.feature_selection.recursive_feature_elimination import recursive_feature_elimination_pipeline
from physXAI.feature_selection.recursive_feature_elimination import save_result
from physXAI.feature_selection.selection import FeatureSelection
from physXAI.models.models import LinearRegressionModel
from physXAI.preprocessing.preprocessing import PreprocessingSingleStep
from physXAI.utils.logging import Logger
Expand Down Expand Up @@ -29,5 +30,10 @@
# Generic Model
m = LinearRegressionModel()

# Feature Selection
fs = recursive_feature_elimination_pipeline(file_path, prep, m, ascending_lag_order=True)
# Feature Selection (experimental). The power has to increase with the heat pump signal (plausibility check)
fs = FeatureSelection(prep, m, controls={'oveHeaPumY_u': 1}, n_repeats=3)
result = fs.run(file_path)
print('Selected inputs:', result.selected.inputs)

# Save the result (json) and its plot (html) into the run directory
save_result(result)
33 changes: 21 additions & 12 deletions executables/bestest_hydronic_heat_pump/TAir_FeatureSelection.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
from physXAI.feature_selection.recursive_feature_elimination import recursive_feature_elimination_pipeline
from physXAI.feature_selection.recursive_feature_elimination import save_result
from physXAI.feature_selection.selection import FeatureSelection
from physXAI.models.models import LinearRegressionModel
from physXAI.preprocessing.preprocessing import PreprocessingSingleStep
from physXAI.utils.logging import Logger


"""
Feature selection for linear regression to predict the air temperature using the Boptest data
Feature selection (experimental) for a linear regression predicting the air temperature using the Boptest data.

- The single step error hardly correlates with the MPC performance, so the candidates are compared by the error
of the recursive prediction over 12 hours (eval_horizon), weighted with the first step counting more.
- Every candidate is evaluated on 3 different block splits, the spread shows which differences are significant.
- The heat pump is the control of the MPC: a candidate whose prediction does not increase with the heat pump
signal on (almost) all validation samples is marked implausible and not selected.
"""
# Setup up logger for saving
Logger.setup_logger(folder_name='TAir_feature', override=True)

# File path to data
file_path = r"data/bestest_hydronic_heat_pump/pid_data.csv"

# List of all possible input features candidates.
inputs = ['reaTZon_y', 'reaTZon_y_lag1', 'reaTZon_y_lag2', 'weaSta_reaWeaTDryBul_y', 'weaSta_reaWeaTDryBul_y_lag1',
'weaSta_reaWeaTDryBul_y_lag2', 'weaSta_reaWeaHDirNor_y', 'weaSta_reaWeaHDirNor_y_lag1',
'weaSta_reaWeaHDirNor_y_lag2', 'oveHeaPumY_u', 'oveHeaPumY_u_lag1', 'oveHeaPumY_u_lag2']
# List of all possible input features candidates. The recursive feature and its lags come last.
inputs = ['weaSta_reaWeaTDryBul_y', 'weaSta_reaWeaTDryBul_y_lag1', 'weaSta_reaWeaTDryBul_y_lag2',
'weaSta_reaWeaHDirNor_y', 'weaSta_reaWeaHDirNor_y_lag1', 'weaSta_reaWeaHDirNor_y_lag2',
'oveHeaPumY_u', 'oveHeaPumY_u_lag1', 'oveHeaPumY_u_lag2', 'reaTZon_y', 'reaTZon_y_lag1', 'reaTZon_y_lag2']
# Output feature
output = 'Change(reaTZon_y)'

# Lagged inputs ('X_lag1', ...) are derived automatically from their names

# Generic Preprocessing Pipeline
# The model is a state model, so it is evaluated recursively over 48 steps (multi step error)
# See example TAir_evaluateMultiStep.py for more information
# Generic Preprocessing Pipeline. The model is a state model, so it is evaluated recursively over 12 h (48 steps)
prep = PreprocessingSingleStep(inputs=inputs, output=output, eval_horizon=48)

# Generic Model
m = LinearRegressionModel()

# Feature Selection
fs = recursive_feature_elimination_pipeline(file_path, prep, m, use_multi_step_error=True, ascending_lag_order=True)
fs = FeatureSelection(prep, m, first_step_weight=0.25, n_repeats=3, controls={'oveHeaPumY_u': 1}, tolerance=0.02)
result = fs.run(file_path)
print('Selected inputs:', result.selected.inputs)
print(result.table()[['n_inputs', 'score', 'score_std', 'single_step_rmse', 'plausible']].to_string())

# Save the result (json) and its plot (html) into the run directory
save_result(result)
Loading
Loading