Package specific_fns :: Module api_common
[hide private]
[frames] | no frames]

Source Code for Module specific_fns.api_common

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2004-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  """API methods in common between different analysis types.""" 
 25   
 26  # Python module imports. 
 27  from copy import deepcopy 
 28   
 29  # relax module imports. 
 30  import arg_check 
 31  from data.mol_res_spin import SpinContainer 
 32  from generic_fns.mol_res_spin import count_spins, exists_mol_res_spin_data, return_spin, spin_loop 
 33  from relax_errors import RelaxError, RelaxLenError, RelaxNoSequenceError 
 34   
 35   
36 -class API_common:
37 """Base class containing API methods common to multiple analysis types.""" 38
39 - def _base_data_loop_spin(self):
40 """Generator method for looping over the base data of the specific analysis type (spin system specific). 41 42 This method simply loops over the spins, returning the spin identification string. 43 44 @return: The spin identification string 45 @rtype: str 46 """ 47 48 # Loop over the spins. 49 for spin, spin_id in spin_loop(return_id=True): 50 # Skip deselected spins. 51 if not spin.select: 52 continue 53 54 # Yield the spin id string. 55 yield spin_id
56 57
58 - def _create_mc_relax_data(self, data_id):
59 """Return the Monte Carlo relaxation data list for the corresponding spin. 60 61 @param data_id: The spin identification string, as yielded by the base_data_loop() generator method. 62 @type data_id: str 63 """ 64 65 # Get the spin container. 66 spin = return_spin(data_id) 67 68 # Initialise the data structure. 69 data = [] 70 71 # Add the data. 72 for ri_id in cdp.ri_ids: 73 data.append(spin.ri_data[ri_id]) 74 75 # Return the data. 76 return data
77 78
79 - def _data_init_dummy(self, data_cont, sim=False):
80 """Dummy method for initialising data structures. 81 82 This method does nothing! 83 84 85 @param data_cont: The data container. 86 @type data_cont: instance 87 @keyword sim: The unused Monte Carlo simulation flag. 88 @type sim: bool 89 """
90 91
92 - def _eliminate_false(self, name, value, model_info, args, sim=None):
93 """Dummy method for model elimination. 94 95 This simply returns False to signal that no model elimination is to be performed. 96 97 98 @param name: The parameter name. 99 @type name: str 100 @param value: The parameter value. 101 @type value: float 102 @param model_info: The model index from model_info(). 103 @type model_info: int 104 @param args: The elimination constant overrides. 105 @type args: None or tuple of float 106 @keyword sim: The Monte Carlo simulation index. 107 @type sim: int 108 @return: False to prevent model elimination. 109 @rtype: bool 110 """ 111 112 # Don't eliminate. 113 return False
114 115
116 - def _has_errors_spin(self):
117 """Testing if errors exist for the current data pipe (spin system specific). 118 119 @return: The answer to the question of whether errors exist. 120 @rtype: bool 121 """ 122 123 # Diffusion tensor errors. 124 if hasattr(cdp, 'diff'): 125 for object_name in dir(cdp.diff): 126 # The object error name. 127 object_error = object_name + '_err' 128 129 # Error exists. 130 if hasattr(cdp.diff, object_error): 131 return True 132 133 # Loop over the sequence. 134 for spin in spin_loop(): 135 # Parameter errors. 136 for object_name in dir(spin): 137 # The object error name. 138 object_error = object_name + '_err' 139 140 # Error exists. 141 if hasattr(spin, object_error): 142 return True 143 144 # No errors found. 145 return False
146 147
148 - def _is_spin_param_true(self, name):
149 """Dummy method stating that the parameter is spin specific. 150 151 This method always returns true, hence all parameters will be considered residents of a SpinContainer object unless this method is overwritten. 152 153 @param name: The name of the parameter. 154 @type name: str 155 @return: True 156 @rtype: bool 157 """ 158 159 # Return the default of True. 160 return True
161 162
163 - def _model_loop_spin(self):
164 """Default generator method for looping over the models, where each spin has a separate model. 165 166 In this case only a single model per spin system is assumed. Hence the yielded data is the spin container object. 167 168 169 @return: The spin container. 170 @rtype: SpinContainer instance 171 """ 172 173 # Loop over the sequence. 174 for spin in spin_loop(): 175 # Skip deselected spins. 176 if not spin.select: 177 continue 178 179 # Yield the spin container. 180 yield spin
181 182
184 """Default generator method for looping over a single global (non-spin specific) model. 185 186 The loop will yield a single index, zero, once to indicate a single model. 187 188 189 @return: The global model index of zero. 190 @rtype: int 191 """ 192 193 # Yield the index zero. 194 yield 0
195 196
197 - def _num_instances_spin(self):
198 """Return the number of instances, equal to the number of selected spins. 199 200 @return: The number of instances (equal to the number of spins). 201 @rtype: int 202 """ 203 204 # Test if sequence data is loaded. 205 if not exists_mol_res_spin_data(): 206 raise RelaxNoSequenceError 207 208 # Return the number of spins. 209 return count_spins()
210 211
212 - def _overfit_deselect_dummy(self):
213 """Dummy method, normally for deselecting spins with insufficient data for minimisation."""
214 215
216 - def _return_no_conversion_factor(self, param):
217 """Method for returning 1.0. 218 219 @param param: The parameter name. 220 @type param: str 221 @return: A conversion factor of 1.0. 222 @rtype: float 223 """ 224 225 return 1.0
226 227
228 - def _return_data_relax_data(self, spin):
229 """Return the Ri data structure for the given spin. 230 231 @param spin: The SpinContainer object. 232 @type spin: SpinContainer instance 233 @return: The array of relaxation data values. 234 @rtype: list of float 235 """ 236 237 # Convert to a list. 238 data = [] 239 for ri_id in cdp.ri_ids: 240 data.append(spin.ri_data[ri_id]) 241 242 # Return the list. 243 return data
244 245
246 - def _return_error_relax_data(self, data_id):
247 """Return the Ri error structure for the corresponding spin. 248 249 @param data_id: The data identification information, as yielded by the base_data_loop() generator method. 250 @type data_id: str 251 @return: The array of relaxation data error values. 252 @rtype: list of float 253 """ 254 255 # Get the spin container. 256 spin = return_spin(data_id) 257 258 # Convert to a list. 259 error = [] 260 for ri_id in cdp.ri_ids: 261 error.append(spin.ri_data_err[ri_id]) 262 263 # Return the list. 264 return error
265 266
267 - def _return_value_general(self, spin, param, sim=None, bc=False):
268 """Return the value and error corresponding to the parameter 'param'. 269 270 If sim is set to an integer, return the value of the simulation and None. The values are taken from the given SpinContainer object. 271 272 273 @param spin: The SpinContainer object. 274 @type spin: SpinContainer 275 @param param: The name of the parameter to return values for. 276 @type param: str 277 @param sim: The Monte Carlo simulation index. 278 @type sim: None or int 279 @keyword bc: The back-calculated data flag. If True, then the back-calculated data will be returned rather than the actual data. 280 @type bc: bool 281 @return: The value and error corresponding to 282 @rtype: tuple of length 2 of floats or None 283 """ 284 285 # Initialise. 286 index = None 287 288 # Get the object name. 289 object_name = self.return_data_name(param) 290 291 # The error, simulation and back calculated names. 292 if object_name: 293 object_error = object_name + '_err' 294 object_sim = object_name + '_sim' 295 object_bc = object_name + '_bc' 296 key = None 297 298 # The data type does not exist. 299 else: 300 # Is it a spectrum id? 301 if hasattr(cdp, 'spectrum_ids') and param in cdp.spectrum_ids: 302 object_name = 'intensities' 303 object_error = 'intensity_err' 304 object_sim = 'intensity_sim' 305 object_bc = 'intensity_bc' 306 key = param 307 308 # Unknown data type. 309 else: 310 raise RelaxError("The parameter " + repr(param) + " does not exist.") 311 312 # Initial values. 313 value = None 314 error = None 315 316 # Switch to back calculated data. 317 if bc: 318 object_name = object_bc 319 320 # Value or sim value? 321 if sim != None: 322 object_name = object_sim 323 324 # The spin value. 325 if hasattr(spin, object_name): 326 value = getattr(spin, object_name) 327 328 # The error. 329 if hasattr(spin, object_error): 330 error = getattr(spin, object_error) 331 332 # The global value. 333 elif hasattr(cdp, object_name): 334 value = getattr(cdp, object_name) 335 336 # The error. 337 if hasattr(cdp, object_error): 338 error = getattr(cdp, object_error) 339 340 # List object. 341 if index != None: 342 value = value[index] 343 if error: 344 error = error[index] 345 346 # Dictionary object. 347 if key: 348 value = value[key] 349 if error: 350 error = error[key] 351 352 # Return the data. 353 if sim == None: 354 return value, error 355 else: 356 return value[sim], error
357 358
359 - def _set_error_spin(self, model_info, index, error):
360 """Set the parameter errors (spin system specific). 361 362 @param model_info: The spin container originating from model_loop(). 363 @type model_info: unknown 364 @param index: The index of the parameter to set the errors for. 365 @type index: int 366 @param error: The error value. 367 @type error: float 368 """ 369 370 # The spin container. 371 if not isinstance(model_info, SpinContainer): 372 raise RelaxError("The model information argument is not a spin container.") 373 spin = model_info 374 375 # Parameter increment counter. 376 inc = 0 377 378 # Loop over the residue specific parameters. 379 for param in self.data_names(set='params'): 380 # Return the parameter array. 381 if index == inc: 382 setattr(spin, param + "_err", error) 383 384 # Increment. 385 inc = inc + 1
386 387
388 - def _set_param_values_global(self, param=None, value=None, spin_id=None, force=True):
389 """Set the global parameter values in the top layer of the data pipe. 390 391 @keyword param: The parameter name list. 392 @type param: list of str 393 @keyword value: The parameter value list. 394 @type value: list 395 @keyword spin_id: The spin identification string (unused). 396 @type spin_id: None 397 @keyword force: A flag which if True will cause current values to be overwritten. If False, a RelaxError will raised if the parameter value is already set. 398 @type force: bool 399 """ 400 401 # Checks. 402 arg_check.is_str_list(param, 'parameter name') 403 arg_check.is_list(value, 'parameter value') 404 405 # Loop over the parameters. 406 for i in range(len(param)): 407 # Get the object's name. 408 obj_name = self.return_data_name(param[i]) 409 410 # Is the parameter is valid? 411 if not obj_name: 412 raise RelaxError("The parameter '%s' is not valid for this data pipe type." % param[i]) 413 414 # Is the parameter already set. 415 if not force and hasattr(cdp, obj_name) and getattr(cdp, obj_name) != None: 416 raise RelaxError("The parameter '%s' already exists, set the force flag to True to overwrite." % param[i]) 417 418 # Set the parameter. 419 setattr(cdp, obj_name, value[i])
420 421
422 - def _set_param_values_spin(self, param=None, value=None, spin_id=None, force=True):
423 """Set the spin specific parameter values. 424 425 @keyword param: The parameter name list. 426 @type param: list of str 427 @keyword value: The parameter value list. 428 @type value: list 429 @keyword spin_id: The spin identification string, only used for spin specific parameters. 430 @type spin_id: None or str 431 @keyword force: A flag which if True will cause current values to be overwritten. If False, a RelaxError will raised if the parameter value is already set. 432 @type force: bool 433 """ 434 435 # Checks. 436 arg_check.is_str_list(param, 'parameter name') 437 arg_check.is_list(value, 'parameter value') 438 439 # Loop over the parameters. 440 for i in range(len(param)): 441 # Is the parameter is valid? 442 if not self.PARAMS.contains(param[i]): 443 raise RelaxError("The parameter '%s' is not valid for this data pipe type." % param[i]) 444 445 # Spin loop. 446 for spin in spin_loop(spin_id): 447 # Skip deselected spins. 448 if not spin.select: 449 continue 450 451 # Set the parameter. 452 setattr(spin, param[i], value[i])
453 454
455 - def _set_selected_sim_global(self, model_info, select_sim):
456 """Set the simulation selection flag (for a single global model). 457 458 @param model_info: The model information originating from model_loop(). This should be zero for the single global model. 459 @type model_info: int 460 @param select_sim: The selection flag for the simulations. 461 @type select_sim: bool 462 """ 463 464 # Set the array. 465 cdp.select_sim = deepcopy(select_sim)
466 467
468 - def _set_selected_sim_spin(self, model_info, select_sim):
469 """Set the simulation selection flag (spin system specific). 470 471 @param model_info: The model information originating from model_loop(). 472 @type model_info: unknown 473 @param select_sim: The selection flag for the simulations. 474 @type select_sim: bool 475 """ 476 477 # The spin container. 478 if not isinstance(model_info, SpinContainer): 479 raise RelaxError("The model information argument is not a spin container.") 480 spin = model_info 481 482 # Set the array. 483 spin.select_sim = deepcopy(select_sim)
484 485
486 - def _set_update(self, param, spin):
487 """Dummy method to do nothing! 488 489 @param param: The name of the parameter which has been changed. 490 @type param: str 491 @param spin: The SpinContainer object. 492 @type spin: SpinContainer 493 """
494 495
496 - def _sim_init_values_spin(self):
497 """Initialise the Monte Carlo parameter values (spin system specific).""" 498 499 # Get the parameter object names. 500 param_names = self.data_names(set='params') 501 502 # Get the minimisation statistic object names. 503 min_names = self.data_names(set='min') 504 505 506 # Test if Monte Carlo parameter values have already been set. 507 ############################################################# 508 509 # Loop over the spins. 510 for spin in spin_loop(): 511 # Skip deselected spins. 512 if not spin.select: 513 continue 514 515 # Loop over all the parameter names. 516 for object_name in param_names: 517 # Name for the simulation object. 518 sim_object_name = object_name + '_sim' 519 520 # Test if the simulation object already exists. 521 if hasattr(spin, sim_object_name): 522 raise RelaxError("Monte Carlo parameter values have already been set.") 523 524 525 # Set the Monte Carlo parameter values. 526 ####################################### 527 528 # Loop over the residues. 529 for spin in spin_loop(): 530 # Skip deselected residues. 531 if not spin.select: 532 continue 533 534 # Loop over all the data names. 535 for object_name in param_names: 536 # Name for the simulation object. 537 sim_object_name = object_name + '_sim' 538 539 # Create the simulation object. 540 setattr(spin, sim_object_name, []) 541 542 # Get the simulation object. 543 sim_object = getattr(spin, sim_object_name) 544 545 # Loop over the simulations. 546 for j in xrange(cdp.sim_number): 547 # Copy and append the data. 548 sim_object.append(deepcopy(getattr(spin, object_name))) 549 550 # Loop over all the minimisation object names. 551 for object_name in min_names: 552 # Name for the simulation object. 553 sim_object_name = object_name + '_sim' 554 555 # Create the simulation object. 556 setattr(spin, sim_object_name, []) 557 558 # Get the simulation object. 559 sim_object = getattr(spin, sim_object_name) 560 561 # Loop over the simulations. 562 for j in xrange(cdp.sim_number): 563 # Copy and append the data. 564 sim_object.append(deepcopy(getattr(spin, object_name)))
565 566
567 - def _sim_pack_relax_data(self, data_id, sim_data):
568 """Pack the Monte Carlo simulation relaxation data into the corresponding spin container. 569 570 @param data_id: The spin identification string, as yielded by the base_data_loop() generator method. 571 @type data_id: str 572 @param sim_data: The Monte Carlo simulation data. 573 @type sim_data: list of float 574 """ 575 576 # Get the spin container. 577 spin = return_spin(data_id) 578 579 # Initialise the data structure. 580 spin.ri_data_sim = {} 581 582 # Loop over the relaxation data. 583 for i in range(len(cdp.ri_ids)): 584 # The ID. 585 ri_id = cdp.ri_ids[i] 586 587 # Initialise the MC data list. 588 spin.ri_data_sim[ri_id] = [] 589 590 # Loop over the simulations. 591 for j in range(cdp.sim_number): 592 spin.ri_data_sim[ri_id].append(sim_data[j][i])
593 594
595 - def _sim_return_chi2_spin(self, model_info, index=None):
596 """Return the simulation chi-squared values (spin system specific). 597 598 @param model_info: The model information originating from model_loop(). 599 @type model_info: unknown 600 @keyword index: The optional simulation index. 601 @type index: int 602 @return: The list of simulation chi-squared values. If the index is supplied, only a single value will be returned. 603 @rtype: list of float or float 604 """ 605 606 # The spin container. 607 if not isinstance(model_info, SpinContainer): 608 raise RelaxError("The model information argument is not a spin container.") 609 spin = model_info 610 611 # Index. 612 if index != None: 613 return spin.chi2_sim[index] 614 615 # List of vals. 616 else: 617 return spin.chi2_sim
618 619
620 - def _sim_return_param_spin(self, model_info, index):
621 """Return the array of simulation parameter values (spin system specific). 622 623 @param model_info: The model information originating from model_loop(). 624 @type model_info: unknown 625 @param index: The index of the parameter to return the array of values for. 626 @type index: int 627 @return: The array of simulation parameter values. 628 @rtype: list of float 629 """ 630 631 # The spin container. 632 if not isinstance(model_info, SpinContainer): 633 raise RelaxError("The model information argument is not a spin container.") 634 spin = model_info 635 636 # Parameter increment counter. 637 inc = 0 638 639 # Loop over the residue specific parameters. 640 for param in self.data_names(set='params'): 641 # Return the parameter array. 642 if index == inc: 643 return getattr(spin, param + "_sim") 644 645 # Increment. 646 inc = inc + 1
647 648
649 - def _sim_return_selected_global(self, model_info):
650 """Return the array of selected simulation flags for the global model. 651 652 @param model_info: The model information originating from model_loop(). This should be zero for the single global model. 653 @type model_info: int 654 @return: The array of selected simulation flags. 655 @rtype: list of int 656 """ 657 658 # Return the array. 659 return cdp.select_sim
660 661
662 - def _sim_return_selected_spin(self, model_info):
663 """Return the array of selected simulation flags (spin system specific). 664 665 @param model_info: The model information originating from model_loop(). 666 @type model_info: unknown 667 @return: The array of selected simulation flags. 668 @rtype: list of int 669 """ 670 671 # The spin container. 672 if not isinstance(model_info, SpinContainer): 673 raise RelaxError("The model information argument is not a spin container.") 674 spin = model_info 675 676 # Return the array. 677 return spin.select_sim
678 679
680 - def _test_grid_ops_general(self, lower=None, upper=None, inc=None, n=None):
681 """Test that the grid search options are reasonable. 682 683 @param lower: The lower bounds of the grid search which must be equal to the number of parameters in the model. 684 @type lower: array of numbers 685 @param upper: The upper bounds of the grid search which must be equal to the number of parameters in the model. 686 @type upper: array of numbers 687 @param inc: The increments for each dimension of the space for the grid search. The number of elements in the array must equal to the number of parameters in the model. 688 @type inc: array of int 689 @param n: The number of parameters in the model. 690 @type n: int 691 """ 692 693 # Lower bounds test. 694 if lower != None: 695 if len(lower) != n: 696 raise RelaxLenError('lower bounds', n) 697 698 # Upper bounds. 699 if upper != None: 700 if len(upper) != n: 701 raise RelaxLenError('upper bounds', n) 702 703 # Increment. 704 if isinstance(inc, list): 705 if len(inc) != n: 706 raise RelaxLenError('increment', n)
707