aolabNeuro / analyze

Code for specific analysis computations on processed data (e.g. spectral coherence)
MIT License
7 stars 0 forks source link

butterworth filter function only works with bandpass #208

Open leoscholl opened 2 years ago

leoscholl commented 2 years ago

https://github.com/aolabNeuro/analyze/blob/0b6947d3dae7c619305450cc170a7f59829faf98/aopy/precondition.py#L64

the or 'bandstop' makes this line always True

m-nolan commented 2 years ago

@leoscholl @pavi-rajes Why does the or 'bandstop' break this? That block won't run unless filter_type is either bandpass or bandstop. The former is filter_type's default value, but if neither are true then the code skips the block.

Maybe using a if filter_type in ['list', 'of', 'filter', 'types']: construction would fix this?

leoscholl commented 2 years ago

if filter_type in ['list', 'of', 'filter', 'types']: would be good, yes.

in if filter_type == 'bandpass' or 'bandstop', the or combines two boolean expressions: filter_type == 'bandpass' on the left, which will be False by default, and 'bandstop' on the right, which if you call bool('bandstop') you will see equates to True always.

so the other way to fix this would be if filter_type == 'bandpass' or filter_type == 'bandstop':

leoscholl commented 2 years ago

this still doesn't quite work to do lowpass or highpass filter. if i use cutoff_freq=120 and filter_type='lowpass' i get:

Traceback (most recent call last):
  File "/home/leo/code/analyze/tests/test_preproc.py", line 725, in test_parse_bmi3d_v11
    data, metadata = parse_bmi3d(data_dir, files) # and with ecube data
  File "/home/leo/code/analyze/aopy/preproc/bmi3d.py", line 93, in parse_bmi3d
    data, metadata = _parse_bmi3d_v1(data_dir, files)
  File "/home/leo/code/analyze/aopy/preproc/bmi3d.py", line 256, in _parse_bmi3d_v1
    filt_out, freq = precondition.butterworth_filter_data(cursor_analog_cm, analog_samplerate, metadata_dict['fps'], filter_type='lowpass')
  File "/home/leo/code/analyze/aopy/precondition.py", line 73, in butterworth_filter_data
    for _, w in enumerate(Wn):
TypeError: 'numpy.float64' object is not iterable

and if i use cutoff_freq=[120] i get

Traceback (most recent call last):
  File "/home/leo/code/analyze/tests/test_preproc.py", line 725, in test_parse_bmi3d_v11
    data, metadata = parse_bmi3d(data_dir, files) # and with ecube data
  File "/home/leo/code/analyze/aopy/preproc/bmi3d.py", line 93, in parse_bmi3d
    data, metadata = _parse_bmi3d_v1(data_dir, files)
  File "/home/leo/code/analyze/aopy/preproc/bmi3d.py", line 256, in _parse_bmi3d_v1
    filt_out, freq = precondition.butterworth_filter_data(cursor_analog_cm, analog_samplerate, [metadata_dict['fps']], filter_type='lowpass')
  File "/home/leo/code/analyze/aopy/precondition.py", line 78, in butterworth_filter_data
    if len(wp) == 2:
TypeError: object of type 'numpy.float64' has no len()

should update the test function to include a filter_type='lowpass'

leoscholl commented 2 years ago

workaround for now is to specify butterworth order