def test_correlate_intra(self, rtol=1e-6, atol=0.0): # test autocorrelator intra = self.rings.correlate_intra(1.0, 1.0, normed=True) assert intra.shape == (self.rings.num_phi,) q_ind = self.rings.q_index(1.0) ref_corr = np.zeros(self.num_phi) for i in range(self.num_shots): x = self.rings.polar_intensities[i,q_ind,:].flatten().copy() ref_corr += brute_force_masked_correlation(x, np.ones(len(x), dtype=np.bool), normed=True) ref_corr /= float(self.num_shots) assert_allclose(intra / intra[0], ref_corr / ref_corr[0], rtol=rtol, atol=atol, err_msg='doesnt match reference implementation') assert_allclose(intra, ref_corr, rtol=rtol, atol=atol, err_msg='doesnt match reference implementation normalization') # test norming assert np.abs(intra[0] - 1.0) < rtol intra_unnorm = self.rings.correlate_intra(1.0, 1.0, normed=False) assert not np.abs(intra_unnorm[0] - 1.0) < rtol assert_allclose(intra, intra_unnorm / intra_unnorm[0], rtol=rtol, atol=atol, err_msg='normalization broken') # test cross correlator intra = self.rings.correlate_intra(1.0, 2.0, normed=True) assert not np.abs(intra_unnorm[0] - 1.0) < 1e-8 # x-corr normed when it shouldnt be # test the limit on intra = self.rings.correlate_intra(1.0, 1.0, num_shots=1) assert intra.shape == (self.rings.num_phi,) # num_shots flag broken
def test_corr_rows_w_mask(self): q1 = 1.0 # chosen arb. q_ind = self.rings.q_index(q1) x = self.rings.polar_intensities[0,q_ind,:].flatten().copy() x_mask = np.random.binomial(1, 0.9, size=len(x)).astype(np.bool) no_mask = np.ones_like(x_mask) corr = self.rings._correlate_rows(x, x, x_mask, x_mask) true_corr = self.rings._correlate_rows(x, x) ref_corr = brute_force_masked_correlation(x, x_mask, normed=False) normed_ref = ref_corr / ref_corr[0] normed_corr = corr / corr[0] # big tol, but w/a lot of masking there is a ton of noise assert_allclose(normed_ref, normed_corr, atol=1e-2, rtol=1e-2, err_msg='correlation incorrect') # make sure the normalization is OK assert np.sum( np.abs(corr - ref_corr) ) / (corr[1]*float(len(corr))) < 1e-2 # make sure masked and unmaksed are somewhat similar assert_allclose(true_corr / true_corr[0], normed_corr, atol=0.1, err_msg='masked correlation very different from unmasked version')
def test_corr_rows_w_mask(self): q1 = 1.0 # chosen arb. q_ind = self.rings.q_index(q1) x = self.rings.polar_intensities[0, q_ind, :].flatten().copy() x_mask = np.random.binomial(1, 0.9, size=len(x)).astype(np.bool) no_mask = np.ones_like(x_mask) corr = self.rings._correlate_rows(x, x, x_mask, x_mask) true_corr = self.rings._correlate_rows(x, x) ref_corr = brute_force_masked_correlation(x, x_mask, normed=False) normed_ref = ref_corr / ref_corr[0] normed_corr = corr / corr[0] # big tol, but w/a lot of masking there is a ton of noise assert_allclose(normed_ref, normed_corr, atol=1e-2, rtol=1e-2, err_msg='correlation incorrect') # make sure the normalization is OK assert np.sum( np.abs(corr - ref_corr)) / (corr[1] * float(len(corr))) < 1e-2 # make sure masked and unmaksed are somewhat similar assert_allclose( true_corr / true_corr[0], normed_corr, atol=0.1, err_msg='masked correlation very different from unmasked version')
def test_brute_correlation_wo_mask(self): # this might be better somewhere else, but is here for now: # this tests the brute force implementation in testing.py used below x = np.random.randn(100) + 0.1 * np.arange(100) mask = np.ones(100, dtype=np.bool) c = brute_force_masked_correlation(x, mask, normed=False) c_norm = brute_force_masked_correlation(x, mask, normed=True) ref = np.zeros(100) for delta in range(100): ref[delta] = np.sum( (x - x.mean()) * \ (np.roll(x, delta) - x.mean()) ) \ / float(len(x)) assert_allclose(ref / ref[0], c_norm, err_msg='normalized fail') assert_allclose(ref, c, err_msg='unnormalized fail')
def test_brute_correlation_wo_mask(self): # this might be better somewhere else, but is here for now: # this tests the brute force implementation in testing.py used below x = np.random.randn(100) + 0.1 * np.arange(100) mask = np.ones(100, dtype=np.bool) c = brute_force_masked_correlation(x, mask) ref = np.zeros(100) for i in range(100): ref[i] = np.correlate(x, np.roll(x, i)) ref /= float(len(x)) * np.mean(x)**2 ref -= 1.0 assert_allclose(ref, c)
def test_brute_correlation_wo_mask(self): # this might be better somewhere else, but is here for now: # this tests the brute force implementation in testing.py used below x = np.random.randn(100) + 0.1 * np.arange(100) mask = np.ones(100, dtype=np.bool) c = brute_force_masked_correlation(x, mask) ref = np.zeros(100) for i in range(100): ref[i] = np.correlate(x, np.roll(x, i)) ref /= float(len(x)) * np.mean(x) ** 2 ref -= 1.0 assert_allclose(ref, c)
def test_corr_rows_w_mask(self): q1 = 1.0 # chosen arb. q_ind = self.rings.q_index(q1) x = self.rings.polar_intensities[0,q_ind,:].flatten().copy() x_mask = np.random.binomial(1, 0.9, size=len(x)).astype(np.bool) no_mask = np.ones_like(x_mask) corr = self.rings._correlate_rows(x, x, x_mask, x_mask, mean_only=True) true_corr = self.rings._correlate_rows(x, x, no_mask, no_mask, mean_only=True) ref_corr = brute_force_masked_correlation(x, x_mask) # big tol, but w/a lot of masking there is a ton of noise assert_allclose(true_corr, corr, atol=0.1) assert_allclose(ref_corr, corr, rtol=1e-03)
def test_correlate_intra(self, rtol=1e-6, atol=0.0): # test autocorrelator intra = self.rings.correlate_intra(1.0, 1.0, normed=True) assert intra.shape == (self.rings.num_phi, ) q_ind = self.rings.q_index(1.0) ref_corr = np.zeros(self.num_phi) for i in range(self.num_shots): x = self.rings.polar_intensities[i, q_ind, :].flatten().copy() ref_corr += brute_force_masked_correlation(x, np.ones(len(x), dtype=np.bool), normed=True) ref_corr /= float(self.num_shots) assert_allclose(intra / intra[0], ref_corr / ref_corr[0], rtol=rtol, atol=atol, err_msg='doesnt match reference implementation') assert_allclose( intra, ref_corr, rtol=rtol, atol=atol, err_msg='doesnt match reference implementation normalization') # test norming assert np.abs(intra[0] - 1.0) < rtol intra_unnorm = self.rings.correlate_intra(1.0, 1.0, normed=False) assert not np.abs(intra_unnorm[0] - 1.0) < rtol assert_allclose(intra, intra_unnorm / intra_unnorm[0], rtol=rtol, atol=atol, err_msg='normalization broken') # test cross correlator intra = self.rings.correlate_intra(1.0, 2.0, normed=True) assert not np.abs(intra_unnorm[0] - 1.0) < 1e-8 # x-corr normed when it shouldnt be # test the limit on intra = self.rings.correlate_intra(1.0, 1.0, num_shots=1) assert intra.shape == (self.rings.num_phi, ) # num_shots flag broken
def test_corr_rows_w_mask(self): q1 = 1.0 # chosen arb. q_ind = self.rings.q_index(q1) x = self.rings.polar_intensities[0, q_ind, :].flatten().copy() x_mask = np.random.binomial(1, 0.9, size=len(x)).astype(np.bool) no_mask = np.ones_like(x_mask) corr = self.rings._correlate_rows(x, x, x_mask, x_mask, mean_only=True) true_corr = self.rings._correlate_rows(x, x, no_mask, no_mask, mean_only=True) ref_corr = brute_force_masked_correlation(x, x_mask) # big tol, but w/a lot of masking there is a ton of noise assert_allclose(true_corr, corr, atol=0.1) assert_allclose(ref_corr, corr, rtol=1e-03)