row_txt[11], row_txt[ 12] = f'{lineFlux:.3f}', f'{lineFluxErr:.3f}' row_txt[13] = percent_func_txt(obsFlux, lineFlux) if lineLabel in srPlus_lineLabels: i_line = sr_lineLabels.index(lineLabel) row_data[6] = r'${:.3f}\pm{:.3f}$'.format( mean_plus_line_values[i_line], std_plus_line_values[i_line]) row_data[6] += percent_func(obsFlux, mean_plus_line_values[i_line]) row_txt[14], row_txt[ 15] = f'{mean_plus_line_values[i_line]:.3f}', f'{std_plus_line_values[i_line]:.3f}' row_txt[16] = percent_func_txt(obsFlux, mean_plus_line_values[i_line]) pdf.addTableRow(row_data, last_row=False) tableDF.loc[lineLabel] = row_txt pdf.table.add_hline() table_file = tables_folder / f'{obj}_lineFitFluxComparison' pdf.generate_pdf(table_file, clean_tex=True) # Save the table as a dataframe. with open(f'{table_file}.txt', 'wb') as output_file: string_DF = tableDF.to_string() output_file.write(string_DF.encode('UTF-8'))
def table_line_fluxes(table_address, db_dict, combined_dict={}, file_type='table'): # Table headers headers = [ 'Line', 'Observed flux', 'Mean', 'Standard deviation', 'Median', r'$16^{th}$ $percentil$', r'$84^{th}$ $percentil$', r'$Difference\,\%$' ] # Create containers tableDF = pd.DataFrame(columns=headers[1:]) pdf = PdfPrinter() pdf.create_pdfDoc(pdf_type=file_type) pdf.pdfDoc.append( NoEscape('\definecolor{background}{rgb}{0.169, 0.169, 0.169}')) pdf.pdfDoc.append( NoEscape('\definecolor{foreground}{rgb}{0.702, 0.780, 0.847}')) pdf.pdfDoc.append(NoEscape(r'\arrayrulecolor{foreground}')) pdf.pdf_insert_table(headers, color_font='foreground', color_background='background') # Input data inputLabels = db_dict['Input_data']['lineLabels_list'] inFlux, inErr = db_dict['Input_data']['inputFlux_array'], db_dict[ 'Input_data']['inputErr_array'] # Output data flux_matrix = db_dict['trace']['calcFluxes_Op'] mean_line_values = flux_matrix.mean(axis=0) std_line_values = flux_matrix.std(axis=0) median_line_values = np.median(flux_matrix, axis=0) p16th_line_values = np.percentile(flux_matrix, 16, axis=0) p84th_line_values = np.percentile(flux_matrix, 84, axis=0) # Array wih true error values for flux diff_Percentage = np.round((1 - (median_line_values / inFlux)) * 100, 2) diff_Percentage = list(map(str, diff_Percentage)) ion_array, wave_array, latexLabel_array = label_decomposition( inputLabels, combined_dict=combined_dict) for i in range(inFlux.size): # label = label_formatting(inputLabels[i]) row_i = [ latexLabel_array[i], inFlux[i], mean_line_values[i], std_line_values[i], median_line_values[i], p16th_line_values[i], p84th_line_values[i], diff_Percentage[i] ] pdf.addTableRow( row_i, last_row=False if inputLabels[-1] != inputLabels[i] else True, color_font='foreground', color_background='background') tableDF.loc[inputLabels[i]] = row_i[1:] pdf.generate_pdf(table_address) # Save the table as a dataframe. with open(f'{table_address}.txt', 'wb') as output_file: string_DF = tableDF.to_string() output_file.write(string_DF.encode('UTF-8'))
flux_intg, flux_intgErr = lm.linesDF.loc[ lineLabel, 'intg_flux'], lm.linesDF.loc[lineLabel, 'intg_err'] flux_gauss, flux_gaussErr = lm.linesDF.loc[ lineLabel, 'gauss_flux'], lm.linesDF.loc[lineLabel, 'gauss_err'] eqw_entry = r'${:0.2f}$ $\pm$ ${:0.2f}$'.format(eqw, eqwErr) intgF_entry = r'${:0.2f}$ $\pm$ ${:0.2f}$'.format( flux_intg / flux_Hbeta, flux_intgErr / flux_Hbeta) gaussF_entry = r'${:0.2f}$ $\pm$ ${:0.2f}$'.format( flux_gauss / flux_Hbeta, flux_gaussErr / flux_Hbeta) if lm.linesDF.loc[lineLabel, 'blended_label'] == 'None': colorGrade = colorChooser(flux_intg / flux_gauss, 1.0) else: colorGrade = 'black' gaussF_color_entry = r'\textcolor{{{}}}{{{}}}'.format( colorGrade, gaussF_entry) lastRow_check = True if lineLabel == obsLines[-1] else False pdf.addTableRow( [label_entry, eqw_entry, intgF_entry, gaussF_color_entry], last_row=lastRow_check) # Plot the matched lines: lm.plot_line_mask_selection(lm.linesDF, ncols=5, output_address=figureLogAddress) # Generate the table pdf.generate_pdf(clean_tex=True)
wave_rest, flux, header = sr.import_fits_data(fitsFolder/fitsFile, instrument='SDSS') idx_wave = (wave_rest >= obsData['sample_data']['wmin_array']) & (wave_rest <= obsData['sample_data']['wmax_array']) # Load line measurer object lm = sr.LineMesurer(wave_rest[idx_wave], flux[idx_wave], lineLogFolder / lineLogFile, normFlux=flux_norm) # Measure line fluxes idcs_lines = ~lm.linesDF.index.str.contains('_b') obsLines = lm.linesDF.loc[idcs_lines].index.values # Equivalent width Hbeta eqw_Hbeta, eqwErr_Hbeta = lm.linesDF.loc['H1_4861A', 'eqw'], lm.linesDF.loc['H1_4861A', 'eqw_err'] eqw_entry = r'${:0.2f}$ $\pm$ ${:0.2f}$'.format(eqw_Hbeta, eqwErr_Hbeta) # Normalizing flux flux_Halpha = lm.linesDF.loc['H1_6563A', 'gauss_flux'] flux_Hbeta = lm.linesDF.loc['H1_4861A', 'intg_flux'] halpha_norm = flux_Halpha / flux_Hbeta halphaRatio_entry = r'${:0.2f}$'.format(halpha_norm) colorGrade = colorChooser(halpha_norm, 2.86) RatioComparison = r'\textcolor{{{}}}{{{}}}'.format(colorGrade, halphaRatio_entry) # Reddening coefficient rc = pn.RedCorr(R_V=3.4, law='G03 LMC') rc.setCorr(obs_over_theo=halpha_norm/2.86, wave1=6563., wave2=4861.) cHbeta_entry = r'${:0.2f}$'.format(rc.cHbeta) pdf.addTableRow([objName, eqw_entry, cHbeta_entry, RatioComparison], last_row=True if file_address == addressList[-1] else False) # Generate the table pdf.generate_pdf(clean_tex=True)
'Mass presently availabled': 'LOGMEAVE', 'Mass > 1 Myr': 'LOGPEAVE' } row_headers = list(dict_ref.keys()) tables_folder = Path(obsData['file_information']['tables_folder']) for i, obj in enumerate(objList): pdf = PdfPrinter() row_object = [''] * len(row_headers) row_object[5] = obj pdf.create_pdfDoc(pdf_type='table') pdf.pdf_insert_table(row_object, addfinalLine=False) pdf.table.add_hline() pdf.addTableRow(row_headers, last_row=False, rounddig=2) pdf.table.add_hline() # Declare input files objFolder = resultsFolder / f'{obj}' results_file = objFolder / f'{obj}_{ext}_measurements.txt' # Declare output file table_file = tables_folder / f'{obj}_FadoFitting' # # Load the data results_dict = sr.loadConfData(results_file, group_variables=False) for j, ssp_lib in enumerate(ssp_lib_list): section_label = f'{ssp_lib}_FADO'
# Correct the flux corr = rc.getCorrHb(wavelength) intensity, intensityErr = flux * corr, fluxErr * corr # Format the entries eqw_entry = r'${:0.2f}\pm{:0.2f}$'.format(eqw, eqwErr) flux_entry = r'${:0.2f}\pm{:0.2f}$'.format(flux, fluxErr) intensity_entry = r'${:0.2f}\pm{:0.2f}$'.format( intensity, intensityErr) # Add row of data row_i = [label_entry, eqw_entry, flux_entry, intensity_entry] lastRow_check = True if lineLabel == obsLines[-1] else False pdf.addTableRow([label_entry, eqw_entry, flux_entry, intensity_entry], last_row=lastRow_check) tableDF.loc[lineLabel] = row_i[1:] # Data last rows row_Hbetaflux = [ r'$H\beta$ $(erg\,cm^{-2} s^{-1} \AA^{-1})$', '', flux_Hbeta * flux_norm, flux_Hbeta * flux_norm * rc.getCorr(4861) ] row_cHbeta = [r'$c(H\beta)$', '', rc.cHbeta, ''] pdf.addTableRow(row_Hbetaflux, last_row=False) pdf.addTableRow(row_cHbeta, last_row=False) tableDF.loc[row_Hbetaflux[0]] = row_Hbetaflux[1:] tableDF.loc[row_cHbeta[0]] = row_cHbeta[1:] # Format last rows
param_value = (np.mean(trace), np.std(trace)) if param == 'Teff': param_entry = r'${:.0f}\pm{:.0f}$'.format(param_value[0], param_value[1]) else: param_entry = r'${:.2f}\pm{:.2f}$'.format(param_value[0], param_value[1]) row_data[4] = param_entry # Bayesian Direct method + HII-CHI-mistry if outputDirectHIIchemDb.is_file(): model_fit = sr.load_MC_fitting(outputDirectHIIchemDb) trace = model_fit['trace'][param] param_value = (np.mean(trace), np.std(trace)) if param == 'Teff': param_entry = r'${:.0f}\pm{:.0f}$'.format( param_value[0], param_value[1]) else: param_entry = r'${:.2f}\pm{:.2f}$'.format( param_value[0], param_value[1]) row_data[5] = param_entry pdf.addTableRow(row_data, color_background='background', color_font='foreground') pdf.table.add_hline() table_file = tables_folder / f'photo-ionization_param_comparison' pdf.generate_pdf(table_file, clean_tex=False)
] row_subHeaders = [ 'Line label', r'$f_{\lambda}$', r'$F(\lambda)$', r'$I(\lambda)$', r'$F(\lambda)$', r'$I(\lambda)$', r'$F(\lambda)$', r'$I(\lambda)$' ] # Table heading pdf = PdfPrinter() pdf.create_pdfDoc(pdf_type='table') pdf.pdf_insert_table(row_headers, table_format=table_header_format, addfinalLine=False) # pdf.table.add_hline(3, 4, cmidruleoption='l{10pt}r{2pt}') # pdf.table.add_hline(5, 6) # pdf.table.add_hline(7, 8) pdf.addTableRow(row_subHeaders, last_row=True) # Table body for i, linelabel in enumerate(tableDF.index): # Line reference if (tableDF.loc[linelabel, 'blended_label'] == 'None') or ('_m' in linelabel): label_ref = tableDF.loc[linelabel, 'latexLabel'] else: label_ref = tableDF.loc[linelabel, 'latexLabel'][:-1] + '_g$' # Line lambda lambda_ref = f'{tableDF.loc[linelabel, "f_lambda"]:0.2f}' # List for table row row_data = [label_ref, lambda_ref] + ['-'] * 6
atomic_data_dict = {'T_low': r'$Normal(\mu=15,000K,\sigma=5000K)$', 'T_high': r'$Normal(\mu=15,000K,\sigma=5000K)$', 'n_e': r'$HalfCauchy(\mu=2.0,\sigma=0)$', 'cHbeta': r'$HalfCauchy(\mu=2.0,\sigma=0)$', 'log(X_i+)': r'$Normal(\mu=5,\sigma=5)$', 'log(He1r)': r'$Normal(\mu=0,\sigma=3)$', 'log(He2r)': r'$Normal(\mu=0,\sigma=3)$', 'Teff': r'$Uniform(min=30,000K, max=90,000K)$', 'logU': r'$Uniform(min=-4.0, max=-1.0K)$'} tables_folder = Path('/home/vital/Dropbox/Astrophysics/Seminars/UniVapo 2021/') pdf = PdfPrinter() pdf.create_pdfDoc(pdf_type='table') pdf.pdfDoc.append(NoEscape('\definecolor{background}{rgb}{0.169, 0.169, 0.169}')) pdf.pdfDoc.append(NoEscape('\definecolor{foreground}{rgb}{0.702, 0.780, 0.847}')) pdf.pdfDoc.append(NoEscape(r'\arrayrulecolor{foreground}')) pdf.pdf_insert_table(['Parameter', 'Prior distribution'], addfinalLine=True, color_font='foreground', color_background='background') for ion, params in atomic_data_dict.items(): ion_label = latex_labels[ion] row = [ion_label, params] pdf.addTableRow(row, last_row=False, color_font='foreground', color_background='background') pdf.table.add_hline() pdf.generate_pdf(tables_folder/'posteriors_table', clean_tex=True)
pdf.create_pdfDoc(pdf_type=None) pdf.pdf_insert_table(table_headers, addfinalLine=True) for i, obj in enumerate(objList): if i < 3: row = ['-'] * len(table_headers) objData = obsData[obj] # row[0] = objData['SDSS_label'] row[0] = r'\href{{{}}}{{{}}}'.format( objData['SDSS_website'].replace('&', '\&'), objData['SDSS_label']) print(row[0]) row[1] = objRef[i] row[2] = objData['RA'] row[3] = objData['DEC'] row[4] = objData['z'] row[5] = f'{objData["grism_b"]}, {objData["grism_r"]}' # row[5] = f'{objData["grism_b"]} (blue), {objData["grism_r"]}(red)' row[6] = r'${}\times{}s$'.format(objData["exp_number"], objData["exp_time"]) row[7] = r'$\approx {}$'.format(objData["seeing"]) row[8] = f'{objData["standard_blue"]}, {objData["standard_red"]}' # row[8] = f'{objData["standard_blue"]} (blue), {objData["standard_red"]}(red)' # row[9] = f'{objData["night_type"]} night' pdf.addTableRow(row, last_row=False) pdf.table.add_hline() pdf.generate_pdf(tables_folder / 'night_log')
row_data[3] = param_entry # Bayesian HII-CHI-mistry model_fit = sr.load_MC_fitting(outputPiDb) trace = model_fit['trace'][param] param_value = (np.mean(trace), np.std(trace)) if param == 'Teff': param_entry = r'${:.0f}\pm{:.0f}$'.format(param_value[0], param_value[1]) else: param_entry = r'${:.2f}\pm{:.2f}$'.format(param_value[0], param_value[1]) row_data[4] = param_entry # Bayesian Direct method + HII-CHI-mistry if outputDirectHIIchemDb.is_file(): model_fit = sr.load_MC_fitting(outputDirectHIIchemDb) trace = model_fit['trace'][param] param_value = (np.mean(trace), np.std(trace)) if param == 'Teff': param_entry = r'${:.0f}\pm{:.0f}$'.format(param_value[0], param_value[1]) else: param_entry = r'${:.2f}\pm{:.2f}$'.format(param_value[0], param_value[1]) row_data[5] = param_entry pdf.addTableRow(row_data) pdf.table.add_hline() table_file = tables_folder / f'photo-ionization_param_comparison' pdf.generate_pdf(table_file)
'intg_err'] / flux_Hbeta * scaleTable flux_gauss = objDF.loc[lineLabel, 'gauss_flux'] / flux_Hbeta * scaleTable flux_gaussErr = objDF.loc[lineLabel, 'gauss_err'] / flux_Hbeta * scaleTable if objDF.loc[lineLabel, 'blended_label'] != 'None': flux, fluxErr = flux_gauss, flux_gaussErr else: flux, fluxErr = flux_intg, flux_intgErr # Correct the flux wavelength = objDF.loc[lineLabel, 'wavelength'] corr = rc.getCorrHb(wavelength) intensity, intensityErr = flux * corr, fluxErr * corr flux_entry = r'${:0.2f}$ $\pm$ ${:0.2f}$'.format(flux, fluxErr) intensity_entry = r'${:0.2f}$ $\pm$ ${:0.2f}$'.format( intensity, intensityErr) else: eqw_entry, flux_entry, intensity_entry, cHbeta_entry = '-', '-', '-', '-' lastRow_check = True if obj == objectList[-1] else False pdf.addTableRow( [obj, eqw_entry, flux_entry, intensity_entry, cHbeta_entry], last_row=lastRow_check) # Generate the table pdf.generate_pdf(clean_tex=True)