예제 #1
0
def test_missing_task(default_param):
    # input invalid value for tasks
    default_param["load_data"]["tasks"] = ""

    # Load data using the invalid field
    with pytest.raises(Exception):
        load.load_files(default_param["load_data"])
        assert True
예제 #2
0
def test_missing_except_subj(default_param):
    # input invalid value for tasks
    default_param["load_data"]["exceptions"]["subjects"] = ""
    default_param["load_data"]["exceptions"]["tasks"] = ["*"]
    default_param["load_data"]["exceptions"]["runs"] = ["*"]

    # Load data using the invalid field
    with pytest.raises(Exception):
        load.load_files(default_param["load_data"])
        assert True
예제 #3
0
def test_missing_data(default_param, tmp_path):
    # create empty and temporary directory
    empty_data = tmp_path / "empty"
    empty_data.mkdir()

    # input invalid value for tasks
    default_param["load_data"]["root"] = empty_data

    # Load data using the invalid field
    with pytest.raises(Exception):
        load.load_files(default_param["load_data"])
        assert True
예제 #4
0
def test_select_subj_task_except_task(default_param, subj_files):
    # select subjects & tasks (randomize?)
    selected_subjects = ["NDARAB793GL3"]
    selected_tasks = ["ContrastChangeBlock1"]
    default_param["load_data"]["subjects"] = selected_subjects
    default_param["load_data"]["tasks"] = selected_tasks

    # select excluded subjects (randomize?)
    exc_task = ["ContrastChangeBlock1"]
    default_param["load_data"]["exceptions"]["subjects"] = ["*"]
    default_param["load_data"]["exceptions"]["tasks"] = exc_task
    default_param["load_data"]["exceptions"]["runs"] = ["*"]

    # Load data using the selected subjects & tasks
    data = load.load_files(default_param["load_data"])

    # check if the number of files loaded matches
    # the total number of files for all selected subjects excluding exceptions
    count = 0
    for subject in selected_subjects:
        for file in subj_files[subject]:

            # split and find task of file (TODO: need to BIDSIFY)
            task_id = [s for s in file.split("_") if "task" in s][0]
            task = [s for s in task_id.split("-") if s != "task"][0]

            if task in exc_task:
                continue
            elif task in selected_tasks:
                count += 1
    assert len(data) == count
예제 #5
0
def test_except_subj_task(default_param, avail_subj, subj_files):
    # select excluded subjects & tasks (randomize?)
    exc_subj = ["NDARAB793GL3"]
    exc_task = ["ContrastChangeBlock1"]

    default_param["load_data"]["exceptions"]["subjects"] = exc_subj
    default_param["load_data"]["exceptions"]["tasks"] = exc_task
    default_param["load_data"]["exceptions"]["runs"] = ["*"]

    # Load data using the excluded subjects & tasks
    data = load.load_files(default_param["load_data"])

    # check if the number of files loaded matches
    # the total number of all subjects & tasks apart from the excluded
    count = 0
    for subject in avail_subj:
        for file in subj_files[subject]:

            file_vals = file.split("_")

            # split and find subject of file (TODO: need to BIDSIFY)
            sub_id = [s for s in file_vals if "sub" in s][0]
            sub = [s for s in sub_id.split("-") if s != "sub"][0]

            # split and find task of file (TODO: need to BIDSIFY)
            task_id = [s for s in file.split("_") if "task" in s][0]
            task = [s for s in task_id.split("-") if s != "task"][0]

            if sub in exc_subj and task in exc_task:
                continue
            count += 1
    assert len(data) == count
