コード例 #1
0
ファイル: test_bbox.py プロジェクト: lsst/scarlet
    def test_from_data(self):
        x = np.arange(25).reshape(5, 5)
        x[0] = 0
        x[:, -2:] = 0
        bbox = scarlet.Box.from_data(x)
        assert bbox == scarlet.Box((4, 3), origin=(1, 0))

        x += 10
        bbox = scarlet.Box.from_data(x)
        assert bbox == scarlet.Box((5, 5), origin=(0, 0))

        bbox = scarlet.Box.from_data(x, min_value=10)
        assert bbox == scarlet.Box((4, 3), origin=(1, 0))
コード例 #2
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)
コード例 #3
0
ファイル: test_component.py プロジェクト: gcmshadow/scarlet
    def test_model(self):
        frame_shape = (10, 20, 30)
        frame = scarlet.Frame(frame_shape)

        shape = (5, 4, 6)
        on_location = (1, 2, 3)
        sed = np.zeros(shape[0])
        sed[on_location[0]] = 1

        fparams = np.array(on_location[1:])

        def f(*params):
            morph = np.zeros(shape[1:])
            morph[tuple(params)] = 1
            return morph

        sed = scarlet.Parameter(sed)
        fparams = scarlet.Parameter(fparams)
        origin = (2, 3, 4)
        bbox = scarlet.Box(shape, origin=origin)

        component = scarlet.FunctionComponent(frame,
                                              sed,
                                              fparams,
                                              f,
                                              bbox=bbox)
        model = component.get_model()

        # everything zero except at one location?
        test_loc = tuple(np.array(on_location) + np.array(origin))
        mask = np.zeros(model.shape, dtype='bool')
        mask[test_loc] = True
        assert_array_equal(model[~mask], 0)
        assert model[test_loc] == 1
コード例 #4
0
    def __init__(self,
                 model_frame,
                 sky_coord,
                 observations,
                 profile,
                 thresh=1.0,
                 shifting=False):

        if not hasattr(observations, "__iter__"):
            observations = (observations, )

        detect = np.zeros(model_frame.shape, dtype=model_frame.dtype)
        for i, obs in enumerate(observations):

            if isinstance(obs.renderer, ConvolutionRenderer):

                data_slice, model_slice = obs.renderer.slices
                obs.renderer.map_channels(detect)[model_slice] = \
                    (np.ones(obs.shape[0])[:,None,None]*profile[None, :,:])[data_slice]

        # initialize morphology
        # make monotonic morphology, trimmed to box with pixels above std
        morph, bbox = self.init_morph(
            model_frame,
            sky_coord,
            detect[-1],
        )

        center = model_frame.get_pixel(sky_coord)
        morphology = scarlet.ExtendedSourceMorphology(
            model_frame,
            center,
            morph,
            bbox=bbox,
            monotonic="angle",
            symmetric=False,
            min_grad=0,
            shifting=shifting,
        )

        # find best-fit spectra for morph from init coadd
        # assumes img only has that source in region of the box
        detect_all, std_all = init.build_initialization_image(observations)
        box_3D = scarlet.Box((model_frame.C, )) @ bbox
        boxed_detect = box_3D.extract_from(detect_all)
        spectrum = init.get_best_fit_spectrum((morph, ), boxed_detect)
        noise_rms = np.concatenate([
            np.array(np.mean(obs.noise_rms, axis=(1, 2)))
            for obs in observations
        ]).reshape(-1)
        spectrum = scarlet.TabulatedSpectrum(model_frame,
                                             spectrum,
                                             min_step=noise_rms)

        # set up model with its parameters
        super().__init__(model_frame, spectrum, morphology)

        # retain center as attribute
        self.center = morphology.center
コード例 #5
0
ファイル: test_bbox.py プロジェクト: lsst/scarlet
    def test_contains(self):
        bbox = scarlet.Box((6, 4, 3), origin=(0, 1, 0))
        p = (2, 2, 2)
        assert bbox.contains(p)

        p = (3, 0, 3)
        assert not bbox.contains(p)

        p = (7, 3, 3)
        assert not bbox.contains(p)

        p = (3, 3, -1)
        assert not bbox.contains(p)
コード例 #6
0
ファイル: test_component.py プロジェクト: gcmshadow/scarlet
    def test_model(self):
        frame_shape = (10, 20, 30)
        frame = scarlet.Frame(frame_shape)

        shape = (5, 4, 6)
        on_location = (1, 2, 3)
        cube = np.zeros(shape)
        cube[on_location] = 1
        cube = scarlet.Parameter(cube)
        origin1 = (2, 3, 4)
        bbox1 = scarlet.Box(shape, origin=origin1)
        component1 = scarlet.CubeComponent(frame, cube, bbox=bbox1)

        sed = np.zeros(shape[0])
        sed[on_location[0]] = 1
        morph = np.zeros(shape[1:])
        morph[on_location[1:]] = 1

        sed = scarlet.Parameter(sed)
        morph = scarlet.Parameter(morph)

        origin2 = (5, 6, 7)
        bbox2 = scarlet.Box(shape, origin=origin2)
        component2 = scarlet.FactorizedComponent(frame, sed, morph, bbox=bbox2)

        tree = scarlet.ComponentTree([component1, component2])
        model = tree.get_model()

        # everything zero except at one location?
        test_locs = [
            tuple(np.array(on_location) + np.array(origin1)),
            tuple(np.array(on_location) + np.array(origin2))
        ]
        mask = np.zeros(model.shape, dtype='bool')
        for test_loc in test_locs:
            mask[test_loc] = True
        assert_array_equal(model[~mask], 0)
        assert_array_equal(model[mask], 1)
コード例 #7
0
ファイル: test_component.py プロジェクト: lsst/scarlet
    def test_model(self):
        frame_shape = (10, 20, 30)
        frame = scarlet.Frame(frame_shape, channels=np.arange(10))

        shape = (5, 4, 6)
        origin = (2, 3, 4)
        box = scarlet.Box(shape, origin=origin)

        on_location = (1, 2, 3)
        sed = np.zeros(shape[0])
        sed[on_location[0]] = 1
        spectrum = scarlet.TabulatedSpectrum(frame, sed, bbox=box[0])

        # construct functional morphology where the parameter sets
        # the location of single pixel that is on
        class OnePixelMorphology(scarlet.Morphology):
            def __init__(self, model_frame, on_pixel, bbox=None):
                self._bbox = bbox
                self._on_pixel = scarlet.Parameter(on_pixel,
                                                   step=1,
                                                   name="on_pixel")
                super().__init__(model_frame, self._on_pixel, bbox=bbox)

            def get_model(self, *params):
                on_pixel = self._on_pixel
                for p in params:
                    if p._value.name == "on_pixel":
                        on_pixel = p

                morph = np.zeros(self._bbox.shape)
                morph[tuple(np.round(on_pixel).astype("int"))] = 1
                return morph

        morphology = OnePixelMorphology(frame,
                                        np.array(on_location[1:],
                                                 dtype="float"),
                                        bbox=box[1:])
        component = scarlet.FactorizedComponent(frame, spectrum, morphology)
        model = component.get_model(frame=frame)

        # everything zero except at one location?
        test_loc = tuple(np.array(on_location) + np.array(origin))
        mask = np.zeros(model.shape, dtype="bool")
        mask[test_loc] = True
        assert_array_equal(model[~mask], 0)
        assert model[test_loc] == 1
コード例 #8
0
ファイル: test_component.py プロジェクト: lsst/scarlet
    def test_model(self):
        frame_shape = (10, 20, 30)
        frame = scarlet.Frame(frame_shape, channels=np.arange(10))

        shape = (5, 4, 6)
        on_location = (1, 2, 3)
        sed = np.zeros(shape[0])
        sed[on_location[0]] = 1
        morph = np.zeros(shape[1:])
        morph[on_location[1:]] = 1

        origin = (2, 3, 4)
        box = scarlet.Box(shape, origin=origin)
        spectrum = scarlet.TabulatedSpectrum(frame, sed, bbox=box[0])
        morphology = scarlet.ImageMorphology(frame, morph, bbox=box[1:])

        component = scarlet.FactorizedComponent(frame, spectrum, morphology)
        model = component.get_model(frame=frame)

        # everything zero except at one location?
        test_loc = tuple(np.array(on_location) + np.array(origin))
        mask = np.zeros(model.shape, dtype="bool")
        mask[test_loc] = True
        assert_array_equal(model[~mask], 0)
        assert model[test_loc] == 1

        # now with shift
        shift_loc = (0, 1, 0)
        shift = scarlet.Parameter(np.array(shift_loc[1:]),
                                  step=0.1,
                                  name="shift")
        morphology = scarlet.ImageMorphology(frame,
                                             morph,
                                             bbox=box[1:],
                                             shift=shift)

        component = scarlet.FactorizedComponent(frame, spectrum, morphology)
        model = component.get_model(frame=frame)

        # everything zero except at one location?
        test_loc = tuple(
            np.array(on_location) + np.array(origin) + np.array(shift_loc))
        mask = np.zeros(model.shape, dtype="bool")
        mask[test_loc] = True
        assert_almost_equal(model[~mask], 0)
        assert_almost_equal(model[test_loc], 1)
