1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
|
import os
import unittest
import unittest.mock
from mcstasscript.helper.managed_mcrun import ManagedMcrun
from mcstasscript.helper.managed_mcrun import load_results
from mcstasscript.helper.managed_mcrun import load_metadata
from mcstasscript.helper.managed_mcrun import load_monitor
from mcstasscript.tests.helpers_for_tests import WorkInTestDir
class TestManagedMcrun(unittest.TestCase):
"""
Testing the ManagedMcrun class that sets up McStas runs, runs the
simulation and loads the data.
Here the simulation is not actually performed, this will be done in
integration tests. The surrounding plumbing and data loading is
tested.
"""
def test_ManagedMcrun_init_simple(self):
"""
Check shortest possible initialization works
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
executable_path = os.path.join(THIS_DIR, "dummy_mcstas")
with WorkInTestDir() as handler:
mcrun_obj = ManagedMcrun("test.instr",
output_path="test_folder",
executable_path=executable_path,
executable="mcrun")
self.assertEqual(mcrun_obj.name_of_instrumentfile, "test.instr")
expected_data_folder = os.path.join(THIS_DIR, "test_folder")
self.assertEqual(mcrun_obj.data_folder_name, expected_data_folder)
expected_executable_path = os.path.join(THIS_DIR, "dummy_mcstas")
self.assertEqual(mcrun_obj.executable_path, expected_executable_path)
def test_ManagedMcrun_init_defaults(self):
"""
Check default values are set up correctly
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
executable_path = os.path.join(THIS_DIR, "dummy_mcstas")
with WorkInTestDir() as handler:
mcrun_obj = ManagedMcrun("test.instr",
output_path="test_folder",
executable_path=executable_path,
executable="mcrun")
self.assertEqual(mcrun_obj.mpi, None)
self.assertEqual(mcrun_obj.ncount, 1000000)
expected_run_path = os.path.join(THIS_DIR, ".")
self.assertEqual(mcrun_obj.run_path, expected_run_path)
def test_ManagedMcrun_init_set_values(self):
"""
Check values given to ManagedMcrun are internalized
run_path set to an existing folder in the test directory
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
executable_path = os.path.join(THIS_DIR, "dummy_mcstas")
with WorkInTestDir() as handler:
mcrun_obj = ManagedMcrun("test.instr",
output_path="test_folder",
executable="mcrun",
executable_path=executable_path,
run_path="test_data_set",
mpi=4,
ncount=128)
self.assertEqual(mcrun_obj.mpi, 4)
self.assertEqual(mcrun_obj.ncount, 128)
expected_run_path = os.path.join(THIS_DIR, "test_data_set")
self.assertEqual(mcrun_obj.run_path, expected_run_path)
def test_ManagedMcrun_init_set_parameters(self):
"""
Check parameters can be given as dictionary
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
executable_path = os.path.join(THIS_DIR, "dummy_mcstas")
par_input = {"A_par": 5.1,
"int_par": 1,
"define_par": "Bike",
"string_par": "\"Car\""}
with WorkInTestDir() as handler:
mcrun_obj = ManagedMcrun("test.instr",
output_path="test_folder",
executable_path=executable_path,
executable="",
parameters=par_input)
self.assertEqual(mcrun_obj.parameters["A_par"], 5.1)
self.assertEqual(mcrun_obj.parameters["int_par"], 1)
self.assertEqual(mcrun_obj.parameters["define_par"], "Bike")
self.assertEqual(mcrun_obj.parameters["string_par"], "\"Car\"")
def test_ManagedMcrun_init_set_custom_flags(self):
"""
Check custom_flags can be given by user
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
executable_path = os.path.join(THIS_DIR, "dummy_mcstas")
custom_flag_input = "-p"
with WorkInTestDir() as handler:
mcrun_obj = ManagedMcrun("test.instr",
output_path="test_folder",
executable="mcrun",
executable_path=executable_path,
custom_flags=custom_flag_input)
self.assertEqual(mcrun_obj.custom_flags, custom_flag_input)
def test_ManagedMcrun_init_no_folder_error(self):
"""
An error should occur if no filename is given
"""
with self.assertRaises(NameError):
ManagedMcrun("test.instr", mcrun_path="")
def test_ManagedMcrun_init_invalid_ncount_error(self):
"""
An error should occur if negative ncount is given
"""
with self.assertRaises(ValueError):
ManagedMcrun("test.instr",
output_path="test_folder",
mcrun_path="",
ncount=-8)
def test_ManagedMcrun_init_invalid_mpi_error(self):
"""
An error should occur if negative mpi is given
"""
with self.assertRaises(ValueError):
ManagedMcrun("test.instr",
output_path="test_folder",
mcrun_path="",
mpi=-8)
def test_ManagedMcrun_init_invalid_parameters_error(self):
"""
An error should occur if parameters is given as non dict
"""
with self.assertRaises(RuntimeError):
ManagedMcrun("test.instr",
output_path="test_folder",
mcrun_path="",
parameters=[1, 2, 3])
@unittest.mock.patch("subprocess.run")
def test_ManagedMcrun_run_simulation_basic(self, mock_sub):
"""
Check a basic system call is correct
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
executable_path = os.path.join(THIS_DIR, "dummy_mcstas")
with WorkInTestDir() as handler:
mcrun_obj = ManagedMcrun("test.instr",
output_path="test_folder",
executable_path=executable_path,
executable="mcrun",)
mcrun_obj.run_simulation()
expected_folder_path = os.path.join(THIS_DIR, "test_folder")
# a double space because of a missing option
executable = os.path.join(executable_path, "mcrun")
executable = '"' + executable + '"'
expected_call = (executable + " -c -n 1000000 "
+ "-d " + expected_folder_path + " test.instr")
mock_sub.assert_called_once_with(expected_call,
shell=True,
stderr=-2, stdout=-1,
universal_newlines=True,
cwd=mcrun_obj.run_path)
@unittest.mock.patch("subprocess.run")
def test_ManagedMcrun_run_simulation_basic_path(self, mock_sub):
"""
Check a basic system call is correct, with different path format
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
executable_path = os.path.join(THIS_DIR, "dummy_mcstas", "")
with WorkInTestDir() as handler:
mcrun_obj = ManagedMcrun("test.instr",
output_path="test_folder",
executable_path=executable_path,
executable="mcrun",)
mcrun_obj.run_simulation()
expected_folder_path = os.path.join(THIS_DIR, "test_folder")
executable = os.path.join(executable_path, "mcrun")
executable = '"' + executable + '"'
# a double space because of a missing option
expected_call = (executable + " -c -n 1000000 "
+ "-d " + expected_folder_path + " test.instr")
mock_sub.assert_called_once_with(expected_call,
shell=True,
stderr=-2, stdout=-1,
universal_newlines=True,
cwd=mcrun_obj.run_path)
@unittest.mock.patch("subprocess.run")
def test_ManagedMcrun_run_simulation_no_standard(self, mock_sub):
"""
Check a non standard system call is correct
Here multiple options are used and ncount is a float that should
be rounded by the class.
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
executable_path = os.path.join(THIS_DIR, "dummy_mcstas")
with WorkInTestDir() as handler:
mcrun_obj = ManagedMcrun("test.instr",
output_path="test_folder",
executable_path=executable_path,
executable="mcrun",
mpi=7, seed=300,
ncount=48.4,
custom_flags="-fo")
mcrun_obj.run_simulation()
expected_folder_path = os.path.join(THIS_DIR, "test_folder")
# a double space because of a missing option
executable = os.path.join(executable_path, "mcrun")
executable = '"' + executable + '"'
expected_call = (executable + " -c -n 48 --mpi=7 --seed=300 "
+ "-d " + expected_folder_path + " -fo test.instr")
mock_sub.assert_called_once_with(expected_call,
shell=True,
stderr=-2, stdout=-1,
universal_newlines=True,
cwd=mcrun_obj.run_path)
@unittest.mock.patch("subprocess.run")
def test_ManagedMcrun_run_simulation_with_gravity(self, mock_sub):
"""
Check a non standard system call is correct when including gravity
Here multiple options are used and ncount is a float that should
be rounded by the class.
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
executable_path = os.path.join(THIS_DIR, "dummy_mcstas")
with WorkInTestDir() as handler:
mcrun_obj = ManagedMcrun("test.instr",
output_path="test_folder",
executable_path=executable_path,
executable="mcrun",
gravity=True,
mpi=7, seed=300,
ncount=48.4,
custom_flags="-fo")
mcrun_obj.run_simulation()
expected_folder_path = os.path.join(THIS_DIR, "test_folder")
# a double space because of a missing option
executable = os.path.join(executable_path, "mcrun")
executable = '"' + executable + '"'
expected_call = (executable + " -c -g -n 48 --mpi=7 --seed=300 "
+ "-d " + expected_folder_path + " -fo test.instr")
mock_sub.assert_called_once_with(expected_call,
shell=True,
stderr=-2, stdout=-1,
universal_newlines=True,
cwd=mcrun_obj.run_path)
@unittest.mock.patch("subprocess.run")
def test_ManagedMcrun_run_simulation_parameters(self, mock_sub):
"""
Check a run with parameters is correct
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
executable_path = os.path.join(THIS_DIR, "dummy_mcstas")
with WorkInTestDir() as handler:
mcrun_obj = ManagedMcrun("test.instr",
output_path="test_folder",
executable_path=executable_path,
executable="mcrun",
mpi=7,
ncount=48.4,
custom_flags="-fo",
parameters={"A": 2,
"BC": "car",
"th": "\"toy\""})
mcrun_obj.run_simulation()
expected_folder_path = os.path.join(THIS_DIR, "test_folder")
# a double space because of a missing option
executable = os.path.join(executable_path, "mcrun")
executable = '"' + executable + '"'
expected_call = (executable + " -c -n 48 --mpi=7 "
+ "-d " + expected_folder_path + " -fo test.instr "
+ "A=2 BC=car th=\"toy\"")
mock_sub.assert_called_once_with(expected_call,
shell=True,
stderr=-2, stdout=-1,
universal_newlines=True,
cwd=mcrun_obj.run_path)
@unittest.mock.patch("subprocess.run")
def test_ManagedMcrun_run_simulation_compile(self, mock_sub):
"""
Check run with force_compile set to False works
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
executable_path = os.path.join(THIS_DIR, "dummy_mcstas")
with WorkInTestDir() as handler:
mcrun_obj = ManagedMcrun("test.instr",
output_path="test_folder",
executable_path=executable_path,
executable="mcrun",
mpi=7,
ncount=48.4,
force_compile=False,
custom_flags="-fo",
parameters={"A": 2,
"BC": "car",
"th": "\"toy\""})
mcrun_obj.run_simulation()
expected_folder_path = os.path.join(THIS_DIR, "test_folder")
executable = os.path.join(executable_path, "mcrun")
executable = '"' + executable + '"'
# a double space because of a missing option
expected_call = (executable + " -n 48 --mpi=7 "
+ "-d " + expected_folder_path + " -fo test.instr "
+ "A=2 BC=car th=\"toy\"")
mock_sub.assert_called_once_with(expected_call,
shell=True,
stderr=-2, stdout=-1,
universal_newlines=True,
cwd=mcrun_obj.run_path)
@unittest.mock.patch("subprocess.run")
def test_ManagedMcrun_run_simulation_NeXus(self, mock_sub):
"""
Check run with NeXus works
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
executable_path = os.path.join(THIS_DIR, "dummy_mcstas")
with WorkInTestDir() as handler:
mcrun_obj = ManagedMcrun("test.instr",
output_path="test_folder",
executable_path=executable_path,
executable="mcrun",
mpi=7,
ncount=57.4,
force_compile=False,
NeXus=True,
custom_flags="-fo",
parameters={"A": 2,
"BC": "car",
"th": "\"toy\""})
mcrun_obj.run_simulation()
expected_folder_path = os.path.join(THIS_DIR, "test_folder")
executable = os.path.join(executable_path, "mcrun")
executable = '"' + executable + '"'
# a double space because of a missing option
expected_call = (executable + " --format=NeXus -n 57 --mpi=7 "
+ "-d " + expected_folder_path + " -fo test.instr "
+ "A=2 BC=car th=\"toy\"")
mock_sub.assert_called_once_with(expected_call,
shell=True,
stderr=-2, stdout=-1,
universal_newlines=True,
cwd=mcrun_obj.run_path)
@unittest.mock.patch("subprocess.run")
def test_ManagedMcrun_run_simulation_openacc(self, mock_sub):
"""
Check run with openacc works
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
executable_path = os.path.join(THIS_DIR, "dummy_mcstas")
with WorkInTestDir() as handler:
mcrun_obj = ManagedMcrun("test.instr",
output_path="test_folder",
executable_path=executable_path,
executable="mcrun",
mpi=7,
ncount=48.4,
openacc=True,
force_compile=False,
custom_flags="-fo",
parameters={"A": 2,
"BC": "car",
"th": "\"toy\""})
mcrun_obj.run_simulation()
expected_folder_path = os.path.join(THIS_DIR, "test_folder")
executable = os.path.join(executable_path, "mcrun")
executable = '"' + executable + '"'
# a double space because of a missing option
expected_call = (executable + " --openacc -n 48 --mpi=7 "
+ "-d " + expected_folder_path + " -fo test.instr "
+ "A=2 BC=car th=\"toy\"")
mock_sub.assert_called_once_with(expected_call,
shell=True,
stderr=-2, stdout=-1,
universal_newlines=True,
cwd=mcrun_obj.run_path)
def test_ManagedMcrun_load_data_PSD4PI(self):
"""
Use test_data_set to test load_data for PSD_4PI
test_data_set contains three data files and some junk, the mccode.sim
file contains names of the data files so only these are loaded.
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
executable_path = os.path.join(THIS_DIR, "dummy_mcstas")
with WorkInTestDir() as handler:
mcrun_obj = ManagedMcrun("test.instr",
output_path="test_data_set",
executable_path=executable_path,
mcrun_path="path")
results = mcrun_obj.load_results()
# Check three data objects are loaded
self.assertEqual(len(results), 4)
# Check properties of PSD_4PI data
PSD_4PI = results[0]
self.assertEqual(PSD_4PI.name, "PSD_4PI")
self.assertEqual(PSD_4PI.metadata.dimension, [300, 300])
self.assertEqual(PSD_4PI.metadata.limits, [-180, 180, -90, 90])
self.assertEqual(PSD_4PI.metadata.xlabel, "Longitude [deg]")
self.assertEqual(PSD_4PI.metadata.ylabel, "Latitude [deg]")
self.assertEqual(PSD_4PI.metadata.title, "4PI PSD monitor")
expected_parameters = {"wavelength": 1.0}
self.assertEqual(PSD_4PI.metadata.info["Parameters"], expected_parameters)
self.assertEqual(PSD_4PI.metadata.parameters, expected_parameters)
self.assertEqual(PSD_4PI.Ncount[4][1], 4)
self.assertEqual(PSD_4PI.Intensity[4][1], 1.537334562E-10)
self.assertEqual(PSD_4PI.Error[4][1], 1.139482296E-10)
def test_ManagedMcrun_load_data_PSD(self):
"""
Use test_data_set to test load_data for PSD
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
executable_path = os.path.join(THIS_DIR, "dummy_mcstas")
with WorkInTestDir() as handler:
mcrun_obj = ManagedMcrun("test.instr",
output_path="test_data_set",
executable_path=executable_path,
mcrun_path="path")
results = mcrun_obj.load_results()
# Check three data objects are loaded
self.assertEqual(len(results), 4)
# Check properties of PSD data
PSD = results[1]
self.assertEqual(PSD.name, "PSD")
self.assertEqual(PSD.metadata.dimension, [200, 200])
self.assertEqual(PSD.metadata.limits, [-5, 5, -5, 5])
self.assertEqual(PSD.metadata.xlabel, "X position [cm]")
self.assertEqual(PSD.metadata.ylabel, "Y position [cm]")
self.assertEqual(PSD.metadata.title, "PSD monitor")
expected_parameters = {"wavelength": 1.0}
self.assertEqual(PSD.metadata.info["Parameters"], expected_parameters)
self.assertEqual(PSD.metadata.parameters, expected_parameters)
self.assertEqual(PSD.Ncount[27][21], 9)
self.assertEqual(PSD.Intensity[27][21], 2.623929371e-13)
self.assertEqual(PSD.Error[27][21], 2.765467693e-13)
def test_ManagedMcrun_load_data_L_mon(self):
"""
Use test_data_set to test load_data for L_mon
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
executable_path = os.path.join(THIS_DIR, "dummy_mcstas")
with WorkInTestDir() as handler:
mcrun_obj = ManagedMcrun("test.instr",
output_path="test_data_set",
executable_path=executable_path,
mcrun_path="path")
results = mcrun_obj.load_results()
# Check three data objects are loaded
self.assertEqual(len(results), 4)
# Check properties of L_mon
L_mon = results[2]
self.assertEqual(L_mon.name, "L_mon")
self.assertEqual(L_mon.metadata.dimension, 150)
self.assertEqual(L_mon.metadata.limits, [0.7, 1.3])
self.assertEqual(L_mon.metadata.xlabel, "Wavelength [AA]")
self.assertEqual(L_mon.metadata.ylabel, "Intensity")
self.assertEqual(L_mon.metadata.title, "Wavelength monitor")
expected_parameters = {"wavelength": 1.0}
self.assertEqual(L_mon.metadata.info["Parameters"], expected_parameters)
self.assertEqual(L_mon.metadata.parameters, expected_parameters)
self.assertEqual(L_mon.xaxis[53], 0.914)
self.assertEqual(L_mon.Ncount[53], 37111)
self.assertEqual(L_mon.Intensity[53], 6.990299315e-06)
self.assertEqual(L_mon.Error[53], 6.215308587e-08)
def test_ManagedMcrun_load_data_L_mon_direct(self):
"""
Use test_data_set to test load_data for L_mon with direct path
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
executable_path = os.path.join(THIS_DIR, "dummy_mcstas")
with WorkInTestDir() as handler:
mcrun_obj = ManagedMcrun("test.instr",
output_path="test_data_set",
executable_path=executable_path,
mcrun_path="path")
load_path = os.path.join(THIS_DIR, "test_data_set")
results = mcrun_obj.load_results(load_path)
# Check properties of L_mon
L_mon = results[2]
self.assertEqual(L_mon.name, "L_mon")
self.assertEqual(L_mon.metadata.dimension, 150)
self.assertEqual(L_mon.metadata.limits, [0.7, 1.3])
self.assertEqual(L_mon.metadata.xlabel, "Wavelength [AA]")
self.assertEqual(L_mon.metadata.ylabel, "Intensity")
self.assertEqual(L_mon.metadata.title, "Wavelength monitor")
expected_parameters = {"wavelength": 1.0}
self.assertEqual(L_mon.metadata.info["Parameters"], expected_parameters)
self.assertEqual(L_mon.metadata.parameters, expected_parameters)
self.assertEqual(L_mon.xaxis[53], 0.914)
self.assertEqual(L_mon.Ncount[53], 37111)
self.assertEqual(L_mon.Intensity[53], 6.990299315e-06)
self.assertEqual(L_mon.Error[53], 6.215308587e-08)
self.assertFalse(hasattr(L_mon, 'Events'))
def test_ManagedMcrun_load_data_Event(self):
"""
Use test_data_set to test load_data for event data
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
executable_path = os.path.join(THIS_DIR, "dummy_mcstas")
with WorkInTestDir() as handler:
mcrun_obj = ManagedMcrun("test.instr",
output_path="test_data_set",
executable_path=executable_path,
mcrun_path="path")
load_path = os.path.join(THIS_DIR, "test_data_set")
results = mcrun_obj.load_results(load_path)
# Check properties of event data file
mon = results[3]
self.assertEqual(mon.name, "monitor")
self.assertEqual(mon.metadata.dimension, [8, 12000])
self.assertEqual(mon.metadata.limits, [1.0, 12000.0, 1.0, 8.0])
self.assertEqual(mon.metadata.xlabel, "List of neutron events")
self.assertEqual(mon.metadata.ylabel, "p x y z vx vy vz t")
self.assertEqual(mon.metadata.title, "Intensity Position Position"
+ " Position Velocity Velocity Velocity"
+ " Time_Of_Flight Monitor (Square)")
expected_parameters = {"wavelength": 1.0}
self.assertEqual(mon.metadata.info["Parameters"], expected_parameters)
self.assertEqual(mon.metadata.parameters, expected_parameters)
self.assertEqual(mon.Events[12, 1], -0.006163896406)
self.assertEqual(mon.Events[43, 4], 22.06193582)
self.assertFalse(hasattr(mon, 'xaxis'))
self.assertFalse(hasattr(mon, 'Error'))
self.assertFalse(hasattr(mon, 'Ncount'))
def test_ManagedMcrun_load_data_nonexisting(self):
"""
If folder does not exists, a warning should be shown and None returned
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
executable_path = os.path.join(THIS_DIR, "dummy_mcstas")
with WorkInTestDir() as handler:
mcrun_obj = ManagedMcrun("test.instr",
output_path="test_data_set",
executable_path=executable_path,
mcrun_path="path")
load_path = os.path.join(THIS_DIR, "non_existent_dataset")
with self.assertWarns(Warning):
result = mcrun_obj.load_results(load_path)
self.assertIsNone(result)
def test_ManagedMcrun_load_data_no_mcsim_file(self):
"""
Check an error occurs when pointed to directory without mcsim file
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
executable_path = os.path.join(THIS_DIR, "dummy_mcstas")
with WorkInTestDir() as handler:
mcrun_obj = ManagedMcrun("test.instr",
output_path="test_data_set",
executable_path=executable_path,
mcrun_path="path")
load_path = os.path.join(THIS_DIR, "dummy_mcstas")
with self.assertRaises(NameError):
mcrun_obj.load_results(load_path)
class Test_load_functions(unittest.TestCase):
"""
Testing the load functions in managed_mcrun.
load_results loads all data in folder
load_metadata loads all metadata in folder
load_monitor loads one monitor given metadata and folder
These are used in ManagedMcrun
"""
def test_mcrun_load_data_PSD4PI(self):
"""
Use test_data_set to test load_data for PSD_4PI
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
current_work_dir = os.getcwd()
os.chdir(THIS_DIR) # Set work directory to test folder
results = load_results("test_data_set")
os.chdir(current_work_dir) # Reset work directory
self.assertEqual(len(results), 4)
PSD_4PI = results[0]
self.assertEqual(PSD_4PI.name, "PSD_4PI")
self.assertEqual(PSD_4PI.metadata.dimension, [300, 300])
self.assertEqual(PSD_4PI.metadata.limits, [-180, 180, -90, 90])
self.assertEqual(PSD_4PI.metadata.xlabel, "Longitude [deg]")
self.assertEqual(PSD_4PI.metadata.ylabel, "Latitude [deg]")
self.assertEqual(PSD_4PI.metadata.title, "4PI PSD monitor")
expected_parameters = {"wavelength": 1.0}
self.assertEqual(PSD_4PI.metadata.info["Parameters"], expected_parameters)
self.assertEqual(PSD_4PI.metadata.parameters, expected_parameters)
self.assertEqual(PSD_4PI.Ncount[4][1], 4)
self.assertEqual(PSD_4PI.Intensity[4][1], 1.537334562E-10)
self.assertEqual(PSD_4PI.Error[4][1], 1.139482296E-10)
def test_mcrun_load_data_PSD(self):
"""
Use test_data_set to test load_data for PSD
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
current_work_dir = os.getcwd()
os.chdir(THIS_DIR) # Set work directory to test folder
results = load_results("test_data_set")
os.chdir(current_work_dir) # Reset work directory
self.assertEqual(len(results), 4)
PSD = results[1]
self.assertEqual(PSD.name, "PSD")
self.assertEqual(PSD.metadata.dimension, [200, 200])
self.assertEqual(PSD.metadata.limits, [-5, 5, -5, 5])
self.assertEqual(PSD.metadata.xlabel, "X position [cm]")
self.assertEqual(PSD.metadata.ylabel, "Y position [cm]")
self.assertEqual(PSD.metadata.title, "PSD monitor")
expected_parameters = {"wavelength": 1.0}
self.assertEqual(PSD.metadata.info["Parameters"], expected_parameters)
self.assertEqual(PSD.metadata.parameters, expected_parameters)
self.assertEqual(PSD.Ncount[27][21], 9)
self.assertEqual(PSD.Intensity[27][21], 2.623929371e-13)
self.assertEqual(PSD.Error[27][21], 2.765467693e-13)
def test_mcrun_load_metadata_PSD4PI(self):
"""
Use test_data_set to test load_metadata for PSD_4PI
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
current_work_dir = os.getcwd()
os.chdir(THIS_DIR) # Set work directory to test folder
metadata = load_metadata("test_data_set")
os.chdir(current_work_dir) # Reset work directory
self.assertEqual(len(metadata), 4)
PSD_4PI = metadata[0]
self.assertEqual(PSD_4PI.dimension, [300, 300])
self.assertEqual(PSD_4PI.limits, [-180, 180, -90, 90])
self.assertEqual(PSD_4PI.xlabel, "Longitude [deg]")
self.assertEqual(PSD_4PI.ylabel, "Latitude [deg]")
self.assertEqual(PSD_4PI.title, "4PI PSD monitor")
expected_parameters = {"wavelength": 1.0}
self.assertEqual(PSD_4PI.info["Parameters"], expected_parameters)
self.assertEqual(PSD_4PI.parameters, expected_parameters)
def test_mcrun_load_metadata_L_mon(self):
"""
Use test_data_set to test load_metadata for PSD_4PI
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
current_work_dir = os.getcwd()
os.chdir(THIS_DIR) # Set work directory to test folder
metadata = load_metadata("test_data_set")
os.chdir(current_work_dir) # Reset work directory
self.assertEqual(len(metadata), 4)
L_mon = metadata[2]
self.assertEqual(L_mon.dimension, 150)
self.assertEqual(L_mon.limits, [0.7, 1.3])
self.assertEqual(L_mon.xlabel, "Wavelength [AA]")
self.assertEqual(L_mon.ylabel, "Intensity")
self.assertEqual(L_mon.title, "Wavelength monitor")
expected_parameters = {"wavelength": 1.0}
self.assertEqual(L_mon.info["Parameters"], expected_parameters)
self.assertEqual(L_mon.parameters, expected_parameters)
def test_mcrun_load_monitor_PSD4PI(self):
"""
Use test_data_set to test load_monitor for PSD_4PI
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
current_work_dir = os.getcwd()
os.chdir(THIS_DIR) # Set work directory to test folder
metadata = load_metadata("test_data_set")
PSD_4PI = metadata[0]
monitor = load_monitor(PSD_4PI, "test_data_set")
os.chdir(current_work_dir) # Reset work directory
self.assertEqual(monitor.name, "PSD_4PI")
self.assertEqual(monitor.metadata.dimension, [300, 300])
self.assertEqual(monitor.metadata.limits, [-180, 180, -90, 90])
self.assertEqual(monitor.metadata.xlabel, "Longitude [deg]")
self.assertEqual(monitor.metadata.ylabel, "Latitude [deg]")
self.assertEqual(monitor.metadata.title, "4PI PSD monitor")
expected_parameters = {"wavelength": 1.0}
self.assertEqual(monitor.metadata.info["Parameters"], expected_parameters)
self.assertEqual(monitor.metadata.parameters, expected_parameters)
self.assertEqual(monitor.Ncount[4][1], 4)
self.assertEqual(monitor.Intensity[4][1], 1.537334562E-10)
self.assertEqual(monitor.Error[4][1], 1.139482296E-10)
def test_mcrun_load_monitor_L_mon(self):
"""
Use test_data_set to test load_monitor for PSD_4PI
"""
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
current_work_dir = os.getcwd()
os.chdir(THIS_DIR) # Set work directory to test folder
metadata = load_metadata("test_data_set")
L_mon = metadata[2]
monitor = load_monitor(L_mon, "test_data_set")
os.chdir(current_work_dir) # Reset work directory
self.assertEqual(monitor.name, "L_mon")
self.assertEqual(monitor.metadata.dimension, 150)
self.assertEqual(monitor.metadata.limits, [0.7, 1.3])
self.assertEqual(monitor.metadata.xlabel, "Wavelength [AA]")
self.assertEqual(monitor.metadata.ylabel, "Intensity")
self.assertEqual(monitor.metadata.title, "Wavelength monitor")
expected_parameters = {"wavelength": 1.0}
self.assertEqual(monitor.metadata.info["Parameters"], expected_parameters)
self.assertEqual(monitor.metadata.parameters, expected_parameters)
self.assertEqual(monitor.xaxis[53], 0.914)
self.assertEqual(monitor.Ncount[53], 37111)
self.assertEqual(monitor.Intensity[53], 6.990299315e-06)
self.assertEqual(monitor.Error[53], 6.215308587e-08)
if __name__ == '__main__':
unittest.main()
|