Exemple #1
0
 def test_fill_zeros(self):
     """ Tests for filling array with zeros. """
     ar = np.random.rand(10, 10)
     in_dtype = ar.dtype
     out = misutil.pad_or_trim(ar, sample_count=15)
     assert np.shape(out)[-1] == 15
     assert out.dtype == in_dtype, "datatype should not change"
     assert np.all(out[:, 10:] == 0)
Exemple #2
0
 def test_trim(self):
     """
     Ensure an array gets trimmed when sample count is smaller than last
     dimension.
     """
     ar = np.random.rand(10, 10)
     out = misutil.pad_or_trim(ar, 1)
     assert np.shape(out)[-1] == 1
Exemple #3
0
    def mtspec(
        self,
        time_bandwidth=2,
        sample_count: Optional[int] = None,
        to_dft: bool = False,
        **kwargs,
    ) -> "mopy.SpectrumGroup":
        """
        Return a SpectrumGroup by calculating amplitude spectra via mtspec.

        Parameters
        ----------
        time_bandwidth
            The time bandwidth used in mtspec, see its docstring for more
            details.
        sample_count
            The number of samples in the time series to use in the
            transformation. If greater than the length of the data columns
            it will be zero padded, if less the data will be cropped.
            Defaults to the next fast length.
        to_dft
            If True, convert the power spectral density calculated by mtspec to
            the discrete fourier transform

        Notes
        -----
        The parameter time_bandwidth and the kwargs are passed directly to
        mtspec.mtspec, see its documentation for details:
        https://krischer.github.io/mtspec/multitaper_mtspec.html
        """
        mtspec = optional_import("mtspec")
        # get prepared input array
        if sample_count is None:
            sample_count = next_fast_len(len(self.data.columns))
        ar = pad_or_trim(self.data.values, sample_count=sample_count)
        # collect kwargs for mtspec
        delta = 1.0 / self.sampling_rate
        kwargs = dict(kwargs)
        kwargs.update({"delta": delta, "time_bandwidth": time_bandwidth})
        # unfortunately we may need to use loops here
        out = [mtspec.mtspec(x, **kwargs) for x in ar]
        array = np.array([x[0] for x in out])
        freqs = out[0][1]
        df = pd.DataFrame(array, index=self.data.index, columns=freqs)
        # normalize to number of non-zero samples
        norm = len(self.data.columns) / self.stats["npts"]
        df = df.multiply(norm, axis=0)
        df.columns.name = "frequency"
        # # collect and return
        sg_kwargs = dict(data=df,
                         stats_group=self.stats_group,
                         spectra_type="psd")
        sg = mopy.SpectrumGroup(**sg_kwargs)
        if to_dft:
            sg = sg.to_spectra_type("dft")
        return sg
Exemple #4
0
 def test_fill_nonzero(self):
     """ Tests for filling non-zero values """
     ar = np.random.rand(10, 10)
     out = misutil.pad_or_trim(ar, sample_count=15, pad_value=np.NaN)
     assert np.all(np.isnan(out[:, 10:]))