Exemple #1
0
    def data(self,index,role):
        if role == Qt.EditRole:
            row = index.row()
            column = index.column()
            if column == 1: toret=atomicdata.lam2ion(self.fitpars[0][row])
            elif column == 2: toret=round(self.fitpars[3][row],5)
            elif column == 3: toret=round(self.fitpars[1][row],3)
            elif column == 4: toret=round(self.fiterrors[1][row],3)
            elif column == 5: toret=round(self.fitpars[2][row],3)
            elif column == 6: toret=round(self.fiterrors[2][row],3)
            elif column == 7: toret=round(self.fitpars[4][row],3)
            elif column == 8: toret=round(self.fiterrors[4][row],3)
            elif column == 9: toret=self.linecmts[0][row]
            elif column == 10: toret=self.linecmts[1][row]
            else: toret=QVariant(round(self.fitpars[column][row],3))
            return toret

        if index.isValid() and role == Qt.DisplayRole:
            row = index.row()
            column = index.column()
            if column == 1: toret=atomicdata.lam2ion(self.fitpars[0][row])
            elif column == 2: toret=round(self.fitpars[3][row],5)
            elif column == 3: toret=round(self.fitpars[1][row],3)
            elif column == 4: toret=round(self.fiterrors[1][row],3)
            elif column == 5: toret=round(self.fitpars[2][row],3)
            elif column == 6: toret=round(self.fiterrors[2][row],3)
            elif column == 7: toret=round(self.fitpars[4][row],3)
            elif column == 8: toret=round(self.fiterrors[4][row],3)
            elif column == 9: toret=self.linecmts[0][row]
            elif column == 10: toret=self.linecmts[1][row]
            else: toret=QVariant(round(self.fitpars[column][row],3))
            return toret

        else: return None
Exemple #2
0
 def validateWavelength(self):
     ### Handle approximate wavelength entries by looking up precise values on focus exit
     if self.lamBox.text()!='':
         restlam=self.lamBox.text()
         restlam=float(restlam)
         try:
             lam,fosc,gam=atomicdata.setatomicdata([restlam],precise=False)
             self.lamBox.setText(str(lam[0]))
             self.ionLabel.setText(atomicdata.lam2ion(lam[0]))
         except:
             pass
Exemple #3
0
 def validateWavelength(self):
     ### Handle approximate wavelength entries by looking up precise values on focus exit
     if self.lamBox.text()!='':
         restlam=self.lamBox.text()
         restlam=float(restlam)
         try:
             lam,fosc,gam=atomicdata.setatomicdata([restlam],precise=False)
             self.lamBox.setText(str(lam[0]))
             self.ionLabel.setText(atomicdata.lam2ion(lam[0]))
         except:
             pass
Exemple #4
0
    def data(self,index,role):
        if role == Qt.EditRole:
            row = index.row()
            column = index.column()
            if column == 1: toret=atomicdata.lam2ion(self.fitpars[0][row])
            elif column == 2: toret=round(self.fitpars[3][row],5)
            elif column == 3: toret=round(self.fitpars[1][row],3)
            elif column == 4: toret=round(self.fiterrors[1][row],3)
            elif column == 5: toret=round(self.fitpars[2][row],3)
            elif column == 6: toret=round(self.fiterrors[2][row],3)
            elif column == 7: toret=round(self.fitpars[4][row],3)
            elif column == 8: toret=round(self.fiterrors[4][row],3)
            elif column == 9: toret=self.linecmts[0][row]
            elif column == 10: toret=self.linecmts[1][row]
            else: toret=QVariant(round(self.fitpars[column][row],3))
            return toret

        if index.isValid() and role == Qt.DisplayRole:
            row = index.row()
            column = index.column()
            if column == 0: toret=round(self.fitpars[0][row],3)
            elif column == 1:
                toret=atomicdata.lam2ion(self.fitpars[0][row])
            elif column == 2: toret=round(self.fitpars[3][row],5)
            elif column == 3: toret=round(self.fitpars[1][row],3)
            elif column == 4: toret=round(self.fiterrors[1][row],3)
            elif column == 5: toret=round(self.fitpars[2][row],3)
            elif column == 6: toret=round(self.fiterrors[2][row],3)
            elif column == 7: toret=round(self.fitpars[4][row],3)
            elif column == 8: toret=round(self.fiterrors[4][row],3)
            elif column == 9: toret=self.linecmts[0][row]
            elif column == 10: toret=self.linecmts[1][row]
            else: toret=QVariant(round(self.fitpars[column][row],3))
            return str(toret)

        else: return None
