Beispiel #1
0
def define_sources(srcdic,model_frame,observation):
    """
    """
    sources = []

    for key, loc in srcdic.items():
        if key == 'point':
            for x,y in loc:
                new_source = scarlet.PointSource(model_frame,
                                                 (y, x),
                                                 observation)
                sources.append(new_source)
        elif key == 'extended':
            for x,y in loc:
                new_source = scarlet.ExtendedSource(model_frame,
                                                    (y, x),
                                                    observation,
                                                    shifting=True)
                sources.append(new_source)
        elif key == 'multi':
            for x,y in loc:
                new_source = scarlet.MultiComponentSource(model_frame,
                                                          (y, x),
                                                          observation,
                                                          shifting=True)
                sources.append(new_source)
    return sources
Beispiel #2
0
    def _get_model(self):
        im, psf_im, coords, dims, sing_im, sing_coord = Simulation.__call__(
            self)
        bg_rms = self['Image']['Bgrms']
        mode = self['Mode']
        if mode == 'scarlet':
            #constraints = {"S": None, "m": {'use_nearest': False}, "+": None}
            #constraints['l1'] = bg_rms
            #psf_dims = np.shape(psf_im)
            #psf_im3d = psf_im.reshape( (1, psf_dims[0], psf_dims[1]) )
            #target_psf = scarlet.psf_match.fit_target_psf(psf_im3d, scarlet.psf_match.gaussian)
            #diff_kernels, psf_blend = scarlet.psf_match.build_diff_kernels(psf_im3d, target_psf)
            sources = [
                scarlet.ExtendedSource(coord, im, [bg_rms]) for coord in coords
            ]  #,psf=diff_kernels) for coord in coords]
            #config = scarlet.Config(edge_flux_thresh=0.05)
            config = scarlet.Config(source_sizes=[25
                                                  ])  #,edge_flux_thresh=0.05)
            #scarlet.ExtendedSource.shift_center=0.0
            blend = scarlet.Blend(sources, im, bg_rms=[bg_rms], config=config)
            blend.fit(10000, e_rel=1e-3)
            model = blend.get_model()
            mod1 = blend.get_model(m=0)
            #mod2 = blend.get_model(m=1)
            cen_mod = sources[0].get_model()
            #neigh_mod = sources[1].get_model()
            #steps_used = blend.it

        #return im,psf_im,model,mod1,mod2,cen_mod,neigh_mod,coords,sing_im,sing_coord
        return im, psf_im, model, mod1, cen_mod, coords, sing_im, sing_coord
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
def scarlet_initialize(images, peaks,
                       bg_rms):
    """ 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.
    """
    sources = []
    for n, peak in enumerate(peaks):
        try:
            result = scarlet.ExtendedSource(
                (peak[1], peak[0]),
                images,
                bg_rms)
            sources.append(result)
        except scarlet.source.SourceInitError:
            print("No flux in peak {0} at {1}".format(n, peak))
    blend = scarlet.Blend(sources).set_data(images, bg_rms=bg_rms)
    return blend
