flint.imager.wsclean¶
Simple interface into wsclean
Some notes around the file naming.
A certain filenaming scheme is required for FITS files to perform leakage correction
when co-adding them together in the yandasoft linmos application. The stokes field
needs to be encoded as .i.. For example:
>>> `SB1234.RACS_0123+43.beam01.i.image.fits`
The wsclean formatted output string appends something denoted with -. Within
this code there is and attempt to rename the wsclean outputs to replace the -
with a ..
Classes¶
A structure to represent the images and auxiliary products produced by |
|
Mapping results to provide to wsclean |
|
A basic container to handle WSClean options. These attributes should |
|
Simple container for a wsclean command. |
Functions¶
|
Create a tuple of polarisations from a polarisation string |
|
Rename a file with wsclean appended string information to convert its |
|
Construct and apply a regular expression that aims to identify |
|
An internal function intended to map a key-value pair to |
|
Call back function used to detect clean divergence |
|
|
|
Combine wsclean subband channel images into a cube. Each collection attribute |
|
Combine wsclean subband channel images into a cube. Each collection attribute |
|
Create a wsclean command from a WSCleanOptions container |
|
Create the value that will be provided to wsclean -name argument. This has |
|
Attempt to remove elected wsclean output files |
|
|
|
Attempt to generate the file names and paths that would be |
|
WSClean can produce a text file that describes the components |
|
|
|
Merge the image sets from multiple wsclean results into a single image set. |
|
Merge the image sets from multiple wsclean results into a single image set. |
|
Given an input image_set, rename the files contained in it to |
|
Rotate the FITS cube axes to a shape of (chan, pol, dec, ra) |
|
Run a provided wsclean command. Optionally will clean up files, |
|
Split an ImageSet by a given field and return the images that match the field of interest. |
|
Split an ImageSet by a given field. |
|
Clean up (i.e. delete) files from wsclean. |
|
Create and run a wsclean imager command against a measurement set. |
Module Contents¶
- class flint.imager.wsclean.ImageSet(/, **data: Any)[source]¶
Bases:
flint.options.BaseOptionsA structure to represent the images and auxiliary products produced by wsclean
- class flint.imager.wsclean.ResolvedCLIResult[source]¶
Bases:
NamedTupleMapping results to provide to wsclean
- class flint.imager.wsclean.WSCleanOptions(/, **data: Any)[source]¶
Bases:
flint.options.BaseOptionsA basic container to handle WSClean options. These attributes should conform to the same option name in the calling signature of wsclean
Basic support for environment variables is available. Should a value start with $ it is assumed to be a environment variable, it is will be looked up. Some basic attempts to determine if it is a path is made.
Should the temp_dir options be specified then all images will be created in this location, and then moved over to the same parent directory as the imaged MS. This is done by setting the wsclean -name argument.
Options that start with flint_ are ignored when generating the wsclean command and generally denote some other flint specific operation.
- beam_fitting_size: float | None = 1.25[source]¶
Use a fitting box the size of <factor> times the theoretical beam size for fitting a Gaussian to the PSF.
- channel_range: tuple[int, int] | None = None[source]¶
Image a channel range between a lower (inclusive) and upper (exclusive) bound
- deconvolution_channels: int | None = None[source]¶
The channels out will be averaged down to this many sub-band images during deconvolution
- fit_spectral_pol: int | None = None[source]¶
Number of spectral terms to include during sub-band subtraction
- flint_make_cube_inplace: bool = True[source]¶
Rotate the cube for the linmos axis ordering in place, or do it via a temporary file that then gets deleted. Good thing to turn off when getting weird OSErrors on file writing
- gridder: str | None = 'wgridder'[source]¶
Use the wgridder kernel in wsclean (instead of the default w-stacking method)
- interval: tuple[int, int] | None = None[source]¶
Image a set of scans between a lower (inclusive) and upper (exclusive) bound
- join_channels: bool = True[source]¶
Collapse the sub-band images down to an MFS image when peak-finding
- join_polarizations: bool = False[source]¶
Perform deconvolution by searching for peaks in the sum of squares of the polarizations, but subtract components from the individual images. Only possible when imaging two or four Stokes or linear parameters. Default: off.
- maxw: float | None = None[source]¶
A percentage specifying the maximum w-term to be gridded, relative to the max w-term being considered
- minuv_l: float | None = None[source]¶
The minimum lambda length that the visibility data needs to meet for it to be selected for imaging
- minuvw_m: float | None = None[source]¶
A (u,v) selection command, where any baselines shorter than this will be ignored during imaging
- multiscale_fits_mask: pathlib.Path | None = None[source]¶
Path to a FITS file that contains per-scale clean masks encoded as per-pixel bit values
- multiscale_gain: float | None = None[source]¶
Size of step made in the subminor loop of multi-scale. Default currently 0.2, but shows sign of instability. A value of 0.1 might be more stable.
- multiscale_scales: tuple[int, Ellipsis] | None = None[source]¶
Scales used for multi-scale deconvolution
- no_mf_weighting: bool = False[source]¶
Opposite of -ms-weighting; can be used to turn off MF weighting in -join-channels mode
- no_reorder: bool = False[source]¶
If True turn off the reordering of the MS at the beginning of wsclean
- no_small_inversion: bool = False[source]¶
Disables an optimisation of wsclean’s w-gridder mode. This might improve accuracy of the w-gridder.
- no_update_model_required: bool = False[source]¶
Will instruct wsclean not to create the MODEL_DATA column
- parallel_deconvolution: int | None = None[source]¶
If not none, the maximum sub-image size in pixels used during deconvolution (suggestion between to between 1024-4096)
- parallel_gridding: int | None = None[source]¶
If not none, then this is the number of channel images that will be gridded in parallel
- save_source_list: bool = False[source]¶
Saves the found clean components as a BBS/DP3 text sky model
- size: int = 10128[source]¶
Image size, only a single dimension is required. Note that this means images will be squares.
- squared_channel_joining: bool = False[source]¶
Use with -join-channels to perform peak finding in the sum of squared values over channels, instead of the normal sum. This is useful for imaging QU polarizations with non-zero rotation measures, for which the normal sum is insensitive.
- class flint.imager.wsclean.WSCleanResult(/, **data: Any)[source]¶
Bases:
flint.options.BaseOptionsSimple container for a wsclean command.
- bind_dirs: tuple[pathlib.Path, Ellipsis][source]¶
Paths that should be binded to when executing the command
- cleanup: bool = True[source]¶
Will clean up the dirty images/psfs/residuals/models when the imaging has completed
- move_hold_directories: tuple[pathlib.Path, pathlib.Path][source]¶
Paths that should be moved to and from when executing the command
- ms: flint.options.MS[source]¶
The measurement sets that have been included in the wsclean command.
- options: WSCleanOptions[source]¶
The set of wslean options used for imaging
- flint.imager.wsclean._make_pols(pol_str: str) tuple[str, Ellipsis] | None[source]¶
Create a tuple of polarisations from a polarisation string
- Parameters:
pol_str (str) – Polarisation string to convert
- Returns:
Tuple of polarisations
- Return type:
tuple[str, …] | None
- flint.imager.wsclean._rename_wsclean_file(input_path: pathlib.Path, rename_file: bool = False) pathlib.Path[source]¶
Rename a file with wsclean appended string information to convert its - separation markers with .. This should handle skipping the field name of the target field observed.
- Parameters:
input_path (Path) – The file path that would be examined and modified
clean_parts (Union[int, Tuple[int, ...]], optional) – Which parts of a split string will be modified. Defaults to -2.
rename_file (bool, optional) – Whether the file should be moved / renamed. Defaults to False.
- Returns:
Path to the renamed file
- Return type:
Path
- flint.imager.wsclean._rename_wsclean_title(name_str: str) str[source]¶
Construct and apply a regular expression that aims to identify the wsclean appended properties string within a file and replace the - separator with a ..
A simple replace of all - with . may not be ideal if the character has been used on purpose.
- Parameters:
name_str (str) – The name that will be extracted and modified
- Returns:
The modified string if a wsclean string was matched, otherwise the input name-str
- Return type:
str
- flint.imager.wsclean._resolve_wsclean_key_value_to_cli_str(key: str, value: Any) ResolvedCLIResult[source]¶
An internal function intended to map a key-value pair to the appropriate form to pass to a CLI call into wsclean.
- Parameters:
key (str) – The wsclean argument name to consider. Underscores will be converted to hyphens, as expected by wsclean
value (Any) – The value of the argument that should be converted to the appropriately formatted string
- Returns:
Converted CLI output, including paths to bind to and unknown conversions
- Return type:
- flint.imager.wsclean._wsclean_output_callback(line: str) None[source]¶
Call back function used to detect clean divergence
- flint.imager.wsclean.combine_image_set_to_cube(image_set: ImageSet, remove_original_images: bool = False, inplace: bool = True) ImageSet[source]¶
Combine wsclean subband channel images into a cube. Each collection attribute of the input image_set will be inspected. The MFS images will be ignored.
A output file name will be generated based on the prefix and mode (e.g. image, residual, psf, dirty).
- Parameters:
image_set (ImageSet) – Collection of wsclean image productds
remove_original_images (bool, optional) – If True, images that went into the cube are removed. Defaults to False.
inplace (bool, optional) – If True, modify the file in-place. If False, write to a temporary file and
True (then replace the original. Default)
- Returns:
Updated iamgeset describing the new outputs
- Return type:
- flint.imager.wsclean.combine_images_to_cube(images: list[pathlib.Path], prefix: str, mode: str, remove_original_images: bool = False, inplace: bool = True) pathlib.Path[source]¶
Combine wsclean subband channel images into a cube. Each collection attribute of the input image_set will be inspected. The MFS images will be ignored.
A output file name will be generated based on the prefix and mode (e.g. image, residual, psf, dirty).
- Parameters:
image_set (ImageSet) – Collection of wsclean image productds
remove_original_images (bool, optional) – If True, images that went into the cube are removed. Defaults to False.
inplace (bool, optional) – If True, modify the file in-place. If False, write to a temporary file and
True (then replace the original. Default)
- Returns:
Updated iamgeset describing the new outputs
- Return type:
- flint.imager.wsclean.create_wsclean_cmd(ms: flint.options.MS, wsclean_options: WSCleanOptions) WSCleanResult[source]¶
Create a wsclean command from a WSCleanOptions container
For the most part these are one-to-one mappings to the wsclean CLI with the exceptions being: #. the -name argument will be generated and supplied to the CLI string and will default to the parent directory and name of the supplied measurement set #. If wsclean_options.temp_dir is specified this directory is used in place of the measurement sets parent directory
If container is supplied to immediately execute this command then the output wsclean image products will be moved from the temp-dir to the same directory as the measurement set.
- Parameters:
ms (MS) – The measurement set to be imaged
wsclean_options (WSCleanOptions) – WSClean options to image with
container (Optional[Path], optional) – If a path to a container is provided the command is executed immediately. Defaults to None.
- Raises:
ValueError – Raised when a option has not been successfully processed
- Returns:
The wsclean command to run
- Return type:
- flint.imager.wsclean.create_wsclean_name_argument(wsclean_options: WSCleanOptions, ms: flint.options.MS) pathlib.Path[source]¶
Create the value that will be provided to wsclean -name argument. This has to be generated. Among things to consider is the desired output directory of imaging files. This by default will be alongside the measurement set. If a temp_dir has been specified then output files will be written here.
- Parameters:
wsclean_options (WSCleanOptions) – Set of wsclean options to consider
ms (MS) – The measurement set to be imaged
- Returns:
Value of the -name argument to provide to wsclean
- Return type:
Path
- flint.imager.wsclean.delete_wsclean_outputs(prefix: str, output_type: str = 'image', ignore_mfs: bool = True, no_subbands: bool = False) list[pathlib.Path][source]¶
Attempt to remove elected wsclean output files
If
no_subbandsis True (as inchannels_outis 1) then nothing is deleted.- Parameters:
prefix (str) – The prefix of the files to remove. This would correspond to the -name of wsclean.
output_type (str, optional) – What type of wsclean output to try to remove. Defaults to ‘image’.
ignore_mfs (bool, optional) – If True, do not remove MFS outputs (attempt to, at least). Defaults to True.
no_subbands (bool, Optional) – If True, nothing is deleted. Defaults to False.
- Returns:
The paths that were removed (or at least attempted to be removed)/
- Return type:
List[Path]
- flint.imager.wsclean.get_wsclean_output_names(prefix: str, subbands: int | None = None, pols: str | tuple[str] | tuple[str, Ellipsis] | None = None, verify_exists: bool = False, include_mfs: bool = True, output_types: str | Collection[str] = ('image', 'dirty', 'residual', 'model', 'psf'), check_exists_when_adding: bool = False) ImageSet[source]¶
Attempt to generate the file names and paths that would be created by an imaging run of wsclean. This is done using a the known (expected) naming format of modern wsclean versions.
Checks can be made to ensure a file exists before adding it to the output
ImageSet. This might be important as some wsclean image products might be deleted in order to preserve disk space.- Parameters:
prefix (str) – The prefix of the imaging run (akin to -name option in wsclean call)
subbands (int) – Number of subbands that were imaged
pols (Optional[Union[str,Tuple[str]]], optional) – The polarisation of the image. If None are provided then this is not used. Multiple polarisation may be supplied. If multiple pols are given in an iterable, each will be produced. Defaults to None.
verify_exists (bool, optional) – Ensures that each generated path corresponds to an actual file. Defaults to False.
include_mfs (bool, optional) – Include the MFS images produced by wsclean. Defaults to True.
output_types (Union[str,Collection[str]]) – Include files of this type, including image, dirty, residual, model, psf. Defaults to (‘image’,’dirty’,’residual’,’model’, ‘psf’).
check_exists_when_adding (bool, optional) – Only add a generated file if it exists. Although related to the
verify_exists, when this optional isTruefiles will silently be ignored if they are not found. if this andverify_existsare bothTruethen no errors on missing files will be raised. Defaults to False.
- Raises:
FileExistsError – Raised when a file does not exist and verify_exists is True.
- Returns:
The file paths that wsclean should create/has created.
- Return type:
- flint.imager.wsclean.get_wsclean_output_source_list_path(name_path: str | pathlib.Path, pol: str | None = None) pathlib.Path[source]¶
WSClean can produce a text file that describes the components that it cleaned, their type, scale and brightness. These are placed in a file that is:
>> {name}.{pol}-sources.txt
where
namerepresented the -name component. Given an input measurement set path or this -name value return the expected source list text file.polis the stokes that the source is expected.- Parameters:
name_path (Union[str,Path]) – Value of the
-nameoption. If str converted to aPathpol (Optional[str], optional) – The polarisation to add to the name. If None the -source.txt suffix is simply appended. Defaults to None.
- Returns:
Path to the source list text file
- Return type:
Path
- flint.imager.wsclean.image_set_from_result(wsclean_result: WSCleanResult) ImageSet | None[source]¶
- flint.imager.wsclean.merge_image_sets(image_sets: list[ImageSet]) ImageSet[source]¶
Merge the image sets from multiple wsclean results into a single image set.
- Parameters:
wsclean_results (List[WSCleanResult]) – The results of multiple wsclean imaging runs
- Returns:
The merged image set
- Return type:
- flint.imager.wsclean.merge_image_sets_from_results(wsclean_results: list[WSCleanResult]) ImageSet[source]¶
Merge the image sets from multiple wsclean results into a single image set.
- Parameters:
wsclean_results (List[WSCleanResult]) – The results of multiple wsclean imaging runs
- Returns:
The merged image set
- Return type:
- flint.imager.wsclean.rename_wsclean_prefix_in_image_set(input_image_set: ImageSet) ImageSet[source]¶
Given an input image_set, rename the files contained in it to remove the - separator that wsclean uses and replace it with ..
Files will be renamed on disk appropriately.
- flint.imager.wsclean.rotate_cube(output_cube_path: str | pathlib.Path, inplace: bool = True) pathlib.Path[source]¶
Rotate the FITS cube axes to a shape of (chan, pol, dec, ra) which is what yandasoft linmos tasks expect.
- Parameters:
output_cube_path (str | Path) – Path to the FITS cube to rotate.
inplace (bool, optional) – If True, modify the file in-place. If False, write to a temporary file and then replace the original. Default True
- Returns:
Path to the rotated FITS cube.
- Return type:
Path
- flint.imager.wsclean.run_wsclean_imager(wsclean_result: WSCleanResult, container: pathlib.Path, make_cube_from_subbands: bool = True) ImageSet[source]¶
Run a provided wsclean command. Optionally will clean up files, including the dirty beams, psfs and other assorted things.
An ImageSet is constructed that attempts to capture the output wsclean image products. If image_prefix_str is specified the image set will be created by (ordered by preference): #. Adding the image_prefix_str to the move_directory #. Guessing it from the path of the measurement set from wsclean_result.ms.path
- Parameters:
wsclean_result (WSCleanResult) – The command to run, and other properties (cleanup.)
container (Path) – Path to the container with wsclean available in it
bind_dirs (Optional[Tuple[Path, ...]], optional) – Additional directories to include when binding to the wsclean container. Defaults to None.
move_hold_directories (Optional[Tuple[Path,Optional[Path]]], optional) – The move_directory and hold_directory passed to the temporary context manager. If None no hold_then_move_into manager is used. Defaults to None.
make_cube_from_subbands (bool, optional) – Form a single FITS cube from the set of sub-band images wsclean produces. Defaults to False.
image_prefix_str (Optional[str], optional) – The name used to search for wsclean outputs. If None, it is guessed from the name and location of the MS. Defaults to None.
- Returns:
The executed wsclean output products.
- Return type:
- flint.imager.wsclean.split_and_get_image_set(image_set: ImageSet, get: str, by: str = 'pol', mode: str = 'image') list[pathlib.Path][source]¶
Split an ImageSet by a given field and return the images that match the field of interest.
- Parameters:
image_set (ImageSet) – The ImageSet to split
get (str) – The field to extract from the split images
by (str, optional) – The field to split by. Defaults to “pol”.
mode (str, optional) – The mode to extract from ImageSet. Defaults to “image”.
- Raises:
NamingException – If the field to split by is not found in the ImageSet
- Returns:
The images that match the field of interest
- Return type:
list[Path]
- flint.imager.wsclean.split_image_set(image_set: ImageSet, by: str = 'pol', mode: str = 'image') dict[str, list[pathlib.Path]][source]¶
Split an ImageSet by a given field.
- Parameters:
image_set (ImageSet) – The ImageSet to split
by (str, optional) – Field to split images by. Defaults to “pol”.
mode (str, optional) – Type of images to extract from ImageSet. Defaults to “image”.
- Raises:
NamingException – If the field to split by is not found in the ImageSet
- Returns:
_description_
- Return type:
dict[str, list[Path]]
- flint.imager.wsclean.wsclean_cleanup_files(prefix: str | pathlib.Path, output_types: tuple[str, Ellipsis] | None = ('dirty', 'psf', 'model', 'residual'), single_channel: bool = False) tuple[pathlib.Path, Ellipsis][source]¶
Clean up (i.e. delete) files from wsclean.
- Parameters:
prefix (Union[str, Path]) – The prefix used to search for files. This is generally the -name
output_types (Optional[Tuple[str]], optional) – Which type of output wsclean products to delete. Defaults to (“dirty”, “psf”, “model”, “residual”).
single_channel (bool, optional) – Whether there is the subband part of the wsclean file names to consider. Defaults to False.
- Returns:
Set of files that were deleted
- Return type:
Tuple[Path, …]
- flint.imager.wsclean.wsclean_imager(ms: pathlib.Path | flint.options.MS, wsclean_container: pathlib.Path, update_wsclean_options: dict[str, Any] | None = None, make_cube_from_subbands: bool = True) WSCleanResult[source]¶
Create and run a wsclean imager command against a measurement set.
- Parameters:
ms (Union[Path,MS]) – Path to the measurement set that will be imaged
wsclean_container (Path) – Path to the container with wsclean installed
update_wsclean_options (Optional[Dict[str, Any]], optional) – Additional options to update the generated WscleanOptions with. Keys should be attributes of WscleanOptions. Defaults to None.
- Returns:
_description_
- Return type: