Exemple #1
0
    def __init__(self):
        super(DeblendAdapt, self).__init__()

        # Method to find the centroid of donut
        self._centroidFind = CentroidFindFactory.createCentroidFind(
            CentroidFindType.RandomWalk)

        # Initial guess of block size used in the adaptive threshold mothod
        self.blockSizeInit = 33
Exemple #2
0
    def __init__(self):
        """DeblendDefault child class to do the deblending by the adaptive
        threshold method."""
        super(DeblendAdapt, self).__init__()

        # Method to find the centroid of donut
        self._centroidFind = CentroidFindFactory.createCentroidFind(
            CentroidFindType.RandomWalk
        )

        # Initial guess of block size used in the adaptive threshold mothod
        self.blockSizeInit = 33
Exemple #3
0
    def __init__(self, centroidFindType=CentroidFindType.RandomWalk):
        """Image class for wavefront estimation.

        Parameters
        ----------
        centroidFindType : enum 'CentroidFindType', optional
            Algorithm to find the centroid of donut. (the default is
            CentroidFindType.RandomWalk.)
        """

        self.image = np.array([])
        self.imageFilePath = ""

        self._centroidFind = CentroidFindFactory.createCentroidFind(centroidFindType)
Exemple #4
0
    def __init__(self, centroidFindType=CentroidFindType.RandomWalk):

        self.image = np.array([])
        self.imageFilePath = ""

        self._centroidFind = CentroidFindFactory.createCentroidFind(centroidFindType)
Exemple #5
0
    def detectDonuts(
        self,
        expArray,
        template,
        blendRadius,
        peakThreshold=0.95,
        dbscanEps=5,
        binaryChoice="centroid",
    ):
        """
        Detect and categorize donut sources as blended/unblended

        Parameters
        ----------
        expArray: numpy.ndarray
            The input image data.
        template: numpy.ndarray
            Donut template appropriate for the image.
        blendRadius: float
            Minimum distance in pixels two donut centers need to
            be apart in order to be tagged as unblended.
        peakThreshold: float, optional
            This value is a specifies a number between 0 and 1 that is
            the fraction of the highest pixel value in the convolved image.
            The code then sets all pixels with a value below this to 0 before
            running the K-means algorithm to find peaks that represent possible
            donut locations. (The default is 0.95)
        dbscanEps: float, optional
            Maximum distance scikit-learn DBSCAN algorithm allows "between two
            samples for one to considered in the neighborhood of the other".
            (The default is 5.0)
        binaryChoice: str, optional
            Indicates choice of how to arrive at the binary image passed to
            centroidFinder. Select 'centroid' to use getImgBinary method from
            centroidFinder, use 'deblend' for adaptative image thresholding,
            use 'exposure' to pass an unchanged copy of the exposure array.
            (The default is 'centroid')

        Returns
        -------
        pandas.DataFrame
            Dataframe identifying donut positions and if they
            are blended with other donuts. If blended also identfies
            which donuts are blended with which.

        Raises
        ------
        ValueError
            binaryChoice is not supported.
        """

        centroidFinder = CentroidFindFactory.createCentroidFind(
            CentroidFindType.ConvolveTemplate)
        if binaryChoice == "centroid":
            binaryExp = centroidFinder.getImgBinary(copy(expArray))

        elif binaryChoice == "deblend":
            deblend = DeblendAdapt()
            binaryExp = deblend._getImgBinaryAdapt(copy(expArray))

        elif binaryChoice == "exposure":
            binaryExp = copy(expArray)

        else:
            raise ValueError(f"binaryChoice {binaryChoice} is not supported.")

        centroidX, centroidY, donutRad = centroidFinder.getCenterAndRfromTemplateConv(
            binaryExp,
            templateImgBinary=template,
            nDonuts=-1,
            peakThreshold=peakThreshold,
            dbscanEps=dbscanEps,
        )

        donutDf = pd.DataFrame(np.array([centroidX, centroidY]).T,
                               columns=["x_center", "y_center"])
        donutDf = self.identifyBlendedDonuts(donutDf, blendRadius)

        return donutDf
