Package specific_analyses :: Package jw_mapping :: Module api
[hide private]
[frames] | no frames]

Source Code for Module specific_analyses.jw_mapping.api

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2002-2005,2007-2014 Edward d'Auvergne                         # 
  4  # Copyright (C) 2006 Chris MacRaild                                           # 
  5  # Copyright (C) 2008 Sebastien Morin                                          # 
  6  #                                                                             # 
  7  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  8  #                                                                             # 
  9  # This program is free software: you can redistribute it and/or modify        # 
 10  # it under the terms of the GNU General Public License as published by        # 
 11  # the Free Software Foundation, either version 3 of the License, or           # 
 12  # (at your option) any later version.                                         # 
 13  #                                                                             # 
 14  # This program is distributed in the hope that it will be useful,             # 
 15  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 16  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 17  # GNU General Public License for more details.                                # 
 18  #                                                                             # 
 19  # You should have received a copy of the GNU General Public License           # 
 20  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 21  #                                                                             # 
 22  ############################################################################### 
 23   
 24  # Module docstring. 
 25  """The reduced spectral density mapping API object.""" 
 26   
 27  # Python module imports. 
 28  from warnings import warn 
 29   
 30  # relax module imports. 
 31  from lib.errors import RelaxError, RelaxNoSequenceError, RelaxNoValueError, RelaxSpinTypeError 
 32  from lib.float import isInf 
 33  from lib.periodic_table import periodic_table 
 34  from lib.physical_constants import h_bar, mu0 
 35  from lib.warnings import RelaxDeselectWarning 
 36  from pipe_control.interatomic import return_interatom_list 
 37  from pipe_control.mol_res_spin import exists_mol_res_spin_data, return_spin, spin_loop 
 38  from specific_analyses.api_base import API_base 
 39  from specific_analyses.api_common import API_common 
 40  from specific_analyses.jw_mapping.parameter_object import Jw_mapping_params 
 41  from target_functions.jw_mapping import Mapping 
 42   
 43   
