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