def test_init(): dlas = DLASurvey(ref='null') assert dlas.abs_type == 'DLA' coord = SkyCoord(ra=123.1143, dec=-12.4321, unit='deg') dlasys = DLASystem(coord, 1.244, [-300, 300.] * u.km / u.s, 20.4) dlasys.name = 'Sys1' # coord2 = SkyCoord(ra=223.1143, dec=42.4321, unit='deg') dlasys2 = DLASystem(coord2, 1.744, [-300, 300.] * u.km / u.s, 21.7) dlasys2.name = 'Sys2' # Add systems dlas.add_abs_sys(dlasys) dlas.add_abs_sys(dlasys2) assert dlas.nsys == 2
def main(args=None): from astropy.coordinates import SkyCoord from astropy import units as u from linetools import utils as ltu from pyigm.abssys.dla import DLASystem from pyigm.abssys.lls import LLSSystem if args is None: pargs = parser() else: pargs = args # Coordinates if pargs.jcoord is not None: coord = ltu.radec_to_coord(pargs.jcoord) else: coord = SkyCoord(ra=0., dec=0., unit='deg') # vlim if pargs.vlim is not None: vlims = [float(vlim) for vlim in pargs.vlim.split(',')]*u.km/u.s else: vlims = None # go if pargs.itype == 'dla': isys = DLASystem(coord, pargs.zabs, vlims, pargs.NHI, zem=pargs.zem, sig_NHI=pargs.sigNHI) elif pargs.itype == 'lls': isys = LLSSystem(coord, pargs.zabs, vlims, NHI=pargs.NHI, zem=pargs.zem, sig_NHI=pargs.sigNHI) else: raise IOError("Not prepared for this type of IGMSystem") # Write isys.write_json(pargs.outfile)
def profile(): coord = SkyCoord(ra=12.231, dec=-12.2432, unit='deg') dla_list = [] vlim = [-500., 500.]*u.km/u.s for ii in range(10000): if (ii % 100) == 0: print('tt: {:d}'.format(ii)) isys = DLASystem(coord, 2., vlim, NHI=21.0, zem=3., name='dumb') dla_list.append(isys) return None
def insert_dlas(sightline, overlap=False, rstate=None, slls=False, mix=False, high=False, noise=False): """ Insert a DLA into input spectrum Also adjusts the noise Will also add noise 'everywhere' if requested Parameters ---------- sightline:dla_cnn.data_model.sightline.Sightline object overlap: bool noise: bool, optional Returns ------- None """ #init if rstate is None: rstate = np.random.RandomState() spec = XSpectrum1D.from_tuple( (10**sightline.loglam, sightline.flux)) #generate xspectrum1d # Generate DLAs dlas = [] spec_dlas = [] zabslist = init_zabs(sightline, overlap) for zabs in zabslist: # Random NHI NHI = uniform_NHI(slls=slls, mix=mix, high=high) spec_dla = Dla((1 + zabs) * 1215.6701, NHI, '00' + str(jj)) if (slls or mix): dla = LLSSystem((sightline.ra, sightline.dec), zabs, None, NHI=NHI) else: dla = DLASystem((sightline.ra, sightline.dec), zabs, None, NHI) dlas.append(dla) spec_dlas.append(spec_dla) # Insert dlas to one sightline vmodel, _ = hi_model(dlas, spec, fwhm=3.) #add noise if noise: rand = rstate.randn(len(sightline.flux)) noise = rand * sightline.error * np.sqrt(1 - vmodel.flux.value**2) else: noise = 0 final_spec = XSpectrum1D.from_tuple( (vmodel.wavelength, spec.flux.value * vmodel.flux.value + noise)) #generate new sightline sightline.flux = final_spec.flux.value sightline.dlas = spec_dlas sightline.s2n = estimate_s2n(sightline)
def get_dla(zabs, NHI, matrix_lam, matrix_flux, wvoff=60.): spec = XSpectrum1D.from_tuple((matrix_lam, matrix_flux)) if NHI < 20.3: dla = LLSSystem((0, 0), zabs, None, NHI=NHI) else: dla = DLASystem((0, 0), zabs, None, NHI) wvcen = (1 + zabs) * 1215.67 gd_wv = (spec.wavelength.value > wvcen - wvoff - 30) & (spec.wavelength.value < wvcen + wvoff + 30) co = 1.5 #np.mean(spec.flux[gd_wv])#amax lya, lines = hi_model(dla, spec, lya_only=True) return lya.wavelength[gd_wv], co * lya.flux[gd_wv]
def insert_dlas(spec, zem, fNHI=None, rstate=None, slls=False, mix=False, high=False, low_s2n=False, noise_boost=4.): """ Insert a DLA into input spectrum Also adjusts the noise Will also add noise 'everywhere' if requested Parameters ---------- spec fNHI rstate low_s2n : bool, optional Reduce the S/N everywhere. By a factor of noise_boost noise_boost : float, optional Factor to *increase* the noise by Returns ------- final_spec : XSpectrum1D dlas : list List of DLAs inserted """ from pyigm.fN import dla as pyi_fd from pyigm.abssys.dla import DLASystem from pyigm.abssys.lls import LLSSystem from pyigm.abssys.utils import hi_model # Init if rstate is None: rstate = np.random.RandomState() if fNHI is None: fNHI = init_fNHI(slls=slls, mix=mix, high=high) # Allowed redshift placement ## Cut on zem and 910A rest-frame zlya = spec.wavelength.value / 1215.67 - 1 dz = np.roll(zlya, -1) - zlya dz[-1] = dz[-2] gdz = (zlya < zem) & (spec.wavelength > 910. * u.AA * (1 + zem)) # l(z) -- Uses DLA for SLLS too which is fine lz = pyi_fd.lX(zlya[gdz], extrap=True, calc_lz=True) cum_lz = np.cumsum(lz * dz[gdz]) tot_lz = cum_lz[-1] fzdla = interpolate.interp1d(cum_lz / tot_lz, zlya[gdz], bounds_error=False, fill_value=np.min(zlya[gdz])) # # n DLA nDLA = 0 while nDLA == 0: nval = rstate.poisson(tot_lz, 100) gdv = nval > 0 if np.sum(gdv) == 0: continue else: nDLA = nval[np.where(gdv)[0][0]] # Generate DLAs dlas = [] for jj in range(nDLA): # Random z zabs = float(fzdla(rstate.random_sample())) # Random NHI NHI = float(fNHI(rstate.random_sample())) if (slls or mix): dla = LLSSystem((0., 0), zabs, None, NHI=NHI) else: dla = DLASystem((0., 0), zabs, (None, None), NHI) dlas.append(dla) # Insert vmodel, _ = hi_model(dlas, spec, fwhm=3., llist=llist) # Add noise rand = rstate.randn(spec.npix) noise = rand * spec.sig * (1 - vmodel.flux.value) # More noise?? if low_s2n: rand2 = rstate.randn(spec.npix) more_noise = noise_boost * rand2 * spec.sig noise += more_noise else: s2n_boost = 1. final_spec = XSpectrum1D.from_tuple( (vmodel.wavelength, spec.flux.value * vmodel.flux.value + noise, noise_boost * spec.sig)) # Return return final_spec, dlas
def test_simple_dla_init(): # Init dla = DLASystem((0.*u.deg, 0.*u.deg), 2.5, None, NHI=20.55) # np.testing.assert_allclose(dla.vlim[0].value,-500.) np.testing.assert_allclose(dla.NHI, 20.55)
def load_ml_file(pred_file): """ Load the search results from the CNN into a DLASurvey object Parameters ---------- pred_file Returns ------- ml_llssurvey: LLSSurvey ml_dlasusrvey: DLASurvey """ print("Loading {:s}. Please be patient..".format(pred_file)) # Read ml_results = ltu.loadjson(pred_file) use_platef = False if 'plate' in ml_results[0].keys(): use_platef = True else: if 'id' in ml_results[0].keys(): use_id = True # Init idict = dict(ra=[], dec=[], plate=[], fiber=[]) if use_platef: for key in ['plate', 'fiber', 'mjd']: idict[key] = [] dlasystems = [] llssystems = [] # Generate coords to speed things up for obj in ml_results: for key in ['ra', 'dec']: idict[key].append(obj[key]) ml_coords = SkyCoord(ra=idict['ra'], dec=idict['dec'], unit='deg') ra_names = ml_coords.icrs.ra.to_string(unit=u.hour, sep='', pad=True) dec_names = ml_coords.icrs.dec.to_string(sep='', pad=True, alwayssign=True) vlim = [-500., 500.] * u.km / u.s dcoord = SkyCoord(ra=0., dec=0., unit='deg') # Loop on list didx, lidx = [], [] print("Looping on sightlines..") for tt, obj in enumerate(ml_results): #if (tt % 100) == 0: # print('tt: {:d}'.format(tt)) # Sightline if use_id: plate, fiber = [int(spl) for spl in obj['id'].split('-')] idict['plate'].append(plate) idict['fiber'].append(fiber) # Systems for ss, syskey in enumerate(['dlas', 'subdlas']): for idla in obj[syskey]: name = 'J{:s}{:s}_z{:.3f}'.format(ra_names[tt], dec_names[tt], idla['z_dla']) if ss == 0: isys = DLASystem(dcoord, idla['z_dla'], vlim, NHI=idla['column_density'], zem=obj['z_qso'], name=name) else: isys = LLSSystem(dcoord, idla['z_dla'], vlim, NHI=idla['column_density'], zem=obj['z_qso'], name=name) isys.confidence = idla['dla_confidence'] isys.s2n = idla['s2n'] if use_platef: isys.plate = obj['plate'] isys.fiber = obj['fiber'] elif use_id: isys.plate = plate isys.fiber = fiber # Save if ss == 0: didx.append(tt) dlasystems.append(isys) else: lidx.append(tt) llssystems.append(isys) # Generate sightline tables sightlines = Table() sightlines['RA'] = idict['ra'] sightlines['DEC'] = idict['dec'] sightlines['PLATE'] = idict['plate'] sightlines['FIBERID'] = idict['fiber'] # Surveys ml_llssurvey = LLSSurvey() ml_llssurvey.sightlines = sightlines.copy() ml_llssurvey._abs_sys = llssystems ml_llssurvey.coords = ml_coords[np.array(lidx)] ml_dlasurvey = DLASurvey() ml_dlasurvey.sightlines = sightlines.copy() ml_dlasurvey._abs_sys = dlasystems ml_dlasurvey.coords = ml_coords[np.array(didx)] # Return return ml_llssurvey, ml_dlasurvey