seawrd 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- seawrd-0.1.0/PKG-INFO +14 -0
- seawrd-0.1.0/README.md +2 -0
- seawrd-0.1.0/pyproject.toml +28 -0
- seawrd-0.1.0/seawrd/__init__.py +0 -0
- seawrd-0.1.0/seawrd/model.py +258 -0
- seawrd-0.1.0/seawrd.egg-info/PKG-INFO +14 -0
- seawrd-0.1.0/seawrd.egg-info/SOURCES.txt +9 -0
- seawrd-0.1.0/seawrd.egg-info/dependency_links.txt +1 -0
- seawrd-0.1.0/seawrd.egg-info/requires.txt +3 -0
- seawrd-0.1.0/seawrd.egg-info/top_level.txt +1 -0
- seawrd-0.1.0/setup.cfg +4 -0
seawrd-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: seawrd
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Surrogate Emulator for Aquatic World Radius Determination
|
|
5
|
+
Author-email: Ashley Parr <amparr83@gmail.com>, Bishwash Devkota <bishwashdevkota567@gmail.com>, Fredi Quispe Huaynasi <fredifqh@gmail.com>, Ian Rain-water <irainw@stanford.edu>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Repository, https://github.com/Siphlygon/SEAWRD
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
Requires-Dist: keras
|
|
10
|
+
Requires-Dist: tensorflow_docs
|
|
11
|
+
Requires-Dist: numpy
|
|
12
|
+
|
|
13
|
+
# SEAWARD
|
|
14
|
+
Surrogate Emulator for Aquatic World Radius Determination
|
seawrd-0.1.0/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "seawrd"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
dependencies = [
|
|
9
|
+
"keras",
|
|
10
|
+
"tensorflow_docs",
|
|
11
|
+
"numpy"
|
|
12
|
+
]
|
|
13
|
+
authors = [
|
|
14
|
+
{name = "Ashley Parr", email = "amparr83@gmail.com"},
|
|
15
|
+
{name = "Bishwash Devkota", email = "bishwashdevkota567@gmail.com"},
|
|
16
|
+
{name = "Fredi Quispe Huaynasi", email = "fredifqh@gmail.com"},
|
|
17
|
+
{name = "Ian Rain-water", email = "irainw@stanford.edu"}
|
|
18
|
+
]
|
|
19
|
+
description = "Surrogate Emulator for Aquatic World Radius Determination"
|
|
20
|
+
readme = "README.md"
|
|
21
|
+
license = "MIT"
|
|
22
|
+
license-files = ["LICEN[CS]E.*"]
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
Repository = "https://github.com/Siphlygon/SEAWRD"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
File without changes
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import keras
|
|
2
|
+
import tensorflow as tf
|
|
3
|
+
import numpy as np
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
import re
|
|
6
|
+
import pickle
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class DNN:
|
|
10
|
+
"""
|
|
11
|
+
A class to manage the creation, saving, and loading of a Deep Neural Network (DNN) model using Keras.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
def __init__(self,
|
|
15
|
+
num_layers : int,
|
|
16
|
+
num_neurons : int,
|
|
17
|
+
num_epochs : int):
|
|
18
|
+
"""
|
|
19
|
+
Initialises the DNN class with the specified number of layers, neurons, and epochs.
|
|
20
|
+
|
|
21
|
+
Parameters
|
|
22
|
+
----------
|
|
23
|
+
num_layers : int
|
|
24
|
+
The number of hidden layers in the neural network.
|
|
25
|
+
num_neurons : int
|
|
26
|
+
The number of neurons in each hidden layer.
|
|
27
|
+
num_epochs : int
|
|
28
|
+
The number of epochs to train the model.
|
|
29
|
+
"""
|
|
30
|
+
self.num_layers = num_layers
|
|
31
|
+
self.num_neurons = num_neurons
|
|
32
|
+
self.num_epochs = num_epochs
|
|
33
|
+
|
|
34
|
+
# ---------- MODEL ARCHITECTURE ----------
|
|
35
|
+
def generate_model(self,
|
|
36
|
+
train_features : np.ndarray,
|
|
37
|
+
normalizer : keras.layers.Normalization,
|
|
38
|
+
labels_cols : list) -> keras.Sequential:
|
|
39
|
+
"""
|
|
40
|
+
Generates a Keras Sequential model, using an input layer, normalisation, num_layers hidden dense layers of
|
|
41
|
+
num_neurons each, and an output layer specified by labels_cols.
|
|
42
|
+
|
|
43
|
+
Parameters
|
|
44
|
+
----------
|
|
45
|
+
train_features : np.ndarray
|
|
46
|
+
The training features.
|
|
47
|
+
normalizer : keras.layers.Normalization
|
|
48
|
+
The normalisation layer to be applied to the input features.
|
|
49
|
+
labels_cols : list
|
|
50
|
+
The list of column names for the labels.
|
|
51
|
+
|
|
52
|
+
Returns
|
|
53
|
+
-------
|
|
54
|
+
keras.Sequential
|
|
55
|
+
The constructed Keras Sequential model.
|
|
56
|
+
"""
|
|
57
|
+
# Creates a new model
|
|
58
|
+
dnn_model = keras.Sequential()
|
|
59
|
+
|
|
60
|
+
# Add an input layer for features
|
|
61
|
+
dnn_model.add(keras.Input(shape=(train_features.shape[1],)))
|
|
62
|
+
dnn_model.add(normalizer)
|
|
63
|
+
|
|
64
|
+
# Add hidden layers
|
|
65
|
+
for _ in range(self.num_layers):
|
|
66
|
+
dnn_model.add(keras.layers.Dense(self.num_neurons, activation='relu'))
|
|
67
|
+
|
|
68
|
+
# Add output layer
|
|
69
|
+
dnn_model.add(keras.layers.Dense(len(labels_cols)))
|
|
70
|
+
|
|
71
|
+
return dnn_model
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
# ---------- MODEL SAVING AND LOADING ----------
|
|
75
|
+
def latest_version(self,
|
|
76
|
+
model_dir : Path | str,
|
|
77
|
+
model_name : str,
|
|
78
|
+
pattern : re.Pattern | str | None = None) -> int:
|
|
79
|
+
"""
|
|
80
|
+
Finds the latest version of a model from the specified directory.
|
|
81
|
+
|
|
82
|
+
Parameters
|
|
83
|
+
----------
|
|
84
|
+
model_dir : Path | str
|
|
85
|
+
The directory containing the model files.
|
|
86
|
+
model_name : str
|
|
87
|
+
The name of the model.
|
|
88
|
+
pattern : re.Pattern | str | None, optional
|
|
89
|
+
The regex pattern to match model files, by default None
|
|
90
|
+
|
|
91
|
+
Returns
|
|
92
|
+
-------
|
|
93
|
+
int
|
|
94
|
+
The latest version number of the model, or 0 if none exists.
|
|
95
|
+
"""
|
|
96
|
+
if pattern is None:
|
|
97
|
+
pattern = re.compile(rf"^{re.escape(model_name)}_v(\d+)_model\.keras$")
|
|
98
|
+
|
|
99
|
+
if isinstance(pattern, str):
|
|
100
|
+
pattern = re.compile(pattern)
|
|
101
|
+
|
|
102
|
+
if isinstance(model_dir, str):
|
|
103
|
+
model_dir = Path(model_dir)
|
|
104
|
+
|
|
105
|
+
# Find all model files matching the pattern and extract their version numbers
|
|
106
|
+
versions = []
|
|
107
|
+
for path in model_dir.glob(f"{model_name}_v*_model.keras"):
|
|
108
|
+
match = pattern.match(path.name)
|
|
109
|
+
if match:
|
|
110
|
+
versions.append(int(match.group(1)))
|
|
111
|
+
|
|
112
|
+
return max(versions) if versions else 0
|
|
113
|
+
|
|
114
|
+
def get_model_paths(self,
|
|
115
|
+
model_dir : Path | str,
|
|
116
|
+
model_name : str,
|
|
117
|
+
version : int) -> dict[str, Path]:
|
|
118
|
+
"""
|
|
119
|
+
Get the paths for the model, history, and plots for a specific version.
|
|
120
|
+
|
|
121
|
+
Parameters
|
|
122
|
+
----------
|
|
123
|
+
model_dir : Path | str
|
|
124
|
+
The directory containing the model files.
|
|
125
|
+
model_name : str
|
|
126
|
+
The name of the model.
|
|
127
|
+
version : int
|
|
128
|
+
The version number of the model.
|
|
129
|
+
|
|
130
|
+
Returns
|
|
131
|
+
-------
|
|
132
|
+
dict[str, Path]
|
|
133
|
+
A dictionary containing the paths for the model, history, and plots.
|
|
134
|
+
"""
|
|
135
|
+
if isinstance(model_dir, str):
|
|
136
|
+
model_dir = Path(model_dir)
|
|
137
|
+
|
|
138
|
+
base = model_dir / f"{model_name}_v{version}"
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
"model": base.with_name(base.name + "_model.keras"),
|
|
142
|
+
"history": base.with_name(base.name + "_history.pkl"),
|
|
143
|
+
"plots": base.with_name(base.name + "_plots.png"),
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
def save_model_version(self,
|
|
147
|
+
model : keras.Model,
|
|
148
|
+
history : keras.callbacks.History,
|
|
149
|
+
model_dir : Path | str,
|
|
150
|
+
model_name : str,
|
|
151
|
+
version : int) -> dict[str, Path]:
|
|
152
|
+
"""
|
|
153
|
+
Save a specific version of the model and its training history.
|
|
154
|
+
|
|
155
|
+
Parameters
|
|
156
|
+
----------
|
|
157
|
+
model : keras.Model
|
|
158
|
+
The model to save.
|
|
159
|
+
history : keras.callbacks.History
|
|
160
|
+
The training history to save.
|
|
161
|
+
model_dir : Path | str
|
|
162
|
+
The directory to save the model in.
|
|
163
|
+
model_name : str
|
|
164
|
+
The name of the model.
|
|
165
|
+
version : int
|
|
166
|
+
The version number of the model.
|
|
167
|
+
|
|
168
|
+
Returns
|
|
169
|
+
-------
|
|
170
|
+
dict[str, Path]
|
|
171
|
+
A dictionary containing the paths for the saved files.
|
|
172
|
+
"""
|
|
173
|
+
if isinstance(model_dir, str):
|
|
174
|
+
model_dir = Path(model_dir)
|
|
175
|
+
|
|
176
|
+
# Tries to create a folder to save the model, if it already exists, it will not raise an error
|
|
177
|
+
model_dir.mkdir(parents=True, exist_ok=True)
|
|
178
|
+
|
|
179
|
+
# Get the paths for the model and history files
|
|
180
|
+
paths = self.get_model_paths(model_dir, model_name, version)
|
|
181
|
+
|
|
182
|
+
# Save the model and history
|
|
183
|
+
model.save(paths["model"])
|
|
184
|
+
with open(paths["history"], "wb") as f:
|
|
185
|
+
pickle.dump(history.history, f)
|
|
186
|
+
|
|
187
|
+
print(f"Saved model version v{version}")
|
|
188
|
+
print(f"Model: {paths['model']}")
|
|
189
|
+
print(f"History: {paths['history']}")
|
|
190
|
+
|
|
191
|
+
return paths
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def load_model_version(self,
|
|
195
|
+
model_dir : Path | str,
|
|
196
|
+
model_name : str,
|
|
197
|
+
version : int | None = None) -> tuple[keras.Model, dict | None, int]:
|
|
198
|
+
"""
|
|
199
|
+
Load a specific version of the model and its training history.
|
|
200
|
+
|
|
201
|
+
Parameters
|
|
202
|
+
----------
|
|
203
|
+
model_dir : Path | str
|
|
204
|
+
The directory containing the saved model.
|
|
205
|
+
model_name : str
|
|
206
|
+
The name of the model.
|
|
207
|
+
version : int | None, optional
|
|
208
|
+
The version number of the model to load. If None, loads the latest version, by default None
|
|
209
|
+
|
|
210
|
+
Returns
|
|
211
|
+
-------
|
|
212
|
+
tuple[keras.Model, dict | None, int]
|
|
213
|
+
A tuple containing the loaded model, its training history (if available), and the version number.
|
|
214
|
+
|
|
215
|
+
Raises
|
|
216
|
+
------
|
|
217
|
+
FileNotFoundError
|
|
218
|
+
If no saved model is found for the specified model name.
|
|
219
|
+
"""
|
|
220
|
+
if isinstance(model_dir, str):
|
|
221
|
+
model_dir = Path(model_dir)
|
|
222
|
+
|
|
223
|
+
# If no version is specified, find the latest version
|
|
224
|
+
if version is None:
|
|
225
|
+
version = self.latest_version(model_dir, model_name)
|
|
226
|
+
|
|
227
|
+
# If the specified version does not exist, raise an error
|
|
228
|
+
if version == 0:
|
|
229
|
+
raise FileNotFoundError(f"No saved model found for '{model_name}'.")
|
|
230
|
+
|
|
231
|
+
paths = self.get_model_paths(model_dir, model_name, version)
|
|
232
|
+
model = keras.models.load_model(paths["model"])
|
|
233
|
+
|
|
234
|
+
history = None
|
|
235
|
+
if paths["history"].exists():
|
|
236
|
+
with open(paths["history"], "rb") as f:
|
|
237
|
+
history = pickle.load(f)
|
|
238
|
+
|
|
239
|
+
print(f"Loaded model version v{version}")
|
|
240
|
+
|
|
241
|
+
return model, history, version
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
if __name__ == "__main__":
|
|
245
|
+
# Example usage of the DNN class
|
|
246
|
+
dnn_manager = DNN(num_layers=2, num_neurons=8, num_epochs=1000)
|
|
247
|
+
|
|
248
|
+
# Example training features (replace with actual data)
|
|
249
|
+
train_features = np.asarray([[0, 1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6]])
|
|
250
|
+
|
|
251
|
+
# Initialise and adapt the normaliser with the training features
|
|
252
|
+
normaliser = keras.layers.Normalization(axis=-1)
|
|
253
|
+
normaliser.adapt(np.array(train_features))
|
|
254
|
+
|
|
255
|
+
label_cols = ["R_p"]
|
|
256
|
+
|
|
257
|
+
dnn = dnn_manager.generate_model(train_features, normaliser, label_cols)
|
|
258
|
+
dnn.fit(train_features, label_cols)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: seawrd
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Surrogate Emulator for Aquatic World Radius Determination
|
|
5
|
+
Author-email: Ashley Parr <amparr83@gmail.com>, Bishwash Devkota <bishwashdevkota567@gmail.com>, Fredi Quispe Huaynasi <fredifqh@gmail.com>, Ian Rain-water <irainw@stanford.edu>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Repository, https://github.com/Siphlygon/SEAWRD
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
Requires-Dist: keras
|
|
10
|
+
Requires-Dist: tensorflow_docs
|
|
11
|
+
Requires-Dist: numpy
|
|
12
|
+
|
|
13
|
+
# SEAWARD
|
|
14
|
+
Surrogate Emulator for Aquatic World Radius Determination
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
seawrd
|
seawrd-0.1.0/setup.cfg
ADDED