コード例 #1
0
def get_noise_sigma2_lenstronomy(img, pixel_scale, exposure_time, magnitude_zero_point, read_noise=None, ccd_gain=None, sky_brightness=None, seeing=None, num_exposures=1, psf_type='GAUSSIAN', kernel_point_source=None, truncation=5, data_count_unit='ADU', background_noise=None):
    """Get the variance of sky, readout, and Poisson flux noise sources using lenstronomy

    Parameters
    ----------
    img : 2D numpy array
        image on which the noise will be evaluated
    pixel_scale : float
        pixel scale in arcsec/pixel
    exposure_time : float
        exposure time per image in seconds
    magnitude_zero_point : float
        magnitude at which 1 count per second per arcsecond square is registered
    read_noise : float
        std of noise generated by readout (in units of electrons)
    ccd_gain : float
        electrons/ADU (analog-to-digital unit). A gain of 8 means that the camera digitizes the CCD signal so that each ADU corresponds to 8 photoelectrons
    sky_brightness : float
         sky brightness (in magnitude per square arcsec)
    seeing : float
        fwhm of PSF
    num_exposures : float
        number of exposures that are combined
    psf_type : str
        type of PSF ('GAUSSIAN' and 'PIXEL' supported)
    kernel_point_source : 2d numpy array
        model of PSF centered with odd number of pixels per axis(optional when psf_type='PIXEL' is chosen)
    truncation : float
        Gaussian truncation (in units of sigma), only required for 'GAUSSIAN' model
    data_count_unit : str
        unit of the data (and other properties), 'e-': (electrons assumed to be IID), 'ADU': (analog-to-digital unit)
    background_noise : float
        sqrt(variance of background) as a total contribution from read noise, sky brightness, etc. in units of the data_count_units
        If you set this parameter, it will override read_noise, sky_brightness. Default: None

    Returns
    -------
    dict
        variance in the poisson, sky, and readout noise sources

    """
    single_band = SingleBand(pixel_scale, exposure_time, magnitude_zero_point, read_noise=read_noise, ccd_gain=ccd_gain, sky_brightness=sky_brightness, seeing=seeing, num_exposures=num_exposures, psf_type=psf_type, kernel_point_source=kernel_point_source, truncation=truncation, data_count_unit=data_count_unit, background_noise=background_noise)
    noise_sigma2 = {}
    noise_sigma2['poisson'] = single_band.flux_noise(img)**2.0
    exposure_time_tot = single_band._num_exposures * single_band._exposure_time
    readout_noise_tot = single_band._num_exposures * single_band.read_noise**2.0
    sky_per_pixel = single_band.sky_brightness * single_band.pixel_scale ** 2
    noise_sigma2['sky'] = sky_per_pixel**2.0/exposure_time_tot
    noise_sigma2['readout'] = readout_noise_tot / exposure_time_tot**2.0
    return noise_sigma2
コード例 #2
0
def add_arc(image, kwargs_band, kwargs_params, kwargs_model, kwargs_numerics={}):
    """
    routine to add lensed arc to existing image

    :param image: 2d square numpy array of original image
    :param kwargs_band: keyword arguments specifying the observation to be simulated according to lenstronomy.SimulationAPI
    :param kwargs_model: keyword arguments of model configurations. All possibilities available at lenstronom.Util.class_creator
    :param kwargs_params: keyword arguments of the different model components. Supports 'kwargs_lens', 'kwargs_source_mag',
    'kwargs_lens_light_mag', 'kwargs_ps_mag'
    :param kwargs_numerics: keyword arguments describing the numerical setting of lenstronomy as outlined in lenstronomy.ImSim.Numerics
    :return: 2d numpy array
    """
    numpix = len(image)
    arc = _arc_model(numpix, kwargs_band, kwargs_model, kwargs_numerics=kwargs_numerics, **kwargs_params)
    band = SingleBand(**kwargs_band)
    noisy_arc = arc + band.flux_noise(arc)
    return image + noisy_arc
