Package specific_analyses :: Module api_objects
[hide private]
[frames] | no frames]

Source Code for Module specific_analyses.api_objects

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2012-2013 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  """A module of special objects used within the analysis specific API.""" 
 24   
 25  # Python module imports. 
 26  from re import search 
 27  from types import FunctionType, MethodType 
 28   
 29  # relax module imports. 
 30  from lib.errors import RelaxError 
 31   
 32   
33 -class Param_list:
34 """A special object for handling global and spin parameters.""" 35
36 - def __init__(self, spin_data=True):
37 """Set up the class. 38 39 @keyword spin_data: A flag which if True indicates that the specific analysis operates with spins. 40 @type spin_data: bool 41 """ 42 43 # Store the flags. 44 self.spin_data = spin_data 45 46 # Initialise the lists and dictionaries for the parameter info. 47 self._names = [] 48 self._scope = {} 49 self._string = {} 50 self._defaults = {} 51 self._units = {} 52 self._desc = {} 53 self._py_types = {} 54 self._conv_factor = {} 55 self._grace_string = {} 56 self._set = {} 57 self._err = {} 58 self._sim = {} 59 60 # Add some spin specific objects. 61 if self.spin_data: 62 self.add('select', scope='spin', desc='The spin selection flag', py_type=bool, sim=True) 63 self.add('fixed', scope='spin', desc='The fixed flag', py_type=bool)
64 65
66 - def add(self, name, scope=None, string=None, default=None, units=None, desc=None, py_type=None, set='generic', conv_factor=None, grace_string=None, err=False, sim=False):
67 """Add a parameter to the list. 68 69 @param name: The name of the parameter. This will be used as the variable name. 70 @type name: str 71 @keyword scope: The parameter scope. This can be set to 'global' for parameters located within the global scope of the current data pipe. Or set to 'spin' for spin specific parameters. Alternatively the value 'both' indicates that there are both global and specific versions of this parameter. 72 @type scope: str 73 @keyword string: The string representation of the parameter. 74 @type string: None or str 75 @keyword default: The default value of the parameter. 76 @type default: anything 77 @keyword units: A string representing the parameters units. 78 @type units: None or str 79 @keyword desc: The text description of the parameter. 80 @type desc: None or str 81 @keyword py_type: The Python type that this parameter should be. 82 @type py_type: Python type object 83 @keyword set: The set of object names. This can be set to 'all' for all names, to 'generic' for generic object names, 'params' for analysis specific parameter names, or to 'min' for minimisation specific object names. 84 @type set: str 85 @keyword conv_factor: The factor of conversion between different parameter units. 86 @type conv_factor: None, float or func 87 @keyword grace_string: The string used for the axes in Grace plots of the data. 88 @type grace_string: None or str 89 @keyword err: A flag which if True indicates that the parameter name + '_err' error data structure can exist. 90 @type err: bool 91 @keyword sim: A flag which if True indicates that the parameter name + '_sim' Monte Carlo simulation data structure can exist. 92 @type sim: bool 93 """ 94 95 # Checks. 96 if scope == None: 97 raise RelaxError("The parameter scope must be set.") 98 if py_type == None: 99 raise RelaxError("The parameter type must be set.") 100 allowed_sets = ['all', 'generic', 'params', 'min'] 101 if set not in allowed_sets: 102 raise RelaxError("The parameter set '%s' must be one of %s." % (set, allowed_sets)) 103 104 # Add the values. 105 self._names.append(name) 106 self._scope[name] = scope 107 self._defaults[name] = default 108 self._units[name] = units 109 self._desc[name] = desc 110 self._py_types[name] = py_type 111 self._set[name] = set 112 self._conv_factor[name] = conv_factor 113 self._err[name] = err 114 self._sim[name] = sim 115 116 # The parameter string. 117 if string: 118 self._string[name] = string 119 else: 120 self._string[name] = name 121 122 # The Grace string. 123 if grace_string: 124 self._grace_string[name] = grace_string 125 else: 126 self._grace_string[name] = name
127 128
129 - def add_min_data(self, min_stats_global=False, min_stats_spin=False):
130 """Add minimisation specific objects. 131 132 @keyword min_stats_global: A flag which if True will include the parameters 'chi2', 'iter', 'f_count', 'g_count', 'h_count', 'warning' in the list of global parameters. 133 @type min_stats_global: bool 134 @keyword min_stats_spin: A flag which if True will include the parameters 'chi2', 'iter', 'f_count', 'g_count', 'h_count', 'warning' in the list of spin parameters. 135 @type min_stats_spin: bool 136 """ 137 138 # Store the flags. 139 self.min_stats_global = min_stats_global 140 self.min_stats_spin = min_stats_spin 141 142 # Global minimisation data. 143 if self.min_stats_global or self.min_stats_spin: 144 # The scope. 145 if self.min_stats_global and self.min_stats_spin: 146 scope = 'both' 147 elif self.min_stats_global: 148 scope = 'global' 149 else: 150 scope = 'spin' 151 152 # The minimisation parameters. 153 self.add('chi2', scope=scope, desc='Chi-squared value', py_type=float, set='min', err=False, sim=True) 154 self.add('iter', scope=scope, desc='Optimisation iterations', py_type=int, set='min', err=False, sim=True) 155 self.add('f_count', scope=scope, desc='Number of function calls', py_type=int, set='min', err=False, sim=True) 156 self.add('g_count', scope=scope, desc='Number of gradient calls', py_type=int, set='min', err=False, sim=True) 157 self.add('h_count', scope=scope, desc='Number of Hessian calls', py_type=int, set='min', err=False, sim=True) 158 self.add('warning', scope=scope, desc='Optimisation warning', py_type=str, set='min', err=False, sim=True)
159 160
161 - def base_loop(self, set=None, scope=None):
162 """An iterator method for looping over all the base parameters. 163 164 @keyword set: The set of object names to return. This can be set to 'all' for all names, to 'generic' for generic object names, 'params' for analysis specific parameter names, or to 'min' for minimisation specific object names. 165 @type set: str 166 @keyword scope: The scope of the parameter to return. If not set, then all will be returned. If set to 'global' or 'spin', then only the parameters within that scope will be returned. 167 @type scope: str or None 168 @returns: The parameter names. 169 @rtype: str 170 """ 171 172 # Loop over the parameters. 173 for name in self._names: 174 # Skip the parameter if the set does not match. 175 if set == 'generic' and self._set[name] != 'generic': 176 continue 177 if set == 'params' and self._set[name] != 'params': 178 continue 179 if set == 'min' and self._set[name] != 'min': 180 continue 181 182 # Skip the parameter is outside of the scope. 183 if scope == 'global' and self._scope[name] == 'spin': 184 continue 185 if scope == 'spin' and self._scope[name] == 'global': 186 continue 187 188 # Yield the parameter name. 189 yield name
190 191
192 - def check_param(self, name):
193 """Check if the parameter exists. 194 195 @param name: The name of the parameter to search for. 196 @type name: str 197 @raises RelaxError: If the parameter does not exist. 198 """ 199 200 # Check. 201 if name not in self._names: 202 raise RelaxError("The parameter '%s' does not exist." % name)
203 204
205 - def contains(self, name):
206 """Determine if the given name is within the parameter list. 207 208 @param name: The name of the parameter to search for. 209 @type name: str 210 @return: True if the parameter is within the list, False otherwise. 211 @rtype: bool 212 """ 213 214 # Check. 215 if name in self._names: 216 return True 217 218 # No match. 219 return False
220 221
222 - def get_conv_factor(self, name):
223 """Return the conversion factor. 224 225 @param name: The name of the parameter. 226 @type name: str 227 @return: The conversion factor. 228 @rtype: float 229 """ 230 231 # Parameter check. 232 self.check_param(name) 233 234 # No factor. 235 if self._conv_factor[name] == None: 236 return 1.0 237 238 # Function. 239 if isinstance(self._conv_factor[name], FunctionType) or isinstance(self._conv_factor[name], MethodType): 240 return self._conv_factor[name]() 241 242 # Value. 243 return self._conv_factor[name]
244 245
246 - def get_default(self, name):
247 """Return the default value of the parameter. 248 249 @param name: The name of the parameter. 250 @type name: str 251 @return: The default value. 252 @rtype: None or str 253 """ 254 255 # Parameter check. 256 self.check_param(name) 257 258 # Return the default value. 259 return self._defaults[name]
260 261
262 - def get_desc(self, name):
263 """Return the description of the parameter. 264 265 @param name: The name of the parameter. 266 @type name: str 267 @return: The description. 268 @rtype: None or str 269 """ 270 271 # Skip error and simulation structures. 272 if name not in ['ri_data_err'] and (search('_err$', name) or search('_sim$', name)): 273 return None 274 275 # Parameter check. 276 self.check_param(name) 277 278 # Return the description. 279 return self._desc[name]
280 281
282 - def get_err(self, name):
283 """Return the error flag for the parameter. 284 285 @param name: The name of the parameter. 286 @type name: str 287 @return: The error flag for the parameter. 288 @rtype: bool 289 """ 290 291 # Parameter check. 292 self.check_param(name) 293 294 # Return the type. 295 return self._err[name]
296 297
298 - def get_grace_string(self, name):
299 """Return the Grace string for the parameter. 300 301 @param name: The name of the parameter. 302 @type name: str 303 @return: The Grace string. 304 @rtype: str 305 """ 306 307 # Parameter check. 308 self.check_param(name) 309 310 # Return the value. 311 return self._grace_string[name]
312 313
314 - def get_set(self, name):
315 """Return the parameter set that the parameter belongs to. 316 317 @param name: The name of the parameter. 318 @type name: str 319 @return: The parameter set. 320 @rtype: str 321 """ 322 323 # Parameter check. 324 self.check_param(name) 325 326 # Return the type. 327 return self._set[name]
328 329
330 - def get_sim(self, name):
331 """Return the Monte Carlo simulation flag for the parameter. 332 333 @param name: The name of the parameter. 334 @type name: str 335 @return: The Monte Carlo simulation flag for the parameter. 336 @rtype: bool 337 """ 338 339 # Parameter check. 340 self.check_param(name) 341 342 # Return the type. 343 return self._sim[name]
344 345
346 - def get_type(self, name):
347 """Return the Python type for the parameter. 348 349 @param name: The name of the parameter. 350 @type name: str 351 @return: The Python type. 352 @rtype: Python type object 353 """ 354 355 # Parameter check. 356 self.check_param(name) 357 358 # Return the Python type. 359 return self._py_types[name]
360 361
362 - def get_units(self, name):
363 """Return the units string for the parameter. 364 365 @param name: The name of the parameter. 366 @type name: str 367 @return: The units string. 368 @rtype: str 369 """ 370 371 # Parameter check. 372 self.check_param(name) 373 374 # Function. 375 if isinstance(self._conv_factor[name], FunctionType) or isinstance(self._conv_factor[name], MethodType): 376 return self._units[name]() 377 378 # Return the value. 379 return self._units[name]
380 381
382 - def loop(self, set=None, scope=None, error_names=False, sim_names=False):
383 """An iterator method for looping over all the parameters. 384 385 @keyword set: The set of object names to return. This can be set to 'all' for all names, to 'generic' for generic object names, 'params' for analysis specific parameter names, or to 'min' for minimisation specific object names. 386 @type set: str 387 @keyword scope: The scope of the parameter to return. If not set, then all will be returned. If set to 'global' or 'spin', then only the parameters within that scope will be returned. 388 @type scope: str or None 389 @keyword error_names: A flag which if True will add the error object names as well. 390 @type error_names: bool 391 @keyword sim_names: A flag which if True will add the Monte Carlo simulation object names as well. 392 @type sim_names: bool 393 @returns: The parameter names. 394 @rtype: str 395 """ 396 397 # Loop over and yield the parameters. 398 for name in self.base_loop(set=set, scope=scope): 399 yield name 400 401 # Error names. 402 if error_names: 403 for name in self.base_loop(set=set): 404 if self.get_err(name): 405 yield name + '_err' 406 407 # Sim names. 408 if sim_names: 409 for name in self.base_loop(set=set): 410 if self.get_sim(name): 411 yield name + '_sim'
412