phoptic.reducer

Classes

Reducer

Class for reducing astronomical images.

Functions

set_psf_params(aperture_selector, catalog)

Set the PSF parameters.

parse_alignment_results(results, camera_files, ...)

Parse the alignment results.

write_queued_logs(queued_logs, logger)

Write queued logs to file.

save_unaligned_files(out_directory, unaligned_files)

Save the unaligned files to a text file.

create_targets_dict(catalogs)

Create a dictionary of target IDs for all catalog sources.

save_photometry_results(results, catalogs, barycenter, ...)

Save the photometry results to disk.

parse_photometry_results(results)

Merge the multiprocessed photometry results into a single dictionary.

save_catalog(catalog, key, out_directory)

Save a camera:filter catalog to file using astropy's ECSV format.

Module Contents

class phoptic.reducer.Reducer(out_directory, data_directory, aperture_selector=np.median, background=None, barycenter=True, bias_corrector=None, dark_corrector=None, finder=None, flat_corrector=None, instrument=OPTICAM_MX(), number_of_processors=cpu_count() // 2, rebin_factor=1, image_filter=None, remove_cosmic_rays=False, show_plots=True, threshold=5, verbose=True)

Class for reducing astronomical images.

Parameters:
verbose = True
out_directory
logger
data_directory
rebin_factor = 1
image_filter = None
instrument
aperture_selector
threshold = 5
remove_cosmic_rays = False
barycenter = True
number_of_processors
show_plots = True
bias_corrector = None
dark_corrector = None
flat_corrector = None
reference_files: dict[str, phoptic.mef_slice.MEFSlice]
get_data: Callable[[phoptic.mef_slice.MEFSlice], tuple[numpy.typing.NDArray[numpy.float64], astropy.io.fits.Header, dict[str, float | numpy.typing.NDArray[numpy.float64]]]]
transforms
unaligned_files = []
catalogs: dict[str, astropy.table.QTable]
psf_params
_log_params()

Log the input parameters of a Reducer instance to file.

Parameters

reducerReducer

The Reducer instance.

Return type:

None

create_catalogs(max_catalog_sources=15, n_alignment_sources=15, transform_type='affine', rotation_limit=None, translation_limit=None, scale_limit=None, overwrite=False)

Initialise the source catalogs for each camera. Some aspects of this method are parallelised for speed.

Parameters

max_catalog_sourcesint, optional

The maximum number of sources to include in the catalog, by default 30. Since source IDs are ordered by brightness, the brightest max_catalog_sources sources are included in the catalog.

n_alignment_sourcesint, optional

The (maximum) number of sources to use for image alignment, by default 30. If transform_type=’translation’, n_alignment_sources must be >= 1, and the brightest n_alignment_sources sources are used for image alignment. If transform_type=’affine’, n_alignment_sources must be >= 3 and represents that maximum number of sources that may be used for image alignment.

transform_typeLiteral[‘affine’, ‘translation’], optional

The type of transform to use for image alignment, by default ‘affine’. ‘translation’ performs simple x, y translations, while ‘affine’ uses astroalign.find_transform(). ‘affine’ is generally more robust (and is therefore the default) while ‘translation’ can work with fewer sources.

rotation_limitfloat, optional

The maximum rotation limit (in degrees) for affine transformations, by default None (no limit).

scale_limitfloat, optional

The maximum scale limit for affine transformations, by default None (no limit).

translation_limitfloat | int | list[float | int] | None, optional

The maximum translation limit for both types of transformations, by default None (no limit). Can be a scalar value that applies to both x- and y-translations, or an iterable where the first value defines the x-translation limit and the second value defines the y-translation limit.

overwritebool, optional

Whether to overwrite existing catalogs, by default False.

Parameters:
  • max_catalog_sources (int)

  • n_alignment_sources (int)

  • transform_type (Literal['affine', 'translation'])

  • rotation_limit (float | None)

  • translation_limit (float | int | list[float | int] | None)

  • scale_limit (float | None)

  • overwrite (bool)

Return type:

None

pick_sources(percentile=99.0, region_size=None)

Interactive plot for manually adding sources to the catalogs.

Parameters

percentile: float, optional

The interval percentile for image normalisation, by default 99.0.

region_sizeint | None, optional

The size of the region used to refine source coordinates, by default None. If None, the region size is set to the image width divided by 32.

Returns

QTable

The table containing the new

Parameters:
  • percentile (float)

  • region_size (int | None)

_add_source(event, fig, stacked_images, region_size)

Add a manually-picked source to the associated catalog.

Parameters

eventLiteral[“button_press_event”]

The event.

figFigure

The catalog plot.

stacked_imagesdict[str, NDArray]

The catalog images.

region_sizeint | None, optional

The size of the region used to refine source coordinates.

Parameters:
  • event (Literal['button_press_event'])

  • fig (matplotlib.figure.Figure)

  • stacked_images (dict[str, numpy.typing.NDArray])

  • region_size (int)

Return type:

None

_align_batch(batch, reference_image_shape, reference_coords, transform_type, rotation_limit, scale_limit, translation_limit, n_alignment_sources)

Align a batch of images with respect to some reference coordinates.

Parameters

batch: list[MEFSlice]

The files.

reference_image_shapetuple[int]

The reference image’s shape.

reference_coordsNDArray

The source coordinates in the reference image.

transform_typeLiteral[‘affine’, ‘translation’]

The type of transform to use for image alignment.

rotation_limitfloat | None

The maximum rotation limit (in degrees) for image alignment.

scale_limitfloat | None

The maximum scaling limit for image alignment.

translation_limitlist[float] | None

The maximum translation limit for image alignment.

n_alignment_sourcesint

The (maximum) number of sources to use for image alignment.

Returns

tuple[NDArray[np.float64], dict[str, list[float]], dict[str, dict[str, float]], list[tuple[str, str]]]

The stacked image, transforms, systematics, and log messages.

Parameters:
  • batch (list[phoptic.mef_slice.MEFSlice])

  • reference_image_shape (tuple[int])

  • reference_coords (numpy.typing.NDArray)

  • transform_type (Literal['affine', 'translation'])

  • rotation_limit (float | None)

  • scale_limit (float | None)

  • translation_limit (list[float] | None)

  • n_alignment_sources (int)

Return type:

tuple[numpy.typing.NDArray[numpy.float64], dict[str, list[float]], dict[str, dict[str, float]], list[tuple[str, str]]]

_valid_transform(file, transform, rotation_limit, scale_limit, translation_limit)

Find whether a transform is valid given some transform limits.

Parameters

fileMEFSlice

The path to the file being transformed.

transformSimilarityTransform

The transform.

rotation_limitfloat | None

The rotation limit.

scale_limitfloat | None

The scale limit.

translation_limitlist[float] | None

The translation limit.

Returns

tuple[bool, None | tuple[str, str]]

Whether the transform is valid. If not, a log message is also returned as a tuple: (log level, log string).

Parameters:
  • file (phoptic.mef_slice.MEFSlice)

  • transform (skimage.transform.SimilarityTransform)

  • rotation_limit (float | None)

  • scale_limit (float | None)

  • translation_limit (list[float] | None)

Return type:

tuple[bool, None | tuple[str, str]]

save_systematics(systematics, key)

Save the systematics to a CSV file.

Parameters

systematicsdict[Path, dict[str, float]]

The systematics for each file.

keystr

The camera:filter key.

Parameters:
  • systematics (dict[str, dict[str, float]])

  • key (str)

Return type:

None

plot_background_meshes(save=False)

Plot the background mesh over an image from each filter to verify it’s appropriately sized. If stacked catalog images exist, those will be used. Otherwise, a random image will be chosen for each filter.

Parameters

savebool, optional

Whether to save the plot, by default False.

Parameters:

save (bool)

Return type:

None

get_random_image_for_each_filter()

Choose a random image for each filter from a dictionary.

Returns

dict[str, NDArray]

A dictionary containing a random image for each filter

Return type:

dict[str, numpy.typing.NDArray]

plot_growth_curves(targets=None, save=False)

Plot the growth curves for the sources identified in the catalog images. The resulting plots are saved to out_directory/diag/growth_curves as PDF files.

Parameters

targetsdict[str, int | list[int]] | None, optional

The targets for which growth curves will be created, by default None (growth curves are created for all catalog sources). To create growth curves for specific targets, pass a dictionary with keys listing the desired filters and values listing each filter’s correpsonding target(s). For example: ``` # plot growth curves for the three brightest sources in each catalog plot_growth_curves(

targets = {

‘g’: [1, 2, 3], ‘r’: [1, 2, 3], ‘i’: [1, 2, 3], },

)

```

savebool, optional

Whether to save the plots, by default False.

Parameters:
  • targets (dict[str, int | list[int]] | None)

  • save (bool)

Return type:

None

plot_psfs()

Plot the PSFs for the catalog sources.

Return type:

None

plot_snrs(save=False)

Plot the signal-to-noise ratios for each catalogued source in the reference images.

Parameters

savebool, optional

Whether to save the plot, by default False.

Parameters:

save (bool)

Return type:

None

create_gifs(keep_frames=True, overwrite=False)

Create alignment gifs for each camera. Some aspects of this method are parallelised for speed. The frames are saved in out_directory/diag/*_gif_frames and the GIFs are saved in out_directory/cat.

Parameters

keep_framesbool, optional

Whether to save the GIF frames in out_directory/diag, by default True. If False, the frames will be deleted after the GIF is saved.

overwritebool, optional

Whether to overwrite existing GIFs, by default False.

Parameters:
  • keep_frames (bool)

  • overwrite (bool)

Return type:

None

plot_apertures(photometer, targets=None, save=False)

Plot the apertures over each source.

Parameters

photometerAperturePhotometer

The AperturePhotometer instance. If a local background estimator has been defined, this will also be plotted.

targetsdict[str, int] | dict[str, list[int]] | dict[str, list[int] | int] | None

The targets for which apertures will be plotted, by default None (apertures are plotted for all sources). To plot apertures for specific targets, pass a dictionary with keys listing the desired filters and values listing each filter’s correpsonding target(s). For example: ``` # plot apertures for the three brightest sources in each filter photometer = opticam.AperturePhotometer() plot_apertures(

photometer=photometer, targets = {

‘g’: [1, 2, 3], ‘r’: [1, 2, 3], ‘i’: [1, 2, 3], },

)

```

savebool, optional

Whether to save the plots, by default False.

Parameters:
Return type:

None

photometry(photometer, overwrite=False)

Perform photometry on the catalogs using the provided photometer.

Parameters

photometerBasePhotometer

The photometer. Should be a subclass of BasePhotometer, or implement a compute method that follows the BasePhotometer interface.

overwritebool, optional

Whether to overwrite any existing light curves files computed using the same photometer, by default False.

Parameters:
Return type:

None

_perform_photometry(file, photometer, cat_coords, key)

Perform photometry on a file.

Parameters

fileMEFSlice

The file.

photometerBasePhotometer

The photometer to use.

cat_coordsNDArray

The coordinates of the sources in the catalog.

keystr

The camera:filter key.

Returns

dict[str, list]

The photometry results.

Parameters:
Return type:

dict[str, list]

update_unaligned_files(files)

Add one or more files to the list of unaligned files. Unaligned files are skipped when performing photometry.

Parameters

filesMEFSlice | list[MEFSlice]

The file or files.

Parameters:

files (phoptic.mef_slice.MEFSlice | list[phoptic.mef_slice.MEFSlice])

Return type:

None

plot_noise(save=False)

Plot the noise characterisation for each reference image.

Parameters

savebool, optional

Whether to save the plot, by default ‘False’.

Parameters:

save (bool)

Return type:

None

_get_noise_params(file, key)

Get the noise values of a science image.

Parameters

fileMEFSlice

The science image file.

keystr

The camera:filter key.

Returns

tuple[NDArray[np.int64], NDArray[np.float64], NDArray[np.float64], float, float, float, float, float, NDArray[np.float64]]

The source IDs, fluxes, flux errors, number of aperture pixels, backgorund counts/pixel, bias variance, dark variance, flat-field variance, and scintillation noise.

Parameters:
Return type:

tuple[numpy.typing.NDArray[numpy.int64], numpy.typing.NDArray[numpy.float64], numpy.typing.NDArray[numpy.float64], float, float, float, float, float, numpy.typing.NDArray[numpy.float64]]

get_snrs(file, key)

Get the S/N ratios for the cataloged sources in a science image.

Parameters

fileMEFSlice

The science image file.

keystr

The camera:filter key.

Returns

dict[int, float]

The source ID (key) and S/N (value) for each source.

Parameters:
Return type:

dict[int, float]

_characterise_noise(file, key)

Characterise the expected noise from an image and compare it to the measured noise for a number of cataloged sources.

Parameters

fileMEFSlice

The science image file.

keystr

The camera:filter key.

Returns

dict[str, NDArray]

The noies properties.

Parameters:
Return type:

dict[str, numpy.typing.NDArray]

phoptic.reducer.set_psf_params(aperture_selector, catalog)

Set the PSF parameters.

Parameters

aperture_selectorCallable

The aperture selector (e.g., numpy.median).

catalogQTable

The source catalog.

Returns

dict[str, float]

The PSF parameters.

Parameters:
  • aperture_selector (Callable)

  • catalog (astropy.table.QTable)

Return type:

dict[str, float]

phoptic.reducer.parse_alignment_results(results, camera_files, transforms, unaligned_files, logger)

Parse the alignment results.

Parameters

resultstuple

The alignment results.

camera_fileslist[MEFSlice]

The files.

transformsdict[Path, list[float]]

The image-to-image alignments {file path: transform}.

unaligned_fileslist[MEFSlice]

The files that could not be aligned.

loggerLogger

The logger.

Returns

tuple[dict[str, list[float]], list[str], NDArray, dict[str, float], dict[str, float]]

The updated transforms, unaligned files, stacked image, and systematics.

Parameters:
Return type:

tuple[dict[str, list[float]], list[phoptic.mef_slice.MEFSlice], numpy.typing.NDArray[numpy.float64], dict[str, dict[str, float]]]

phoptic.reducer.write_queued_logs(queued_logs, logger)

Write queued logs to file.

Parameters

queued_logslist[tuple[str, str]]

The queued logs (level, log).

loggerLogger

The logger.

Raises

ValueError

If the log level is not recognised.

Parameters:
  • queued_logs (list[tuple[str, str]])

  • logger (logging.Logger)

Return type:

None

phoptic.reducer.save_unaligned_files(out_directory, unaligned_files)

Save the unaligned files to a text file.

Parameters

out_directoryPath

The output directory.

unaligned_fileslist[MEFSlice]

The list of unaligned files.

Parameters:
Return type:

None

phoptic.reducer.create_targets_dict(catalogs)

Create a dictionary of target IDs for all catalog sources.

Parameters

catalogsdict[str, QTable]

The catalogs.

Returns

dict[str, list[int]]

The target IDs for all catalog sources.

Parameters:

catalogs (dict[str, astropy.table.QTable])

Return type:

dict[str, list[int]]

phoptic.reducer.save_photometry_results(results, catalogs, barycenter, save_dir, key)

Save the photometry results to disk.

Parameters

resultstuple[dict]

The photometry results.

catalogsdict[str, QTable]

The source catalogs.

save_dirPath

The save directory path.

keystr

The camera:filter key.

Parameters:
  • results (tuple[dict])

  • catalogs (dict[str, astropy.table.QTable])

  • barycenter (bool)

  • save_dir (pathlib.Path)

  • key (str)

phoptic.reducer.parse_photometry_results(results)

Merge the multiprocessed photometry results into a single dictionary.

Parameters

resultstuple[dict[str, list]]

The multiprocessed photometry results.

Returns

dict[str, list[list[float]]]

The photometry results in a single dictionary.

Parameters:

results (tuple[dict[str, list]])

Return type:

dict[str, list[list[float]]]

phoptic.reducer.save_catalog(catalog, key, out_directory)

Save a camera:filter catalog to file using astropy’s ECSV format.

Parameters

catalogQTable

The catalog for a specific camera:filter combination.

keystr

The camera:filter key.

out_directoryPath

The output directory.

Parameters:
  • catalog (astropy.table.QTable)

  • key (str)

  • out_directory (pathlib.Path)

Return type:

None