Beispiel #5
0
    def _get_scar_model(self):
        im, psf_im, coords, dims, dx1, dy1, noise = Simulation.__call__(self)
        bg_rms = self['Image']['Bgrms']
        mode = self['Mode']
        constraints = {"S": None, "m": {'use_nearest': False}, "+": None}
        #constraints['l0'] = bg_rms
        sources = [
            scarlet.ExtendedSource(coord, im, [bg_rms]) for coord in coords
        ]
        #scarlet.ExtendedSource.shift_center=0.0
        #config = scarlet.Config(edge_flux_thresh=0.05)
        blend = scarlet.Blend(sources, im, bg_rms=[bg_rms])  #,config=config)
        blend.fit(10000, e_rel=1e-3)
        model = blend.get_model()
        #plt.imshow(im[0,:,:])
        #plt.colorbar()
        #plt.savefig('test.png')
        mod1 = blend.get_model(m=0)
        mod2 = blend.get_model(m=1)
        cen_mod = sources[0].get_model()
        #output['mod_size_flag'][j] = 0
        #if np.shape(cen_mod) != (1,25,25):
        #output['mod_size_flag'][j] = 3
        neigh_mod = sources[1].get_model()
        #steps_used = blend.it

        return im, psf_im, model, mod1, mod2, cen_mod, neigh_mod, coords, dx1, dy1, noise
    def _get_model(self):
        im,psf_im,coords,dims,sing_im,sing_coord = Simulation.__call__(self)
        bg_rms = self['Image']['Bgrms']
        mode = self['Mode']
        if mode == 'scarlet':
            constraints = {"S": None, "m": {'use_nearest': False}, "+": None}
            #constraints['l1'] = bg_rms
            sources = [scarlet.ExtendedSource(coord, im, [bg_rms]) for coord in coords]
            #config = scarlet.Config(edge_flux_thresh=0.05)
            #scarlet.ExtendedSource.shift_center=0.0
            blend = scarlet.Blend(sources, im, bg_rms=[bg_rms])#,config=config)
            blend.fit(10000, e_rel=1e-3)
            model = blend.get_model()
            mod1 = blend.get_model(m=0)
            #mod2 = blend.get_model(m=1)
            cen_mod = sources[0].get_model()
            #mod_size_flag = 0
            #if np.shape(cen_mod) != (1,25,25):
            #    mod_size_flag = 3
#print("not 25x25")
                #output['mod_size_flag'][j] = 3
            #neigh_mod = sources[1].get_model()
            #steps_used = blend.it

        #return im,psf_im,model,mod1,mod2,cen_mod,neigh_mod,coords,sing_im,sing_coord
        return im,psf_im,model,mod1,cen_mod,coords,sing_im,sing_coord
Beispiel #7
0
    def test_fit_extended_source(self):
        shape = (6, 31, 55)
        coords = [(20, 10), (10, 30), (17, 42)]
        amplitudes = [3, 2, 1]
        result = init_data(shape, coords, amplitudes, dtype=np.float64)
        target_psf, psfs, images, channels, seds, morphs = result
        B, Ny, Nx = shape

        frame = scarlet.Frame(images.shape,
                              psfs=target_psf[None],
                              dtype=np.float64)
        observation = scarlet.Observation(images, psfs=psfs).match(frame)
        bg_rms = np.ones((B, ))
        sources = [
            scarlet.ExtendedSource(frame, coord, observation, bg_rms)
            for coord in coords
        ]
        blend = scarlet.Blend(sources, observation)

        # Scale the input psfs by the observation and model psfs to ensure
        # the sources were initialized correctly
        psf_scale = observation.frame.psfs.max(axis=(1,
                                                     2)) / frame.psfs[0].max()
        scaled_seds = np.array([c.sed * psf_scale for c in blend.components])

        assert_almost_equal(scaled_seds, seds)

        # Fit the model
        blend.fit(100)
        assert blend.it < 20
        mse = np.array(blend.mse[:-1])
        _mse = np.array(blend.mse[1:])
        assert np.all(mse - _mse >= 0)
