Skip to content

Commit

Permalink
Updated formatting, removed ptype dependency, updated docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
djgagne committed Aug 26, 2024
1 parent 87b1a76 commit 57789c5
Show file tree
Hide file tree
Showing 28 changed files with 683 additions and 199 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/python-package-conda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ jobs:
- uses: mamba-org/setup-micromamba@v1
with:
environment-file: environment_torch.yml
activate-environment: test
- shell: bash -l {0}
run: |
conda info
Expand All @@ -28,12 +27,14 @@ jobs:
run: |
micromamba install ruff
# stop the build if there are Python syntax errors or undefined names
ruff . --count --select=E9,F63,F7,F82 --show-source --statistics
ruff check --select=E9,F63,F7,F82
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
ruff . --count --exit-zero --max-complexity=100 --max-line-length=127 --statistics
ruff check --output-format concise
# Checking documentation errors
ruff . --count --select=D --exit-zero --max-complexity=100 --max-line-length=127 --statistics
ruff check --select=D
ruff check --select=D --statistics
- name: Test with pytest
shell: bash -l {0}
run: |
export KERAS_BACKEND="torch"
pytest
4 changes: 2 additions & 2 deletions applications/evaluate_ptype.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import pandas as pd
from sklearn.metrics import precision_recall_fscore_support

