Newer
Older
from juliacall import Main as jl
import numpy as np
import pandas as pd
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
class LWPLSR:
"""The lwpls regression model from Jchemo (M. Lesnoff)
Returns:
self.scores (DataFrame): various metrics and scores
self.predicted_results (Dictionary): Dict containing all predicted results (train, test, cross-validation)
self.mod (Julia model): the prepared model
"""
def __init__(self, dataset, preT):
"""Initiate the LWPLSR and prepare data for Julia computing."""
# get train / test data from dataset
self.x_train, self.y_train, self.x_test, self.y_test = [dataset[i] for i in range(4)]
# calculate number of KFolds and get CV data from dataset
self.nb_fold = int((len(dataset)-4)/4)
for i in range(self.nb_fold):
setattr(self, "xtr_fold"+str(i+1), dataset[i+7])
setattr(self, "ytr_fold"+str(i+1), dataset[i+13])
setattr(self, "xte_fold"+str(i+1), dataset[i+4])
setattr(jl, "xtr_fold"+str(i+1), dataset[i+7])
setattr(jl, "ytr_fold"+str(i+1), dataset[i+13])
setattr(jl, "xte_fold"+str(i+1), dataset[i+4])
# prepare to send dataframes to julia and Jchemo (with the jl. prefix)
jl.x_train, jl.y_train, jl.x_test, jl.y_test = self.x_train, self.y_train, self.x_test, self.y_test
# Get parameters for preTreatment of the spectra (acquired from a global PLSR)
self.preT = preT
# initialize vars from the class
y_shape = self.y_test.shape
self.pred_test = np.zeros(shape=(y_shape[0], 1))
self.pred_train = np.zeros(shape=(y_shape[0], 1))
self.mod = ""
self.best_lwplsr_params = np.zeros(shape=(5, 1))
self.predicted_results = {}
def Jchemo_lwplsr_fit(self):
"""Send data to Julia to fit lwplsr.
Args:
self.x_train (DataFrame):
self.y_train (DataFrame):
self.x_test (DataFrame):
self.y_test (DataFrame):
Returns:
self.mod (Julia model): the prepared model
"""
# launch Julia Jchemo lwplsr and convert DataFrames from Python Pandas DataFrame to Julia DataFrame
jl.seval("""
using DataFrames
using Pandas
using Jchemo
x_train |> Pandas.DataFrame |> DataFrames.DataFrame
y_train |> Pandas.DataFrame |> DataFrames.DataFrame
x_test |> Pandas.DataFrame |> DataFrames.DataFrame
y_test |> Pandas.DataFrame |> DataFrames.DataFrame
""")
# apply pre-treatments on X data
print('LWPLSR - preTreatment')
# apply pre-treatments to X data before working with
jl.npoint = self.preT['window_length']
jl.deriv = self.preT['deriv']
jl.degree = self.preT['polyorder']
if self.preT['polyorder'] > 0:
jl.seval("""
mod1 = model(snv; centr = true, scal = true)
mod2 = model(savgol; npoint = npoint, deriv = deriv, degree = degree)
""")
if self.preT['normalization'] == "No_transformation":
jl.seval("""
preMod = mod2
""")
elif self.preT['normalization'] == 'Snv':
jl.seval("""
preMod = pip(mod1, mod2)
""")
jl.seval("""
fit!(preMod, x_train)
x_train = transf(preMod, x_train)
x_test = transf(preMod, x_test)
""")
# LWPLSR tuning
print('LWPLSR - tuning')
# set tuning parameters to test
jl.seval("""
nlvdis = [5; 10; 15] ; metric = [:eucl; :mah]
h = [1; 2; 6; Inf] ; k = [30; 80; 200]
nlv = 5:15
pars = Jchemo.mpar(nlvdis = nlvdis, metric = metric, h = h, k = k)
""")
# split Train data into Cal/Val for tuning
jl.seval("""
pct = .3
ntrain = Jchemo.nro(x_train)
nval = Int(round(pct * ntrain))
s = Jchemo.samprand(ntrain, nval)
Xcal = x_train[s.train, :]
ycal = y_train[s.train]
Xval = x_train[s.test, :]
yval = y_train[s.test]
ncal = ntrain - nval
""")
# Create LWPLSR model and tune with GridScore
jl.seval("""
mod = Jchemo.model(Jchemo.lwplsr)
res = gridscore(mod, Xcal, ycal, Xval, yval; score = Jchemo.rmsep, pars, nlv, verbose = false)
u = findall(res.y1 .== minimum(res.y1))[1] #best parameters combination
""")
# save best lwplsr parameters
self.best_lwplsr_params = {'nlvdis' : jl.res.nlvdis[jl.u], 'metric' : str(jl.res.metric[jl.u]), 'h' : jl.res.h[jl.u], 'k' : jl.res.k[jl.u], 'nlv' : jl.res.nlv[jl.u]}
print('best lwplsr params ' + str(self.best_lwplsr_params))
# run LWPLSR model with best parameters
jl.seval("""
mod = Jchemo.model(Jchemo.lwplsr; nlvdis = res.nlvdis[u], metric = res.metric[u], h = res.h[u], k = res.k[u], nlv = res.nlv[u])
# Fit model
Jchemo.fit!(mod, x_train, y_train)
""")
# save Julia Jchemo model
self.mod = jl.mod
def Jchemo_lwplsr_predict(self):
"""Send data to Julia to predict with lwplsr.
Args:
self.mod (Julia model): the prepared model
self.x_train (DataFrame):
self.y_train (DataFrame):
self.x_test (DataFrame):
self.y_test (DataFrame):
Returns:
self.pred_test (Julia DataFrame): predicted values on x_test
self.pred_train (Julia DataFrame): predicted values on x_train
"""
# Predictions on x_test and store in self.pred
self.pred_test = jl.seval("""
println("LWPLSR - start test predict")
res = Jchemo.predict(mod, x_test)
res.pred
""")
self.pred_train = jl.seval("""
println("LWPLSR - start train predict")
res = Jchemo.predict(mod, x_train)
res.pred
""")
print('LWPLSR - end')
def Jchemo_lwplsr_cv(self):
"""Send Cross-Validation data to Julia to fit & predict with lwplsr.
Args:
self.best_lwplsr_params: the best parameters to use (from tuning) for CV
self.xtr_fold1 (DataFrame):
self.ytr_fold1 (DataFrame):
self.xte_fold1 (DataFrame):
Returns:
self.pred_cv (Julia DataFrame): predicted values on x_train with Cross-Validation
"""
for i in range(self.nb_fold):
jl.Xtr = getattr(self, "xtr_fold"+str(i+1))
jl.Ytr = getattr(self, "ytr_fold"+str(i+1))
jl.Xte = getattr(self, "xte_fold"+str(i+1))
# convert Python Pandas DataFrame to Julia DataFrame
jl.seval("""
using DataFrames
using Pandas
using Jchemo
Xtr |> Pandas.DataFrame |> DataFrames.DataFrame
Ytr |> Pandas.DataFrame |> DataFrames.DataFrame
Xte |> Pandas.DataFrame |> DataFrames.DataFrame
""")
# set lwplsr parameters as the best one from tuning
jl.nlvdis = int(self.best_lwplsr_params['nlvdis'])
jl.metric = self.best_lwplsr_params['metric']
jl.h = self.best_lwplsr_params['h']
jl.k = int(self.best_lwplsr_params['k'])
jl.nlv = int(self.best_lwplsr_params['nlv'])
jl.seval("""
println("LWPLSR - start CV mod")
mod_cv = Jchemo.model(Jchemo.lwplsr; nlvdis = nlvdis, metric = Symbol(metric), h = h, k = k, nlv = nlv)
# Fit model
Jchemo.fit!(mod_cv, Xtr, Ytr)
""")
pred_cv = jl.seval("""
println("LWPLSR - start CV predict")
res = Jchemo.predict(mod_cv, Xte)
res.pred
""")
# save predicted values for each KFold in the predicted_results dictionary
@property
def pred_data_(self):
# convert predicted data from x_test to Pandas DataFrame
self.predicted_results["pred_data_train"] = DataFrame(self.pred_train)
self.predicted_results["pred_data_test"] = DataFrame(self.pred_test)