Beispiel #8
0
    def get_scar_model(self):
        import scarlet
        import scarlet.constraint as sc
        im, psf_im, coords, dims, dx1, dy1, noise = self.sim()
        bg_rms = self.sim['Image']['Bgrms']
        bg_rms = bg_rms / np.sqrt(len(im))
        mode = self.sim['Mode']

        #create multiband background im
        bg = np.zeros((len(im)))
        bg += bg_rms

        #choose constraints for PSF and sources (optional)
        #psf_constraints = ()#sc.SimpleConstraint())
        #source_constraints = ()#sc.SimpleConstraint(),sc.DirectSymmetryConstraint()) #sc.DirectMonotonicityConstraint(use_nearest=False)

        config = scarlet.Config(source_sizes=[25])

        #create multiband PSF image
        #psf_dims = np.shape(psf_im)
        #psf_im3d = psf_im.reshape( (1, psf_dims[0], psf_dims[1]) )
        #psfs = np.zeros((len(im), psf_dims[0],psf_dims[1]))
        #psfs += psf_im3d

        #PSF Matching
        #target_psf = scarlet.psf_match.fit_target_psf(psfs,
        #                                scarlet.psf_match.gaussian)
        #diff_kernels, psf_blend = scarlet.psf_match.build_diff_kernels(psfs,
        #                                    target_psf)
        #if use PSF matching, set psf = diff_kernels below
        sources = [
            scarlet.ExtendedSource(coord,
                                   im,
                                   bg_rms=bg,
                                   psf=None,
                                   config=config) for coord in coords
        ]
        #config = scarlet.Config(edge_flux_thresh=0.05)
        blend = scarlet.Blend(sources)
        blend.set_data(im, bg_rms=bg, config=config)
        blend.fit(10000, e_rel=1e-3)

        #get full sized scarlet models
        model = blend.get_model()
        mod1 = blend.get_model(0)
        mod2 = blend.get_model(1)

        #get postage stamp models
        cen_mod = sources[0].get_model()
        neigh_mod = sources[1].get_model()

        #get scarlet coordinates
        cen_cen_pos = blend[0][0].center
        neigh_cen_pos = blend[1][0].center

        return im, psf_im, model, mod1, mod2, cen_mod, neigh_mod, coords, dx1, dy1, noise, cen_cen_pos, neigh_cen_pos
    def _get_model(self):
        im, psf_im, coords, dims, sing_im, sing_coord = Simulation.__call__(
            self)
        bg_rms = self['Image']['Bgrms']
        mode = self['Mode']
        #constraints = {"S": None, "m": {'use_nearest': False}, "+": None}
        constraint = (sc.SimpleConstraint())
        #constraint = PSFConstraint()
        config = (scarlet.Config(source_sizes=[25]))
        #psf_dims = np.shape(psf_im)
        #psf_im3d = psf_im.reshape( (1, psf_dims[0], psf_dims[1]) )
        #target_psf = scarlet.psf_match.fit_target_psf(psf_im3d, scarlet.psf_match.gaussian)
        #diff_kernels, psf_blend = scarlet.psf_match.build_diff_kernels(psf_im3d, target_psf,constraints=constraint)

        #constraint =  (sc.L0Constraint(.5*bg_rms))
        #& sc.DirectSymmetryConstraint(sigma=0.25))
        #& sc.MonotonicityConstraint(use_nearest=True))
        #constraints['l1'] = bg_rms
        sources = [
            scarlet.ExtendedSource(coord,
                                   im, [bg_rms],
                                   psf=None,
                                   config=config,
                                   shift_center=0.0) for coord in coords
        ]
        #config = scarlet.Config(edge_flux_thresh=1.25)
        #scarlet.ExtendedSource.shift_center=0.0
        blend = scarlet.Blend(sources)
        blend.set_data(im, bg_rms=[bg_rms], config=config)
        blend.fit(10000, e_rel=1e-3)
        model = blend.get_model()
        mod1 = blend.get_model(0)
        #mod2 = blend.get_model(1)
        cen_mod = sources[0].get_model()
        #cen_pos = sources[0].center
        #neigh_mod = sources[1].get_model()
        #print(cen_pos[0]-coords[0][0],cen_pos[1]-coords[0][1])
        #psf_model = psf_blend.get_model()
        """
        masked_mod = np.ma.masked_equal(diff_kernels,0.0)
        f,ax = plt.subplots(1,2,figsize=(8,4))
        f1 = ax[0].imshow(psf_im3d[0,:,:])
        ax[0].set_title("Orig PSF")
        plt.colorbar(f1,ax=ax[0])
        f2 = ax[1].imshow(masked_mod[0,:,:])
        plt.colorbar(f2,ax=ax[1])
        ax[1].set_title("Diff Kernels")
        plt.tight_layout()
        f.savefig('test.png')
        plt.close()
        steps_used = blend.it
        print(steps_used)
        """
        #return im,psf_im,model,mod1,mod2,cen_mod,neigh_mod,coords,sing_im,sing_coord
        return im, psf_im, model, mod1, cen_mod, coords, sing_im, sing_coord
