def test_factory_creates_single_instance(self): dummy_adapter1 = InstrumentAdapterFactory.get_instrument_adapter( 'DummyInstrumentAdapter', 'dev1') dummy_adapter2 = InstrumentAdapterFactory.get_instrument_adapter( 'DummyInstrumentAdapter', 'dev1') self.assertIs(dummy_adapter1, dummy_adapter2) dummy_adapter1.close_instrument()
def test_add_instrument_adapter_package(self): InstrumentAdapterFactory.add_instrument_adapter_package( sys.modules[__name__]) dummy_adapter = InstrumentAdapterFactory.get_instrument_adapter( 'DummyInstrumentAdapter', '') self.assertIsInstance(dummy_adapter, DummyInstrumentAdapter) InstrumentAdapterFactory.adapter_instances.pop( ('DummyInstrumentAdapter', '')) dummy_adapter.close_instrument()
def test_factory_new_instance(self): dummy_adapter1 = InstrumentAdapterFactory.get_instrument_adapter( 'DummyInstrumentAdapter', 'dev1') dummy_adapter2 = InstrumentAdapterFactory.get_instrument_adapter( 'DummyInstrumentAdapter', 'dev2') self.assertIsNot(dummy_adapter1, dummy_adapter2) dummy_adapter3 = InstrumentAdapterFactory.get_instrument_adapter( 'DummyInstrumentAdapter', 'dev1') self.assertIs(dummy_adapter1, dummy_adapter3) dummy_adapter1.close_instrument() dummy_adapter2.close_instrument()
def test_apply_config(self): with patch('qilib.configuration_helper.adapters.spi_rack_instrument_adapter.SPI_rack') as spi_mock, \ patch('qcodes_contrib_drivers.drivers.QuTech.F1d.F1d_module') as f1d_module_mock: address = 'spirack1_module3' adapter_name = 'F1dInstrumentAdapter' instrument_name = '{0}_{1}'.format(adapter_name, address) SerialPortResolver.serial_port_identifiers = {'spirack1': 'COMnumber_test'} f1d_adapter = InstrumentAdapterFactory.get_instrument_adapter(adapter_name, address) spi_mock.assert_called() f1d_module_mock.assert_called() self.assertEqual(address, f1d_adapter.address) self.assertEqual(f1d_module_mock(), f1d_adapter.instrument.f1d) self.assertEqual(instrument_name, f1d_adapter.instrument.name) f1d_adapter.apply(self.mock_config) f1d_adapter.instrument.f1d.set_IQ_filter.assert_called_with(3) f1d_adapter.instrument.f1d.set_I_gain.assert_called_with('low') f1d_adapter.instrument.f1d.set_Q_gain.assert_called_with('high') f1d_adapter.instrument.f1d.enable_remote.assert_called_with(True) parameter_name = 'Q_gain' self.mock_config[parameter_name]['value'] = None error_message = f'The following parameter\(s\) of .* \[\'{parameter_name}\'\]\!' self.assertRaisesRegex(ValueError, error_message, f1d_adapter.apply, self.mock_config) f1d_adapter.instrument.close()
def test_read_apply(self): with patch.object(zhinst.utils, 'create_api_session', return_value=3 * (MagicMock(),)): adapter = InstrumentAdapterFactory.get_instrument_adapter('ZIUHFLIInstrumentAdapter', 'dev4242') adapter.instrument.scope_trig_level(0.2) trigger_level = adapter.instrument.scope_trig_level.raw_value self.assertEqual(0.2, trigger_level) with self.assertLogs(level='ERROR') as log_grabber: config = adapter.read() expected_regex = "Parameter values of ZIUHFLIInstrumentAdapter_dev4242 are .*" self.assertRegex(log_grabber.records[0].message, expected_regex) self.assertTrue(all(['val_mapping' not in config[key] for key in config.keys()])) adapter.instrument.scope_trig_level(0.7) trigger_level = adapter.instrument.scope_trig_level.raw_value self.assertEqual(0.7, trigger_level) adapter.apply(config) trigger_level = adapter.instrument.scope_trig_level.raw_value self.assertEqual(0.2, trigger_level) parameter_name = 'scope_trig_level' config[parameter_name]['value'] = None error_message = f'The following parameter\(s\) of .* \[\'{parameter_name}\'\]\!' self.assertRaisesRegex(ValueError, error_message, adapter.apply, config) adapter.instrument.close()
def test_stop(self): with patch('qilib.configuration_helper.adapters.hdawg8_instrument_adapter.ZIHDAWG8') as dawg: adapter_name = 'ZIHDAWG8InstrumentAdapter' hdawg_adapter = InstrumentAdapterFactory.get_instrument_adapter(adapter_name, 'dev41') hdawg_adapter.stop(1) dawg.assert_has_calls([call().stop_awg(1)]) hdawg_adapter.instrument.close()
def test_apply_config(self): with patch('qilib.configuration_helper.adapters.spi_rack_instrument_adapter.SPI_rack') as spi_mock, \ patch('qcodes_contrib_drivers.drivers.QuTech.S5i.S5i_module') as s5i_module_mock: address = 'spirack1_module3' adapter_name = 'S5iInstrumentAdapter' instrument_name = '{0}_{1}'.format(adapter_name, address) SerialPortResolver.serial_port_identifiers = { 'spirack1': 'COMnumber_test' } s5i_adapter = InstrumentAdapterFactory.get_instrument_adapter( adapter_name, address) spi_mock.assert_called() s5i_module_mock.assert_called() self.assertEqual(address, s5i_adapter.address) self.assertEqual(s5i_module_mock(), s5i_adapter.instrument.s5i) self.assertEqual(instrument_name, s5i_adapter.instrument.name) s5i_adapter.apply(self.mock_config) s5i_adapter.instrument.s5i.enable_output_soft.assert_called_with( True) s5i_adapter.instrument.s5i.set_stepsize.assert_called_with( 1000000.0) s5i_adapter.instrument.s5i.set_frequency.assert_called_with( 41000000.0) s5i_adapter.instrument.s5i.set_output_power.assert_called_with(10) parameter_name = 'frequency' self.mock_config[parameter_name]['value'] = None error_message = f'The following parameter\(s\) of .* \[\'{parameter_name}\'\]\!' self.assertRaisesRegex(ValueError, error_message, s5i_adapter.apply, self.mock_config) s5i_adapter.instrument.close()
def test_upload(self): with patch('qilib.configuration_helper.adapters.hdawg8_instrument_adapter.ZIHDAWG8') as dawg: adapter_name = 'ZIHDAWG8InstrumentAdapter' hdawg_adapter = InstrumentAdapterFactory.get_instrument_adapter(adapter_name, 'dev40') hdawg_adapter.upload('program', 1) dawg.assert_has_calls([call().upload_sequence_program(1, 'program')]) hdawg_adapter.instrument.close()
def test_read_config(self): with patch('qilib.configuration_helper.adapters.spi_rack_instrument_adapter.SPI_rack') as spi_mock, \ patch('qcodes_contrib_drivers.drivers.QuTech.D5a.D5a_module') as d5a_module_mock: range_4volt_bi = 2 d5a_module_mock.range_4V_bi = range_4volt_bi d5a_module_mock().span.__getitem__.return_value = range_4volt_bi mock_config = copy.deepcopy(self.mock_config) address = 'spirack1_module3' SerialPortResolver.serial_port_identifiers = {'spirack1': 'COMnumber_test'} d5a_adapter = InstrumentAdapterFactory.get_instrument_adapter('D5aInstrumentAdapter', address) spi_mock.assert_called() d5a_module_mock.assert_called() self.assertEqual(address, d5a_adapter.address) self.assertEqual(d5a_adapter.instrument.d5a, d5a_module_mock()) identity = 'IDN' mock_config[identity] = 'version_test' mocked_snapshot = {'name': 'd5a', 'parameters': mock_config.copy()} d5a_adapter.instrument.snapshot = MagicMock(return_value=mocked_snapshot) config = d5a_adapter.read() mock_config.pop(identity) self.assertTrue(identity not in config.keys()) self.assertDictEqual(mock_config, config) d5a_adapter.instrument.close()
def test_apply_config_ok(self): with patch('qilib.configuration_helper.adapters.spi_rack_instrument_adapter.SPI_rack') as spi_mock, \ patch('qcodes_contrib_drivers.drivers.QuTech.D5a.D5a_module') as d5a_module_mock: range_4volt_bi = 2 d5a_module_mock.range_4V_bi = range_4volt_bi d5a_module_mock().span.__getitem__.return_value = range_4volt_bi address = 'spirack1_module3' adapter_name = 'D5aInstrumentAdapter' instrument_name = '{0}_{1}'.format(adapter_name, address) SerialPortResolver.serial_port_identifiers = {'spirack1': 'COMnumber_test'} d5a_adapter = InstrumentAdapterFactory.get_instrument_adapter(adapter_name, address) spi_mock.assert_called() d5a_module_mock.assert_called() self.assertEqual(address, d5a_adapter.address) self.assertEqual(d5a_module_mock(), d5a_adapter.instrument.d5a) self.assertEqual(instrument_name, d5a_adapter.instrument.name) mocked_snapshot = {'name': 'd5a', 'parameters': self.mock_config} d5a_adapter.instrument.snapshot = MagicMock(return_value=mocked_snapshot) d5a_adapter.apply(self.mock_config) d5a_adapter.instrument.d5a.set_voltage.assert_not_called() d5a_adapter.instrument.d5a.change_span_update.assert_not_called() d5a_adapter.instrument.d5a.get_stepsize.assert_not_called() self.assertEqual(d5a_adapter.instrument.parameters['dac1'].step, 20) self.assertEqual(d5a_adapter.instrument.parameters['dac1'].inter_delay, 0.05) self.assertEqual(d5a_adapter.instrument.parameters['dac1'].unit, 'V') d5a_adapter.instrument.close()
def test_apply_config(self): with patch('qilib.configuration_helper.adapters.spi_rack_instrument_adapter.SPI_rack') as spi_mock, \ patch('qcodes_contrib_drivers.drivers.QuTech.M2j.M2j_module') as m2j_module_mock: address = 'spirack1_module3' adapter_name = 'M2jInstrumentAdapter' instrument_name = '{0}_{1}'.format(adapter_name, address) SerialPortResolver.serial_port_identifiers = { 'spirack1': 'COMnumber_test' } m2j_adapter = InstrumentAdapterFactory.get_instrument_adapter( adapter_name, address) spi_mock.assert_called() m2j_module_mock.assert_called() self.assertEqual(address, m2j_adapter.address) self.assertIsInstance(m2j_adapter.instrument, M2j) self.assertEqual(instrument_name, m2j_adapter.instrument.name) self.assertIsNone(m2j_adapter.instrument.gain()) gain = 35 ref_scale = 3325 m2j_adapter.apply(self.mock_config) self.assertEqual(gain, m2j_adapter.instrument.gain()) m2j_adapter.instrument.m2j.get_level.assert_not_called() m2j_adapter.instrument.m2j.set_gain.assert_called_once_with( ref_scale) parameter_name = 'gain' self.mock_config[parameter_name]['value'] = None error_message = f'The following parameter\(s\) of .* \[\'{parameter_name}\'\]\!' self.assertRaisesRegex(ValueError, error_message, m2j_adapter.apply, self.mock_config) m2j_adapter.instrument.close()
def test_apply(self): qilib.configuration_helper.adapters.DummyInstrumentAdapter = DummyInstrumentAdapter dummy_adapter = InstrumentAdapterFactory.get_instrument_adapter('DummyInstrumentAdapter', 'some_address') dummy_adapter.instrument.amplitude(1) dummy_adapter.instrument.frequency(1) dummy_adapter.instrument.enable_output(False) mock_virtual_dac_instance = MagicMock() with patch('qtt.instrument_drivers.adapters.virtual_dac_instrument_adapter.VirtualDAC') as mock_virtual_dac: mock_virtual_dac.return_value = mock_virtual_dac_instance adapter = VirtualDACInstrumentAdapter('spirack3_module3') mock_virtual_dac.assert_called() config = PythonJsonStructure() config['config'] = snapshot['parameters'] config['boundaries'] = {'P1': (-10, 10)} config['gate_map'] = {'P1': (0, 1)} config['instruments'] = {dummy_adapter.name: {'address': 'some_address', 'adapter_class_name': 'DummyInstrumentAdapter', 'config': {'amplitude': {'value': 1e-3}, 'frequency': {'value': 130e6}, 'enable_output': {'value': True}}}} adapter.apply(config) mock_virtual_dac_instance.set_boundaries.assert_called_with({'P1': (-10, 10)}) mock_virtual_dac_instance.add_instruments.assert_called_with([dummy_adapter.instrument]) self.assertDictEqual({'P1': (0, 1)}, mock_virtual_dac_instance.gate_map) self.assertEqual(1e-3, dummy_adapter.instrument.amplitude()) self.assertEqual(130e6, dummy_adapter.instrument.frequency()) self.assertTrue(dummy_adapter.instrument.enable_output()) dummy_adapter.close_instrument()
def test_read_config(self): with patch('qilib.configuration_helper.adapters.spi_rack_instrument_adapter.SPI_rack') as spi_mock, \ patch('qcodes_contrib_drivers.drivers.QuTech.M2j.M2j_module') as m2j_module_mock: address = 'spirack1_module3' SerialPortResolver.serial_port_identifiers = { 'spirack1': 'COMnumber_test' } m2j_adapter = InstrumentAdapterFactory.get_instrument_adapter( 'M2jInstrumentAdapter', address) mock_config = copy.deepcopy(self.mock_config) spi_mock.assert_called() m2j_module_mock.assert_called() self.assertEqual(address, m2j_adapter.address) self.assertEqual(m2j_adapter.instrument.m2j, m2j_module_mock()) identity = 'IDN' mock_config[identity] = 'version_test' mocked_snapshot = {'name': 'd5a', 'parameters': mock_config.copy()} m2j_adapter.instrument.snapshot = MagicMock( return_value=mocked_snapshot) config = m2j_adapter.read() mock_config.pop(identity) self.assertTrue(identity not in config.keys()) self.assertDictEqual(mock_config, config) m2j_adapter.instrument.close()
def test_close_instrument(self): dawg_instance = MagicMock() with patch('qilib.configuration_helper.adapters.hdawg8_instrument_adapter.ZIHDAWG8', return_value=dawg_instance): adapter_name = 'ZIHDAWG8InstrumentAdapter' hdawg_adapter = InstrumentAdapterFactory.get_instrument_adapter(adapter_name, 'dev43') hdawg_adapter.close_instrument() dawg_instance.close.assert_called_once_with()
def test_constructor(self): with patch( 'qcodes_contrib_drivers.drivers.Spectrum.M4i.M4i') as m4i_mock: adapter = InstrumentAdapterFactory.get_instrument_adapter( self.adapter_name, self.address) m4i_mock.assert_called_with(self.instrument_name, cardid=self.address) m4i_mock.return_value.initialize_channels.assert_called_once() self.assertEqual(adapter.instrument, m4i_mock())
def test_different_instrument_name_raises_error(self): dummy_adapter = InstrumentAdapterFactory.get_instrument_adapter( 'DummyInstrumentAdapter', 'dev12', 'some_name') self.assertEqual('some_name', dummy_adapter.instrument.name) error_msg = 'An adapter exist with different instrument name \'some_name\' != \'other_name\'' self.assertRaisesRegex(ValueError, error_msg, InstrumentAdapterFactory.get_instrument_adapter, 'DummyInstrumentAdapter', 'dev12', 'other_name') dummy_adapter.close_instrument()
def __init__(self, address: str) -> None: """ Instantiate a LockInStimulus. Args: address: A unique ID of a UHFLI device. """ self._adapter = InstrumentAdapterFactory.get_instrument_adapter('ZIUHFLIInstrumentAdapter', address) self._uhfli = self._adapter.instrument
def test_filter_parameters(self): with patch( 'qilib.configuration_helper.adapters.spi_rack_instrument_adapter.SPI_rack' ) as spi_mock: address = 'COM_testport_read' spi_adapter = InstrumentAdapterFactory.get_instrument_adapter( 'SpiRackInstrumentAdapter', address) parameters = MagicMock() reply = spi_adapter._filter_parameters(parameters) self.assertEqual(parameters, reply)
def test_apply(self): with patch.object(zhinst.utils, 'create_api_session', return_value=3 * (MagicMock(),)) as daq, \ patch.object(qilib.configuration_helper.adapters.hdawg8_instrument_adapter.ZIHDAWG8, 'download_device_node_tree', return_value=self.node_tree) as ddnt: address = 'test_dev1' adapter_name = 'ZIHDAWG8InstrumentAdapter' instrument_name = "{0}_{1}".format(adapter_name, address) hdawg_adapter = InstrumentAdapterFactory.get_instrument_adapter(adapter_name, address) daq.assert_called() ddnt.assert_called() daq.return_value[1].getInt.return_value = 1 daq.return_value[1].getDouble.return_value = 0.5 daq.return_value[1].getString.return_value = "Test String" daq.return_value[1].getAsEvent.return_value = [0.1, 0.2, 0.3, 0.4] hdawg_adapter.instrument.set_channel_grouping(1) hdawg_adapter.instrument.enable_channel(0) hdawg_adapter.instrument.upload_waveform(1, [0.1, 0.2, 0.3, 0.4], 1) hdawg_adapter.instrument.set('sines_0_amplitudes_0', 0.5) self.assertEqual(instrument_name, hdawg_adapter.instrument.name) self.assertEqual(1, hdawg_adapter.instrument.system_awg_channelgrouping.raw_value) self.assertEqual(1, hdawg_adapter.instrument.sigouts_0_on.raw_value) self.assertEqual(1, hdawg_adapter.instrument.awgs_1_waveform_index.raw_value) self.assertListEqual([0.1, 0.2, 0.3, 0.4], hdawg_adapter.instrument.awgs_1_waveform_data.raw_value) self.assertEqual(0.5, hdawg_adapter.instrument.sines_0_amplitudes_0.raw_value) config = hdawg_adapter.read() hdawg_adapter.instrument.set_channel_grouping(0) hdawg_adapter.instrument.disable_channel(0) hdawg_adapter.instrument.upload_waveform(1, [0.3, 0.4, 0.5, 0.6], 0) hdawg_adapter.instrument.set('sines_0_amplitudes_0', 0.2) self.assertEqual(0, hdawg_adapter.instrument.system_awg_channelgrouping.raw_value) self.assertEqual(0, hdawg_adapter.instrument.sigouts_0_on.raw_value) self.assertEqual(0, hdawg_adapter.instrument.awgs_1_waveform_index.raw_value) self.assertListEqual([0.3, 0.4, 0.5, 0.6], hdawg_adapter.instrument.awgs_1_waveform_data.raw_value) self.assertEqual(0.2, hdawg_adapter.instrument.sines_0_amplitudes_0.raw_value) hdawg_adapter.apply(config) self.assertEqual(1, hdawg_adapter.instrument.system_awg_channelgrouping.raw_value) self.assertEqual(1, hdawg_adapter.instrument.sigouts_0_on.raw_value) self.assertEqual(1, hdawg_adapter.instrument.awgs_1_waveform_index.raw_value) self.assertListEqual([0.1, 0.2, 0.3, 0.4], hdawg_adapter.instrument.awgs_1_waveform_data.raw_value) self.assertEqual(0.5, hdawg_adapter.instrument.sines_0_amplitudes_0.raw_value) parameter_name = 'sigouts_0_on' config = hdawg_adapter.read() config[parameter_name]['value'] = None error_message = f'The following parameter\(s\) of .*' self.assertRaisesRegex(ValueError, error_message, hdawg_adapter.apply, config) hdawg_adapter.instrument.close()
def test_filtered_parameters(self): with patch.object(zhinst.utils, 'create_api_session', return_value=3 * (MagicMock(),)): adapter = InstrumentAdapterFactory.get_instrument_adapter('ZIUHFLIInstrumentAdapter', 'dev4242') adapter.instrument.demod1_oscillator(1) adapter.instrument.value_mapping = 1 with self.assertLogs(level='ERROR') as log_grabber: config = adapter.read() self.assertTrue(all(['val_mapping' not in config[key] for key in config.keys()])) self.assertTrue('IDN' not in config.keys())
def test_full_node_tree(self): with patch.object(zhinst.utils, 'create_api_session', return_value=3 * (MagicMock(),)) as daq, \ open(self._node_tree_path) as node_tree, \ patch.object(qilib.configuration_helper.adapters.hdawg8_instrument_adapter.ZIHDAWG8, 'download_device_node_tree', return_value=json.load(node_tree)) as ddnt: hdawg_adapter = InstrumentAdapterFactory.get_instrument_adapter('ZIHDAWG8InstrumentAdapter', 'test-dev2') ddnt.assert_called() daq.assert_called() daq.return_value[1].getInt.return_value = 1 daq.return_value[1].getDouble.return_value = 0.5 daq.return_value[1].getString.return_value = "Test String" daq.return_value[1].getAsEvent.return_value = [0.1, 0.2, 0.3, 0.4] config_original = hdawg_adapter.read() daq.return_value[1].getInt.return_value = 0 daq.return_value[1].getDouble.return_value = 0.7 daq.return_value[1].getString.return_value = "New String" daq.return_value[1].getAsEvent.return_value = [0.5, 0.6, 0.7, 0.8] config_new = hdawg_adapter.read() # Apply original config and assert parameters have been updated hdawg_adapter.apply(config_original) for parameter in config_original: if parameter in hdawg_adapter.instrument.parameters and hasattr( hdawg_adapter.instrument.parameters[parameter], 'set'): self.assertIn(hdawg_adapter.instrument.parameters[parameter].raw_value, [1, 0.5, "Test String", [0.1, 0.2, 0.3, 0.4]]) # Apply new config and assert parameters are updated hdawg_adapter.apply(config_new) for parameter in config_new: if parameter in hdawg_adapter.instrument.parameters and hasattr( hdawg_adapter.instrument.parameters[parameter], 'set'): self.assertIn(hdawg_adapter.instrument.parameters[parameter].raw_value, [0, 0.7, "New String", [0.5, 0.6, 0.7, 0.8]]) self.assertNotIn(hdawg_adapter.instrument.parameters[ parameter].raw_value, [1, 0.5, "Test String", [0.1, 0.2, 0.3, 0.4]]) # Apply original config again and check if updated hdawg_adapter.apply(config_original) for parameter in config_original: if parameter in hdawg_adapter.instrument.parameters and hasattr( hdawg_adapter.instrument.parameters[parameter], 'set'): self.assertIn(hdawg_adapter.instrument.parameters[parameter].raw_value, [1, 0.5, "Test String", [0.1, 0.2, 0.3, 0.4]]) self.assertNotIn(hdawg_adapter.instrument.parameters[parameter].raw_value, [0, 0.7, "New String", [0.5, 0.6, 0.7, 0.8]]) hdawg_adapter.instrument.close()
def test_apply_config(self): with patch( 'qilib.configuration_helper.adapters.spi_rack_instrument_adapter.SPI_rack' ) as spi_mock: address = 'COM_testport_apply' spi_adapter = InstrumentAdapterFactory.get_instrument_adapter( 'SpiRackInstrumentAdapter', address) self.assertIsInstance(spi_adapter.instrument, MagicMock) spi_mock.assert_called_once_with(address, baud='115200', timeout=1) spi_adapter.apply(self.mock_config) spi_adapter.instrument.apply_settings.assert_called_once_with( self.serial_port_settings)
def add_adapter_to_instrument_adapter(self, instrument_adapter_class_name: str, address: str, instrument_name: Optional[str] = None) -> None: """ Adds a DAC to the virtual DAC and cache a corresponding instrument adapter. Args: instrument_adapter_class_name: Name of the InstrumentAdapter subclass. address: Address of the physical instrument. instrument_name: An optional name for the underlying instrument. """ adapter = InstrumentAdapterFactory.get_instrument_adapter(instrument_adapter_class_name, address, instrument_name) if adapter.name not in self._dac_adapters: self._dac_adapters[adapter.name] = adapter if adapter.instrument not in self.instrument.instruments: self.instrument.add_instruments([adapter.instrument])
def test_apply_config_none_value_raises(self): with patch( 'qcodes_contrib_drivers.drivers.Spectrum.M4i.M4i') as m4i_mock: adapter = InstrumentAdapterFactory.get_instrument_adapter( self.adapter_name, self.address) adapter.instrument.parameters = { key: MagicMock() for key in self.snapshot.keys() } parameter_name = 'channel_1' self.snapshot[parameter_name]['value'] = None error_message = f'The following parameter\(s\) of .* \[\'{parameter_name}\'\]\!' self.assertRaisesRegex(ValueError, error_message, adapter.apply, self.snapshot)
def test_read_config(self): with patch( 'qcodes_contrib_drivers.drivers.Spectrum.M4i.M4i') as m4i_mock: adapter = InstrumentAdapterFactory.get_instrument_adapter( self.adapter_name, self.address) adapter.instrument.snapshot.return_value = { 'parameters': copy.deepcopy(self.snapshot) } self.assertEqual(adapter.instrument, m4i_mock()) self.assertEqual(adapter.name, self.instrument_name) config = adapter.read() expected_config = self.snapshot.copy() expected_config.pop('IDN') expected_config.pop('box_averages') expected_config.pop('card_available_length') self.assertDictEqual(expected_config, config)
def __init__(self, adapter_class_name: str, address: str, storage: StorageInterface, tag: Optional[List[str]] = None, configuration: Optional[PythonJsonStructure] = None, instrument_name: Optional[str] = None) -> None: """ A set of instrument configurations Args instrument_adapter_class_name: Name of the InstrumentAdapter subclass address: Address of the physical instrument storage: Any storage that implements the StorageInterface tag: A unique identifier for a instrument configuration set configuration: The instrument configuration instrument_name: User defined name for the instrument """ self._adapter_class_name = adapter_class_name self._address = address self._storage = storage self._instrument_name = instrument_name self._adapter = InstrumentAdapterFactory.get_instrument_adapter(adapter_class_name, address, instrument_name) self._configuration = PythonJsonStructure() if configuration is None else configuration self._tag = [self.STORAGE_BASE_TAG, adapter_class_name, StorageInterface.datetag_part()] if tag is None else tag
def test_apply_config_parameters_called(self): with patch( 'qcodes_contrib_drivers.drivers.Spectrum.M4i.M4i') as m4i_mock: adapter = InstrumentAdapterFactory.get_instrument_adapter( self.adapter_name, self.address) adapter.instrument.parameters = { key: MagicMock() for key in self.snapshot.keys() } adapter.apply(self.snapshot) self.assertEqual(adapter.name, self.instrument_name) self.assertEqual(adapter.instrument, m4i_mock()) calls = [ call(key, parameter['value']) for key, parameter in snapshot.items() if parameter['value'] is not None ] adapter.instrument.set.assert_has_calls(calls, any_order=True)
def test_apply_config_raises_configuration_mismatch_error(self): with patch('qilib.configuration_helper.adapters.spi_rack_instrument_adapter.SPI_rack'), \ patch('qcodes_contrib_drivers.drivers.QuTech.D5a.D5a_module') as d5a_module_mock: range_4volt_bi = 2 dac_value = 0.03997802734375 d5a_module_mock.range_4V_bi = range_4volt_bi d5a_module_mock().span.__getitem__.return_value = range_4volt_bi d5a_module_mock().voltages.__getitem__.return_value = dac_value address = 'spirack1_module3' SerialPortResolver.serial_port_identifiers = {'spirack1': 'COMnumber_test'} d5a_adapter = InstrumentAdapterFactory.get_instrument_adapter('D5aInstrumentAdapter', address) update_config = copy.deepcopy(self.mock_config) update_config['dac1']['value'] = 49.978 mocked_snapshot = {'name': 'd5a', 'parameters': update_config} d5a_adapter.instrument.snapshot = MagicMock(return_value=mocked_snapshot) error_msg = "Configuration for dac1 does not match: '39.97802734375' != '49.978'" self.assertRaisesRegex(ConfigurationError, error_msg, d5a_adapter.apply, self.mock_config) d5a_adapter.instrument.close()
def test_int64_convert_to_int(self): with patch.object(zhinst.utils, 'create_api_session', return_value=3 * (MagicMock(),)): adapter = InstrumentAdapterFactory.get_instrument_adapter('ZIUHFLIInstrumentAdapter', 'dev4142') adapter.instrument.scope_trig_level(np.int64(1)) self.assertIsInstance(adapter.instrument.scope_trig_level.raw_value, np.int64) adapter.instrument.scope_trig_hystabsolute(np.int64(2)) self.assertIsInstance(adapter.instrument.scope_trig_hystabsolute.raw_value, np.int64) with self.assertLogs(level='ERROR') as log_grabber: config = adapter.read() expected_regex = "Parameter values of ZIUHFLIInstrumentAdapter_dev4142 are .*" self.assertRegex(log_grabber.records[0].message, expected_regex) adapter.apply(config) for parameter in adapter.instrument.parameters.values(): if hasattr(parameter, 'raw_value'): self.assertNotIsInstance(parameter.raw_value, np.int64) adapter.instrument.close()
def test_read_filter_out_val_mapping(self): with patch('qilib.configuration_helper.adapters.keysight_e8267d_instrument_adapter.Keysight_E8267D') \ as mock_instrument: mock_instrument_instance = MagicMock() mock_instrument.return_value = mock_instrument_instance mock_instrument_instance.snapshot.return_value = { 'name': 'some_keysight', 'parameters': { 'good_parameter': {'value': 42}, 'filtered_parameter_1': {'val_mapping': {1: True, 0: False}, 'value': False}, 'filtered_parameter_2': {'on_off_mapping': {1: 'ON', 0: 'OFF'}, 'value': 'OFF'} } } adapter = InstrumentAdapterFactory.get_instrument_adapter('KeysightE8267DInstrumentAdapter', 'fake') config = adapter.read() self.assertNotIn('val_mapping', config['filtered_parameter_1']) self.assertNotIn('on_off_mapping', config['filtered_parameter_2']) self.assertEqual(42, config['good_parameter']['value']) self.assertFalse(config['filtered_parameter_1']['value']) self.assertEqual('OFF', config['filtered_parameter_2']['value']) adapter.close_instrument()