def test_correlate_difference(self, rtol=1e-4, atol=0.0): # compute diff corr for 2 shots q = 1.0 r = xray.Rings.simulate(self.traj, 1, self.q_values, self.num_phi, 2) assert r.num_shots == 2, 'this test will only work w/a 2 shot Rings' iq = r.q_index(q) pi = r.polar_intensities.copy() d = r.correlate_difference(q, q, normed=True) nd = d / d[0] d2 = r.correlate_difference(q, q, normed=False) # regression test for in-place modification pi2 = r.polar_intensities.copy() assert np.all(pi == pi2), 'inappropriate in-place modification of intensities' # compute the reference minus = pi[0,iq,:] - pi[1,iq,:] normed_ref = brute_force_masked_correlation(minus, np.ones(len(minus), dtype=np.bool), normed=True) ref = brute_force_masked_correlation(minus, np.ones(len(minus), dtype=np.bool), normed=False) # make sure the results are close assert_allclose(nd, normed_ref, rtol=rtol, atol=atol, err_msg='shapes of norm difference correlator dont match') assert_allclose(d, normed_ref, rtol=rtol, atol=atol, err_msg='normalization constant doesnt match') assert_allclose(d2, ref, rtol=rtol, atol=atol, err_msg='non-normalized results mismatch') # regression test for nans when intensity_normed=True d2 = r.correlate_difference(q, q, normed=True, intensity_normed=True) assert not np.any( np.isnan(d2) ), 'nans found when intensity_normed=True'
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_correlate_intra(self, rtol=0.1, atol=0.1): # 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, 'first normalized entry not 1, is: %f' % intra[0] 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, mean_only=False) assert intra.shape == (1, self.rings.num_phi)
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')