Exemple #6
0
    def generateMultiDonut(self, template, spaceCoef, magRatio, theta):
        """Gemerate multiple donut images.

        Only one neightboring star will be generated for test, which is the
        baseline of LSST.

        Parameters
        ----------
        template : numpy.ndarray
            Template donut image.
        spaceCoef : float
            Spacing coefficient to decide the distance between donuts.
        magRatio : float
            Magnitude ratio of new donut compared with the original one.
        theta : float
            Theta angle of generated neighboring star in degree.

        Returns
        -------
        numpy.ndarray
            Image of donuts.
        numpy.ndarray
            Image of bright star.
        numpy.ndarray
            Image of neighboring star.
        float
            Position x of neighboring star.
        float
            Position y of neighboring star.

        Raises
        ------
        ValueError
            spaceCoef should be greater than zero.
        ValueError
            magRatio should be postive and less than 1.
        """

        # Check the inputs
        if spaceCoef <= 0:
            raise ValueError("spaceCoef should be greater than zero.")
        elif (magRatio < 0) or (magRatio > 1):
            raise ValueError("magRatio should be postive and less than 1.")

        # Get the center and radius of self-donut
        centroidFind = CentroidFindFactory.createCentroidFind(CentroidFindType.Otsu)
        imgBinary = centroidFind.getImgBinary(template)
        selfX, selfY, selfR = centroidFind.getCenterAndRfromImgBinary(imgBinary)

        # Get the position of new donut based on spaceCoef and theta
        thetaInRad = np.deg2rad(theta)
        newX = selfX + spaceCoef * selfR * np.cos(thetaInRad)
        newY = selfY + spaceCoef * selfR * np.sin(thetaInRad)

        # Calculate the frame size and shift the center of donuts
        lengthX = max(selfX, newX) - min(selfX, newX) + 5 * selfR
        lengthY = max(selfY, newY) - min(selfY, newY) + 5 * selfR
        length = int(max(lengthX, lengthY))

        # Enforce the length to be even for the symmetry
        if length % 2 == 1:
            length += 1

        shiftX = length / 2 - (selfX + newX) / 2
        shiftY = length / 2 - (selfY + newY) / 2

        # Get the new coordinate
        selfX += shiftX
        selfY += shiftY

        newX += shiftX
        newY += shiftY

        # Generate image of multiple donuts
        imageMain = np.zeros([length, length])
        imageNeighbor = np.zeros([length, length])

        # Get the shifted main donut image
        m, n = template.shape
        imageMain[
            int(selfY - m / 2) : int(selfY + m / 2),
            int(selfX - n / 2) : int(selfX + n / 2),
        ] += template

        # Get the shifted neighboring donut image
        imageNeighbor[
            int(newY - m / 2) : int(newY + m / 2), int(newX - n / 2) : int(newX + n / 2)
        ] += (magRatio * template)

        # Get the synthesized multi-donut image
        image = imageMain + imageNeighbor

        return image, imageMain, imageNeighbor, newX, newY
    def testCreateCentroidFindOtsu(self):

        centroidFind = CentroidFindFactory.createCentroidFind(
            CentroidFindType.Otsu)
        self.assertTrue(isinstance(centroidFind, CentroidOtsu))
    def testCreateCentroidFindRandomWalk(self):

        centroidFind = CentroidFindFactory.createCentroidFind(
            CentroidFindType.RandomWalk)
        self.assertTrue(isinstance(centroidFind, CentroidRandomWalk))
    def testCreateCentroidFindConvolveTemplate(self):

        centroidFind = CentroidFindFactory.createCentroidFind(
            CentroidFindType.ConvolveTemplate
        )
        self.assertTrue(isinstance(centroidFind, CentroidConvolveTemplate))