예제 #6
0
def test_return_values(default_param, select_subjects, select_tasks):

    default_param["load_data"]["subjects"] = select_subjects
    default_param["load_data"]["tasks"] = select_tasks

    # Load data using the selected subjects & tasks
    data = load.load_files(default_param["load_data"])

    # get the pipeline steps
    feature_params = default_param["preprocess"]
    interp_param = feature_params["interpolate_data"]

    for file in data:
        eeg_obj = mne_bids.read_raw_bids(file)

        # generate epoched object to be interpolated
        epo, _ = pre.segment_data(eeg_obj, **feature_params["segment_data"])

        # interpolate data
        interp_eeg, output_dict = pre.interpolate_data(epo, **interp_param)

        # assert that None does not exist in final reject
        assert None not in output_dict.values()

        # assert object returned is epoch object
        assert isinstance(interp_eeg, Epochs)
예제 #7
0
def test_select_all(default_param, avail_subj, subj_files):
    # Load data using the default parameters
    data = load.load_files(default_param["load_data"])

    # check if the number of files loaded matches
    # the number of all files available
    count = 0
    for subject in avail_subj:
        count += len(subj_files[subject])
    assert len(data) == count
예제 #8
0
def test_except_all(default_param):
    # select all for exceptions
    default_param["load_data"]["exceptions"]["subjects"] = ["*"]
    default_param["load_data"]["exceptions"]["tasks"] = ["*"]
    default_param["load_data"]["exceptions"]["runs"] = ["*"]

    # Load data using the excluded subjects & tasks
    data = load.load_files(default_param["load_data"])

    # assert nothing is selected
    assert len(data) == 0
예제 #9
0
def test_select_subj(default_param, subj_files):
    # select participants (make this a random selection?)
    selected_subjects = ["NDARAB793GL3"]
    default_param["load_data"]["subjects"] = selected_subjects

    # Load data using the selected participants
    data = load.load_files(default_param["load_data"])

    # check if the number of files loaded matches
    # the total number of files for all selected subjects
    count = 0
    for subject in selected_subjects:
        count += len(subj_files[subject])
    assert len(data) == count
예제 #10
0
def test_return_values(default_param, select_subjects, select_tasks):

    default_param["load_data"]["subjects"] = select_subjects
    default_param["load_data"]["tasks"] = select_tasks

    # Load data using the selected subjects & tasks
    data = load.load_files(default_param["load_data"])

    for file in data:
        eeg_obj = mne_bids.read_raw_bids(file)

        # reject epochs
        badchans_raw, output_dict = pre.identify_badchans_raw(eeg_obj)

        # assert that None does not exist in bad chans
        assert None not in output_dict.values()
예제 #11
0
def test_except_subj(default_param, avail_subj, subj_files):
    # select excluded subjects (randomize?)
    exc_subj = ["NDARAB793GL3"]
    default_param["load_data"]["exceptions"]["subjects"] = exc_subj
    default_param["load_data"]["exceptions"]["tasks"] = ["*"]
    default_param["load_data"]["exceptions"]["runs"] = ["*"]

    # Load data using the excluded subjects
    data = load.load_files(default_param["load_data"])

    # get the excluded subset of subjects
    subjects_diff = set(avail_subj).difference(set(exc_subj))

    # check if the number of files loaded matches
    # the total number of all subjects apart from the excluded
    count = 0
    for subject in subjects_diff:
        count += len(subj_files[subject])
    assert len(data) == count
예제 #12
0
def test_return_values(default_param, select_subjects, select_tasks):

    default_param["load_data"]["subjects"] = select_subjects
    default_param["load_data"]["tasks"] = select_tasks

    # Load data using the selected subjects & tasks
    data = load.load_files(default_param["load_data"])

    # get the pipeline steps
    feature_params = default_param["preprocess"]
    ica_param = feature_params["ica_raw"]

    for file in data:
        eeg_obj = mne_bids.read_raw_bids(file)

        # reject epochs
        ica_raw, output_dict = pre.ica_raw(eeg_obj, **ica_param)

        # assert that None does not exist in bad chans
        assert None not in output_dict.values()
