uqregressors.utils.file_manager
file_manager
Handles saving paths, including saving and loading models and plots.
Examples:
>>> from uqregressors.bayesian.deep_ens import DeepEnsembleRegressor
>>> from uqregressors.metrics.metrics import compute_all_metrics
>>> from uqregressors.utils.file_manager import FileManager
>>> # Instantiate File Manager
>>> BASE_PATH = "C:/.uqregressors"
>>> fm = FileManager(BASE_PATH) # Replace with desired base path
>>> # Fit a model and compute metrics
>>> reg = DeepEnsembleRegressor()
>>> reg.fit(X_train, y_train)
>>> mean, lower, upper = reg.predict(X_test)
>>> metrics = compute_all_metrics(
... mean, lower, upper, y_test, reg.alpha
... )
>>> # Save model and metrics
>>> save_path = fm.save_model(
... name="Deep_Ens",
... model=reg,
... metrics=metrics,
... X_train=X_train,
... y_train=y_train,
... X_test=X_test,
... y_test=y_test
... )
>>> # Will save to: BASE_PATH/models/Deep_Ens
>>> # Alternatively, specify full path directly
>>> save_path = fm.save_model(path="SAVE_PATH", model=reg, ...)
>>> # Load model and metrics
>>> load_dict = fm.load_model(
... reg.__class__, save_path, load_logs=True
... )
>>> metrics = load_dict["metrics"]
>>> loaded_model = load_dict["model"]
>>> X_test = load_dict["X_test"]
FileManager
FileManager class to handle paths and saving.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base_dir
|
str
|
Base directory to save files to. Defaults to creating a folder ".uqregressors" within the Users home path. |
home() / '.uqregressors'
|
Attributes:
Name | Type | Description |
---|---|---|
base_dir |
Path
|
The base directory as a Path object. |
model_dir |
Path
|
The directory "models" within the base_dir, where models will be saved and loaded. |
Source code in uqregressors\utils\file_manager.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
|
load_model(model_class, path=None, name=None, device='cpu', load_logs=False)
Loads a model and associated metadata from path
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
BaseEstimator
|
The class of the model to be loaded. This should match with the class of model which was saved. |
required |
path
|
str
|
The path to the directory in which the model and associated metadata is stored. If not given, name must be given. |
None
|
name
|
str
|
The name if the directory containing the model is fm.base_dir/models/{name}. If not given, the path must be given. |
None
|
device
|
str
|
The device, "cpu" or "cuda" to load the model with. |
'cpu'
|
load_logs
|
bool
|
Whether training and hyperparameter logs should be loaded along with the model so they can be accessed by code. |
False
|
Returns:
Type | Description |
---|---|
dict
|
Dictionary of loaded objects with the following keys:
|
Source code in uqregressors\utils\file_manager.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
|
save_model(model, name=None, path=None, metrics=None, X_train=None, y_train=None, X_test=None, y_test=None)
Saves a model, along with metrics, and training and testing data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
BaseEstimator
|
The regressor to save. Note that it must implement the save method. |
required |
name
|
str
|
The name of the model for directory purposes. If given, the model will be saved wihin the directory: fm.base_dir/models/name. |
None
|
path
|
str
|
The path to the directory where the model should be saved. Only one of name or path should be given. If neither are given, a directory with the model class and timestamp is created. |
None
|
metrics
|
dict
|
A dictionary of metrics to store. Can be used with uqregressors.metrics.metrics.compute_all_metrics. |
None
|
X_train
|
array - like
|
Training features. |
None
|
y_train
|
array - like
|
Training targets. |
None
|
X_test
|
array - like
|
Testing features. |
None
|
y_test
|
array - like
|
Testing targets. |
None
|
Source code in uqregressors\utils\file_manager.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
|
save_plot(fig, model_path, filename='plot.png', show=True, subdir='plots')
A helper method to save plots to a subdirectory within the directory in which the model would be saved.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fig
|
Figure
|
The figure to be saved. |
required |
model_path
|
str
|
The directory in which to create a "plots" subdirectory where the image will be saved. |
required |
filename
|
str
|
The filename of the plot to be saved, including the file extension. |
'plot.png'
|
show
|
bool
|
Whether to display the plot after saving it. |
True
|
subdir
|
str
|
The subdirectory in which the plot will be saved, each image will be saved to model_path/subdir/filename . |
'plots'
|
Source code in uqregressors\utils\file_manager.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
|