def plotSequence(*args, **kwargs): ''' :Purpose: Compares a spectrum to a sequence of standards a batch of spectra into a 2x2 set of PDF files, with options of overplotting comparison spectra, including best-match spectral standards. :param input: A single or list of Spectrum objects or filenames, or the glob search string for a set of files (e.g., '/Data/myspectra/*.fits'). :type input: required :param type_range: Number of subtypes to consider above and below best-fit spectral type :type type_range: optional, default = 2 :param spt: Default spectral type for source; this input skips classifyByStandard :type spt: optional, default = None :param output: Filename for output; full path should be include if not saving to current directory. If blank, plot is shown on screen :type output: optional, default = None (screen display) Relevant parameters for plotSpectrum may also be passed :Example: >>> import glob, splat >>> files = glob.glob('/home/mydata/*.fits') >>> sp = splat.plotBatch(files,classify=True,output='comparison.pdf') >>> sp = splat.plotBatch('/home/mydata/*.fits',classify=True,output='comparison.pdf') >>> sp = splat.plotBatch([splat.Spectrum(file=f) for f in files],classify=True,output='comparison.pdf') All three of these commands produce the same result ''' # check inputs if len(args) == 0: raise ValueError('\nNeed to provide a spectrum object or filename for plotStandardSequence') parameters = ['type_range'] splat.checkKeys(kwargs,parameters,forcekey=False) type_range =kwargs.get('type_range',2) kwargs['stack']=kwargs.get('stack',0.5) # kwargs['legendLocation']=kwargs.get('legendLocation','outside') kwargs['telluric']=kwargs.get('telluric',True) kwargs['method']=kwargs.get('method','kirkpatrick') # kwargs['color']=kwargs.get('color','r') kwargs['colorComparison']=kwargs.get('colorComparison','k') kwargs['colorScheme']=kwargs.get('colorScheme','winter') kwargs['figsize']=kwargs.get('figsize',[10,10*type_range*kwargs['stack']]) kwargs['fontscale']=kwargs.get('fontscale',1.5) # process input if isinstance(args[0],str): if len(glob.glob(args[0])) == 0: raise ValueError('\nCannot find input file {} - make sure full path is included'.format(args[0])) try: sp = splat.Spectrum(file = args[0]) except: raise ValueError('\nCould not read in file {} - make sure the file is correctly formatted'.format(args[0])) elif isinstance(args[0],splat.Spectrum): sp = copy.deepcopy(args[0]) else: raise ValueError('\nInput should be a Spectrum object or filename') sp.normalize() # classify by comparison to standards spt = kwargs.get('spt',splat.classifyByStandard(sp,**kwargs)[0]) if not isinstance(spt,str): spt = splat.typeToNum(spt) # produce range of standards for plot stdnum = numpy.arange(numpy.floor(splat.typeToNum(spt)-type_range),numpy.ceil(splat.typeToNum(spt)+type_range)+1) if numpy.max(stdnum) > 39: stdnum-=(numpy.max(stdnum)-39) if numpy.min(stdnum) < 10: stdnum+=(10-numpy.min(stdnum)) stdspt = [splat.typeToNum(i) for i in stdnum] stds = [splat.SPEX_STDS[s] for s in stdspt] stdlabels = ['{} Standard'.format(s) for s in stdspt] plotlist = [] labels = [] colors = [] for i,stdsp in enumerate(stds): plotlist.append([stdsp,sp]) labels.extend([]) fig = splat.plotSpectrum(stds,comparison=sp,labels=stdlabels,**kwargs) return fig
def plotSequence(*args, **kwargs): ''' :Purpose: Compares a spectrum to a sequence of standards a batch of spectra into a 2x2 set of PDF files, with options of overplotting comparison spectra, including best-match spectral standards. :param input: A single or list of Spectrum objects or filenames, or the glob search string for a set of files (e.g., '/Data/myspectra/*.fits'). :type input: required :param type_range: Number of subtypes to consider above and below best-fit spectral type :type type_range: optional, default = 2 :param spt: Default spectral type for source; this input skips classifyByStandard :type spt: optional, default = None :param output: Filename for output; full path should be include if not saving to current directory. If blank, plot is shown on screen :type output: optional, default = None (screen display) Relevant parameters for plotSpectrum may also be passed :Example: >>> import glob, splat >>> files = glob.glob('/home/mydata/*.fits') >>> sp = splat.plotBatch(files,classify=True,output='comparison.pdf') >>> sp = splat.plotBatch('/home/mydata/*.fits',classify=True,output='comparison.pdf') >>> sp = splat.plotBatch([splat.Spectrum(file=f) for f in files],classify=True,output='comparison.pdf') All three of these commands produce the same result ''' # check inputs if len(args) == 0: raise ValueError( '\nNeed to provide a spectrum object or filename for plotStandardSequence' ) parameters = ['type_range'] splat.checkKeys(kwargs, parameters, forcekey=False) type_range = kwargs.get('type_range', 2) kwargs['stack'] = kwargs.get('stack', 0.5) # kwargs['legendLocation']=kwargs.get('legendLocation','outside') kwargs['telluric'] = kwargs.get('telluric', True) kwargs['method'] = kwargs.get('method', 'kirkpatrick') # kwargs['color']=kwargs.get('color','r') kwargs['colorComparison'] = kwargs.get('colorComparison', 'k') kwargs['colorScheme'] = kwargs.get('colorScheme', 'winter') kwargs['figsize'] = kwargs.get('figsize', [10, 10 * type_range * kwargs['stack']]) kwargs['fontscale'] = kwargs.get('fontscale', 1.5) # process input if isinstance(args[0], str): if len(glob.glob(args[0])) == 0: raise ValueError( '\nCannot find input file {} - make sure full path is included' .format(args[0])) try: sp = splat.Spectrum(file=args[0]) except: raise ValueError( '\nCould not read in file {} - make sure the file is correctly formatted' .format(args[0])) elif isinstance(args[0], splat.Spectrum): sp = copy.deepcopy(args[0]) else: raise ValueError('\nInput should be a Spectrum object or filename') sp.normalize() # classify by comparison to standards spt = kwargs.get('spt', splat.classifyByStandard(sp, **kwargs)[0]) if not isinstance(spt, str): spt = splat.typeToNum(spt) # produce range of standards for plot stdnum = numpy.arange(numpy.floor(splat.typeToNum(spt) - type_range), numpy.ceil(splat.typeToNum(spt) + type_range) + 1) if numpy.max(stdnum) > 39: stdnum -= (numpy.max(stdnum) - 39) if numpy.min(stdnum) < 10: stdnum += (10 - numpy.min(stdnum)) stdspt = [splat.typeToNum(i) for i in stdnum] stds = [splat.SPEX_STDS[s] for s in stdspt] stdlabels = ['{} Standard'.format(s) for s in stdspt] plotlist = [] labels = [] colors = [] for i, stdsp in enumerate(stds): plotlist.append([stdsp, sp]) labels.extend([]) fig = splat.plotSpectrum(stds, comparison=sp, labels=stdlabels, **kwargs) return fig
def plotBatch(*args, **kwargs): ''' :Purpose: Plots a batch of spectra into a 2x2 set of PDF files, with options of overplotting comparison spectra, including best-match spectral standards. :param input: A single or list of Spectrum objects or filenames, or the glob search string for a set of files (e.g., '/Data/myspectra/*.fits'). :type input: required :param output: Filename for PDF file output; full path should be include if not saving to current directory :type output: optional, default = 'spectra_plot.pdf' :param comparisons: list of Spectrum objects or filenames for comparison spectra. If comparisons list is shorter than source list, then last comparison source will be repeated. If the comparisons list is longer, the list will be truncated. :type comparisons: optional, default = None :param classify: Set to True to classify sources based on comparison to MLT spectral standards following the method of `Kirkpatrick et al. (2010) <http://adsabs.harvard.edu/abs/2010ApJS..190..100K>`_. This option normalizes the spectra by default :type classify: optional, default = False :param normalize: Set to True to normalize source and (if passed) comparison spectra. :type normalize: optional, default = False :param legend: Set to list of legends for plots. The number of legends should equal the number of sources and comparisons (if passed) in an alternating sequence. T :type legend: optional, default = displays file name for source and object name for comparison (if passed) Relevant parameters for plotSpectrum may also be passed :Example: >>> import glob, splat >>> files = glob.glob('/home/mydata/*.fits') >>> sp = splat.plotBatch(files,classify=True,output='comparison.pdf') >>> sp = splat.plotBatch('/home/mydata/*.fits',classify=True,output='comparison.pdf') >>> sp = splat.plotBatch([splat.Spectrum(file=f) for f in files],classify=True,output='comparison.pdf') All three of these commands produce the same result ''' # keyword check parameters = ['output','comparisons','classify','normalize','legend'] splat.checkKeys(kwargs,parameters,forcekey=False) kwargs['layout']=kwargs.get('layout',[2,2]) kwargs['fontscale']=kwargs.get('fontscale',0.7) # input check if len(args) == 0: print('\nNeed to provide a list of spectra or filenames, or a file search string, to use plotBatch') return kwargs['output'] = kwargs.get('output','spectra_plot.pdf') # break out a list if isinstance(args[0],list): inputlist = args[0] else: inputlist = args # if filenames, read in each file to a spectrum object if isinstance(inputlist[0],str): # try a glob search string files = glob.glob(inputlist[0]) if len(files) > 1 or (len(files) == 1 and inputlist[0].find('*') != -1): inputlist = files # try reading in files into Spectrum object try: splist = [splat.Spectrum(file = f) for f in inputlist] except: raise ValueError('\nCould not read in list of files - make sure the full path is specified and the files are correctly formatted') # if filenames, read in each file to a spectrum object elif isinstance(inputlist[0],splat.Spectrum): splist = copy.deepcopy(inputlist) else: print('\nInput should be list of Spectra objects or filenames') return # comparison files are present compflag = False complist = [] if kwargs.get('comparisons',False) != False: comp = kwargs.get('comparisons') if not isinstance(comp,list): comp = [comp] if len(comp) < len(splist): while len(comp) < len(splist): comp.append(comp[-1]) if isinstance(comp[0],str): try: complist = [splat.Spectrum(file = f) for f in comp] compflag = True except: print('\nCould not read in comparison files: ignoring comparisons') if isinstance(comp[0],splat.Spectrum): complist = comp compflag = True # set comparison files to be standards for spectral classification if kwargs.get('classify',False): complist = [] for sp in splist: spt = splat.classifyByStandard(sp,method='kirkpatrick') complist.append(splat.SPEX_STDS[spt[0]]) compflag = True kwargs['normalize'] = kwargs.get('normalize',True) # normalize if desired if kwargs.get('normalize',False): tmp = [sp.normalize() for sp in splist] if compflag == True: tmp = [sp.normalize() for sp in complist] # prep for plotting plotlist = [] clist = [] for i,sp in enumerate(splist): if compflag == True: plotlist.append([sp,complist[i]]) clist.extend(['k','r']) else: plotlist.append([sp]) clist.extend(['k']) # manage legends if kwargs.get('legend',False) != False: legends = kwargs.get('legend') if not isinstance(legends,list): legends = [legends] if len(legends) < (len(splist)+len(complist)): # guess: just left out the comparison legends if len(complist) > 0 and len(legends) == len(splist): legtmp = [] for i,l in enumerate(legends): legtmp.extend([l,'{}'.format(complist[i].name)]) legends = legtmp else: # otherwise: pad the remaining legends with the last legend (pairs) while len(legends) < (len(splist)+len(complist)): if compflag: legends.extend([legends[-2],legends[-1]]) else: legends.extend([legends[-1]]) if len(legends) > (len(splist)+len(complist)): legends = legends[0:(len(splist)+len(complist))] else: legends = [] for i,sp in enumerate(splist): if compflag == True: legends.extend(['{}'.format(os.path.basename(sp.filename)),'{}'.format(complist[i].name)]) else: legends.extend(['{}'.format(os.path.basename(sp.filename))]) # generate pdf splat.plotSpectrum(plotlist,multiplot=True,multipage=True,legends=legends,colors=clist,**kwargs) return
def plotBatch(*args, **kwargs): ''' :Purpose: Plots a batch of spectra into a 2x2 set of PDF files, with options of overplotting comparison spectra, including best-match spectral standards. :param input: A single or list of Spectrum objects or filenames, or the glob search string for a set of files (e.g., '/Data/myspectra/*.fits'). :type input: required :param output: Filename for PDF file output; full path should be include if not saving to current directory :type output: optional, default = 'spectra_plot.pdf' :param comparisons: list of Spectrum objects or filenames for comparison spectra. If comparisons list is shorter than source list, then last comparison source will be repeated. If the comparisons list is longer, the list will be truncated. :type comparisons: optional, default = None :param classify: Set to True to classify sources based on comparison to MLT spectral standards following the method of `Kirkpatrick et al. (2010) <http://adsabs.harvard.edu/abs/2010ApJS..190..100K>`_. This option normalizes the spectra by default :type classify: optional, default = False :param normalize: Set to True to normalize source and (if passed) comparison spectra. :type normalize: optional, default = False :param legend: Set to list of legends for plots. The number of legends should equal the number of sources and comparisons (if passed) in an alternating sequence. T :type legend: optional, default = displays file name for source and object name for comparison (if passed) Relevant parameters for plotSpectrum may also be passed :Example: >>> import glob, splat >>> files = glob.glob('/home/mydata/*.fits') >>> sp = splat.plotBatch(files,classify=True,output='comparison.pdf') >>> sp = splat.plotBatch('/home/mydata/*.fits',classify=True,output='comparison.pdf') >>> sp = splat.plotBatch([splat.Spectrum(file=f) for f in files],classify=True,output='comparison.pdf') All three of these commands produce the same result ''' # keyword check parameters = ['output', 'comparisons', 'classify', 'normalize', 'legend'] splat.checkKeys(kwargs, parameters, forcekey=False) kwargs['layout'] = kwargs.get('layout', [2, 2]) kwargs['fontscale'] = kwargs.get('fontscale', 0.7) # input check if len(args) == 0: print( '\nNeed to provide a list of spectra or filenames, or a file search string, to use plotBatch' ) return kwargs['output'] = kwargs.get('output', 'spectra_plot.pdf') # break out a list if isinstance(args[0], list): inputlist = args[0] else: inputlist = args # if filenames, read in each file to a spectrum object if isinstance(inputlist[0], str): # try a glob search string files = glob.glob(inputlist[0]) if len(files) > 1 or (len(files) == 1 and inputlist[0].find('*') != -1): inputlist = files # try reading in files into Spectrum object try: splist = [splat.Spectrum(file=f) for f in inputlist] except: raise ValueError( '\nCould not read in list of files - make sure the full path is specified and the files are correctly formatted' ) # if filenames, read in each file to a spectrum object elif isinstance(inputlist[0], splat.Spectrum): splist = copy.deepcopy(inputlist) else: print('\nInput should be list of Spectra objects or filenames') return # comparison files are present compflag = False complist = [] if kwargs.get('comparisons', False) != False: comp = kwargs.get('comparisons') if not isinstance(comp, list): comp = [comp] if len(comp) < len(splist): while len(comp) < len(splist): comp.append(comp[-1]) if isinstance(comp[0], str): try: complist = [splat.Spectrum(file=f) for f in comp] compflag = True except: print( '\nCould not read in comparison files: ignoring comparisons' ) if isinstance(comp[0], splat.Spectrum): complist = comp compflag = True # set comparison files to be standards for spectral classification if kwargs.get('classify', False): complist = [] for sp in splist: spt = splat.classifyByStandard(sp, method='kirkpatrick') complist.append(splat.SPEX_STDS[spt[0]]) compflag = True kwargs['normalize'] = kwargs.get('normalize', True) # normalize if desired if kwargs.get('normalize', False): tmp = [sp.normalize() for sp in splist] if compflag == True: tmp = [sp.normalize() for sp in complist] # prep for plotting plotlist = [] clist = [] for i, sp in enumerate(splist): if compflag == True: plotlist.append([sp, complist[i]]) clist.extend(['k', 'r']) else: plotlist.append([sp]) clist.extend(['k']) # manage legends if kwargs.get('legend', False) != False: legends = kwargs.get('legend') if not isinstance(legends, list): legends = [legends] if len(legends) < (len(splist) + len(complist)): # guess: just left out the comparison legends if len(complist) > 0 and len(legends) == len(splist): legtmp = [] for i, l in enumerate(legends): legtmp.extend([l, '{}'.format(complist[i].name)]) legends = legtmp else: # otherwise: pad the remaining legends with the last legend (pairs) while len(legends) < (len(splist) + len(complist)): if compflag: legends.extend([legends[-2], legends[-1]]) else: legends.extend([legends[-1]]) if len(legends) > (len(splist) + len(complist)): legends = legends[0:(len(splist) + len(complist))] else: legends = [] for i, sp in enumerate(splist): if compflag == True: legends.extend([ '{}'.format(os.path.basename(sp.filename)), '{}'.format(complist[i].name) ]) else: legends.extend(['{}'.format(os.path.basename(sp.filename))]) # generate pdf splat.plotSpectrum(plotlist, multiplot=True, multipage=True, legends=legends, colors=clist, **kwargs) return