Beispiel #1
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
Beispiel #2
0
 def test_bad_import(self):
     """ Test importing a module that does not exist """
     with pytest.raises(ImportError, match="is not installed"):
         misutil.optional_import("areallylongbadmodulename")
Beispiel #3
0
 def test_good_import(self):
     """ Test importing a module that should work. """
     mod = misutil.optional_import("mopy")
     assert mod is mopy