コード例 #1
0
    def test_centroid(self):
        """
        Test that the centroiding operation works as expected.

        """
        # Start by building a sample test image.
        nx, ny = 150, 200
        X, Y = np.meshgrid(range(nx), range(ny))
        centers = np.array([[50.0, 45.0], [75.6, 150.1]])
        widths = [10, 4]
        amps = [5, 2]
        img = np.zeros((ny, nx))
        for i, p in enumerate(centers):
            R2 = (X - p[0])**2 + (Y - p[1])**2
            img += amps[i] * np.exp(-0.5 * R2 / widths[i]**2) / widths[i]

        # Then try a centered scene.
        nsx, nsy = 45, 57
        SX, SY = np.meshgrid(range(nsx), range(nsy))
        scene = np.exp(-0.5 * ((SX - (nsx - 1) / 2) ** 2 \
                + (SY - (nsy - 1) / 2) ** 2))

        # Run the centroid.
        size = 24
        coords, data, mask = utils.centroid_image(img, size, scene=scene)

        # Make sure that it gets the right coordinates.
        truth = centers[np.argmax(amps)]
        assert np.all(coords[::-1] == truth)
コード例 #2
0
ファイル: tests.py プロジェクト: davidwhogg/TheThresher
    def test_centroid(self):
        """
        Test that the centroiding operation works as expected.

        """
        # Start by building a sample test image.
        nx, ny = 150, 200
        X, Y = np.meshgrid(range(nx), range(ny))
        centers = np.array([[50.0, 45.0], [75.6, 150.1]])
        widths = [10, 4]
        amps = [5, 2]
        img = np.zeros((ny, nx))
        for i, p in enumerate(centers):
            R2 = (X - p[0]) ** 2 + (Y - p[1]) ** 2
            img += amps[i] * np.exp(-0.5 * R2 / widths[i] ** 2) / widths[i]

        # Then try a centered scene.
        nsx, nsy = 45, 57
        SX, SY = np.meshgrid(range(nsx), range(nsy))
        scene = np.exp(-0.5 * ((SX - (nsx - 1) / 2) ** 2 + (SY - (nsy - 1) / 2) ** 2))

        # Run the centroid.
        size = 24
        coords, data, mask = utils.centroid_image(img, size, scene=scene)

        # Make sure that it gets the right coordinates.
        truth = centers[np.argmax(amps)]
        assert np.all(coords[::-1] == truth)
コード例 #3
0
ファイル: thresher.py プロジェクト: kcompher/TheThresher
    def do_update(self, fn, alpha, maskfn=None, maskhdu=0, median=True,
            nn=False, hack=True):
        """
        Do a single stochastic gradient update using the image in a
        given file and learning rate.

        ## Arguments

        * `fn` (str): The filename of the image to be used.
        * `alpha` (float): The learning rate.

        ## Keyword Arguments

        * `maskfn` (str): Path to the mask file.
        * `maskhdu` (int): The HDU number for the mask.
        * `median` (bool): Subtract the median of the scene?
        * `nn` (bool): Project onto the non-negative plane?

        ## Returns

        * `data` (numpy.ndarray): The centered and cropped data image used
          for this update.

        """
        image = utils.load_image(fn, hdu=self.hdu)
        if maskfn is not None:
            mask = utils.load_image(maskfn, hdu=maskhdu)
            if self.invert:
                inds = np.isnan(mask) + np.isinf(mask)
                mask[~inds] = 1.0 / mask[~inds]
                mask[inds] = 0.0
            if self.square:
                mask = mask ** 2
        else:
            mask = np.ones_like(image)

        # Center the data.
        if self.centers is None:
            data = utils.trim_image(image, self.size)
            mask = utils.trim_image(mask, self.size)
        else:
            result = utils.centroid_image(image, self.size,
                    coords=self.centers[fn], mask=mask)
            data = result[1]
            mask = result[2]

        # Deal with NaNs and infinities.
        if maskfn is None:
            mask *= ~(np.isnan(data) + np.isinf(data))
        data[mask == 0.0] = 0.0
        assert np.all(~(np.isnan(data) + np.isinf(data))), \
                "Yer data's got some unmasked NaNs or infs, dude."

        # Add the DC offset.
        data += self.dc

        # Do the inference.
        self.old_scene = np.array(self.scene)

        self.psf, self.sky = self.infer_psf(data, mask)

        if hack:
            logging.info("Hacking.")
            X, Y = np.meshgrid(np.arange(-self.psf_hw, self.psf_hw + 1),
                    np.arange(-self.psf_hw, self.psf_hw + 1))
            R = np.sqrt(X ** 2 + Y ** 2)
            outer = self.psf[R > 0.9 * self.psf_hw]
            inds = outer > 0
            if np.any(inds):
                self.psf -= np.sum(inds) / float(outer.size) \
                    * np.median(outer[inds])

        print "sky:", self.sky
        self.dlds = self.get_dlds(data, mask)

        # self.old_scene = self.scene + alpha * self.dlds
        self.scene += alpha * self.dlds

        # WTF?!?
        gc.collect()

        # Apply some serious HACKS!
        if median:
            self.scene -= np.median(self.scene)
        if nn:
            self.scene[self.scene < 0] = 0.0

        return data