soxspipe.commonutils.toolkit¶
small reusable functions used throughout soxspipe
- Author
David Young
- Date Created
September 18, 2020
Module Contents¶
Classes¶
Functions¶
add a recipe-specific handler to the default logger that writes the recipe’s logs adjacent to the recipe project |
|
add quality checks to the qc table |
|
Calculate the rolling Signal-to-Noise Ratio (SNR) for a given column in a pandas DataFrame. |
|
given a dispersion solution and accompanying 2D dispersion map image, generate the grid lines to add to QC plots |
|
cut and return an N-pixel wide and M-pixels long slice, centred on a given coordinate from an image frame |
|
convert a given frame to 32-bit float format |
|
measure very basic quality checks on a frame and return the QC table with results appended |
|
given a frame, determine which calibration lamp is being used |
|
return the root path to the static calibrations |
|
Plot merged spectrum QC plot as a standalone function. |
|
predict the path of the recipe product from a given SOF name |
|
generate QC and settings table to be placed at the bottom of the QC plots |
|
generate a quicklook image of a CCDObject - useful for development/debugging |
|
read the spectral format table to get some key parameters |
|
measure and record spectroscopic image quailty checks |
|
convert the 2D dispersion image map to a pandas dataframe |
|
Unpack an order location table and return an |
|
setup some tools needed by most soxspipe utils |
API¶
- soxspipe.commonutils.toolkit.add_recipe_logger(log, productPath)[source][source]¶
add a recipe-specific handler to the default logger that writes the recipe’s logs adjacent to the recipe project
Key Arguments:
log– original loggerproductPath– path to the recipe product
Usage:
from soxspipe.commonutils.toolkit import add_recipe_logger log = add_recipe_logger(log, productPath="/path/to/product")
- soxspipe.commonutils.toolkit.add_snr_efficiency_qcs(log, spectrumDF, qcTable, orderJoins, recipeName, dateObs)[source][source]¶
add quality checks to the qc table
Key Arguments:
log– loggerspectrumDF– the dataframe containing the extracted spectrum with a column named ‘SNR’ or ‘EFFICIENCY’ to calculate the checks from.qcTable– the qc table to which the SNR checks will be addedorderJoins– a dictionary containing the wavelengths of order joins (if any) to be added as QCs.recipeName– name of the recipe to add to the QC entriesdateObs– the observation date to add to the QC entries (in ISO format, e.g., “2024-06-01T00:00:00”)
Return:
qcTable– the updated qc table with SNR checks added
Usage:
qcTable = add_snr_qcs(log, spectrumDF, qcTable, orderJoins)
- soxspipe.commonutils.toolkit.calculate_rolling_snr(dataframe, flux_column, window_size)[source][source]¶
Calculate the rolling Signal-to-Noise Ratio (SNR) for a given column in a pandas DataFrame.
This function computes the rolling SNR for a specified column in the DataFrame using a custom rolling window function. The SNR is calculated as the ratio of the median signal to the noise, where the noise is estimated using a robust statistical method.
Key Arguments:
- `dataframe`: The input pandas DataFrame containing the data. - `flux_column`: The name of the column in the DataFrame for which the rolling SNR will be calculated. - `window_size`: The size of the rolling window to use for the calculation.Return:
- `dataframe`: The input DataFrame with an additional column 'SNR' containing the calculated rolling SNR values.
Usage:
```python from soxspipe.commonutils.toolkit import calculate_rolling_snr df_with_snr = calculate_rolling_snr(dataframe=df, flux_column='flux', window_size=5) ```
- soxspipe.commonutils.toolkit.create_dispersion_solution_grid_lines_for_plot(log, dispMap, dispMapImage, associatedFrame, kw, skylines=False, slitPositions=False, slit_length=11)[source][source]¶
given a dispersion solution and accompanying 2D dispersion map image, generate the grid lines to add to QC plots
Key Arguments:
log– loggerdispMap– path to dispersion map. Default FalsedispMapImage– the 2D dispersion map imageassociatedFrame– a frame associated with the reduction (to read arm and binning info).kw– fits header kw dictionaryskylines– a list of skylines to use as the grid. Default FalseslitPositions– slit positions to plot (else plot min and max)slit_length– length of the slit to use for the dispersion map dataframe (default 11)
Returns:
orderPixelTable– DataFrame containing the pixel coordinates for grid lines to plot.interOrderMask– Mask array indicating inter-order regions.
Usage:
from soxspipe.commonutils.toolkit import create_dispersion_solution_grid_lines_for_plot gridLinePixelTable, interOrderMask = create_dispersion_solution_grid_lines_for_plot( log=log, dispMap=dispMap, dispMapImage=dispMapImage, associatedFrame=CCDObject, kw=kw, skylines=skylines ) for l in range(int(gridLinePixelTable['line'].max())): mask = (gridLinePixelTable['line'] == l) ax.plot(gridLinePixelTable.loc[mask]["fit_y"], gridLinePixelTable.loc[mask]["fit_x"], "w-", linewidth=0.5, alpha=0.8, color="black")
- soxspipe.commonutils.toolkit.cut_image_slice(log, frame, width, length, x, y, sliceAxis='x', median=False, debug=False)[source][source]¶
cut and return an N-pixel wide and M-pixels long slice, centred on a given coordinate from an image frame
Key Arguments:
log– loggerframe– the data array to cut the slice from (masked array)width– width of the slice (odd number)length– length of the slicex– x-coordinatey– y-coordinatesliceAxis– the axis along which slice is to be taken. Default xmedian– collapse the slice to a median value across its widthdebug– generate a plot of slice. Useful for debugging.
Usage:
from soxspipe.commonutils.toolkit import cut_image_slice slice = cut_image_slice(log=self.log, frame=self.pinholeFlat.data, width=1, length=sliceLength, x=x_fit, y=y_fit, plot=False) if slice is None: return None
- soxspipe.commonutils.toolkit.extinction_correction_factor(wave, extinctionTablePath, airmass)[source][source]¶
- soxspipe.commonutils.toolkit.frame_to_32(frame)[source][source]¶
convert a given frame to 32-bit float format
Key Arguments:
frame– the input frame
Returns:
frame– the converted frame
Usage:
from soxspipe.commonutils.toolkit import frame_to_32 frame = frame_to_32(frame)
- soxspipe.commonutils.toolkit.generic_quality_checks(log, frame, settings, recipeName, qcTable)[source][source]¶
measure very basic quality checks on a frame and return the QC table with results appended
Key Arguments:
log– loggerframe– CCDData objectsettings– soxspipe settingsrecipeName– the name of the recipeqcTable– the QC pandas data-frame to save the QC measurements
Usage:
from soxspipe.commonutils.toolkit import generic_quality_checks qcTable = generic_quality_checks(log=log, frame=myFrame, settings=settings, recipeName="my recipe", qcTable=qcTable)
- soxspipe.commonutils.toolkit.get_calibration_lamp(log, frame, kw)[source][source]¶
given a frame, determine which calibration lamp is being used
Key Arguments:
log– loggerframe– the frame to determine the calibration lamp forkw– the FITS header keyword dictionary
Usage:
from soxspipe.commonutils.toolkit import get_calibration_lamp lamp = get_calibration_lamp(log=log, frame=frame, kw=kw)
- soxspipe.commonutils.toolkit.get_calibrations_path(log, settings)[source][source]¶
return the root path to the static calibrations
Key Arguments:
log– loggersettings– the settings dictionary
Usage:
from soxspipe.commonutils.toolkit import get_calibrations_path calibrationRootPath = get_calibrations_path(log=log, settings=settings)
- soxspipe.commonutils.toolkit.plot_merged_spectrum_qc(merged_orders, products, log, qcDir, filenameTemplate, noddingSequence, dateObs, arm, recipeName, orderJoins=False, debug=False, fluxCalibrated=False, qcTable=False, settings=False)[source][source]¶
Plot merged spectrum QC plot as a standalone function.
Returns: products (pd.DataFrame): Updated products table. filePath (str): Path to the saved QC plot PDF.
- soxspipe.commonutils.toolkit.predict_product_path(sofName, recipeName=False)[source][source]¶
predict the path of the recipe product from a given SOF name
Key Arguments:
log– logger,sofName– name or full path to the sof filerecipeName– name of the recipe being considered. Default False.
Usage:
from soxspipe.commonutils import toolkit productPath, startNightDate = toolkit.predict_product_path(sofFilePath)
- soxspipe.commonutils.toolkit.qc_settings_plot_tables(log, qc, qcAx, settings, settingsAx)[source][source]¶
generate QC and settings table to be placed at the bottom of the QC plots
Key Arguments:
log– loggerqc– date frame of collected QCsqcAx– the axis to add the QC table tosettings– settings to report in settings tablesettingsAx– the axis to add the settings table to
Usage:
from soxspipe.commonutils.toolkit import qc_settings_plot_tables qc_settings_plot_tables(log=log,qc=self.qc,qcAx=qcAx, settings=settings,settingsAx=settingsAx)
- soxspipe.commonutils.toolkit.quicklook_image(log, CCDObject, show=True, ext='data', stdWindow=3, title=False, surfacePlot=False, dispMap=False, dispMapImage=False, inst=False, settings=False, skylines=False, saveToPath=False)[source][source]¶
generate a quicklook image of a CCDObject - useful for development/debugging
Key Arguments:
log– loggerCCDObject– the CCDObject to plotshow– show the image. Set to False to skipext– the name of the the extension to show. Can be “data”, “mask” or “err”. Default “data”.title– give a title for the plotsurfacePlot– plot as a 3D surface plotdispMap– path to dispersion map. Default FalsedispMapImage– the 2D dispersion map imageinst– provide instrument name if no header existsskylines– mark skylines on image
from soxspipe.commonutils.toolkit import quicklook_image quicklook_image( log=self.log, CCDObject=myframe, show=True)
- soxspipe.commonutils.toolkit.read_spectral_format(log, settings, arm, dispersionMap=False, extended=True, binx=1, biny=1)[source][source]¶
read the spectral format table to get some key parameters
Key Arguments:
log– loggersettings– soxspipe settingsarm– arm to retrieve format fordispersionMap– if a dispersion map is given, the minimum and maximum dispersion axis pixel limits are computedextended– the spectral format table can provide WLMIN/WLMAX (extended=False) or WLMINFUL/WLMAXFUL (extended=True)binx– binning in the x-axis (from FITS header). Default 1biny– binning in the y-axis (from FITS header). Default 1
Return:
orderNums– a list of the order numberswaveLengthMin– a list of the maximum wavelengths reached by each orderwaveLengthMax– a list of the minimum wavelengths reached by each order
Usage:
from soxspipe.commonutils.toolkit import read_spectral_format # READ THE SPECTRAL FORMAT TABLE TO DETERMINE THE LIMITS OF THE TRACES orderNums, waveLengthMin, waveLengthMax = read_spectral_format( log=self.log, settings=self.settings, arm=arm)
- soxspipe.commonutils.toolkit.spectroscopic_image_quality_checks(log, frame, orderTablePath, settings, recipeName, qcTable)[source][source]¶
measure and record spectroscopic image quailty checks
Key Arguments:
log– loggerframe– CCDData objectorderTablePath– path to the order tablesettings– soxspipe settingsrecipeName– the name of the recipeqcTable– the QC pandas data-frame to save the QC measurements
Usage:
from soxspipe.commonutils.toolkit import spectroscopic_image_quality_checks qcTable = spectroscopic_image_quality_checks( log=log, frame=myFrame, settings=settings, recipeName="this recipe", qcTable=qcTable, orderTablePath=orderTablePath)
- soxspipe.commonutils.toolkit.twoD_disp_map_image_to_dataframe(log, slit_length, twoDMapPath, kw=False, associatedFrame=False, removeMaskedPixels=False, dispAxis='y')[source][source]¶
convert the 2D dispersion image map to a pandas dataframe
Key Arguments:
log– loggertwoDMapPath– 2D dispersion map image pathkw– fits keyword lookup dictionaryassociatedFrame– include a flux column in returned dataframe from a frame associated with the dispersion map. Default FalseremoveMaskedPixels– remove the masked pixels from the associated image? Default FalsedispAxis– x or y. Needed for pixel scale calculation
Usage:
from soxspipe.commonutils.toolkit import twoD_disp_map_image_to_dataframe mapDF = twoD_disp_map_image_to_dataframe(log=log, twoDMapPath=twoDMap, associatedFrame=objectFrame, kw=kw)
- soxspipe.commonutils.toolkit.unpack_order_table(log, orderTablePath, extend=0.0, pixelDelta=1, binx=1, biny=1, prebinned=False, order=False, limitToDetectorFormat=False)[source][source]¶
Unpack an order location table and return an
orderPolyTabledataframe containing the polynomial coefficients for the order centres and edges, anorderPixelTabledataframe containing the pixel-coordinates for each order centre and edges, and finally, anorderMetaTabledataframe giving metadata about the frame binning and format.Key Arguments:
orderTablePath– path to the order tableextend– fractional increase to the order area in the y-axis (needed for masking)pixelDelta– space between returned data points. Default 1 (sampled at every pixel)binx– binning in the x-axis (from FITS header). Default 1biny– binning in the y-axis (from FITS header). Default 1prebinned– was the order-table measured on a pre-binned frame (typically only for mflats). Default Falseorder– unpack only a single orderlimitToDetectorFormat– limit the pixels return to those limited by the detector format static calibration table
Usage:
# UNPACK THE ORDER TABLE from soxspipe.commonutils.toolkit import unpack_order_table orderPolyTable, orderPixelTable = unpack_order_table( log=self.log, orderTablePath=orderTablePath, extend=0.)
- soxspipe.commonutils.toolkit.utility_setup(log, settings, recipeName, startNightDate)[source][source]¶
setup some tools needed by most soxspipe utils
Key Arguments:
log– loggersettings– the settings dictionaryrecipeName– name of the recipe as it appears in the settings dictionarystartNightDate– YYYY-MM-DD date of the observation night. Default “”
Return:
qcDir– the QC directory (created if missing)productDir– the product directory (created if missing)
Usage:
# Example usage with timezone-aware UTC datetime from datetime import datetime, UTC startNightDate = datetime.now(UTC).strftime("%Y-%m-%d") qcDir, productDir = utility_setup(log=log, settings=settings, recipeName="my_recipe", startNightDate=startNightDate)