def test_convert_qdict(): vlim = dict(unit='km/s', value=50.) time = dict(unit='s', value=100.) mass = dict(unit='g', value=10.) idict = dict(vlim=vlim, nest=dict(time=time, mass=mass)) newdict = ltu.convert_quantity_in_dict(idict) assert newdict['nest']['mass'] == 10 * u.g # Simple one obj = ltu.convert_quantity_in_dict(vlim) assert obj.unit == u.km / u.s
def from_dict(cls, idict, **kwargs): """ Instantiate from a dict Args: idict (dict): **kwargs: Passed to the __init__ call Returns: """ # Init coord = SkyCoord(ra=idict['ra'], dec=idict['dec'], unit='deg') DM = units.Quantity(idict['DM']['value'], unit=idict['DM']['unit']) slf = cls(idict['FRB'], coord, DM, **kwargs) for key in ['ra', 'dec', 'DM']: idict.pop(key) for key in [ 'DM_err', 'DMISM', 'DMISM_err', 'RM', 'RM_err', 'fluence', 'fluence_err' ]: if key in idict.keys(): setattr( slf, key, units.Quantity(idict[key]['value'], unit=idict[key]['unit'])) idict.pop(key) # Cosmology if slf.cosmo.name != idict['cosmo']: raise AssertionError( f"Your cosmology does not match the expected for {idict['FRB']}. Gotta deal.." ) idict.pop('cosmo') # dicts for ndict in slf.main_dict: if ndict in idict.keys(): for key, value in idict[ndict].items(): if isinstance(value, dict): newvalue = ltu.convert_quantity_in_dict(value) else: newvalue = value idict[ndict][key] = newvalue setattr(slf, ndict, idict[ndict]) # Deal with quantities idict.pop(ndict) # Remainder for key in idict.keys(): setattr(slf, key, idict[key]) # Return return slf
def from_dict(cls, idict): """ Generate a LineLimits class from an input dict keys -- wrest, z, zlim -- are required Parameters ---------- idict Returns ------- """ wrest = ltu.convert_quantity_in_dict(idict['wrest']) slf = cls(wrest, idict['z'], idict['zlim']) # Return return slf
def add_other_from_dict(slf, idict): """ Add other attributes to a system from a dict Useful for handling various AbsSystem types Parameters ---------- slf : AbsSystem idict : dict """ # Other if 'kin' in idict.keys(): slf.kin = ltu.convert_quantity_in_dict(idict['kin']) if 'Refs' in idict.keys(): slf.Refs = idict['Refs'] if 'ZH' in idict.keys(): slf.ZH = idict['ZH'] slf.sig_ZH = idict['sig_ZH']
def from_dict(cls, idict): """ Generate a LineLimits class from an input dict keys -- wrest, z, zlim -- are required Parameters ---------- idict Returns ------- """ try: wrest = ltu.convert_quantity_in_dict(idict['wrest']) except KeyError: wrest = None slf = cls(idict['z'], idict['zlim'], wrest=wrest) # Return return slf
def from_dict(cls, idict, skip_components=False, use_coord=False, **kwargs): """ Instantiate from a dict. Usually read from the hard-drive Parameters ---------- idict : dict skip_components : bool, optional If True, absorption components (if any exist) are not loaded from the input dict. Use when you are only interested in the global properties of an AbsSystem use_coord : bool, optinal Use coordinates from the AbsSystem to build the components (and lines) Speeds up performance, but you should know things are OK before using this Returns ------- AbsSystem """ if 'NHI' in idict.keys(): ckwargs = dict(NHI=idict['NHI'], sig_NHI=idict['sig_NHI'], flag_NHI=idict['flag_NHI']) slf = cls(SkyCoord(ra=idict['RA']*u.deg, dec=idict['DEC']*u.deg), idict['zabs'], idict['vlim']*u.km/u.s, zem=idict['zem'], name=idict['Name'], **ckwargs) # Other if 'kin' in idict.keys(): slf.kin = ltu.convert_quantity_in_dict(idict['kin']) if 'Refs' in idict.keys(): slf.Refs = idict['Refs'] # Components if not skip_components: # Components if use_coord: # Speed up performance coord = slf.coord else: coord = None components = ltiu.build_components_from_dict(idict, coord=coord, **kwargs) for component in components: # This is to insure the components follow the rules slf.add_component(component, **kwargs) # Return return slf
def from_dict(cls, idict, coord=None, skip_abslines=False, **kwargs): """ Instantiate from a dict Parameters ---------- idict : dict skip_abslines : bool, optional Skip loading up the AbsLine objects. Speeds up performance when one only needs the component object Returns ------- AbsComponent """ if coord is not None: radec = coord else: radec = SkyCoord(ra=idict['RA'], dec=idict['DEC'], unit='deg') # Init # slf = cls(radec, tuple(idict['Zion']), idict['zcomp'], Quantity(idict['vlim'], unit='km/s'), # For IGMGuesses for key in ['reliability', 'Reliability']: if key in idict.keys(): reliability = idict[key] break else: reliability = 'none' # Deprecated column density attributes if 'logN' in idict.keys(): warnings.warn('Column density attributes are now Deprecated', DeprecationWarning) #print("We will use yours for now..") Ntup = tuple( [idict[key] for key in ['flag_N', 'logN', 'sig_logN']]) else: Ntup = None # Instantiate slf = cls(radec, tuple(idict['Zion']), idict['zcomp'], idict['vlim'] * u.km / u.s, Ej=idict['Ej'] / u.cm, A=idict['A'], Ntup=Ntup, comment=idict['comment'], name=idict['Name'], reliability=reliability) # Attributes if 'attrib' in idict.keys(): attrkeys = idict['attrib'].keys() for ak in attrkeys: if isinstance(idict['attrib'][ak], dict): slf.attrib[ak] = ltu.convert_quantity_in_dict( idict['attrib'][ak]) else: slf.attrib[ak] = idict['attrib'][ak] # Insist that error values are 2-elements :: Mainly for older saved files if ak == 'sig_N': if slf.attrib[ak].size == 1: slf.attrib[ak] = Quantity( [slf.attrib[ak].value] * 2) * slf.attrib[ak].unit if ak == 'sig_logN': if isinstance(slf.attrib[ak], (float, int)): slf.attrib[ak] = np.array([slf.attrib[ak]] * 2) elif isinstance(slf.attrib[ak], (list)): slf.attrib[ak] = np.array(slf.attrib[ak]) # Deprecated column (again) if Ntup is not None: warnings.warn( 'Overwriting column density attributes (if they existed).', DeprecationWarning) slf.attrib['flag_N'] = Ntup[0] slf.attrib['logN'] = Ntup[1] if isiterable(Ntup[2]): slf.attrib['sig_logN'] = np.array(Ntup[2]) else: slf.attrib['sig_logN'] = np.array([Ntup[2]] * 2) _, _ = ltaa.linear_clm(slf.attrib) # Set linear quantities # Add AbsLine objects if not skip_abslines: for key in idict['lines'].keys(): iline = AbsLine.from_dict(idict['lines'][key], coord=coord, **kwargs) slf.add_absline(iline, **kwargs) # Return return slf
def from_dict(cls, idict, coord=None, warn_only=False, chk_data=True, **kwargs): """ Initialize from a dict (usually read from disk) Parameters ---------- idict : dict dict with the Line parameters chk_data : bool, optional Check atomic data in dict against current values in LineList warn_only : bool, optional If the chk_data is performed and the values do not match, only throw a Warning as opposed to crashing Returns ------- sline : SpectralLine SpectralLine of the proper type """ # Init if idict['ltype'] == 'Abs': # TODO: remove this try/except eventually try: sline = AbsLine(idict['name'], **kwargs) except KeyError: # This is to be compatible JSON files already written with old notation (e.g. DLA H100) sline = AbsLine(idict['trans'], **kwargs) elif idict['ltype'] == 'Em': sline = EmLine(idict['name'], **kwargs) else: raise ValueError("Not prepared for type {:s}.".format(idict['ltype'])) # Check data if chk_data: #for key in idict['data']: for key in sline.data.keys(): if key not in idict['data'].keys(): warnings.warn("Key {:s} not in your input dict".format(key)) continue if isinstance(idict['data'][key], dict): # Assume Quantity val = idict['data'][key]['value'] else: val = idict['data'][key] try: assert sline.data[key] == val except AssertionError: if warn_only: warnings.warn("Different data value for {:s}: {}, {}".format(key,sline.data[key],val)) # Set analy for key in idict['analy'].keys(): if isinstance(idict['analy'][key], dict): # Assume Quantity #sline.analy[key] = Quantity(idict['analy'][key]['value'], # unit=idict['analy'][key]['unit']) #pdb.set_trace() sline.analy[key] = ltu.convert_quantity_in_dict(idict['analy'][key]) elif key == 'spec_file': # spec_file is intended to be the name of the spectrum file # spec is intended to hold an XSpectrum1D object #warnings.warn("You will need to load {:s} into analy['spec'] yourself".format( # idict['analy'][key])) sline.analy[key] = idict['analy'][key] else: sline.analy[key] = idict['analy'][key] # Set attrib for key in idict['attrib'].keys(): if isinstance(idict['attrib'][key], dict): sline.attrib[key] = ltu.convert_quantity_in_dict(idict['attrib'][key]) elif key in ['RA','DEC']: if coord is None: sline.attrib['coord'] = SkyCoord(ra=idict['attrib']['RA']*u.deg, dec=idict['attrib']['DEC']*u.deg) else: sline.attrib['coord'] = coord else: sline.attrib[key] = idict['attrib'][key] # Set z and limits if 'z' in sline.attrib.keys(): # Backwards compatability z = sline.attrib.pop('z') else: z = 0. if 'limits' in idict.keys(): if 'wrest' not in idict['limits'].keys(): # compatibility with IGMGuesses # import pdb; pdb.set_trace() idict['limits']['wrest'] = ltu.jsonify(sline.wrest) idict['limits']['z'] = z sline.limits = zLimits.from_dict(idict['limits']) else: sline.limits = zLimits(z, [z,z], wrest=sline.wrest) if 'vlim' in sline.analy.keys(): # Backwards compatability if sline.analy['vlim'][1] > sline.analy['vlim'][0]: sline.limits.set(sline.analy['vlim']) elif 'wvlim' in sline.analy.keys(): # Backwards compatability if sline.analy['wvlim'][1] > sline.analy['wvlim'][0]: sline.limits.set(sline.analy['wvlim']) return sline
def from_dict(cls, idict, coord=None, warn_only=False, chk_data=True, **kwargs): """ Initialize from a dict (usually read from disk) Parameters ---------- idict : dict dict with the Line parameters chk_data : bool, optional Check atomic data in dict against current values in LineList warn_only : bool, optional If the chk_data is performed and the values do not match, only throw a Warning as opposed to crashing Returns ------- sline : SpectralLine SpectralLine of the proper type """ # Init if idict['ltype'] == 'Abs': # TODO: remove this try/except eventually try: sline = AbsLine(idict['name'], **kwargs) except KeyError: # This is to be compatible JSON files already written with old notation (e.g. DLA H100) sline = AbsLine(idict['trans'], **kwargs) elif idict['ltype'] == 'Em': sline = EmLine(idict['name'], **kwargs) else: raise ValueError("Not prepared for type {:s}.".format( idict['ltype'])) # Check data if chk_data: for key in idict['data']: if isinstance(idict['data'][key], dict): # Assume Quantity val = idict['data'][key]['value'] else: val = idict['data'][key] try: assert sline.data[key] == val except AssertionError: if warn_only: warnings.warn( "Different data value for {:s}: {}, {}".format( key, sline.data[key], val)) # Set analy for key in idict['analy'].keys(): if isinstance(idict['analy'][key], dict): # Assume Quantity #sline.analy[key] = Quantity(idict['analy'][key]['value'], # unit=idict['analy'][key]['unit']) #pdb.set_trace() sline.analy[key] = ltu.convert_quantity_in_dict( idict['analy'][key]) elif key == 'spec_file': # spec_file is intended to be the name of the spectrum file # spec is intendended to hold an XSpectrum1D object warnings.warn( "You will need to load {:s} into analy['spec'] yourself". format(idict['analy'][key])) sline.analy[key] = idict['analy'][key] else: sline.analy[key] = idict['analy'][key] # Set attrib for key in idict['attrib'].keys(): if isinstance(idict['attrib'][key], dict): sline.attrib[key] = ltu.convert_quantity_in_dict( idict['attrib'][key]) elif key in ['RA', 'DEC']: if coord is None: sline.attrib['coord'] = SkyCoord( ra=idict['attrib']['RA'] * u.deg, dec=idict['attrib']['DEC'] * u.deg) else: sline.attrib['coord'] = coord else: sline.attrib[key] = idict['attrib'][key] # Set z and limits if 'z' in sline.attrib.keys(): # Backwards compatability z = sline.attrib.pop('z') else: z = 0. if 'limits' in idict.keys(): if 'wrest' not in idict['limits'].keys( ): # compatibility with IGMGuesses # import pdb; pdb.set_trace() idict['limits']['wrest'] = ltu.jsonify(sline.wrest) idict['limits']['z'] = z sline.limits = LineLimits.from_dict(idict['limits']) else: sline.limits = LineLimits(sline.wrest, z, [z, z]) if 'vlim' in sline.analy.keys(): # Backwards compatability if sline.analy['vlim'][1] > sline.analy['vlim'][0]: sline.limits.set(sline.analy['vlim']) elif 'wvlim' in sline.analy.keys(): # Backwards compatability if sline.analy['wvlim'][1] > sline.analy['wvlim'][0]: sline.limits.set(sline.analy['wvlim']) return sline
def from_dict(cls, idict, coord=None, warn_only=False, chk_data=True, **kwargs): """ Initialize from a dict (usually read from disk) Parameters ---------- idict : dict dict with the Line parameters chk_data : bool, optional Check atomic data in dict against current values in LineList warn_only : bool, optional If the chk_data is performed and the values do not match, only throw a Warning as opposed to crashing Returns ------- sline : SpectralLine SpectralLine of the proper type """ # Init if idict['ltype'] == 'Abs': # TODO: remove this try/except eventually try: sline = AbsLine(idict['name'], **kwargs) except KeyError: # This is to be compatible JSON files already written with old notation (e.g. DLA H100) sline = AbsLine(idict['trans'], **kwargs) else: raise ValueError("Not prepared for type {:s}.".format(idict['ltype'])) # Check data if chk_data: for key in idict['data']: if isinstance(idict['data'][key], dict): # Assume Quantity val = idict['data'][key]['value'] else: val = idict['data'][key] try: assert sline.data[key] == val except AssertionError: if warn_only: warnings.warn("Different data value for {:s}: {}, {}".format(key,sline.data[key],val)) # Set analy for key in idict['analy']: if isinstance(idict['analy'][key], dict): # Assume Quantity #sline.analy[key] = Quantity(idict['analy'][key]['value'], # unit=idict['analy'][key]['unit']) #pdb.set_trace() sline.analy[key] = ltu.convert_quantity_in_dict(idict['analy'][key]) elif key == 'spec_file': warnings.warn("You will need to load {:s} into attrib['spec'] yourself".format( idict['analy'][key])) else: sline.analy[key] = idict['analy'][key] # Set attrib for key in idict['attrib']: if isinstance(idict['attrib'][key], dict): sline.attrib[key] = ltu.convert_quantity_in_dict(idict['attrib'][key]) elif key in ['RA','DEC']: if coord is None: sline.attrib['coord'] = SkyCoord(ra=idict['attrib']['RA']*u.deg, dec=idict['attrib']['DEC']*u.deg) else: sline.attrib['coord'] = coord else: sline.attrib[key] = idict['attrib'][key] return sline
def from_dict(cls, idict, coord=None, skip_abslines=False, **kwargs): """ Instantiate from a dict Parameters ---------- idict : dict skip_abslines : bool, optional Skip loading up the AbsLine objects. Speeds up performance when one only needs the component object Returns ------- AbsComponent """ if coord is not None: radec = coord else: radec = SkyCoord(ra=idict['RA'], dec=idict['DEC'], unit='deg') # Init # slf = cls(radec, tuple(idict['Zion']), idict['zcomp'], Quantity(idict['vlim'], unit='km/s'), # For IGMGuesses for key in ['reliability', 'Reliability']: if key in idict.keys(): reliability = idict[key] break else: reliability = 'none' # Deprecated column density attributes if 'logN' in idict.keys(): warnings.warn('Column density attributes are now Deprecated', DeprecationWarning) #print("We will use yours for now..") Ntup = tuple([idict[key] for key in ['flag_N', 'logN', 'sig_logN']]) else: Ntup = None # Instantiate slf = cls(radec, tuple(idict['Zion']), idict['zcomp'], idict['vlim']*u.km/u.s, Ej=idict['Ej']/u.cm, A=idict['A'], Ntup=Ntup, comment=idict['comment'], name=idict['Name'], reliability=reliability) # Attributes if 'attrib' in idict.keys(): attrkeys = idict['attrib'].keys() for ak in attrkeys: if isinstance(idict['attrib'][ak],dict): slf.attrib[ak] = ltu.convert_quantity_in_dict(idict['attrib'][ak]) else: slf.attrib[ak] = idict['attrib'][ak] # Insist that error values are 2-elements :: Mainly for older saved files if ak == 'sig_N': if slf.attrib[ak].size == 1: slf.attrib[ak] = Quantity([slf.attrib[ak].value]*2) * slf.attrib[ak].unit if ak == 'sig_logN': if isinstance(slf.attrib[ak], (float,int)): slf.attrib[ak] = np.array([slf.attrib[ak]]*2) elif isinstance(slf.attrib[ak], (list)): slf.attrib[ak] = np.array(slf.attrib[ak]) # Deprecated column (again) if Ntup is not None: warnings.warn('Overwriting column density attributes (if they existed).', DeprecationWarning) slf.attrib['flag_N'] = Ntup[0] slf.attrib['logN'] = Ntup[1] if isiterable(Ntup[2]): slf.attrib['sig_logN'] = np.array(Ntup[2]) else: slf.attrib['sig_logN'] = np.array([Ntup[2]]*2) _, _ = ltaa.linear_clm(slf.attrib) # Set linear quantities # Add AbsLine objects if not skip_abslines: for key in idict['lines'].keys(): iline = AbsLine.from_dict(idict['lines'][key], coord=coord, **kwargs) slf.add_absline(iline, **kwargs) # Return return slf