File: test_simulation_interface.py

package info (click to toggle)
python-mcstasscript 0.0.46%2Bgit20250402111921.bfa5a26-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,440 kB
  • sloc: python: 13,421; makefile: 14
file content (211 lines) | stat: -rw-r--r-- 7,283 bytes parent folder | download
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
import os
import io
import unittest
import unittest.mock
import ipywidgets as widgets

import matplotlib
matplotlib.use('Agg')  # Set the backend to Agg

from mcstasscript.jb_interface.simulation_interface import SimInterface
from mcstasscript.jb_interface.simulation_interface import ParameterWidget
from mcstasscript.jb_interface.widget_helpers import parameter_has_default
from mcstasscript.jb_interface.widget_helpers import get_parameter_default
from mcstasscript.interface.instr import McStas_instr
from mcstasscript.interface.instr import McXtrace_instr


def setup_instr_root_path_McStas():
    """
    Sets up a neutron instrument with root package_path
    """
    THIS_DIR = os.path.dirname(os.path.abspath(__file__))

    current_work_dir = os.getcwd()
    os.chdir(THIS_DIR)  # Set work directory to test folder

    instrument = McStas_instr("test_instrument", package_path="/")

    os.chdir(current_work_dir)

    return instrument

def setup_populated_instr_McStas():
    """
    Sets up a neutron instrument with some features used and three components
    """
    instr = setup_instr_root_path_McStas()

    instr.add_parameter("double", "theta")
    instr.add_parameter("double", "has_default", value=37)
    instr.add_declare_var("double", "two_theta")
    instr.append_initialize("two_theta = 2.0*theta;")

    instr.add_component("first_component", "test_for_reading")
    instr.add_component("second_component", "test_for_reading")
    instr.add_component("third_component", "test_for_reading")

    return instr

def setup_instr_root_path_McXtrace():
    """
    Sets up a neutron instrument with root package_path
    """
    THIS_DIR = os.path.dirname(os.path.abspath(__file__))

    current_work_dir = os.getcwd()
    os.chdir(THIS_DIR)  # Set work directory to test folder

    instrument = McStas_instr("test_instrument", package_path="/")

    os.chdir(current_work_dir)

    return instrument

def setup_populated_instr_McXtrace():
    """
    Sets up a neutron instrument with some features used and three components
    """
    instr = setup_instr_root_path_McXtrace()

    instr.add_parameter("double", "theta")
    instr.add_parameter("double", "has_default", value=37)
    instr.add_declare_var("double", "two_theta")
    instr.append_initialize("two_theta = 2.0*theta;")

    instr.add_component("first_component", "test_for_reading")
    instr.add_component("second_component", "test_for_reading")
    instr.add_component("third_component", "test_for_reading")

    return instr


class FakeChange:
    def __init__(self, new=None, old=None, name=None):
        self.new = new
        self.old = old
        self.name = name


class TestSimulationInterface(unittest.TestCase):
    """
    Tests of simulation interface
    """
    def test_initialization_McStas(self):
        """
        Checking that interface can initialize from instrument and retrieve the
        parameters that has a default value.
        """

        sim_interface = SimInterface(setup_populated_instr_McStas())
        self.assertEqual(sim_interface.parameters["has_default"], 37)

    def test_show_interface_McStas(self):
        """
        Ensure that show_interface runs without errors and returns widget
        """

        sim_interface = SimInterface(setup_populated_instr_McStas())
        widget = sim_interface.show_interface()

        self.assertIsInstance(widget, widgets.widgets.widget_box.VBox)

    def test_initialization_McXtrace(self):
        """
        Checking that interface can initialize from instrument and retrieve the
        parameters that has a default value.
        """

        sim_interface = SimInterface(setup_populated_instr_McXtrace())
        self.assertEqual(sim_interface.parameters["has_default"], 37)

    def test_show_interface_McXtrace(self):
        """
        Ensure that show_interface runs without errors and returns widget
        """

        sim_interface = SimInterface(setup_populated_instr_McXtrace())
        widget = sim_interface.show_interface()

        self.assertIsInstance(widget, widgets.widgets.widget_box.VBox)

    def test_update_ncount(self):
        """
        Check ncount can be set correctly
        """
        sim_interface = SimInterface(setup_populated_instr_McStas())
        sim_interface.show_interface()

        fake_change = FakeChange(new=100)
        sim_interface.update_ncount(fake_change)
        self.assertEqual(sim_interface.ncount, 100)

    def test_update_mpi(self):
        """
        Check mpi can be set correctly
        """
        sim_interface = SimInterface(setup_populated_instr_McStas())
        sim_interface.show_interface()

        fake_change = FakeChange(new=3)
        sim_interface.update_mpi(fake_change)
        self.assertEqual(sim_interface.mpi, 3)

        # Check input that wouldn't work is ignored
        fake_change = FakeChange(new="wrong input")
        sim_interface.update_mpi(fake_change)
        self.assertEqual(sim_interface.mpi, 3)

    def test_ParameterWidget(self):
        """
        Test that ParameterWidgets are initialized correctly

        This code is part of the initialization for the simulation interface,
        yet it is useful to have as its own test as if it fails its clear where
        the overall problem is.
        """

        instrument = setup_populated_instr_McStas()

        instrument.add_parameter("string", "choice", options=["A", "B", "Long"])

        parameters = {}
        # get default parameters from instrument
        for parameter in instrument.parameters:
            if parameter_has_default(parameter):
                parameters[parameter.name] = get_parameter_default(parameter)

        parameter_widgets = []
        parameterwidget_objects = []
        for parameter in instrument.parameters:
            par_widget = ParameterWidget(parameter, parameters)
            parameterwidget_objects.append(par_widget)
            parameter_widgets.append(par_widget.make_widget())

        self.assertEqual(parameterwidget_objects[0].name, "theta")
        self.assertEqual(parameterwidget_objects[0].default_value, None)
        # Parameter does not exist in parameter dict yet
        with self.assertRaises(KeyError):
            parameters[parameterwidget_objects[0].name]

        change = FakeChange(new=222)
        parameterwidget_objects[0].update(change)
        self.assertEqual(parameters[parameterwidget_objects[0].name], 222)

        self.assertEqual(parameterwidget_objects[1].name, "has_default")
        self.assertEqual(parameterwidget_objects[1].default_value, 37)
        self.assertEqual(parameters[parameterwidget_objects[1].name], 37)

        change = FakeChange(new=227)
        parameterwidget_objects[1].update(change)
        self.assertEqual(parameters[parameterwidget_objects[1].name], 227)

        self.assertEqual(parameterwidget_objects[2].name, "choice")
        self.assertEqual(parameterwidget_objects[2].default_value, None)
        with self.assertRaises(KeyError):
            parameters[parameterwidget_objects[2].name]

        change = FakeChange(new="test")
        parameterwidget_objects[2].update(change) # Should add necessary extra quotation marks
        self.assertEqual(parameters[parameterwidget_objects[2].name], "\"test\"")