Example #1
0
def calculate_lhipa(d):
    # find max decomposition level
    w = pywt.Wavelet("sym16")
    maxlevel = pywt.dwt_max_level(len(d), filter_len=w.dec_len)
    # set high and low frequency band indeces
    hif, lof = 1, int(maxlevel / 2)
    # get detail coefficients of pupil diameter signal d
    cD_H = pywt.downcoef("d", d, "sym16", "per", level=hif)
    cD_L = pywt.downcoef("d", d, "sym16", "per", level=lof)
    # normalize by 1/ 2j
    cD_H[:] = [x / math.sqrt(2**hif) for x in cD_H]
    cD_L[:] = [x / math.sqrt(2**lof) for x in cD_L]
    # obtain the LH:HF ratio
    cD_LH = cD_L
    for i in range(len(cD_L)):
        cD_LH[i] = cD_L[i] / cD_H[int((2**lof) / (2**hif)) * i]
    # detect modulus maxima , see Duchowski et al. [15]
    cD_LHm = modmax(cD_LH)
    # threshold using universal threshold λuniv = σˆ (2logn)
    # where σˆ is the standard deviation of the noise
    lambda_univ = np.std(cD_LHm) * math.sqrt(2.0 * np.log2(len(cD_LHm)))
    cD_LHt = pywt.threshold(cD_LHm, lambda_univ, mode="less")
    # get signal duration (in seconds)
    sampling_rate = 250
    tt = len(d) // sampling_rate
    # compute LHIPA
    ctr = 0
    LHIPA = np.nan  # should read the paper
    for i in range(len(cD_LHt)):
        if math.fabs(cD_LHt[i]) > 0:
            ctr += 1
            LHIPA = float(ctr) / tt
    return LHIPA
 def transform(self, x, y=None):
     res = []
     if type(self.level) == dict:
         for alevel in self.level['a']:
             res.append(
                 np.concatenate([
                     downcoef('a',
                              x,
                              wavelet=self.wavelet,
                              mode=self.mode,
                              level=alevel).reshape((1, -1)) for x in x
                 ],
                                axis=0))
         for blevel in self.level['d']:
             res.append(
                 np.concatenate([
                     downcoef('d',
                              x,
                              wavelet=self.wavelet,
                              mode=self.mode,
                              level=blevel).reshape((1, -1)) for x in x
                 ],
                                axis=0))
     else:
         res = wavedec(x,
                       wavelet=self.wavelet,
                       mode=self.mode,
                       level=self.level,
                       axis=-1)
     return np.concatenate(res, axis=-1)
Example #3
0
def test_upcoef_reconstruct():
    data = np.arange(3)
    a = pywt.downcoef("a", data, "haar")
    d = pywt.downcoef("d", data, "haar")

    rec = pywt.upcoef("a", a, "haar", take=3) + pywt.upcoef("d", d, "haar", take=3)
    assert_allclose(rec, data)
def test_downcoef_complex():
    rstate = np.random.RandomState(1234)
    r = rstate.randn(16) + 1j * rstate.randn(16)
    nlevels = 3
    a = pywt.downcoef('a', r, 'haar', level=nlevels)
    a_ref = pywt.downcoef('a', r.real, 'haar', level=nlevels)
    a_ref = a_ref + 1j * pywt.downcoef('a', r.imag, 'haar', level=nlevels)
    assert_allclose(a, a_ref)
Example #5
0
def test_downcoef_complex():
    rstate = np.random.RandomState(1234)
    r = rstate.randn(16) + 1j * rstate.randn(16)
    nlevels = 3
    a = pywt.downcoef('a', r, 'haar', level=nlevels)
    a_ref = pywt.downcoef('a', r.real, 'haar', level=nlevels)
    a_ref = a_ref + 1j * pywt.downcoef('a', r.imag, 'haar', level=nlevels)
    assert_allclose(a, a_ref)
Example #6
0
def test_upcoef_reconstruct():
    data = np.arange(3)
    a = pywt.downcoef('a', data, 'haar')
    d = pywt.downcoef('d', data, 'haar')

    rec = (pywt.upcoef('a', a, 'haar', take=3) +
           pywt.upcoef('d', d, 'haar', take=3))
    assert_allclose(rec, data)