Beispiel #10
0
def initialize(images, peaks, diff_kernels, bg_rms):
    sources = []
    rejected_sources = []
    for n, peak in enumerate(peaks):
        try:
            result = scarlet.ExtendedSource((peak[1], peak[0]),
                                            images,
                                            bg_rms,
                                            psf=diff_kernels)
            sources.append(result)
        except scarlet.source.SourceInitError:
            rejected_sources.append(n)
            print("No flux in peak {0} at {1}".format(n, peak))
    blend = scarlet.Blend(sources, images, bg_rms=bg_rms)
    return blend, rejected_sources
 def get_scar_model(self):
     import scarlet
     import scarlet.constraint as sc
     im, psf_im, coords, dims, dx1, dy1, noise = self.sim()
     bg_rms = self.sim['Image']['Bgrms']
     mode = self.sim['Mode']
     bg = np.zeros((len(im)))
     bg += bg_rms
     #psf_constraints = (sc.SimpleConstraint())
     #source_constraints = ()#sc.SimpleConstraint(),sc.DirectSymmetryConstraint()) #sc.DirectMonotonicityConstraint(use_nearest=False)
     config = scarlet.Config(source_sizes=[25])
     psf_dims = np.shape(psf_im)
     psf_im3d = psf_im.reshape((1, psf_dims[0], psf_dims[1]))
     psfs = np.zeros((len(im), psf_dims[0], psf_dims[1]))
     psfs += psf_im3d
     #target_psf = scarlet.psf_match.fit_target_psf(psfs,
     #                                scarlet.psf_match.gaussian)
     #diff_kernels, psf_blend = scarlet.psf_match.build_diff_kernels(psfs,
     #                        target_psf, constraints = None)
     sources = [
         scarlet.ExtendedSource(coord,
                                im,
                                bg_rms=bg,
                                psf=None,
                                config=config) for coord in coords
     ]
     #config = scarlet.Config(edge_flux_thresh=0.05)
     blend = scarlet.Blend(sources)
     blend.set_data(im, bg_rms=bg, config=config)
     blend.fit(10000, e_rel=1e-3)
     model = blend.get_model()
     mod1 = blend.get_model(0)
     #mod2 = blend.get_model(1)
     cen_mod = sources[0].get_model()
     #neigh_mod = sources[1].get_model()
     #psf_model = psf_blend.get_model()
     #steps_used = blend.it
     cen_cen_pos = blend[0][0].center
     #return im,psf_im,model,mod1,mod2,cen_mod,neigh_mod,coords,dx1,dy1,noise
     return im, psf_im, model, mod1, cen_mod, coords, dx1, dy1, noise, cen_cen_pos, psfs
    def get_scar_model(self):
        import scarlet

        im, psf_im, coords, dims, dx1, dy1, noise, sep_coords = self.sim()
        bg_rms = self.sim['Image']['Bgrms']
        mode = self.sim['Mode']
        #constraints = {"S": None, "m": {'use_nearest': False}, "+": None}
        #constraints['l0'] = bg_rms
        constraint = (sc.SimpleConstraint())
        #                       & sc.MonotonicityConstraint(use_nearest=True))
        config = scarlet.Config(source_sizes=[25])
        psf_dims = np.shape(psf_im)
        psf_im3d = psf_im.reshape((1, psf_dims[0], psf_dims[1]))
        target_psf = scarlet.psf_match.fit_target_psf(
            psf_im3d, scarlet.psf_match.gaussian)
        diff_kernels, psf_blend = scarlet.psf_match.build_diff_kernels(
            psf_im3d, target_psf, constraints=constraint.copy())
        sources = [
            scarlet.ExtendedSource(coord,
                                   im, [bg_rms],
                                   psf=diff_kernels,
                                   config=config) for coord in coords
        ]
        #sources = [scarlet.ExtendedSource(coord, im, [bg_rms]) for coord in coords]
        #scarlet.ExtendedSource.shift_center=0.0
        #config = scarlet.Config(edge_flux_thresh=0.05)
        blend = scarlet.Blend(sources, im, bg_rms=[bg_rms], config=config)
        blend.fit(10000, e_rel=1e-3)
        model = blend.get_model()
        mod1 = blend.get_model(m=0)
        mod2 = blend.get_model(m=1)
        cen_mod = sources[0].get_model()
        neigh_mod = sources[1].get_model()
        #steps_used = blend.it

        return im, psf_im, model, mod1, mod2, cen_mod, neigh_mod, coords, dx1, dy1, noise, sep_coords
