Module relax
[hide private]
[frames] | no frames]

Source Code for Module relax

  1  ############################################################################### 
  2  #                                                                             # 
  3  #                                    relax                                    # 
  4  #                                                                             # 
  5  #               Protein dynamics by NMR relaxation data analysis              # 
  6  #                                                                             # 
  7  #                             by Edward d'Auvergne                            # 
  8  #                                                                             # 
  9  ############################################################################### 
 10  #                                                                             # 
 11  #                                   Licence                                   # 
 12  #                                                                             # 
 13  # relax, a program for relaxation data analysis.                              # 
 14  #                                                                             # 
 15  # Copyright (C) 2001-2013  Edward d'Auvergne                                  # 
 16  # Copyright (C) 2006-2013  the relax development team                         # 
 17  #                                                                             # 
 18  # This program is free software; you can redistribute it and/or modify        # 
 19  # it under the terms of the GNU General Public License as published by        # 
 20  # the Free Software Foundation, either version 3 of the License, or           # 
 21  # (at your option) any later version.                                         # 
 22  #                                                                             # 
 23  # This program is distributed in the hope that it will be useful,             # 
 24  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 25  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 26  # GNU Library General Public License for more details.                        # 
 27  #                                                                             # 
 28  # You should have received a copy of the GNU General Public License           # 
 29  # along with this program; if not, write to the Free Software                 # 
 30  #                                                                             # 
 31  ############################################################################### 
 32   
 33  # Module docstring. 
 34  """The main module for relax execution.""" 
 35   
 36  # Set up the Python 2 and 3 work-arounds. 
 37  import compat 
 38   
 39  # Dependency checks. 
 40  import dep_check 
 41   
 42  # Python modules. 
 43  import numpy 
 44  from optparse import Option, OptionParser 
 45  from os import F_OK, access, getpid, putenv 
 46  if dep_check.cprofile_module: 
 47      import cProfile as profile 
 48  elif dep_check.profile_module: 
 49      import profile 
 50  import pstats 
 51  from pydoc import pager 
 52  import sys 
 53   
 54  # relax modules. 
 55  from info import Info_box 
 56  import lib.errors 
 57  from lib.io import io_streams_log, io_streams_tee 
 58  import lib.warnings 
 59  from multi import Application_callback, load_multiprocessor 
 60  from prompt import interpreter 
 61  from status import Status; status = Status() 
 62  import user_functions 
 63  import version 
 64   
 65   
 66  # Set up the user functions. 
 67  user_functions.initialise() 
 68   
 69  # Modify the environmental variables. 
 70  putenv('PDBVIEWER', 'vmd') 
 71   
 72   
