示例#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
示例#2
0
    def dft(self, sample_count: Optional[int] = None) -> "mopy.SpectrumGroup":
        """
        Apply a discrete Fourier transform to the data to create a SpectrumGroup

        Parameters
        ----------
        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.
        """
        data = self.data
        if sample_count is None:
            sample_count = next_fast_len(len(self.data.columns))
        spec = np.fft.rfft(data.values, n=sample_count, axis=-1)
        freqs = np.fft.rfftfreq(len(data.columns), 1.0 / self.sampling_rate)
        df = pd.DataFrame(spec, index=data.index, columns=freqs)
        df.columns.name = "frequency"
        # create SpectrumGroup
        kwargs = dict(data=df,
                      stats_group=self.stats_group,
                      spectra_type="dft")
        return mopy.SpectrumGroup(**kwargs)
示例#3
0
def source_group_node_session(node_trace_group):
    """ Return a source group with node data. """
    sg = mopy.SpectrumGroup(node_trace_group)
    assert not hasattr(sg.stats, "process") or not sg.stats.processing
    assert not (sg.data == 0).all().all()
    return sg