Exemple #5
0
def abscomponents_from_abslines(abslinelist, **kwargs):
    '''
    Organizes list of AbsLines into components based on the following order: redshift, velocity, and species

    Parameters
    ----------
    abslinelist : list
        List of AbsLine objects

    Returns
    -------
    complist : list
        List AbsComponent objects
    '''
    ### Import machine learning clustering algorithm for velocity grouping
    from sklearn.cluster import MeanShift, estimate_bandwidth
    from linetools.isgm.abscomponent import AbsComponent

    ### Populate arrays with line redshifts, velocity centroids, and name of species
    zarr = np.zeros(len(abslinelist))
    varr = np.zeros(len(abslinelist))
    sparr = np.chararray(len(abslinelist), itemsize=6)
    for i, absline in enumerate(abslinelist):
        zarr[i] = absline.z
        # import pdb;        pdb.set_trace()
        varr[i] = absline.attrib['vel'].value
        sparr[i] = atomicdata.lam2ion(absline.wrest.value)

    abslinelist = np.array(
        abslinelist)  # Convert to array for the indexing used below

    ### Group lines with the same redshifts and similar velocities
    complines = []
    uqzs = np.unique(zarr)
    for uqz in uqzs:
        ### Identify velocity groups
        thesez = np.where(zarr == uqz)[0]
        X = np.array(list(zip(varr[thesez], np.zeros(len(varr[thesez])))),
                     dtype=float)
        ms = MeanShift(bandwidth=7.)
        ms.fit(X)
        vidxs = ms.labels_
        vs = ms.cluster_centers_
        uqvidxs = np.unique(vidxs)
        ### Make lists of lines that will belong to each component
        for idx in uqvidxs:
            theseuqvs = thesez[np.where(
                vidxs ==
                idx)[0]]  # isolate velocity-grouped lines with this redshift
            uqsp = np.unique(
                sparr[theseuqvs]
            )  # identify the unique species with lines in this group
            ### Get lines belonging to each species and add them to become components
            for sp in uqsp:
                spidx = theseuqvs[np.where(sparr[theseuqvs] == sp)]
                complines.append(abslinelist[spidx])
    ### Instantiate the AbsComponents
    comps = []
    for lst in complines:
        if '*' in lst[0].name:
            linename = lst[0].name
            starct = linename.count('*')
            stars = '*' * starct
        else:
            stars = None
        thiscomp = AbsComponent.from_abslines(lst.tolist(),
                                              stars=stars,
                                              chk_vel=False,
                                              adopt_median=True,
                                              **kwargs)
        comps.append(thiscomp)
    return comps
