示例#1
0
def test_fit():
    speech_response = loadmat(str(root / "data" / "speech_data.mat"))
    fs = speech_response["fs"][0][0]
    response = np.stack([speech_response["resp"][0:100] for _ in range(20)])
    stimulus = np.stack([speech_response["stim"][0:100] for _ in range(20)])
    tmin = np.random.uniform(-0.1, 0.05)
    tmax = np.random.uniform(0.1, 0.4)
    direction = np.random.choice([1, -1])
    reg = np.random.uniform(0, 10)
    trf = TRF(direction=direction)
    trf.fit(stimulus, response, fs, tmin, tmax, reg)
    reg = [np.random.uniform(0, 10) for _ in range(randint(2, 10))]
    trf = TRF(direction=direction)
    trf.fit(stimulus, response, fs, tmin, tmax, reg)
示例#2
0
def test_save_load():
    tmpdir = Path(tempfile.gettempdir())
    speech_response = loadmat(str(root / "data" / "speech_data.mat"))
    fs = speech_response["fs"][0][0]
    response = np.stack([speech_response["resp"][0:100] for _ in range(20)])
    stimulus = np.stack([speech_response["stim"][0:100] for _ in range(20)])
    tmin = np.random.uniform(-0.1, 0.05)
    tmax = np.random.uniform(0.1, 0.4)
    direction = np.random.choice([1, -1])
    reg = np.random.uniform(0, 10)
    trf1 = TRF(direction=direction)
    trf1.fit(stimulus, response, fs, tmin, tmax, reg)
    trf1.save(tmpdir / "test.trf")
    trf2 = TRF()
    trf2.load(tmpdir / "test.trf")
    np.testing.assert_equal(trf1.weights, trf2.weights)
def test_encoding():
    # load the data
    speech_response = loadmat(str(root / "data" / "speech_data.mat"))
    fs = speech_response["fs"][0][0]
    response = speech_response["resp"]
    stimuli = speech_response["stim"]

    # and the expected result
    encoder_results = loadmat(str(root / "results" / "encoder_results.mat"))
    # w = input features (stimuli) x times x output features (=channels)
    w, b, times, _, direction, kind = encoder_results["modelEncoder"][0][0]
    prediction1 = encoder_results["predResp"]
    correlation1 = encoder_results["predRespStats"]["r"][0][0][0]
    error1 = encoder_results["predRespStats"]["err"][0][0][0]
    # train the TRF model on the data
    trf_encoder = TRF()
    tmin, tmax = -0.1, 0.2
    trf_encoder.train(stimuli, response, fs, tmin, tmax, 100)
    # use the trained TRF to predict data
    prediction2, correlation2, error2 = trf_encoder.predict(
        stimuli, response, average_features=False)

    # check that the results are the same as in matlab
    np.testing.assert_almost_equal(trf_encoder.weights, w, decimal=12)
    np.testing.assert_almost_equal(trf_encoder.bias, b, decimal=12)
    np.testing.assert_equal(trf_encoder.times, times[0] / 1e3)
    np.testing.assert_almost_equal(prediction1, prediction2, decimal=12)
    np.testing.assert_almost_equal(correlation1, correlation2, decimal=12)
    np.testing.assert_almost_equal(error1, error2, decimal=12)

    # we should get the same results if we duplicate the data and use the
    # fit function
    stimuli = np.stack([stimuli for _ in range(10)], axis=0)
    response = np.stack([response for _ in range(10)], axis=0)
    trf_encoder = TRF()
    tmin, tmax = -0.1, 0.2
    trf_encoder.fit(stimuli, response, fs, tmin, tmax, 100)
    prediction2, correlation2, error2 = trf_encoder.predict(
        stimuli, response, average_features=False)

    # check that the results are the same as in matlab
    np.testing.assert_almost_equal(trf_encoder.weights, w, decimal=12)
    np.testing.assert_almost_equal(trf_encoder.bias, b, decimal=12)
    np.testing.assert_equal(trf_encoder.times, times[0] / 1e3)
    np.testing.assert_almost_equal(correlation1, correlation2, decimal=12)
    np.testing.assert_almost_equal(error1, error2, decimal=12)