def _register_filters(self, force: bool = False): """Register filters for this survey / data release with SNCosmo Args: force: Re-register a band if already registered """ data_arr = np.genfromtxt(self._filter_path, skip_header=1) filt_table = Table(data_arr, names=['wave', 'u', 'g', 'r', 'i', 'z']) filt_table['wave'] *= 10 # Convert nm to angstroms # Bands are already registered in sncosmo under a different name. # We register them using the package standardized name for new_band_name in self.band_names: built_in_name = new_band_name.split('_')[-1] # MEGACAMPSF are radius dependant if 'MEGACAMPSF' in built_in_name: trans = filt_table[built_in_name[-1].lower()] new_band = sncosmo.Bandpass(filt_table['wave'], trans) else: built_in_band = sncosmo.get_bandpass(built_in_name) new_band = sncosmo.Bandpass(built_in_band.wave, built_in_band.trans) new_band.name = new_band_name sncosmo.register(new_band, force=force)
def register_filter_file(file_path: str, filter_name: str, force: bool = False): """Registers filter profiles with sncosmo if not already registered Assumes the file at ``file_path`` is a two column, white space delimited ascii table. Args: file_path: Path of ascii table with wavelength (Ang) and transmission filter_name: The name of the registered filter. force: Whether to re-register a band if already registered """ # Get set of registered builtin and custom band passes # noinspection PyProtectedMember available_bands = set(k[0] for k in sncosmo.bandpasses._BANDPASSES._loaders) # noinspection PyProtectedMember available_bands.update(k[0] for k in sncosmo.bandpasses._BANDPASSES._instances) # Register the new bandpass if filter_name not in available_bands: wave, trans = np.genfromtxt(file_path).T is_good_data = ~np.isnan(wave) & ~np.isnan(trans) band = sncosmo.Bandpass(wave[is_good_data], trans[is_good_data]) band.name = filter_name sncosmo.register(band, force=force)
def sdssrpband(): import sncosmo from astropy.io import ascii import astropy.units as u # https://lco.global/observatory/instruments/filters/sdss-r/ sdssrp = ascii.read(open("sdssrp.txt", "rb")) sdssrp.rename_columns(['\lambda', '[nm]'], ['lambda [nm]', 'transmission']) wavelength = sdssrp['lambda [nm]'] transmission = sdssrp['transmission'] band = sncosmo.Bandpass(wavelength, transmission, name='sdssrp', wave_unit=u.nm) sncosmo.register(band) #print(band.minwave(),band.maxwave(),band.wave_eff) plot = False if plot: import matplotlib.pyplot as plt import matplotlib matplotlib.rcParams.update({'font.size': 15}) fig, ax = plt.subplots(figsize=(10.5, 8.5)) ax.plot(wavelength, transmission) ax.set_xlabel("Wavelength [nm]") ax.set_ylabel("Transmission") ax.set_xlim(525, 725) plt.savefig('transmission_sdssrp.pdf', bbox_inches='tight') return band
def test_register(): disp = np.array([4000., 4200., 4400., 4600., 4800., 5000.]) trans = np.array([0., 1., 1., 1., 1., 0.]) # create a band, register it, make sure we can get it back. band = sncosmo.Bandpass(disp, trans, name='tophatg') sncosmo.register(band) assert sncosmo.get_bandpass('tophatg') is band
def test_register_source(): phase = np.linspace(0., 100., 10) wave = np.linspace(800., 20000., 100) flux = np.ones((len(phase), len(wave)), dtype=float) source = sncosmo.TimeSeriesSource(phase, wave, flux, name='test_source') sncosmo.register(source) # Retrieving a source makes a copy, so just check that the names are the # same. assert sncosmo.get_source('test_source').name == 'test_source'
def register_ztf_bandpass(band): bandtable = pd.read_csv(os.path.expandvars( "$SIRAHPIPE_DIR/data/ztf_filters/ZTF_{}.csv".format(band)), names=['wave', 'trans'], comment='#') bandtable = bandtable.drop_duplicates('wave') bandpass = sncosmo.Bandpass(wave=bandtable.wave, trans=bandtable.trans, wave_unit=u.nm) bandpass.name = 'ztf' + band sncosmo.register(bandpass, force=True)
def test_register(): disp = np.array([4000., 4200., 4400., 4600., 4800., 5000.]) trans = np.array([0., 1., 1., 1., 1., 0.]) # create a band, register it, make sure we can get it back. band = sncosmo.Bandpass(disp, trans, name='tophatg') sncosmo.register(band) assert sncosmo.get_bandpass('tophatg') is band # test deprecated path to registry band = sncosmo.Bandpass(disp, trans, name='tophatg2') sncosmo.registry.register(band) assert sncosmo.get_bandpass('tophatg2') is band
def register_sncosmo_filter(wave: np.array, trans: np.array, name: str, force: bool = False) -> None: """Register a filter profile with sncosmo Args: wave: Array of wavelength values in Angstroms trans: Array of transmission values between 0 and 1 name: Name of the filter to register force: Whether to overwrite an existing filter with the given name """ # Specifying the name argument in the constructor seems to not work in # at least some sncosmo versions, so we set it after instantiation sncosmo_ccd = sncosmo.Bandpass(wave, trans) sncosmo_ccd.name = name sncosmo.register(sncosmo_ccd, force=force)
def register_filter(f: str, instrument: str = 'FORS2'): data = p.filter_params(instrument=instrument, f=f) bandpass = sncosmo.Bandpass(data['wavelengths_filter_only'], data['transmissions_filter_only']) sncosmo.register(bandpass, f)
# phaseN wlsN flux ## We need to adapt the format to build the sncosmo class TimeSeriesSource for tmpl in tmpl_host_extinction_corrected[:]: tmpl_name = tmpl.replace('.SED', '') phase_raw, wls_raw, flux_raw = np.genfromtxt( pycoco_dir + 'Templates_HostCorrected/' + tmpl, unpack=True) wls = np.unique(wls_raw) phase = phase_raw[::len(wls)] flux_reshaped = flux_raw.reshape(len(phase), len(wls)) source = sncosmo.TimeSeriesSource(phase, wls, flux_reshaped, zero_before=True, name=tmpl_name) sncosmo.register(source, name=tmpl_name) print( tmpl_name, '(type %s) registered!' % name2type[tmpl_name.replace('pycoco_', '')]) for tmpl in tmpl_host_extinction_NOTcorrected[:]: tmpl_name = tmpl.replace('.SED', '') phase_raw, wls_raw, flux_raw = np.genfromtxt( pycoco_dir + 'Templates_noHostCorr/' + tmpl, unpack=True) wls = np.unique(wls_raw) phase = phase_raw[::len(wls)] flux_reshaped = flux_raw.reshape(len(phase), len(wls)) source = sncosmo.TimeSeriesSource(phase, wls, flux_reshaped, zero_before=True,
def test_register_invalid(): with pytest.raises(ValueError): sncosmo.register("invalid")
def test_register_magsystem(): magsys = sncosmo.get_magsystem('ab') sncosmo.register(magsys, name='test_magsys') assert sncosmo.get_magsystem('test_magsys') is magsys
import sncosmo #Change band names to ones used in P60 dataframe band_g = sncosmo.get_bandpass('sdssg') band_g.name = 'g' sncosmo.register(band_g) band_r = sncosmo.get_bandpass('sdssr') band_r.name = 'r' sncosmo.register(band_r) band_i = sncosmo.get_bandpass('sdssi') band_i.name = 'i' sncosmo.register(band_i) band_u = sncosmo.get_bandpass('sdssu') band_u.name = 'u' sncosmo.register(band_u)
def extract_features(meta: pd.DataFrame, lc: pd.DataFrame, source: str, normalize: bool = False, snr: int = 3, zbounds: str = 'estimated', skip: int = 0, end: int = -1, clip_bounds: bool = False, t_bounds: bool = False, columns: List[str] = None): if normalize: try: for band in ['g', 'i', 'r', 'u', 'y', 'z']: b = read_bandpass('lsst/total_{}.dat'.format(band), wave_unit=u.nm, trim_level=0.001, name='lsst{}_n'.format(band), normalize=True) sncosmo.register(b, 'lsst{}_n'.format(band)) except: raise pass with timer('dropping meta'): meta = meta[meta.object_id.isin(lc.object_id)].reset_index(drop=True) print('shape(meta): {}'.format(meta.shape)) if 'object_id' in meta: meta.set_index('object_id', inplace=True) if normalize: passbands = [ 'lsstu_n', 'lsstg_n', 'lsstr_n', 'lssti_n', 'lsstz_n', 'lssty_n' ] else: passbands = ['lsstu', 'lsstg', 'lsstr', 'lssti', 'lsstz', 'lssty'] with timer('prep'): lc['band'] = lc['passband'].apply(lambda x: passbands[x]) lc['zpsys'] = 'ab' lc['zp'] = 25.0 # create a model model = Model(source=source) params = model.param_names if columns: ret = pd.DataFrame(columns=columns) else: columns = ['chisq', 'ncall'] + [source + '_' + c for c in params] + [ source + '_' + c + '_err' for c in params ] ret = pd.DataFrame(columns=columns) prefix = source if zbounds == 'fixed': prefix += '_f_' else: prefix += '_p_' prefix += 'sn{}_'.format(snr) if normalize: prefix += 'n_' ret.columns = [prefix + c for c in ret.columns] n_errors = 0 n_loop = len(meta) if end > 0: n_loop = end for i in tqdm(range(skip, n_loop)): object_id = meta.index[i] try: ret.loc[object_id] = fit_lc(model, meta, lc, object_id, zbounds, clip_bounds, t_bounds, snr) except: n_errors += 1 if i == 30 and n_errors == 31: print('All 30 first attempts were failed. stopped') raise print('total {} data processed. {} data was skipped'.format( len(meta), n_errors)) ret.reset_index(inplace=True) ret.rename(columns={'index': 'object_id'}, inplace=True) return ret