Exemple #6
0
def writelinepars(fitpars,
                  fiterrors,
                  parinfo,
                  specfile,
                  outfilename,
                  linecmts=None):
    '''
	Write fit parameters out to file.

	Parameters
	----------
	fitpars : list of lists
		Parameters for fit ready for fitter!
	fiterrors : array of numpy vectors
		Error array for the fitting initialized to '0' for each param
	parinfo : array of arrays
		Flags to be used in fit
	specfile : str
		Name of the input file containing the spectrum
	outfilename : str
		Parameter output filename
	linecmts : list of lists, optional
		Reliability flags and comments, e.g., from igmguesses

	'''
    import os
    ### Set outputs and open files
    bigfiletowrite = cfg.largeVPparfile
    filetowrite = outfilename
    if os.path.isfile(filetowrite):
        VPparfile = open(filetowrite, 'wb')
        bigparfile = open(bigfiletowrite, 'ab')  # Append to the running list
    else:
        VPparfile = open(filetowrite, 'wb')
        bigparfile = open(bigfiletowrite, 'wb')

    ### Prep header of line parameter file
    if linecmts is not None:
        header = b'specfile|restwave|zsys|col|sigcol|bval|sigbval|vel|sigvel|nflag|bflag|vflag|vlim1|vlim2|wobs1|wobs2|pix1|pix2|z_comp|trans|rely|comment \n'
    else:
        header = b'specfile|restwave|zsys|col|sigcol|bval|sigbval|vel|sigvel|nflag|bflag|vflag|vlim1|vlim2|wobs1|wobs2|pix1|pix2|z_comp|trans \n'
    VPparfile.write(header)
    bigparfile.write(header)

    ### Grab parameters/info for each line
    for i in range(len(fitpars[0])):
        zline = fitpars[3][i]
        vlim1 = fitpars[5][i]
        vlim2 = fitpars[6][i]
        restwave = fitpars[0][i]
        wobs1 = restwave * (1 + zline + vlim1 / 299792.458)
        wobs2 = restwave * (1 + zline + vlim2 / 299792.458)
        pix1 = jbg.closest(cfg.wave, wobs1)
        pix2 = jbg.closest(cfg.wave, wobs2)
        trans = atomicdata.lam2ion(fitpars[0][i])
        z_comp = ltu.z_from_dv(fitpars[4][i] * u.km / u.s, zline)
        if linecmts is not None:
            towrite = jbg.pipedelimrow([
                specfile, restwave,
                round(zline, 5),
                round(fitpars[1][i], 3),
                round(fiterrors[1][i], 3),
                round(fitpars[2][i], 3),
                round(fiterrors[2][i], 3),
                round(fitpars[4][i], 3),
                round(fiterrors[4][i], 3), parinfo[1][i], parinfo[2][i],
                parinfo[4][i], vlim1, vlim2, wobs1, wobs2, pix1, pix2,
                round(z_comp, 5), trans, linecmts[0][i], linecmts[1][i]
            ])
        else:
            towrite = jbg.pipedelimrow([
                specfile, restwave,
                round(zline, 5),
                round(fitpars[1][i], 3),
                round(fiterrors[1][i], 3),
                round(fitpars[2][i], 3),
                round(fiterrors[2][i], 3),
                round(fitpars[4][i], 3),
                round(fiterrors[4][i], 3), parinfo[1][i], parinfo[2][i],
                parinfo[4][i], vlim1, vlim2, wobs1, wobs2, pix1, pix2,
                round(z_comp, 5), trans
            ])
        VPparfile.write(towrite.encode())
        bigparfile.write(towrite.encode())
    VPparfile.close()
    bigparfile.close()
    print('Line parameters written to:')
    print(filetowrite)
