def test_mean_mode_equivalent(self): """Test 2 equivalent ways to deweight mean, see if they agree.""" self.DM.nf = 1 time_stream, ra, dec, az, el, time, mask_inds = \ self.DM.get_all_trimmed() nt = len(time) # Frist Way. Noise1 = dirty_map.Noise(time_stream, time) thermal_noise_levels = sp.zeros((1, )) + 0.04 # Kelvin**2 Noise1.add_thermal(thermal_noise_levels) Noise1.add_mask(mask_inds) Noise1.deweight_time_mean() Noise1.deweight_time_slope() Noise1.add_correlated_over_f(0.01, -1.2, 0.1) Noise1.finalize() N1 = Noise1.get_inverse() # Second Way. Noise2 = dirty_map.Noise(time_stream, time) thermal_noise_levels = sp.zeros((1, )) + 0.04 # Kelvin**2 Noise2.add_thermal(thermal_noise_levels) Noise2.add_mask(mask_inds) Noise2.deweight_time_slope() Noise2.add_correlated_over_f(0.01, -1.2, 0.1) Noise2.freq_mode_noise += dirty_map.T_huge**2 Noise2.finalize() N2 = Noise2.get_inverse() N2_m = N2.view() N2_m.shape = (nt, nt) self.assertTrue(sp.allclose(N2, N1))
def deactivated_test_extreme_index(self): """Set of parameters know to have cased issues in the past with numerical stability.""" nf = 40 nt = 150 n = nf * nt dt = 0.26214 BW = 1. / dt / 2. time_stream = sp.zeros((nf, nt)) time_stream = al.make_vect(time_stream, axis_names=("freq", "time")) time = dt * (sp.arange(nt) + 50) N = dirty_map.Noise(time_stream, time) # Thermal. thermal = sp.zeros(nf, dtype=float) + 0.0002 * BW * 2. thermal[22] = dirty_map.T_infinity**2 N.add_thermal(thermal) # Time mean and slope. N.deweight_time_mean() N.deweight_time_slope() # Extreem index over_f bit. mode = -sp.ones(nf, dtype=float) / sp.sqrt(nf - 1) mode[22] = 0 # Parameters measured from one of the data sets. Known to screw things # up. #N.add_over_f_freq_mode(8.128e-7, -4.586, 1.0, 1.422e-7, mode, True) N.add_over_f_freq_mode(0.001729, -0.777, 1.0, 1e-8, mode, True) #N.orthogonalize_modes() N.finalize() # Check if the fast inverse works. N_mat = N.get_mat() N_mat.shape = (n, n) N_inv = N.get_inverse() N_inv.shape = (n, n)
def test_numerical_stability(self): nf = 15 nt = 100 dt = 0.3 BW = 1. / dt / 2. time_stream = sp.zeros((nf, nt)) time_stream = al.make_vect(time_stream, axis_names=("freq", "time")) time = dt * (sp.arange(nt) + 50) N = dirty_map.Noise(time_stream, time) # Thermal. thermal = sp.zeros(nf, dtype=float) + 0.0003 * BW * 2. N.add_thermal(thermal) # K**2 # Mask. mask_f = [] mask_t = [] # Mask out a whole time. mask_f += range(nf) mask_t += [74] * nf # Mask out a channel. mask_f += [13] * nt mask_t += range(nt) # Mask out a few more points. mask_f += [7, 14, 9, 7] mask_t += [87, 42, 54, 95] N.add_mask((sp.array(mask_f, dtype=int), sp.array(mask_t, dtype=int))) # Time mean and slope. N.deweight_time_mean() N.deweight_time_slope() # Correlated modes. f_0 = 1. amps = [0.05, 0.01, 0.001] index = [-2.5, -1.7, -1.2] for ii in range(3): mode = sp.arange(nf, dtype=float) mode += 5 * nf * ii mode *= ii * 2. * sp.pi / nf mode = sp.cos(mode) mode /= sp.sqrt(sp.sum(mode**2)) N.add_over_f_freq_mode(amps[ii], index[ii], f_0, 0.0003 * BW * 2., mode, True) # All freq modes over_f. N.add_all_chan_low(thermal, -0.9, 0.01) N.finalize() N_mat = N.get_inverse() N_mat.shape = (nf * nt, ) * 2 e, v = linalg.eigh(N_mat) self.assertTrue(sp.amin(e) > 0)
def deactivate_test_profile(self): """Not an actual test, this is for profiling.""" nf = 32 nra = 32 ndec = 32 DM = DataMaker(nscans=10, nt_scan=200, nf=nf, nra=nra, ndec=ndec, scan_size=5.0, map_size=7.0, add_noise=False, add_ground=False) map = DM.get_map() time_stream, ra, dec, az, el, time, mask_inds = DM.get_all_trimmed() P = dirty_map.Pointing(("ra", "dec"), (ra, dec), map, 'linear') Noise = dirty_map.Noise(time_stream, time) thermal_noise_levels = sp.zeros((nf)) + 0.04 # Kelvin**2 Noise.add_thermal(thermal_noise_levels) Noise.add_mask(mask_inds) self.assertTrue(sp.alltrue(Noise.diagonal[mask_inds] > 10)) Noise.deweight_time_mean() Noise.deweight_time_slope() Noise.add_correlated_over_f(0.01, -1.2, 0.1) start = time_module.clock() Noise.finalize() stop = time_module.clock() print "Finalizing noise took %5.2f seconds." % (stop - start) # Do the profiling. map_noise_inv = sp.zeros((nf, nra, ndec, nf, nra, ndec), dtype=float) print "Frequency ind:", start = time_module.clock() for ii in xrange(1): print ii, for jj in xrange(nra): P.noise_to_map_domain(Noise, ii, jj, map_noise_inv[ii, jj, :, :, :, :]) stop = time_module.clock() print print "Constructing map noise took %5.2f seconds." % (stop - start)
def test_uncoupled_channels(self): time_stream, ra, dec, az, el, time, mask_inds = \ self.DM.get_all_trimmed() nt = len(time) map = self.map # Calculate two noise matrices. Niether have noise couplig terms, but # the second is calculated in the most general way. # The first one. P = dirty_map.Pointing(("ra", "dec"), (ra, dec), map, 'nearest') Noise1 = dirty_map.Noise(time_stream, time) thermal_noise_level = 0.4 # Kelvin**2 Noise1.add_thermal(thermal_noise_level) Noise1.add_mask(mask_inds) Noise1.deweight_time_mean() Noise1.deweight_time_slope() Noise1.finalize(frequency_correlations=False) # The second one. Noise2 = dirty_map.Noise(time_stream, time) Noise2.add_thermal(thermal_noise_level) Noise2.add_mask(mask_inds) Noise2.deweight_time_mean() Noise2.deweight_time_slope() Noise2.finalize(frequency_correlations=True) # Check that they agree. N_inv = Noise1.get_inverse() self.assertEqual(N_inv.shape, (nf_d, nt, nt)) N_inv2 = Noise2.get_inverse() for ii in range(nf_d): self.assertTrue(sp.allclose(N_inv[ii, ...], N_inv2[ii, :, ii, :])) # Check that Noise1 is right. # Calculate the noise on a slice and make sure it multiplies to give # the identity. N = sp.zeros((nf_d, nt * nt)) N[:, ::nt + 1] = Noise1.diagonal N.shape = (nf_d, nt, nt) for ii in range(Noise1.time_mode_noise.shape[0]): N += (Noise1.time_mode_noise[ii, ...].flat[::nf_d + 1][:, None, None] * Noise1.time_modes[ii, None, :, None] * Noise1.time_modes[ii, None, None, :]) eye = sp.eye(nt) for ii in range(nf_d): tmp_eye = sp.dot(N[ii, ...], N_inv[ii, ...]) self.assertTrue(sp.allclose(tmp_eye, eye)) # Now test that weighting the time stream is the same for both # algorithms. t1 = Noise1.weight_time_stream(time_stream) t2 = Noise2.weight_time_stream(time_stream) self.assertTrue(sp.allclose(t1, t2)) # Now transform to the map domain and make sure they still match. # Noise1. map_noise_inv1 = sp.zeros((nf_d, nra_d, ndec_d, nra_d, ndec_d), dtype=float) map_noise_inv1 = al.make_mat(map_noise_inv1, axis_names=('freq', 'ra', 'dec', 'ra', 'dec'), row_axes=(0, 1, 2), col_axes=(0, 3, 4)) for ii in xrange(nf_d): P.noise_channel_to_map(Noise1, ii, map_noise_inv1[ii, ...]) # Noise2. map_noise_inv2 = sp.zeros((nf_d, nra_d, ndec_d, nf_d, nra_d, ndec_d), dtype=float) map_noise_inv2 = al.make_mat(map_noise_inv2, axis_names=('freq', 'ra', 'dec', 'freq', 'ra', 'dec'), row_axes=(0, 1, 2), col_axes=(3, 4, 5)) for ii in xrange(nf_d): for jj in xrange(nra_d): P.noise_to_map_domain(Noise2, ii, jj, map_noise_inv2[ii, jj, :, :, :, :]) # Check them. for ii in xrange(nf_d): self.assertTrue( sp.allclose(map_noise_inv1[ii, ...], map_noise_inv2[ii, :, :, ii, :, :]))
def test_build_noise(self): map = self.map time_stream, ra, dec, az, el, time, mask_inds = \ self.DM.get_all_trimmed() nt = len(time) Noise = dirty_map.Noise(time_stream, time) thermal_noise_levels = sp.zeros((nf_d)) + 0.04 # Kelvin**2 Noise.add_thermal(thermal_noise_levels) Noise.add_mask(mask_inds) self.assertTrue(sp.alltrue(Noise.diagonal[mask_inds] > 10)) Noise.deweight_time_mean() Noise.deweight_time_slope() Noise.add_correlated_over_f(0.01, -1.2, 0.1) Noise.finalize() #### Test the full inverse. # Frist get a full representation of the noise matrix #tmp_mat = sp.zeros((nf_d, nt, nf_d, nt)) #tmp_mat.flat[::nt*nf_d + 1] += Noise.diagonal.flat #for jj in xrange(Noise.time_modes.shape[0]): # tmp_mat += (Noise.time_mode_noise[jj,:,None,:,None] # * Noise.time_modes[jj,None,:,None,None] # * Noise.time_modes[jj,None,None,None,:]) #for jj in xrange(Noise.freq_modes.shape[0]): # tmp_mat += (Noise.freq_mode_noise[jj,None,:,None,:] # * Noise.freq_modes[jj,:,None,None,None] # * Noise.freq_modes[jj,None,None,:,None]) tmp_mat = Noise.get_mat() tmp_mat.shape = (nt * nf_d, nt * nf_d) # Check that the matrix I built for testing is indeed symetric. self.assertTrue(sp.allclose(tmp_mat, tmp_mat.transpose())) noise_inv = Noise.get_inverse() noise_inv.shape = (nt * nf_d, nt * nf_d) # Check that the production matrix is symetric. self.assertTrue(sp.allclose(noise_inv, noise_inv.transpose())) tmp_eye = sp.dot(tmp_mat, noise_inv) #print tmp_eye noise_inv.shape = (nf_d, nt, nf_d, nt) self.assertTrue(sp.allclose(tmp_eye, sp.identity(nt * nf_d))) # Check that the calculation of the diagonal is correct. noise_inv_diag = Noise.get_inverse_diagonal() self.assertTrue( sp.allclose(noise_inv_diag.flat, noise_inv.flat[::nf_d * nt + 1])) #### Test the noise weighting of the data. noise_weighted_data = Noise.weight_time_stream(time_stream) self.assertTrue( sp.allclose(noise_weighted_data, al.dot(noise_inv, time_stream))) #### Test making noise in map space. # First make the noise matrix by brute force. P = dirty_map.Pointing(("ra", "dec"), (ra, dec), map, 'nearest') P_mat = P.get_matrix() tmp_map_noise_inv = al.partial_dot(noise_inv, P_mat) tmp_map_noise_inv = al.partial_dot(P_mat.mat_transpose(), tmp_map_noise_inv) # I mess up the meta data by doing this, but rotate the axes so they # are in the desired order. tmp_map_noise_inv = sp.rollaxis(tmp_map_noise_inv, 2, 0) # Now use fast methods. map_noise_inv = sp.zeros((nf_d, nra_d, ndec_d, nf_d, nra_d, ndec_d), dtype=float) map_noise_inv = al.make_mat(map_noise_inv, axis_names=('freq', 'ra', 'dec', 'freq', 'ra', 'dec'), row_axes=(0, 1, 2), col_axes=(3, 4, 5)) start = time_module.clock() for ii in xrange(nf_d): for jj in xrange(nra_d): P.noise_to_map_domain(Noise, ii, jj, map_noise_inv[ii, jj, :, :, :, :]) stop = time_module.clock() #print "Constructing map noise took %5.2f seconds." % (stop - start) self.assertTrue(sp.allclose(map_noise_inv, tmp_map_noise_inv))