-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_data.py
103 lines (86 loc) · 3.98 KB
/
generate_data.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
import subprocess
import os
import errno
import json
import numpy as np
import time
import datetime
def run_specific(cylinder_rads, cylinder_sep_fraction=2, no_voxels=1000):
"""
Method for generating (specific) data with camino
Calls bash-script which in turn calls camino to generate data
@param cylinder_rads: List of cylinder radiuses to simulate
@param cylinder_sep_fraction: Fraction of the cylinder radius separation (for example 2 gives separation 2*cylinder radius)
@param no_voxels: Number of voxels
"""
print 'Begin data generation with ' + str(len(cylinder_rads)) + ' iterations and ' + str(no_voxels) + ' in every iteration'
for i in range(0, len(cylinder_rads)):
print('Running iteration ' + str(i+1) + ' of ' + str(len(cylinder_rads)))
print(time.strftime("%c"))
dirname = "./data/search/" + str(i) + '-' + str(datetime.datetime.now().isoformat()) + '/'
# Get radius
radius = cylinder_rads[i]
# Get a config file from describing the generated data
config = _get_config(voxels=no_voxels, cylinder_rad=radius, cylinder_sep=cylinder_sep_fraction*radius, dir_name=dirname)
# Perform the actual data generation
_generate_data(config)
def run_random(no_iter=100, no_voxels=1000, cylinder_rad_from=1E-8, cylinder_rad_to=1E-6, cylinder_sep_from=1.1E-6, cylinder_sep_to=1.1E-6):
"""
Main method for generating data with camino
Calls bash-script which in turn calls camino to generate data
@param no_iter: Number of
@param no_voxels: Number of voxels to generate
@param cylinder_rad_from: random cylinder radius in range from
@param cylinder_rad_to: random cylinder radius in range to
@param cylinder_sep_from: random cylinder separation in range from
@param cylinder_sep_to: random cylinder separation in range to
"""
# Seed the random generator
np.random.seed(int(round(time.time())))
print 'Begin data generation with ' + str(no_iter) + ' iterations and ' + str(no_voxels) + ' in every iteration'
for i in range(0, no_iter):
print('Running iteration ' + str(i+1) + ' of ' + str(no_iter))
print(time.strftime("%c"))
dirname = "./data/gen/" + str(i) + '-' + str(datetime.datetime.now().isoformat()) + '/'
# Sample cylinder radius and separation in range
radius = (cylinder_rad_to - cylinder_rad_from) * np.random.random_sample() + cylinder_rad_from
separation = (cylinder_sep_to - cylinder_sep_from) * np.random.random_sample() + cylinder_sep_from
# Get a config file from describing the generated data
config = _get_config(voxels=no_voxels, cylinder_rad=radius, cylinder_sep=separation, dir_name=dirname)
# Perform the actual data generation
_generate_data(config)
# Helper method to call bash script
def _generate_data(config):
if not os.path.exists(config['dir_name']):
try:
os.makedirs(config['dir_name'])
except OSError as exc:
if exc.errno != errno.EEXIST:
raise
# Write the config file
with open(config['dir_name'] + 'config.json', 'w') as outfile:
json.dump(config, outfile, sort_keys=True, indent=4)
# Run camino and generate data
p = subprocess.Popen(['./run_camino.sh', str(config['walkers']), str(config['tmax']), str(config['voxels']), str(config['p']), str(config['scheme_path']), str(config['cylinder_rad']), str(config['cylinder_sep']), str(config['out_name'])], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.communicate()
# Write the target file
rad = np.full(config['voxels'], config['cylinder_rad'])
sep = np.full(config['voxels'], config['cylinder_sep'])
np.savetxt(config['dir_name'] + 'targets.txt', np.transpose([rad, sep]))
# Generates a config about what is generated
def _get_config(walkers=100000, tmax=1000, voxels=1, p=0.0, scheme_path='./data/hpc.scheme', cylinder_rad=1E-6, cylinder_sep=2.1E-6, dir_name=''):
out_name = str(dir_name) + 'cylinders.bfloat'
obj = {
'walkers': walkers,
'tmax': tmax,
'voxels': voxels,
'p': p,
'scheme_path': scheme_path,
'cylinder_rad': cylinder_rad,
'cylinder_sep': cylinder_sep,
'dir_name': dir_name,
'out_name': out_name
}
return obj
if __name__ == '__main__':
run_random()