Exemple #7
0
def readpars(filename, wave1=None, wave2=None):
    '''

	Parameters
	----------
	filename : str
		Name of parameter input (or joebvp output) file
		File should at least have following columns:
			specfile|restwave|zsys|col|sigcol|bval|sigbval|vel|sigvel|nflag|bflag|vflag|vlim1|vlim2
	wave1 : float, optional
		Beginning of wavelength range over which to load lines (must be set with wave2)
	wave2 : float, optional
		End of wavelength range over which to load lines (must be set with wave1)

	Returns
	-------
	fitpars : list of lists
		Parameters for fit ready for fitter!
	fiterrors : array of numpy vectors
		Error array for the fitting initialized to '0' for each param
	parinfo : array of arrays
		Flags to be used in fit
	linecmts: list of lists
		Reliability and comment flags, e.g., from igmguesses
	'''

    linelist = ascii.read(filename)
    linerestwave = linelist['restwave'].data
    linez = linelist['zsys'].data
    if (wave1 == None) & (wave2 == None):
        lineshere = np.arange(len(linelist))
    elif ((wave1 == None) | (wave2 == None)) | (wave1 >= wave2):
        lineshere = np.arange(len(linelist))
        warnings.warn(
            'Note that both \'wave1\' and \'wave2\' must be declared or neither must be. \n Loading all lines in list.'
        )
    else:
        lineobswave = linerestwave * (1. + linez)
        lineshere = np.where((lineobswave > wave1) & (lineobswave < wave2))[0]
    linelist = linelist[lineshere]
    linelist['ions'] = atomicdata.lam2ion(linelist['restwave'])

    linelist.sort(['ions', 'zsys', 'vel', 'restwave'])

    linerestwave = linelist['restwave']
    zs = linelist['zsys']
    linecol = linelist['col']
    lineb = linelist['bval']
    linevel = linelist['vel']
    linevlim1 = linelist['vlim1']
    linevlim2 = linelist['vlim2']
    colflag = linelist['nflag']
    bflag = linelist['bflag']
    velflag = linelist['vflag']
    restwaves = linerestwave
    if (('rely' in linelist.colnames) & ('comment' in linelist.colnames)):
        pass
    elif ('rely' in linelist.colnames):
        linelist['comment'] = ['none'] * len(linelist)
    else:
        linelist['rely'] = ['-'] * len(linelist)
        linelist['comment'] = ['none'] * len(linelist)

    reliability = linelist['rely']
    comment = linelist['comment']
    initinfo = [colflag, bflag, velflag]
    initpars = [restwaves, linecol, lineb, zs, linevel, linevlim1, linevlim2]
    fitpars, parinfo = initlinepars(zs, restwaves, initpars, initinfo=initinfo)
    fiterrors = np.zeros([5, len(fitpars[0])])  # Initialize errors to zero
    linecmts = [reliability, comment]
    #fiterrors[1] = colsig
    #fiterrors[2] = bsig
    #fiterrors[4] = velsig
    return fitpars, fiterrors, parinfo, linecmts
Exemple #8
0
def writelinepars(fitpars,fiterrors,parinfo, specfile, outfilename, linecmts=None):
	'''
	Write fit parameters out to file.

	Parameters
	----------
	fitpars : list of lists
		Parameters for fit ready for fitter!
	fiterrors : array of numpy vectors
		Error array for the fitting initialized to '0' for each param
	parinfo : array of arrays
		Flags to be used in fit
	specfile : str
		Name of the input file containing the spectrum
	outfilename : str
		Parameter output filename
	linecmts : list of lists, optional
		Reliability flags and comments, e.g., from igmguesses

	'''
	import os
	### Set outputs and open files
	bigfiletowrite = cfg.largeVPparfile
	filetowrite = outfilename
	if os.path.isfile(filetowrite):
		VPparfile = open(filetowrite, 'wb')
		bigparfile = open(bigfiletowrite, 'ab') # Append to the running list
	else:
		VPparfile = open(filetowrite, 'wb')
		bigparfile = open(bigfiletowrite, 'wb')

	### Prep header of line parameter file
	if linecmts is not None:
		header = 'specfile|restwave|zsys|col|sigcol|bval|sigbval|vel|sigvel|nflag|bflag|vflag|vlim1|vlim2|wobs1|wobs2|pix1|pix2|z_comp|trans|rely|comment \n'
	else:
		header = 'specfile|restwave|zsys|col|sigcol|bval|sigbval|vel|sigvel|nflag|bflag|vflag|vlim1|vlim2|wobs1|wobs2|pix1|pix2|z_comp|trans \n'
	VPparfile.write(header)
	bigparfile.write(header)

	### Grab parameters/info for each line
	for i in range(len(fitpars[0])):
		zline = fitpars[3][i]
		vlim1 = fitpars[5][i]
		vlim2 = fitpars[6][i]
		restwave = fitpars[0][i]
		wobs1 = restwave * (1 + zline + vlim1 / 299792.458)
		wobs2 = restwave * (1 + zline + vlim2 / 299792.458)
		pix1 = jbg.closest(cfg.wave, wobs1)
		pix2 = jbg.closest(cfg.wave, wobs2)
		trans = atomicdata.lam2ion(fitpars[0][i])
		z_comp = ltu.z_from_dv(fitpars[4][i]*u.km/u.s, zline)
		if linecmts is not None:
			towrite = jbg.pipedelimrow(
				[specfile, restwave, round(zline, 5), round(fitpars[1][i], 3), round(fiterrors[1][i], 3),
				 round(fitpars[2][i], 3), round(fiterrors[2][i], 3), round(fitpars[4][i], 3), round(fiterrors[4][i], 3),
				 parinfo[1][i], parinfo[2][i], parinfo[4][i], vlim1, vlim2, wobs1, wobs2, pix1, pix2,round(z_comp, 5), trans,
				 linecmts[0][i],linecmts[1][i]])
		else:
			towrite = jbg.pipedelimrow(
				[specfile, restwave, round(zline, 5), round(fitpars[1][i], 3), round(fiterrors[1][i], 3),
				 round(fitpars[2][i], 3), round(fiterrors[2][i], 3), round(fitpars[4][i], 3), round(fiterrors[4][i], 3),
				 parinfo[1][i], parinfo[2][i], parinfo[4][i], vlim1, vlim2, wobs1, wobs2, pix1, pix2, round(z_comp, 5),trans])
		VPparfile.write(towrite)
		bigparfile.write(towrite)
	VPparfile.close()
	bigparfile.close()
	print 'Line parameters written to:'
	print filetowrite
