1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """Module containing the base class for the OpenDX space mapping classes."""
25
26
27
28 from numpy import float64, array, zeros
29 from time import asctime, localtime
30
31
32 from generic_fns import diffusion_tensor
33 from generic_fns import pipes
34 from relax_errors import RelaxError, RelaxUnknownParamError
35 from relax_io import open_write_file
36 from specific_fns.setup import get_specific_fn
37
38
39
40
42 """The space mapping base class."""
43
44 - def __init__(self, params, spin_id, inc, lower, upper, axis_incs, file_prefix, dir, point, point_file, remap):
45 """Map the space upon class instantiation."""
46
47
48
49
50
51 self.params = params
52 self.spin_id = spin_id
53 self.n = len(params)
54 self.inc = inc
55 self.axis_incs = axis_incs
56 self.file_prefix = file_prefix
57 self.dir = dir
58 self.point_file = point_file
59 self.remap = remap
60
61
62 self.calculate = get_specific_fn('calculate', cdp.pipe_type)
63 self.model_stats = get_specific_fn('model_stats', cdp.pipe_type)
64 self.return_data_name = get_specific_fn('return_data_name', cdp.pipe_type)
65 self.map_bounds = []
66 self.return_conversion_factor = []
67 self.return_units = []
68 for i in xrange(self.n):
69 self.map_bounds.append(get_specific_fn('map_bounds', cdp.pipe_type))
70 self.return_conversion_factor.append(get_specific_fn('return_conversion_factor', cdp.pipe_type))
71 self.return_units.append(get_specific_fn('return_units', cdp.pipe_type))
72
73
74 self.diff_params = zeros(self.n)
75
76
77 self.get_param_names()
78
79
80 for i in xrange(self.n):
81 if self.diff_params[i]:
82 self.map_bounds[i] = diffusion_tensor.map_bounds
83 self.return_conversion_factor[i] = diffusion_tensor.return_conversion_factor
84 self.return_units[i] = diffusion_tensor.return_units
85
86
87 if point != None:
88 self.point = array(point, float64)
89 self.num_points = 1
90 else:
91 self.num_points = 0
92
93
94 self.bounds = zeros((self.n, 2), float64)
95 for i in xrange(self.n):
96
97 bounds = self.map_bounds[i](self.param_names[i], self.spin_id)
98
99
100 if not bounds:
101 raise RelaxError("No bounds for the parameter " + repr(self.params[i]) + " could be determined.")
102
103
104 self.bounds[i] = bounds
105
106
107 if lower != None:
108 self.bounds[:, 0] = array(lower, float64)
109
110
111 if upper != None:
112 self.bounds[:, 1] = array(upper, float64)
113
114
115 self.step_size = zeros(self.n, float64)
116 self.step_size = (self.bounds[:, 1] - self.bounds[:, 0]) / self.inc
117
118
119
120
121
122
123 self.get_date()
124
125
126 self.create_program()
127
128
129 self.create_config()
130
131
132 self.create_general()
133
134
135 if self.num_points == 1:
136 self.create_point()
137
138
139 self.create_map()
140
141
143 """Function for creating the OpenDX .cfg program configuration file."""
144
145
146 print("\nCreating the OpenDX .cfg program configuration file.")
147
148
149 config_file = open_write_file(file_name=self.file_prefix+".cfg", dir=self.dir, force=True)
150
151
152 text = self.config_text()
153
154
155 config_file.write(text)
156
157
158 config_file.close()
159
160
162 """Function for creating the OpenDX .general file."""
163
164
165 print("\nCreating the OpenDX .general file.")
166
167
168 general_file = open_write_file(file_name=self.file_prefix+".general", dir=self.dir, force=True)
169
170
171 text = self.general_text()
172
173
174 general_file.write(text)
175
176
177 general_file.close()
178
179
181 """Function for creating the map."""
182
183
184 print("\nCreating the map.")
185
186
187 map_file = open_write_file(file_name=self.file_prefix, dir=self.dir, force=True)
188
189
190 self.map_text(map_file)
191
192
193 map_file.close()
194
195
197 """Function for creating a sphere at a given position within the 3D map.
198
199 The formula used to calculate the coordinate position is::
200
201 V - L
202 coord = Inc * -----
203 U - L
204
205 where:
206 - V is the coordinate or parameter value.
207 - L is the lower bound value.
208 - U is the upper bound value.
209 - Inc is the number of increments.
210
211 Both a data file and .general file will be created.
212 """
213
214
215 print("\nCreating the OpenDX .general and data files for the given point.")
216
217
218 point_file = open_write_file(file_name=self.point_file, dir=self.dir, force=True)
219 point_file_general = open_write_file(file_name=self.point_file+".general", dir=self.dir, force=True)
220
221
222 coords = self.inc * (self.point - self.bounds[:, 0]) / (self.bounds[:, 1] - self.bounds[:, 0])
223 for i in xrange(self.n):
224 point_file.write("%-15.5g" % coords[i])
225 point_file.write("1\n")
226
227
228 text = self.point_text()
229
230
231 point_file_general.write(text)
232
233
234 point_file.close()
235 point_file_general.close()
236
237
239 """Function for creating the OpenDX .net program file."""
240
241
242 print("\nCreating the OpenDX .net program file.")
243
244
245 program_file = open_write_file(file_name=self.file_prefix+".net", dir=self.dir, force=True)
246
247
248 self.map_axes()
249
250
251 self.corners = "{[0"
252 for i in xrange(self.n - 1):
253 self.corners = self.corners + " 0"
254 self.corners = self.corners + "] [" + repr(self.inc)
255 for i in xrange(self.n - 1):
256 self.corners = self.corners + " " + repr(self.inc)
257 self.corners = self.corners + "]}"
258
259
260 self.sphere_size = repr(0.025 * (self.inc + 1.0))
261
262
263 text = self.program_text()
264
265
266 program_file.write(text)
267
268
269 program_file.close()
270
271
273 """Function for creating a date string."""
274
275 self.date = asctime(localtime())
276
277
307
308
310 """Function for creating labels, tick locations, and tick values for an OpenDX map."""
311
312
313 self.labels = "{"
314 self.tick_locations = []
315 self.tick_values = []
316 loc_inc = float(self.inc) / float(self.axis_incs)
317
318
319 for i in xrange(self.n):
320
321 factor = self.return_conversion_factor[i](self.param_names[i])
322
323
324 units = self.return_units[i](self.param_names[i])
325
326
327 if units:
328 self.labels = self.labels + "\"" + self.params[i] + " (" + units + ")\""
329 else:
330 self.labels = self.labels + "\"" + self.params[i] + "\""
331
332 if i < self.n - 1:
333 self.labels = self.labels + " "
334 else:
335 self.labels = self.labels + "}"
336
337
338 vals = self.bounds[i, 0] / factor
339 val_inc = (self.bounds[i, 1] - self.bounds[i, 0]) / (self.axis_incs * factor)
340
341 string = ""
342 for j in xrange(self.axis_incs + 1):
343 string = string + "\"" + "%.2f" % vals + "\" "
344 vals = vals + val_inc
345 self.tick_values.append("{" + string + "}")
346
347
348 string = ""
349 val = 0.0
350 for j in xrange(self.axis_incs + 1):
351 string = string + " " + repr(val)
352 val = val + loc_inc
353 self.tick_locations.append("{" + string + " }")
354