Commit 68af17b7ce93746cac84ddd2323c393c608e4d4c
1 parent
e57c8cc3
Implement quickstart tutorials test and avoid plotting on screen
Showing
2 changed files
with
114 additions
and
23 deletions
doc/source/users/tutorials/1dc/show_onoff_spectrum.py
| ... | ... | @@ -154,16 +154,18 @@ def show_spectrum_butterfly(): |
| 154 | 154 | plt.grid() |
| 155 | 155 | |
| 156 | 156 | # Set usage string |
| 157 | - usage = 'show_onoff_spectrum.py [-t title] [specfile] [butterfile]' | |
| 157 | + usage = 'show_onoff_spectrum.py [-p plotfile] [-t title] [specfile] [butterfile]' | |
| 158 | 158 | |
| 159 | 159 | # Set default options |
| 160 | - options = [{'option': '-t', 'value': 'Cassiopeia A'}] | |
| 160 | + options = [{'option': '-p', 'value': ''}, | |
| 161 | + {'option': '-t', 'value': 'Cassiopeia A'}] | |
| 161 | 162 | |
| 162 | 163 | # Get arguments and options from command line arguments |
| 163 | 164 | args, options = cscripts.ioutils.get_args_options(options, usage) |
| 164 | 165 | |
| 165 | 166 | # Extract script parameters from options |
| 166 | - title = options[0]['value'] | |
| 167 | + plotfile = options[0]['value'] | |
| 168 | + title = options[1]['value'] | |
| 167 | 169 | |
| 168 | 170 | # Set filenames |
| 169 | 171 | specfile = 'spectrum.fits' |
| ... | ... | @@ -184,7 +186,10 @@ def show_spectrum_butterfly(): |
| 184 | 186 | plt.title(title) |
| 185 | 187 | |
| 186 | 188 | # Show figure |
| 187 | - plt.show() | |
| 189 | + if len(plotfile) > 0: | |
| 190 | + plt.savefig(plotfile) | |
| 191 | + else: | |
| 192 | + plt.show() | |
| 188 | 193 | |
| 189 | 194 | # Return |
| 190 | 195 | return | ... | ... |
test/tutorials.py
| ... | ... | @@ -119,7 +119,7 @@ class tutorials(gammalib.GPythonTestSuite): |
| 119 | 119 | # Extract command |
| 120 | 120 | command = gammalib.strip_whitespace(cmdline[0]) |
| 121 | 121 | |
| 122 | - # Handle commands | |
| 122 | + # Handle "mkdir" command | |
| 123 | 123 | if command == 'mkdir': |
| 124 | 124 | self.test_value(len(cmdline), 2, 'Test command "mkdir".') |
| 125 | 125 | self.test_value(len(args), 0, 'Test that no lines follow "mkdir".') |
| ... | ... | @@ -127,6 +127,7 @@ class tutorials(gammalib.GPythonTestSuite): |
| 127 | 127 | dirname = gammalib.strip_whitespace(cmdline[1]) |
| 128 | 128 | os.system('mkdir -p %s' % dirname) |
| 129 | 129 | |
| 130 | + # Handle "cd" command | |
| 130 | 131 | elif command == 'cd': |
| 131 | 132 | self.test_value(len(cmdline), 2, 'Test command "cd".') |
| 132 | 133 | self.test_value(len(args), 0, 'Test that no lines follow "cd".') |
| ... | ... | @@ -134,6 +135,7 @@ class tutorials(gammalib.GPythonTestSuite): |
| 134 | 135 | dirname = gammalib.strip_whitespace(cmdline[1]) |
| 135 | 136 | os.chdir(dirname) |
| 136 | 137 | |
| 138 | + # Handle "nano" command | |
| 137 | 139 | elif command == 'nano': |
| 138 | 140 | self.test_value(len(cmdline), 2, 'Test command "nano".') |
| 139 | 141 | self.test_assert((len(args) > 0), 'Test that lines follow "nano".') |
| ... | ... | @@ -144,26 +146,68 @@ class tutorials(gammalib.GPythonTestSuite): |
| 144 | 146 | f.write(arg) |
| 145 | 147 | f.close() |
| 146 | 148 | |
| 149 | + # Handle special plotting scripts | |
| 147 | 150 | elif command[0:2] == './': |
| 148 | - scriptname = command[2:] | |
| 149 | - self.test_assert((len(args) == 0), 'Test script "%s".' % scriptname) | |
| 150 | - tool = '%s/%s' % (self._rst_path, scriptname) | |
| 151 | 151 | |
| 152 | - # Debug: show tool | |
| 153 | - #print tool.split(' '), | |
| 152 | + # Build command line | |
| 153 | + script = '%s/%s' % (self._rst_path, command[2:]) | |
| 154 | + plotname, _ = os.path.splitext(command[2:]) | |
| 155 | + cmd = '%s -p %s.png' % (script, plotname) | |
| 156 | + self.test_assert((len(args) == 0), 'Test script "%s".' % command[2:]) | |
| 157 | + | |
| 158 | + # Execute command, make sure we catch any exception | |
| 159 | + try: | |
| 160 | + rc = os.system(cmd+' > tutorials_test_output.dmp 2>&1') | |
| 161 | + except: | |
| 162 | + rc = -1 | |
| 163 | + | |
| 164 | + # If an exception occured then put output on screen | |
| 165 | + msg = '' | |
| 166 | + if rc != 0: | |
| 167 | + f = open('tutorials_test_output.dmp', 'r') | |
| 168 | + for line in f: | |
| 169 | + msg += line | |
| 170 | + f.close() | |
| 171 | + | |
| 172 | + # Remove test output | |
| 173 | + os.remove('tutorials_test_output.dmp') | |
| 154 | 174 | |
| 155 | - # Create subprocess for tool | |
| 156 | - p = subprocess.Popen(tool.split(' '), stdout=subprocess.PIPE, | |
| 157 | - stdin=subprocess.PIPE, | |
| 158 | - stderr=subprocess.PIPE) | |
| 175 | + # Set error flag and text | |
| 176 | + name = 'Test execution of %s.' % command[2:] | |
| 177 | + self.test_value(rc, 0, name, msg) | |
| 178 | + | |
| 179 | + # Handle plotting scripts | |
| 180 | + elif command[0:7] == '$CTOOLS': | |
| 181 | + | |
| 182 | + # Build command line | |
| 183 | + script = gammalib.expand_env(cmdline[0]) | |
| 184 | + filename = cmdline[1] | |
| 185 | + plotname, _ = os.path.splitext(filename) | |
| 186 | + cmd = '%s -p %s.png %s' % (script, plotname, filename) | |
| 187 | + self.test_assert((len(args) == 0), 'Test script "%s".' % command) | |
| 188 | + | |
| 189 | + # Execute command, make sure we catch any exception | |
| 190 | + try: | |
| 191 | + rc = os.system(cmd+' > tutorials_test_output.dmp 2>&1') | |
| 192 | + except: | |
| 193 | + rc = -1 | |
| 194 | + | |
| 195 | + # If an exception occured then put output on screen | |
| 196 | + msg = '' | |
| 197 | + if rc != 0: | |
| 198 | + f = open('tutorials_test_output.dmp', 'r') | |
| 199 | + for line in f: | |
| 200 | + msg += line | |
| 201 | + f.close() | |
| 159 | 202 | |
| 160 | - # Get results | |
| 161 | - res = p.communicate() | |
| 203 | + # Remove test output | |
| 204 | + os.remove('tutorials_test_output.dmp') | |
| 162 | 205 | |
| 163 | 206 | # Set error flag and text |
| 164 | - name = 'Test execution of %s.' % tool | |
| 165 | - self.test_assert((len(res[1]) == 0), name, res[1]) | |
| 207 | + name = 'Test execution of %s.' % script | |
| 208 | + self.test_value(rc, 0, name, msg) | |
| 166 | 209 | |
| 210 | + # Signal that the command was not known | |
| 167 | 211 | else: |
| 168 | 212 | name = 'Test command "%s".' % command |
| 169 | 213 | msg = 'Unknown command "%s"' % command |
| ... | ... | @@ -185,7 +229,7 @@ class tutorials(gammalib.GPythonTestSuite): |
| 185 | 229 | # Initialise commands and arguments |
| 186 | 230 | cmd = '' |
| 187 | 231 | args = '' |
| 188 | - | |
| 232 | + | |
| 189 | 233 | # Loop over all lines |
| 190 | 234 | for line in lines: |
| 191 | 235 | |
| ... | ... | @@ -195,7 +239,7 @@ class tutorials(gammalib.GPythonTestSuite): |
| 195 | 239 | # If we have a command then execute it |
| 196 | 240 | if cmd != '': |
| 197 | 241 | self._test_execute_cmd(cmd, args) |
| 198 | - | |
| 242 | + | |
| 199 | 243 | # Set new command |
| 200 | 244 | cmd = line[2:] |
| 201 | 245 | args = '' |
| ... | ... | @@ -230,7 +274,7 @@ class tutorials(gammalib.GPythonTestSuite): |
| 230 | 274 | """ |
| 231 | 275 | # Increment step counter |
| 232 | 276 | self._rst_step += 1 |
| 233 | - | |
| 277 | + | |
| 234 | 278 | # Initialise arguments |
| 235 | 279 | args = '' |
| 236 | 280 | |
| ... | ... | @@ -396,7 +440,49 @@ class tutorials(gammalib.GPythonTestSuite): |
| 396 | 440 | self.name('Tutorials Verification') |
| 397 | 441 | |
| 398 | 442 | # Append tutorials |
| 399 | - self.append(self.tutorials_1dc, 'Test 1DC tutorial') | |
| 443 | + self.append(self.tutorials_quickstart, 'Test quickstart tutorial') | |
| 444 | + self.append(self.tutorials_1dc, 'Test 1DC tutorial') | |
| 445 | + | |
| 446 | + # Return | |
| 447 | + return | |
| 448 | + | |
| 449 | + # Test quickstart tutorials | |
| 450 | + def tutorials_quickstart(self): | |
| 451 | + """ | |
| 452 | + Test quickstart tutorials | |
| 453 | + """ | |
| 454 | + # Set Sphinx rst file path | |
| 455 | + path = os.path.abspath('../doc/source/users/tutorials/quickstart') | |
| 456 | + | |
| 457 | + # Set result directory | |
| 458 | + self._set_result_dir('tutorials/quickstart') | |
| 459 | + | |
| 460 | + # Get current working directory | |
| 461 | + cwd = os.getcwd() | |
| 462 | + | |
| 463 | + # Clean pfiles | |
| 464 | + self._clean_pfiles() | |
| 465 | + | |
| 466 | + # Initialise step counter | |
| 467 | + self._rst_step = 0 | |
| 468 | + | |
| 469 | + # Test Sphinx rst files | |
| 470 | + self._test_rst_file('%s/simulating.rst' % path) | |
| 471 | + self._test_rst_file('%s/selecting.rst' % path) | |
| 472 | + self._test_rst_file('%s/skymap.rst' % path) | |
| 473 | + self._test_rst_file('%s/binning.rst' % path) | |
| 474 | + self._test_rst_file('%s/response.rst' % path) | |
| 475 | + self._test_rst_file('%s/fitting.rst' % path) | |
| 476 | + self._test_rst_file('%s/residual_map.rst' % path) | |
| 477 | + self._test_rst_file('%s/residual_spectrum.rst' % path) | |
| 478 | + self._test_rst_file('%s/butterfly.rst' % path) | |
| 479 | + self._test_rst_file('%s/spectrum.rst' % path) | |
| 480 | + self._test_rst_file('%s/unbinned.rst' % path) | |
| 481 | + self._test_rst_file('%s/energy_dispersion.rst' % path) | |
| 482 | + self._test_rst_file('%s/onoff.rst' % path) | |
| 483 | + | |
| 484 | + # Reset working directory | |
| 485 | + os.chdir(cwd) | |
| 400 | 486 | |
| 401 | 487 | # Return |
| 402 | 488 | return |
| ... | ... | @@ -408,7 +494,7 @@ class tutorials(gammalib.GPythonTestSuite): |
| 408 | 494 | """ |
| 409 | 495 | # Continue only if CTADATA1DC environment variable is set |
| 410 | 496 | if 'CTADATA1DC' in os.environ: |
| 411 | - | |
| 497 | + | |
| 412 | 498 | # Set environment variables |
| 413 | 499 | os.environ['CTADATA'] = os.environ['CTADATA1DC'] |
| 414 | 500 | os.environ['CALDB'] = os.environ['CTADATA1DC']+'/caldb' | ... | ... |