Exemple #9
0
def readpars(filename,wave1=None,wave2=None):
	'''

	Parameters
	----------
	filename : str
		Name of parameter input (or joebvp output) file
		File should at least have following columns:
			specfile|restwave|zsys|col|sigcol|bval|sigbval|vel|sigvel|nflag|bflag|vflag|vlim1|vlim2
	wave1 : float, optional
		Beginning of wavelength range over which to load lines (must be set with wave2)
	wave2 : float, optional
		End of wavelength range over which to load lines (must be set with wave1)

	Returns
	-------
	fitpars : list of lists
		Parameters for fit ready for fitter!
	fiterrors : array of numpy vectors
		Error array for the fitting initialized to '0' for each param
	parinfo : array of arrays
		Flags to be used in fit
	linecmts: list of lists
		Reliability and comment flags, e.g., from igmguesses
	'''

	linelist = ascii.read(filename)
	linerestwave = linelist['restwave'].data
	linez = linelist['zsys'].data
	if (wave1 == None)&(wave2 == None):
		lineshere = range(len(linelist))
	elif ((wave1 == None)|(wave2 == None))|(wave1>=wave2):
		lineshere = range(len(linelist))
		warnings.warn('Note that both \'wave1\' and \'wave2\' must be declared or neither must be. \n Loading all lines in list.')
	else:
		lineobswave = linerestwave * (1. + linez)
		lineshere = np.where((lineobswave > wave1) & (lineobswave < wave2))[0]
	linelist=linelist[lineshere]
	linelist['ions']=atomicdata.lam2ion(linelist['restwave'])

	linelist.sort(['ions','zsys','vel','restwave'])


	linerestwave = linelist['restwave']
	zs = linelist['zsys']
	linecol = linelist['col']
	lineb = linelist['bval']
	linevel = linelist['vel']
	linevlim1 = linelist['vlim1']
	linevlim2 = linelist['vlim2']
	colflag = linelist['nflag']
	bflag = linelist['bflag']
	velflag = linelist['vflag']
	restwaves = linerestwave
	if (('rely' in linelist.colnames)&('comment' in linelist.colnames)):
		pass
	elif ('rely' in linelist.colnames):
		linelist['comment']=['none']*len(linelist)
	else:
		linelist['rely'] = ['-'] * len(linelist)
		linelist['comment'] = ['none'] * len(linelist)

	reliability = linelist['rely']
	comment = linelist['comment']
	initinfo = [colflag, bflag, velflag]
	initpars = [restwaves, linecol, lineb, zs, linevel, linevlim1, linevlim2]
	fitpars, parinfo = initlinepars(zs, restwaves, initpars, initinfo=initinfo)
	fiterrors = np.zeros([5, len(fitpars[0])])  # Initialize errors to zero
	linecmts = [reliability,comment]
	#fiterrors[1] = colsig
	#fiterrors[2] = bsig
	#fiterrors[4] = velsig
	return fitpars,fiterrors,parinfo,linecmts