Exemplo n.º 1
0
def capsules():
    """
    Generating two different capsules to test: one from an .easy file and the other from nedf files.
    """
    if os.path.isdir(testpath) is False:
        pytest.exit(
            'The the -testfiles- folder path of your computer does not match with the one written '
            'in test_data.py (testpath) ')
    easy_filepath = os.path.join(
        testpath,
        str(easyTestData[list(easyTestData.keys())[0]]['filename']) + '.easy')
    nedf_filepath = os.path.join(
        testpath,
        str(nedfTestData[list(nedfTestData.keys())[0]]['filename']) + '.nedf')

    caps = {
        'easy': Capsule(easy_filepath),
        'nedf': Capsule(nedf_filepath)
    }  # Dictionary containing a capsule per test file.
    for file in caps:
        if caps[file].good_init is False:
            pytest.exit(
                "File not found to create the capsule. Check that the file exists or the reader's tests."
            )

    return caps
Exemplo n.º 2
0
def test_rereference(ref_chan, exp_init_offsets, fobj):
    """
    We check for any input reference_channel in the param attribute of the Frida object.
    This function is made to test any input test file: with or without info file, with any number of channels.
    """

    fobj = define_testdata(fobj)
    fobj.QC(plotit=False)

    # Test that the test data is saved as expected
    initial_offsets = 0.5 * np.ones(fobj.c.num_channels)
    initial_offsets[1] = 1.1
    if not np.array_equal(initial_offsets, np.round(fobj.offsets, decimals=3)):
        pytest.exit(
            'The test data of the set up is not the one expected for this test. Exiting...'
        )

    else:  # Then we can perform the tests...

        # First we need to get the electrode references from the test data
        test_elist = easyTestData[list(easyTestData.keys())[0]]['chanlist']
        numchan = len(test_elist)
        test_refs = []
        ave_flag = False
        if test_elist[
                0] is not 'Ch1':  # If the test file has an info file with it
            try:  # the index of electrodes, not strings
                for i in ref_chan:
                    test_refs.append(test_elist[i])
            except:  # strings --> Ave, reference. It can be either 'ave', 'average'...
                test_refs = ref_chan
                ave_flag = True
        else:
            test_refs = 'ave'
            ave_flag = True

        # Now that we have the reference channel list saved, we can test the _rereference function in Frida.
        fobj.param[
            'reference_electrodes'] = test_refs  # Save the reference chan list in Frida object, then rereference
        fobj.preprocess(pipeline=['rereference'])
        fobj.QC(plotit=False)

        # We test the 5 first channels of the data since from the fifth, they have the same signal in every channel
        if not ave_flag:  # If we are not performing the average rereferencing...
            assert np.array_equal(exp_init_offsets,
                                  np.round(fobj.offsets[:5], decimals=3))
        else:  # If we do the average rereferencing, we need to know how many electrodes we have:
            if numchan == 8 and ref_chan == 'ave8':
                assert np.array_equal(exp_init_offsets,
                                      np.round(fobj.offsets[:5], decimals=3))
            elif numchan == 20 and ref_chan == 'ave20':
                assert np.array_equal(exp_init_offsets,
                                      np.round(fobj.offsets[:5], decimals=3))
            elif numchan == 32 and ref_chan == 'ave32':
                assert np.array_equal(exp_init_offsets,
                                      np.round(fobj.offsets[:5], decimals=3))
Exemplo n.º 3
0
def test_reset(fobj):
    """
    Since we are changing already the values in the setup, the reset function should bring the np_eeg to the original.
    Also, we are checking here that the np_eeg_original is the expected and that the detrend flag is False again.
    """
    # testset:
    fobj = define_testdata(fobj)
    first = easyTestData[list(easyTestData.keys())[0]]['first_easy_row']
    numchan = len(easyTestData[list(easyTestData.keys())[0]]['chanlist'])
    first_eegdata = np.float32(np.array(first[:numchan]) / 1000.)

    pipeline = ['reset']
    fobj.preprocess(pipeline=pipeline)
    assert np.array_equal(first_eegdata, fobj.eeg[0, :])
    assert np.array_equal(first_eegdata, fobj.eeg_original[0, :])
    assert fobj.detrend_flag is False

    # And again...
    pipeline = ['bandpassfilter', 'reset']
    fobj.preprocess(pipeline=pipeline)
    assert np.array_equal(first_eegdata, fobj.eeg[0, :])
    assert np.array_equal(first_eegdata, fobj.eeg_original[0, :])
    assert fobj.detrend_flag is False
Exemplo n.º 4
0
def test_attributes(capsules):
    """
    We are creating another directory with the test data to test a capsule created with either .easy and .nedf files.
    Since the capsule class copies all the information that the reader gets, it is necessary first to check that the
    readers tests pass (hardcore dependency needed).

    """
    tests = {
        'easy': easyTestData[list(easyTestData.keys())[0]],
        'nedf': nedfTestData[list(nedfTestData.keys())[0]]
    }
    for file in tests:
        # Getting truth from test_data to perform asserts...
        numchan = len(tests[file]['chanlist'])
        first = tests[file]['first_easy_row']
        first_eegdata = np.float32(np.array(first[:numchan]) / 1000.)
        first_markdata = np.array(first[numchan + 3])
        first_dateraw = first[-1]
        first_date = datetime.datetime.fromtimestamp(
            first_dateraw / 1000).strftime("%Y-%m-%d %H:%M:%S")

        # Assertions now...
        assert capsules[file].good_init
        assert 'anonymous' == capsules[file].author
        assert first_date == capsules[file].eegstartdate

        # file path names assertions:
        assert capsules[file].filepath.endswith(tests[file]['filename'] + '.' +
                                                file)
        assert tests[file]['filename'] == capsules[file].basename
        filepath = os.path.join(testpath, tests[file]['filename'])
        assert filepath == capsules[file].filenameroot

        # time related assertions:
        assert 500. == capsules[file].fs
        assert tests[file]['num_samples'] == len(capsules[file].np_time)

        # channel data assertions:
        assert len(tests[file]['chanlist']) == capsules[file].num_channels
        assert tests[file]['chanlist'] == capsules[file].electrodes

        # eeg assertions:
        assert len(first_eegdata) == len(capsules[file].np_eeg[0, :])
        assert tests[file]['num_samples'] == len(capsules[file].np_eeg)
        assert np.array_equal(np.round(first_eegdata),
                              np.round(capsules[file].np_eeg[0, :]))

        # markers assertions:
        assert np.array_equal(first_markdata, capsules[file].np_markers[0])
        assert tests[file]['num_samples'] == len(capsules[file].np_markers)
Exemplo n.º 5
0
def fobj():
    """
    Since we have a test for the readers and for capsule (with two extension types), it is enough to test Frida object
    with one of the easyFiles. In this case, we are always going to take the first one that it finds in easyTestData
    so we make sure we have at leasst one file to test!
    """
    if os.path.isdir(testpath) is False:
        pytest.exit(
            'The the -testfiles- folder path of your computer does not match with the one written '
            'in test_data.py (testpath) ')
    filepath = os.path.join(
        testpath,
        str(easyTestData[list(easyTestData.keys())[0]]['filename']) + '.easy')

    fobj = Frida(filepath)
    if fobj.c.good_init is False:
        pytest.exit(
            "File not found to create the capsule. Check that the file exists or the reader's tests."
        )

    # Now we are going to fill the capsule eeg data with a synthetic signal to perform the signal processing tests:
    fobj = define_testdata(fobj)
    fobj.updatePSD()
    return fobj