def get_testbed_sbp_image(mp, si): """ Get an image in the specified sub-bandpass from a testbed. This function calls an equivalent sub-function depending on mp.testbed. Parameters ---------- mp: falco.config.ModelParameters Structure of model parameters si: int Index of sub-bandpass for which to take the image Returns ------- TBD Normalized intensity in the sub-bandpass (i.e. approximate raw contrast but normalized by a photometry measurement at a single offset) """ if type(mp) is not falco.config.ModelParameters: raise TypeError('Input "mp" must be of type ModelParameters') check.nonnegative_scalar_integer(si, 'si', TypeError) raise NotImplementedError('Testbed functionality not implemented yet.') return None
def get_sbp_image(mp, si): """ Get an image in the specified sub-bandpass. Get an image in the specified sub-bandpass. Wrapper for functions to get a simulated image or a testbed image. Parameters ---------- mp: falco.config.ModelParameters Structure of model parameters si: int Index of sub-bandpass for which to take the image Returns ------- Isbp : numpy ndarray Sub-bandpass image in units of normalized intensity """ if type(mp) is not falco.config.ModelParameters: raise TypeError('Input "mp" must be of type ModelParameters') check.nonnegative_scalar_integer(si, 'si', TypeError) if mp.flagSim: Isbp = get_sim_sbp_image(mp, si) else: Isbp = get_testbed_sbp_image(mp, si) return Isbp
def _get_single_sbp_image_wvlPol(mp, si, ilist, inds_list): """ Use only with get_sim_sbp_image for parallel processing. Function to return the weighted, normalized intensity image at a given wavelength in the specified sub-bandpass. """ if type(mp) is not falco.config.ModelParameters: raise TypeError('Input "mp" must be of type ModelParameters') check.nonnegative_scalar_integer(si, 'si', TypeError) wi = inds_list[ilist][0] ipol = inds_list[ilist][1] # Get the starlight image modvar = falco.config.Object() modvar.sbpIndex = si modvar.wpsbpIndex = wi mp.full.polaxis = mp.full.pol_conds[ipol] modvar.whichSource = 'star' Estar = falco.model.full(mp, modvar) Iout = np.abs(Estar)**2 # Apply spectral weighting outside this function # Apply weight within the sub-bandpass. # Assume polarizations are evenly weighted. Iout = mp.full.lambda_weights[wi] / len(mp.full.pol_conds) * Iout return Iout
def get_sim_sbp_image(mp, si): """ Get a simulated image in the specified sub-bandpass. Parameters ---------- mp: falco.config.ModelParameters Structure of model parameters si: int Index of sub-bandpass for which to take the image Returns ------- subbandImage Simulated sub-bandpass image in units of normalized intensity """ if type(mp) is not falco.config.ModelParameters: raise TypeError('Input "mp" must be of type ModelParameters') check.nonnegative_scalar_integer(si, 'si', TypeError) Npol = len(mp.full.pol_conds) # Number of polarization states used # Loop over all wavelengths and polarizations inds_list = [(x, y) for x in range(mp.Nwpsbp) for y in range(Npol)] Nvals = mp.Nwpsbp * Npol Iall = np.zeros((Nvals, mp.Fend.Neta, mp.Fend.Nxi)) if mp.flagParallel: pool = multiprocessing.Pool(processes=mp.Nthreads) resultsRaw = pool.starmap(_get_single_sbp_image_wvlPol, [(mp, si, ilist, inds_list) for ilist in range(Nvals)]) results = resultsRaw pool.close() pool.join() for ilist in range(Nvals): Iall[ilist, :, :] = results[ilist] else: for ilist in range(Nvals): Iall[ilist, :, :] = _get_single_sbp_image_wvlPol( mp, si, ilist, inds_list) # Apply the spectral weights and sum subbandImage = 0 for ilist in range(Nvals): subbandImage += np.squeeze(Iall[ilist, :, :]) if mp.flagImageNoise: subbandImage = add_noise_to_subband_image(mp, subbandImage, si) return subbandImage
def test_nonnegative_scalar_integer_bad_var(self): """ Fail on invalid variable type. Type: nonnegative scalar integer """ for v0 in [1.0, -1, 1j, (1., ), [5, 5], 'nsi']: with self.assertRaises(TestCheckException): check.nonnegative_scalar_integer(v0, 'nsi', TestCheckException) pass pass pass
def test_nonnegative_scalar_integer_good(self): """ Verify checker works correctly for valid input. Type: nonnegative scalar integer """ for j in [0, 1, 2]: try: check.nonnegative_scalar_integer(j, 'nsi', TestCheckException) except check.CheckException: self.fail('nonnegative_scalar_integer failed on valid input') pass pass
def add_noise_to_subband_image(mp, imageIn, iSubband): """ Add noise (photon shot, dark current, & read) to a simulated image. Parameters ---------- mp : ModelParameters structure of model parameters. imageIn : array_like 2-Dnoiseless starting image for a given subband [normalized intensity] iSubband : int index of subband in which the image was taken Returns ------- imageOut : array_like 2-D noisy image [normalized intensity] """ check.twoD_array(imageIn, 'imageIn', ValueError) check.nonnegative_scalar_integer(iSubband, 'iSubband', ValueError) peakCounts = (mp.detector.peakFluxVec[iSubband] * mp.detector.tExpVec[iSubband]) peakElectrons = mp.detector.gain * peakCounts imageInElectrons = peakElectrons * imageIn imageInCounts = 0 for iExp in range(mp.detector.Nexp): # Add photon shot noise noisyImageInElectrons = np.random.poisson(imageInElectrons) # Compute dark current darkCurrent = (mp.detector.darkCurrentRate * mp.detector.tExpVec[iSubband] * np.ones_like(imageIn)) darkCurrent = np.random.poisson(darkCurrent) # Compute Gaussian read noise readNoise = (mp.detector.readNoiseStd * np.random.randn(imageIn.shape[0], imageIn.shape[1])) # Convert back from e- to counts and then discretize imageInCounts = (imageInCounts + np.round( (noisyImageInElectrons + darkCurrent + readNoise) / mp.detector.gain) / mp.detector.Nexp) # Convert back from counts to normalized intensity imageOut = imageInCounts / peakCounts return imageOut
def test_nonnegative_scalar_integer_bad_vexc(self): """Fail on input vexc not an Exception.""" with self.assertRaises(check.CheckException): check.nonnegative_scalar_integer(1, 'nsi', 'TestCheckException') pass pass
def test_nonnegative_scalar_integer_bad_vname(self): """Fail on invalid input name for user output.""" with self.assertRaises(check.CheckException): check.nonnegative_scalar_integer(1, (1, ), TestCheckException) pass pass