/
dataset.py
371 lines (339 loc) · 15.4 KB
/
dataset.py
1
2
3
4
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env python
# coding: utf-8
import re
import os
import json
import numpy as np
from tqdm import tqdm
import multiprocessing as mp
from datetime import datetime
from generator.treeBased.generateData import dataGen
from utils import * # TODO: replace with a safer import
def processData(numSamples, nv, decimals,
template, dataPath, fileID, time,
supportPoints=None,
supportPointsTest=None,
numberofPoints=[20,250],
xRange=[0.1,3.1], testPoints=False,
testRange=[0.0,6.0], n_levels = 3,
allow_constants=True,
const_range=[-0.4, 0.4],
const_ratio=0.8,
op_list=[
"id", "add", "mul", "div",
"sqrt", "sin", "exp", "log"],
sortY=False,
exponents= [3,4,5,6],
numSamplesEachEq=1,
threshold = 100,
templatesEQs=None,
templateProb=0.4,
):
for i in tqdm(range(numSamples)):
structure = template.copy()
# generate a formula
# Create a new random equation
try:
_, skeletonEqn, _ = dataGen(
nv = nv, decimals = decimals,
numberofPoints=numberofPoints,
supportPoints=supportPoints,
supportPointsTest=supportPointsTest,
xRange=xRange,
testPoints=testPoints,
testRange=testRange,
n_levels=n_levels,
op_list=op_list,
allow_constants=allow_constants,
const_range=const_range,
const_ratio=const_ratio,
exponents=exponents
)
if templatesEQs != None and np.random.rand() < templateProb:
# by a chance, replace the skeletonEqn with a given templates
idx = np.random.randint(len(templatesEQs[nv]))
skeletonEqn = templatesEQs[nv][idx]
except Exception as e:
# Handle any exceptions that timing might raise here
print("\n-->dataGen(.) was terminated!\n{}\n".format(e))
i = i-1
continue
# fix exponents that are larger than our expected value, sometimes the data generator generates those odd numbers
exps = re.findall(r"(\*\*[0-9\.]+)", skeletonEqn)
for ex in exps:
# correct the exponent
cexp = '**'+str(eval(ex[2:]) if eval(ex[2:]) < exponents[-1] else np.random.randint(2,exponents[-1]+1))
# replace the exponent
skeletonEqn = skeletonEqn.replace(ex, cexp)
for e in range(numSamplesEachEq):
# replace the constants with new ones
cleanEqn = ''
for chr in skeletonEqn:
if chr == 'C':
# genereate a new random number
chr = '{}'.format(np.random.uniform(const_range[0], const_range[1]))
cleanEqn += chr
if 'I' in cleanEqn or 'zoo' in cleanEqn:
# repeat the equation generation
print('This equation has been rejected: {}'.format(cleanEqn))
i -= 1 #TODO: this might lead to a bad loop
break
# generate new data points
nPoints = np.random.randint(
*numberofPoints) if supportPoints is None else len(supportPoints)
try:
data = generateDataStrEq(cleanEqn, n_points=nPoints, n_vars=nv,
decimals=decimals, supportPoints=supportPoints, min_x=xRange[0], max_x=xRange[1])
except:
# for different reason this might happend including but not limited to division by zero
continue
# if testPoints:
# dataTest = generateDataStrEq(currEqn, n_points=numberofPoints, n_vars=nv, decimals=decimals,
# supportPoints=supportPointsTest, min_x=testRange[0], max_x=testRange[1]))
# use the new x and y
x,y = data
# check if there is nan/inf/very large numbers in the y
if np.isnan(y).any() or np.isinf(y).any(): # TODO: Later find a more optimized solution
# repeat the data generation
#i -= 1 #TODO: this might lead to a bad loop
#break
e -= 1
continue
# replace out of threshold with maximum numbers
y = [e if abs(e)<threshold else np.sign(e) * threshold for e in y]
if len(y) == 0: # if for whatever reason the y is empty
print('Empty y, x: {}, most of the time this is because of wrong numberofPoints: {}'.format(x, numberofPoints))
e -= 1
continue
# just make sure there is no samples out of the threshold
if abs(min(y)) > threshold or abs(max(y)) > threshold:
raise 'Err: Min:{},Max:{},Threshold:{}, \n Y:{} \n Eq:{}'.format(min(y), max(y), threshold, y, cleanEqn)
# sort data based on Y
if sortY:
x,y = zip(*sorted(zip(x,y), key=lambda d: d[1]))
# hold data in the structure
structure['X'] = list(x)
structure['Y'] = y
structure['Skeleton'] = skeletonEqn
structure['EQ'] = cleanEqn
outputPath = dataPath.format(fileID, nv, time)
if os.path.exists(outputPath):
fileSize = os.path.getsize(outputPath)
if fileSize > 500000000: # 500 MB
fileID +=1
with open(outputPath, "a", encoding="utf-8") as h:
json.dump(structure, h, ensure_ascii=False)
h.write('\n')
def main():
# Config
seed = 2021 # 2021 Train, 2022 Val, 2023 Test, you have to change the generateData.py seed as well
#from GenerateData import seed
import random
random.seed(seed)
np.random.seed(seed=seed) # fix the seed for reproducibility
#NOTE: For linux you can only use unique numVars, in Windows, it is possible to use [1,2,3,4] * 10!
numVars = list(range(1,10)) #[1,2,3,4,5]
decimals = 4
numberofPoints = [20,250] # only usable if support points has not been provided
numSamples = 10000 # number of generated samples
folder = './Dataset'
dataPath = folder +'/{}_{}_{}.json'
testPoints = False
trainRange = [-3.0,3.0]
testRange = [[-5.0, 3.0],[-3.0, 5.0]] # this means Union((-5,-1),(1,5))
supportPoints = None
#supportPoints = np.linspace(xRange[0],xRange[1],numberofPoints[1])
#supportPoints = [[np.round(p,decimals)] for p in supportPoints]
#supportPoints = [[np.round(p,decimals), np.round(p,decimals)] for p in supportPoints]
#supportPoints = [[np.round(p,decimals) for i in range(numVars[0])] for p in supportPoints]
supportPointsTest = None
#supportPoints = None # uncomment this line if you don't want to use support points
#supportPointsTest = np.linspace(xRange[0],xRange[1],numberofPoints[1])
#supportPointsTest = [[np.round(p,decimals) for i in range(numVars[0])] for p in supportPointsTest]
n_levels = 4
allow_constants = True
const_range = [-2.1, 2.1]
const_ratio = 0.5
op_list=[
"id", "add", "mul",
"sin", "pow", "cos", "sqrt",
"exp", "div", "sub", "log",
"arcsin",
]
exponents=[3, 4, 5, 6]
sortY = False # if the data is sorted based on y
numSamplesEachEq = 5
threshold = 5000
templateProb = 0.1 # the probability of generating an equation from the templates
templatesEQs = None # template equations, if NONE then there will be no specific templates for the generated equations
templatesEQs = {
1: [
# NGUYEN
'C*x1**3+C*x1**2+C*x1+C',
'C*x1**4+C*x1**3+C*x1**2+C*x1+C',
'C*x1**5+C*x1**4+C*x1**3+C*x1**2+C*x1+C',
'C*x1**6+C*x1**5+C*x1**4+C*x1**3+C*x1**2+C*x1+C',
'C*sin(C*x1**2)*cos(C*x1+C)+C',
'C*sin(C*x1+C)+C*sin(C*x1+C*x1**2)+C',
'C*log(C*x1+C)+C*log(C*x1**2+C)+C',
'C*sqrt(C*x1+C)+C',
],
2: [
# NGUYEN
'C*sin(C*x1+C)+C*sin(C*x2**2+C)+C',
'C*sin(C*x1+C)*cos(C*x2+C)+C',
'C*x1**x2+C',
'C*x1**4+C*x1**3+C*x2**2+C*x2+C',
# # AI Faynman
# 'C*exp(C*x1**2+C)/sqrt(C*x2+C)+C',
# 'C*x1*x2+C',
# 'C*1/2*x1*x2**2+C',
# 'C*x1/x2+C',
# 'C*arcsin(C*x1*sin(C*x2+C)+C)+C',
# 'C*(C*x1/(2*pi)+C)*x2+C',
# 'C*3/2*x1*x2+C',
# 'C*x1/(C*4*pi*x2**2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',
# 'C*x1/(C*4*pi*x2+C)*3*x5/x6**5*sqrt(C*x3**2+x4**2+C)+C',
# 'C*x1*(1+C*x2*x3*cos(C*x4+C)/(C*x5*x6+C)+C)+C',
# 'C*(C*x1*x5*x4/(C*x6/(2*pi)+C)+C)*sin(C*(C*x2-C*x3+C)*x4/2)**2/(C*(C*x2-C*x3+C)*x4/2)**2+C',
# ],
# 7: [
# # AI Faynman
# 'C*(C*1/2*x1*x4*x5**2+C)*(C*8*x6*x7**2/3+C)*(C*x2**4/(C*x2**2-C*x3**2+C)**2+C)+C',
# ],
# 8: [
# # AI Faynman
# 'C*x1*x8/(C*x4*x5+C)+C*(C*x1*x2+C)/(C*x3*x7**2*x4*x5+C)*x6+C',
# ],
# 9: [
# # AI Faynman
# 'C*x3*x4*x5/((C*x2+C*x1+C)**2+(C*x6+C*x7+C)**2+(C*x8+C*x9)**2+C)+C',
# ],
}
print(os.mkdir(folder) if not os.path.isdir(folder) else 'We do have the path already!')
template = {'X':[], 'Y':0.0, 'EQ':''}
fileID = 0
#mp.set_start_method('spawn')
#q = mp.Queue()
processes = []
for i, nv in enumerate(numVars):
now = datetime.now()
time = '{}_'.format(i) + now.strftime("%d%m%Y_%H%M%S")
print('Processing equations with {} variables!'.format(nv))
p = mp.Process(target=processData,
args=(
numSamples, nv, decimals, template,
dataPath, fileID, time, supportPoints,
supportPointsTest,
numberofPoints,
trainRange, testPoints, testRange, n_levels,
allow_constants, const_range,
const_ratio, op_list, sortY, exponents,
numSamplesEachEq,
threshold,
templatesEQs,
templateProb
)
)
p.start()
processes.append(p)
for p in processes:
p.join()
if __name__ == '__main__':
main()