73 -def start(mode=None, profile_flag=False):
74 """Execute relax. 75 76 @keyword mode: Force a relax mode, overriding the command line. 77 @type mode: str 78 @keyword profile_flag: Change this flag to True for code profiling. 79 @type profile_flag: bool 80 """ 81 82 # Normal relax operation. 83 relax = Relax() 84 85 # Override normal operation. 86 if mode: 87 # Override the mode. 88 relax.mode = mode 89 90 # Some defaults. 91 relax.script_file = None 92 relax.log_file = None 93 relax.tee_file = None 94 relax.multiprocessor_type = 'uni' 95 relax.n_processors = 1 96 97 # Process the command line arguments. 98 else: 99 relax.arguments() 100 101 # Store some start up info in the status object. 102 status.relax_mode = relax.mode 103 104 # Set up the multi-processor elements. 105 callbacks = Application_callback(master=relax) 106 verbosity = 0 107 if status.debug: 108 verbosity = 1 109 processor = load_multiprocessor(relax.multiprocessor_type, callbacks, processor_size=relax.n_processors, verbosity=verbosity) 110 111 # Place the processor fabric intro string into the info box. 112 info = Info_box() 113 info.multi_processor_string = processor.get_intro_string() 114 115 # Normal relax operation. 116 if not profile_flag: 117 # Execute relax in multi-processor mode (this includes the uni-processor for normal operation). 118 processor.run() 119 120 # relax in profiling mode. 121 else: 122 def print_stats(stats, status=0): 123 pstats.Stats(stats).sort_stats('time', 'name').print_stats()
124 125 # No profile module. 126 if not dep_check.profile_module: 127 sys.stderr.write("The profile module is not available, please install the Python development packages for profiling.\n\n") 128 sys.exit() 129 130 # Run relax in profiling mode. 131 profile.Profile.print_stats = print_stats 132 profile.runctx('processor.run()', globals(), locals()) 133 134 135
136 -class Relax:
137 """The main relax class. 138 139 This contains information about the running state, for example the mode of operation of relax, 140 whether debugging is turned on, etc. 141 """ 142
143 - def __init__(self):
144 """The top level class for initialising the program. 145 146 @keyword mode: Force a relax mode, overriding the command line. 147 @type mode: str 148 """ 149 150 # Get and store the PID of this process. 151 self.pid = getpid()
152 153
154 - def run(self):
155 """Execute relax. 156 157 This is the application callback method executed by the multi-processor framework. 158 """ 159 160 # Set up the warning system. 161 lib.warnings.setup() 162 163 # Show the version number and exit. 164 if self.mode == 'version': 165 print('relax ' + version.version_full()) 166 return 167 168 # Show the relax info and exit. 169 if self.mode == 'info': 170 # Initialise the information box. 171 info = Info_box() 172 173 # Print the program intro. 174 print(info.intro_text()) 175 176 # Print the system info. 177 print(info.sys_info()) 178 179 # Stop execution. 180 return 181 182 # Logging. 183 if self.log_file: 184 io_streams_log(self.log_file) 185 186 # Tee. 187 elif self.tee_file: 188 io_streams_tee(self.tee_file) 189 190 # Run the interpreter for the prompt or script modes. 191 if self.mode == 'prompt' or self.mode == 'script': 192 # Run the interpreter. 193 self.interpreter = interpreter.Interpreter() 194 self.interpreter.run(self.script_file) 195 196 # Execute the relax GUI. 197 elif self.mode == 'gui': 198 # Dependency check. 199 if not dep_check.wx_module: 200 sys.stderr.write("Please install the wx Python module to access the relax GUI.\n\n") 201 return 202 203 # Only import the module in this mode (to improve program start up speeds). 204 import gui 205 206 # Set the GUI flag in the status object. 207 status.show_gui = True 208 209 # Start the relax GUI wx application. 210 app = gui.App(script_file=self.script_file) 211 app.MainLoop() 212 213 # Execute the relax test suite 214 elif self.mode == 'test suite': 215 # Only import the module in the test modes (to improve program start up speeds). 216 from test_suite.test_suite_runner import Test_suite_runner 217 218 # Load the interpreter and turn intros on. 219 self.interpreter = interpreter.Interpreter(show_script=False, quit=False, raise_relax_error=True) 220 self.interpreter.on() 221 222 # Run the tests. 223 runner = Test_suite_runner(self.tests, timing=self.test_timings) 224 runner.run_all_tests() 225 226 # Execute the relax system tests. 227 elif self.mode == 'system tests': 228 # Only import the module in the test modes (to improve program start up speeds). 229 from test_suite.test_suite_runner import Test_suite_runner 230 231 # Load the interpreter and turn intros on. 232 self.interpreter = interpreter.Interpreter(show_script=False, quit=False, raise_relax_error=True) 233 self.interpreter.on() 234 235 # Run the tests. 236 runner = Test_suite_runner(self.tests, timing=self.test_timings) 237 runner.run_system_tests() 238 239 # Execute the relax unit tests. 240 elif self.mode == 'unit tests': 241 # Only import the module in the test modes (to improve program start up speeds). 242 from test_suite.test_suite_runner import Test_suite_runner 243 244 # Run the tests. 245 runner = Test_suite_runner(self.tests, timing=self.test_timings) 246 runner.run_unit_tests() 247 248 # Execute the relax GUI tests. 249 elif self.mode == 'GUI tests': 250 # Only import the module in the test modes (to improve program start up speeds). 251 from test_suite.test_suite_runner import Test_suite_runner 252 253 # Run the tests. 254 runner = Test_suite_runner(self.tests, timing=self.test_timings) 255 runner.run_gui_tests() 256 257 # Test mode. 258 elif self.mode == 'test': 259 self.test_mode() 260 261 # Licence mode. 262 elif self.mode == 'licence': 263 self.licence() 264 265 # Unknown mode. 266 else: 267 raise lib.errors.RelaxError("The '%s' mode is unknown." % self.mode)
268 269
270 - def arguments(self):
271 """Process the command line arguments.""" 272 273 # Parser object. 274 parser = RelaxParser(self, usage="usage: %prog [options] [script_file]") 275 276 # Recognised command line options. 277 parser.add_option('-d', '--debug', action='store_true', dest='debug', default=0, help='enable debugging output') 278 parser.add_option('-l', '--log', action='store', type='string', dest='log', help='log relax output to the file LOG_FILE', metavar='LOG_FILE') 279 parser.add_option('--licence', action='store_true', dest='licence', default=0, help='display the licence') 280 parser.add_option('-t', '--tee', action='store', type='string', dest='tee', help='tee relax output to stdout and the file LOG_FILE', metavar='LOG_FILE') 281 parser.add_option('-g', '--gui', action='store_true', dest='gui', default=0, help='launch the relax GUI') 282 parser.add_option('-p', '--pedantic', action='store_true', dest='pedantic', default=0, help='escalate all warnings to errors') 283 parser.add_option('--test', action='store_true', dest='test', default=0, help='run relax in test mode') 284 parser.add_option('-x', '--test-suite', action='store_true', dest='test_suite', default=0, help='execute the relax test suite') 285 parser.add_option('-s', '--system-tests', action='store_true', dest='system_tests', default=0, help='execute the relax system/functional tests (part of the test suite)') 286 parser.add_option('-u', '--unit-tests', action='store_true', dest='unit_tests', default=0, help='execute the relax unit tests (part of the test suite)') 287 parser.add_option('--gui-tests', action='store_true', dest='gui_tests', default=0, help='execute the relax GUI tests (part of the test suite)') 288 parser.add_option('--time', action='store_true', dest='tt', default=0, help='enable the timing of individual tests in the test suite') 289 parser.add_option('-i', '--info', action='store_true', dest='info', default=0, help='display information about this version of relax') 290 parser.add_option('-v', '--version', action='store_true', dest='version', default=0, help='show the version number and exit') 291 parser.add_option('-m', '--multi', action='store', type='string', dest='multiprocessor', default='uni', help='set multi processor method') 292 parser.add_option('-n', '--processors', action='store', type='int', dest='n_processors', default=-1, help='set number of processors (may be ignored)') 293 parser.add_option('--numpy-raise', action='store_true', dest='numpy_raise', default=0, help='convert numpy warnings to errors') 294 295 # Parse the options. 296 (options, args) = parser.parse_args() 297 298 # Debugging flag (and numpy warning to error conversion). 299 if options.debug: 300 status.debug = True 301 if options.numpy_raise: 302 numpy.seterr(all='raise') 303 304 # Pedantic flag. 305 if options.pedantic: 306 status.pedantic = True 307 308 # Logging. 309 if options.log: 310 # Exclusive modes. 311 if options.tee: 312 parser.error("the logging and tee options cannot be set simultaneously") 313 314 # The log file. 315 self.log_file = options.log 316 317 # Fail if the file already exists. 318 if access(self.log_file, F_OK): 319 parser.error("the log file " + repr(self.log_file) + " already exists") 320 else: 321 self.log_file = None 322 323 # Tee. 324 if options.tee: 325 # Exclusive modes. 326 if options.log: 327 parser.error("the tee and logging options cannot be set simultaneously") 328 329 # The tee file. 330 self.tee_file = options.tee 331 332 # Fail if the file already exists. 333 if access(self.tee_file, F_OK): 334 parser.error("the tee file " + repr(self.tee_file) + " already exists") 335 else: 336 self.tee_file = None 337 338 # Test suite mode, therefore the args are the tests to run and not a script file. 339 if options.test_suite or options.system_tests or options.unit_tests or options.gui_tests: 340 # Store the arguments. 341 self.tests = args 342 343 # Test timings. 344 self.test_timings = False 345 if options.tt: 346 self.test_timings = True 347 348 # The argument is a script. 349 else: 350 # Number of positional arguments should only be 0 or 1. 1 should be the script file. 351 if len(args) > 1: 352 parser.error("incorrect number of arguments") 353 354 # Script file. 355 self.script_file = None 356 if len(args) == 1: 357 self.script_file = args[0] 358 359 # Test if the script file exists. 360 if not access(self.script_file, F_OK): 361 parser.error("the script file " + repr(self.script_file) + " does not exist") 362 363 # Set the multi-processor type and number. 364 self.multiprocessor_type = options.multiprocessor 365 self.n_processors = options.n_processors 366 367 # Checks for the multiprocessor mode. 368 if self.multiprocessor_type == 'mpi4py' and not dep_check.mpi4py_module: 369 parser.error(dep_check.mpi4py_message) 370 371 372 # Determine the relax mode and test for mutually exclusive modes. 373 ################################################################# 374 375 # Show the version number. 376 if options.version: 377 self.mode = 'version' 378 379 # Show the info about this relax version. 380 elif options.info: 381 self.mode = 'info' 382 383 # Run the relax tests. 384 elif options.test_suite or options.system_tests or options.unit_tests or options.gui_tests: 385 # Exclusive modes. 386 if options.test: 387 parser.error("executing the relax test suite and running relax in test mode are mutually exclusive") 388 elif options.licence: 389 parser.error("executing the relax test suite and running relax in licence mode are mutually exclusive") 390 391 # Set the mode. 392 if options.test_suite: 393 self.mode = 'test suite' 394 elif options.system_tests: 395 self.mode = 'system tests' 396 elif options.unit_tests: 397 self.mode = 'unit tests' 398 elif options.gui_tests: 399 self.mode = 'GUI tests' 400 401 # Set the status flag. 402 status.test_mode = True 403 404 # Test mode. 405 elif options.test: 406 # Make sure no script is supplied. 407 if self.script_file: 408 parser.error("a script should not be supplied in test mode") 409 410 # Exclusive modes. 411 if options.test_suite or options.system_tests or options.unit_tests or options.gui_tests: 412 parser.error("the relax test mode and executing the test suite are mutually exclusive") 413 elif options.licence: 414 parser.error("the relax modes test and licence are mutually exclusive") 415 416 # Set the mode. 417 self.mode = 'test' 418 419 # Licence mode. 420 elif options.licence: 421 # Make sure no script is supplied. 422 if self.script_file: 423 parser.error("a script should not be supplied in test mode") 424 425 # Exclusive modes. 426 if options.test_suite or options.system_tests or options.unit_tests or options.gui_tests: 427 parser.error("the relax licence mode and executing the test suite are mutually exclusive") 428 elif options.test: 429 parser.error("the relax modes licence and test are mutually exclusive") 430 431 # Set the mode. 432 self.mode = 'licence' 433 434 # GUI. 435 elif options.gui: 436 # Exclusive models. 437 if options.test_suite or options.system_tests or options.unit_tests or options.gui_tests: 438 parser.error("the relax GUI mode and testing modes are mutually exclusive") 439 elif options.licence: 440 parser.error("the relax GUI mode and licence mode are mutually exclusive") 441 442 # Missing wx module. 443 if not dep_check.wx_module: 444 parser.error("To use the GUI, the wx python module must be installed.") 445 446 # Set the mode. 447 self.mode = 'gui' 448 449 # Script mode. 450 elif self.script_file: 451 self.mode = 'script' 452 453 # Prompt mode (default). 454 else: 455 self.mode = 'prompt'
456 457
458 - def licence(self):
459 """Function for displaying the licence.""" 460 461 # Present the GPL using paging. 462 file = open('docs/COPYING') 463 pager(file.read())
464 465
466 - def test_mode(self):
467 """Relax test mode code.""" 468 469 # Don't actually do anything. 470 return
471 472 473
474 -class RelaxParser(OptionParser):
475 - def __init__(self, relax, usage=None, option_list=None, option_class=Option, version=None, conflict_handler="error", description=None, formatter=None, add_help_option=1, prog=None):
476 """Subclassed OptionParser class with a replacement error function.""" 477 478 # Relax base class. 479 self.relax = relax 480 481 # Run the __init__ method of the OptionParser class. 482 OptionParser.__init__(self, usage, option_list, option_class, version, conflict_handler, description, formatter, add_help_option, prog)
483 484
485 - def error(self, message):
486 """Replacement error function.""" 487 488 # Usage message. 489 self.print_usage(sys.stderr) 490 491 # Raise a clean error. 492 try: 493 raise lib.errors.RelaxError(message) 494 except lib.errors.AllRelaxErrors: 495 instance = sys.exc_info()[1] 496 sys.stderr.write(instance.__str__()) 497 498 # Exit. 499 sys.exit()
500 501 502 # Start relax if this file is passed to Python. 503 if __name__ == "__main__": 504 start() 505