def test_step_shorter_than_period(self): step = 10 * u.ms fh = Fold(self.sh, self.n_phase, self.phase, step, samples_per_frame=1, average=False) fh.seek(0) fr = fh.read(3) # Note: API for accessing counts may change. fr_count = fr['count'] fr_data = fr['data'] # With 10 ms and a sample rate of 10 kHz, we should always get # 100 samples assert np.all(fr_count.sum(1) == 100) # period = 12.5 ms; hence of first sample only the first 40 bins # should have gotten anything. (Rounding may spill it in one # further bin.) assert np.all((fr_count[0, :40] == 3) | (fr_count[0, :40] == 2)) assert np.all(fr_count[0, 41:] == 0) # further samples, shifted by 10 each: assert np.all(fr_count[1, :30] != 0) assert np.all(fr_count[1, 40:] != 0) assert np.all(fr_count[1, 31:39] == 0) assert np.all(fr_count[2, :20] != 0) assert np.all(fr_count[2, 30:] != 0) assert np.all(fr_count[2, 21:29] == 0) # Phases near 0 should have the pulse and the rest background. assert np.all(fr_data[:, (0, 1, -1)].sum(1) > 10) assert np.all(fr_data[:, 2:49] <= 0.125 * 3)
def test_times_wrong(self): with pytest.raises(ValueError): Fold(self.sh, 8, self.phase, start=self.start_time - 1. * u.s) with pytest.raises(ValueError): Fold(self.sh, 8, self.phase, start=self.start_time + 3. * u.s) with pytest.raises(AssertionError): Fold(self.sh, 8, self.phase, step=1. * u.hr)
def test_folding_with_averaging(self): # Test averaging fh = Fold(self.sh, self.n_phase, self.phase, step=26 * u.ms, samples_per_frame=20, average=True) fh.seek(0) fr = fh.read(10) assert np.all(fr[:, 2:-1] == 0.125), \ "Average off-gate power is incorrect."
def test_read_whole_file(self): ref_data = self.raw_data[:, 0] phase = self.phase(self.start_time + np.arange(self.shape[0]) / self.sample_rate) i_phase = ((phase.to_value(u.cycle) * self.n_phase) % self.n_phase).astype(int) expected = np.bincount(i_phase, ref_data) / np.bincount(i_phase) fh = Fold(self.sh, self.n_phase, self.phase, average=True) assert abs(fh.stop_time - self.sh.stop_time) < 1. * u.ns fr = fh.read(1) assert np.all(fr[:, 2:-1] == 0.125), \ "Average off-gate power is incorrect." assert np.all(fr[0, :, 0] == expected)
def test_step_longer_than_period(self): step = 30 * u.ms fh = Fold(self.sh, self.n_phase, self.phase, step, samples_per_frame=1, average=False) fh.seek(0) fr = fh.read(10) fr_count = fr['count'] fr_data = fr['data'] # Compare the total counts of all the samples. assert np.all(fr_count.sum(1) == 300) # Test the output on and off gates. 3 gates -> 7.5 bins. pulse_power = (fr_data[:, (0, 1, -1)].sum(1) / fr_count[:, (0, 1, -1)].sum(1)) assert np.all(np.abs(pulse_power - 10. / 7.5 - 0.125) < 0.5), \ "On-gate power is incorrect." assert np.allclose(fr_data[:, 2:-1] / fr_count[:, 2:-1], 0.125), \ "Off-gate power is incorrect." # Try with offset time fh2 = Fold(self.sh, self.n_phase, self.phase, step, start=self.sh.start_time + step, samples_per_frame=1, average=False) fr2 = fh2.read(9) assert np.all(fr2 == fr[1:])
def test_read_part(self): ref_data = self.raw_data[10000:, 0] start = self.start_time + 10000 / self.sample_rate phase = self.phase(start + (np.arange(self.shape[0] - 10000) / self.sample_rate)) i_phase = ((phase.to_value(u.cycle) * self.n_phase) % self.n_phase).astype(int) expected = np.bincount(i_phase, ref_data) / np.bincount(i_phase) fh = Fold(self.sh, self.n_phase, self.phase, average=False, start=start) assert abs(fh.start_time - start) < 1. * u.ns assert abs(fh.stop_time - self.sh.stop_time) < 1. * u.ns fr = fh.read(1) assert np.all(fr['count'].sum((0, 1)) == 6000) average = fr['data'][0] / fr['count'][0] assert np.all(average[2:-1] == 0.125), \ "Average off-gate power is incorrect." assert np.all(average[:, 0] == expected), \ "On-gate power is incorrect."
def test_non_integer_sample_rate_ratio(self): step = (1. / 3.) * u.s fh = Fold(self.sh, self.n_phase, self.phase, step, average=True) fr = fh.read() assert np.all(fr[:, 2:-1] == 0.125), \ "Average off-gate power is incorrect." fh1 = Fold(self.sh, self.n_phase, self.phase, step, start=self.start_time + step, average=True) fr1 = fh1.read() assert np.all(fr1 == fr[1:]), \ "Fold applied start offset incorrectly." fh2 = Fold(self.sh, self.n_phase, self.phase, step, start=self.start_time + 2 * step, average=True) fr2 = fh2.read() assert np.all(fr2 == fr[2:]), \ "Fold applied start offset incorrectly."