def test_pls_load_plot_without_labels(): pls = nipals.PLS(oliveoil_missing_y.iloc[:, :5], oliveoil_missing_y.iloc[:, 5:]) assert pls.fit(ncomp=2) plt = pls.loadingsplot(labels=False) assert isinstance(plt, matplotlib.figure.Figure) return plt
def test_cv_pls(): pls = nipals.PLS(oliveoil.iloc[:, :5], oliveoil.iloc[:, 5:]) assert pls.fit(ncomp=2, cv=True) assert pls.fit(ncomp=2, cv=4) np.testing.assert_almost_equal(pls.Q2, [0.2684853, 0.0644187]) np.testing.assert_almost_equal(pls.R2X, [0.5826444, 0.2367458]) np.testing.assert_almost_equal(pls.R2Y, [0.4326835, 0.0856207])
def test_pls_load_plot_with_markers(): pls = nipals.PLS(oliveoil_missing_y.iloc[:, :5], oliveoil_missing_y.iloc[:, 5:]) assert pls.fit(ncomp=2) plt = pls.loadingsplot(weightmarkers=['s', 'o', 'v', '^', '+', '3']) assert isinstance(plt, matplotlib.figure.Figure) return plt
def test_pls_nocenter_and_scale(): pls = nipals.PLS(yarn.iloc[:, :268] - yarn.iloc[:, :268].mean(), yarn.iloc[:, 268]) assert pls.fit(ncomp=6, scale=False, center=False) np.testing.assert_almost_equal(pls.scores.values, yarn_scores) np.testing.assert_almost_equal(pls.loadings.values, yarn_loadings) np.testing.assert_almost_equal(pls.weights.values, yarn_weights)
def test_pls_score_plot(): pls = nipals.PLS(oliveoil_missing_y.iloc[:, :5], oliveoil_missing_y.iloc[:, 5:]) assert pls.fit(ncomp=2) plt = pls.plot() assert isinstance(plt, matplotlib.figure.Figure) return plt
def test_pls_zerovar_y(): tmpy = oliveoil.iloc[:, 5:].copy() tmpy.loc[:, 'extra'] = 2.0 pls = nipals.PLS(oliveoil.iloc[:, :5], tmpy) with pytest.raises(ValueError) as e_info: pls.fit(2) assert e_info.match("zero variance in column.*extra") assert pls.fit(2, dropzerovar=True)
def test_pls_optionvariations(caplog): pls = nipals.PLS(yarn.iloc[:, :268], yarn.iloc[:, 268]) assert pls.fit() assert pls.fit(ncomp=500, startcol=0) assert caplog.record_tuples == [ ('root', logging.WARNING, 'ncomp is larger than the max dimension of the x matrix.\n' 'fit will only return 28 components'), ] with pytest.raises(RuntimeError): pls.fit(maxiter=1)
def test_pls_missing_x(): tmpx = yarn.iloc[:, :268] csel = [171, 107, 222, 150, 76, 205, 63, 19, 121, 183] rsel = [23, 0, 3, 22, 15, 21, 19, 7, 19, 5] for r, c in zip(rsel, csel): tmpx.iloc[r, c] = np.nan pls = nipals.PLS(tmpx, yarn.iloc[:, 268]) assert pls.fit(ncomp=3) np.testing.assert_almost_equal(pls.scores.values, yarn_missing_scores, 5) np.testing.assert_almost_equal(pls.loadings.values, yarn_missing_loadings) np.testing.assert_almost_equal(pls.weights.values, yarn_missing_weights)
def test_inf_values_in_dfs(caplog): olive_inf = oliveoil.replace([18.7, 75.1], np.inf) nipals.Nipals(olive_inf) nipals.PLS(olive_inf.iloc[:, :5], olive_inf.iloc[:, 5:]) assert caplog.record_tuples == [ ('root', logging.WARNING, 'Data contained infinite values, converting to missing values'), ('root', logging.WARNING, 'X data contained infinite values, converting to missing values'), ('root', logging.WARNING, 'Y data contained infinite values, converting to missing values'), ]
def test_dmod_pls(): pls = nipals.PLS(oliveoil.iloc[:, :5], oliveoil.iloc[:, 5:]) assert pls.fit(ncomp=2) np.testing.assert_almost_equal(pls.dModX(), [ 0.8561775, 0.6808639, 1.2475466, 1.9522224, 0.1694258, 0.6151471, 0.8651022, 1.1295028, 1.2212977, 1.2945167, 0.8681262, 1.141947, 0.7637504, 0.3900809, 0.7382779, 0.7063927 ]) np.testing.assert_almost_equal(pls.dModY(), [ 0.5728738, 2.0598601, 1.2420525, 0.5257193, 1.4290988, 0.7752674, 1.0029673, 0.8943648, 1.0509669, 0.8511583, 0.7153933, 1.0285289, 0.8238886, 0.4616424, 0.5664762, 0.7409856 ])
def test_overviewplotplot_pls(): pls = nipals.PLS(oliveoil.iloc[:, :5], oliveoil.iloc[:, 5:]) assert pls.fit(ncomp=2) plt = pls.overviewplot() assert isinstance(plt, matplotlib.figure.Figure) return plt
def test_pls_missing_y(): pls = nipals.PLS(oliveoil_missing_y.iloc[:, :5], oliveoil_missing_y.iloc[:, 5:]) assert pls.fit(ncomp=2) np.testing.assert_almost_equal(pls.scores.values, oliveoil_missing_y_scores * [-1, 1], 3)
def test_pls_nondf(): pls = nipals.PLS(yarn.iloc[:, :268].values, yarn.iloc[:, 268].values) assert pls.fit(ncomp=6, scale=False) np.testing.assert_almost_equal(pls.scores.values, yarn_scores) np.testing.assert_almost_equal(pls.loadings.values, yarn_loadings) np.testing.assert_almost_equal(pls.weights.values, yarn_weights)