def br_caller(df, method, params, expected, expected_baseline): df = norm(df, [[580, 600]]) result, result_baseline = remove_baseline(df, method, params=params) np.testing.assert_array_almost_equal(expected, np.array(result['wvl'].iloc[5, 0:5])) np.testing.assert_array_almost_equal( expected_baseline, np.array(result_baseline['wvl'].iloc[5, 0:5]))
def test_norm(): df = pd.read_csv(get_path('test_data.csv'), header=[0, 1]) result = norm.norm(df, [[580, 590], [590, 600]], col_var='wvl') np.testing.assert_almost_equal(result['wvl'].iloc[0, :].sum(), 2.0)
import numpy as np import pandas as pd from libpyhat.examples import get_path from libpyhat.transform.norm import norm from libpyhat.regression.regression import regression np.random.seed(1) df = pd.read_csv(get_path('test_data.csv'), header=[0, 1]) df = norm(df, [[580, 600]]) x = df['wvl'] y = df[('comp', 'SiO2')] def test_PLS(): regress = regression(method=['PLS'], params=[{ 'n_components': 3, 'scale': False }]) regress.fit(x, y) prediction = np.squeeze(regress.predict(x)) rmse = np.sqrt(np.average((prediction - y)**2)) expected = 9.568890617713505 np.testing.assert_almost_equal(rmse, expected) regress.calc_Qres_Lev(x) Qres_expected = [ 0.04055878, 0.04188589, 0.04159104, 0.04374264, 0.04080776, 0.04072383, 0.04057845, 0.04053754, 0.04056575, 0.04077855 ] np.testing.assert_array_almost_equal(regress.Q_res[0:10], Qres_expected)
def norm(self, ranges, col_var): self.df = norm.norm(self.df, ranges, col_var=col_var)
import numpy as np import pandas as pd from libpyhat.examples import get_path import libpyhat.clustering.cluster as cluster import libpyhat.transform.norm as norm np.random.seed(1) df = pd.read_csv(get_path('test_data.csv'), header=[0, 1]) df = norm.norm(df, [[585, 600]]) def test_spectral(): kws = { 'n_clusters': 3, 'n_init': 10, 'affinity': 'rbf', 'gamma': 1.0, 'n_neighbors': 5, 'degree': 3, 'coef0': 1, 'n_jobs': 1, 'random_state': 1 } result = cluster.cluster(df, 'wvl', 'Spectral', [], kws) clusters = np.squeeze(np.array(result['Spectral'])) cluster_count = [ np.count_nonzero(clusters == 1), np.count_nonzero(clusters == 2), np.count_nonzero(clusters == 3) ] cluster_count.sort() assert cluster_count == [8, 32, 63]
import numpy as np import pandas as pd from libpyhat.examples import get_path from libpyhat.transform import cal_tran, norm from libpyhat.transform.caltran_utils import prepare_data np.random.seed(1) data1 = pd.read_csv(get_path('caltran_test1.csv'),header=[0,1]) data2 = pd.read_csv(get_path('caltran_test2.csv'),header=[0,1]) data1, data2 = prepare_data(data1,data2,'Target','Target') #normalize the data for numerical stability data1 = norm.norm(data1,[[240,250]]) data2 = norm.norm(data2,[[240,250]]) def cal_tran_helper(data1,data2,params, expected, single_spect = False): ct = cal_tran.cal_tran(params) ct.derive_transform(data1['wvl'], data2['wvl']) if single_spect: result = ct.apply_transform(data1['wvl'].iloc[0,:]) else: result = ct.apply_transform(data1['wvl']) if len(result.shape)>1: np.testing.assert_array_almost_equal(np.array(result,dtype=float)[:, 4], expected) else: np.testing.assert_array_almost_equal(np.array(result,dtype=float)[4], expected) def test_no_transform(): params = {'method':'None'} ct = cal_tran.cal_tran(params)