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

Functionality to check if user specifies valid profile parameters #210

Open
rmorgan10 opened this issue Sep 21, 2020 · 2 comments
Open

Functionality to check if user specifies valid profile parameters #210

rmorgan10 opened this issue Sep 21, 2020 · 2 comments

Comments

@rmorgan10
Copy link
Contributor

rmorgan10 commented Sep 21, 2020

I developed this functionality for deeplenstronomy, and I'm posting here in case it might be useful.

from inspect import getfullargspec

import lenstronomy

class ProfileArgumentError(Exception): pass
class ProfileNameError(Exception): pass

# This method requires the lenstronomy module for a given model to be directly imported
class ProfileChecker(self):
    def __init__(self):
        self.light_profiles = {'GAUSSIAN': ".gaussian.Gaussian",
              'GAUSSIAN_ELLIPSE': ".gaussian.GaussianEllipse",
              'ELLIPSOID': ".ellipsoid.Ellipsoid",
              'MULTI_GAUSSIAN': ".gaussian.MultiGaussian",
              'MULTI_GAUSSIAN_ELLIPSE': ".gaussian.MultiGaussianEllipse",
              'SERSIC': ".sersic.Sersic",
              'SERSIC_ELLIPSE': ".sersic.SersicElliptic",
              'CORE_SERSIC': ".sersic.CoreSersic",
              'SHAPELETS': ".shapelets.Shapelets",
              'SHAPELETS_POLAR': ".shapelets_polar.ShapeletsPolar",
              'SHAPELETS_POLAR_EXP': ".shapelets_polar.ShapeletsPolarExp",
              'HERNQUIST': ".hernquist.Hernquist",
              'HERNQUIST_ELLIPSE': ".hernquist.HernquistEllipse",
              'PJAFFE': ".p_jaffe.PJaffe",
              'PJAFFE_ELLIPSE': ".p_jaffe.PJaffe_Ellipse",
              'UNIFORM': ".uniform.Uniform",
              'POWER_LAW': ".power_law.PowerLaw",
              'NIE': ".nie.NIE",
              'CHAMELEON': ".chameleon.Chameleon",
              'DOUBLE_CHAMELEON': ".chameleon.DoubleChameleon",
              'TRIPLE_CHAMELEON': ".chameleon.TripleChameleon",
              'INTERPOL': ".interpolation.Interpol",
              'SLIT_STARLETS': ".starlets.SLIT_Starlets",
              'SLIT_STARLETS_GEN2': ".starlets.SLIT_Starlets"}

        self.lens_profiles = {"SHIFT": ".alpha_shift.Shift",
              "NIE_POTENTIAL": ".nie_potential.NIE_POTENTIAL",
              "CONST_MAG": ".const_mag.ConstMag",
              "SHEAR": ".shear.Shear",
              "SHEAR_GAMMA_PSI": ".shear.ShearGammaPsi",
              "CONVERGENCE": ".convergence.Convergence",
              "FLEXION": ".flexion.Flexion",
              "FLEXIONFG": ".flexionfg.Flexionfg",
              "POINT_MASS": ".point_mass.PointMass",
              "SIS": ".sis.SIS",
              "SIS_TRUNCATED": ".sis_truncate.SIS_truncate",
              "SIE": ".sie.SIE",
              "SPP": ".spp.SPP",
              "NIE": ".nie.NIE",
              #"NIE_SIMPLE": "warn",
              "CHAMELEON": ".chameleon.Chameleon",
              "DOUBLE_CHAMELEON": ".chameleon.DoubleChameleon",
              "TRIPLE_CHAMELEON": ".chameleon.TripleChameleon",
              "SPEP": ".spep.SPEP",
              "PEMD": ".pemd.PEMD",
              "SPEMD": "spemd.SPEMD",
              "EPL": "epl.EPL",
              "NFW": ".nfw.NFW",
              "NFW_ELLIPSE": ".nfw_ellipse.NFW_ELLIPSE",
              #"NFW_ELLIPSE_GAUSS_DEC": "warn",
              "TNFW": ".tnfw.TNFW",
              "CNFW": ".cnfw.CNFW",
              "CNFW_ELLIPSE": ".cnfw_ellipse.CNFW_ELLIPSE",
              #"CTNFW_GAUSS_DEC": "warn",
              "NFW_MC": ".nfw_mass_concentration.NFWMC",
              "SERSIC": ".sersic.Sersic",
              "SERSIC_ELLIPSE_POTENTIAL": ".sersic_ellipse_potential.SersicEllipse",
              "SERSIC_ELLIPSE_KAPPA": ".sersic_ellipse_kappa.SersicEllipseKappa",
              #"SERSIC_ELLIPSE_GAUSS_DEC": "warn",
              "PJAFFE": ".p_jaffe.PJaffe",
              "PJAFFE_ELLIPSE": ".p_jaffe_ellipse.PJaffe_Ellipse",
              "HERNQUIST": ".hernquist.Hernquist",
              "HERNQUIST_ELLIPSE": ".hernquist_ellipse.Hernquist_Ellipse",
              "GAUSSIAN": ".gaussian_potential.Gaussian",
              "GAUSSIAN_KAPPA": ".gaussian_kappa.GaussianKappa",
              "GAUSSIAN_ELLIPSE_KAPPA": ".gaussian_ellipse_kappa.GaussianEllipseKappa",
              "GAUSSIAN_ELLIPSE_POTENTIAL": ".gaussian_ellipse_potential.GaussianEllipsePotential",
              "MULTI_GAUSSIAN_KAPPA": ".multi_gaussian_kappa.MultiGaussianKappa",
              "MULTI_GAUSSIAN_KAPPA_ELLIPSE": ".multi_gaussian_kappa.MultiGaussianKappaEllipse",
              "INTERPOL": ".interpol.Interpol",
              "INTERPOL_SCALED": ".interpol.InterpolScaled",
              "SHAPELETS_POLAR": ".shapelet_pot_polar.PolarShapelets",
              "SHAPELETS_CART": ".shapelet_pot_cartesian.CartShapelets",
              "DIPOLE": ".dipole.Dipole",
              "CURVED_ARC": ".curved_arc.CurvedArc",
              "ARC_PERT": ".arc_perturbations.ArcPerturbations",
              "coreBURKERT": ".coreBurkert.CoreBurkert",
              "CORED_DENSITY": ".cored_density.CoredDensity",
              "CORED_DENSITY_2": ".cored_density_2.CoredDensity2",
              "CORED_DENSITY_MST": ".cored_density_mst.CoredDensityMST",
              #"CORED_DENSITY_2_MST": "warn",
              "NumericalAlpha": ".numerical_deflections.NumericalAlpha",
              "MULTIPOLE": ".multipole.Multipole",
              "HESSIAN": ".hessian.Hessian"}

        return

    def check_parameters(self, name, params, profile_type='lens'):
        """
        Determine if a passed parameter is valid for a particular model

        :param name: str, lenstronomy profile 'NAME'
        :param params: dict, keys are parameters for the lenstronomy profile,
                                      values are the passed value for that parameter
        :param profile_type: str, choose from ['lens', 'light']
        """
        assert profile_type in ['lens', 'light'], "passed profile_type must be 'lens' or 'light'"
    
        #import the needed profile module
        try:
            if profile_type == 'lens':
                lenstronomy_path = "lenstronomy.LensModel.Profiles"
            else:
                lenstronomy_path = "lenstronomy.LightModel.Profiles"
            exec("import " + lenstronomy_path + self.lens_profiles[name])
        except KeyError:
            raise ProfileNameError
        
        #check the parameters of the model
        allowed_arguments = getfullargspec(eval(lenstronomy_path + self.lens_profiles[name] + ".function"))[0]
        for param in params.keys():
            if param not in allowed_arguments:
                raise ProfileArgumentError

        return
@rmorgan10
Copy link
Contributor Author

There are a couple lens models marked as "warn", for which I was unable to track down the path to the source code. Please let me know if you know the paths.

@sibirrer
Copy link
Collaborator

For all imports of the classes, you can have a look here: https://github.com/sibirrer/lenstronomy/blob/master/lenstronomy/LensModel/profile_list_base.py#L55

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants