-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #273 from nkhadka21/false_positives
False positive routine
- Loading branch information
Showing
22 changed files
with
623 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import numpy as np | ||
from slsim.lens import Lens | ||
|
||
class FalsePositive(Lens): | ||
"""Class to manage individual false positive. Here, false positives refer to a | ||
configuration that includes an elliptical galaxy at the center with blue galaxies | ||
surrounding the central elliptical galaxy.""" | ||
|
||
def __init__( | ||
self, | ||
source_class, | ||
deflector_class, | ||
cosmo, | ||
test_area=4 * np.pi, | ||
los_config=None, | ||
los_dict=None, | ||
): | ||
""" | ||
:param source_class: A Source class instance or list of Source class instance | ||
:type source_class: Source class instance from slsim.Sources.source | ||
:param deflector_class: deflector instance | ||
:type deflector_class: Deflector class instance from slsim.Deflectors.deflector | ||
:param cosmo: astropy.cosmology instance | ||
:param test_area: area of disk around one lensing galaxies to be investigated | ||
on (in arc-seconds^2). | ||
:param los_config: LOSConfig instance which manages line-of-sight (LOS) effects | ||
and Gaussian mixture models in a simulation or analysis context. | ||
:param los_dict: line of sight dictionary (optional, takes these values instead | ||
of drawing from distribution) Takes "gamma" = [gamma1, gamma2] and | ||
"kappa" = kappa as entries | ||
:type los_dict: dict | ||
""" | ||
Lens.__init__(self, | ||
source_class=source_class, | ||
deflector_class=deflector_class, | ||
cosmo=cosmo, | ||
test_area=test_area, | ||
los_config=los_config, | ||
los_dict=los_dict, | ||
) | ||
|
||
def lenstronomy_kwargs(self, band=None): | ||
"""Generates lenstronomy dictionary conventions for the class object. | ||
:param band: imaging band, if =None, will result in un-normalized amplitudes | ||
:type band: string or None | ||
:return: lenstronomy model and parameter conventions | ||
""" | ||
lens_mass_model_list, kwargs_lens = self.deflector_mass_model_lenstronomy() | ||
( | ||
lens_light_model_list, | ||
kwargs_lens_light, | ||
) = self.deflector.light_model_lenstronomy(band=band) | ||
|
||
sources, sources_kwargs = self.source_light_model_lenstronomy(band=band) | ||
combined_lens_light_model_list = ( | ||
lens_light_model_list + sources["source_light_model_list"] | ||
) | ||
combined_kwargs_lens_light = kwargs_lens_light + sources_kwargs["kwargs_source"] | ||
|
||
kwargs_model = { | ||
"lens_light_model_list": combined_lens_light_model_list, | ||
"lens_model_list": lens_mass_model_list, | ||
} | ||
|
||
kwargs_source = None | ||
kwargs_ps = sources_kwargs["kwargs_ps"] | ||
|
||
kwargs_params = { | ||
"kwargs_lens": kwargs_lens, | ||
"kwargs_source": kwargs_source, | ||
"kwargs_lens_light": combined_kwargs_lens_light, | ||
"kwargs_ps": kwargs_ps, | ||
} | ||
|
||
return kwargs_model, kwargs_params |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
from slsim.FalsePositives.false_positive import FalsePositive | ||
from slsim.ParamDistributions.los_config import LOSConfig | ||
from slsim.lens_pop import draw_test_area | ||
import random | ||
|
||
|
||
class FalsePositivePop(object): | ||
"""Class to perform samples of false positive population. Here, false | ||
positives refer to a configuration that includes an elliptical galaxy at the center | ||
with blue galaxies surrounding the central elliptical galaxy. This class generates | ||
specified number of false positives.""" | ||
|
||
def __init__( | ||
self, | ||
elliptical_galaxy_population, | ||
blue_galaxy_population, | ||
cosmo=None, | ||
los_config=None, | ||
source_number_choice=[1, 2, 3], | ||
weights_for_source_number=None, | ||
test_area_factor=1 | ||
): | ||
""" | ||
Args: | ||
:param elliptical_galaxy_population: Deflector population as an deflectors class | ||
instance. | ||
:param blue_galaxy_population: Source population as an sources class inatnce. | ||
:param cosmo: astropy.cosmology instance | ||
:param los_config: LOSConfig instance which manages line-of-sight (LOS) effects | ||
and Gaussian mixture models in a simulation or analysis context. | ||
:param source_number_choice: A list of integers to choose source number from. If | ||
None, defaults to [1, 2, 3]. | ||
:param weights: A list of weights corresponding to the probabilities of | ||
selecting each value in source_number_choice. If None, all choices are equally | ||
likely. Defaults to None. | ||
:param test_area_factor: A multiplicative factor of a test_area. A test area is | ||
computed using a velocity dispersion of a central galaxy and that area is | ||
multiplied by this factor. A default value is 1. | ||
""" | ||
|
||
self.cosmo = cosmo | ||
self._lens_galaxies = elliptical_galaxy_population | ||
self._sources = blue_galaxy_population | ||
self._choice = source_number_choice | ||
self._weights = weights_for_source_number | ||
self._test_area_factor = test_area_factor | ||
self.los_config = los_config | ||
if self.los_config is None: | ||
self.los_config = LOSConfig() | ||
|
||
def draw_deflector(self): | ||
"""Draw and prepare a deflector (lens) with tolerance-based z_max. | ||
:return: a deflector instance and deflector redshift with tolerance added. | ||
""" | ||
deflector = self._lens_galaxies.draw_deflector() | ||
z_max = deflector.redshift + 0.002 # Adding tolerance to redshift | ||
return deflector, z_max | ||
|
||
def draw_sources(self, z_max): | ||
"""Draw source(s) within the redshift limit of z_max. | ||
:param z_max: maximum redshift for drawn source. | ||
:return: A Source instance or a list of Source instance. | ||
""" | ||
source_number = random.choices(self._choice, weights=self._weights)[0] | ||
source_list = [] | ||
|
||
for _ in range(source_number): | ||
source = self._sources.draw_source(z_max=z_max) | ||
# If no source is available, return None | ||
if source is None: | ||
return None | ||
source_list.append(source) | ||
if source_number==1: | ||
sources = source_list[0] | ||
else: | ||
sources = source_list | ||
return sources | ||
|
||
def draw_false_positive(self, number=1): | ||
|
||
"""Draw given number of false positives within the cuts of the lens and source. | ||
:param number: number of false positive requested. The default value is 1. | ||
:return: list of FalsePositive() instance. | ||
""" | ||
false_positive_population = [] | ||
|
||
for _ in range(number): | ||
successful = False | ||
while not successful: | ||
# Step 1: Draw deflector | ||
deflector, z_max = self.draw_deflector() | ||
# Step 2: Draw sources | ||
source = self.draw_sources(z_max) | ||
if source is None: | ||
continue # Retry if sources are invalid | ||
|
||
# Step 3: Create false positive | ||
vd=deflector.velocity_dispersion(cosmo=self.cosmo) | ||
test_area = self._test_area_factor * draw_test_area( | ||
v_sigma=vd) | ||
false_positive = FalsePositive( | ||
deflector_class=deflector, | ||
source_class=source, | ||
cosmo=self.cosmo, | ||
test_area=test_area, | ||
) | ||
false_positive_population.append(false_positive) | ||
successful = True | ||
return false_positive_population[0] if number==1 else false_positive_population |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.