Skip to content

Commit

Permalink
Merge pull request #197 from deepskies/feature/small_tweaks_mostly_co…
Browse files Browse the repository at this point in the history
…smetic

Feature/small tweaks mostly cosmetic
  • Loading branch information
beckynevin authored Nov 1, 2024
2 parents c8e899f + 7ba635a commit e38f178
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 69 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,3 @@ jobs:
env:
PYTHONPATH: ${{ env.PYTHONPATH }}
ENV_FILE: ${{ runner.workspace }}/.env

- name: Upload coverage to Codecov
run: bash <(curl -s https://codecov.io/bash)

6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [0.1.5] - 2024-11-01
### Fixed
- verbosity in data.py and analyze.py
- can load rs DE model in analyze.py
- torch.Tensor error addressed in data.py

## [0.1.4] - 2024-10-28
### Added
- test_data
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# DeepUQ
DeepUQ is a package for injecting and measuring different types of uncertainty in ML models.

[![PyPi](https://img.shields.io/badge/PyPi-0.1.4-blue)](https://pypi.org/project/deepuq/)
[![PyPi](https://img.shields.io/badge/PyPi-0.1.5-blue)](https://pypi.org/project/deepuq/)
[![License](https://img.shields.io/badge/License-MIT-lightgrey)](https://opensource.org/licenses/MIT)
[![Downloads](https://static.pepy.tech/personalized-badge/deepuq?period=month&units=international_system&left_color=black&right_color=brightgreen&left_text=Total%20Downloads)](https://pepy.tech/project/deepuq)

Expand Down
61 changes: 15 additions & 46 deletions deepuq/analyze/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ def load_checkpoint(
path="DeepUQResources/checkpoints/",
BETA=0.5,
nmodel=1,
COEFF=0.5,
loss="SDER",
COEFF=0.01,
loss="DER",
load_rs_chk=False,
rs=42,
load_nh_chk=False,
nh=64,
verbose=False
):
"""Loads a PyTorch model checkpoint from a .pt file, constructing the
file name based on model type (DE or DER) and other configuration
Expand All @@ -60,8 +61,8 @@ def load_checkpoint(
path (str): Directory path to the model checkpoints
(default: "models/").
BETA (float): Beta value for DE models (default: 0.5).
nmodel (int): Number of models in the ensemble (default: 1).
COEFF (float): Coefficient for the DER model (default: 0.5).
nmodel (int): Number of the model to select in the DE (default: 1).
COEFF (float): Coefficient for the DER model (default: 0.01).
loss (str): Type of loss used for DER models (e.g., 'SDER', 'DER').
load_rs_chk (bool): Flag to indicate if random seed checkpoint is
used (default: False).
Expand All @@ -70,6 +71,8 @@ def load_checkpoint(
configuration is used (default: False).
nh (int): Number of hidden units if load_nh_chk is True
(default: 64).
verbose (bool): Whether to print out the checkpoint
(default: False).
Returns:
dict: The loaded checkpoint containing model weights and
Expand All @@ -82,53 +85,19 @@ def load_checkpoint(
+ f"{model_name}_{inject_type}_{data_dim}"
+ f"_noise_{noise}_loss_{loss}_COEFF_{COEFF}_epoch_{epoch}"
)
if load_rs_chk:
file_name += f"_rs_{rs}"
if load_nh_chk:
file_name += f"_n_hidden_{nh}"
file_name += ".pt"
elif model_name[0:2] == "DE":
file_name = (
str(path)
+ f"{model_name}_{inject_type}_{data_dim}"
f"_noise_{noise}_beta_{BETA}_nmodel_{nmodel}_epoch_{epoch}.pt"
f"_noise_{noise}_beta_{BETA}_nmodel_{nmodel}_epoch_{epoch}"
)
print("loading this chk", file_name)
if load_rs_chk:
file_name += f"_rs_{rs}"
if load_nh_chk:
file_name += f"_n_hidden_{nh}"
file_name += ".pt"
if verbose:
print("loading this chk", file_name)
checkpoint = torch.load(file_name, map_location=device,
weights_only=False)
return checkpoint

def ep_al_checkpoint_DE(self, checkpoint):
"""Extracts mean and variance validation metrics from a loaded DE model
checkpoint.
Parameters:
checkpoint (dict): The loaded DE model checkpoint.
Returns:
tuple: A tuple containing mean validation and variance validation
metrics.
"""
# Extract additional information
# loaded_epoch = checkpoint.get("epoch", None)
mean_validation = checkpoint.get("valid_mean", None).detach().numpy()
var_validation = checkpoint.get("valid_var", None).detach().numpy()
return mean_validation, var_validation

def ep_al_checkpoint_DER(self, checkpoint):
"""Extracts mean and variance validation metrics from a loaded DER
model checkpoint.
Parameters:
checkpoint (dict): The loaded DER model checkpoint.
Returns:
tuple: A tuple containing mean validation and variance validation
metrics.
"""
mean_u_ep = checkpoint.get("mean_u_ep_validation", None)
mean_u_al = checkpoint.get("mean_u_al_validation", None)
std_u_ep = checkpoint.get("std_u_ep_validation", None)
std_u_al = checkpoint.get("std_u_al_validation", None)

return mean_u_ep, mean_u_al, std_u_ep, std_u_al
41 changes: 23 additions & 18 deletions deepuq/data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ def load_data_h5(self, data_name, path="../data/"):
loaded_data = {}
with h5py.File(file_name, "r") as file:
for key in file.keys():
loaded_data[key] = torch.Tensor(file[key][...])
loaded_data[key] = torch.tensor(file[key][...],
dtype=torch.float32)
return loaded_data


Expand Down Expand Up @@ -422,6 +423,7 @@ def simulate_data_2d(
image_size=32,
inject_type="output",
rs_simulate_2D=40,
verbose=False
):
"""Simulates 2D image data based on provided parameters and noise
levels.
Expand All @@ -447,6 +449,7 @@ def simulate_data_2d(
- "input": Noise is added to the image pixels directly.
Default is "output".
rs (int): Random seed for reproducibility. Default is 40.
verbose (bool): Display printouts? Default False.
Returns:
tuple:
Expand Down Expand Up @@ -506,10 +509,10 @@ def simulate_data_2d(
# total_brightness_prop_noisy.append(np.sum(noisy_image))
self.input = image_array
self.output = total_brightness
print(
f"2D data generated, \
with noise injected type: {inject_type}."
)
if verbose:
print(
f"2D data generated, with noise injected type: {inject_type}."
)
return image_array, total_brightness

def simulate_data(
Expand Down Expand Up @@ -565,7 +568,8 @@ def simulate_data(
# convert to numpy array (if tensor):
thetas = np.atleast_2d(thetas)
n_sim = thetas.shape[0]
print("number of sims", n_sim)
if verbose:
print("number of sims", n_sim)
# Check if the input has the correct shape
if thetas.shape[1] != 2:
raise ValueError(
Expand All @@ -587,12 +591,14 @@ def simulate_data(
# Generate random noise (epsilon) based
# on a normal distribution with mean 0 and standard deviation sigma
if vary_sigma:
print("YES WERE VARYING SIGMA")
if verbose:
print("YES WERE VARYING SIGMA")
new_sig = self.get_sigma_m(sigma, m)
ε = rs.normal(loc=0, scale=new_sig, size=(len(x), n_sim))
scale = new_sig
else:
print("NO WERE NOT VARYING SIGMA")
if verbose:
print("NO WERE NOT VARYING SIGMA")
ε = rs.normal(loc=0, scale=sigma, size=(len(x), n_sim))
scale = sigma
if verbose:
Expand Down Expand Up @@ -621,18 +627,17 @@ def simulate_data(
x_noisy[:, i] = x + ε[:, i]

if inject_type == "output":
# self.input = x
self.input = torch.Tensor(np.tile(x, thetas.shape[0]).T)
self.output = torch.Tensor(y_noisy.T)
# self.output_err = ε[:, i].T
self.input = torch.tensor(np.tile(x, thetas.shape[0]).T,
dtype=torch.float32)
self.output = torch.tensor(y_noisy.T, dtype=torch.float32)
elif inject_type == "input":
self.input = torch.Tensor(x_noisy.T)
self.output = torch.Tensor(y.T)
self.input = torch.tensor(x_noisy.T, dtype=torch.float32)
self.output = torch.tensor(y.T, dtype=torch.float32)
# self.output_err = ε[:, i].T
print(
f"0D data generated, \
with noise injected type: {inject_type}."
)
if verbose:
print(
f"0D data generated, with noise injected type: {inject_type}."
)
return

def sample_params_from_prior(
Expand Down

0 comments on commit e38f178

Please sign in to comment.