nimo 0.0.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
nimo-0.0.2/PKG-INFO ADDED
@@ -0,0 +1,12 @@
1
+ Metadata-Version: 2.1
2
+ Name: nimo
3
+ Version: 0.0.2
4
+ Summary: NIMO package
5
+ Author: NIMO developers
6
+ License: MIT
7
+ Requires-Dist: Cython
8
+ Requires-Dist: matplotlib
9
+ Requires-Dist: numpy
10
+ Requires-Dist: physbo>=2.0.0
11
+ Requires-Dist: scikit-learn
12
+ Requires-Dist: scipy
@@ -0,0 +1,27 @@
1
+ import os
2
+ import sys
3
+ sys.path.append(os.path.dirname(os.path.abspath(__file__)))
4
+
5
+ from .nimo_modules import selection
6
+ from .nimo_modules import preparation_input
7
+ from .nimo_modules import analysis_output
8
+ from .nimo_modules import history
9
+ from .nimo_modules import analysis
10
+
11
+ from .ai_tools import ai_tool_re
12
+ from .ai_tools import ai_tool_physbo
13
+ from .ai_tools import ai_tool_blox
14
+ from .ai_tools import ai_tool_pdc
15
+ from .ai_tools import ai_tool_ptr
16
+ from .ai_tools import ai_tool_slesa
17
+ from .ai_tools import ai_tool_slesa_WAM
18
+
19
+ from .input_tools import preparation_input_standard
20
+ from .input_tools import preparation_input_naree
21
+
22
+ from .output_tools import analysis_output_standard
23
+ from .output_tools import analysis_output_naree
24
+
25
+ from .visualization import plot_history
26
+ from .visualization import plot_phase_diagram
27
+ from .visualization import plot_distribution
@@ -0,0 +1,316 @@
1
+ import numpy as np
2
+ import random
3
+ import copy
4
+ import csv
5
+
6
+ from sklearn.ensemble import RandomForestRegressor
7
+ from sklearn.preprocessing import StandardScaler, MinMaxScaler
8
+ from sklearn.model_selection import GridSearchCV
9
+ from sklearn.preprocessing import MinMaxScaler
10
+
11
+
12
+ class BLOX():
13
+ """Class of BLOX
14
+
15
+ This class can select the next candidates by random exploration.
16
+
17
+ """
18
+
19
+ def __init__(self, input_file, output_file, num_objectives, num_proposals, output_res):
20
+ """Constructor
21
+
22
+ This function do not depend on robot.
23
+
24
+ Args:
25
+ input_file (str): the file for candidates for MI algorithm
26
+ output_file (str): the file for proposals from MI algorithm
27
+ num_objectives (int): the number of objectives
28
+ num_proposals (int): the number of proposals
29
+
30
+ """
31
+
32
+ self.input_file = input_file
33
+ self.output_file = output_file
34
+ self.num_objectives = num_objectives
35
+ self.num_proposals = num_proposals
36
+
37
+ self.output_res = output_res
38
+
39
+
40
+ def load_data(self):
41
+ """Loading candidates
42
+
43
+ This function do not depend on robot.
44
+
45
+ Returns:
46
+ t_train (list[float]): the list where observed objectives are stored
47
+ X_all (list[float]): the list where all descriptors are stored
48
+ train_actions (list[float]): the list where observed actions are stored
49
+ test_actions (list[float]): the list where test actions are stored
50
+
51
+ """
52
+
53
+ arr = np.genfromtxt(self.input_file, skip_header=1, delimiter=',')
54
+
55
+ arr_train = arr[~np.isnan(arr[:, - 1]), :]
56
+ arr_test = arr[np.isnan(arr[:, - 1]), :]
57
+
58
+
59
+ X_train = arr_train[:, : - self.num_objectives]
60
+ t_train = arr_train[:, - self.num_objectives:]
61
+
62
+ X_test = arr_test[:, : - self.num_objectives]
63
+
64
+ test_actions = np.where(np.isnan(arr[:, -1]))[0].tolist()
65
+
66
+ X_all=arr[:, : - self.num_objectives]
67
+
68
+ all_actions = [i for i in range(len(X_all))]
69
+
70
+ train_actions = np.sort(list(set(all_actions) - set(test_actions)))
71
+
72
+ return t_train, X_all, train_actions, test_actions
73
+
74
+
75
+
76
+ def calc_ai(self, t_train, X_all, train_actions, test_actions):
77
+ """Calculating the proposals by AI algorithm
78
+
79
+ This function is for BLOX.
80
+ This function do not depend on robot.
81
+ If the new AI alborithm is developed, this function is only changed.
82
+
83
+ Args:
84
+ t_train (list[float]): the list where observed objectives are stored
85
+ X_all (list[float]): the list where all descriptors are stored
86
+ train_actions (list[float]): the list where observed actions are stored
87
+ test_actions (list[float]): the list where test actions are stored
88
+
89
+ Returns:
90
+ actions (list[int]): the list where the selected actions are stored
91
+
92
+ """
93
+
94
+ prediction_model = "RF"
95
+
96
+ #train prediction models
97
+ def build_model(prediction_model, x_train, y_train):
98
+ """Building prediction model
99
+
100
+ This function do not depend on robot.
101
+
102
+ Args:
103
+ prediction_model (str): "RF"
104
+ x_train (list[float]): the list of training features
105
+ y_train (list[float]): the list of training labels
106
+
107
+ Returns:
108
+ model: the prediction model
109
+
110
+ """
111
+
112
+ if prediction_model == 'RF':
113
+ params = {'n_estimators':[10, 50, 100]}
114
+ gridsearch = GridSearchCV(RandomForestRegressor(), param_grid = params, cv = 3, scoring = "r2", n_jobs = 1, verbose = 1)
115
+ gridsearch.fit(x_train,y_train)
116
+ model = RandomForestRegressor(n_estimators = gridsearch.best_params_['n_estimators'])
117
+ model.fit(x_train, y_train)
118
+ return model
119
+
120
+
121
+ #stein novelty calculation
122
+ def hesgau(x, y, sigma):
123
+ """Calculating hesgau
124
+
125
+ This function do not depend on robot.
126
+
127
+ Args:
128
+ x (list[float]): the list of target 1
129
+ y (list[float]): the list of target 2
130
+ sigma (int): the variance of Gaussian distribution
131
+
132
+ Returns:
133
+ the value of hesgau
134
+
135
+ """
136
+
137
+ dim = len(x)
138
+ dist = np.sum(np.power(x - y, 2))
139
+ return (dim / sigma - dist / sigma**2) * np.exp(- dist / (2*sigma))
140
+
141
+
142
+ def stein_novelty(point, data_list, sigma):
143
+ """Calculating stein_novelty
144
+
145
+ This function do not depend on robot.
146
+
147
+ Args:
148
+ point (list[float]): the list of testing data
149
+ data_list (list[float]): the list of training data
150
+ sigma (int): the variance of Gaussian distribution
151
+
152
+ Returns:
153
+ the score of stein novelty
154
+
155
+ """
156
+
157
+ n = len(data_list)
158
+ score = 0
159
+ score = np.sum([hesgau(point, data_list[k,:], sigma) for k in range(n)])
160
+ score = score / (n * (n + 1) /2)
161
+ return - score
162
+
163
+
164
+
165
+ #Preparation of data
166
+ features_observed = X_all[train_actions]
167
+ features_unchecked = X_all[test_actions]
168
+ properties_observed = t_train
169
+
170
+
171
+ sc = StandardScaler()
172
+ sc.fit(features_observed)
173
+ sc_features_observed = sc.transform(features_observed)
174
+ sc_features_unchecked = sc.transform(features_unchecked)
175
+
176
+ sc_property = StandardScaler()
177
+ sc_property.fit(properties_observed)
178
+
179
+ dimension = self.num_objectives
180
+
181
+
182
+ actions = []
183
+
184
+
185
+ for i in range(self.num_proposals):
186
+
187
+ sc_properties_observed = sc_property.transform(properties_observed)
188
+
189
+ #Build prediction models
190
+ model_list = []
191
+ for d in range(dimension):
192
+ model = build_model(prediction_model, sc_features_observed, properties_observed[:,d])
193
+ model_list.append(model)
194
+
195
+
196
+ #Predict properties of unchecked data
197
+ predicted_properties_list = []
198
+ for d in range(dimension):
199
+ predicted_properties_list.append(model_list[d].predict(sc_features_unchecked))
200
+ predicted_properties_list = np.array(predicted_properties_list).T
201
+
202
+
203
+ #Calc. Stein Novelty
204
+ sc_predicted_properties_list = sc_property.transform(predicted_properties_list)
205
+ sn_data = [stein_novelty(point, sc_properties_observed, sigma=0.1) for point in sc_predicted_properties_list]
206
+
207
+
208
+ #Output prediction results
209
+ if i == 0 and self.output_res == True:
210
+
211
+ res_tot = []
212
+
213
+ f = open(self.input_file, 'r')
214
+ reader = csv.reader(f)
215
+ header = next(reader)
216
+
217
+ header.append('acquisition')
218
+
219
+ res_tot.append(header)
220
+
221
+ for ii in range(len(features_unchecked)):
222
+
223
+ res_each = []
224
+
225
+ for jj in range(len(features_unchecked[0])):
226
+ res_each.append(features_unchecked[ii][jj])
227
+
228
+ for jj in range(len(predicted_properties_list[0])):
229
+ res_each.append(predicted_properties_list[ii][jj])
230
+
231
+ res_each.append(sn_data[ii])
232
+
233
+ res_tot.append(res_each)
234
+
235
+
236
+ with open('output_res.csv', 'w') as f:
237
+ writer = csv.writer(f)
238
+ writer.writerows(res_tot)
239
+
240
+
241
+ #Select next candidates
242
+
243
+ rank_index = np.array(sn_data).argsort()[::-1]
244
+
245
+ actions.append(test_actions[rank_index[0]])
246
+
247
+
248
+ sc_features_observed = sc_features_observed.tolist()
249
+ properties_observed = properties_observed.tolist()
250
+
251
+ sc_features_observed.append(sc_features_unchecked[rank_index[0]])
252
+ properties_observed.append(predicted_properties_list[rank_index[0]])
253
+
254
+ sc_features_observed = np.array(sc_features_observed)
255
+ properties_observed = np.array(properties_observed)
256
+
257
+
258
+ test_actions.pop(rank_index[0])
259
+
260
+ sc_features_unchecked = np.delete(sc_features_unchecked, rank_index[0], axis = 0)
261
+
262
+ return actions
263
+
264
+
265
+
266
+ def select(self):
267
+ """Selecting the proposals by MI algorithm
268
+
269
+ This function do not depend on robot.
270
+
271
+ Returns:
272
+ True (str) for success.
273
+
274
+ """
275
+
276
+ print("Start selection of proposals by BLOX!")
277
+
278
+ t_train, X_all, train_actions, test_actions = self.load_data()
279
+
280
+ actions = self.calc_ai(t_train = t_train, X_all = X_all,
281
+ train_actions = train_actions, test_actions = test_actions)
282
+
283
+
284
+ print('Proposals')
285
+
286
+ proposals_all = []
287
+
288
+ input_data = open(self.input_file, 'r')
289
+ indexes = input_data.readlines()[0].rstrip('\n').split(',')
290
+
291
+ indexes = ["actions"] + indexes[0 : - self.num_objectives]
292
+
293
+ proposals_all.append(indexes)
294
+
295
+ for i in range(len(actions)):
296
+
297
+ row = [str(X_all[actions[i]][j]) for j in range(len(X_all[actions[i]]))]
298
+
299
+ row = [str(actions[i])] + row
300
+
301
+ proposals_all.append(row)
302
+
303
+ print("###")
304
+ print("number =", i+1)
305
+ print("actions = ", actions[i])
306
+ print("proposal = ", X_all[actions[i]])
307
+ print("###")
308
+
309
+
310
+ with open(self.output_file, 'w', newline="") as f:
311
+ writer = csv.writer(f)
312
+ writer.writerows(proposals_all)
313
+
314
+ print("Finish selection of proposals!")
315
+
316
+ return "True"
@@ -0,0 +1,291 @@
1
+ import numpy as np
2
+ import random
3
+ import copy
4
+ import csv
5
+
6
+ import sklearn.semi_supervised
7
+ from sklearn.preprocessing import StandardScaler
8
+ from scipy.spatial import distance
9
+
10
+
11
+ class PDC():
12
+ """Class of PDC
13
+
14
+ This class can select the next candidates by phase diagram construction.
15
+
16
+ """
17
+
18
+ def __init__(self, input_file, output_file, num_objectives, num_proposals):
19
+ """Constructor
20
+
21
+ This function do not depend on robot.
22
+
23
+ Args:
24
+ input_file (str): the file for candidates for MI algorithm
25
+ output_file (str): the file for proposals from MI algorithm
26
+ num_objectives (int): the number of objectives
27
+ num_proposals (int): the number of proposals
28
+
29
+ """
30
+
31
+ self.input_file = input_file
32
+ self.output_file = output_file
33
+ self.num_objectives = num_objectives
34
+ self.num_proposals = num_proposals
35
+
36
+
37
+ def load_data(self):
38
+ """Loading candidates
39
+
40
+ This function do not depend on robot.
41
+
42
+ Returns:
43
+ t_train (list[float]): the list where observed objectives are stored
44
+ X_all (list[float]): the list where all descriptors are stored
45
+ train_actions (list[float]): the list where observed actions are stored
46
+ test_actions (list[float]): the list where test actions are stored
47
+
48
+ """
49
+
50
+ arr = np.genfromtxt(self.input_file, skip_header=1, delimiter=',')
51
+
52
+ arr_train = arr[~np.isnan(arr[:, - 1]), :]
53
+ arr_test = arr[np.isnan(arr[:, - 1]), :]
54
+
55
+
56
+ X_train = arr_train[:, : - self.num_objectives]
57
+ t_train = arr_train[:, - self.num_objectives:]
58
+
59
+ X_test = arr_test[:, : - self.num_objectives]
60
+
61
+ test_actions = np.where(np.isnan(arr[:, -1]))[0].tolist()
62
+
63
+ X_all=arr[:, : - self.num_objectives]
64
+
65
+ all_actions = [i for i in range(len(X_all))]
66
+
67
+ train_actions = list(np.sort(list(set(all_actions) - set(test_actions))))
68
+
69
+ return t_train, X_all, train_actions, test_actions
70
+
71
+
72
+
73
+ def calc_ai(self, t_train, X_all, train_actions, test_actions):
74
+ """Calculating the proposals by AI algorithm
75
+
76
+ This function is for PDC.
77
+ This function do not depend on robot.
78
+ If the new AI alborithm is developed, this function is only changed.
79
+
80
+ Args:
81
+ t_train (list[float]): the list where observed objectives are stored
82
+ X_all (list[float]): the list where all descriptors are stored
83
+ train_actions (list[float]): the list where observed actions are stored
84
+ test_actions (list[float]): the list where test actions are stored
85
+
86
+ Returns:
87
+ actions (list[int]): the list where the selected actions are stored
88
+
89
+ """
90
+
91
+ #parameters
92
+ LP_algorithm = 'LP' #'LP', 'LS'
93
+ US_strategy = 'LC' #'LC' ,'MS', 'EA', 'RS'
94
+ multi_method = 'OU' #'OU', 'NE'
95
+ k = 2
96
+
97
+
98
+ #Preparation of data
99
+ data_list = np.array(X_all)
100
+
101
+ label_list = []
102
+ for i in range(len(data_list)):
103
+ if i in train_actions:
104
+ phase = int(t_train[train_actions.index(i)])
105
+ else:
106
+ phase = - 1
107
+ label_list.append(phase)
108
+
109
+
110
+ label_index_list = range(len(data_list))
111
+ labeled_index_list = train_actions
112
+ unlabeled_index_list = test_actions
113
+ dimension = len(data_list[0])
114
+
115
+
116
+ ss = StandardScaler()
117
+ ss.fit(data_list)
118
+ data_list_std = ss.transform(data_list)
119
+
120
+
121
+
122
+ #----SAMPLING
123
+ label_train = np.copy(label_list)
124
+
125
+ #estimate phase of each point
126
+ if LP_algorithm == 'LS':
127
+ #lp_model = label_propagation.LabelSpreading()
128
+ lp_model = sklearn.semi_supervised.LabelSpreading()
129
+ elif LP_algorithm == 'LP':
130
+ #lp_model = label_propagation.LabelPropagation()
131
+ lp_model = sklearn.semi_supervised.LabelPropagation()
132
+
133
+ lp_model.fit(data_list_std, label_train)
134
+ predicted_labels = lp_model.transduction_[unlabeled_index_list]
135
+ predicted_all_labels = lp_model.transduction_
136
+ label_distributions = lp_model.label_distributions_[unlabeled_index_list]
137
+ label_distributions_all = lp_model.label_distributions_
138
+ classes = lp_model.classes_
139
+
140
+
141
+
142
+ #calculate Uncertainly Score
143
+ if US_strategy == 'EA':
144
+ pred_entropies = stats.distributions.entropy(label_distributions.T)
145
+ u_score_list = pred_entropies/np.max(pred_entropies)
146
+
147
+ uncertainty_index = [unlabeled_index_list[np.argmax(u_score_list)]]
148
+
149
+ #############
150
+ # all ranking of uncertain point
151
+ ranking = np.array(u_score_list).argsort()[::-1]
152
+ multi_uncertainty_index = [unlabeled_index_list[ranking[i]] for i in range(len(unlabeled_index_list))]
153
+ #############
154
+
155
+ elif US_strategy == 'LC':
156
+ u_score_list = 1 - np.max(label_distributions, axis = 1)
157
+ uncertainty_index = [unlabeled_index_list[np.argmax(u_score_list)]]
158
+
159
+ #############
160
+ # all ranking of uncertain point
161
+ ranking = np.array(u_score_list).argsort()[::-1]
162
+ multi_uncertainty_index = [unlabeled_index_list[ranking[i]] for i in range(len(unlabeled_index_list))]
163
+ #############
164
+
165
+
166
+ elif US_strategy == 'MS':
167
+
168
+ u_score_list = []
169
+ for pro_dist in label_distributions:
170
+ pro_ordered = np.sort(pro_dist)[::-1]
171
+ margin = pro_ordered[0] - pro_ordered[1]
172
+ u_score_list.append(margin)
173
+
174
+ u_score_list = 1 - np.array(u_score_list)
175
+
176
+ uncertainty_index = [unlabeled_index_list[np.argmax(u_score_list)]]
177
+
178
+ #############
179
+ # all ranking of uncertain point
180
+ ranking = np.array(u_score_list).argsort()[::-1]
181
+ multi_uncertainty_index = [unlabeled_index_list[ranking[i]] for i in range(len(unlabeled_index_list))]
182
+ #############
183
+
184
+
185
+ if self.num_proposals == 1:
186
+
187
+ actions = [uncertainty_index[0]]
188
+
189
+
190
+ else:
191
+
192
+ #############
193
+ #multi
194
+
195
+ if multi_method == 'OU':
196
+
197
+ US_point = multi_uncertainty_index[0:self.num_proposals]
198
+
199
+ if multi_method == 'NE':
200
+
201
+ from scipy.spatial import distance
202
+
203
+ neighbor_dist = []
204
+
205
+ for i in range(len(data_list)):
206
+
207
+ dist_value = round(distance.euclidean(data_list[0],data_list[i]),5)
208
+
209
+ if dist_value not in neighbor_dist:
210
+ neighbor_dist.append(dist_value)
211
+
212
+ neighbor_dist.sort()
213
+
214
+ delta = neighbor_dist[k]
215
+
216
+ US_point = []
217
+
218
+ for i in range(len(multi_uncertainty_index)):
219
+
220
+ if i == 0: US_point.append(multi_uncertainty_index[i])
221
+
222
+ true_num = 0
223
+
224
+ for j in range(len(US_point)):
225
+
226
+ two_dist = distance.euclidean(data_list[US_point[j]], data_list[multi_uncertainty_index[i]])
227
+
228
+ if round(two_dist, 5) > delta:
229
+
230
+ true_num += 1
231
+
232
+ if true_num == len(US_point) and len(US_point) < self.num_proposals: US_point.append(multi_uncertainty_index[i])
233
+
234
+
235
+ actions = US_point
236
+
237
+ return actions
238
+
239
+
240
+
241
+ def select(self):
242
+ """Selecting the proposals by MI algorithm
243
+
244
+ This function do not depend on robot.
245
+
246
+ Returns:
247
+ True (str) for success.
248
+
249
+ """
250
+
251
+ print("Start selection of proposals by PDC!")
252
+
253
+ t_train, X_all, train_actions, test_actions = self.load_data()
254
+
255
+ actions = self.calc_ai(t_train = t_train, X_all = X_all,
256
+ train_actions = train_actions, test_actions = test_actions)
257
+
258
+
259
+ print('Proposals')
260
+
261
+ proposals_all = []
262
+
263
+ input_data = open(self.input_file, 'r')
264
+ indexes = input_data.readlines()[0].rstrip('\n').split(',')
265
+
266
+ indexes = ["actions"] + indexes[0 : - self.num_objectives]
267
+
268
+ proposals_all.append(indexes)
269
+
270
+ for i in range(len(actions)):
271
+
272
+ row = [str(X_all[actions[i]][j]) for j in range(len(X_all[actions[i]]))]
273
+
274
+ row = [str(actions[i])] + row
275
+
276
+ proposals_all.append(row)
277
+
278
+ print("###")
279
+ print("number =", i+1)
280
+ print("actions = ", actions[i])
281
+ print("proposal = ", X_all[actions[i]])
282
+ print("###")
283
+
284
+
285
+ with open(self.output_file, 'w', newline="") as f:
286
+ writer = csv.writer(f)
287
+ writer.writerows(proposals_all)
288
+
289
+ print("Finish selection of proposals!")
290
+
291
+ return "True"