def test_upcoef_reconstruct():
    data = np.arange(3)
    a = pywt.downcoef('a', data, 'haar')
    d = pywt.downcoef('d', data, 'haar')

    rec = (pywt.upcoef('a', a, 'haar', take=3) +
           pywt.upcoef('d', d, 'haar', take=3))
    assert_allclose(rec, data)
Example #8
0
def test_downcoef_multilevel():
    r = np.random.randn(16)
    nlevels = 3
    # calling with level=1 nlevels times
    a1 = r.copy()
    for i in range(nlevels):
        a1 = pywt.downcoef('a', a1, 'haar', level=1)
    # call with level=nlevels once
    a3 = pywt.downcoef('a', r, 'haar', level=3)
    assert_allclose(a1, a3)
Example #9
0
def wavelet_decomposition(y, wavelet_mother="db8", level_wt=3):
    w = pywt.Wavelet(wavelet_mother)
    coeffs = {}
    wd = {}
    coeffs['cA' + str(level_wt)] = pywt.downcoef('a', y, w, mode='sym', level=level_wt)
    wd['cA' + str(level_wt)] = pywt.upcoef('a', coeffs['cA' + str(level_wt)], w, level=level_wt, take=len(y))
    for i in range(level_wt, 0, -1):
        coeffs['cD' + str(i)] = pywt.downcoef('d', y, w, mode='sym', level=i)
        wd['cD' + str(i)] = pywt.upcoef('d', coeffs['cD' + str(i)], w, level=i, take=len(y)) 
    return (w ,wd, coeffs)
Example #10
0
def test_downcoef_multilevel():
    r = np.random.randn(16)
    nlevels = 3
    # calling with level=1 nlevels times
    a1 = r.copy()
    for i in range(nlevels):
        a1 = pywt.downcoef('a', a1, 'haar', level=1)
    # call with level=nlevels once
    a3 = pywt.downcoef('a', r, 'haar', level=3)
    assert_allclose(a1, a3)
Example #11
0
def test_downcoef_multilevel():
    rstate = np.random.RandomState(1234)
    r = rstate.randn(16)
    nlevels = 3
    # calling with level=1 nlevels times
    a1 = r.copy()
    for i in range(nlevels):
        a1 = pywt.downcoef("a", a1, "haar", level=1)
    # call with level=nlevels once
    a3 = pywt.downcoef("a", r, "haar", level=nlevels)
    assert_allclose(a1, a3)
Example #12
0
def test_compare_downcoef_coeffs():
    rstate = np.random.RandomState(1234)
    r = rstate.randn(16)
    # compare downcoef against wavedec outputs
    for nlevels in [1, 2, 3]:
        for wavelet in pywt.wavelist():
            a = pywt.downcoef('a', r, wavelet, level=nlevels)
            d = pywt.downcoef('d', r, wavelet, level=nlevels)
            coeffs = pywt.wavedec(r, wavelet, level=nlevels)
            assert_allclose(a, coeffs[0])
            assert_allclose(d, coeffs[1])
Example #13
0
        def discrete_wavelet(self):
            n_series = self.ts_matrix.shape[0]
            for i in range(n_series):
                for j in range(n_series):
                    x = self.ts_matrix[i, :].tolist()
                    y = self.ts_matrix[j, :].tolist()
                    if i != j:
                        Xcoeffs = pywt.downcoef('a', x, 'sym8', level=4)
                        Ycoeffs = pywt.downcoef('a', y, 'sym8', level=4)

                        dist = np.linalg.norm(Xcoeffs - Ycoeffs)
                        self.dm[i, j] = dist
Example #14
0
def decimation_arrays(raw_emg, mode='normal'):
    """
	Returns the output of the dwt at each level of decimation in an array
    Ex: Output = [cA4 cD4 cA3 cD3 ... ]
    """
    output = []
    for dec_level in range(LEVEL, 0, -1):
        for type in ['a', 'd']:
            if mode == 'normal':
                output.extend(downcoef(type, raw_emg, WAVELET, level=dec_level))
            elif mode == 'thresh':
                output.extend(thresh(downcoef(type, raw_emg, WAVELET, level=dec_level)))
    return output