44 -class Jw_mapping(API_base, API_common):
45 """Class containing functions specific to reduced spectral density mapping.""" 46 47 # Class variable for storing the class instance (for the singleton design pattern). 48 instance = None 49
50 - def __init__(self):
51 """Initialise the class by placing API_common methods into the API.""" 52 53 # Place methods into the API. 54 self.base_data_loop = self._base_data_loop_spin 55 self.create_mc_data = self._create_mc_relax_data 56 self.model_loop = self._model_loop_spin 57 self.print_model_title = self._print_model_title_spin 58 self.return_conversion_factor = self._return_no_conversion_factor 59 self.return_error = self._return_error_relax_data 60 self.return_value = self._return_value_general 61 self.set_param_values = self._set_param_values_spin 62 self.set_selected_sim = self._set_selected_sim_spin 63 self.sim_pack_data = self._sim_pack_relax_data 64 65 # Place a copy of the parameter list object in the instance namespace. 66 self._PARAMS = Jw_mapping_params()
67 68
69 - def calculate(self, spin_id=None, scaling_matrix=None, verbosity=1, sim_index=None):
70 """Calculation of the spectral density values. 71 72 @keyword spin_id: The spin identification string. 73 @type spin_id: None or str 74 @keyword scaling_matrix: The per-model list of diagonal and square scaling matrices. 75 @type scaling_matrix: list of numpy rank-2, float64 array or list of None 76 @keyword verbosity: The amount of information to print. The higher the value, the greater the verbosity. 77 @type verbosity: int 78 @keyword sim_index: The optional MC simulation index. 79 @type sim_index: None or int 80 """ 81 82 # Test if the frequency has been set. 83 if not hasattr(cdp, 'jw_frq') or not isinstance(cdp.jw_frq, float): 84 raise RelaxError("The frequency has not been set up.") 85 86 # Test if the sequence data is loaded. 87 if not exists_mol_res_spin_data(): 88 raise RelaxNoSequenceError 89 90 # Test if the spin data has been set. 91 for spin, id in spin_loop(spin_id, return_id=True): 92 # Skip deselected spins. 93 if not spin.select: 94 continue 95 96 # Test if the nuclear isotope type has been set. 97 if not hasattr(spin, 'isotope'): 98 raise RelaxSpinTypeError 99 100 # Test if the CSA value has been set. 101 if not hasattr(spin, 'csa') or spin.csa == None: 102 raise RelaxNoValueError("CSA") 103 104 # Test the interatomic data. 105 interatoms = return_interatom_list(spin_hash=spin._hash) 106 for interatom in interatoms: 107 # No relaxation mechanism. 108 if not interatom.dipole_pair: 109 continue 110 111 # The interacting spin. 112 if id != interatom.spin_id1: 113 spin_id2 = interatom.spin_id1 114 else: 115 spin_id2 = interatom.spin_id2 116 spin2 = return_spin(spin_id=spin_id2) 117 118 # Test if the nuclear isotope type has been set. 119 if not hasattr(spin2, 'isotope'): 120 raise RelaxSpinTypeError 121 122 # Test if the interatomic distance has been set. 123 if not hasattr(interatom, 'r') or interatom.r == None: 124 raise RelaxNoValueError("interatomic distance", spin_id=spin_id, spin_id2=spin_id2) 125 126 # Frequency index. 127 if cdp.jw_frq not in list(cdp.spectrometer_frq.values()): 128 raise RelaxError("No relaxation data corresponding to the frequency " + repr(cdp.jw_frq) + " has been loaded.") 129 130 # Reduced spectral density mapping. 131 for spin, id in spin_loop(spin_id, return_id=True): 132 # Skip deselected spins. 133 if not spin.select: 134 continue 135 136 # Set the r1, r2, and NOE to None. 137 r1 = None 138 r2 = None 139 noe = None 140 141 # Get the R1, R2, and NOE values corresponding to the set frequency. 142 for ri_id in cdp.ri_ids: 143 # The frequency does not match. 144 if cdp.spectrometer_frq[ri_id] != cdp.jw_frq: 145 continue 146 147 # R1. 148 if cdp.ri_type[ri_id] == 'R1': 149 if sim_index == None: 150 r1 = spin.ri_data[ri_id] 151 else: 152 r1 = spin.ri_data_sim[ri_id][sim_index] 153 154 # R2. 155 if cdp.ri_type[ri_id] == 'R2': 156 if sim_index == None: 157 r2 = spin.ri_data[ri_id] 158 else: 159 r2 = spin.ri_data_sim[ri_id][sim_index] 160 161 # NOE. 162 if cdp.ri_type[ri_id] == 'NOE': 163 if sim_index == None: 164 noe = spin.ri_data[ri_id] 165 else: 166 noe = spin.ri_data_sim[ri_id][sim_index] 167 168 # Skip the spin if not all of the three value exist. 169 if r1 == None or r2 == None or noe == None: 170 continue 171 172 # Loop over the interatomic data. 173 interatoms = return_interatom_list(spin_hash=spin._hash) 174 for i in range(len(interatoms)): 175 # No relaxation mechanism. 176 if not interatoms[i].dipole_pair: 177 continue 178 179 # The surrounding spins. 180 if id != interatoms[i].spin_id1: 181 spin_id2 = interatoms[i].spin_id1 182 else: 183 spin_id2 = interatoms[i].spin_id2 184 spin2 = return_spin(spin_id=spin_id2) 185 186 # Gyromagnetic ratios. 187 gx = periodic_table.gyromagnetic_ratio(spin.isotope) 188 gh = periodic_table.gyromagnetic_ratio(spin2.isotope) 189 190 # The interatomic distance. 191 r = interatoms[i].r 192 193 # Initialise the function to calculate. 194 jw = Mapping(frq=cdp.jw_frq, gx=gx, gh=gh, mu0=mu0, h_bar=h_bar) 195 196 # Calculate the spectral density values. 197 j0, jwx, jwh = jw.func(r=r, csa=spin.csa, r1=r1, r2=r2, noe=noe) 198 199 # Reduced spectral density values. 200 if sim_index == None: 201 spin.j0 = j0 202 spin.jwx = jwx 203 spin.jwh = jwh 204 205 # Monte Carlo simulated reduced spectral density values. 206 else: 207 # Initialise the simulation data structures. 208 self.data_init(id, sim=1) 209 if spin.j0_sim == None: 210 spin.j0_sim = [] 211 spin.jwx_sim = [] 212 spin.jwh_sim = [] 213 214 # Reduced spectral density values. 215 spin.j0_sim.append(j0) 216 spin.jwx_sim.append(jwx) 217 spin.jwh_sim.append(jwh)
218 219
220 - def data_init(self, data, sim=False):
221 """Initialise the data structures. 222 223 @param data: The spin ID string from the _base_data_loop_spin() method. 224 @type data: str 225 @keyword sim: The Monte Carlo simulation flag, which if true will initialise the simulation data structure. 226 @type sim: bool 227 """ 228 229 # Get the spin container. 230 spin = return_spin(spin_id=data) 231 232 # Get the data names. 233 data_names = self.data_names() 234 235 # Loop over the data structure names. 236 for name in data_names: 237 # Simulation data structures. 238 if sim: 239 # Add '_sim' to the names. 240 name = name + '_sim' 241 242 # If the name is not in the spin container, add it. 243 if not hasattr(spin, name): 244 # Set the attribute. 245 setattr(spin, name, None)
246 247
248 - def get_param_names(self, model_info=None):
249 """Return a vector of parameter names. 250 251 @keyword model_info: The spin container and the spin ID string from the _model_loop_spin() method. 252 @type model_info: SpinContainer instance, str 253 @return: The vector of parameter names. 254 @rtype: list of str 255 """ 256 257 # Return the fixed list. 258 return ['j0', 'jwx', 'jwh']
259 260
261 - def overfit_deselect(self, data_check=True, verbose=True):
262 """Deselect spins which have insufficient data to support calculation. 263 264 @keyword data_check: A flag to signal if the presence of base data is to be checked for. 265 @type data_check: bool 266 @keyword verbose: A flag which if True will allow printouts. 267 @type verbose: bool 268 """ 269 270 # Print out. 271 if verbose: 272 print("\nOver-fit spin deselection:") 273 274 # Test if sequence data exists. 275 if not exists_mol_res_spin_data(): 276 raise RelaxNoSequenceError 277 278 # Loop over spin data. 279 deselect_flag = False 280 spin_count = 0 281 for spin, spin_id in spin_loop(return_id=True): 282 # Skip deselected spins. 283 if not spin.select: 284 continue 285 286 # The interatomic data. 287 interatoms = return_interatom_list(spin_hash=spin._hash) 288 289 # Loop over the interatomic data. 290 dipole_relax = False 291 for i in range(len(interatoms)): 292 # No dipolar relaxation mechanism. 293 if not interatoms[i].dipole_pair: 294 continue 295 296 # The surrounding spins. 297 if spin_id != interatoms[i].spin_id1: 298 spin_id2 = interatoms[i].spin_id1 299 else: 300 spin_id2 = interatoms[i].spin_id2 301 spin2 = return_spin(spin_id=spin_id2) 302 303 # Dipolar relaxation flag. 304 dipole_relax = True 305 306 # No relaxation mechanism. 307 if not dipole_relax or not hasattr(spin, 'csa') or spin.csa == None: 308 warn(RelaxDeselectWarning(spin_id, 'an absence of relaxation mechanisms')) 309 spin.select = False 310 deselect_flag = True 311 continue 312 313 # Data checks. 314 if data_check: 315 # The number of relaxation data points (and for infinite data). 316 data_points = 0 317 inf_data = False 318 if hasattr(cdp, 'ri_ids') and hasattr(spin, 'ri_data'): 319 for id in cdp.ri_ids: 320 if id in spin.ri_data and spin.ri_data[id] != None: 321 data_points += 1 322 323 # Infinite data! 324 if isInf(spin.ri_data[id]): 325 inf_data = True 326 327 # Infinite data. 328 if inf_data: 329 warn(RelaxDeselectWarning(spin_id, 'infinite relaxation data')) 330 spin.select = False 331 deselect_flag = True 332 continue 333 334 # Relaxation data must exist! 335 if not hasattr(spin, 'ri_data'): 336 warn(RelaxDeselectWarning(spin_id, 'missing relaxation data')) 337 spin.select = False 338 deselect_flag = True 339 continue 340 341 # Require 3 or more relaxation data points. 342 if data_points < 3: 343 warn(RelaxDeselectWarning(spin_id, 'insufficient relaxation data, 3 or more data points are required')) 344 spin.select = False 345 deselect_flag = True 346 continue 347 348 # Increment the spin number. 349 spin_count += 1 350 351 # No spins selected, so fail hard to prevent the user from going any further. 352 if spin_count == 0: 353 warn(RelaxWarning("No spins are selected therefore the optimisation or calculation cannot proceed.")) 354 355 # Final printout. 356 if verbose and not deselect_flag: 357 print("No spins have been deselected.")
358 359
360 - def set_error(self, index, error, model_info=None):
361 """Set the parameter errors. 362 363 @param index: The index of the parameter to set the errors for. 364 @type index: int 365 @param error: The error value. 366 @type error: float 367 @keyword model_info: The spin container and the spin ID string from the _model_loop_spin() method. 368 @type model_info: SpinContainer instance, str 369 """ 370 371 # Unpack the data. 372 spin, spin_id = model_info 373 374 # Return J(0) sim data. 375 if index == 0: 376 spin.j0_err = error 377 378 # Return J(wX) sim data. 379 if index == 1: 380 spin.jwx_err = error 381 382 # Return J(wH) sim data. 383 if index == 2: 384 spin.jwh_err = error
385 386
387 - def sim_return_param(self, index, model_info=None):
388 """Return the array of simulation parameter values. 389 390 @param index: The index of the parameter to return the array of values for. 391 @type index: int 392 @keyword model_info: The spin container and the spin ID string from the _model_loop_spin() method. 393 @type model_info: SpinContainer instance, str 394 @return: The array of simulation parameter values. 395 @rtype: list of float 396 """ 397 398 # Unpack the data. 399 spin, spin_id = model_info 400 401 # Skip deselected spins. 402 if not spin.select: 403 return 404 405 # Return J(0) sim data. 406 if index == 0: 407 return spin.j0_sim 408 409 # Return J(wX) sim data. 410 if index == 1: 411 return spin.jwx_sim 412 413 # Return J(wH) sim data. 414 if index == 2: 415 return spin.jwh_sim
416 417
418 - def sim_return_selected(self, model_info=None):
419 """Return the array of selected simulation flags. 420 421 @keyword model_info: The spin container and the spin ID string from the _model_loop_spin() method. 422 @type model_info: SpinContainer instance, str 423 @return: The array of selected simulation flags. 424 @rtype: list of int 425 """ 426 427 # Unpack the data. 428 spin, spin_id = model_info 429 430 # Multiple spins. 431 return spin.select_sim
432