예제 #1
0
def compute_log_power(data,
                      frequencies=np.logspace(np.log10(3), np.log10(180), 8),
                      epsilon=1.0e-4,
                      buffer=2.0):
    wf = MorletWaveletFilterCpp(time_series=data.T,
                                freqs=frequencies,
                                output='power',
                                cpus=25)
    pow_wavelet, phase_wavelet = wf.filter()
    pow_wavelet = pow_wavelet.remove_buffer(duration=buffer)
    pow_wavelet = np.log10(pow_wavelet + epsilon)

    pow_wavelet_avg = pow_wavelet.mean(axis=2)
    return pow_wavelet_avg
    def test_wavelets_python_cpp(self):
        from ptsa.data.filters import MorletWaveletFilterCpp
        print('hello')

        wf = MorletWaveletFilter(
            time_series=self.base_eegs,
            freqs=np.logspace(np.log10(3), np.log10(180), 8),
            output='power',
        )

        pow_wavelet, phase_wavelet = wf.filter()

        wf_cpp = MorletWaveletFilterCpp(time_series=self.base_eegs,
                                        freqs=np.logspace(
                                            np.log10(3), np.log10(180), 8),
                                        output='power',
                                        cpus=4)

        pow_wavelet_cpp, phase_wavelet_cpp = wf_cpp.filter()

        decimal = 2
        freq_num = 0

        from scipy.stats import describe

        desc_cpp = describe(pow_wavelet_cpp[freq_num, :, :, 500:-500])
        desc_py = describe(pow_wavelet[freq_num, :, :, 500:-500])

        try:
            assert_array_almost_equal(
                (pow_wavelet_cpp[freq_num, :, :, 500:-500] -
                 pow_wavelet[freq_num, :, :, 500:-500]) /
                pow_wavelet_cpp[freq_num, :, :, 500:-500],
                np.zeros_like(pow_wavelet_cpp[freq_num, :, :, 500:-500]),
                decimal=decimal)
        except AssertionError:
            print(
                'WARNING: Cpp and Python wavelets are not within 1%. Will try weaker test '
            )

            mean_min = np.min((desc_cpp.mean - desc_py.mean) / desc_cpp.mean)
            mean_max = np.max((desc_cpp.mean - desc_py.mean) / desc_cpp.mean)
            print('mean_max=', mean_max)
            print('mean_min=', mean_min)

            self.assertTrue(np.abs(mean_max) < 0.05)
            self.assertTrue(np.abs(mean_min) < 0.05)
예제 #3
0
    def test_wavelets_python_cpp(self):
        from ptsa.data.filters import MorletWaveletFilterCpp
        print('hello')

        wf = MorletWaveletFilter(time_series=self.base_eegs,
                                 freqs=np.logspace(np.log10(3), np.log10(180), 8),
                                 output='power',
                                 )

        pow_wavelet, phase_wavelet = wf.filter()

        wf_cpp = MorletWaveletFilterCpp(time_series=self.base_eegs,
                                 freqs=np.logspace(np.log10(3), np.log10(180), 8),
                                 output='power',
                                 cpus=4
                                 )

        pow_wavelet_cpp, phase_wavelet_cpp = wf_cpp.filter()

        decimal = 2
        freq_num=0

        from scipy.stats import describe

        desc_cpp = describe(pow_wavelet_cpp[freq_num,:,:,500:-500])
        desc_py = describe(pow_wavelet[freq_num,:,:,500:-500])

        try:
            assert_array_almost_equal(
                (pow_wavelet_cpp[freq_num,:,:,500:-500]-pow_wavelet[freq_num,:,:,500:-500])/pow_wavelet_cpp[freq_num,:,:,500:-500],
                np.zeros_like(pow_wavelet_cpp[freq_num,:,:,500:-500]), decimal=decimal)
        except AssertionError:
            print('WARNING: Cpp and Python wavelets are not within 1%. Will try weaker test ')

            mean_min = np.min((desc_cpp.mean-desc_py.mean)/desc_cpp.mean)
            mean_max = np.max((desc_cpp.mean-desc_py.mean)/desc_cpp.mean)
            print('mean_max=',mean_max)
            print('mean_min=',mean_min)


            self.assertTrue(np.abs(mean_max)<0.05)
            self.assertTrue(np.abs(mean_min)<0.05)
예제 #4
0
    def test_wavelets_multicore(self):
        from ptsa.data.filters import MorletWaveletFilterCpp
        print 'hello'
        print sys.path

        wf_cpp_multi = MorletWaveletFilterCpp(time_series=self.base_eegs,
                                 freqs=np.logspace(np.log10(3), np.log10(180), 8),
                                 output='both',
                                 cpus=8
                                 )

        pow_wavelet_multi, phase_wavelet_multi = wf_cpp_multi.filter()

        wf_cpp = MorletWaveletFilterCpp(time_series=self.base_eegs,
                                 freqs=np.logspace(np.log10(3), np.log10(180), 8),
                                 output='both',
                                 cpus=1
                                 )

        pow_wavelet, phase_wavelet= wf_cpp.filter()

        assert_array_equal(pow_wavelet_multi,pow_wavelet)
        assert_array_equal(phase_wavelet_multi,phase_wavelet)
    def test_wavelets_multicore(self):
        from ptsa.data.filters import MorletWaveletFilterCpp
        print('hello')
        print(sys.path)

        wf_cpp_multi = MorletWaveletFilterCpp(time_series=self.base_eegs,
                                              freqs=np.logspace(
                                                  np.log10(3), np.log10(180),
                                                  8),
                                              output='both',
                                              cpus=8)

        pow_wavelet_multi, phase_wavelet_multi = wf_cpp_multi.filter()

        wf_cpp = MorletWaveletFilterCpp(time_series=self.base_eegs,
                                        freqs=np.logspace(
                                            np.log10(3), np.log10(180), 8),
                                        output='both',
                                        cpus=1)

        pow_wavelet, phase_wavelet = wf_cpp.filter()

        assert_array_equal(pow_wavelet_multi, pow_wavelet)
        assert_array_equal(phase_wavelet_multi, phase_wavelet)
    use_reref_eeg=True,
    common_root='data').read()
words = events[events.type == 'WORD']

# select all available channels
eegfile = words[0].eegfile
eegfile_reref = str.split(str(eegfile), '/')
day = eegfile_reref[-1]
eegfile_reref = '/'.join(eegfile_reref[0:-1])
channels = glob.glob(eegfile_reref + '/' + day + "*.[0-9]*")
channels = np.array([str.split(x, '.')[-1] for x in channels])
#channels = np.array(['{:03}'.format(x) for x in range(0,132)])

eeg = EEGReader(events=words, channels=channels, start_time=0.3,
                end_time=1.6).read()

b_filter = ButterworthFilter(time_series=eeg, order=4, freq_range=[58, 62])
eeg_filtered = b_filter.filter()
eeg_buffered = eeg_filtered.add_mirror_buffer(1.3)
eeg_buffered.data = np.ascontiguousarray(eeg_buffered.data)

import time
pt = time.time()
freqs = np.logspace(np.log10(3), np.log10(180), 50)
pow_ev, _ = MorletWaveletFilterCpp(time_series=eeg_buffered,
                                   freqs=freqs[0:1],
                                   output='power',
                                   cpus=20,
                                   verbose=False).filter()
run_time = time.time() - pt