def test_nested_parameter_node_snapshot_with_parent(self): parameter_node = ParameterNode(use_as_attributes=True) nested_parameter_node = ParameterNode(use_as_attributes=True) parameter_node.nested = nested_parameter_node nested_parameter_node.parent = parameter_node nested_parameter_node.snapshot()
def test_nested_parameter_node_no_parent(self): parameter_node = ParameterNode(use_as_attributes=True) nested_parameter_node = ParameterNode(use_as_attributes=True) nested_parameter_node.parent = False # Should not have parent parameter_node.nested = nested_parameter_node self.assertEqual(parameter_node.nested.name, 'nested') self.assertEqual(parameter_node.nested.parent, False) self.assertNotIn('parent', parameter_node.parameter_nodes)
def test_subnode_snapshot(self): node = ParameterNode() node.subnode = ParameterNode() self.assertDictEqual(node.snapshot()['parameter_nodes']['subnode'], node.subnode.snapshot()) node.subnode.param = Parameter() self.assertDictEqual(node.snapshot()['parameter_nodes']['subnode'], node.subnode.snapshot())
def test_nested_parameter_node(self): parameter_node = ParameterNode(use_as_attributes=True) nested_parameter_node = ParameterNode(use_as_attributes=True) parameter_node.nested = nested_parameter_node self.assertEqual(parameter_node.nested.name, 'nested') self.assertEqual(parameter_node.nested.parent, parameter_node) self.assertNotIn('parent', parameter_node.parameter_nodes) # Add parameter parameter_node.nested.param = Parameter(set_cmd=None) parameter_node.nested.param = 42 self.assertEqual(parameter_node.nested.param, 42)
def test_deepcopy_nested_node(self): node = ParameterNode('node') nested_node = ParameterNode('nested_node') node.nested_node = nested_node self.assertEqual(node.nested_node.parent, node) copied_nested_node = deepcopy(nested_node) self.assertEqual(copied_nested_node.parent, None) self.assertListEqual(self.parameter_copies, []) self.assertListEqual(self.parameter_deepcopies, []) self.assertListEqual(self.parameter_node_copies, []) self.assertListEqual(self.parameter_node_deepcopies, [nested_node])
def test_overlapping_attributes(self): # If any new attributes get introduced to either, this test will check # if it overlaps. node = ParameterNode('node') p = Parameter('param', set_cmd=None) parameter_dict = [*p.__dict__, *Parameter.__dict__] node_dict = [*node.__dict__, *ParameterNode.__dict__] overlapping_attrs = { k for k in parameter_dict + node_dict if k in parameter_dict and k in node_dict } # Remove slotnames if it exists, used for caching __getstate__ try: overlapping_attrs.remove('__slotnames__') except: pass self.assertSetEqual( overlapping_attrs, { '__init__', '_meta_attrs', '__doc__', '__module__', 'metadata', '__deepcopy__', 'name', '__getitem__', '__getstate__', 'log_changes', 'sweep', 'parent', 'get' })
def __init__( self, name="ESR", pulse_sequences=None, **kwargs ): self.pulse_sequence = ESRPulseSequenceComposite(pulse_sequences=pulse_sequences) self.ESR = self.pulse_sequence.ESR self.EPR = self.pulse_sequence.EPR self.analyses = ParameterNode() self.analyses.EPR = AnalyseEPR("EPR") self.analyses.ESR = AnalyseElectronReadout("ESR") self.layout.sample_rate.connect(self.analyses.EPR.settings["sample_rate"]) self.layout.sample_rate.connect(self.analyses.ESR.settings["sample_rate"]) self.EPR["enabled"].connect(self.analyses.ESR.outputs["contrast"]) self.EPR["enabled"].connect(self.analyses.EPR["enabled"]) self.ESR["enabled"].connect(self.analyses.ESR["enabled"]) self.analyses.ESR.settings["num_frequencies"].define_get( lambda: len(self.ESR.frequencies) ) self.analyses.ESR.settings["samples"].define_get(lambda: self.samples) super().__init__(name=name, **kwargs)
def test_copy_copied_parameter_node(self): node = ParameterNode(use_as_attributes=True) node.p = Parameter(set_cmd=None) node.p = 123 self.assertEqual(node['p'].parent, node) node2 = copy(node) node3 = copy(node2) self.assertEqual(node2['p'].parent, node2) self.assertEqual(node3['p'].parent, node3) self.assertListEqual(self.parameter_copies, [node['p'], node2['p']]) self.assertListEqual(self.parameter_deepcopies, []) self.assertListEqual(self.parameter_node_copies, [node, node2]) self.assertListEqual(self.parameter_node_deepcopies, []) self.assertIsNot(node['p'], node2['p']) self.assertIsNot(node2['p'], node3['p']) self.assertEqual(node.p, 123) self.assertEqual(node2.p, 123) self.assertEqual(node3.p, 123) node.p = 124 self.assertEqual(node.p, 124) self.assertEqual(node2.p, 123) self.assertEqual(node3.p, 123) node2.p = 125 self.assertEqual(node.p, 124) self.assertEqual(node2.p, 125) self.assertEqual(node3.p, 123) node3.p = 126 self.assertEqual(node.p, 124) self.assertEqual(node2.p, 125) self.assertEqual(node3.p, 126)
def test_parameter_no_parent(self): p = Parameter(set_cmd=None) p.parent = False node = ParameterNode() node.p = p self.assertIs(p.parent, False)
def test_empty_node_snapshot(self): node = ParameterNode() snapshot = node.snapshot() self.assertCountEqual(snapshot.keys(), [ '__class__', 'functions', 'parameters', 'submodules', 'parameter_nodes' ]) node.name = 'node_name' self.assertEqual(node.snapshot()['name'], 'node_name')
def test_pickled_node_with_parameter(self): node = ParameterNode(name='node') node.p = Parameter(initial_value=123) pickle_dump = pickle.dumps(node) node_pickled = pickle.loads(pickle_dump) self.assertEqual(node_pickled.name, 'node') self.assertEqual(node_pickled['p'].name, 'p') self.assertEqual(node_pickled['p'].get_latest(), 123) self.assertEqual(node_pickled['p'].get(), 123)
def test_use_as_attributes(self): parameter_node = ParameterNode(use_as_attributes=True) test_parameter = Parameter(set_cmd=None) parameter_node.test_parameter = test_parameter self.assertIs(parameter_node['test_parameter'], test_parameter) self.assertEqual(parameter_node.test_parameter, None) parameter_node.test_parameter = 42 self.assertEqual(parameter_node.test_parameter, 42)
def test_copy_parameter_in_parameter_node(self): node = ParameterNode(use_as_attributes=True) node.p = Parameter(set_cmd=None) node.p = 123 self.assertEqual(node['p'].parent, node) copy(node['p']) self.assertListEqual(self.parameter_copies, [node['p']]) self.assertListEqual(self.parameter_node_copies, []) self.assertListEqual(self.parameter_node_deepcopies, [])
def test_parameter_str(self): p = Parameter(set_cmd=None) self.assertEqual(str(p), 'None') p = Parameter('param1', set_cmd=None) self.assertEqual(str(p), 'param1') node = ParameterNode() p.parent = node self.assertEqual(str(p), 'param1') node.name = 'node1' self.assertEqual(str(p), 'node1_param1')
def test_use_not_as_attributes(self): parameter_node = ParameterNode(use_as_attributes=False) test_parameter = Parameter(set_cmd=None) parameter_node.test_parameter = test_parameter self.assertIs(parameter_node['test_parameter'], test_parameter) self.assertIs(parameter_node.test_parameter, test_parameter) self.assertEqual(parameter_node.test_parameter(), None) # Setting value of parameter still works via attribute parameter_node.test_parameter = 42 self.assertEqual(parameter_node.test_parameter(), 42)
def test_deepcopy_parameter_node_add_parameter(self): node = ParameterNode(use_as_attributes=True) node.add_parameter('p', set_cmd=None) node.p = 123 self.assertEqual(node['p'](), 123) self.assertEqual(node['p']._instrument, node) node_copy = deepcopy(node) self.assertEqual(node.p, 123) self.assertEqual(node['p']._instrument, node) self.assertEqual(node_copy['p']._instrument, None) self.assertListEqual(self.parameter_copies, []) self.assertListEqual(self.parameter_deepcopies, [node['p']]) self.assertListEqual(self.parameter_node_copies, []) self.assertListEqual(self.parameter_node_deepcopies, [node])
def test_implicit_parameter_name(self): for use_as_attribute in [True, False]: parameter_node = ParameterNode(use_as_attributes=use_as_attribute) parameter_node.implicit_name_parameter = Parameter(set_cmd=None) # Test that name and label of parameter are implicitly set self.assertEqual(parameter_node['implicit_name_parameter'].name, 'implicit_name_parameter') self.assertEqual(parameter_node['implicit_name_parameter'].label, 'Implicit name parameter') parameter_node.explicit_name_parameter = Parameter(name='explicit', set_cmd=None) # Test that name and label of parameter are not implicitly set self.assertEqual(parameter_node['explicit_name_parameter'].name, 'explicit') self.assertEqual(parameter_node['explicit_name_parameter'].label, 'Explicit')
def __init__(self, name: str = "NMR", pulse_sequence=None, **kwargs): if pulse_sequence is None: pulse_sequence = NMRPulseSequenceComposite() self.pulse_sequence = pulse_sequence self.NMR = self.pulse_sequence.NMR self.ESR = self.pulse_sequence.ESR self.analyses = ParameterNode() self.analyses.ESR = AnalyseElectronReadout('ESR') self.analyses.NMR_electron_readout = AnalyseElectronReadout('NMR_electron_readout') self.analyses.NMR = AnalyseMultiStateReadout('NMR') # Turn off NMR_electron_readout analysis self.analyses.NMR_electron_readout.enabled = False self._connect_analyses() super().__init__(name=name, **kwargs)
def test_copy_parameter_in_node(self): node = ParameterNode(use_as_attributes=True) node.p = Parameter(set_cmd=None) node.p = 123 p_copy = copy(node['p']) self.assertEqual(node.p, 123) self.assertEqual(p_copy(), 123) node.p = 124 self.assertEqual(node.p, 124) self.assertEqual(p_copy(), 123) p_copy(125) self.assertEqual(node.p, 124) self.assertEqual(p_copy(), 125) self.assertListEqual(self.parameter_copies, [node['p']]) self.assertListEqual(self.parameter_node_copies, []) self.assertListEqual(self.parameter_node_deepcopies, [])
def test_sweep_parameter_node(self): node = ParameterNode(use_as_attributes=True) node.param1 = Parameter(set_cmd=None) sweep_values_node = node.sweep('param1', 0, 10, step=1) sweep_values_parameter = node['param1'].sweep(0, 10, step=1) self.assertEqual(list(sweep_values_node), list(sweep_values_parameter))
def test_pickled_empty_node(self): node = ParameterNode(name='node') pickle_dump = pickle.dumps(node) node_pickled = pickle.loads(pickle_dump) self.assertEqual(node_pickled.name, 'node')
def test_parameter_node_nested_explicit_name(self): parameter_node = ParameterNode(use_as_attributes=True) nested_explicit_parameter_node = ParameterNode(name='explicit_name', use_as_attributes=True) parameter_node.nested = nested_explicit_parameter_node self.assertEqual(parameter_node.nested.name, 'explicit_name')
def test_parameter_in_node_simplified_snapshot(self): node = ParameterNode(simplify_snapshot=True) node.p = Parameter(initial_value=42) self.assertEqual(node.snapshot()['p'], 42)
def test_parameter_in_node_snapshot(self): node = ParameterNode() node.p = Parameter() self.assertEqual(node.snapshot()['parameters']['p'], node.p.snapshot())