Beispiel #13
0
    def test_init_extended(self):
        shape = (5, 11, 15)
        B, Ny, Nx = shape

        x = np.linspace(-2, 2, 5)
        y = np.linspace(-2, 2, 5)
        x, y = np.meshgrid(x, y)
        r = np.sqrt(x**2 + y**2)

        true_sed = np.arange(B)
        true_morph = np.zeros(shape[1:])

        skycoord = (np.array(true_morph.shape) - 1) // 2
        cy, cx = skycoord
        true_morph[cy - 2:cy + 3, cx - 2:cx + 3] = 3 - r

        morph = true_morph.copy()
        morph[5, 3] = 10

        # Test function
        images = true_sed[:, None, None] * morph[None, :, :]
        frame = scarlet.Frame(shape)
        observation = scarlet.Observation(images).match(frame)
        bg_rms = np.ones_like(true_sed) * 1e-3
        sed, morph = scarlet.source.init_extended_source(
            skycoord, frame, observation, bg_rms)

        assert_array_equal(sed / 3, true_sed)
        assert_almost_equal(morph * 3, true_morph)

        # Test ExtendedSource.__init__
        src = scarlet.ExtendedSource(frame, skycoord, observation, bg_rms)
        assert_array_equal(src.pixel_center, skycoord)
        assert src.symmetric is True
        assert src.monotonic is True
        assert src.center_step == 5
        assert src.delay_thresh == 10

        assert_array_equal(src.sed / 3, true_sed)
        assert_almost_equal(src.morph * 3, true_morph)

        # Test monotonicity
        morph = true_morph.copy()
        morph[5, 5] = 2

        images = true_sed[:, None, None] * morph[None, :, :]
        frame = scarlet.Frame(shape)
        observation = scarlet.Observation(images).match(frame)
        bg_rms = np.ones_like(true_sed) * 1e-3
        sed, morph = scarlet.source.init_extended_source(skycoord,
                                                         frame,
                                                         observation,
                                                         bg_rms,
                                                         symmetric=False)

        _morph = true_morph.copy()
        _morph[5, 5] = 1.5816233815926433
        assert_array_equal(sed / 3, true_sed)
        assert_almost_equal(morph * 3, _morph)

        # Test symmetry
        morph = true_morph.copy()
        morph[5, 5] = 2

        images = true_sed[:, None, None] * morph[None, :, :]
        frame = scarlet.Frame(shape)
        observation = scarlet.Observation(images).match(frame)
        bg_rms = np.ones_like(true_sed) * 1e-3
        sed, morph = scarlet.source.init_extended_source(skycoord,
                                                         frame,
                                                         observation,
                                                         bg_rms,
                                                         monotonic=False)

        assert_array_equal(sed / 3, true_sed)
        assert_almost_equal(morph * 3, true_morph)