Package dx :: Module base_map
[hide private]
[frames] | no frames]

Source Code for Module dx.base_map

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2005 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax is free software; you can redistribute it and/or modify               # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation; either version 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax is distributed in the hope that it will be useful,                    # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23   
 24  from Numeric import Float64, array, zeros 
 25  from time import asctime, localtime 
 26   
 27   
28 -class Base_Map:
29 - def __init__(self):
30 """The space mapping base class."""
31 32
33 - def create_config(self):
34 """Function for creating the OpenDX .cfg program configuration file.""" 35 36 # Print out. 37 print "\nCreating the OpenDX .cfg program configuration file." 38 39 # Open the file. 40 config_file = self.relax.IO.open_write_file(file_name=self.file+".cfg", dir=self.dir, force=1) 41 42 # Get the text of the configuration file. 43 text = self.config_text() 44 45 # Write the text. 46 config_file.write(text) 47 48 # Close the file. 49 config_file.close()
50 51
52 - def create_general(self):
53 """Function for creating the OpenDX .general file.""" 54 55 # Print out. 56 print "\nCreating the OpenDX .general file." 57 58 # Open the file. 59 general_file = self.relax.IO.open_write_file(file_name=self.file+".general", dir=self.dir, force=1) 60 61 # Get the text of the configuration file. 62 text = self.general_text() 63 64 # Write the text. 65 general_file.write(text) 66 67 # Close the file. 68 general_file.close()
69 70
71 - def create_map(self):
72 """Function for creating the map.""" 73 74 # Print out. 75 print "\nCreating the map." 76 77 # Open the file. 78 map_file = self.relax.IO.open_write_file(file_name=self.file, dir=self.dir, force=1) 79 80 # Generate and write the text of the map. 81 self.map_text(map_file) 82 83 # Close the file. 84 map_file.close()
85 86
87 - def create_point(self):
88 """Function for creating a sphere at a given position within the 3D map. 89 90 The formula used to calculate the coordinate position is: 91 92 V - L 93 coord = Inc * ----- 94 U - L 95 96 where: 97 V is the coordinate or parameter value. 98 L is the lower bound value. 99 U is the upper bound value. 100 Inc is the number of increments. 101 102 Both a data file and .general file will be created. 103 """ 104 105 # Print out. 106 print "\nCreating the OpenDX .general and data files for the given point." 107 108 # Open the files. 109 point_file = self.relax.IO.open_write_file(file_name=self.point_file, dir=self.dir, force=1) 110 point_file_general = self.relax.IO.open_write_file(file_name=self.point_file+".general", dir=self.dir, force=1) 111 112 # Calculate the coordinate values. 113 coords = self.inc * (self.point - self.bounds[:, 0]) / (self.bounds[:, 1] - self.bounds[:, 0]) 114 for i in xrange(self.n): 115 point_file.write("%-15.5g" % coords[i]) 116 point_file.write("1\n") 117 118 # Get the text of the point .general file. 119 text = self.point_text() 120 121 # Write the text. 122 point_file_general.write(text) 123 124 # Close the data and .general files. 125 point_file.close() 126 point_file_general.close()
127 128
129 - def create_program(self):
130 """Function for creating the OpenDX .net program file.""" 131 132 # Print out. 133 print "\nCreating the OpenDX .net program file." 134 135 # Open the file. 136 program_file = self.relax.IO.open_write_file(file_name=self.file+".net", dir=self.dir, force=1) 137 138 # Create the strings associated with the map axes. 139 self.map_axes() 140 141 # Corners. 142 self.corners = "{[0" 143 for i in xrange(self.n - 1): 144 self.corners = self.corners + " 0" 145 self.corners = self.corners + "] [" + `self.inc` 146 for i in xrange(self.n - 1): 147 self.corners = self.corners + " " + `self.inc` 148 self.corners = self.corners + "]}" 149 150 # Sphere size. 151 self.sphere_size = `0.025 * (self.inc + 1.0)` 152 153 # Get the text of the program. 154 text = self.program_text() 155 156 # Write the text. 157 program_file.write(text) 158 159 # Close the file. 160 program_file.close()
161 162
163 - def get_date(self):
164 """Function for creating a date string.""" 165 166 self.date = asctime(localtime())
167 168
169 - def get_param_names(self):
170 """Function for retrieving the parameter names.""" 171 172 # Initialise. 173 self.param_names = [] 174 175 # Loop over the parameters. 176 for i in xrange(self.n): 177 # Get the parameter name. 178 name = self.return_data_name(self.params[i]) 179 180 # Diffusion tensor parameter. 181 if self.function_type == 'mf': 182 # The diffusion tensor parameter name. 183 diff_name = self.relax.generic.diffusion_tensor.return_data_name(self.params[i]) 184 185 # Replace the model-free parameter with the diffusion tensor parameter if it exists. 186 if diff_name: 187 name = diff_name 188 189 # Set the flag indicating if there are diffusion tensor parameters. 190 self.diff_params[i] = 1 191 192 # Bad parameter name. 193 if not name: 194 raise RelaxUnknownParamError, self.params[i] 195 196 # Append the parameter name. 197 self.param_names.append(name)
198 199
200 - def map_axes(self):
201 """Function for creating labels, tick locations, and tick values for an OpenDX map.""" 202 203 # Initialise. 204 self.labels = "{" 205 self.tick_locations = [] 206 self.tick_values = [] 207 loc_inc = float(self.inc) / float(self.axis_incs) 208 209 # Loop over the parameters 210 for i in xrange(self.n): 211 # Parameter conversion factors. 212 factor = self.return_conversion_factor[i](self.param_names[i]) 213 214 # Parameter units. 215 units = self.return_units[i](self.param_names[i]) 216 217 # Labels. 218 if units: 219 self.labels = self.labels + "\"" + self.params[i] + " (" + units + ")\"" 220 else: 221 self.labels = self.labels + "\"" + self.params[i] + "\"" 222 223 if i < self.n - 1: 224 self.labels = self.labels + " " 225 else: 226 self.labels = self.labels + "}" 227 228 # Tick values. 229 vals = self.bounds[i, 0] / factor 230 val_inc = (self.bounds[i, 1] - self.bounds[i, 0]) / (self.axis_incs * factor) 231 232 string = "" 233 for j in xrange(self.axis_incs + 1): 234 string = string + "\"" + "%.2f" % vals + "\" " 235 vals = vals + val_inc 236 self.tick_values.append("{" + string + "}") 237 238 # Tick locations. 239 string = "" 240 val = 0.0 241 for j in xrange(self.axis_incs + 1): 242 string = string + " " + `val` 243 val = val + loc_inc 244 self.tick_locations.append("{" + string + " }")
245 246
247 - def map_space(self, run, params, res_num, index, inc, lower, upper, axis_incs, file, dir, point, point_file, remap):
248 """Generic function for mapping a space.""" 249 250 # Initialise. 251 ############# 252 253 # Function type. 254 self.function_type = self.relax.data.run_types[self.relax.data.run_names.index(run)] 255 256 # Function arguments. 257 self.run = run 258 self.params = params 259 self.res_num = res_num 260 self.index = index 261 self.n = len(params) 262 self.inc = inc 263 self.axis_incs = axis_incs 264 self.file = file 265 self.dir = dir 266 self.point_file = point_file 267 self.remap = remap 268 269 # Specific function setup. 270 self.calculate = self.relax.specific_setup.setup('calculate', self.function_type) 271 self.model_stats = self.relax.specific_setup.setup('model_stats', self.function_type) 272 self.return_data_name = self.relax.specific_setup.setup('return_data_name', self.function_type) 273 self.map_bounds = [] 274 self.return_conversion_factor = [] 275 self.return_units = [] 276 for i in xrange(self.n): 277 self.map_bounds.append(self.relax.specific_setup.setup('map_bounds', self.function_type)) 278 self.return_conversion_factor.append(self.relax.specific_setup.setup('return_conversion_factor', self.function_type)) 279 self.return_units.append(self.relax.specific_setup.setup('return_units', self.function_type)) 280 281 # Diffusion tensor parameter flag. 282 self.diff_params = zeros(self.n) 283 284 # Get the parameter names. 285 self.get_param_names() 286 287 # Specific function setup (for diffusion tensor parameters). 288 for i in xrange(self.n): 289 if self.diff_params[i]: 290 self.map_bounds[i] = self.relax.generic.diffusion_tensor.map_bounds 291 self.return_conversion_factor[i] = self.relax.generic.diffusion_tensor.return_conversion_factor 292 self.return_units[i] = self.relax.generic.diffusion_tensor.return_units 293 294 # Points. 295 if point != None: 296 self.point = array(point, Float64) 297 self.num_points = 1 298 else: 299 self.num_points = 0 300 301 # The OpenDX directory. 302 self.relax.IO.mkdir(self.dir, print_flag=0) 303 304 # Get the default map bounds. 305 self.bounds = zeros((self.n, 2), Float64) 306 for i in xrange(self.n): 307 # Get the bounds for the parameter i. 308 bounds = self.map_bounds[i](self.run, self.param_names[i]) 309 310 # No bounds found. 311 if not bounds: 312 raise RelaxError, "No bounds for the parameter " + `self.params[i]` + " could be determined." 313 314 # Assign the bounds to the global data structure. 315 self.bounds[i] = bounds 316 317 # Lower bounds. 318 if lower != None: 319 self.bounds[:, 0] = array(lower, Float64) 320 321 # Upper bounds. 322 if upper != None: 323 self.bounds[:, 1] = array(upper, Float64) 324 325 # Setup the step sizes. 326 self.step_size = zeros(self.n, Float64) 327 self.step_size = (self.bounds[:, 1] - self.bounds[:, 0]) / self.inc 328 329 330 # Create all the OpenDX data and files. 331 ####################################### 332 333 # Get the date. 334 self.get_date() 335 336 # Create the OpenDX .net program file. 337 self.create_program() 338 339 # Create the OpenDX .cfg program configuration file. 340 self.create_config() 341 342 # Create the OpenDX .general file. 343 self.create_general() 344 345 # Create the OpenDX .general and data files for the given point. 346 if self.num_points == 1: 347 self.create_point() 348 349 # Generate the map. 350 self.create_map()
351