Skip to content

Commit

Permalink
Further development of instrument_checker and version bump.
Browse files Browse the repository at this point in the history
  • Loading branch information
mads-bertelsen committed Jul 25, 2024
1 parent 510010b commit f23de3b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 12 deletions.
2 changes: 1 addition & 1 deletion mcstasscript/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
from .interface.reader import McStas_file

from .tools.cryostat_builder import Cryostat
from .tools.instrument_checker import has_component
from .tools.instrument_checker import has_component, has_parameter, all_parameters_set

from .instrument_diagnostics.beam_diagnostics import BeamDiagnostics as Diagnostics
2 changes: 1 addition & 1 deletion mcstasscript/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.67"
__version__ = "0.0.68"
72 changes: 63 additions & 9 deletions mcstasscript/tests/test_instrument_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import mcstasscript as ms
from mcstasscript.tests.helpers_for_tests import WorkInTestDir

from mcstasscript.tools.instrument_checker import has_component
from mcstasscript.tools.instrument_checker import has_component, has_parameter, all_parameters_set


def setup_instr_with_path():
Expand All @@ -30,7 +30,10 @@ def setup_populated_instr():

instr.add_parameter("double", "theta")
instr.add_parameter("double", "has_default", value=37)
instr.add_parameter("int", "n_pulses")

instr.add_declare_var("double", "two_theta")

instr.append_initialize("two_theta = 2.0*theta;")

instr.add_component("first_component", "test_for_reading")
Expand All @@ -47,52 +50,103 @@ class TestToolInstrumentChecker(unittest.TestCase):

def test_simple_case_name(self):
"""
check component of given name
check has_component of given name
"""
instr = setup_populated_instr()
self.assertTrue(has_component(instr, component_name="second_component"))

def test_simple_case_name_not_found(self):
"""
check component of given name
check has_component returns false when name is not there
"""
instr = setup_populated_instr()
self.assertFalse(has_component(instr, component_name="fourth_component"))

def test_simple_case_type(self):
"""
check component of given name
check has_component can find component of given type
"""
instr = setup_populated_instr()
self.assertTrue(has_component(instr, component_type="test_for_reading"))

def test_simple_case_type_not_found(self):
"""
check component of given name
check has_component returns false when component type is not found
"""
instr = setup_populated_instr()
self.assertFalse(has_component(instr, component_type="Arm"))

def test_case_both(self):
"""
check component of given name
check has_component when given both name and component type
"""
instr = setup_populated_instr()
self.assertTrue(has_component(instr, component_name="first_component",
component_type="test_for_reading"))

def test_case_both_not_found_name(self):
"""
check component of given name
check has_component returns false when name not found but type is
"""
instr = setup_populated_instr()
self.assertFalse(has_component(instr, component_name="",
component_type="test_for_reading"))

def test_case_both_not_found_type(self):
"""
check component of given name
check has_component returns false when type not found but name is
"""
instr = setup_populated_instr()
self.assertFalse(has_component(instr, component_name="first_component",
component_type="Arm"))
component_type="Arm"))

def test_parameter_found(self):
"""
check has_parameter of given name
"""
instr = setup_populated_instr()
self.assertTrue(has_parameter(instr, parameter_name="theta"))

def test_parameter_not_found(self):
"""
check has_parameter returns false when of given name
"""
instr = setup_populated_instr()
self.assertFalse(has_parameter(instr, parameter_name="bogus"))

def test_parameter_found_with_type(self):
"""
check has_parameter of given name and type
"""
instr = setup_populated_instr()
self.assertTrue(has_parameter(instr, parameter_name="n_pulses", parameter_type="int"))

def test_parameter_found_with_type_default(self):
"""
check has_parameter of given name and type (using default double)
"""
instr = setup_populated_instr()
self.assertTrue(has_parameter(instr, parameter_name="theta", parameter_type="double"))

def test_parameter_found_with_wrong_type(self):
"""
check has_parameter of given name
"""
instr = setup_populated_instr()
self.assertFalse(has_parameter(instr, parameter_name="theta", parameter_type="int"))

def test_all_parameters_set_not_set(self):
"""
check all_parameters_set returns false when not all parameters are set of given name
"""
instr = setup_populated_instr()
self.assertFalse(all_parameters_set(instr))

def test_all_parameters_set_actually_set(self):
"""
check all_parameters_set returns false when all parameters are set of given name
"""
instr = setup_populated_instr()

instr.set_parameters(theta=37, n_pulses=3)
self.assertTrue(all_parameters_set(instr))
34 changes: 33 additions & 1 deletion mcstasscript/tools/instrument_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,36 @@ def has_component(instrument, component_name=None, component_type=None):
return True

# Did not find any such component
return False
return False


def has_parameter(instrument, parameter_name, parameter_type=None):
if parameter_name is None and parameter_type is None:
raise ValueError("Specify parameter_name, parameter_type or both.")

parameter_dict = instrument.parameters.parameters

if parameter_name not in parameter_dict:
return False

if parameter_type is not None:
found_type = parameter_dict[parameter_name].type

if parameter_type == "double" and found_type == "":
# Default McStas type is double
return True

if found_type != parameter_type:
return False

return True

def all_parameters_set(instrument):

parameter_dict = instrument.parameters.parameters

for par_object in parameter_dict.values():
if par_object.value is None:
return False

return True

0 comments on commit f23de3b

Please sign in to comment.