コード例 #3
0
class TestData(object):
    def setup(self):
        self.ccd_gain = 4.
        pixel_scale = 0.13
        self.read_noise = 10.
        self.kwargs_instrument = {
            'read_noise': self.read_noise,
            'pixel_scale': pixel_scale,
            'ccd_gain': self.ccd_gain
        }

        exposure_time = 100
        sky_brightness = 20.
        self.magnitude_zero_point = 21.
        num_exposures = 2
        seeing = 0.9
        kwargs_observations = {
            'exposure_time': exposure_time,
            'sky_brightness': sky_brightness,
            'magnitude_zero_point': self.magnitude_zero_point,
            'num_exposures': num_exposures,
            'seeing': seeing,
            'psf_type': 'GAUSSIAN'
        }
        self.kwargs_data = util.merge_dicts(self.kwargs_instrument,
                                            kwargs_observations)
        self.data_adu = SingleBand(data_count_unit='ADU', **self.kwargs_data)
        self.data_e_ = SingleBand(data_count_unit='e-', **self.kwargs_data)

    def test_sky_brightness(self):
        sky_adu = self.data_adu.sky_brightness
        sky_e_ = self.data_e_.sky_brightness
        assert sky_e_ == sky_adu * self.ccd_gain
        npt.assert_almost_equal(sky_adu, 0.627971607877395, decimal=6)

    def test_background_noise(self):
        bkg_adu = self.data_adu.background_noise
        bkg_e_ = self.data_e_.background_noise
        assert bkg_adu == bkg_e_ / self.ccd_gain

        self.data_adu._background_noise = 1
        bkg = self.data_adu.background_noise
        assert bkg == 1

    def test_flux_noise(self):
        flux_iid = 50.
        flux_adu = flux_iid / self.ccd_gain
        noise_adu = self.data_adu.flux_noise(flux_adu)
        noise_e_ = self.data_e_.flux_noise(flux_iid)
        assert noise_e_ == 100. / 200.
        assert noise_e_ == noise_adu * self.ccd_gain

    def test_noise_for_model(self):
        model_adu = np.ones((10, 10))
        model_e_ = model_adu * self.ccd_gain
        noise_adu = self.data_adu.noise_for_model(model_adu,
                                                  background_noise=True,
                                                  poisson_noise=True,
                                                  seed=42)
        noise_adu_2 = self.data_adu.noise_for_model(model_adu,
                                                    background_noise=True,
                                                    poisson_noise=True,
                                                    seed=42)
        npt.assert_almost_equal(noise_adu, noise_adu_2, decimal=10)
        noise_e_ = self.data_e_.noise_for_model(model_e_,
                                                background_noise=True,
                                                poisson_noise=True,
                                                seed=42)
        npt.assert_almost_equal(noise_adu,
                                noise_e_ / self.ccd_gain,
                                decimal=10)
        noise_e_ = self.data_e_.noise_for_model(model_e_,
                                                background_noise=True,
                                                poisson_noise=True,
                                                seed=None)

    def test_estimate_noise(self):
        image_adu = np.ones((10, 10))
        image_e_ = image_adu * self.ccd_gain
        noise_adu = self.data_adu.estimate_noise(image_adu)
        noise_e_ = self.data_e_.estimate_noise(image_e_)
        npt.assert_almost_equal(noise_e_, noise_adu * self.ccd_gain)

    def test_magnitude2cps(self):
        mag_0 = self.data_adu.magnitude2cps(
            magnitude=self.magnitude_zero_point)
        npt.assert_almost_equal(mag_0, 1. / self.ccd_gain, decimal=10)
        mag_0_e_ = self.data_e_.magnitude2cps(
            magnitude=self.magnitude_zero_point)
        npt.assert_almost_equal(mag_0_e_, 1, decimal=10)

        mag_0 = self.data_adu.magnitude2cps(
            magnitude=self.magnitude_zero_point + 1)
        npt.assert_almost_equal(mag_0, 0.0995267926383743, decimal=10)

        mag_0 = self.data_adu.magnitude2cps(
            magnitude=self.magnitude_zero_point - 1)
        npt.assert_almost_equal(mag_0, 0.627971607877395, decimal=10)

    def test_flux_iid(self):
        flux_iid_adu = self.data_adu.flux_iid(flux_per_second=1)
        flux_iid_e = self.data_e_.flux_iid(flux_per_second=1)
        npt.assert_almost_equal(flux_iid_e,
                                flux_iid_adu / self.ccd_gain,
                                decimal=6)

        flux_adu = 10
        flux_e_ = flux_adu * self.ccd_gain
        noise_e_ = self.data_e_.flux_noise(flux_e_)
        noise_adu = self.data_adu.flux_noise(flux_adu)
        npt.assert_almost_equal(noise_e_ / self.ccd_gain, noise_adu, decimal=8)

    def test_psf_type(self):
        assert self.data_adu._psf_type == 'GAUSSIAN'
        kwargs_observations = {
            'exposure_time': 1,
            'sky_brightness': 1,
            'magnitude_zero_point': self.magnitude_zero_point,
            'num_exposures': 1,
            'seeing': 1,
            'psf_type': 'PIXEL'
        }
        kwargs_data = util.merge_dicts(self.kwargs_instrument,
                                       kwargs_observations)
        data_pixel = SingleBand(data_count_unit='ADU', **kwargs_data)
        assert data_pixel._psf_type == 'PIXEL'

        kwargs_observations = {
            'exposure_time': 1,
            'sky_brightness': 1,
            'magnitude_zero_point': self.magnitude_zero_point,
            'num_exposures': 1,
            'seeing': 1,
            'psf_type': 'NONE'
        }
        kwargs_data = util.merge_dicts(self.kwargs_instrument,
                                       kwargs_observations)
        data_pixel = SingleBand(data_count_unit='ADU', **kwargs_data)
        assert data_pixel._psf_type == 'NONE'