|
| 1 | +from pathlib import Path |
| 2 | +from bcipy.helpers.load import load_json_parameters, load_raw_data, load_experimental_data |
| 3 | +from bcipy.helpers.triggers import trigger_decoder, TriggerType |
| 4 | +from bcipy.config import ( |
| 5 | + BCIPY_ROOT, |
| 6 | + DEFAULT_PARAMETER_FILENAME, |
| 7 | + RAW_DATA_FILENAME, |
| 8 | + TRIGGER_FILENAME, |
| 9 | + DEFAULT_DEVICE_SPEC_FILENAME) |
| 10 | + |
| 11 | +from bcipy.acquisition import devices |
| 12 | +from bcipy.helpers.acquisition import analysis_channels |
| 13 | +from bcipy.helpers.visualization import visualize_erp |
| 14 | +from bcipy.signal.process import get_default_transform |
| 15 | +from bcipy.signal.evaluate.artifact import ArtifactDetection |
| 16 | +from bcipy.helpers.report import Report, SignalReportSection, SessionReportSection |
| 17 | + |
| 18 | + |
| 19 | +if __name__ == "__main__": |
| 20 | + import argparse |
| 21 | + |
| 22 | + parser = argparse.ArgumentParser() |
| 23 | + parser.add_argument( |
| 24 | + '-p', |
| 25 | + '--path', |
| 26 | + help='Path to the directory with >= 1 sessions to be analyzed for artifacts', |
| 27 | + required=False) |
| 28 | + |
| 29 | + args = parser.parse_args() |
| 30 | + colabel = True |
| 31 | + # if no path is provided, prompt for one using a GUI |
| 32 | + path = args.path |
| 33 | + if not path: |
| 34 | + path = load_experimental_data() |
| 35 | + |
| 36 | + trial_window = (0, 1.0) |
| 37 | + |
| 38 | + positions = None |
| 39 | + for session in Path(path).iterdir(): |
| 40 | + # loop through the sessions, pausing after each one to allow for manual stopping |
| 41 | + if session.is_dir(): |
| 42 | + print(f'Processing {session}') |
| 43 | + prompt = input('Hit enter to continue or type "skip" to skip processing: ') |
| 44 | + if prompt != 'skip': |
| 45 | + # load the parameters from the data directory |
| 46 | + parameters = load_json_parameters( |
| 47 | + f'{session}/{DEFAULT_PARAMETER_FILENAME}', value_cast=True) |
| 48 | + |
| 49 | + # load the raw data from the data directory |
| 50 | + raw_data = load_raw_data(Path(session, f'{RAW_DATA_FILENAME}.csv')) |
| 51 | + type_amp = raw_data.daq_type |
| 52 | + channels = raw_data.channels |
| 53 | + sample_rate = raw_data.sample_rate |
| 54 | + downsample_rate = parameters.get("down_sampling_rate") |
| 55 | + notch_filter = parameters.get("notch_filter_frequency") |
| 56 | + filter_high = parameters.get("filter_high") |
| 57 | + filter_low = parameters.get("filter_low") |
| 58 | + filter_order = parameters.get("filter_order") |
| 59 | + static_offset = parameters.get("static_trigger_offset") |
| 60 | + |
| 61 | + default_transform = get_default_transform( |
| 62 | + sample_rate_hz=sample_rate, |
| 63 | + notch_freq_hz=notch_filter, |
| 64 | + bandpass_low=filter_low, |
| 65 | + bandpass_high=filter_high, |
| 66 | + bandpass_order=filter_order, |
| 67 | + downsample_factor=downsample_rate, |
| 68 | + ) |
| 69 | + |
| 70 | + # load the triggers |
| 71 | + if colabel: |
| 72 | + trigger_type, trigger_timing, trigger_label = trigger_decoder( |
| 73 | + offset=parameters.get('static_trigger_offset'), |
| 74 | + trigger_path=f"{session}/{TRIGGER_FILENAME}", |
| 75 | + exclusion=[TriggerType.PREVIEW, TriggerType.EVENT, TriggerType.FIXATION], |
| 76 | + ) |
| 77 | + triggers = (trigger_type, trigger_timing, trigger_label) |
| 78 | + else: |
| 79 | + triggers = None |
| 80 | + |
| 81 | + devices.load(Path(BCIPY_ROOT, DEFAULT_DEVICE_SPEC_FILENAME)) |
| 82 | + device_spec = devices.preconfigured_device(raw_data.daq_type) |
| 83 | + channel_map = analysis_channels(channels, device_spec) |
| 84 | + |
| 85 | + # check the device spec for any frontal channels to use for EOG detection |
| 86 | + eye_channels = [] |
| 87 | + for channel in device_spec.channels: |
| 88 | + if 'F' in channel: |
| 89 | + eye_channels.append(channel) |
| 90 | + if len(eye_channels) == 0: |
| 91 | + eye_channels = None |
| 92 | + |
| 93 | + artifact_detector = ArtifactDetection( |
| 94 | + raw_data, |
| 95 | + parameters, |
| 96 | + device_spec, |
| 97 | + eye_channels=eye_channels, |
| 98 | + session_triggers=triggers) |
| 99 | + |
| 100 | + detected = artifact_detector.detect_artifacts() |
| 101 | + figure_handles = visualize_erp( |
| 102 | + raw_data, |
| 103 | + channel_map, |
| 104 | + trigger_timing, |
| 105 | + trigger_label, |
| 106 | + trial_window, |
| 107 | + transform=default_transform, |
| 108 | + plot_average=True, |
| 109 | + plot_topomaps=True, |
| 110 | + ) |
| 111 | + |
| 112 | + # Try to find a pkl file in the session folder |
| 113 | + pkl_file = None |
| 114 | + for file in session.iterdir(): |
| 115 | + if file.suffix == '.pkl': |
| 116 | + pkl_file = file |
| 117 | + break |
| 118 | + |
| 119 | + if pkl_file: |
| 120 | + auc = pkl_file.stem.split('_')[-1] |
| 121 | + else: |
| 122 | + auc = 'No Signal Model found in session folder' |
| 123 | + |
| 124 | + sr = SignalReportSection(figure_handles, artifact_detector) |
| 125 | + report = Report(session) |
| 126 | + session = {'Label': 'Demo Session Report', 'AUC': auc} |
| 127 | + session_text = SessionReportSection(session) |
| 128 | + report.add(session_text) |
| 129 | + report.add(sr) |
| 130 | + report.compile() |
| 131 | + report.save() |
0 commit comments