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