Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Robustness tests and inference helpers #38

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
40d6d18
Analytic/Gaussian inference helper class
JelleAalbers Aug 24, 2022
4912f54
Demote global variables to class attributes
JelleAalbers Aug 24, 2022
74d65e4
Allow unnormalization of the precision matrix
JelleAalbers Aug 24, 2022
b441185
Neural network runner, fixes/renaming
JelleAalbers Aug 24, 2022
ba843db
Allow dataset generation with function calls
JelleAalbers Aug 24, 2022
6fd26ef
Robustness test helper script, minor fixes
JelleAalbers Aug 25, 2022
3045f51
Make scripts executable by default
Aug 25, 2022
ee4160d
Use safe config py names
JelleAalbers Aug 25, 2022
dae3278
Remove hdf5 mcmc backend
JelleAalbers Aug 25, 2022
990cf59
Fixes, even safer config name generation
JelleAalbers Aug 25, 2022
49c9231
Sampler: allow fixed value tuples
JelleAalbers Aug 25, 2022
3bed681
Use config_val as a reference instead
JelleAalbers Aug 25, 2022
bc54cb0
Specify reference config, infer all params
JelleAalbers Sep 9, 2022
1b8ca0b
Rotational averaging in robustness tests
JelleAalbers Sep 10, 2022
2c3599f
Workaround for scipy.stats seeding issue
JelleAalbers Sep 12, 2022
02a988c
Allow artificial scaling of source pixel images
JelleAalbers Sep 12, 2022
6b63688
Allow unnormalize call without mean correction
JelleAalbers Sep 12, 2022
9d5e283
Revert to network_outputs.npz
JelleAalbers Sep 12, 2022
a8849a4
Use 100 rots by default, not for center_x and center_y
JelleAalbers Sep 12, 2022
f8de180
Allow use of fewer images, small extensions
JelleAalbers Sep 15, 2022
d61eb7c
Allow varying no or multiple params, sane dataset names
JelleAalbers Sep 15, 2022
c43a4b7
Common command line interface maker
JelleAalbers Sep 15, 2022
3133c52
Flag arguments, ship norms.csv, cleanup option
JelleAalbers Sep 15, 2022
d65fa1f
Add end-to-end test in test suite
JelleAalbers Sep 15, 2022
7e67c9d
Add brightness scaling option
JelleAalbers Sep 15, 2022
67ac5e5
Fix generate tests, add test for CLI maker
JelleAalbers Sep 15, 2022
4b657ba
Fix shebang line typo, ignore auto-downloaded model
JelleAalbers Sep 15, 2022
81fb4af
Make k-corrections optional
JelleAalbers Sep 16, 2022
4a35df6
Fix import snafu
JelleAalbers Sep 16, 2022
74e1af2
Include csv files in package
JelleAalbers Sep 16, 2022
94bfd3d
Fix posterior plot test
JelleAalbers Sep 16, 2022
b5f66c7
Include .fits files (HST PSF) in package
JelleAalbers Sep 16, 2022
406a066
Use mini-COSMOS for end-to-end test
JelleAalbers Sep 16, 2022
e927254
Get config_dict without ConfigHandler, fix symlink
JelleAalbers Sep 17, 2022
b88b6d8
Forgot numdifftools requirement
JelleAalbers Sep 17, 2022
8f29e4c
numdifftools and emcee are test requirements now
JelleAalbers Sep 17, 2022
9db46e1
Add type annotations so generate+autocli works
JelleAalbers Nov 18, 2022
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ lib64

# COSMOS images
datasets/cosmos

# Network that may be downloaded during testing
test/xresnet34_full_final.h5
1 change: 1 addition & 0 deletions datasets/hst_psf
1 change: 1 addition & 0 deletions paltas/Analysis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
from . import hierarchical_inference
from . import pdf_functions
from . import transformer_models
from . import gaussian_inference
31 changes: 21 additions & 10 deletions paltas/Analysis/dataset_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import numpy as np
import tensorflow as tf
import pandas as pd
import glob, os
import os
from pathlib import Path
from tqdm import tqdm
from lenstronomy.SimulationAPI.observation_api import SingleBand
import warnings
Expand Down Expand Up @@ -79,8 +80,8 @@ def normalize_outputs(metadata,learning_params,input_norm_path,
return norm_dict


def unnormalize_outputs(input_norm_path,learning_params,mean,standard_dev=None,
cov_mat=None):
def unnormalize_outputs(input_norm_path,learning_params,mean=None,standard_dev=None,
cov_mat=None, prec_mat=None):
"""Given NN outputs, undo the normalization step and return the parameters
in the original space

Expand All @@ -95,7 +96,10 @@ def unnormalize_outputs(input_norm_path,learning_params,mean,standard_dev=None,
n_params) containing the standard deviation estimate for each
parameter.
cov_mat (np.array): A numpy array with dimensions (batch_size,n_params,
n_params) containing the covariance matrix estiamtes for each
n_params) containing the covariance matrix estimates for each
image.
prec_mat (np.array): A numpy array with dimensions (batch_size,n_params,
n_params) containing the precision matrix estimates for each
image.

Notes:
Expand All @@ -108,9 +112,10 @@ def unnormalize_outputs(input_norm_path,learning_params,mean,standard_dev=None,
param_mean = norm_dict['mean'][param]
param_std = norm_dict['std'][param]

# We always want to correct the mean
mean[:,lpi] *= param_std
mean[:,lpi] += param_mean
# If provided we want to correct the mean
if mean is not None:
mean[:,lpi] *= param_std
mean[:,lpi] += param_mean

# If provided we want to correct the standard deviation
if standard_dev is not None:
Expand All @@ -121,6 +126,11 @@ def unnormalize_outputs(input_norm_path,learning_params,mean,standard_dev=None,
cov_mat[:,lpi,:] *= param_std
cov_mat[:,:,lpi] *= param_std

# If provided we want to correct the precision matrix
if prec_mat is not None:
prec_mat[:,lpi,:] /= param_std
prec_mat[:,:,lpi] /= param_std


def kwargs_detector_to_tf_noise(image,kwargs_detector):
"""Add noise to the tf tensor provided in agreement with kwargs_detector
Expand Down Expand Up @@ -163,8 +173,9 @@ def generate_tf_record(npy_folder,learning_params,metadata_path,
tf_record_path (str): The path to which the TFRecord will be saved
"""
# Pull the list of numpy filepaths from the directory
npy_file_list = glob.glob(os.path.join(npy_folder,'image_*.npy'))
npy_file_list = list(sorted(npy_file_list))
# Path().glob > glob.glob(os.path.join) for dirs with funny chars
npy_file_list = Path(npy_folder).glob('image_*.npy')
npy_file_list = list(sorted([str(x) for x in npy_file_list]))
# Open label csv
metadata = pd.read_csv(metadata_path, index_col=None)

Expand Down Expand Up @@ -433,7 +444,7 @@ def rotate_image_batch(image_batch,learning_params,output,rot_angle):
(np.array): A numpy array containing the rotated images.

Notes:
output is changed in place.
output is changed in place -- image_batch is not!!
"""

# Rotate the image
Expand Down
Loading