Package generic_fns :: Module value
[hide private]
[frames] | no frames]

Source Code for Module generic_fns.value

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2009 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  """Module for the manipulation of parameter and constant values.""" 
 25   
 26  # Python module imports. 
 27  from numpy import ndarray 
 28  import sys 
 29   
 30  # relax module imports. 
 31  from generic_fns import minimise, pipes 
 32  from generic_fns.mol_res_spin import exists_mol_res_spin_data, generate_spin_id_data_array, return_spin, spin_loop 
 33  from relax_errors import RelaxError, RelaxNoSequenceError, RelaxNoSpinError, RelaxParamSetError, RelaxValueError 
 34  from relax_io import get_file_path, open_write_file, read_spin_data, write_spin_data 
 35  import specific_fns 
 36  from status import Status; status = Status() 
 37   
 38   
39 -def copy(pipe_from=None, pipe_to=None, param=None):
40 """Copy spin specific data values from pipe_from to pipe_to. 41 42 @param pipe_from: The data pipe to copy the value from. This defaults to the current data 43 pipe. 44 @type pipe_from: str 45 @param pipe_to: The data pipe to copy the value to. This defaults to the current data pipe. 46 @type pipe_to: str 47 @param param: The name of the parameter to copy the values of. 48 @type param: str 49 """ 50 51 # The current data pipe. 52 if pipe_from == None: 53 pipe_from = pipes.cdp_name() 54 if pipe_to == None: 55 pipe_to = pipes.cdp_name() 56 57 # The second pipe does not exist. 58 pipes.test(pipe_to) 59 60 # Test if the sequence data for pipe_from is loaded. 61 if not exists_mol_res_spin_data(pipe_from): 62 raise RelaxNoSequenceError(pipe_from) 63 64 # Test if the sequence data for pipe_to is loaded. 65 if not exists_mol_res_spin_data(pipe_to): 66 raise RelaxNoSequenceError(pipe_to) 67 68 # Specific value and error returning function. 69 return_value = specific_fns.setup.get_specific_fn('return_value', pipes.get_type(pipe_from)) 70 71 # Test if the data exists for pipe_to. 72 for spin in spin_loop(pipe_to): 73 # Get the value and error for pipe_to. 74 value, error = return_value(spin, param) 75 76 # Data exists. 77 if value != None or error != None: 78 raise RelaxValueError(param, pipe_to) 79 80 # Copy the values. 81 for spin, spin_id in spin_loop(pipe_from, return_id=True): 82 # Get the value and error from pipe_from. 83 value, error = return_value(spin, param) 84 85 # Get the equivalent spin in pipe_to. 86 spin_to = return_spin(spin_id, pipe_to) 87 88 # Set the values of pipe_to. 89 set(spin_id=spin_to, value=value, error=error, param=param) 90 91 # Reset all minimisation statistics. 92 minimise.reset_min_stats(pipe_to)
93 94
95 -def display(param=None):
96 """Display spin specific data values. 97 98 @param param: The name of the parameter to display. 99 @type param: str 100 """ 101 102 # Test if the current pipe exists. 103 pipes.test() 104 105 # Test if the sequence data is loaded. 106 if not exists_mol_res_spin_data(): 107 raise RelaxNoSequenceError 108 109 # Print the data. 110 write_data(param, sys.stdout)
111 112
113 -def partition_params(val, param):
114 """Function for sorting and partitioning the parameters and their values. 115 116 The two major partitions are the tensor parameters and the spin specific parameters. 117 118 @param val: The parameter values. 119 @type val: None, number, or list of numbers 120 @param param: The parameter names. 121 @type param: None, str, or list of str 122 @return: A tuple, of length 4, of lists. The first and second elements are the lists of 123 spin specific parameters and values respectively. The third and forth elements 124 are the lists of all other parameters and their values. 125 @rtype: tuple of 4 lists 126 """ 127 128 # Specific functions. 129 is_spin_param = specific_fns.setup.get_specific_fn('is_spin_param', pipes.get_type()) 130 131 # Initialise. 132 spin_params = [] 133 spin_values = [] 134 other_params = [] 135 other_values = [] 136 137 # Single parameter. 138 if isinstance(param, str): 139 # Spin specific parameter. 140 if is_spin_param(param): 141 params = spin_params 142 values = spin_values 143 144 # Other parameters. 145 else: 146 params = other_params 147 values = other_values 148 149 # List of values. 150 if isinstance(val, list) or isinstance(val, ndarray): 151 # Parameter name. 152 for i in xrange(len(val)): 153 params.append(param) 154 155 # Parameter value. 156 values = val 157 158 # Single value. 159 else: 160 # Parameter name. 161 params.append(param) 162 163 # Parameter value. 164 values.append(val) 165 166 # Multiple parameters. 167 elif isinstance(param, list): 168 # Loop over all parameters. 169 for i in xrange(len(param)): 170 # Spin specific parameter. 171 if is_spin_param(param[i]): 172 params = spin_params 173 values = spin_values 174 175 # Other parameters. 176 else: 177 params = other_params 178 values = other_values 179 180 # Parameter name. 181 params.append(param[i]) 182 183 # Parameter value. 184 if isinstance(val, list) or isinstance(val, ndarray): 185 values.append(val[i]) 186 else: 187 values.append(val) 188 189 190 # Return the partitioned parameters and values. 191 return spin_params, spin_values, other_params, other_values
192 193
194 -def read(param=None, scaling=1.0, file=None, dir=None, file_data=None, spin_id_col=None, mol_name_col=None, res_num_col=None, res_name_col=None, spin_num_col=None, spin_name_col=None, data_col=None, error_col=None, sep=None, spin_id=None):
195 """Read spin specific data values from a file. 196 197 @keyword param: The name of the parameter to read. 198 @type param: str 199 @keyword scaling: A scaling factor by which all read values are multiplied by. 200 @type scaling: float 201 @keyword file: The name of the file to open. 202 @type file: str 203 @keyword dir: The directory containing the file (defaults to the current directory if None). 204 @type dir: str or None 205 @keyword file_data: An alternative to opening a file, if the data already exists in the correct format. The format is a list of lists where the first index corresponds to the row and the second the column. 206 @type file_data: list of lists 207 @keyword spin_id_col: The column containing the spin ID strings. If supplied, the mol_name_col, res_name_col, res_num_col, spin_name_col, and spin_num_col arguments must be none. 208 @type spin_id_col: int or None 209 @keyword mol_name_col: The column containing the molecule name information. If supplied, spin_id_col must be None. 210 @type mol_name_col: int or None 211 @keyword res_name_col: The column containing the residue name information. If supplied, spin_id_col must be None. 212 @type res_name_col: int or None 213 @keyword res_num_col: The column containing the residue number information. If supplied, spin_id_col must be None. 214 @type res_num_col: int or None 215 @keyword spin_name_col: The column containing the spin name information. If supplied, spin_id_col must be None. 216 @type spin_name_col: int or None 217 @keyword spin_num_col: The column containing the spin number information. If supplied, spin_id_col must be None. 218 @type spin_num_col: int or None 219 @keyword data_col: The column containing the RDC data in Hz. 220 @type data_col: int or None 221 @keyword error_col: The column containing the RDC errors. 222 @type error_col: int or None 223 @keyword sep: The column separator which, if None, defaults to whitespace. 224 @type sep: str or None 225 @keyword spin_id: The spin ID string. 226 @type spin_id: None or str 227 """ 228 229 # Test if the current pipe exists. 230 pipes.test() 231 232 # Test if sequence data is loaded. 233 if not exists_mol_res_spin_data(): 234 raise RelaxNoSequenceError 235 236 # Minimisation parameter. 237 if minimise.return_data_name(param): 238 # Minimisation statistic flag. 239 min_stat = True 240 241 # Specific value and error returning function. 242 return_value = minimise.return_value 243 244 # Specific set function. 245 set_fn = minimise.set 246 247 # Normal parameter. 248 else: 249 # Minimisation statistic flag. 250 min_stat = False 251 252 # Specific v 253 return_value = specific_fns.setup.get_specific_fn('return_value', pipes.get_type()) 254 255 # Specific set function. 256 set_fn = set 257 258 # Test data corresponding to param already exists. 259 for spin in spin_loop(): 260 # Skip deselected spins. 261 if not spin.select: 262 continue 263 264 # Get the value and error. 265 value, error = return_value(spin, param) 266 267 # Data exists. 268 if value != None or error != None: 269 raise RelaxValueError(param) 270 271 # Loop over the data. 272 for data in read_spin_data(file=file, dir=dir, file_data=file_data, spin_id_col=spin_id_col, mol_name_col=mol_name_col, res_num_col=res_num_col, res_name_col=res_name_col, spin_num_col=spin_num_col, spin_name_col=spin_name_col, data_col=data_col, error_col=error_col, sep=sep, spin_id=spin_id): 273 # Unpack. 274 if data_col and error_col: 275 id, value, error = data 276 elif data_col: 277 id, value = data 278 else: 279 id, error = data 280 281 # Set the value. 282 set_fn(val=value, error=error, param=param, spin_id=id) 283 284 # Reset the minimisation statistics. 285 if not min_stat: 286 minimise.reset_min_stats()
287 288
289 -def set(val=None, param=None, error=None, pipe=None, spin_id=None, force=True, reset=True):
290 """Set global or spin specific data values. 291 292 @keyword val: The parameter values. 293 @type val: None or list 294 @keyword param: The parameter names. 295 @type param: None, str, or list of str 296 @keyword error: The parameter errors. 297 @type error: None, number, or list of numbers 298 @keyword pipe: The data pipe the values should be placed in. 299 @type pipe: None or str 300 @keyword spin_id: The spin identification string. 301 @type spin_id: str 302 @keyword force: A flag forcing the overwriting of current values. 303 @type force: bool 304 @keyword reset: A flag which if True will cause all minimisation statistics to be reset. 305 @type reset: bool 306 """ 307 308 # Switch to the data pipe, storing the original. 309 if pipe: 310 orig_pipe = pipes.cdp_name() 311 pipes.switch(pipe) 312 313 # Test if the current data pipe exists. 314 pipes.test() 315 316 # Specific functions. 317 default_value = specific_fns.setup.get_specific_fn('default_value', pipes.get_type()) 318 get_param_names = specific_fns.setup.get_specific_fn('get_param_names', pipes.get_type()) 319 return_data_name = specific_fns.setup.get_specific_fn('return_data_name', pipes.get_type()) 320 set_param_values = specific_fns.setup.get_specific_fn('set_param_values', pipes.get_type()) 321 322 # Convert numpy arrays to lists, if necessary. 323 if isinstance(val, ndarray): 324 val = val.tolist() 325 326 # Invalid combinations. 327 if (isinstance(val, float) or isinstance(val, int)) and param == None: 328 raise RelaxError("The combination of a single value '%s' without specifying the parameter name is invalid." % val) 329 if isinstance(val, list) and isinstance(param, str): 330 raise RelaxError("Invalid combination: When multiple values '%s' are specified, either no parameters or a list of parameters must by supplied rather than the single parameter '%s'." % (val, param)) 331 332 # Get the parameter list if needed. 333 if param == None: 334 param = get_param_names() 335 336 # Convert the param to a list if needed. 337 if not isinstance(param, list): 338 param = [param] 339 340 # Convert the value to a list if needed. 341 if val != None and not isinstance(val, list): 342 val = [val] * len(param) 343 344 # Default values. 345 if val == None: 346 # Loop over the parameters, getting the default values. 347 val = [] 348 for i in range(len(param)): 349 val.append(default_value(return_data_name(param[i]))) 350 351 # Check that there is a default. 352 if val[-1] == None: 353 raise RelaxParamSetError(param[i]) 354 355 # Set the parameter values. 356 set_param_values(param=param, value=val, spin_id=spin_id, force=force) 357 358 # Reset all minimisation statistics. 359 if reset: 360 minimise.reset_min_stats() 361 362 # Switch back. 363 if pipe: 364 pipes.switch(orig_pipe)
365 366
367 -def write(param=None, file=None, dir=None, bc=False, force=False, return_value=None):
368 """Write data to a file. 369 370 @keyword param: The name of the parameter to write to file. 371 @type param: str 372 @keyword file: The file to write the data to. 373 @type file: str 374 @keyword dir: The name of the directory to place the file into (defaults to the 375 current directory). 376 @type dir: str 377 @keyword bc: A flag which if True will cause the back calculated values to be written. 378 @type bc: bool 379 @keyword force: A flag which if True will cause any pre-existing file to be overwritten. 380 @type force: bool 381 @keyword return_value: An optional function which if supplied will override the default value 382 returning function. 383 @type return_value: None or func 384 """ 385 386 # Test if the current pipe exists. 387 pipes.test() 388 389 # Test if the sequence data is loaded. 390 if not exists_mol_res_spin_data(): 391 raise RelaxNoSequenceError 392 393 # Open the file for writing. 394 file_path = get_file_path(file, dir) 395 file = open_write_file(file, dir, force) 396 397 # Write the data. 398 write_data(param, file, bc, return_value) 399 400 # Close the file. 401 file.close() 402 403 # Add the file to the results file list. 404 if not hasattr(cdp, 'result_files'): 405 cdp.result_files = [] 406 cdp.result_files.append(['text', 'Text', file_path]) 407 status.observers.result_file.notify()
408 409
410 -def write_data(param=None, file=None, bc=False, return_value=None):
411 """The function which actually writes the data. 412 413 @keyword param: The parameter to write. 414 @type param: str 415 @keyword file: The file to write the data to. 416 @type file: str 417 @keyword bc: A flag which if True will cause the back calculated values to be written. 418 @type bc: bool 419 @keyword return_value: An optional function which if supplied will override the default value returning function. 420 @type return_value: None or func 421 """ 422 423 # Get the value and error returning function if required. 424 if not return_value: 425 return_value = specific_fns.setup.get_specific_fn('return_value', pipes.get_type()) 426 427 # Format string. 428 format = "%-30s%-30s" 429 430 # Init the data. 431 mol_names = [] 432 res_nums = [] 433 res_names = [] 434 spin_nums = [] 435 spin_names = [] 436 values = [] 437 errors = [] 438 439 # Loop over the sequence. 440 for spin, mol_name, res_num, res_name in spin_loop(full_info=True): 441 # Get the value and error. 442 value, error = return_value(spin, param, bc=bc) 443 444 # Append the data. 445 mol_names.append(mol_name) 446 res_nums.append(res_num) 447 res_names.append(res_name) 448 spin_nums.append(spin.num) 449 spin_names.append(spin.name) 450 values.append(value) 451 errors.append(error) 452 453 # Write the data. 454 write_spin_data(file, mol_names=mol_names, res_nums=res_nums, res_names=res_names, spin_nums=spin_nums, spin_names=spin_names, data=values, data_name='value', error=errors, error_name='error')
455