def test_method_works_on_complex_numbers(self): re = np.arange(self.xmin, self.xmax, self.dx) im = np.arange(self.xmin, self.xmax, self.dx) y = np.zeros(re.shape[0], dtype=np.complex64) yerr = np.zeros(re.shape[0], dtype=complex) for k, (r, i) in enumerate(zip(re, im)): y[k] = r + i * 1j yerr[k] = r + i * 1j real_binned = np.zeros(self.true_values.shape[0], dtype=complex) for i in range(self.true_values.shape[0]): real_binned[i] = self.true_values[i] + self.true_values[i] * 1j _, _, binyerr_real, _ = \ utils.rebin_data_log(self.x, y.real, self.f, y_err=yerr.real, dx=self.dx) _, biny, binyerr, _ = \ utils.rebin_data_log(self.x, y, self.f, y_err=yerr, dx=self.dx) assert np.iscomplexobj(biny) assert np.iscomplexobj(binyerr) assert np.allclose(biny, real_binned) assert np.allclose(binyerr, binyerr_real + 1.j * binyerr_real)
def rebin_log(self, f=0.01): """ Logarithmic rebin of the periodogram. The new frequency depends on the previous frequency modified by a factor f: .. math:: d\\nu_j = d\\nu_{j-1} (1+f) Parameters ---------- f: float, optional, default ``0.01`` parameter that steers the frequency resolution Returns ------- new_spec : :class:`Crossspectrum` (or one of its subclasses) object The newly binned cross spectrum or power spectrum. Note: this object will be of the same type as the object that called this method. For example, if this method is called from :class:`AveragedPowerspectrum`, it will return an object of class """ binfreq, binpower, binpower_err, nsamples = \ rebin_data_log(self.freq, self.power, f, y_err=self.power_err, dx=self.df) # the frequency resolution df = np.diff(binfreq) # shift the lower bin edges to the middle of the bin and drop the # last right bin edge binfreq = binfreq[:-1] + df / 2 new_spec = copy.copy(self) new_spec.freq = binfreq new_spec.power = binpower new_spec.power_err = binpower_err new_spec.m = nsamples * self.m if hasattr(self, 'unnorm_power'): _, binpower_unnorm, _, _ = \ rebin_data_log(self.freq, self.unnorm_power, f, dx=self.df) new_spec.unnorm_power = binpower_unnorm if hasattr(self, 'pds1'): new_spec.pds1 = self.pds1.rebin_log(f) if hasattr(self, 'pds2'): new_spec.pds2 = self.pds2.rebin_log(f) if hasattr(self, 'cs_all'): cs_all = [] for c in self.cs_all: cs_all.append(c.rebin_log(f)) new_spec.cs_all = cs_all return new_spec
def rebin_log(self, f=0.01): """ Logarithmic rebin of the periodogram. The new frequency depends on the previous frequency modified by a factor f: dnu_j = dnu_{j-1}*(1+f) Parameters ---------- f: float, optional, default 0.01 parameter that steers the frequency resolution Returns ------- new_spec : Crossspectrum (or one of its subclasses) object The newly binned cross spectrum or power spectrum. Note: this object will be of the same type as the object that called this method. For example, if this method is called from `AveragedPowerspectrum`, it will return an object of class """ binfreq, binpower, binpower_err, nsamples = \ rebin_data_log(self.freq, self.power, f, y_err=self.power_err, dx=self.df) # the frequency resolution df = np.diff(binfreq) # shift the lower bin edges to the middle of the bin and drop the # last right bin edge binfreq = binfreq[:-1] + df / 2 new_spec = copy.copy(self) new_spec.freq = binfreq new_spec.power = binpower new_spec.power_err = binpower_err new_spec.m = nsamples * self.m if hasattr(self, 'unnorm_power'): _, binpower_unnorm, _, _ = \ rebin_data_log(self.freq, self.unnorm_power, f, dx=self.df) new_spec.unnorm_power = binpower_unnorm if hasattr(self, 'pds1'): new_spec.pds1 = self.pds1.rebin_log(f) if hasattr(self, 'pds2'): new_spec.pds2 = self.pds2.rebin_log(f) if hasattr(self, 'cs_all'): cs_all = [] for c in self.cs_all: cs_all.append(c.rebin_log(f)) new_spec.cs_all = cs_all return new_spec
def test_method_fails_if_y_and_yerr_of_unequal_length(self): with pytest.raises(ValueError): _, _, _, _ = utils.rebin_data_log(self.x, self.y, self.f, y_err=self.y_err[1:], dx=self.dx)
def test_all_outputs_have_the_same_dimension_except_binx(self): binx, biny, binyerr, nsamples = utils.rebin_data_log(self.x, self.y, self.f, y_err=self.y_err, dx=self.dx) # binx describes the bin _edges_ rather than midpoints, so has one more entry # than biny and the rest assert binx.shape[0] == biny.shape[0]+1 assert biny.shape[0] == binyerr.shape[0] assert binyerr.shape[0] == nsamples.shape[0]
def test_bin_values_are_correct(self): _, biny, _, _ = utils.rebin_data_log(self.x, self.y, self.f, y_err=self.y_err, dx=self.dx) assert np.allclose(biny, self.true_values)
def test_return_float_with_floats(self): _, biny, binyerr, _ = utils.rebin_data_log(self.x, self.y, self.f, y_err=self.y_err, dx=self.dx) assert not np.iscomplexobj(biny) assert not np.iscomplexobj(binyerr)
def test_nsamples_are_correctly_calculated(self): _, _, _, nsamples = utils.rebin_data_log(self.x, self.y, self.f, y_err=self.y_err, dx=self.dx) assert np.allclose(nsamples, self.true_nsamples)
def test_binning_works_correctly(self): binx, _, _, _ = utils.rebin_data_log(self.x, self.y, self.f, y_err=self.y_err, dx=self.dx) assert np.allclose(np.diff(binx), self.true_bins)
def test_all_outputs_have_the_same_dimension_except_binx(self): binx, biny, binyerr, nsamples = utils.rebin_data_log(self.x, self.y, self.f, y_err=self.y_err, dx=self.dx) # binx describes the bin _edges_ rather than midpoints, so has one # more entry than biny and the rest assert binx.shape[0] == biny.shape[0] + 1 assert biny.shape[0] == binyerr.shape[0] assert binyerr.shape[0] == nsamples.shape[0]
def test_method_works_on_complex_numbers(self): re = np.arange(self.xmin, self.xmax, self.dx) im = np.arange(self.xmin, self.xmax, self.dx) y = np.zeros(re.shape[0], dtype=np.complex) yerr = np.zeros(re.shape[0], dtype=np.complex) for k, (r, i) in enumerate(zip(re, im)): y[k] = r + i * 1j yerr[k] = r + i * 1j real_binned = np.zeros(self.true_values.shape[0], dtype=np.complex) for i in range(self.true_values.shape[0]): real_binned[i] = self.true_values[i] + self.true_values[i] * 1j _, biny, _, _ = utils.rebin_data_log(self.x, y, self.f, y_err=yerr, dx=self.dx) assert np.allclose(biny, real_binned)
def test_rebin_data_log_runs(self): _, _, _, _ = utils.rebin_data_log(self.x, self.y, self.f, y_err=self.y_err, dx=self.dx)
def rebin_log(self, f=0.01): """ Logarithmic rebin of the periodogram. The new frequency depends on the previous frequency modified by a factor f: dnu_j = dnu_{j-1}*(1+f) Parameters ---------- f: float, optional, default 0.01 parameter that steers the frequency resolution Returns ------- binfreq: numpy.ndarray the binned frequencies binpower: numpy.ndarray the binned powers binpower_err: numpy.ndarray the uncertainties in binpower nsamples: numpy.ndarray the samples of the original periodogram included in each frequency bin """ binfreq, binpower, binpower_err, nsamples = \ rebin_data_log(self.freq, self.power, f, y_err=self.power_err, dx=self.df) # the frequency resolution df = np.diff(binfreq) # shift the lower bin edges to the middle of the bin and drop the # last right bin edge binfreq = binfreq[:-1] + df / 2 new_spec = copy.copy(self) new_spec.freq = binfreq new_spec.power = binpower new_spec.power_err = binpower_err new_spec.m = nsamples * self.m if hasattr(self, 'unnorm_power'): _, binpower_unnorm, _, _ = \ rebin_data_log(self.freq, self.unnorm_power, f, dx=self.df) new_spec.unnorm_power = binpower_unnorm if hasattr(self, 'pds1'): new_spec.pds1 = self.pds1.rebin_log(f) if hasattr(self, 'pds2'): new_spec.pds2 = self.pds2.rebin_log(f) if hasattr(self, 'cs_all'): cs_all = [] for c in self.cs_all: cs_all.append(c.rebin_log(f)) new_spec.cs_all = cs_all return new_spec
def test_method_fails_if_x_and_y_of_unequal_length(self): with pytest.raises(ValueError): _, _, _, _ = utils.rebin_data_log(self.x[1:], self.y, self.f, y_err=self.y_err, dx=self.dx)