def main(libpath, modeldir, overwrite): ext = os.path.splitext(libpath)[-1].lower() if ext == '.csv': params = pd.read_csv(libpath) maskpath = libpath[:-4] + '_mask.csv' # Create mask dataframe param_mask = pd.DataFrame() for p in library.Library.STAR_PROPS: param_mask[p] = np.isfinite(params[p]) param_mask['u_' + p] = np.isfinite(params[p]) params = get_isochrone_params(params, modeldir, overwrite) params.to_csv(libpath) param_mask.to_csv(maskpath) elif ext == '.h5': lib = library.read_hdf(libpath) param_mask = pd.DataFrame() for p in library.Library.STAR_PROPS: param_mask[p] = np.isfinite(lib.library_params[p]) param_mask['u_' + p] = np.isfinite(lib.library_params[p]) lib.library_params = get_isochrone_params(lib.library_params, modeldir, overwrite) lib.param_mask = param_mask lib.to_hdf(libpath) else: print("Library file was not in a valid format.") sys.exit(1)
def main(libpath, modeldir, overwrite): ext = os.path.splitext(libpath)[-1].lower() if ext == '.csv': params = pd.read_csv(libpath) maskpath = libpath[:-4] + '_mask.csv' # Create mask dataframe param_mask = pd.DataFrame() for p in library.Library.STAR_PROPS: param_mask[p] = np.isfinite(params[p]) param_mask['u_'+p] = np.isfinite(params[p]) params = get_isochrone_params(params, modeldir, overwrite) params.to_csv(libpath) param_mask.to_csv(maskpath) elif ext == '.h5': lib = library.read_hdf(libpath) param_mask = pd.DataFrame() for p in library.Library.STAR_PROPS: param_mask[p] = np.isfinite(lib.library_params[p]) param_mask['u_'+p] = np.isfinite(lib.library_params[p]) lib.library_params = get_isochrone_params(lib.library_params, modeldir, overwrite) lib.param_mask = param_mask lib.to_hdf(libpath) else: print("Library file was not in a valid format.") sys.exit(1)
def main(append, parampath, specdir, outdir): library_params = pd.read_csv(parampath, index_col=0) library_params['lib_index'] = None # Read in NSO spectrum nso = spectrum.read_fits(os.path.join(specdir, 'nso_adj.fits')) nso = nso.cut(*WAVLIM) # Use NSO wavelength scale as library wavelength scale wav = nso.w # Read in all spectra spectra = np.empty((0, 3, len(wav))) for idx, row in library_params.iterrows(): lib_obs = row['lib_obs'] spec_path = os.path.join(specdir, lib_obs+'_adj.fits') spec = spectrum.read_fits(spec_path).cut(*WAVLIM) # Check that shifted spectra are on same wavelength scale if not np.allclose(spec.w, wav): print("Spectrum {0} was not on the same wavelength scale".format( row.lib_obs)) library_params.loc[idx, 'lib_obs'] = None continue # Add spectrum to library library_params.loc[idx, 'lib_index'] = len(spectra) spectra = np.vstack((spectra, [[spec.s, spec.serr, spec.mask]])) # Read in parameter mask maskpath = parampath[:-4] + '_mask.csv' if os.path.exists(maskpath): param_mask = pd.read_csv(maskpath, index_col=0) else: param_mask = pd.DataFrame() for p in library.Library.STAR_PROPS: param_mask[p] = np.isfinite(library_params[p]) param_mask['u_'+p] = np.isfinite(library_params[p]) # Drop stars with missing spectra missing_spectra = pd.isnull(library_params.lib_obs) missing_indices = library_params[missing_spectra].index.tolist() print("Dropping {0:d} stars with no spectra".format(len(missing_indices))) library_params.drop(missing_indices, inplace=True) param_mask.drop(missing_indices, inplace=True) libpath = os.path.join(outdir, 'library.h5') if append: lib = library.read_hdf(libpath) lib.append(library_params, spectra, param_mask) else: lib = library.Library(wav, spectra, library_params, wavlim=WAVLIM, param_mask=param_mask, nso=nso) # Get allowed shift references shift_obs = [row[0] for r in SHIFT_REFERENCES] lib.header['shift_refs'] = shift_obs # Save library lib.to_hdf(libpath)
def specmatch_spectrum(args): lib_subset = None if args.debug: lib = library.read_hdf(wavlim='none') nspec = len(lib.library_params) lib_subset = np.random.choice(np.arange(nspec), size=args.n_lib_subset, replace=False) lib_subset = np.sort(lib_subset) lib_subset = list(lib_subset) core.specmatch_spectrum(args.spectrum, plot_level=args.plots, inlib=args.in_library, outdir=args.outdir, num_best=args.num_best, suffix=args.suffix, lib_subset=lib_subset)
def main(libpath, targ_name, outpath): lib = library.read_hdf(libpath) targ_idx = lib.get_index(targ_name) targ_param, targ_spec = lib[targ_idx] results = lib.library_params.copy() for wl in range(WAVLIM[0], WAVLIM[1], WAVSTEP): print("Matching region {0:d} - {1:d}".format(wl, wl + 100)) sm = specmatch.SpecMatch(targ_spec, lib, (wl, wl + WAVSTEP)) sm.match(ignore=targ_idx) cs_col = 'chi_squared_{0:d}'.format(wl) results[cs_col] = sm.match_results['chi_squared'] fit_col = 'fit_params_{0:d}'.format(wl) results[fit_col] = sm.match_results['fit_params'] results.to_csv(outpath)
def main(libpath, targ_name, outpath): lib = library.read_hdf(libpath) targ_idx = lib.get_index(targ_name) targ_param, targ_spec = lib[targ_idx] results = lib.library_params.copy() for wl in range(WAVLIM[0], WAVLIM[1], WAVSTEP): print("Matching region {0:d} - {1:d}".format(wl, wl+100)) sm = specmatch.SpecMatch(targ_spec, lib, (wl, wl+WAVSTEP)) sm.match(ignore=targ_idx) cs_col = 'chi_squared_{0:d}'.format(wl) results[cs_col] = sm.match_results['chi_squared'] fit_col = 'fit_params_{0:d}'.format(wl) results[fit_col] = sm.match_results['fit_params'] results.to_csv(outpath)
def main(libpath, targ_name, respath, outpath, num_best): lib = library.read_hdf(libpath) targ_idx = lib.get_index(targ_name) targ_param, targ_spec = lib[targ_idx] res_match = pd.read_csv(respath, index_col=0) res_lincomb = targ_param.to_frame().transpose() for wl in range(WAVLIM[0], WAVLIM[1], WAVSTEP): matchcs_col = 'chi_squared_{0:d}'.format(wl) res_match.sort_values(by=matchcs_col, inplace=True) matchfit_col = 'fit_params_{0:d}'.format(wl) sm = specmatch.SpecMatch(targ_spec, lib, (wl,wl+100), num_best) sm.match(match_results=res_match.rename(\ columns={matchcs_col:'chi_squared', matchfit_col:'fit_params'})) bestcs_col = 'best_cs_lincomb{0:d}_{1:d}'.format(num_best, wl) bestcs = np.array(res_match.head(num_best)[matchcs_col]) res_lincomb[bestcs_col] = json.dumps(bestcs.tolist()) refidxs_col = 'ref_idxs_lincomb{0:d}_{1:d}'.format(num_best, wl) ref_idxs = np.array(res_match.head(num_best).index) res_lincomb[refidxs_col] = json.dumps(ref_idxs.tolist()) coeffs_col = 'coeffs_lincomb{0:d}_{1:d}'.format(num_best, wl) coeffs = np.array(match.get_lincomb_coeffs(sm.mt_lincomb.best_params)) res_lincomb[coeffs_col] = json.dumps(coeffs.tolist()) cs_col = 'chi_squared_lincomb{0:d}_{1:d}'.format(num_best, wl) res_lincomb[cs_col] = sm.mt_lincomb.best_chisq fit_col = 'fit_params_lincomb{0:d}_{1:d}'.format(num_best, wl) res_lincomb[fit_col] = sm.mt_lincomb.best_params.dumps() res_lincomb.to_csv(outpath)
from specmatchemp import library if __name__ == '__main__': # Argument parser psr = ArgumentParser(description="Combine match results") psr.add_argument('libpath', type=str, help="Path to library file") psr.add_argument('resdir', type=str, help="Path to results directory") psr.add_argument('-s', '--suffix', type=str, default="", help="Suffix to append to results files") args = psr.parse_args() if not os.path.isdir(args.resdir): print("Could not find folder at {0}".format(args.resdir)) sys.exit(1) lib = library.read_hdf(args.libpath, wavlim='none') # get names of stars names = list(lib.library_params.cps_name) # global dataframe to store res_global = pd.DataFrame() min_num = 2 max_num = 9 for name in names: # combine results as one row for each star. res_star = pd.DataFrame() for num_best in range(min_num, max_num): res_path = os.path.join(args.resdir, '{0}/{0}_lincomb{1:d}.csv'.format(name, num_best))
def main(resdir, suffix, plots): lib = library.read_hdf(wavlim='none') # Create global dataframe to store results match_res_global = pd.DataFrame() lincomb_res_global = pd.DataFrame() for idx, row in lib.library_params.iterrows(): targ_name = row['cps_name'] res_path = os.path.join( resdir, targ_name + '/' + targ_name + args.suffix + '_lincomb_sm.hdf') if not os.path.exists(res_path): res_path = os.path.join( resdir, targ_name + '/' + targ_name + args.suffix + '_sm.hdf') if not os.path.exists(res_path): print("Results file for {0} not found!".format(targ_name)) continue print("Reading in results for {0}".format(targ_name)) sm = specmatch.SpecMatch.read_hdf(res_path, lib) # Read in library grid search results sm.match_results['targ_idx'] = idx sm.match_results['ref_idx'] = sm.match_results.index # Concatenate library grid search results if match_res_global.empty: match_res_global = sm.match_results else: match_res_global = pd.concat((match_res_global, sm.match_results), ignore_index=True) if lincomb_res_global.empty: # Create global results dataframe containing final lincomb # chi-squared and generated results lincomb_res_global = lib.library_params.copy() for i in range(len(sm.lincomb_regions)): reg = sm.lincomb_regions[i] suf = '_lincomb_{0:.0f}'.format(reg[0]) cs_col = 'chi_squared' + suf lincomb_res_global[cs_col] = np.nan for p in library.Library.STAR_PROPS: p_col = p + suf lincomb_res_global[p_col] = np.nan suf = '_lincomb' for p in library.Library.STAR_PROPS: p_col = p + suf p_nodetrend_col = p + '_nodetrend' + suf lincomb_res_global[p_col] = np.nan lincomb_res_global['u_' + p_col] = np.nan lincomb_res_global[p_nodetrend_col] = np.nan # Read in lincomb results for each wavelength region for i in range(len(sm.lincomb_regions)): mt = sm.lincomb_matches[i] reg = sm.lincomb_regions[i] suf = '_lincomb_{0:.0f}'.format(reg[0]) cs_col = 'chi_squared' + suf lincomb_res_global.loc[idx, cs_col] = mt.best_chisq for p in library.Library.STAR_PROPS: p_col = p + suf lincomb_res_global.loc[idx, p_col] = sm.lincomb_results[i][p] # Read in averaged results suf = '_lincomb' for p in library.Library.STAR_PROPS: p_col = p + suf p_nodetrend_col = p + '_nodetrend' + suf lincomb_res_global.loc[idx, p_col] = sm.results[p] lincomb_res_global.loc[idx, 'u_' + p_col] = sm.results['u_' + p] lincomb_res_global.loc[idx, p_nodetrend_col] = \ sm.results_nodetrend[p] # Save results outpath = os.path.join(resdir, 'match_results' + suffix + '.csv') match_res_global.index.names = ['idx'] match_res_global.to_csv(outpath) outpath = os.path.join(resdir, 'lincomb_results' + suffix + '.csv') lincomb_res_global.to_csv(outpath) if plots: # Plots for individual regions plotspath = os.path.join(resdir, 'library_plots_regions.pdf') with PdfPages(plotspath) as pdf: cols = list(lincomb_res_global.columns) cscols = cscols = [ c for c in cols if re.search('chi_squared_lincomb_\d+', c) ] regions = list( set([ re.search('chi_squared_lincomb_(\d+)$', c).group(1) for c in cscols ])) for reg in regions: reg = float(reg) suf = '_lincomb_{0:.0f}'.format(reg) title = 'Wavelength region: {0:.0f} - {1:.0f} A'\ .format(reg, reg + 100) plot_diag(lincomb_res_global, suf, pdf, title) # Plots for nodetrend parameters plotspath = os.path.join(resdir, 'library_plots_nodetrend.pdf') with PdfPages(plotspath) as pdf: suf = '_nodetrend_lincomb' title = 'Averaged over all wavelengths, not detrended' # Plot all stars plot_diag(lincomb_res_global, suf, pdf, title, trend=True) # Plots for detrended parameters plotspath = os.path.join(resdir, 'library_plots_lincomb.pdf') with PdfPages(plotspath) as pdf: suf = '_lincomb' title = 'Averaged over all wavelengths, detrended' # Plot all stars plot_diag(lincomb_res_global, suf, pdf, title)
def main(append, parampath, specdir, outdir): library_params = pd.read_csv(parampath, index_col=0) library_params['lib_index'] = None # Read in NSO spectrum nso = spectrum.read_fits(os.path.join(specdir, 'nso_adj.fits')) nso = nso.cut(*WAVLIM) # Use NSO wavelength scale as library wavelength scale wav = nso.w # Read in all spectra spectra = np.empty((0, 3, len(wav))) for idx, row in library_params.iterrows(): lib_obs = row['lib_obs'] spec_path = os.path.join(specdir, lib_obs + '_adj.fits') spec = spectrum.read_fits(spec_path).cut(*WAVLIM) # Check that shifted spectra are on same wavelength scale if not np.allclose(spec.w, wav): print("Spectrum {0} was not on the same wavelength scale".format( row.lib_obs)) library_params.loc[idx, 'lib_obs'] = None continue # Add spectrum to library library_params.loc[idx, 'lib_index'] = len(spectra) spectra = np.vstack((spectra, [[spec.s, spec.serr, spec.mask]])) # Read in parameter mask maskpath = parampath[:-4] + '_mask.csv' if os.path.exists(maskpath): param_mask = pd.read_csv(maskpath, index_col=0) else: param_mask = pd.DataFrame() for p in library.Library.STAR_PROPS: param_mask[p] = np.isfinite(library_params[p]) param_mask['u_' + p] = np.isfinite(library_params[p]) # Drop stars with missing spectra missing_spectra = pd.isnull(library_params.lib_obs) missing_indices = library_params[missing_spectra].index.tolist() print("Dropping {0:d} stars with no spectra".format(len(missing_indices))) library_params.drop(missing_indices, inplace=True) param_mask.drop(missing_indices, inplace=True) libpath = os.path.join(outdir, 'library.h5') if append: lib = library.read_hdf(libpath) lib.append(library_params, spectra, param_mask) else: lib = library.Library(wav, spectra, library_params, wavlim=WAVLIM, param_mask=param_mask, nso=nso) # Get allowed shift references shift_obs = [row[0] for r in SHIFT_REFERENCES] lib.header['shift_refs'] = shift_obs # Save library lib.to_hdf(libpath)
psr.add_argument('formatstring', type=str, help="Format string") psr.add_argument('cols', nargs='*', type=str, help="Columns to use") psr.add_argument('-a', '--append', action='store_const', const='a+', default="w+", help="Append to existing script") args = psr.parse_args() if not os.path.isfile(args.libpath): print("Could not find {0}".format(args.libpath)) sys.exit(1) if os.path.splitext(args.libpath)[1] == ".h5": lib = library.read_hdf(args.libpath, wavlim='none') df = lib.library_params elif os.path.splitext(args.libpath)[1] == ".csv": df = pd.read_csv(args.libpath) else: print("{0} was not a valid library format".format(args.libpath)) scriptfile = open(args.outpath, args.append) for idx, row in df.iterrows(): s = "source ~/.bash_profile; " s += args.formatstring.format(*(row[args.cols].values)) s += "\n" scriptfile.write(s) scriptfile.close()
def specmatch_spectrum(specpath, plot_level=0, inlib=False, outdir="./", num_best=5, suffix="", wavlim='all', lib_subset=None, name=None, n_lib_subset=None): """Perform the specmatch on a given spectrum Args: specpath (str): Path to target spectrum plot_level (int, 0-2): Level of plots 0 - No plots saved, 1 - Representative plots, 2 - All plots inlib (str or False): String to search within library for to exclude from matching process outdir (str): Output file directory num_best (int): Number of best matches to use at lincomb stage suffix (str): String to append to output file names Returns: specmatch.SpecMatch object """ if not os.path.exists(specpath): raise ValueError(specpath + " does not exist!") if wavlim == 'all': target = spectrum.read_hires_fits(specpath) else: target = spectrum.read_hires_fits(specpath).cut(*wavlim) # Determine the name of the target if inlib: name = inlib elif name is None: name = os.path.basename(specpath)[:-5] if n_lib_subset is not None: lib = library.read_hdf(wavlim='none') lib_subset = lib.library_params.lib_index lib_subset = np.random.choice( lib_subset, size=n_lib_subset, replace=False ) lib = library.read_hdf(wavlim=wavlim, lib_index_subset=lib_subset) sm = specmatch.SpecMatch(target, lib) sm.shift() if inlib: targ_idx = lib.get_index(inlib) targ_param, targ_spec = lib[targ_idx] sm.match(ignore=targ_idx, wavlim=wavlim) else: targ_param = None sm.match(wavlim=wavlim) sm.target.name = name # attach target name sm.lincomb(num_best) # Print results print("SpecMatch Results for {0}".format(name)) sm.results_to_txt(sys.stdout) outdir = os.path.join(outdir, name) if not os.path.exists(outdir): os.makedirs(outdir) # Save final results outpath = os.path.join(outdir, name + suffix + '_results.txt') with open(outpath, 'w') as f: if inlib: f.write('Library Parameters\n') f.write('------------------\n') f.write('Teff: {0:.0f} +/- {1:.0f} K\n'.format( targ_param['Teff'], targ_param['u_Teff'])) f.write('Radius: {0:.3f} +/- {1:.3f} Rsun\n'.format( targ_param['radius'], targ_param['u_radius'])) f.write('[Fe/H]: {0:.2f} +/- {1:.2f} dex\n'.format( targ_param['feh'], targ_param['u_feh'])) f.write('\n') sm.results_to_txt(f, verbose=True) print("created {}".format(outpath)) # Save full results outpath = os.path.join(outdir, name + suffix + '_sm.hdf') sm.to_hdf(outpath) print("created {}".format(outpath)) # Create representative plots if plot_level is not None and plot_level > 0: plotspath = os.path.join(outdir, name + suffix + '_plots.pdf') with PdfPages(plotspath) as pdf: region = (5100, 5200) wavlim = (5160, 5190) order = 2 plot_shifts(sm, pdf, order, wavlim) plot_match(sm, pdf, region, wavlim, targ_param) plot_lincomb(sm, pdf, region, wavlim, targ_param) print("created {}".format(plotspath)) # Create full plots if plot_level == 2: shiftplotspath = os.path.join(outdir, name + suffix + '_shift_plots.pdf') with PdfPages(shiftplotspath) as pdf: for order in range(np.shape(sm.target_unshifted.w)[0]): plot_shifts(sm, pdf, order, wavlim='all') matchplotspath = os.path.join(outdir, name + suffix + '_match_plots.pdf') with PdfPages(matchplotspath) as pdf: for reg in sm.regions: plot_match(sm, pdf, reg, wavlim='all', targ_param=targ_param) lincombplotspath = os.path.join(outdir, name + suffix + '_lincomb_plots.pdf') with PdfPages(lincombplotspath) as pdf: for reg in sm.lincomb_regions: plot_lincomb(sm, pdf, reg, wavlim='all', targ_param=targ_param) print("created {}".format(plotspath)) return sm
def match_spectrum(specpath, indir="./", plot_level=0, inlib=False, outdir="./", suffix=""): """Match a spectrum given its observation ID Args: specpath (str): Path to spectrum or its CPS observation id jXX.XXXX indir (str): Directory to look in for target spectrum plot_level (int, 0-2): Level of plotting to save inlib (str or False): String to search within library for to exclude from matching process outdir (str): Output file directory suffix (str): String to append to output file names Returns: specmatch.SpecMatch object """ # Check if specpath is a path or an observation ID if os.path.exists(specpath): targ_path = specpath targid = os.path.splitext(os.path.basename(specpath))[0] else: targ_path = os.path.join(indir, 'r' + specpath + '_adj' + suffix + '.fits') if not os.path.exists(targ_path): raise ValueError(specpath + " does not exist!") targid = 'r' + specpath # Load shifted spectrum target = spectrum.read_fits(targ_path) lib = library.read_hdf() sm = specmatch.SpecMatch(target, lib) if inlib: name = inlib sm.target.name = inlib sm.target.attrs['obs'] = targid targ_idx = lib.get_index(inlib) targ_param, targ_spec = lib[targ_idx] sm.match(ignore=targ_idx) else: name = targid targ_param = None sm.match() # Save results outdir = os.path.join(outdir, name) if not os.path.exists(outdir): os.mkdir(outdir) outpath = os.path.join(outdir, name + suffix + '_match.csv') sm.match_results.to_csv(outpath) # Save SpecMatch object outpath = os.path.join(outdir, name + suffix + '_sm.hdf') sm.to_hdf(outpath) # Generate representative plots if plot_level == 1: plotspath = os.path.join(outdir, name + suffix + '_match_plots.pdf') with PdfPages(plotspath) as pdf: region = (5100, 5200) wavlim = (5160, 5190) plot_match(sm, pdf, region, wavlim, targ_param) # Generate full plots if plot_level == 2: plotspath = os.path.join(outdir, name + suffix + '_match_plots.pdf') with PdfPages(plotspath) as pdf: for reg in sm.regions: plot_match(sm, pdf, reg, wavlim='all', targ_param=targ_param) return sm
def lincomb_spectrum(respath, plot_level=0, inlib=False, outdir="./", num_best=5, suffix=""): """Match a spectrum using the linear combination approach. Can only be used to resume an existing SpecMatch object. Args: respath (str): Path to existing SpecMatch.hdf file plot_level (int, 0-2): Level of plotting to save inlib (str or False): String to search within library for to exclude from matching process outdir (str): Output file directory num_best (int): Number of best matches to use at lincomb stage suffix (str): String to append to output file names Returns: specmatch.SpecMatch object """ lib = library.read_hdf() sm = specmatch.SpecMatch.read_hdf(respath, lib) name = sm.target.name if inlib: targ_idx = lib.get_index(inlib) targ_param, targ_spec = lib[targ_idx] else: targ_param = None sm.lincomb(num_best=num_best) # Print results print("SpecMatch Results for {0}".format(name)) for p in library.Library.STAR_PROPS: print("{0}: {1:.2f}".format(p, sm.results[p])) outdir = os.path.join(outdir, name) if not os.path.exists(outdir): os.mkdir(outdir) # Save final results outpath = os.path.join(outdir, name + suffix + '_results.txt') with open(outpath, 'w') as f: if inlib: f.write('Library Parameters\n') f.write('------------------\n') f.write('Teff: {0:.0f} +/- {1:.0f} K\n'.format( targ_param['Teff'], targ_param['u_Teff'])) f.write('Radius: {0:.3f} +/- {1:.3f} Rsun\n'.format( targ_param['radius'], targ_param['u_radius'])) f.write('[Fe/H]: {0:.2f} +/- {1:.2f} dex\n'.format( targ_param['feh'], targ_param['u_feh'])) f.write('\n') sm.results_to_txt(f, verbose=True) # Save full results outpath = os.path.join(outdir, name + suffix + '_lincomb_sm.hdf') sm.to_hdf(outpath) # Plot results if plot_level == 1: plotspath = os.path.join(outdir, name + suffix + '_lincomb_plots.pdf') with PdfPages(plotspath) as pdf: region = (5100, 5200) wavlim = (5160, 5190) plot_lincomb(sm, pdf, region, wavlim, targ_param) if plot_level == 2: plotspath = os.path.join(outdir, name + suffix + '_lincomb_plots.pdf') with PdfPages(plotspath) as pdf: for reg in sm.lincomb_regions: plot_lincomb(sm, pdf, reg, wavlim='all', targ_param=targ_param) return sm
def specmatch_spectrum(specpath, plot_level=0, inlib=False, outdir="./", num_best=5, suffix="", wavlim='all', lib_subset=None, name=None, n_lib_subset=None): """Perform the specmatch on a given spectrum Args: specpath (str): Path to target spectrum plot_level (int, 0-2): Level of plots 0 - No plots saved, 1 - Representative plots, 2 - All plots inlib (str or False): String to search within library for to exclude from matching process outdir (str): Output file directory num_best (int): Number of best matches to use at lincomb stage suffix (str): String to append to output file names Returns: specmatch.SpecMatch object """ if not os.path.exists(specpath): raise ValueError(specpath + " does not exist!") if wavlim == 'all': target = spectrum.read_hires_fits(specpath) else: target = spectrum.read_hires_fits(specpath).cut(*wavlim) # Determine the name of the target if inlib: name = inlib elif name is None: name = os.path.basename(specpath)[:-5] if n_lib_subset is not None: lib = library.read_hdf(wavlim='none') lib_subset = lib.library_params.lib_index lib_subset = np.random.choice(lib_subset, size=n_lib_subset, replace=False) lib = library.read_hdf(wavlim=wavlim, lib_index_subset=lib_subset) sm = specmatch.SpecMatch(target, lib) sm.shift() if inlib: targ_idx = lib.get_index(inlib) targ_param, targ_spec = lib[targ_idx] sm.match(ignore=targ_idx, wavlim=wavlim) else: targ_param = None sm.match(wavlim=wavlim) sm.target.name = name # attach target name sm.lincomb(num_best) # Print results print("SpecMatch Results for {0}".format(name)) sm.results_to_txt(sys.stdout) outdir = os.path.join(outdir, name) if not os.path.exists(outdir): os.makedirs(outdir) # Save final results outpath = os.path.join(outdir, name + suffix + '_results.txt') with open(outpath, 'w') as f: if inlib: f.write('Library Parameters\n') f.write('------------------\n') f.write('Teff: {0:.0f} +/- {1:.0f} K\n'.format( targ_param['Teff'], targ_param['u_Teff'])) f.write('Radius: {0:.3f} +/- {1:.3f} Rsun\n'.format( targ_param['radius'], targ_param['u_radius'])) f.write('[Fe/H]: {0:.2f} +/- {1:.2f} dex\n'.format( targ_param['feh'], targ_param['u_feh'])) f.write('\n') sm.results_to_txt(f, verbose=True) print("created {}".format(outpath)) # Save full results outpath = os.path.join(outdir, name + suffix + '_sm.hdf') sm.to_hdf(outpath) print("created {}".format(outpath)) # Create representative plots if plot_level is not None and plot_level > 0: plotspath = os.path.join(outdir, name + suffix + '_plots.pdf') with PdfPages(plotspath) as pdf: region = (5100, 5200) wavlim = (5160, 5190) order = 2 plot_shifts(sm, pdf, order, wavlim) plot_match(sm, pdf, region, wavlim, targ_param) plot_lincomb(sm, pdf, region, wavlim, targ_param) print("created {}".format(plotspath)) # Create full plots if plot_level == 2: shiftplotspath = os.path.join(outdir, name + suffix + '_shift_plots.pdf') with PdfPages(shiftplotspath) as pdf: for order in range(np.shape(sm.target_unshifted.w)[0]): plot_shifts(sm, pdf, order, wavlim='all') matchplotspath = os.path.join(outdir, name + suffix + '_match_plots.pdf') with PdfPages(matchplotspath) as pdf: for reg in sm.regions: plot_match(sm, pdf, reg, wavlim='all', targ_param=targ_param) lincombplotspath = os.path.join(outdir, name + suffix + '_lincomb_plots.pdf') with PdfPages(lincombplotspath) as pdf: for reg in sm.lincomb_regions: plot_lincomb(sm, pdf, reg, wavlim='all', targ_param=targ_param) print("created {}".format(plotspath)) return sm
import numpy as np from collections import defaultdict from specmatchemp import library from specmatchemp import specmatch # Noise selection path lst = '/home/syee/specmatchemp-working/specmatchemp/tests/noise_selection.csv' lc = '/home/syee/specmatchemp-working/specmatchemp/results/lincomb_results.csv' res_path = '/home/syee/specmatchemp-working/specmatchemp/tests/resstudy_result.csv' resdir = '/home/syee/specmatchemp-working/specmatchemp/results/' RESOLUTION = [50000, 30000, 20000, 10000, 5000, 2500] if __name__ == '__main__': # Read library lib = library.read_hdf() # Get list of stars which were tested starlist = pd.read_csv(lst) # Open lincomb results lincomb_res_table = pd.read_csv(lc) for idx, row in starlist.iterrows(): star = row['cps_name'] # Get original lincomb result lc_res = lincomb_res_table.query('cps_name == "' + star + '"').iloc[0] suf = '_nodetrend_lincomb' lincomb_result = {} for p in library.Library.STAR_PROPS: p_col = p + suf starlist.loc[idx, p_col] = lc_res[p_col] lincomb_result[p] = lc_res[p_col]
def main(resdir, suffix, plots): lib = library.read_hdf(wavlim='none') # Create global dataframe to store results match_res_global = pd.DataFrame() lincomb_res_global = pd.DataFrame() for idx, row in lib.library_params.iterrows(): targ_name = row['cps_name'] res_path = os.path.join(resdir, targ_name + '/' + targ_name + args.suffix + '_lincomb_sm.hdf') if not os.path.exists(res_path): res_path = os.path.join(resdir, targ_name + '/' + targ_name + args.suffix + '_sm.hdf') if not os.path.exists(res_path): print("Results file for {0} not found!".format(targ_name)) continue print("Reading in results for {0}".format(targ_name)) sm = specmatch.SpecMatch.read_hdf(res_path, lib) # Read in library grid search results sm.match_results['targ_idx'] = idx sm.match_results['ref_idx'] = sm.match_results.index # Concatenate library grid search results if match_res_global.empty: match_res_global = sm.match_results else: match_res_global = pd.concat((match_res_global, sm.match_results), ignore_index=True) if lincomb_res_global.empty: # Create global results dataframe containing final lincomb # chi-squared and generated results lincomb_res_global = lib.library_params.copy() for i in range(len(sm.lincomb_regions)): reg = sm.lincomb_regions[i] suf = '_lincomb_{0:.0f}'.format(reg[0]) cs_col = 'chi_squared' + suf lincomb_res_global[cs_col] = np.nan for p in library.Library.STAR_PROPS: p_col = p + suf lincomb_res_global[p_col] = np.nan suf = '_lincomb' for p in library.Library.STAR_PROPS: p_col = p + suf p_nodetrend_col = p + '_nodetrend' + suf lincomb_res_global[p_col] = np.nan lincomb_res_global['u_'+p_col] = np.nan lincomb_res_global[p_nodetrend_col] = np.nan # Read in lincomb results for each wavelength region for i in range(len(sm.lincomb_regions)): mt = sm.lincomb_matches[i] reg = sm.lincomb_regions[i] suf = '_lincomb_{0:.0f}'.format(reg[0]) cs_col = 'chi_squared' + suf lincomb_res_global.loc[idx, cs_col] = mt.best_chisq for p in library.Library.STAR_PROPS: p_col = p + suf lincomb_res_global.loc[idx, p_col] = sm.lincomb_results[i][p] # Read in averaged results suf = '_lincomb' for p in library.Library.STAR_PROPS: p_col = p + suf p_nodetrend_col = p + '_nodetrend' + suf lincomb_res_global.loc[idx, p_col] = sm.results[p] lincomb_res_global.loc[idx, 'u_'+p_col] = sm.results['u_'+p] lincomb_res_global.loc[idx, p_nodetrend_col] = \ sm.results_nodetrend[p] # Save results outpath = os.path.join(resdir, 'match_results' + suffix + '.csv') match_res_global.index.names = ['idx'] match_res_global.to_csv(outpath) outpath = os.path.join(resdir, 'lincomb_results' + suffix + '.csv') lincomb_res_global.to_csv(outpath) if plots: # Plots for individual regions plotspath = os.path.join(resdir, 'library_plots_regions.pdf') with PdfPages(plotspath) as pdf: cols = list(lincomb_res_global.columns) cscols = cscols = [c for c in cols if re.search( 'chi_squared_lincomb_\d+', c)] regions = list(set([re.search('chi_squared_lincomb_(\d+)$', c) .group(1) for c in cscols])) for reg in regions: reg = float(reg) suf = '_lincomb_{0:.0f}'.format(reg) title = 'Wavelength region: {0:.0f} - {1:.0f} A'\ .format(reg, reg + 100) plot_diag(lincomb_res_global, suf, pdf, title) # Plots for nodetrend parameters plotspath = os.path.join(resdir, 'library_plots_nodetrend.pdf') with PdfPages(plotspath) as pdf: suf = '_nodetrend_lincomb' title = 'Averaged over all wavelengths, not detrended' # Plot all stars plot_diag(lincomb_res_global, suf, pdf, title, trend=True) # Plots for detrended parameters plotspath = os.path.join(resdir, 'library_plots_lincomb.pdf') with PdfPages(plotspath) as pdf: suf = '_lincomb' title = 'Averaged over all wavelengths, detrended' # Plot all stars plot_diag(lincomb_res_global, suf, pdf, title)