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

Source Code for Module generic_fns.pipes

  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  """Module for manipulating data pipes.""" 
 25   
 26   
 27  # Python module imports 
 28  import __builtin__ 
 29  import sys 
 30   
 31  # relax module imports. 
 32  from data import Relax_data_store; ds = Relax_data_store() 
 33  from dep_check import C_module_exp_fn, scipy_module 
 34  from relax_errors import RelaxError, RelaxNoPipeError, RelaxPipeError 
 35  from status import Status; status = Status() 
 36   
 37   
 38  # List of valid data pipe types and descriptions. 
 39  VALID_TYPES = ['ct', 'frame order', 'jw', 'hybrid', 'mf', 'N-state', 'noe', 'relax_fit'] 
 40  PIPE_DESC = { 
 41      'ct':  'Consistency testing', 
 42      'frame order':  'Frame Order theories', 
 43      'jw':  'Reduced spectral density mapping', 
 44      'hybrid':  'Special hybrid pipe', 
 45      'mf':  'Model-free analysis', 
 46      'N-state':  'N-state model of domain motions', 
 47      'noe':  'Steady state NOE calculation', 
 48      'relax_fit':  'Relaxation curve fitting' 
 49  } 
 50  PIPE_DESC_LIST = [] 
 51  for name in VALID_TYPES: 
 52      PIPE_DESC_LIST.append(PIPE_DESC[name]) 
 53   
 54   
