Package: pycadet
Cavity Detection Tool(CADET)CADETis a machine learning pipeline trained to identify of surface brightness depressions (X-ray cavities) in noisyChandraimages of early-type galaxies and galaxy clusters. The pipeline consists of a convolutional neural network trained to produce pixel-wise cavity predictions and a DBSCAN clustering algorithm that decomposes the predictions into individual cavities. The pipeline is described in detail inPlšek et al. 2023.The architecture of the convolutional network consists of 5 convolutional blocks, each resembling an Inception layer, it was implemented using theKeraslibrary and its development was inspired byFort et al. 2017andSecká 2019. For the clustering, we used is theScikit-learnimplementation of the Density-Based Spatial Clustering of Applications with Noise (DBSCAN).Python packageThe CADET pipeline has been released as a standalone Python3 packagepycadet, which can be installed using pip:$pip3installpycadetor from source:$pip3installgit+https://github.com/tomasplsek/CADET.gitThepycadetpackage requires the following libraries (which should be installed automatically with the package):numpy
scipy
astropy
matplotlib
pyds9
scikit-learn>=1.1
tensorflow>=2.8For Conda environments, it is recommended to install the dependencies beforehand as some of the packages can be tricky to install in an existing environment (especiallytensorflow) and on some machines (especially new Macs). For machines with dedicated NVIDIA GPUs,tensorflow-gpucan be installed to allow the CADET model to leverage the GPU for faster inference.An exemplary notebook on how to use thepycadetpackage can be found here:DS9 PluginThe CADET pipeline can also be used as aSAOImageDS9plugin which is installed together with thepycadetPython package. The CADET plugin requires that SAOImageDS9 is already installed on the system. To avoid conflicts (e.g. the CIAO installation of DS9), it is recommended to installpycadetusing a system installation of Python3 rather than a Conda environment.After the installation, the CADET plugin should be available in theAnalysismenu of DS9. After clicking on theCADEToption, a new window will appear, where the user can set several options: whether the prediction should be averaged over multiple input images by shifting by +/- 1 pixel (Shift); and whether the prediction should be decomposed into individual cavities (Decompose). When decomposing into individual cavities, the user can also set a pair of discrimination thresholds, where the first one (Threshold1) is used for volume error calibration and the second one (Threshold2) for false positive rate calibration (for more info seePlšek et al. 2023).If the CADET plugin does not appear in theAnalysismenu, it can be added manually by openingEdit>Preferences>Analysisand adding a path to the following fileDS9CADET.ds9.ans(after the installation it should be located in~/.ds9/). The plugin is inspired by thepyds9pluginlibrary.Online CADET interfaceA simplified version of the CADET pipeline is available via aweb interfacehosted on HuggingFace Spaces. The input image should be centred on the galaxy centre and cropped to a square shape. It is also recommended to remove point sources from the image and fill them with the surrounding background level using Poisson statistics (dmfilthwithinCIAO). Furthermore, compared to thepycadetpackage, the web interface performs only a single thresholding of the raw pixel-wise prediction, which is easily adjustable using a slider.Convolutional partThe convolutional part of the pipeline can be used separately to produce raw pixel-wise predictions. Since the convolutional network was implemented using the functionalKerasAPI, the architecture could have been stored together with the trained weights in the HDF5 format (CADET.hdf5). The trained model can then simply be loaded using theload_modelTensorFlowfunction:fromtensorflow.keras.modelsimportload_modelmodel=load_model("CADET.hdf5")y_pred=model.predict(X)The raw CADET model only inputs 128x128 images. Furthermore, to maintain the compatibility withKeras, the input needs to be reshaped asX.reshape(1, 128, 128, 1)for single image or asX.reshape(-1, 128, 128, 1)for multiple images.Alternatively, the CADET model can be imported from HuggingFace'smodel hub:fromhuggingface_hubimportfrom_pretrained_kerasmodel=from_pretrained_keras("Plsek/CADET-v1")y_pred=model.predict(X)How to citeIf you use the CADET  pipeline in your research, please cite the following paperPlšek et al. 2023(arXiv):@ARTICLE{2023MNRAS.tmp.3233P,
       author = {{Pl{\v{s}}ek}, T. and {Werner}, N. and {Topinka}, M. and {Simionescu}, A.},
        title = "{CAvity DEtection Tool (CADET): Pipeline for detection of X-ray cavities in hot galactic and cluster atmospheres}",
      journal = {\mnras},
         year = 2023,
        month = nov,
          doi = {10.1093/mnras/stad3371},
}TodoThe following improvements to the data generation and training process are currently planned:add other features (cold fronts, complex sloshing, point sources, jets)use more complex cavity shapes (e.g.Guo et al. 2015)train on multiband images simulated using PyXsim/SOXSreplace DBSCAN by using instance segmentationrestrict the cavity number and shape using regularization?systematic cavity size uncertainty estimation using MC Dropout