Package specific_analyses :: Package relax_disp :: Module model
[hide private]
[frames] | no frames]

Source Code for Module specific_analyses.relax_disp.model

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2014 Troels E. Linnet                                         # 
  4  # Copyright (C) 2014-2015 Edward d'Auvergne                                   # 
  5  #                                                                             # 
  6  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  7  #                                                                             # 
  8  # This program is free software: you can redistribute it and/or modify        # 
  9  # it under the terms of the GNU General Public License as published by        # 
 10  # the Free Software Foundation, either version 3 of the License, or           # 
 11  # (at your option) any later version.                                         # 
 12  #                                                                             # 
 13  # This program is distributed in the hope that it will be useful,             # 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 16  # GNU General Public License for more details.                                # 
 17  #                                                                             # 
 18  # You should have received a copy of the GNU General Public License           # 
 19  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Module docstring. 
 24  """Functions for sorting and nesting the models in relaxation dispersion specific analysis.""" 
 25   
 26  # Python module imports. 
 27  from datetime import date 
 28  from functools import partial 
 29  from operator import attrgetter, ne 
 30   
 31  # relax module imports. 
 32  from lib.dispersion.variables import EQ_ANALYTIC, EQ_NUMERIC, EQ_SILICO, EXP_TYPE_CPMG_MMQ, EXP_TYPE_R1RHO, EXP_TYPE_CPMG_SQ, EXP_TYPE_NOREX, EXP_TYPE_R2EFF, MODEL_DESC, MODEL_EQ, MODEL_EXP_TYPE, MODEL_NEST, MODEL_PARAMS, MODEL_PARAMS_LM63, MODEL_PARAMS_LM63_3SITE, MODEL_PARAMS_NS_MMQ_2SITE, MODEL_PARAMS_NS_MMQ_3SITE, MODEL_PARAMS_NS_MMQ_3SITE_LINEAR, MODEL_PARAMS_NS_R1RHO_2SITE, MODEL_PARAMS_NS_R1RHO_3SITE, MODEL_PARAMS_NS_R1RHO_3SITE_LINEAR, MODEL_SITES, MODEL_YEAR, PARAMS_R20 
 33  from specific_analyses.relax_disp.data import is_r1_optimised 
 34   
 35   
 36  # Define class for describing the model. 
 37  # This class is defined to be able to make better sorting of the models. 