Example #15
0
def decimation_arrays(raw_emg, mode='normal'):
    """
	@returns
    	output[List[List[Int]]] := [cA4 cD4 cA3 cD3 cA2 ... ]
    """
    output = []
    for dec_level in range(LEVEL, -1, 0):
        for type in ['a', 'd']:
            if mode == 'normal':
                output.append(downcoef(type, raw_emg, WAVELET, level=dec_level))
            elif mode == 'thresh':
                output.append(thresh(downcoef(type, raw_emg, WAVELET, level=dec_level)))
    return output
Example #16
0
def test_compare_downcoef_coeffs():
    rstate = np.random.RandomState(1234)
    r = rstate.randn(16)
    # compare downcoef against wavedec outputs
    for nlevels in [1, 2, 3]:
        for wavelet in pywt.wavelist():
            wavelet = pywt.Wavelet(wavelet)
            max_level = pywt.dwt_max_level(r.size, wavelet.dec_len)
            if nlevels <= max_level:
                a = pywt.downcoef('a', r, wavelet, level=nlevels)
                d = pywt.downcoef('d', r, wavelet, level=nlevels)
                coeffs = pywt.wavedec(r, wavelet, level=nlevels)
                assert_allclose(a, coeffs[0])
                assert_allclose(d, coeffs[1])
Example #17
0
def extract_features(input_sample):
    out = np.array([])
    # sym8
    cA = pywt.downcoef('a', input_sample, 'sym8', level=4, mode='per')
    out = np.append(out, cA)
    cD = pywt.downcoef('d', input_sample, 'sym8', level=4, mode='per')
    out = np.append(out, cD)
    # db6/9
    cA = pywt.downcoef('a', input_sample, 'db6', level=4, mode='per')
    out = np.append(out, cA)
    cD = pywt.downcoef('d', input_sample, 'db6', level=4, mode='per')
    out = np.append(out, cD)
    cA = pywt.downcoef('a', input_sample, 'db9', level=4, mode='per')
    out = np.append(out, cA)
    cD = pywt.downcoef('d', input_sample, 'db9', level=4, mode='per')
    out = np.append(out, cD)
    # dmey
    cA = pywt.downcoef('a', input_sample, 'dmey', level=4, mode='per')
    out = np.append(out, cA)
    cD = pywt.downcoef('d', input_sample, 'dmey', level=4, mode='per')
    out = np.append(out, cD)

    # differences
    differences = np.zeros(16)
    for i, t in enumerate(range(40, 56)):
        differences[i] = input_sample[t + 1] - input_sample[t]
    out = np.append(out, differences)
    return out
Example #18
0
def test_compare_downcoef_coeffs():
    rstate = np.random.RandomState(1234)
    r = rstate.randn(16)
    # compare downcoef against wavedec outputs
    for nlevels in [1, 2, 3]:
        for wavelet in pywt.wavelist():
            wavelet = pywt.DiscreteContinuousWavelet(wavelet)
            if isinstance(wavelet, pywt.Wavelet):
                max_level = pywt.dwt_max_level(r.size, wavelet.dec_len)
                if nlevels <= max_level:
                    a = pywt.downcoef("a", r, wavelet, level=nlevels)
                    d = pywt.downcoef("d", r, wavelet, level=nlevels)
                    coeffs = pywt.wavedec(r, wavelet, level=nlevels)
                    assert_allclose(a, coeffs[0])
                    assert_allclose(d, coeffs[1])
Example #19
0
    def _calculate_pt_locations(self, wave, ds, cc, window):
        # The wavelet to use for PT extraction
        wav = pywt.Wavelet('haar')
        # Use wavelet decomposition until level 4 to retrieve energy of the P and T wave
        coeff = pywt.downcoef('d', window.flatten(), wavelet=wav, level=4)**2
        # Use std as threshold value
        std = 1.5 * np.std(coeff)
        max_idx = -1

        # Search for the index where the energy is maxed
        for idx, val in enumerate(coeff):
            if val >= std and (max_idx == -1 or coeff[idx] > coeff[max_idx]):
                max_idx = idx

        # Correct index within level 4 detail coefficient due to subsampling
        c_idx = max_idx * 2**self._dwt_level

        if wave == 'p':
            # Calculate P location relative to the detection window
            r_idx = ds.q[cc] + c_idx - len(window) - self._noise_comp
        else:
            # Calculate T location relative to the detection window
            r_idx = ds.s[cc] + c_idx + self._noise_comp

        # Prevent out of bounds
        if r_idx < 0: r_idx = 0
        if r_idx >= len(ds.data): r_idx = len(ds.data) - 1

        return r_idx
