def test_receptive_field(): """Test model prep and fitting.""" from sklearn.linear_model import Ridge # Make sure estimator pulling works mod = Ridge() # Test the receptive field model # Define parameters for the model and simulate inputs + weights tmin, tmax = 0., 10. n_feats = 3 X = rng.randn(n_feats, 10000) w = rng.randn(int((tmax - tmin) + 1) * n_feats) # Delay inputs and cut off first 4 values since they'll be cut in the fit X_del = np.vstack(_delay_time_series(X, tmin, tmax, 1., axis=-1)) y = np.dot(w, X_del) X = np.rollaxis(X, -1, 0) # time to first dimension # Fit the model and test values feature_names = ['feature_%i' % ii for ii in [0, 1, 2]] rf = ReceptiveField(tmin, tmax, 1, feature_names, estimator=mod) rf.fit(X, y) assert_array_equal(rf.delays_, np.arange(tmin, tmax + 1)) y_pred = rf.predict(X) assert_array_almost_equal(y[rf.keep_samples_], y_pred.squeeze()[rf.keep_samples_], 2) scores = rf.score(X, y) assert_true(scores > .99) assert_array_almost_equal(rf.coef_.reshape(-1, order='F'), w, 2) # Make sure different input shapes work rf.fit(X[:, np.newaxis:, ], y[:, np.newaxis]) rf.fit(X, y[:, np.newaxis]) assert_raises(ValueError, rf.fit, X[..., np.newaxis], y) assert_raises(ValueError, rf.fit, X[:, 0], y) assert_raises(ValueError, rf.fit, X[..., np.newaxis], np.tile(y[..., np.newaxis], [2, 1, 1])) # stim features must match length of input data assert_raises(ValueError, rf.fit, X[:, :1], y) # auto-naming features rf = ReceptiveField(tmin, tmax, 1, estimator=mod) rf.fit(X, y) assert_equal(rf.feature_names, ['feature_%s' % ii for ii in [0, 1, 2]]) # X/y same n timepoints assert_raises(ValueError, rf.fit, X, y[:-2]) # Float becomes ridge rf = ReceptiveField(tmin, tmax, 1, ['one', 'two', 'three'], estimator=0) str(rf) # repr works before fit rf.fit(X, y) assert_true(isinstance(rf.estimator_, TimeDelayingRidge)) str(rf) # repr works after fit rf = ReceptiveField(tmin, tmax, 1, ['one'], estimator=0) rf.fit(X[:, [0]], y) str(rf) # repr with one feature # Should only accept estimators or floats rf = ReceptiveField(tmin, tmax, 1, estimator='foo') assert_raises(ValueError, rf.fit, X, y) rf = ReceptiveField(tmin, tmax, 1, estimator=np.array([1, 2, 3])) assert_raises(ValueError, rf.fit, X, y) # tmin must be <= tmax rf = ReceptiveField(5, 4, 1) assert_raises(ValueError, rf.fit, X, y) # scorers for key, val in _SCORERS.items(): rf = ReceptiveField(tmin, tmax, 1, ['one'], estimator=0, scoring=key) rf.fit(X[:, [0]], y) y_pred = rf.predict(X[:, [0]]) assert_array_almost_equal(val(y[:, np.newaxis], y_pred), rf.score(X[:, [0]], y), 4) # Need 2D input assert_raises(ValueError, _SCORERS['corrcoef'], y.squeeze(), y_pred) # Need correct scorers rf = ReceptiveField(tmin, tmax, 1., scoring='foo') assert_raises(ValueError, rf.fit, X, y)
def test_receptive_field(): """Test model prep and fitting.""" from sklearn.linear_model import Ridge # Make sure estimator pulling works mod = Ridge() # Test the receptive field model # Define parameters for the model and simulate inputs + weights tmin, tmax = -10., 0 n_feats = 3 X = rng.randn(10000, n_feats) w = rng.randn(int((tmax - tmin) + 1) * n_feats) # Delay inputs and cut off first 4 values since they'll be cut in the fit X_del = np.concatenate(_delay_time_series(X, tmin, tmax, 1.).transpose(2, 0, 1), axis=1) y = np.dot(X_del, w) # Fit the model and test values feature_names = ['feature_%i' % ii for ii in [0, 1, 2]] rf = ReceptiveField(tmin, tmax, 1, feature_names, estimator=mod) rf.fit(X, y) assert_array_equal(rf.delays_, np.arange(tmin, tmax + 1)) y_pred = rf.predict(X) assert_allclose(y[rf.valid_samples_], y_pred[rf.valid_samples_], atol=1e-2) scores = rf.score(X, y) assert_true(scores > .99) assert_allclose(rf.coef_.T.ravel(), w, atol=1e-2) # Make sure different input shapes work rf.fit(X[:, np.newaxis:, ], y[:, np.newaxis]) rf.fit(X, y[:, np.newaxis]) assert_raises(ValueError, rf.fit, X[..., np.newaxis], y) assert_raises(ValueError, rf.fit, X[:, 0], y) assert_raises(ValueError, rf.fit, X[..., np.newaxis], np.tile(y[..., np.newaxis], [2, 1, 1])) # stim features must match length of input data assert_raises(ValueError, rf.fit, X[:, :1], y) # auto-naming features rf = ReceptiveField(tmin, tmax, 1, estimator=mod) rf.fit(X, y) assert_equal(rf.feature_names, ['feature_%s' % ii for ii in [0, 1, 2]]) # X/y same n timepoints assert_raises(ValueError, rf.fit, X, y[:-2]) # Float becomes ridge rf = ReceptiveField(tmin, tmax, 1, ['one', 'two', 'three'], estimator=0) str(rf) # repr works before fit rf.fit(X, y) assert_true(isinstance(rf.estimator_, TimeDelayingRidge)) str(rf) # repr works after fit rf = ReceptiveField(tmin, tmax, 1, ['one'], estimator=0) rf.fit(X[:, [0]], y) str(rf) # repr with one feature # Should only accept estimators or floats rf = ReceptiveField(tmin, tmax, 1, estimator='foo') assert_raises(ValueError, rf.fit, X, y) rf = ReceptiveField(tmin, tmax, 1, estimator=np.array([1, 2, 3])) assert_raises(ValueError, rf.fit, X, y) # tmin must be <= tmax rf = ReceptiveField(5, 4, 1) assert_raises(ValueError, rf.fit, X, y) # scorers for key, val in _SCORERS.items(): rf = ReceptiveField(tmin, tmax, 1, ['one'], estimator=0, scoring=key) rf.fit(X[:, [0]], y) y_pred = rf.predict(X[:, [0]]).T.ravel()[:, np.newaxis] assert_allclose(val(y[:, np.newaxis], y_pred), rf.score(X[:, [0]], y), rtol=1e-2) # Need 2D input assert_raises(ValueError, _SCORERS['corrcoef'], y.ravel(), y_pred) # Need correct scorers rf = ReceptiveField(tmin, tmax, 1., scoring='foo') assert_raises(ValueError, rf.fit, X, y)
def test_receptive_field(n_jobs): """Test model prep and fitting.""" from sklearn.linear_model import Ridge # Make sure estimator pulling works mod = Ridge() rng = np.random.RandomState(1337) # Test the receptive field model # Define parameters for the model and simulate inputs + weights tmin, tmax = -10., 0 n_feats = 3 rng = np.random.RandomState(0) X = rng.randn(10000, n_feats) w = rng.randn(int((tmax - tmin) + 1) * n_feats) # Delay inputs and cut off first 4 values since they'll be cut in the fit X_del = np.concatenate( _delay_time_series(X, tmin, tmax, 1.).transpose(2, 0, 1), axis=1) y = np.dot(X_del, w) # Fit the model and test values feature_names = ['feature_%i' % ii for ii in [0, 1, 2]] rf = ReceptiveField(tmin, tmax, 1, feature_names, estimator=mod, patterns=True) rf.fit(X, y) assert_array_equal(rf.delays_, np.arange(tmin, tmax + 1)) y_pred = rf.predict(X) assert_allclose(y[rf.valid_samples_], y_pred[rf.valid_samples_], atol=1e-2) scores = rf.score(X, y) assert scores > .99 assert_allclose(rf.coef_.T.ravel(), w, atol=1e-3) # Make sure different input shapes work rf.fit(X[:, np.newaxis:, ], y[:, np.newaxis]) rf.fit(X, y[:, np.newaxis]) pytest.raises(ValueError, rf.fit, X[..., np.newaxis], y) pytest.raises(ValueError, rf.fit, X[:, 0], y) pytest.raises(ValueError, rf.fit, X[..., np.newaxis], np.tile(y[..., np.newaxis], [2, 1, 1])) # stim features must match length of input data pytest.raises(ValueError, rf.fit, X[:, :1], y) # auto-naming features feature_names = ['feature_%s' % ii for ii in [0, 1, 2]] rf = ReceptiveField(tmin, tmax, 1, estimator=mod, feature_names=feature_names) assert_equal(rf.feature_names, feature_names) rf = ReceptiveField(tmin, tmax, 1, estimator=mod) rf.fit(X, y) assert_equal(rf.feature_names, None) # X/y same n timepoints pytest.raises(ValueError, rf.fit, X, y[:-2]) # Float becomes ridge rf = ReceptiveField(tmin, tmax, 1, ['one', 'two', 'three'], estimator=0, patterns=True) str(rf) # repr works before fit rf.fit(X, y) assert isinstance(rf.estimator_, TimeDelayingRidge) str(rf) # repr works after fit rf = ReceptiveField(tmin, tmax, 1, ['one'], estimator=0, patterns=True) rf.fit(X[:, [0]], y) str(rf) # repr with one feature # Should only accept estimators or floats rf = ReceptiveField(tmin, tmax, 1, estimator='foo', patterns=True) pytest.raises(ValueError, rf.fit, X, y) rf = ReceptiveField(tmin, tmax, 1, estimator=np.array([1, 2, 3])) pytest.raises(ValueError, rf.fit, X, y) # tmin must be <= tmax rf = ReceptiveField(5, 4, 1, patterns=True) pytest.raises(ValueError, rf.fit, X, y) # scorers for key, val in _SCORERS.items(): rf = ReceptiveField(tmin, tmax, 1, ['one'], estimator=0, scoring=key, patterns=True) rf.fit(X[:, [0]], y) y_pred = rf.predict(X[:, [0]]).T.ravel()[:, np.newaxis] assert_allclose(val(y[:, np.newaxis], y_pred, multioutput='raw_values'), rf.score(X[:, [0]], y), rtol=1e-2) # Need 2D input pytest.raises(ValueError, _SCORERS['corrcoef'], y.ravel(), y_pred, multioutput='raw_values') # Need correct scorers rf = ReceptiveField(tmin, tmax, 1., scoring='foo') pytest.raises(ValueError, rf.fit, X, y)
def test_receptive_field_basic(n_jobs): """Test model prep and fitting.""" from sklearn.linear_model import Ridge # Make sure estimator pulling works mod = Ridge() rng = np.random.RandomState(1337) # Test the receptive field model # Define parameters for the model and simulate inputs + weights tmin, tmax = -10., 0 n_feats = 3 rng = np.random.RandomState(0) X = rng.randn(10000, n_feats) w = rng.randn(int((tmax - tmin) + 1) * n_feats) # Delay inputs and cut off first 4 values since they'll be cut in the fit X_del = np.concatenate( _delay_time_series(X, tmin, tmax, 1.).transpose(2, 0, 1), axis=1) y = np.dot(X_del, w) # Fit the model and test values feature_names = ['feature_%i' % ii for ii in [0, 1, 2]] rf = ReceptiveField(tmin, tmax, 1, feature_names, estimator=mod, patterns=True) rf.fit(X, y) assert_array_equal(rf.delays_, np.arange(tmin, tmax + 1)) y_pred = rf.predict(X) assert_allclose(y[rf.valid_samples_], y_pred[rf.valid_samples_], atol=1e-2) scores = rf.score(X, y) assert scores > .99 assert_allclose(rf.coef_.T.ravel(), w, atol=1e-3) # Make sure different input shapes work rf.fit(X[:, np.newaxis:], y[:, np.newaxis]) rf.fit(X, y[:, np.newaxis]) with pytest.raises(ValueError, match='If X has 3 .* y must have 2 or 3'): rf.fit(X[..., np.newaxis], y) with pytest.raises(ValueError, match='X must be shape'): rf.fit(X[:, 0], y) with pytest.raises(ValueError, match='X and y do not have the same n_epo'): rf.fit(X[:, np.newaxis], np.tile(y[:, np.newaxis, np.newaxis], [1, 2, 1])) with pytest.raises(ValueError, match='X and y do not have the same n_tim'): rf.fit(X, y[:-2]) with pytest.raises(ValueError, match='n_features in X does not match'): rf.fit(X[:, :1], y) # auto-naming features feature_names = ['feature_%s' % ii for ii in [0, 1, 2]] rf = ReceptiveField(tmin, tmax, 1, estimator=mod, feature_names=feature_names) assert_equal(rf.feature_names, feature_names) rf = ReceptiveField(tmin, tmax, 1, estimator=mod) rf.fit(X, y) assert_equal(rf.feature_names, None) # Float becomes ridge rf = ReceptiveField(tmin, tmax, 1, ['one', 'two', 'three'], estimator=0) str(rf) # repr works before fit rf.fit(X, y) assert isinstance(rf.estimator_, TimeDelayingRidge) str(rf) # repr works after fit rf = ReceptiveField(tmin, tmax, 1, ['one'], estimator=0) rf.fit(X[:, [0]], y) str(rf) # repr with one feature # Should only accept estimators or floats with pytest.raises(ValueError, match='`estimator` must be a float or'): ReceptiveField(tmin, tmax, 1, estimator='foo').fit(X, y) with pytest.raises(ValueError, match='`estimator` must be a float or'): ReceptiveField(tmin, tmax, 1, estimator=np.array([1, 2, 3])).fit(X, y) with pytest.raises(ValueError, match='tmin .* must be at most tmax'): ReceptiveField(5, 4, 1).fit(X, y) # scorers for key, val in _SCORERS.items(): rf = ReceptiveField(tmin, tmax, 1, ['one'], estimator=0, scoring=key, patterns=True) rf.fit(X[:, [0]], y) y_pred = rf.predict(X[:, [0]]).T.ravel()[:, np.newaxis] assert_allclose(val(y[:, np.newaxis], y_pred, multioutput='raw_values'), rf.score(X[:, [0]], y), rtol=1e-2) with pytest.raises(ValueError, match='inputs must be shape'): _SCORERS['corrcoef'](y.ravel(), y_pred, multioutput='raw_values') # Need correct scorers with pytest.raises(ValueError, match='scoring must be one of'): ReceptiveField(tmin, tmax, 1., scoring='foo').fit(X, y)