def interpolate(data_lr, data_hr): ''' Interpolate low resolution data to high resolution Parameters ---------- data_lr: Data low resolution Data data_hr: Data high resolution Data Result ------ interp: numpy array the images in data_lr interpolated to the grid of data_hr ''' frame_lr = scarlet.Frame(data_lr.images.shape, wcs=data_lr.wcs, channels=data_lr.channels) frame_hr = scarlet.Frame(data_hr.images.shape, wcs=data_hr.wcs, channels=data_hr.channels) coord_lr0 = (np.arange(data_lr.images.shape[1]), np.arange( data_lr.images.shape[1])) coord_hr = (np.arange(data_hr.images.shape[1]), np.arange( data_hr.images.shape[1])) coord_lr = scarlet.resampling.convert_coordinates( coord_lr0, frame_lr, frame_hr) interp = [] for image in data_lr.images: interp.append(scarlet.interpolation.sinc_interp( image[None, :, :], coord_hr, coord_lr, angle=None)[0].T) return np.array(interp)
def test_frame(self): # Minimal initialization shape = (5, 11, 13) frame = scarlet.Frame(shape) wcs = get_airy_wcs() psfs = np.arange(1, 716).reshape(5, 11, 13) norm_psfs = psfs / psfs.sum(axis=(1, 2))[:, None, None] assert frame.C == 5 assert frame.Ny == 11 assert frame.Nx == 13 assert frame.shape == shape assert frame.psfs is None assert_array_equal(frame.get_pixel((5.1, 1.3)), (5, 1)) # Full initialization frame = scarlet.Frame(shape, wcs=wcs, psfs=norm_psfs) assert frame.C == 5 assert frame.Ny == 11 assert frame.Nx == 13 assert frame.shape == shape assert_almost_equal(frame.psfs.sum(axis=(1, 2)), [1] * 5) skycoord = [210.945, -73.1] assert_array_equal(frame.get_pixel(skycoord), [-110, -202])
def test_point_source(self): shape = (5, 11, 21) coords = [(4, 8), (8, 11), (5, 16)] B, Ny, Nx = shape seds, morphs, images = create_sources(shape, coords, [2, 3, .1]) psfs = np.array([[[.25, .5, .25], [.5, 1, .5], [.25, .5, .25]]]) psfs /= psfs.sum(axis=(1, 2))[:, None, None] frame = scarlet.Frame(images.shape) obs = scarlet.Observation(images).match(frame) src = scarlet.PointSource(frame, coords[0], obs) truth = np.zeros_like(src.morph) truth[coords[0]] = 1 assert_array_equal(src.sed, seds[0]) assert_array_equal(src.morph, truth) assert src.pixel_center == coords[0] assert src.symmetric is True assert src.monotonic is True assert src.center_step == 5 assert src.delay_thresh == 10 # frame PSF same as source frame = scarlet.Frame(images.shape, psfs=psfs) src = scarlet.PointSource(frame, coords[0], obs) # We need to multiply by 4 because of psf normalization assert_almost_equal(src.sed * 4, seds[0]) assert_almost_equal(morphs[0], src.morph) assert src.pixel_center == coords[0]
def test_fit_point_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) sources = [ scarlet.PointSource(frame, coord, observation) for coord in coords ] blend = scarlet.Blend(sources, observation) # Try to run for 10 iterations # Since the model is already near exact, it should converge # on the 2nd iteration (since it doesn't calculate the initial loss) blend.fit(10) assert blend.it == 2 assert_almost_equal(blend.mse, [3.875628098330452e-15, 3.875598349723412e-15], decimal=10) assert blend.mse[0] > blend.mse[1]
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)
def test_non_negativity(self): shape = (6, 3, 3) frame = scarlet.Frame(shape) sed = np.array([-.1, .1, 4, -.2, .2, 0], dtype=frame.dtype) morph = np.array([[-1, -.5, -1], [.1, 2, .3], [-.5, .3, 0]], dtype=frame.dtype) # Test SED only src = scarlet.Component(frame, sed.copy(), morph.copy()) update.positive_sed(src) np.testing.assert_array_almost_equal(src.sed, [0, .1, 4, 0, .2, 0]) np.testing.assert_array_equal(src.morph, morph) # Test morph only src = scarlet.Component(frame, sed.copy(), morph.copy()) update.positive_morph(src) np.testing.assert_array_equal(src.sed, sed) np.testing.assert_array_almost_equal( src.morph, [[0, 0, 0], [.1, 2, .3], [0, .3, 0]]) # Test SED and morph src = scarlet.Component(frame, sed.copy(), morph.copy()) update.positive(src) np.testing.assert_array_almost_equal(src.sed, [0, .1, 4, 0, .2, 0]) np.testing.assert_array_almost_equal( src.morph, [[0, 0, 0], [.1, 2, .3], [0, .3, 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)
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 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
def test_sparsity(self): shape = (6, 5, 5) frame = scarlet.Frame(shape) sed = np.arange(shape[0]) morph = np.arange(shape[1] * shape[2], dtype=float).reshape(shape[1], shape[2]) # Test l0 sparsity src = scarlet.Component(frame, sed.copy(), morph.copy()) src.L_morph = 1 update.sparse_l0(src, thresh=4) true_morph = morph.copy() true_morph[0, :-1] = 0 np.testing.assert_array_equal(src.sed, sed) np.testing.assert_array_equal(src.morph, true_morph) # Test l1 sparsity src = scarlet.Component(frame, sed.copy(), morph.copy()) src.L_morph = 0.5 update.sparse_l1(src, thresh=2) true_morph = np.zeros((morph.size)) true_morph[5:] = np.arange(20) + 1 true_morph = true_morph.reshape(5, 5) np.testing.assert_array_equal(src.sed, sed) np.testing.assert_array_equal(src.morph, true_morph)
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
def test_normalized(self): shape = (6, 5, 5) frame = scarlet.Frame(shape) sed = np.arange(shape[0], dtype=frame.dtype) morph = np.arange(shape[1] * shape[2], dtype=frame.dtype).reshape(shape[1], shape[2]) # Test SED normalization src = scarlet.Component(frame, sed.copy(), morph.copy()) update.normalized(src, type='sed') np.testing.assert_array_equal(src.sed, sed / 15) np.testing.assert_array_equal(src.morph, morph * 15) # Test morph unity normalization src = scarlet.Component(frame, sed.copy(), morph.copy()) update.normalized(src, type='morph') norm = np.sum(morph) np.testing.assert_array_equal(src.sed, sed * norm) np.testing.assert_array_equal(src.morph, morph / norm) # Test morph max normalization src = scarlet.Component(frame, sed.copy(), morph.copy()) update.normalized(src) np.testing.assert_array_equal(src.sed, sed * 24) np.testing.assert_array_equal(src.morph, morph / 24) with pytest.raises(ValueError): update.normalized(src, type='fubar')
def test_symmetry(self): shape = (6, 5, 5) frame = scarlet.Frame(shape) sed = np.arange(shape[0]) morph = np.arange(shape[1] * shape[2], dtype=float).reshape(shape[1], shape[2]) # Centered symmetry src = scarlet.Component(frame, sed.copy(), morph.copy()) src.L_morph = 1 src.pixel_center = (2, 2) update.symmetric(src, src.pixel_center) result = np.ones_like(morph) * 12 np.testing.assert_array_equal(src.morph, result) # Centered symmetry at half strength src = scarlet.Component(frame, sed.copy(), morph.copy()) src.L_morph = 1 src.pixel_center = (2, 2) update.symmetric(src, src.pixel_center, strength=.5, algorithm="soft") result = [[6.0, 6.5, 7.0, 7.5, 8.0], [8.5, 9.0, 9.5, 10.0, 10.5], [11.0, 11.5, 12.0, 12.5, 13.0], [13.5, 14.0, 14.5, 15.0, 15.5], [16.0, 16.5, 17.0, 17.5, 18.0]] np.testing.assert_array_equal(src.morph, result) # Uncentered symmetry src = scarlet.Component(frame, sed.copy(), morph.copy()) src.L_morph = 1 src.pixel_center = (1, 1) update.symmetric(src, src.pixel_center) result = morph.copy() result[:3, :3] = 6 np.testing.assert_array_equal(src.morph, result)
def test_items(self): shape = (3, 5, 5) frame = scarlet.Frame(shape) sed = np.arange(3, dtype=frame.dtype) morph = np.arange(25, dtype=frame.dtype).reshape(5, 5) c1 = UpdateComponent(frame=frame, sed=sed, morph=morph) c2 = UpdateComponent(frame=frame, sed=sed, morph=morph) c3 = UpdateComponent(frame=frame, sed=sed, morph=morph) c4 = UpdateComponent(frame=frame, sed=sed, morph=morph) c5 = UpdateComponent("morph", frame, sed, morph) tree1 = scarlet.ComponentTree([c1, c2]) tree2 = scarlet.ComponentTree([c3, c4]) # Test iadd tree1 += tree2 assert tree1.n_components == 4 assert tree1.n_sources == 4 assert tree1.components == (c1, c2, c3, c4) assert tree1.sources == (c1, c2, c3, c4) tree1 += c5 assert tree1.n_components == 5 assert tree1.n_sources == 5 assert tree1.components == (c1, c2, c3, c4, c5) assert tree1.sources == (c1, c2, c3, c4, c5) assert tree2.n_components == 2 assert tree2.n_sources == 2 assert tree2.components == (c3, c4) assert tree2.sources == (c3, c4) # Test getitem tree1[0] == c1 tree1[-1] == c5 # Test update tree2.update() sed_norm = sed.sum() morph_norm = morph.sum() assert_array_equal(c1.sed, sed) assert_array_equal(c2.sed, sed) assert_array_equal(c3.sed, sed / sed_norm) assert_array_equal(c4.sed, sed / sed_norm) assert_array_equal(c5.sed, sed) assert_array_equal(c1.morph, morph) assert_array_equal(c2.morph, morph) assert_array_equal(c3.morph, morph * sed_norm) assert_array_equal(c4.morph, morph * sed_norm) assert_array_equal(c5.morph, morph) tree1.update() assert_array_equal(c1.sed, sed / sed_norm) assert_array_equal(c2.sed, sed / sed_norm) assert_array_equal(c3.sed, sed / sed_norm) assert_array_equal(c4.sed, sed / sed_norm) assert_array_equal(c5.sed, sed * morph_norm) assert_array_equal(c1.morph, morph * sed_norm) assert_array_equal(c2.morph, morph * sed_norm) assert_array_equal(c3.morph, morph * sed_norm) assert_array_equal(c4.morph, morph * sed_norm) assert_array_equal(c5.morph, morph / morph_norm)
def test_get_loss(self): shape = (3, 4, 5) frame = scarlet.Frame(shape) images = np.arange(60).reshape(shape) weights = np.ones_like(images) * 2 observation = scarlet.Observation(images, weights=weights).match(frame) model = 4 * np.ones_like(images) true_loss = 0.5 * np.sum((weights * (model - images))**2) assert_almost_equal(true_loss, observation.get_loss(model))
def test_get_best_fit_seds(self): shape = (7, 11, 21) coords = [(4, 8), (8, 11), (5, 16)] seds, morphs, images = create_sources(shape, coords) frame = scarlet.Frame(images.shape) obs = scarlet.Observation(images).match(frame) _seds = scarlet.source.get_best_fit_seds(morphs, frame, obs) assert_array_equal(_seds, seds)
def test_psf_match(self): shape = (43, 43) target_psf = self.get_psfs(shape, [.9])[1] psfs, truth = self.get_psfs(shape, [2.1, 1.1, 3.5]) psfs /= psfs.sum(axis=(1, 2))[:, None, None] frame = scarlet.Frame(psfs.shape, psfs=target_psf) observation = scarlet.Observation(psfs, psfs) observation.match(frame) result = observation.render(np.array([target_psf[0]] * len(psfs))) assert_almost_equal(result, truth)
def test_init(self): # Initialize the model shape = (5, 31, 55) 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) trueSed = np.arange(B) trueMorph = np.zeros(shape[1:]) center = (np.array(trueMorph.shape) - 1) // 2 cy, cx = center trueMorph[cy-2:cy+3, cx-2:cx+3] = 3-r morph = trueMorph.copy() # Make a point that is not monotonic or symmetric to ensure # that it is supressed. morph[5, 3] = 10 # Create the scarlet objects images = trueSed[:, None, None] * morph[None, :, :] frame = scarlet.Frame(shape, channels=np.arange(B)) observation = scarlet.Observation(images, channels=np.arange(B)) # init stack objects foot, peak, bbox = numpyToStack(images, center, (15, 3)) # init source xmin = bbox.getMinX() ymin = bbox.getMinY() center = np.array([peak.getIy()-ymin, peak.getIx()-xmin], dtype=int) src = initSource(frame=frame, center=center, observation=observation, thresh=0, downgrade=False) # scarlet has more flexible models now, # so `sed` and `morph` are no longer attributes, # meaning we have to extract them ourselves. sed = src.children[0].parameters[0]._data morph = src.children[1].parameters[0]._data self.assertFloatsAlmostEqual(sed/3, trueSed) src_morph = np.zeros(frame.shape[1:], dtype=morph.dtype) src_morph[src._model_frame_slices[1:]] = (morph*3)[src._model_slices[1:]] self.assertFloatsAlmostEqual(src_morph, trueMorph, rtol=1e-7) self.assertFloatsEqual(src.center, center) self.assertEqual(foot.getBBox(), bbox)
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)
def test_build_detection_coadd(self): truth = np.array( [[[0.05235454, 0.02073789, 0.04880617, 0.03637619, 0.02399899], [0.03744485, 0.29331713, 0.52876383, 0.28429441, 0.04679640], [0.02611349, 0.52057058, 1.02958156, 0.51620345, 0.02391584], [0.03627858, 0.28669982, 0.54027293, 0.26347546, 0.05124271], [0.03635369, 0.05010319, 0.04445647, 0.04545365, 0.02991638]], [[0.00704491, 0.00566508, 0.00848275, 0.00673316, 0.00564367], [0.00686349, 0.25730076, 0.50470101, 0.25151582, 0.00715177], [0.00951771, 0.50618275, 1.00161330, 0.50362382, 0.00521353], [0.00189936, 0.25494626, 0.50437369, 0.25620321, 0.00515993], [0.00751398, 0.00719382, 0.00812517, 0.00260853, 0.00908961]], [[0.66289178, 0.31370533, 0.62558879, 0.43856888, 0.72209347], [0.96661099, 0.57698197, 1.58224512, 0.93506400, 1.08122335], [0.99298264, 1.26054670, 1.58495813, 1.61148374, 0.82737327], [1.05820433, 0.92412937, 1.24225533, 1.33838207, 0.79615945], [0.82488505, 1.13293652, 0.93197919, 1.37564087, 0.96079598]]]) true_cutoff = np.array([0.03630302, 0.00769658, 0.82658430]) np.random.seed(0) shape = (5, 11, 21) coords = [(4, 8), (8, 11), (5, 16)] B, Ny, Nx = shape K = len(coords) seds, morphs, images = create_sources(shape, coords, [2, 3, .1]) bg_rms = np.arange(1, B + 1) / 10 # Add noise to the image noise = np.random.rand(*shape) * bg_rms[:, None, None] images += noise frame = scarlet.Frame(shape) for k in range(K): observation = scarlet.Observation(images).match(frame) coadd, cutoff = scarlet.source.build_detection_coadd( seds[k], bg_rms, observation) cy, cx = coords[k] window = slice(cy - 2, cy + 3), slice(cx - 2, cx + 3) assert_almost_equal(coadd[window], truth[k]) assert_almost_equal(cutoff, true_cutoff[k]) with pytest.raises(ValueError): scarlet.source.build_detection_coadd(seds[0], np.zeros_like(bg_rms), observation, frame)
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
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
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)
def test_to_heavy(self): shape = (5, 31, 55) B, Ny, Nx = shape coords = [(20, 10), (10, 30), (17, 42)] result = initData(shape, coords, [3, 2, 1]) targetPsfImage, psfImages, images, channels, seds, morphs, targetPsf, psfs = result images = images.astype(np.float32) seds = seds.astype(np.float32) frame = scarlet.Frame(shape, psf=targetPsf, channels=np.arange(B)) observation = scarlet.Observation(images, psf=psfImages, channels=np.arange(B)).match(frame) foot, peak, bbox = numpyToStack(images, coords[0], (15, 3)) xmin = bbox.getMinX() ymin = bbox.getMinY() center = np.array([peak.getIy()-ymin, peak.getIx()-xmin], dtype=int) src = init_source(frame=frame, center=center, observations=[observation], thresh=0) # Convolve the model with the observed PSF model = src.get_model(frame=src.frame) model = observation.render(model)
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
def test_model_render(self): shape = (6, 31, 55) coords = [(20, 10), (10, 30), (17, 42)] result = init_data(shape, coords, [3, 2, 1], dtype=np.float64) target_psf, psfs, images, channels, seds, morphs = result # Test init with psfs frame = scarlet.Frame(images.shape, psfs=target_psf[None], dtype=np.float64) observation = scarlet.Observation(images, psfs=psfs).match(frame) sources = [ scarlet.PointSource(frame, coord, observation) for coord in coords ] blend = scarlet.Blend(sources, observation) model = observation.render(blend.get_model()) assert_almost_equal(images, model, decimal=5) for s0, s in zip(sources, blend.sources): assert_array_equal(s.get_model(), s0.get_model())
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)
def test_to_heavy(self): shape = (5, 31, 55) B, Ny, Nx = shape coords = [(20, 10), (10, 30), (17, 42)] result = initData(shape, coords, [3, 2, 1]) targetPsfImage, psfImages, images, channels, seds, morphs, targetPsf, psfs = result images = images.astype(np.float32) seds = seds.astype(np.float32) frame = scarlet.Frame(shape, psfs=targetPsf, channels=np.arange(B)) observation = scarlet.Observation(images, psfs=psfImages, channels=np.arange(B)).match(frame) foot, peak, bbox = numpyToStack(images, coords[0], (15, 3)) xmin = bbox.getMinX() ymin = bbox.getMinY() center = np.array([peak.getIy()-ymin, peak.getIx()-xmin], dtype=int) src = initSource(frame=frame, center=center, observation=observation, thresh=0, downgrade=False) # Convolve the model with the observed PSF model = src.get_model(frame=src.frame) model = observation.render(model) # Test Model to Heavy filters = [f for f in "grizy"] src.detectedPeak = peak hFoot = mes.source.modelToHeavy(src, filters, bbox.getMin(), observation) hModel = hFoot.getImage(fill=0).image.array self.assertEqual(bbox, hFoot.getBBox()) self.assertFloatsAlmostEqual(hModel, model, rtol=1e-4, atol=1e-4) # Test the peak in each band for single in hFoot: peaks = single.getPeaks() self.assertEqual(len(peaks), 1) hPeak = peaks[0] self.assertEqual(hPeak.getIx()-xmin, coords[0][1]) self.assertEqual(hPeak.getIy()-ymin, coords[0][0])
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)
def test_methods(self): # get_model sed = np.arange(5) morph = np.arange(20).reshape(4, 5) shape = (len(sed), morph.shape[0], morph.shape[1]) frame = scarlet.Frame(shape) component = scarlet.Component(frame, sed, morph) model = component.get_model() truth = sed[:, None, None] * morph[None, :, :] assert_array_equal(model, truth) other_sed = np.ones_like(sed) other_morph = morph + 10 other_truth = other_sed[:, None, None] * other_morph[None, :, :] model = component.get_model() other_model = component.get_model(other_sed, other_morph) assert_array_equal(model, truth) assert_array_equal(other_model, other_truth) # TODO: test component with partial parameter list (issue #121) # get_flux flux = component.get_flux() true_flux = model.sum(axis=(1, 2)) assert_array_equal(flux, true_flux) # empty update test = component.update() assert test is component # Nothing should be changed assert component.frame.C == 5 assert component.frame.Ny == 4 assert component.frame.Nx == 5 assert component.coord is None assert component.step_sed == 1 assert component.step_morph == 1