Example #20
0
def test_compare_downcoef_coeffs():
    rstate = np.random.RandomState(1234)
    r = rstate.randn(16)
    # compare downcoef against wavedec outputs
    for nlevels in [1, 2, 3]:
        for wavelet in pywt.wavelist():
            if wavelet in ['cmor', 'shan', 'fbsp']:
                # skip these CWT families to avoid warnings
                continue
            wavelet = pywt.DiscreteContinuousWavelet(wavelet)
            if isinstance(wavelet, pywt.Wavelet):
                max_level = pywt.dwt_max_level(r.size, wavelet.dec_len)
                if nlevels <= max_level:
                    a = pywt.downcoef('a', r, wavelet, level=nlevels)
                    d = pywt.downcoef('d', r, wavelet, level=nlevels)
                    coeffs = pywt.wavedec(r, wavelet, level=nlevels)
                    assert_allclose(a, coeffs[0])
                    assert_allclose(d, coeffs[1])
Example #21
0
def test_compare_downcoef_coeffs():
    rstate = np.random.RandomState(1234)
    r = rstate.randn(16)
    # compare downcoef against wavedec outputs
    for nlevels in [1, 2, 3]:
        for wavelet in pywt.wavelist():
            if wavelet in ['cmor', 'shan', 'fbsp']:
                # skip these CWT families to avoid warnings
                continue
            wavelet = pywt.DiscreteContinuousWavelet(wavelet)
            if isinstance(wavelet, pywt.Wavelet):
                max_level = pywt.dwt_max_level(r.size, wavelet.dec_len)
                if nlevels <= max_level:
                    a = pywt.downcoef('a', r, wavelet, level=nlevels)
                    d = pywt.downcoef('d', r, wavelet, level=nlevels)
                    coeffs = pywt.wavedec(r, wavelet, level=nlevels)
                    assert_allclose(a, coeffs[0])
                    assert_allclose(d, coeffs[1])
Example #22
0
def wavelet(image):
    taille = (64, 64)
    if np.shape(image) != taille:
        image = cv2.resize(image, taille)
    imLab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
    #print(np.shape(imLab))
    coeffs = pywt.downcoef('d',
                           np.reshape(imLab[:, :, 0], taille[0] * taille[1]),
                           'Haar')
    return (np.atleast_2d(coeffs).T)
Example #23
0
def DWT(x: np.ndarray):
    """ Discrete Wavelet Transform

    :param x: a numeric sequence
    :return: np.ndarray
    """

    xt = pywt.downcoef(part="a", data=x, wavelet="db1", level=1)

    return xt
def get_delta_ap_en(ts):
    d2_coefficients = pywt.downcoef('d', ts, 'db4', level=2)
    surrogate_coefficients = generate_surrogate_series(d2_coefficients)
    app_entropy_sample = entropy.app_entropy(d2_coefficients,
                                             order=2,
                                             metric='chebyshev')
    app_entropy_surrogate = entropy.app_entropy(surrogate_coefficients,
                                                order=2,
                                                metric='chebyshev')

    # Return the delta
    delta_ap_en = app_entropy_surrogate - app_entropy_sample
    return delta_ap_en.item()
