Пример #1
0
def scarlet1_multi_initialize(images, peaks, psfs, variances, bands):
    """ Initializes scarlet MultiComponentSource at locations input as
    peaks in the (multi-band) input images.
    Args:
        images: Numpy array of multi-band image to run scarlet on
                [Number of bands, height, width].
        peaks: Array of x and y coordinates of centroids of objects in
               the image [number of sources, 2].
        bg_rms: Background RMS value of the images [Number of bands]
    Returns
        blend: scarlet.Blend object for the initialized sources
        rejected_sources: list of sources (if any) that scarlet was
                          unable to initialize the image with.
    """
    model_psf = scarlet.PSF(partial(scarlet.psf.gaussian, sigma=0.8),
                            shape=(None, 41, 41))
    model_frame = scarlet.Frame(images.shape, psfs=model_psf, channels=bands)
    observation = scarlet.Observation(images, psfs=scarlet.PSF(psfs),
                                      weights=1./variances,
                                      channels=bands).match(model_frame)
    sources = []
    for n, peak in enumerate(peaks):
        result = scarlet.MultiComponentSource(model_frame, (peak[1], peak[0]),
                                              observation, symmetric=True,
                                              monotonic=True, thresh=1,
                                              shifting=True)
        for i in range(result.n_sources):
            sed = result.components[i].sed
            morph = result.components[i].morph
            if np.all([s < 0 for s in sed]) or np.sum(morph) == 0:
                raise ValueError("Incorrectly initialized")
        sources.append(result)
    blend = scarlet.Blend(sources, observation)
    return blend, observation
Пример #2
0
def scarlet1_initialize(images, peaks, psfs, variances, bands):
    """ Deblend input images with scarlet
    Args:
        images: Numpy array of multi-band image to run scarlet on
               [Number of bands, height, width].
        peaks: Array of x and y coordinates of centroids of objects in
               the image [number of sources, 2].
        bg_rms: Background RMS value of the images [Number of bands]
        iters: Maximum number of iterations if scarlet doesn't converge
               (Default: 200).
    e_rel: Relative error for convergence (Default: 0.015)
    Returns
        blend: scarlet.Blend object for the initialized sources
        rejected_sources: list of sources (if any) that scarlet was
        unable to initialize the image with.
    """
    model_psf = scarlet.PSF(partial(scarlet.psf.gaussian, sigma=0.8),
                            shape=(None, 41, 41))
    model_frame = scarlet.Frame(images.shape, psfs=model_psf, channels=bands)
    observation = scarlet.Observation(images, psfs=scarlet.PSF(psfs),
                                      weights=1./variances,
                                      channels=bands).match(model_frame)
    sources = []
    for n, peak in enumerate(peaks):
        result = scarlet.ExtendedSource(model_frame, (peak[1], peak[0]),
                                        observation, symmetric=True,
                                        monotonic=True, thresh=1,
                                        shifting=True)
        sed = result.sed
        morph = result.morph
        if np.all([s < 0 for s in sed]) or np.sum(morph) == 0:
            raise ValueError("Incorrectly initialized")
        sources.append(result)
    blend = scarlet.Blend(sources, observation)
    return blend, observation
Пример #3
0
    def test_render_loss(self):
        # model frame with minimal PSF
        shape0 = (3, 13, 13)
        s0 = 0.9
        model_psf = scarlet.PSF(partial(scarlet.psf.gaussian, sigma=s0), shape=shape0)
        shape = (3, 43, 43)
        channels = np.arange(shape[0])
        model_frame = scarlet.Frame(shape, psfs=model_psf, channels=channels)

        # insert point source manually into center for model
        origin = (0, shape[1] // 2 - shape0[1] // 2, shape[2] // 2 - shape0[2] // 2)
        bbox = scarlet.Box(shape0, origin=origin)
        model = np.zeros(shape)
        box = np.stack([model_psf.image[0] for c in range(shape[0])], axis=0)
        bbox.insert_into(model, box)

        # generate observation with wider PSFs
        psf = scarlet.PSF(self.get_psfs(shape[1:], [2.1, 1.1, 3.5]))
        images = np.ones(shape)
        observation = scarlet.Observation(images, psfs=psf, channels=channels)
        observation.match(model_frame)
        model_ = observation.render(model)
        assert_almost_equal(model_, psf.image)

        # compute the expected loss
        weights = 1
        log_norm = (
            np.prod(images.shape) / 2 * np.log(2 * np.pi)
            + np.sum(np.log(1 / weights)) / 2
        )
        true_loss = log_norm + np.sum(weights * (model_ - images) ** 2) / 2
        # loss is negative logL
        assert_almost_equal(observation.get_log_likelihood(model), -true_loss)
Пример #4
0
def define_model(images,weights,psf="startpsf.npy"):
    """ Create model psf and obsevation
    """
    start_psf = np.load(psf)
    out = np.outer(np.ones(len(images)),start_psf)
    # WARNING, using same arbitray psf for all now.
    out.shape = (len(images),start_psf.shape[0],start_psf.shape[1])
    psfs = scarlet.PSF(out)
    model_psf = scarlet.PSF(partial(scarlet.psf.gaussian, sigma=.8),
                            shape=(None, 8, 8))
    model_frame = scarlet.Frame(
                  images.shape,
                  psfs=model_psf)
    observation = scarlet.Observation(
                  images,
                  weights=weights,
                  psfs=psfs).match(model_frame)
    return model_frame, observation
Пример #5
0
    def get_psfs(self, shape, sigmas):

        shape_ = (None, *shape)
        psfs = np.array([
            scarlet.PSF(partial(scarlet.psf.gaussian, sigma=s), shape=shape_).image[0]
            for s in sigmas
        ])

        psfs /= psfs.sum(axis=(1, 2))[:, None, None]
        return psfs
Пример #6
0
    def test_threshold(self):
        # Use a random seed in the test to prevent race conditions
        np.random.seed(0)
        noise = np.random.rand(21, 21) * 2  # noise background to eliminate
        signal = np.zeros(noise.shape)
        func = scarlet.psf.gaussian
        signal[7:14, 7:14] = 10 * scarlet.PSF(
            func, shape=(None, 21, 21)).image[0, 7:14, 7:14]
        X = signal + noise

        step = 0
        constraint = scarlet.ThresholdConstraint()
        X_ = constraint(X, step)

        # regression test with thresh from reference version
        thresh = 0.05704869232578929
        mask = X < thresh
        assert all(X_[mask] == 0)
        assert_array_equal(X_[~mask], X[~mask])