38 -class Model_class:
39 - def __init__(self, model=None):
40 """Class for storing model information. 41 42 @keyword model: Current model 43 @type model: str 44 """ 45 46 # Save the info to variables. 47 self.model = model 48 49 # model description. 50 self.desc = MODEL_DESC[self.model] 51 52 # model equation type: analytic, silico or numeric. 53 self.eq = MODEL_EQ[self.model] 54 55 # The model experiment type. 56 self.exp_type = MODEL_EXP_TYPE[self.model] 57 58 # model parameters. 59 self.params = MODEL_PARAMS[self.model] 60 if is_r1_optimised(model=model) and 'r1' not in self.params: 61 self.params.insert(0, 'r1') 62 63 # model number of parameters. 64 self.params_nr = len(self.params) 65 66 # The number of chemical sites. 67 self.sites = MODEL_SITES[self.model] 68 69 # year where model was developed or published. 70 self.year = MODEL_YEAR[self.model] 71 72 # Ordered lists of models to nest from. 73 nest_list = MODEL_NEST[self.model] 74 75 # Remove the model itself from the list. 76 if nest_list == None: 77 self.nest_list = nest_list 78 else: 79 nest_list = list(filter(partial(ne, self.model), nest_list)) 80 self.nest_list = nest_list 81 82 # Define the order of how exp type ranks. 83 order_exp_type = [EXP_TYPE_R2EFF, EXP_TYPE_NOREX, EXP_TYPE_CPMG_SQ, EXP_TYPE_CPMG_MMQ, EXP_TYPE_R1RHO] 84 85 # Save the index of current model to order of equation type. 86 self.exp_type_i = order_exp_type.index(self.exp_type) 87 88 # Define the order of how equation type ranks. 89 order_eq = [EQ_NUMERIC, EQ_SILICO, EQ_ANALYTIC] 90 91 # Save the index of current model to order of equation type. 92 self.eq_i = order_eq.index(self.eq) 93 94 # Define the order of how equation type ranks, when sorting before auto analyses. 95 order_s = [EQ_SILICO, EQ_ANALYTIC, EQ_NUMERIC] 96 97 # Save the index of current model to order of equation type. 98 self.eq_s = order_s.index(self.eq) 99 100 # Save the difference in year from now, to implemented model. 101 self.year_diff = date.today().year - self.year
102 103 # Make a readable presentation of the class object. Here a tuple.
104 - def __repr__(self):
105 return repr((self.model, self.desc, self.exp_type, self.eq, self.sites, self.year, self.params, self.params_nr))
106 107 108 # Define function, to return model info.
109 -def models_info(models=None):
110 """Get model info for list of models. 111 112 @keyword model: The list of all models analysed. 113 @type model: list of str 114 @return: List of tuples, where each tuple contains model info. 115 @rtype: List of tuples with str. 116 """ 117 118 # Define list of return. 119 models_info = [] 120 121 # Loop over models. 122 for model in models: 123 # Append to the list, the class instance of model info. 124 models_info.append(Model_class(model=model)) 125 126 # Return the list of model info. 127 return models_info
128 129 130 # Define function, to determine which model to nest from.
131 -def nesting_model(self_models=None, model=None):
132 """Determine if the current model can use nested results from any of the previous analysed models. 133 134 @keyword self_models: The list of all models analysed. 135 @type self_models: list of str 136 @keyword model: The current model to analyse. 137 @type model: str 138 @return: The current model info, the possible nest model info. 139 @rtype: class, class 140 """ 141 142 143 # Get the list index for the current model in all models. 144 model_index = self_models.index(model) 145 146 # Define the list of models which can be tested. This is the number of models which have already been tested. 147 completed_models = self_models[:model_index] 148 149 # Get the current models information. 150 model_info = models_info([model])[0] 151 152 # Get the info of the completed models. 153 completed_models_info = models_info(completed_models) 154 155 # Sort the models according to: exp_type, equation type, chemical sites, year for model, number of parameters. 156 completed_models_info = sorted(completed_models_info, key=attrgetter('exp_type_i', 'eq_i', 'sites', 'year_diff', 'params_nr')) 157 158 # If no nest model list is specified, return None. 159 if model_info.nest_list == None: 160 return model_info, None 161 162 else: 163 # Loop over ordered list of possible nested models. 164 for nest_model in model_info.nest_list: 165 # Then loop over list of completed models, and its associated information. 166 for completed_model_info in completed_models_info: 167 # If the nested model is in list of completed models, then return that model. 168 if nest_model == completed_model_info.model: 169 return model_info, completed_model_info 170 171 # If nothing is found, return None. 172 return model_info, None
173 174 175 # Define function, to determine which parameters to nest/copy over.
176 -def nesting_param(model_params=None, nested_model_params=None):
177 """Determine the conversion from the nested models params, to the current model params. 178 179 @keyword model_params: The list of the current model parameters. 180 @type model_params: list of str 181 @keyword nested_model_params: The list of the nested model parameters. 182 @type nested_model_params: list of str 183 @return: A dictionary of parameter conversion for the current model params. 184 @rtype: dictionary 185 """ 186 187 # Define dictionary. 188 par_dic = {} 189 190 # Loop over the parameters in the model parameters. 191 for param in model_params: 192 # The R20 parameters. 193 if param in PARAMS_R20: 194 # If both models have same parameter. 195 if param in nested_model_params: 196 par_dic[param] = param 197 198 # If copying from a simple model to a complex model. 199 elif param == 'r2a' and 'r2' in nested_model_params: 200 par_dic[param] = 'r2' 201 202 elif param == 'r2b' and 'r2' in nested_model_params: 203 par_dic[param] = 'r2' 204 205 # If copying from a complex model to a simple model. 206 elif param == 'r2' and 'r2a' in nested_model_params: 207 par_dic[param] = 'r2a' 208 209 # All other parameters. 210 elif param in nested_model_params: 211 par_dic[param] = param 212 213 else: 214 par_dic[param] = None 215 216 ## The LM63 3-site model parameters. 217 if set(model_params) == set(MODEL_PARAMS_LM63_3SITE) and set(nested_model_params) == set(MODEL_PARAMS_LM63): 218 for param in model_params: 219 if param == 'phi_ex_B': 220 par_dic[param] = 'phi_ex' 221 222 elif param == 'phi_ex_C': 223 par_dic[param] = 'phi_ex' 224 225 elif param == 'kB': 226 par_dic[param] = 'kex' 227 228 elif param == 'kC': 229 par_dic[param] = 'kex' 230 231 ## The 'MODEL_PARAMS_NS_R1RHO_3SITE' model parameters from 'MODEL_PARAMS_NS_R1RHO_3SITE_LINEAR'. 232 elif set(model_params) == set(MODEL_PARAMS_NS_R1RHO_3SITE) and set(nested_model_params) == set(MODEL_PARAMS_NS_R1RHO_3SITE_LINEAR): 233 for param in model_params: 234 if param == 'kex_AC': 235 par_dic[param] = '0.0' 236 237 ## The 'MODEL_PARAMS_NS_R1RHO_3SITE_LINEAR' model parameters from R1RHO 2SITE. 238 elif set(model_params) == set(MODEL_PARAMS_NS_R1RHO_3SITE_LINEAR) and set(nested_model_params) == set(MODEL_PARAMS_NS_R1RHO_2SITE): 239 for param in model_params: 240 if param == 'dw_AB': 241 par_dic[param] = 'dw' 242 243 elif param == 'kex_AB': 244 par_dic[param] = 'kex' 245 246 elif param == 'dw_BC': 247 par_dic[param] = 'dw' 248 249 elif param == 'kex_BC': 250 par_dic[param] = 'kex' 251 252 elif param == 'pB': 253 par_dic[param] = '1 - pA' 254 255 ## The 'MODEL_PARAMS_NS_R1RHO_3SITE' model parameters from R1RHO 2SITE. 256 elif set(model_params) == set(MODEL_PARAMS_NS_R1RHO_3SITE) and set(nested_model_params) == set(MODEL_PARAMS_NS_R1RHO_2SITE): 257 for param in model_params: 258 if param == 'dw_AB': 259 par_dic[param] = 'dw' 260 261 elif param == 'kex_AB': 262 par_dic[param] = 'kex' 263 264 elif param == 'dw_BC': 265 par_dic[param] = 'dw' 266 267 elif param == 'kex_BC': 268 par_dic[param] = 'kex' 269 270 elif param == 'kex_AC': 271 par_dic[param] = 'kex' 272 273 elif param == 'pB': 274 par_dic[param] = '1 - pA' 275 276 ## The 'MODEL_PARAMS_NS_MMQ_3SITE' model parameters from 'MODEL_PARAMS_NS_MMQ_3SITE_LINEAR'. 277 elif set(model_params) == set(MODEL_PARAMS_NS_MMQ_3SITE) and set(nested_model_params) == set(MODEL_PARAMS_NS_MMQ_3SITE_LINEAR): 278 for param in model_params: 279 if param == 'kex_AC': 280 par_dic[param] = '0.0' 281 282 ## The 'MODEL_PARAMS_NS_MMQ_3SITE_LINEAR' model parameters from 'MODEL_PARAMS_NS_MMQ_2'. 283 elif set(model_params) == set(MODEL_PARAMS_NS_MMQ_3SITE_LINEAR) and set(nested_model_params) == set(MODEL_PARAMS_NS_MMQ_2SITE): 284 for param in model_params: 285 if param == 'dw_AB': 286 par_dic[param] = 'dw' 287 288 elif param == 'dwH_AB': 289 par_dic[param] = 'dwH' 290 291 elif param == 'kex_AB': 292 par_dic[param] = 'kex' 293 294 elif param == 'dw_BC': 295 par_dic[param] = 'dw' 296 297 elif param == 'dwH_BC': 298 par_dic[param] = 'dwH' 299 300 elif param == 'kex_BC': 301 par_dic[param] = 'kex' 302 303 elif param == 'pB': 304 par_dic[param] = '1 - pA' 305 306 ## The 'MODEL_PARAMS_NS_MMQ_3SITE' model parameters from 'MODEL_PARAMS_NS_MMQ_2'. 307 elif set(model_params) == set(MODEL_PARAMS_NS_MMQ_3SITE) and set(nested_model_params) == set(MODEL_PARAMS_NS_MMQ_2SITE): 308 for param in model_params: 309 if param == 'dw_AB': 310 par_dic[param] = 'dw' 311 312 elif param == 'dwH_AB': 313 par_dic[param] = 'dwH' 314 315 elif param == 'kex_AB': 316 par_dic[param] = 'kex' 317 318 elif param == 'dw_BC': 319 par_dic[param] = 'dw' 320 321 elif param == 'dwH_BC': 322 par_dic[param] = 'dwH' 323 324 elif param == 'kex_BC': 325 par_dic[param] = 'kex' 326 327 elif param == 'kex_AC': 328 par_dic[param] = 'kex' 329 330 elif param == 'pB': 331 par_dic[param] = '1 - pA' 332 333 # Return the dictionary of conversion. 334 return par_dic
335 336 337 # Define function, to sort models.
338 -def sort_models(models=None):
339 """Determine how to order the models for analyses. 340 341 @keyword models: The list of all models to be analysed. 342 @type models: list of str 343 @return: The ordered list how models should be analysed. 344 @rtype: list of str 345 """ 346 347 # Get the info of the models selected for analysis. 348 all_models_info = models_info(models) 349 350 # Sort the models according to: exp_type, equation type, chemical sites, year for model, number of parameters. 351 all_models_info_sorted = sorted(all_models_info, key=attrgetter('exp_type_i', 'eq_s', 'sites', 'year_diff', 'params_nr')) 352 353 # Define list of sorted models. 354 sorted_models = [] 355 356 # Loop over the models info, and extract model. 357 for model_info in all_models_info_sorted: 358 sorted_models.append(model_info.model) 359 360 # Return sorted list of models. 361 return sorted_models
362