def smooth_for_trend(y_values: np.ndarray,
                     smooth_params: configparser.ConfigParser) -> np.ndarray:
    """
    Method that smooth a time series using wavelet transforms. In the configuration file ([file])
    the type of wavelet, the number of transforms and the desired length of the results is given
    in order to perform the operation.

    Remember that if the length of the result is smaller than the desired length, the
    computation of the transformations will be halted. Moreover, if the first iteration
    already gives a shorter time series an exception will be raised.

    An example of configuration is:
    ```\n
    [file]\n
    ...\n
    wavelet=db8 #wavelet type\n
    levels=5 #transformation levels\n
    data_points=300 #desired length\n
    ```

    :param y_values: y values of the original time series
    :param smooth_params: parameters relative to the smoothing
    :return: smoothed time series that will be used as trend
    """
    wavelet = smooth_params[WAVELET]
    levels = pywt.dwt_max_level(y_values.shape[0], wavelet)
    data_points = int(smooth_params[DATA_PTS])

    # Decompose getting only the details
    for _ in range(levels):
        y_values = downcoef(part='a', data=y_values, wavelet=wavelet)

    for _ in range(levels):
        details = np.zeros(y_values.shape)
        y_values = pywt.idwt(y_values, details, wavelet=wavelet)

    if y_values.shape[0] > data_points:
        return y_values[:data_points]
    else:
        raise ValueError(
            'The original time series is to short to perform this operation')
 def wv_signal_comprs(self, original_signal_df, signals_names):
     """
     Make a compression of the input signals with a discrete wavelet
     transform.
     """
     # TODO It is still necessary to see how to use it in order to compress
     signal_wv_list = []
     # and decompress data effectively.
     for signal_name in signals_names:
         print("Compression of signal {} with wavelet {}".format(
             signal_name, self.wv_sel))
         signal_wv_list.append(
             pywt.downcoef("a",
                           original_signal_df[signal_name],
                           self.wv_sel,
                           level=self.wv_order).tolist())
     # Create and return the output dataframe with the compressed signals
     output_df = pd.DataFrame()
     for i, signal in enumerate(signals_names):
         output_df[signal] = signal_wv_list[i]
     return output_df
print(rec)
print()

# Multilevel decomposition using
from pywt import wavedec
cA2, cD2, cD1 = wavedec([1, 2, 3, 4, 3, 2, 1], "db1", level=2)
print(cA2)
print(cD2)
print(cD1)
print()

# Partial Discrete Wavelet Transform data decomposition
# Similar to pywt.dwt, but computes only one set of coefficients.
# Useful when you need only approximation or only details at the given level.
coeffs = pywt.downcoef(part="a",
                       data=[1, 2, 3, 4, 3, 2, 1],
                       wavelet="db1",
                       level=2)
print(coeffs)
print()

# Maximum decomposition level
w = pywt.Wavelet("sym5")
l = pywt.dwt_max_level(data_len=1000, filter_len=w.dec_len)
print(l)
l = pywt.dwt_max_level(data_len=1000, filter_len=w)
print(l)
print()

# Result coefficients length
l = pywt.dwt_coeff_len(data_len=1000, filter_len=w, mode="per")
print(l)
Example #28
0
    "ifft":
    (np.fft.ifft, "numpy.fft.ifft", "an inverse Discrete Fourier Transform"),
    "rfft":
    (np.fft.rfft, "numpy.fft.rfft", "a real-input Discrete Fourier Transform"),
    "irfft": (np.fft.irfft, "numpy.fft.irfft",
              "a real-input inverse Discrete Fourier Transform"),
    "dwt": (pywt.dwt, "pywt.dwt", "a single level Discrete Wavelet Transform"),
    "idwt": (lambda x, *args, **kwargs: pywt.idwt(*x, *args, **kwargs),
             "pywt.idwt", "a single level inverse Discrete Wavelet Transform"),
    "wavedec": (pywt.wavedec, "pywt.wavedec",
                "a multilevel 1D Discrete Wavelet Transform"),
    "waverec":
    (lambda x, *args, **kwargs: pywt.waverec(list(x), *args, **kwargs),
     "pywt.waverec", "a multilevel 1D Inverse Discrete Wavelet Transform"),
    "pdwt":
    (lambda x, part, *args, **kwargs: pywt.downcoef(part, x, *args, **kwargs),
     "pywt.downcoef",
     "a partial Discrete Wavelet Transform data decomposition"),
    "cwt": (lambda x, *args, **kwargs: pywt.cwt(x, *args, **kwargs)[0].T,
            "pywt.cwt", "a Continuous Wavelet Transform"),
}

