karabo.sourcedetection package
Submodules
karabo.sourcedetection.evaluation module
- class SourceDetectionEvaluation(sky: SkyModel, ground_truth: ndarray[Any, dtype[float64]], assignments: ndarray[Any, dtype[float64]], sky_idxs: ndarray[Any, dtype[int64]], source_detection: ISourceDetectionResult)
Bases:
object
- classmethod automatic_assignment_of_ground_truth_and_prediction(ground_truth: ndarray[Any, dtype[int64]] | ndarray[Any, dtype[float64]], detected: ndarray[Any, dtype[int64]] | ndarray[Any, dtype[float64]], max_dist: float, top_k: int = 3) ndarray[Any, dtype[float64]]
Automatic assignment of the predicted sources predicted to the ground truth gtruth. The strategy is the following (similar to AUTOMATIC SOURCE DETECTION IN ASTRONOMICAL IMAGES, P.61, Marc MASIAS MOYSET, 2014): Each distance between the predicted and the ground truth sources is calculated. Any distances > max_dist are not considered. Assign the closest distance from the predicted and ground truth. Repeat the assignment, until every source from the gtruth has an assignment if possible, not allowing any double assignments from the predicted sources to the ground truth and vice versa. So each ground truth source should be assigned with a predicted source if at least one was in range and the predicted source assigned to another ground truth source before. If there are duplicate sources (e.g. same source, different frequency), the duplicate sources are removed and the assignment is done on the remaining.
- Parameters:
ground_truth – nx2 np.ndarray with the ground truth pixel
coordinates of the catalog :param detected: kx2 np.ndarray with the predicted pixel coordinates of the image :param max_dist: maximal allowed euclidean distance for assignment (in pixel domain) :param top_k: number of top predictions to be considered in scipy.spatial. KDTree. A small value could lead to imperfect results. :return: nx3 np.ndarray where each row represents an assignment - first column represents the ground truth index
(return is sorted by this column) a minus index means a ground-truth source with no allocated prediction
- second column represents the predicted index
a minus index means a predicted source with no allocated ground-truth
- third column represents the euclidean distance between the assignment
a “inf” means no allocation between ground-truth and prediction of that source
- static calculate_evaluation_measures(assignments: ndarray[Any, dtype[float64]]) Tuple[int, int, int]
Calculates the True Positive (TP), False Positive (FP) and False Negative (FN) of the ground truth and predictions. - TP are the detections associated with a source - FP are detections without any associated source - FN are sources with no associations with a detection
- Parameters:
assignments – nx3 did np.ndarray where each row represents an assignment The assignments is expected to be as automatic_assignment_of_ground_truth_and_prediction return. Therefore, the non-assigned sources must have a value of “-1”.
- Returns:
TP, FP, FN
- get_accuracy() float
- get_confusion_matrix() ndarray[Any, dtype[int64]]
- get_f_score() float
- get_precision() float
- get_sensitivity() float
- plot(exclude_img: bool = False, show_legend: bool = True, filename: str | None = None) None
Plot the found sources as green x’s and the source truth as red ‘o’ on the original image, that the source detection was performed on.
- plot_confusion_matrix(filename: str | None = None) None
- plot_error_ra_dec(filename: str | None = None) None
- plot_flux_histogram(nbins: int = 10, filename: str | None = None) None
- plot_flux_ratio_to_distance(filename: str | None = None) None
- plot_flux_ratio_to_ra_dec(filename: str | None = None) None
- plot_quiver_positions(filename: str | None = None) None
karabo.sourcedetection.result module
- class ISourceDetectionResult
Bases:
ABC
SourceDetectionResult interface.
- abstract property detected_sources: ndarray[Any, dtype[float64]]
- abstract has_source_image() bool
- class PyBDSFSourceDetectionResult(bdsf_detection: Image)
Bases:
SourceDetectionResult
- class PyBDSFSourceDetectionResultList(bdsf_detection: List[PyBDSFSourceDetectionResult] | None = None)
Bases:
ISourceDetectionResult
A class to handle the detection results of sources from PyBDSF detections.
This class calculates the pixel positions of sources in a mosaic image and removes sources that are closer than a specified minimum pixel distance, preferring sources with higher total flux.
Parameters
- bdsf_detectionOptional[List[PyBDSFSourceDetectionResult]
A list of PyBDSF source detection results.
Attributes
- bdsf_detectionList[PyBDSFSourceDetectionResult]
The list of detection results from PyBDSF.
- detected_sourcesNDArray[np.float_]
Array of detected sources. Calculated based on bdsf_detection and min_pixel_distance_between_sources. It’s populated when the property is accessed and not beforehand.
- min_pixel_distance_between_sourcesint
The minimum number of pixels that must separate sources to be considered distinct. Defaults to 5. This attribute can be modified before calling any function that relies on it to adjust the minimum distance criterion.
- verbosebool
If True, prints verbose output. Defaults to False.
Methods
- detected_sources() -> NDArray[np.float_]:
Returns the detected sources after applying the minimum pixel distance criteria. Lazy loads the value when accessed.
Notes
The detected_sources property depends on bdsf_detection and min_pixel_distance_between_sources. It calculates the pixel positions based on these inputs and filters out sources that are closer than the minimum distance. It also assumes a single mosaic header is applicable for all detections for CRPIX calibration.
- classmethod detect_sources_in_images(images: List[Image], beams: List[BeamType] | None = None, verbose: bool = False, **kwargs: Any) PyBDSFSourceDetectionResultList | None
- property detected_sources: ndarray[Any, dtype[float64]]
Aggregate detected sources from multiple pybdsf detection instances.
This method concatenates detected sources from all instances in the `bdsf_detection`attribute. It identifies and removes overlapping sources using pixel overlap.
Returns
- NDArray[np.float_]
A numpy array of detected sources after removing overlaps. The array structure and content depend on the format used by individual detection instances.
Notes
This method relies on self.__get_idx_of_overlapping_sources() to identify indices of overlapping sources and self.__drop_cast_sources() to remove them.
- get_pixel_position_of_sources() ndarray[Any, dtype[float64]]
Calculate and return corrected pixel positions of sources in a mosaic image.
This public method calculates the pixel positions of sources in a mosaic image, corrects them, and removes overlapping sources based on a specified minimum distance and total flux criteria.
Returns
- NDArray[np.float_]
A NumPy array of the corrected and filtered pixel positions of sources in the mosaic image.
- has_source_image() bool
- class SourceDetectionResult(detected_sources: ndarray[Any, dtype[float64]], source_image: Image)
Bases:
ISourceDetectionResult
- classmethod detect_sources_in_image(image: Image, beam: BeamType | None = None, verbose: bool = False, **kwargs: Any) _SourceDetectionResultType | None
Detect sources in an astronomical image using PyBDSF.process_image function.
Parameters
- clsType[_SourceDetectionResultType]
The class on which this method is called.
- imageImage or List[Image]
Image object for source detection. Can be a single image or a list of images.
- beamOptional[BeamType],
The Full Width Half Maximum (FWHM) of the restoring beam, BMAJ(arcsec), BMIN(arcsec), BPA(degree). If None, tries to extract from image metadata.
verbose : verbose? n_splits : int, default 0
The number of parts to split the image into for processing. A value greater than 1 requires Dask.
- overlapint, default 0
The overlap between split parts of the image in pixels.
- **kwargsAny
Additional keyword arguments to pass to PyBDSF.process_image function.
Returns
- Optional[List[_SourceDetectionResultType]]
A list of detected sources, or None if all pixels in the image are blanked or on failure.
Raises
- RuntimeError
If an unexpected error occurs during the source detection process.
Notes
The dask client has to be created with the setting processes=False to avoid issues with PyBDSF multiprocessing. See similar issue here: https://stackoverflow.com/questions/51485212/multiprocessing-gives-assertionerror-daemonic-processes-are-not-allowed-to-have # noqa If ‘n_splits’ is greater than 1 and ‘use_dask’ is True, the image will be split into multiple parts and processed in parallel using Dask. Overlap can be specified to prevent edge artifacts.
If the ‘beam’ parameter is not provided, the method will attempt to extract the beam parameters from the image metadata. A warning is raised if beam parameters are not found.
The PyBDSF process_image function is called for source detection, which is particularly designed for radio astronomical images. For details on this function, refer to the PyBDSF documentation.
- property detected_sources: ndarray[Any, dtype[float64]]
- get_pixel_position_of_sources() ndarray[Any, dtype[float64]]
- get_source_image() Image | None
Return the source image, where the source detection was performed on. :return: Karabo Image or None (if not supplied)
- has_source_image() bool
Check if source image is present. :return: True if present, False if not present
- classmethod read_from_file(path: Path | str) SourceDetectionResult
- write_to_file(path: Path | str) None
Save Source Detection Result to ZIP Archive containing the .fits source image and source-finding catalog. :param path: path to save the zip archive as.