コード例 #9
0
ファイル: test_component.py プロジェクト: gcmshadow/scarlet
    def test_model(self):
        frame_shape = (10, 20, 30)
        frame = scarlet.Frame(frame_shape)

        shape = (5, 4, 6)
        on_location = (1, 2, 3)
        sed = np.zeros(shape[0])
        sed[on_location[0]] = 1
        morph = np.zeros(shape[1:])
        morph[on_location[1:]] = 1

        sed = scarlet.Parameter(sed)
        morph = scarlet.Parameter(morph)
        origin = (2, 3, 4)
        bbox = scarlet.Box(shape, origin=origin)

        component = scarlet.FactorizedComponent(frame, sed, morph, bbox=bbox)
        model = component.get_model()

        # everything zero except at one location?
        test_loc = tuple(np.array(on_location) + np.array(origin))
        mask = np.zeros(model.shape, dtype='bool')
        mask[test_loc] = True
        assert_array_equal(model[~mask], 0)
        assert model[test_loc] == 1

        # now with shift
        shift_loc = (0, 1, 0)
        shift = scarlet.Parameter(np.array(shift_loc[1:]))
        component = scarlet.FactorizedComponent(frame,
                                                sed,
                                                morph,
                                                shift=shift,
                                                bbox=bbox)
        model = component.get_model()

        # everything zero except at one location?
        test_loc = tuple(
            np.array(on_location) + np.array(origin) + np.array(shift_loc))
        mask = np.zeros(model.shape, dtype='bool')
        mask[test_loc] = True
        assert_almost_equal(model[~mask], 0)
        assert_almost_equal(model[test_loc], 1)
コード例 #10
0
ファイル: test_component.py プロジェクト: gcmshadow/scarlet
    def test_model(self):
        frame_shape = (10, 20, 30)
        frame = scarlet.Frame(frame_shape)

        shape = (5, 4, 6)
        cube = np.zeros(shape)
        on_location = (1, 2, 3)
        cube[on_location] = 1
        cube = scarlet.Parameter(cube)
        origin = (2, 3, 4)
        bbox = scarlet.Box(shape, origin=origin)

        component = scarlet.CubeComponent(frame, cube, bbox=bbox)
        model = component.get_model()

        # everything zero except at one location?
        test_loc = tuple(np.array(on_location) + np.array(origin))
        mask = np.zeros(model.shape, dtype='bool')
        mask[test_loc] = True
        assert_array_equal(model[~mask], 0)
        assert model[test_loc] == 1
コード例 #11
0
ファイル: test_component.py プロジェクト: lsst/scarlet
    def test_model(self):
        frame_shape = (10, 20, 30)
        frame = scarlet.Frame(frame_shape, channels=np.arange(10))

        shape = (5, 4, 6)
        origin = (2, 3, 4)
        box = scarlet.Box(shape, origin=origin)
        on_location1 = (1, 2, 3)
        cube = np.zeros(shape)
        cube[on_location1] = 1
        cube = scarlet.Parameter(cube, name="cube")
        component1 = scarlet.CubeComponent(frame, cube, bbox=box)

        # make factorized component with a different origin
        on_location2 = (1, 1, 1)
        sed = np.zeros(shape[0])
        sed[on_location2[0]] = 1
        morph = np.zeros(shape[1:])
        morph[on_location2[1:]] = 1

        spectrum = scarlet.TabulatedSpectrum(frame, sed, bbox=box[0])
        morphology = scarlet.ImageMorphology(frame, morph, bbox=box[1:])
        component2 = scarlet.FactorizedComponent(frame, spectrum, morphology)

        combined = scarlet.CombinedComponent([component1, component2])
        model = combined.get_model(frame=frame)

        # everything zero except at one location?
        test_locs = [
            tuple(np.array(on_location1) + np.array(origin)),
            tuple(np.array(on_location2) + np.array(origin)),
        ]
        mask = np.zeros(model.shape, dtype="bool")
        for test_loc in test_locs:
            mask[test_loc] = True
        assert_array_equal(model[~mask], 0)
        assert_array_equal(model[mask], 1)