TEMPLATE_DOCSTRING = """
    Compute {description} for each trace.
    This method simply wraps ``apply_along_axis`` method by setting the
    ``func`` argument to ``{full_name}``.

    Parameters
    ----------
    src : str, optional
        Batch component to get the data from.
Example #29
0
def get_spectr_approx(spectra, w='db3', l=3):
    return pywt.downcoef('a', spectra, w, level=l)
Example #30
0
Irec = simpleImageRec(C)
plt.imshow(Irec)
plt.show()

###############################
# python module pywt
# Values are the same as computed previously, just use a ratio 1/sqrt(2)
import pywt
cA, cD = pywt.dwt(s, 'haar')
print(cA, cD)

###############################
t = np.linspace(-1, 1, 300, endpoint=True)
f1 = 3
f2 = 50
period = t[2] - t[1]
Fs = 1 / period
sig = np.sin(2 * np.pi * f1 * t) + 2 * np.sin(2 * np.pi * f2 * t)
scales = np.arange(1, 300)
coef, freq = pywt.cwt(sig, scales, 'morl', period)
#freq = pywt.scale2frequency('morl', scales);
fig = plt.figure()
plt.imshow(np.abs(coef), aspect='auto')
plt.show()
fig.savefig('cwt.python.pdf', bbox_inches='tight')

################################
a5 = pywt.downcoef('a', sig, 'db4', level=5)
d4 = pywt.downcoef('d', sig, 'db4', level=4)
Example #31
0
 def downcoef_n(self, data):
     aC = pywt.downcoef('a', data, self.st, level=self.levels)
     dC = pywt.downcoef('d', data, self.st, level=self.levels)
     return dC, aC
Example #32
0
def preprocess(x_raw):
    gs = x_raw['gs'].to_numpy().astype(float).flatten()
    gs = downcoef('a', gs, 'db4', level=7)
    load = float(x_raw['load'][0])/300  # normalize the load value

    return np.append(gs, load)
Example #33
0
def plot_trial(df_path,
               subject_info,
               run,
               trial,
               bandpass=False,
               method=None,
               dwt=False,
               sub_band=None,
               psd=False,
               save=True,
               name_extension=None):
    """
    Creates ERP plots for each electrode for a given run and trial

    Parameters
    ----------
    df_path : str
        Path to experiment dataframe

    subject_info : str
        Subject identification number

    run : int
        Run identification number

    trial : int
        Trial identification number
    """

    #load experiment df
    df = pd.read_csv(df_path)

    #extract eeg samples for this specific trial
    index = int(df.loc[df['Run'] == run][df['Trial'] == trial]
                ['Trial EEG Samples'].index[0])
    samples = literal_eval(df.loc[index, 'Trial EEG Samples'])

    #hex color codes for each electrode
    colors = [
        '#F0A3FF', '#0075DC', '#993F00', '#4C005C', '#191919', '#005C31',
        '#2BCE48', '#FFCC99', '#808080', '#94FFB5', '#8F7C00', '#9DCC00',
        '#C20088', '#003380', '#FFA405', '#FFA8BB', '#426600', '#FF0010',
        '#5EF1F2', '#00998F', '#E0FF66', '#740AFF', '#990000', '#FFFF80',
        '#FFFF00', '#FF5005'
    ]

    #create list of dictionaries containing x (timestamp), y(voltage), color, and width values for each electrode
    all_signals = []
    for each_electrode in range(20):
        signal = f'Electrode{each_electrode}'
        all_signals.append({
            'name': signal,
            'x': [],
            'y': [],
            'color': colors[each_electrode],
            'linewidth': 1
        })

    # #iterate through all eeg samples
    for sample in samples:
        #separate out timestamps and voltage values from eeg samples
        all_readings = sample[0][:20]
        timestamp = sample[1]

        #for each electrode update the all_signals dictionary
        for each_electrode in range(len(all_readings)):
            all_signals[each_electrode]['y'].append(
                all_readings[each_electrode])
            all_signals[each_electrode]['x'].append(timestamp)

    #create subplots
    for signal in range(len(all_signals)):

        fig, ax = plt.subplots()

        #normalize timestamps to start from zero
        first_timestamp = all_signals[signal]['x'][0]
        for each_timestamp in range(len(all_signals[signal]['x'])):
            all_signals[signal]['x'][each_timestamp] = all_signals[signal][
                'x'][each_timestamp] - first_timestamp

        #apply filters
        if bandpass:
            x, y = all_signals[signal]['x'], bandpass_filter(
                all_signals[signal]['y'],
                fs=500,
                low_freq=7,
                high_freq=30,
                method=method)

        elif sub_band:
            x, y = all_signals[signal]['x'], extract_band(
                all_signals[signal]['y'], fs=500, band=sub_band)
        elif dwt:
            # coeffs = wavedec(all_signals[signal]['y'], dwt)[-1]
            coeffs = downcoef('d', all_signals[signal]['y'], 'coif1', level=6)
            x, y = np.linspace(0,
                               all_signals[signal]['x'][-1],
                               num=len(coeffs)), coeffs
        else:
            x, y = all_signals[signal]['x'], all_signals[signal]['y']

        #plot each electrode
        if psd:

            plt.psd(y, Fs=500)

            #format plot
            ax.set_axisbelow(True)
            ax.minorticks_on()
            ax.grid(which='minor',
                    linestyle=':',
                    linewidth='0.5',
                    color='black')
            ax.set_title(
                f"PSD for Run: {run} Trial: {trial} Electrode: {signal}")
            plt.ylabel('Power Spectral Density (dB/Hz)')
            plt.xlabel('Frequency')
            # plt.xlim(0, 150)
            # plt.ylim(-15,15)
        else:
            ax.plot(x,
                    y,
                    color=all_signals[signal]['color'],
                    linewidth=all_signals[signal]['linewidth'],
                    label=all_signals[signal]['name'])

            #format plot
            ax.set_title(
                f"ERP for Run: {run} Trial: {trial} Electrode: {signal}")
            plt.ylabel('Voltage (mV)')
            plt.xlabel('Time (S)')

        #save plot
        if save:
            if name_extension:
                plt.savefig(
                    f'data/{subject_info}/ERP_plots/run{run}_trial{trial}_electrode{signal}_{name_extension}.png'
                )
            else:
                plt.savefig(
                    f'data/{subject_info}/ERP_plots/run{run}_trial{trial}_electrode{signal}.png'
                )
        else:
            plt.show()
Example #34
0
def autoRemoveBlink(icas):
    waveletLevel = 0
    if len(icas) > 0:
        # getting the level of decomposition for the wavelet transform
        # muscular artifacts appear in the Theta 4-7Hz and Mu 8-12 bands
        frq = icas[0].frequency
        while frq >= 4:
            frq = frq / 2
            waveletLevel += 1
    for ica in icas:
        frequency = ica.frequency
        duration = ica.duration
        newC = []
        for c in ica.components:
            # we need to get up to Ca4 to get to theta band
            Ca = pywt.downcoef('a',
                               c,
                               'Haar',
                               mode='symmetric',
                               level=waveletLevel)
            # padding to make map to time domain
            new = []
            pad = int((len(c) - len(Ca)) / len(Ca))
            for i in range(len(Ca)):
                new.append(Ca[i])
                for j in range(pad):
                    new.append(Ca[i])
            Ca = np.array(new)
            # getting all negative peaks index in Ca4
            negative = Ca[Ca <= 0]
            maxsNP = []
            for n in negative:
                # checking window of 400ms with eye blink
                centerMs = sampleToMS(n, frequency, duration)
                s = msToReading(centerMs - 200, frequency, duration)
                e = msToReading(centerMs + 200, frequency, duration)
                nSamp = frequency * duration
                if s < 0:
                    s = 0
                if e >= nSamp:
                    e = nSamp - 1
                maxN = 0
                maxI = s
                while s < e:
                    if Ca[s] < maxN:
                        maxN = Ca[s]
                        maxI = s
                    s += 1
                # obtaining the maximum negative peak
                maxsNP.append([maxI, maxN])
            # computing the mean of the maximum negative peaks
            mean = np.sum(maxsNP[:][1]) / len(maxsNP)
            # turning all maximum negative peaks that are above the mean to zero
            for peak in maxsNP:
                if peak[1] < mean:
                    Ca[peak[0]] = 0.0
            # returning Ca to original shape
            ca = []
            for i in range(len(Ca)):
                if i % (pad + 1) == 0:
                    ca.append(Ca[i])
            # applying reverse wavelet
            component = pywt.upcoef('a', ca, 'Haar', level=waveletLevel)
            newC.append(component)
        ica.components = newC