from ptype.reliability import (
from mlguess.reliability import (
compute_calibration,
reliability_diagram,
reliability_diagrams,
)
from ptype.plotting import (
from mlguess.plotting import (
plot_confusion_matrix,
coverage_figures,
)
Expand Down
1 change: 1 addition & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
extensions = ['myst_parser',
'sphinx.ext.napoleon',
'sphinx.ext.intersphinx',
'sphinx.ext.mathjax',
'nbsphinx']

templates_path = ['_templates']
Expand Down
2 changes: 2 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Welcome to miles-guess's documentation!
:caption: Contents:

install.rst
keras.md
torch.md
mlguess.rst
notebooks/classifier_example.ipynb
notebooks/regression_example.ipynb
Expand Down
9 changes: 7 additions & 2 deletions docs/source/keras.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Keras in MILES-GUESS

Welcome to the Keras users page. The instructions below outline how to compute various UQ quantities like aleatoric and epistemic using different modeling approaches.

## Regression Models

### 1a. Train/evaluate a deterministic multi-layer perceptrion (MLP) on the SL dataset:
### 1a. Train/evaluate a deterministic multi-layer perceptron (MLP) on the SL dataset:
```bash
python3 applications/train_mlp_SL.py -c config/model_mlp_SL.yml
```
Expand All @@ -16,6 +19,8 @@ python applications/train_gaussian_SL.py -c config/model_gaussian_SL.yml
python applications/train_evidential_SL.py -c config/model_evidential_SL.yml
```

## Classification Models

### 2a. Train a categorical MLP classifier on the p-type dataset:
```bash
python applications/train_classifier_ptype.py -c config/model_classifier_ptype.yml
Expand Down Expand Up @@ -213,4 +218,4 @@ Depending on the problem, a data field is customized and also present in the con
## ECHO hyperparameter optimization
Configuration files are also supplied for use with the Earth Computing Hyperparameter Optimization (ECHO) package. See the echo package https://github.com/NCAR/echo-opt/tree/main/echo for more details on the configuration fields.
Configuration files are also supplied for use with the Earth Computing Hyperparameter Optimization (ECHO) package. See the echo package https://github.com/NCAR/echo-opt/tree/main/echo for more details on the configuration fields.
4 changes: 2 additions & 2 deletions docs/source/mlguess.keras.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ mlguess.keras.models module
:undoc-members:
:show-inheritance:

mlguess.keras.models\_deprecated module
mlguess.keras.deprecated module
---------------------------------------

.. automodule:: mlguess.keras.models_deprecated
.. automodule:: mlguess.keras.deprecated
:members:
:undoc-members:
:show-inheritance:
Expand Down
4 changes: 3 additions & 1 deletion docs/source/torch.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Pytorch in MILES-GUESS

Welcome to the pyTorch users page. The instructions below outline how to compute various UQ quantities like aleatoric and epistemic using different modeling approaches. Email [email protected] for questions/concerns/fixes/etc

## Regression usage
Expand Down Expand Up @@ -158,4 +160,4 @@ scheduler:

### Classifier Models

* `output_activation`: Set to `softmax` for standard classification. If not set, the model will use evidential classification.
* `output_activation`: Set to `softmax` for standard classification. If not set, the model will use evidential classification.
6 changes: 3 additions & 3 deletions environment_gpu.yml → environment_gpu_tf.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: guess
name: guesstf
channels:
- conda-forge
- nvidia
dependencies:
- python=3.10
- numpy<1.24
- python=3.11
- numpy<2
- scipy
- matplotlib
- xarray
Expand Down
36 changes: 36 additions & 0 deletions environment_gpu_torch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: guesstorch
channels:
- conda-forge
- pytorch
dependencies:
- python=3.11
- numpy<2
- scipy
- matplotlib
- xarray
- metpy
- netcdf4
- pandas
- pyyaml
- pytest
- pip
- pyproj
- jupyter
- jupyterlab
- cartopy
- tqdm
- seaborn
- sphinx
- numba
- properscoring
- pyarrow
- imbalanced-learn
- pytorch
- pytorch-cuda=12.1
- pip:
- keras
- echo-opt
- hagelslag
- bridgescaler
- safetensors
- -e .
5 changes: 2 additions & 3 deletions environment_tensorflow.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: guess
name: guesstf
channels:
- conda-forge
dependencies:
- python=3.10
- numpy<1.24
- numpy<2
- scipy
- matplotlib
- xarray
Expand Down Expand Up @@ -31,5 +31,4 @@ dependencies:
- echo-opt
- hagelslag
- bridgescaler
- -e git+https://github.com/ai2es/ptype-physical.git#egg=ptype
- -e .
2 changes: 1 addition & 1 deletion environment_torch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ dependencies:
- echo-opt
- hagelslag
- bridgescaler
- -e git+https://github.com/ai2es/ptype-physical.git#egg=ptype
- safetensors
- -e .
2 changes: 1 addition & 1 deletion mlguess/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024.1.0b2
2024.2.0
48 changes: 30 additions & 18 deletions mlguess/keras/deprecated/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ def __init__(
self.history = None

def build_neural_network(self, inputs, outputs, last_layer="Dense"):
"""Create Keras neural network model and compile it.
"""
Create Keras neural network model and compile it.
Args:
inputs (int): Number of input predictor variables.
Expand All @@ -108,7 +109,6 @@ def build_neural_network(self, inputs, outputs, last_layer="Dense"):

if self.activation == "leaky":
self.activation = LeakyReLU()

if self.kernel_reg == "l1":
self.kernel_reg = L1(self.l1_weight)
elif self.kernel_reg == "l2":
Expand Down Expand Up @@ -522,7 +522,8 @@ def __init__(


class GaussianRegressorDNN(BaseRegressor):
"""A Dense Neural Network Model that can support arbitrary numbers of hidden layers
"""
A Dense Neural Network Model that can support arbitrary numbers of hidden layers
and provides evidential uncertainty estimation.
Inherits from BaseRegressor.
Expand All @@ -542,6 +543,7 @@ class GaussianRegressorDNN(BaseRegressor):
model: Keras Model object.
evidential_coef: Evidential regularization coefficient.
metrics: Optional list of metrics to monitor during training.
"""

def __init__(
Expand Down Expand Up @@ -571,11 +573,13 @@ def __init__(
metrics=None,
eps=1e-7
):
"""Initialize the EvidentialRegressorDNN.
"""
Initialize the EvidentialRegressorDNN.
Args:
coupling_coef: Coupling coeffient for loss fix
evidential_coef: Evidential regularization coefficient.
"""
super().__init__( # Call the constructor of the base class
hidden_layers,
Expand Down Expand Up @@ -606,7 +610,8 @@ def __init__(
self.loss = gaussian_nll

def build_neural_network(self, inputs, outputs, last_layer="DenseNormal"):
"""Create Keras neural network model and compile it.
"""
Create Keras neural network model and compile it.
Args:
inputs (int): Number of input predictor variables.
Expand Down Expand Up @@ -702,7 +707,8 @@ def predict_monte_carlo(self, x_test, forward_passes, scaler=None, batch_size=No


class EvidentialRegressorDNN(BaseRegressor):
"""A Dense Neural Network Model that can support arbitrary numbers of hidden layers
"""
A Dense Neural Network Model that can support arbitrary numbers of hidden layers
and provides evidential uncertainty estimation.
Inherits from BaseRegressor.
Expand All @@ -722,6 +728,7 @@ class EvidentialRegressorDNN(BaseRegressor):
model: Keras Model object.
evidential_coef: Evidential regularization coefficient.
metrics: Optional list of metrics to monitor during training.
"""
def __init__(
self,
Expand Down Expand Up @@ -803,7 +810,8 @@ def __init__(
logging.info(f"Using loss: {loss}")

def build_neural_network(self, inputs, outputs):
"""Create Keras neural network model and compile it.
"""
Create Keras neural network model and compile it.
Args:
inputs (int): Number of input predictor variables.
Expand Down Expand Up @@ -905,19 +913,17 @@ def predict_dist_params(self, x, y_scaler=None, batch_size=None):

return mu, v, alpha, beta

def predict_ensemble(
self, x_test, scaler=None, batch_size=None
):
def predict_ensemble(self, x_test, scaler=None, batch_size=None):
return super().predict_ensemble(x_test, scaler=scaler, batch_size=batch_size, num_outputs=3)

def predict_monte_carlo(
self, x_test, forward_passes, scaler=None, batch_size=None
):
return super().predict_monte_carlo(x_test, forward_passes, scaler=scaler, batch_size=batch_size, num_outputs=3)
def predict_monte_carlo(self, x_test, forward_passes, scaler=None, batch_size=None):
return super().predict_monte_carlo(x_test, forward_passes,
scaler=scaler, batch_size=batch_size, num_outputs=3)


class CategoricalDNN(object):
"""A Dense Neural Network Model that can support arbitrary numbers of hidden layers.
"""
A Dense Neural Network Model that can support arbitrary numbers of hidden layers.
Attributes:
hidden_layers: Number of hidden layers
Expand All @@ -941,6 +947,7 @@ class CategoricalDNN(object):
decay: Level of decay to apply to learning rate
verbose: Level of detail to provide during training (0 = None, 1 = Minimal, 2 = All)
classifier: (boolean) If training on classes
"""
def __init__(
self,
Expand Down Expand Up @@ -969,7 +976,7 @@ def __init__(
decay=0,
verbose=0,
random_state=1000,
callbacks=[],
callbacks=None,
balanced_classes=0,
steps_per_epoch=0,
):
Expand Down Expand Up @@ -997,6 +1004,8 @@ def __init__(
self.use_dropout = use_dropout
self.dropout_alpha = dropout_alpha
self.epochs = epochs
if callbacks is None:
self.callbacks = []
self.callbacks = callbacks
self.decay = decay
self.verbose = verbose
Expand All @@ -1007,7 +1016,8 @@ def __init__(
self.steps_per_epoch = steps_per_epoch

def build_neural_network(self, inputs, outputs):
"""Create Keras neural network model and compile it.
"""
Create Keras neural network model and compile it.
Args:
inputs (int): Number of input predictor variables
Expand Down Expand Up @@ -1066,13 +1076,15 @@ def build_neural_network(self, inputs, outputs):
self.model.compile(optimizer=self.optimizer_obj, loss=self.loss)

def build_from_sequential(self, model, optimizer="adam", loss="mse", metrics=None):
"""Build the neural network model using a Keras Sequential model.
"""
Build the neural network model using a Keras Sequential model.
Args:
model (tf.keras.Sequential): Keras Sequential model to use.
optimizer (str or tf.keras.optimizers.Optimizer): Optimizer for the model.
loss (str or tf.keras.losses.Loss): Loss function for the model.
metrics (list of str or tf.keras.metrics.Metric): Metrics for the model.
"""
self.model = model

Expand Down
2 changes: 0 additions & 2 deletions mlguess/keras/layers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import keras
import keras.layers as layers
import keras.ops as ops
from keras.src import activations
from keras.src.layers.layer import Layer


@keras.saving.register_keras_serializable()
Expand Down
Loading

0 comments on commit 57789c5

Please sign in to comment.