55 -def copy(pipe_from=None, pipe_to=None):
56 """Copy the contents of the source data pipe to a new target data pipe. 57 58 If the 'pipe_from' argument is None then the current data pipe is assumed as the source. The 59 data pipe corresponding to 'pipe_to' cannot exist. 60 61 @param pipe_from: The name of the source data pipe to copy the data from. 62 @type pipe_from: str 63 @param pipe_to: The name of the target data pipe to copy the data to. 64 @type pipe_to: str 65 """ 66 67 # Test if the pipe already exists. 68 if pipe_to in list(ds.keys()): 69 raise RelaxPipeError(pipe_to) 70 71 # Acquire the pipe lock (data modifying function), and make sure it is finally released. 72 status.pipe_lock.acquire(sys._getframe().f_code.co_name) 73 try: 74 # The current data pipe. 75 if pipe_from == None: 76 pipe_from = cdp_name() 77 78 # Copy the data. 79 ds[pipe_to] = ds[pipe_from].__clone__() 80 81 # Release the lock. 82 finally: 83 status.pipe_lock.release(sys._getframe().f_code.co_name) 84 85 # Notify observers that a pipe change has occurred. 86 status.observers.pipe_alteration.notify()
87 88
89 -def create(pipe_name=None, pipe_type=None, switch=True):
90 """Create a new data pipe. 91 92 The current data pipe will be changed to this new data pipe. 93 94 95 @keyword pipe_name: The name of the new data pipe. 96 @type pipe_name: str 97 @keyword pipe_type: The new data pipe type which can be one of the following: 98 'ct': Consistency testing, 99 'frame order': The Frame Order theories. 100 'jw': Reduced spectral density mapping, 101 'hybrid': The hybridised data pipe. 102 'mf': Model-free analysis, 103 'N-state': N-state model of domain dynamics, 104 'noe': Steady state NOE calculation, 105 'relax_fit': Relaxation curve fitting, 106 'relax_disp': Relaxation dispersion, 107 @type pipe_type: str 108 @keyword switch: If True, this new pipe will be switched to, otherwise the current data pipe will remain as is. 109 @type switch: bool 110 """ 111 112 # Test if pipe_type is valid. 113 if not pipe_type in VALID_TYPES: 114 raise RelaxError("The data pipe type " + repr(pipe_type) + " is invalid and must be one of the strings in the list " + repr(VALID_TYPES) + ".") 115 116 # Test that the C modules have been loaded. 117 if pipe_type == 'relax_fit' and not C_module_exp_fn: 118 raise RelaxError("Relaxation curve fitting is not available. Try compiling the C modules on your platform.") 119 120 # Test that the scipy is installed for the frame order analysis. 121 if pipe_type == 'frame order' and not scipy_module: 122 raise RelaxError("The frame order analysis is not available. Please install the scipy Python package.") 123 124 # Acquire the pipe lock (data modifying function), and make sure it is finally released. 125 status.pipe_lock.acquire(sys._getframe().f_code.co_name) 126 try: 127 # Add the data pipe. 128 ds.add(pipe_name=pipe_name, pipe_type=pipe_type, switch=switch) 129 130 # Release the lock. 131 finally: 132 status.pipe_lock.release(sys._getframe().f_code.co_name)
133 134
135 -def cdp_name():
136 """Return the name of the current data pipe. 137 138 @return: The name of the current data pipe. 139 @rtype: str 140 """ 141 142 return ds.current_pipe
143 144
145 -def delete(pipe_name=None):
146 """Delete a data pipe. 147 148 @param pipe_name: The name of the data pipe to delete. 149 @type pipe_name: str 150 """ 151 152 # Acquire the pipe lock (data modifying function), and make sure it is finally released. 153 status.pipe_lock.acquire(sys._getframe().f_code.co_name) 154 try: 155 # Pipe name is supplied. 156 if pipe_name != None: 157 # Test if the data pipe exists. 158 test(pipe_name) 159 160 # Convert to a list. 161 pipes = [pipe_name] 162 163 # All pipes. 164 else: 165 pipes = ds.keys() 166 167 # Loop over the pipes. 168 for pipe in pipes: 169 # Delete the data pipe. 170 del ds[pipe] 171 172 # Set the current data pipe to None if it is the deleted data pipe. 173 if ds.current_pipe == pipe: 174 ds.current_pipe = None 175 __builtin__.cdp = None 176 177 # Release the lock. 178 finally: 179 status.pipe_lock.release(sys._getframe().f_code.co_name) 180 181 # Notify observers that the switch has occurred. 182 status.observers.pipe_alteration.notify()
183 184
185 -def display():
186 """Print the details of all the data pipes.""" 187 188 # Acquire the pipe lock, and make sure it is finally released. 189 status.pipe_lock.acquire(sys._getframe().f_code.co_name) 190 try: 191 # Heading. 192 print(("%-20s%-20s%-20s" % ("Data pipe name", "Data pipe type", "Current"))) 193 194 # Loop over the data pipes. 195 for pipe_name in ds: 196 # The current data pipe. 197 current = '' 198 if pipe_name == cdp_name(): 199 current = '*' 200 201 # Print out. 202 print("%-20s%-20s%-20s" % ("'"+pipe_name+"'", get_type(pipe_name), current)) 203 204 # Release the lock. 205 finally: 206 status.pipe_lock.release(sys._getframe().f_code.co_name)
207 208
209 -def get_pipe(name=None):
210 """Return a data pipe. 211 212 @keyword name: The name of the data pipe to return. If None, the current data pipe is 213 returned. 214 @type name: str or None 215 @return: The current data pipe. 216 @rtype: PipeContainer instance 217 """ 218 219 # The name of the data pipe. 220 if name == None: 221 name = cdp_name() 222 223 # Test if the data pipe exists. 224 test(name) 225 226 return ds[name]
227 228
229 -def get_type(name=None):
230 """Return the type of the data pipe. 231 232 @keyword name: The name of the data pipe. If None, the current data pipe is used. 233 @type name: str or None 234 @return: The current data pipe type. 235 @rtype: str 236 """ 237 238 # The name of the data pipe. 239 if name == None: 240 name = cdp_name() 241 242 # Get the data pipe. 243 pipe = get_pipe(name) 244 245 return pipe.pipe_type
246 247
248 -def has_pipe(name):
249 """Determine if the relax data store contains the data pipe. 250 251 @param name: The name of the data pipe. 252 @type name: str 253 @return: True if the data pipe exists, False otherwise. 254 @rtype: bool 255 """ 256 257 # Check. 258 if name in ds: 259 return True 260 else: 261 return False
262 263
264 -def pipe_loop(name=False):
265 """Generator function for looping over and yielding the data pipes. 266 267 @keyword name: A flag which if True will cause the name of the pipe to be returned. 268 @type name: bool 269 @return: The data pipes, and optionally the pipe names. 270 @rtype: PipeContainer instance or tuple of PipeContainer instance and str if name=True 271 """ 272 273 # Acquire the pipe lock, and make sure it is finally released. 274 status.pipe_lock.acquire(sys._getframe().f_code.co_name) 275 try: 276 # Loop over the keys. 277 for key in list(ds.keys()): 278 # Return the pipe and name. 279 if name: 280 yield ds[key], key 281 282 # Return just the pipe. 283 else: 284 yield ds[key] 285 286 # Release the lock. 287 finally: 288 status.pipe_lock.release(sys._getframe().f_code.co_name)
289 290
291 -def pipe_names():
292 """Return the list of all data pipes. 293 294 @return: The list of data pipes. 295 @rtype: list of str 296 """ 297 298 return list(ds.keys())
299 300
301 -def switch(pipe_name=None):
302 """Switch the current data pipe to the given data pipe. 303 304 @param pipe_name: The name of the data pipe to switch to. 305 @type pipe_name: str 306 """ 307 308 # Acquire the pipe lock (data modifying function), and make sure it is finally released. 309 status.pipe_lock.acquire(sys._getframe().f_code.co_name) 310 try: 311 # Test if the data pipe exists. 312 test(pipe_name) 313 314 # Switch the current data pipe. 315 ds.current_pipe = pipe_name 316 __builtin__.cdp = get_pipe() 317 318 # Release the lock. 319 finally: 320 status.pipe_lock.release(sys._getframe().f_code.co_name) 321 322 # Notify observers that the switch has occurred. 323 status.observers.pipe_alteration.notify()
324 325
326 -def test(pipe_name=None):
327 """Function for testing the existence of the current or supplied data pipe. 328 329 @param pipe_name: The name of the data pipe to switch to. 330 @type pipe_name: str 331 @return: The answer to the question of whether the pipe exists. 332 @rtype: Boolean 333 """ 334 335 # No supplied data pipe and no current data pipe. 336 if pipe_name == None: 337 # Get the current pipe. 338 pipe_name = cdp_name() 339 340 # Still no luck. 341 if pipe_name == None: 342 raise RelaxNoPipeError 343 344 # Test if the data pipe exists. 345 if pipe_name not in ds: 346 raise RelaxNoPipeError(pipe_name)
347