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

Source Code for Module opendx.base_map

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2012 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  # Module docstring. 
 24  """Module containing the base class for the OpenDX space mapping classes.""" 
 25   
 26   
 27  # Python module imports. 
 28  from numpy import float64, array, zeros 
 29  from time import asctime, localtime 
 30   
 31  # relax module imports. 
 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   
41 -class Base_Map:
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 # Initialise. 48 ############# 49 50 # Function arguments. 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 # Specific function setup. 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 # Diffusion tensor parameter flag. 74 self.diff_params = zeros(self.n) 75 76 # Get the parameter names. 77 self.get_param_names() 78 79 # Specific function setup (for diffusion tensor parameters). 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 # Points. 87 if point != None: 88 self.point = array(point, float64) 89 self.num_points = 1 90 else: 91 self.num_points = 0 92 93 # Get the default map bounds. 94 self.bounds = zeros((self.n, 2), float64) 95 for i in xrange(self.n): 96 # Get the bounds for the parameter i. 97 bounds = self.map_bounds[i](self.param_names[i], self.spin_id) 98 99 # No bounds found. 100 if not bounds: 101 raise RelaxError("No bounds for the parameter " + repr(self.params[i]) + " could be determined.") 102 103 # Assign the bounds to the global data structure. 104 self.bounds[i] = bounds 105 106 # Lower bounds. 107 if lower != None: 108 self.bounds[:, 0] = array(lower, float64) 109 110 # Upper bounds. 111 if upper != None: 112 self.bounds[:, 1] = array(upper, float64) 113 114 # Setup the step sizes. 115 self.step_size = zeros(self.n, float64) 116 self.step_size = (self.bounds[:, 1] - self.bounds[:, 0]) / self.inc 117 118 119 # Create all the OpenDX data and files. 120 ####################################### 121 122 # Get the date. 123 self.get_date() 124 125 # Create the OpenDX .net program file. 126 self.create_program() 127 128 # Create the OpenDX .cfg program configuration file. 129 self.create_config() 130 131 # Create the OpenDX .general file. 132 self.create_general() 133 134 # Create the OpenDX .general and data files for the given point. 135 if self.num_points == 1: 136 self.create_point() 137 138 # Generate the map. 139 self.create_map()
140 141
142 - def create_config(self):
143 """Function for creating the OpenDX .cfg program configuration file.""" 144 145 # Print out. 146 print("\nCreating the OpenDX .cfg program configuration file.") 147 148 # Open the file. 149 config_file = open_write_file(file_name=self.file_prefix+".cfg", dir=self.dir, force=True) 150 151 # Get the text of the configuration file. 152 text = self.config_text() 153 154 # Write the text. 155 config_file.write(text) 156 157 # Close the file. 158 config_file.close()
159 160
161 - def create_general(self):
162 """Function for creating the OpenDX .general file.""" 163 164 # Print out. 165 print("\nCreating the OpenDX .general file.") 166 167 # Open the file. 168 general_file = open_write_file(file_name=self.file_prefix+".general", dir=self.dir, force=True) 169 170 # Get the text of the configuration file. 171 text = self.general_text() 172 173 # Write the text. 174 general_file.write(text) 175 176 # Close the file. 177 general_file.close()
178 179
180 - def create_map(self):
181 """Function for creating the map.""" 182 183 # Print out. 184 print("\nCreating the map.") 185 186 # Open the file. 187 map_file = open_write_file(file_name=self.file_prefix, dir=self.dir, force=True) 188 189 # Generate and write the text of the map. 190 self.map_text(map_file) 191 192 # Close the file. 193 map_file.close()
194 195
196 - def create_point(self):
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 # Print out. 215 print("\nCreating the OpenDX .general and data files for the given point.") 216 217 # Open the files. 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 # Calculate the coordinate values. 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 # Get the text of the point .general file. 228 text = self.point_text() 229 230 # Write the text. 231 point_file_general.write(text) 232 233 # Close the data and .general files. 234 point_file.close() 235 point_file_general.close()
236 237
238 - def create_program(self):
239 """Function for creating the OpenDX .net program file.""" 240 241 # Print out. 242 print("\nCreating the OpenDX .net program file.") 243 244 # Open the file. 245 program_file = open_write_file(file_name=self.file_prefix+".net", dir=self.dir, force=True) 246 247 # Create the strings associated with the map axes. 248 self.map_axes() 249 250 # Corners. 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 # Sphere size. 260 self.sphere_size = repr(0.025 * (self.inc + 1.0)) 261 262 # Get the text of the program. 263 text = self.program_text() 264 265 # Write the text. 266 program_file.write(text) 267 268 # Close the file. 269 program_file.close()
270 271
272 - def get_date(self):
273 """Function for creating a date string.""" 274 275 self.date = asctime(localtime())
276 277
278 - def get_param_names(self):
279 """Function for retrieving the parameter names.""" 280 281 # Initialise. 282 self.param_names = [] 283 284 # Loop over the parameters. 285 for i in xrange(self.n): 286 # Get the parameter name. 287 name = self.return_data_name(self.params[i]) 288 289 # Diffusion tensor parameter. 290 if pipes.get_type() == 'mf': 291 # The diffusion tensor parameter name. 292 diff_name = diffusion_tensor.return_data_name(self.params[i]) 293 294 # Replace the model-free parameter with the diffusion tensor parameter if it exists. 295 if diff_name: 296 name = diff_name 297 298 # Set the flag indicating if there are diffusion tensor parameters. 299 self.diff_params[i] = 1 300 301 # Bad parameter name. 302 if not name: 303 raise RelaxUnknownParamError(self.params[i]) 304 305 # Append the parameter name. 306 self.param_names.append(name)
307 308
309 - def map_axes(self):
310 """Function for creating labels, tick locations, and tick values for an OpenDX map.""" 311 312 # Initialise. 313 self.labels = "{" 314 self.tick_locations = [] 315 self.tick_values = [] 316 loc_inc = float(self.inc) / float(self.axis_incs) 317 318 # Loop over the parameters 319 for i in xrange(self.n): 320 # Parameter conversion factors. 321 factor = self.return_conversion_factor[i](self.param_names[i]) 322 323 # Parameter units. 324 units = self.return_units[i](self.param_names[i]) 325 326 # Labels. 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 # Tick values. 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 # Tick locations. 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