예제 #13
0
def test_return_values(default_param, select_subjects, select_tasks):

    default_param["load_data"]["subjects"] = select_subjects
    default_param["load_data"]["tasks"] = select_tasks

    # Load data using the selected subjects & tasks
    data = load.load_files(default_param["load_data"])

    # get the pipeline steps
    feature_params = default_param["preprocess"]
    filt_param = feature_params["filter_data"]

    for file in data:
        eeg_obj = mne_bids.read_raw_bids(file)

        # filter data
        filt_eeg, output_dict = pre.filter_data(eeg_obj, **filt_param)

        # assert that None does not exist in final reject
        assert None not in output_dict.values()
예제 #14
0
def test_select_task(default_param, avail_subj, subj_files):
    # select tasks (make this a random selection?)
    selected_tasks = ["ContrastChangeBlock1"]
    default_param["load_data"]["tasks"] = selected_tasks

    # Load data using the selected task
    data = load.load_files(default_param["load_data"])

    # check if the number of files loaded matches
    # the total number of files for all selected task
    count = 0
    for subject in avail_subj:
        for file in subj_files[subject]:

            # split and find task of file (TODO: need to BIDSIFY)
            task_id = [s for s in file.split("_") if "task" in s][0]
            task = [s for s in task_id.split("-") if s != "task"][0]

            if task in selected_tasks:
                count += 1
    assert len(data) == count
예제 #15
0
def test_return_values(default_param, select_subjects, select_tasks):

    default_param["load_data"]["subjects"] = select_subjects
    default_param["load_data"]["tasks"] = select_tasks

    # Load data using the selected subjects & tasks
    data = load.load_files(default_param["load_data"])

    # get the pipeline steps
    feature_params = default_param["preprocess"]
    reref_param = feature_params["reref_raw"]

    for file in data:
        eeg_obj = mne_bids.read_raw_bids(file)

        # reref the data
        reref_eeg, output_dict = pre.reref_raw(eeg_obj, **reref_param)

        # assert that None does not exist in final reject
        assert None not in output_dict.values()

        # assert object returned is epoch object
        assert isinstance(reref_eeg, Epochs)
예제 #16
0
def test_except_value(default_param, select_subjects, error_tasks):

    default_param["load_data"]["subjects"] = select_subjects
    default_param["load_data"]["tasks"] = error_tasks

    # Load data using the selected subjects & tasks
    data = load.load_files(default_param["load_data"])

    # get the pipeline steps
    feature_params = default_param["preprocess"]

    for file in data:
        eeg_obj = mne_bids.read_raw_bids(file)

        # generate epoched object to be rejected
        epo, _ = pre.segment_data(eeg_obj, **feature_params["segment_data"])

        # attempt to reject epochs with data containing only one entire epoch
        # across each channel
        with pytest.raises(ValueError):
            _, output_dict = pre.final_reject_epoch(epo)

            assert True
            assert isinstance(output_dict, dict)
예제 #17
0
# get data and metadata parameters
preprocess_params = user_params["preprocess"]
data_params = user_params["load_data"]
write_params = user_params["output_data"]

# get output root and channel type of data
ch_type = data_params["channel-type"]
output_path = write_params["root"]

# overwrite data_params using sys.argv arguments
if len(sys.argv) > 1:
    data_params["subjects"] = [sys.argv[1]]

# get set of subjects & tasks to run while omitting existing exceptions
data = load.load_files(data_params)

# for each file in filtered data
for file in data:
    # load raw data
    eeg_obj = mne_bids.read_raw_bids(file)

    outputs = [None] * len(preprocess_params)
    # for each pipeline step in user_params, execute with parameters
    for idx, (func, params) in enumerate(preprocess_params.items()):
        eeg_obj, outputs[idx] = getattr(preprocess, func)(eeg_obj, **params)

        # check if this is the fully preprocessed eeg object
        final = idx == len(preprocess_params.items()) - 1
        write.write_eeg_data(eeg_obj, func, file, ch_type, final, output_path)