Skip to content

Commit

Permalink
Merge pull request #1 from aymgal/master
Browse files Browse the repository at this point in the history
Fix bug of NaN in lnprob
  • Loading branch information
martin-millon committed Jan 31, 2019
2 parents 9e47070 + 8742db0 commit 7ea722c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
50 changes: 50 additions & 0 deletions lenstronomy/LightModel/Profiles/fromfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
__author__ = 'aymgal'

import numpy as np
import astropy.io.fits as pyfits

import lenstronomy.Util.util as util



class FromFile(object):
"""
light profile loaded from a fits file
TODO : implement 'scale' and 'center_x' / 'center_y' parameters,
in order to shift and/or zoom on the image file
"""
param_names = []
lower_limit_default = {}
upper_limit_default = {}


def __init__(self, file_path, data_class):
image = pyfits.open(file_path)[0].data
self.coords = data_class._coords
nx, ny = data_class.nx, data_class.ny
if image.shape != (nx, ny):
# need to resize the image
try:
import skimage
except ImportError:
self.image = image[:nx, :ny]
else:
# cleaner way to resize the image
self.image = skimage.transform.resize(image, (nx, ny))
else:
self.image = image


def function(self, x, y, center_x=0, center_y=0):
"""
:param x:
:param y:
:return:
"""
x_shift = x - center_x
y_shift = y - center_y
i_, j_ = self.coords.map_coord2pix(y_shift, x_shift)
i, j = i_.astype(int), j_.astype(int)
return self.image[i, j]
8 changes: 7 additions & 1 deletion lenstronomy/LightModel/light_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class LightModel(object):
"""
class to handle source and lens light models
"""
def __init__(self, light_model_list, smoothing=0.0000001):
def __init__(self, light_model_list, smoothing=0.0000001,
file_path=None, data_class=None):
self.profile_type_list = light_model_list
self.func_list = []
for profile_type in light_model_list:
Expand Down Expand Up @@ -65,6 +66,11 @@ def __init__(self, light_model_list, smoothing=0.0000001):
elif profile_type == 'DOUBLE_CHAMELEON':
from lenstronomy.LightModel.Profiles.chameleon import DoubleChameleon
self.func_list.append(DoubleChameleon())
elif profile_type == 'FROM_FILE':
if file_path is None or data_class is None:
raise ValueError('Warning! You must provide the path to the FITS file and pixel size for profile of type', profile_type)
from lenstronomy.LightModel.Profiles.fromfile import FromFile
self.func_list.append(FromFile(file_path, data_class))
else:
raise ValueError('Warning! No light model of type', profile_type, ' found!')

Expand Down
3 changes: 3 additions & 0 deletions lenstronomy/Sampling/likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ def logL(self, args):
if np.min(flux) < 0:
logL -= 10**10
self.imSim.reset_point_source_cache(bool=False)
if np.isnan(logL):
print("WARNING : logL returns NaN, changed to penalty")
logL = -10**15
return logL, None

def solver_penalty(self, kwargs_lens, kwargs_ps, kwargs_cosmo, tolerance):
Expand Down

0 comments on commit 7ea722c

Please sign in to comment.