def apply(self, x, mode): self._check_input(x, mode) if mode == self.TIMES: y = ift.from_global_data(self._target, x.to_global_data()) if mode == self.INVERSE_TIMES: y = ift.from_global_data(self._domain, x.to_global_data()) if mode == self.ADJOINT_TIMES: y = ift.from_global_data(self._domain, x.to_global_data()) if mode == self.ADJOINT_INVERSE_TIMES: y = ift.from_global_data(self._target, x.to_global_data()) return y
def apply(self, x, mode): self._check_mode(mode) if mode == self.TIMES: #Here the ordinary applycation of the shear_operator array = x.to_global_data() sheared = custom_shear(array, self._g1,self._g2) return ift.from_global_data(self._target, sheared) if mode == self.ADJOINT_TIMES: # Here the adjoint is just the inverse, since I have # a unitary operator array = x.to_global_data() inverse_sheared = custom_shear(array, -self._g1,-self._g2) return ift.from_global_data(self._target, inverse_sheared)
def checkerboard_response(position_space): '''Checkerboard mask for 2D mode''' mask = np.ones(position_space.shape) x, y = position_space.shape for i in range(8): for j in range(8): if (i + j) % 2 == 0: mask[i*x//8:(i + 1)*x//8, j*y//8:(j + 1)*y//8] = 0 mask = ift.from_global_data(position_space, mask) return ift.MaskOperator(mask)
def test_dataconv(): f1 = ift.full(dom, 27) f2 = ift.from_global_data(dom, f1.to_global_data()) for key, val in f1.items(): assert_equal(val.local_data, f2[key].local_data) if "d1" not in f2: raise KeyError() assert_equal({"d1": f1}, f2.to_dict()) f3 = ift.full(dom, 27+1.j) f4 = ift.full(dom, 1.j) assert_equal(f2, f3.real) assert_equal(f4, f3.imag)
def test_trivialities(): s1 = ift.RGSpace((10, )) f1 = ift.Field.full(s1, 27) assert_equal(f1.clip(min=29).local_data, 29.) assert_equal(f1.clip(max=25).local_data, 25.) assert_equal(f1.local_data, f1.real.local_data) assert_equal(f1.local_data, (+f1).local_data) f1 = ift.Field.full(s1, 27. + 3j) assert_equal(f1.one_over().local_data, (1. / f1).local_data) assert_equal(f1.real.local_data, 27.) assert_equal(f1.imag.local_data, 3.) assert_equal(f1.sum(), f1.sum(0)) assert_equal(f1.conjugate().local_data, ift.Field.full(s1, 27. - 3j).local_data) f1 = ift.from_global_data(s1, np.arange(10)) # assert_equal(f1.min(), 0) # assert_equal(f1.max(), 9) assert_equal(f1.prod(), 0)
correlated_field = ift.CorrelatedField(position_space, A) ### SETTING UP SPECIFIC SCENARIO #### R = ift.GeometryRemover(position_space) data_space = R.target signal_response = R(correlated_field) # Set up likelihood and load data N = ift.ScalingOperator(0.1, data_space) data, ground_truth = generate_mysterious_data(position_space) data = ift.from_global_data(data_space, data) likelihood = ift.GaussianEnergy(mean=data, inverse_covariance=N.inverse)(signal_response) #### SOLVING PROBLEM #### ic_sampling = ift.GradientNormController(iteration_limit=100) ic_newton = ift.GradInfNormController( name='Newton', tol=1e-6, iteration_limit=30) minimizer = ift.NewtonCG(ic_newton) H = ift.StandardHamiltonian(likelihood, ic_sampling) initial_mean = ift.MultiField.full(H.domain, 0.) mean = initial_mean
plot.add(conv_delta, title='Kernel') plot.output() # Healpix test nside = 64 npix = 12 * nside * nside domain = ift.HPSpace(nside) # Define test signal (some point sources) signal_vals = np.zeros(npix, dtype=np.float64) for i in range(0, npix, npix // 12 + 27): signal_vals[i] = 500. signal = ift.from_global_data(domain, signal_vals) delta_vals = np.zeros(npix, dtype=np.float64) delta_vals[0] = 1.0 delta = ift.from_global_data(domain, delta_vals) # Define kernel function def func(theta): ct = np.cos(theta) return 1. * np.logical_and(ct > 0.7, ct <= 0.8) convtest(signal, delta, func) domain = ift.RGSpace((100, 100))
import nifty5 as ift from helpers import generate_wf_data, plot_WF np.random.seed(42) # Want to implement: m = Dj = (S^{-1} + R^T N^{-1} R)^{-1} R^T N^{-1} d position_space = ift.RGSpace(256) prior_spectrum = lambda k: 1 / (10. + k**2.5) data, ground_truth = generate_wf_data(position_space, prior_spectrum) R = ift.GeometryRemover(position_space) data_space = R.target data = ift.from_global_data(data_space, data) ground_truth = ift.from_global_data(position_space, ground_truth) plot_WF('data', ground_truth, data) N = ift.ScalingOperator(0.1, data_space) harmonic_space = position_space.get_default_codomain() HT = ift.HartleyOperator(harmonic_space, target=position_space) S_h = ift.create_power_operator(harmonic_space, prior_spectrum) S = HT @ S_h @ HT.adjoint D_inv = S.inverse + R.adjoint @ N.inverse @ R j = (R.adjoint @ N.inverse)(data)
def plot_prior_samples_2d(n_samps, signal, R, correlated_field, A, likelihood, N=None): samples, pspecmin, pspecmax = [], np.inf, 0 pspec = A * A for _ in range(n_samps): ss = ift.from_random('normal', signal.domain) samples.append(ss) foo = pspec.force(ss).to_global_data() pspecmin = min([min(foo), pspecmin]) pspecmax = max([max(foo), pspecmin]) fig, ax = plt.subplots(nrows=n_samps, ncols=5, figsize=(2 * 5, 2 * n_samps)) for ii, sample in enumerate(samples): cf = correlated_field(sample) signal_response = R @ signal sg = signal(sample) sr = (R.adjoint @ R @ signal)(sample) if likelihood == 'gauss': data = signal_response(sample) + N.draw_sample() elif likelihood == 'poisson': rate = signal_response(sample).to_global_data() data = ift.from_global_data(signal_response.target, np.random.poisson(rate)) elif likelihood == 'bernoulli': rate = signal_response(sample).to_global_data() data = ift.from_global_data(signal_response.target, np.random.binomial(1, rate)) else: raise ValueError('likelihood type not implemented') data = R.adjoint(data + 0.) As = pspec.force(sample) ax[ii, 0].plot(As.domain[0].k_lengths, As.to_global_data()) ax[ii, 0].set_ylim(pspecmin, pspecmax) ax[ii, 0].set_yscale('log') ax[ii, 0].set_xscale('log') ax[ii, 0].get_xaxis().set_visible(False) ax[ii, 1].imshow(cf.to_global_data(), aspect='auto') ax[ii, 1].get_xaxis().set_visible(False) ax[ii, 1].get_yaxis().set_visible(False) ax[ii, 2].imshow(sg.to_global_data(), aspect='auto') ax[ii, 2].get_xaxis().set_visible(False) ax[ii, 2].get_yaxis().set_visible(False) ax[ii, 3].imshow(sr.to_global_data(), aspect='auto') ax[ii, 3].get_xaxis().set_visible(False) ax[ii, 3].get_yaxis().set_visible(False) ax[ii, 4].imshow(data.to_global_data(), cmap='viridis', aspect='auto') ax[ii, 4].get_xaxis().set_visible(False) ax[ii, 4].yaxis.tick_right() ax[ii, 4].get_yaxis().set_visible(False) if ii == 0: ax[0, 0].set_title('power spectrum') ax[0, 1].set_title('correlated field') ax[0, 2].set_title('signal') ax[0, 3].set_title('signal Response') ax[0, 4].set_title('synthetic data') ax[n_samps - 1, 0].get_xaxis().set_visible(True) plt.tight_layout() plt.savefig('prior_samples_{}.png'.format(likelihood)) plt.close('all')
def test_multifield_field_consistency(): f1 = ift.full(dom, 27) f2 = ift.from_global_data(dom['d1'], f1['d1'].to_global_data()) assert_equal(f1.sum(), f2.sum()) assert_equal(f1.size, f2.size)
def test_cast_domain(): s1 = ift.RGSpace((10, )) s2 = ift.RGSpace((10, ), distances=20.) d = np.arange(s1.shape[0]) d2 = ift.from_global_data(s1, d).cast_domain(s2).to_global_data() assert_equal(d, d2)
def test_dataconv(): s1 = ift.RGSpace((10, )) ld = np.arange(ift.dobj.local_shape(s1.shape)[0]) gd = np.arange(s1.shape[0]) assert_equal(ld, ift.from_local_data(s1, ld).local_data) assert_equal(gd, ift.from_global_data(s1, gd).to_global_data())
""" Make the ground_truth image from gal, and define the position space """ gal = galsim.Gaussian(flux = gal_flux, scale_radius = gal_r0) image_original = gal.drawImage(scale = pixel_scale) image_sheared_galsim = i plt.imshow(image_original.array) plt.show() exit() image_original_arr = np.asarray(image_original.array) position_space = ift.RGSpace(image_original_arr.shape) ground_truth = ift.from_global_data(position_space, image_original_arr) # It would be better to enforce the positivity of my prior spectrum # by using lognormal prior_spectrum = lambda k: 5/(10. + k**2.) """ Make the data, which is the sheared image, with known shear and the shear operator which would be my instrument response """ R = ShearOperator(position_space, g1, g2) data_space = R.target sheared_image = custom_shear(image_original_arr, g1=g1,g2=g2) """