Skip to content

Commit aa881c0

Browse files
author
Jordan Stomps
committed
adding pytest for cotraining
1 parent 70a0f88 commit aa881c0

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

tests/test_models.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# diagnostics
2+
import numpy as np
3+
from datetime import datetime, timedelta
4+
# testing models
5+
from sklearn.model_selection import train_test_split
6+
from sklearn.preprocessing import StandardScaler
7+
import tests.test_data as test_data
8+
# hyperopt
9+
from hyperopt.pyll.base import scope
10+
from hyperopt import hp
11+
# models
12+
from models.SSML.CoTraining import CoTraining
13+
# testing write
14+
import joblib
15+
import os
16+
17+
# initialize sample data
18+
start_date = datetime(2019, 2, 2)
19+
delta = timedelta(seconds=1)
20+
timestamps = np.arange(start_date,
21+
start_date + (test_data.timesteps * delta),
22+
delta).astype('datetime64[s]').astype('float64')
23+
24+
live = np.full((len(timestamps),), test_data.livetime)
25+
sample_val = 1.0
26+
spectra = np.full((len(timestamps), test_data.energy_bins),
27+
np.full((1, test_data.energy_bins), sample_val))
28+
# setting up for rejected null hypothesis
29+
rejected_H0_time = np.random.choice(spectra.shape[0],
30+
test_data.timesteps//2,
31+
replace=False)
32+
spectra[rejected_H0_time] = 100.0
33+
34+
labels = np.full((spectra.shape[0],), 0)
35+
labels[rejected_H0_time] = 1
36+
37+
38+
def test_CoTraining():
39+
# test saving model input parameters
40+
params = {'max_iter': 2022, 'tol': 0.5, 'C': 5.0}
41+
model = CoTraining(params=params)
42+
43+
assert model.model1.max_iter == params['max_iter']
44+
assert model.model1.tol == params['tol']
45+
assert model.model1.C == params['C']
46+
47+
assert model.model2.max_iter == params['max_iter']
48+
assert model.model2.tol == params['tol']
49+
assert model.model2.C == params['C']
50+
51+
X, Ux, y, Uy = train_test_split(spectra,
52+
labels,
53+
test_size=0.5,
54+
random_state=0)
55+
X_train, X_test, y_train, y_test = train_test_split(X,
56+
y,
57+
test_size=0.2,
58+
random_state=0)
59+
60+
# normalization
61+
normalizer = StandardScaler()
62+
normalizer.fit(X_train)
63+
64+
X_train = normalizer.transform(X_train)
65+
X_test = normalizer.transform(X_test)
66+
Ux = normalizer.transform(Ux)
67+
68+
# default behavior
69+
model = CoTraining(params=None, random_state=0)
70+
model.train(X_train, y_train, Ux)
71+
72+
# testing train and predict methods
73+
pred, acc, *_ = model.predict(X_test, y_test)
74+
75+
assert acc > 0.7
76+
np.testing.assert_equal(pred, y_test)
77+
78+
# testing hyperopt optimize methods
79+
space = {'max_iter': scope.int(hp.quniform('max_iter',
80+
10,
81+
10000,
82+
10)),
83+
'tol': hp.loguniform('tol', 1e-5, 1e-3),
84+
'C': hp.uniform('C', 1.0, 1000.0),
85+
'n_samples': scope.int(hp.quniform('n_samples',
86+
1,
87+
20,
88+
1)),
89+
'seed': 0
90+
}
91+
data_dict = {'trainx': X_train,
92+
'testx': X_test,
93+
'trainy': y_train,
94+
'testy': y_test,
95+
'Ux': Ux
96+
}
97+
model.optimize(space, data_dict, max_evals=2, verbose=True)
98+
99+
assert model.best['accuracy'] >= model.worst['accuracy']
100+
assert model.best['status'] == 'ok'
101+
102+
# testing model plotting method
103+
filename = 'test_plot'
104+
model.plot_cotraining(model1_accs=model.best['model1_acc_history'],
105+
model2_accs=model.best['model2_acc_history'],
106+
filename=filename)
107+
os.remove(filename+'.png')
108+
109+
# testing model write to file method
110+
filename = 'test_LogReg'
111+
ext = '.joblib'
112+
model.save(filename)
113+
model_file = joblib.load(filename+ext)
114+
assert model_file.best['params'] == model.best['params']
115+
116+
os.remove(filename+ext)

0 commit comments

Comments
 (0)