예제 #1
0
def test_monitor_loaded_if_specified(example_station_config):
    st = Station(config_file=example_station_config, use_monitor=True)
    st.load_instrument('mock_dac')
    assert Monitor.running is not None
    assert len(Monitor.running._parameters) == 1
    assert Monitor.running._parameters[0].name == 'ch1'
    Monitor.running.stop()
예제 #2
0
def test_add_component():
    bob = DummyInstrument('bob', gates=['one'])
    station = Station()
    station.add_component(bob, 'bob')

    assert ['bob'] == list(station.components.keys())
    assert bob == station.components['bob']
예제 #3
0
def test_monitor_loaded_by_default_if_in_config(example_station_config):
    qcodes.config["station"]['use_monitor'] = True
    st = Station(config_file=example_station_config)
    st.load_instrument('mock_dac')
    assert Monitor.running is not None
    assert len(Monitor.running._parameters) == 1
    assert Monitor.running._parameters[0].name == 'ch1'
    Monitor.running.stop()
예제 #4
0
def test_close_all_registered_instruments():
    names = [f'some_name_{i}' for i in range(10)]
    instrs = [Instrument(name=name) for name in names]
    st = Station(*instrs)
    for name in names:
        assert name in Instrument._all_instruments
    st.close_all_registered_instruments()
    for name in names:
        assert name not in Instrument._all_instruments
예제 #5
0
def test_dynamic_reload_of_file(example_station_config):
    st = Station(config_file=example_station_config)
    mock_dac = st.load_instrument('mock_dac')
    assert 'ch1' in mock_dac.parameters
    with open(example_station_config, 'r') as f:
        filedata = f.read().replace('ch1', 'gate1')
    with open(example_station_config, 'w') as f:
        f.write(filedata)
    mock_dac = st.load_instrument('mock_dac')
    assert 'ch1' not in mock_dac.parameters
    assert 'gate1' in mock_dac.parameters
예제 #6
0
def test_add_component_without_specifying_name():
    """
    Test that station looks for 'name' attribute in the component and uses it
    """
    bob = DummyInstrument('bob', gates=['one'])
    assert hasattr(bob, 'name')
    assert 'bob' == bob.name

    station = Station()
    station.add_component(bob)

    assert ['bob'] == list(station.components.keys())
    assert bob == station.components['bob']
예제 #7
0
    def __init__(self,
                 name,
                 address,
                 mirrorfactors=[1, 1],
                 verbose=1,
                 **kwargs):
        logging.debug(__name__ + ' : Initializing instrument')
        super().__init__(name, address, **kwargs)

        self.station = Station()
        self._address = address
        self._values = {}
        self.verbose = verbose
        self._total_cycle_num = 100
        self._sampling_frequency = 1000e3
        self.visa_handle.baud_rate = 57600
        self.set_sampling_frequency(self._sampling_frequency)
        self.mirrorfactors = mirrorfactors

        # Add parameters
        #self.add_parameter('mode',
        #                   set_cmd=functools.partial(self.write_to_serial, 130))
        #self.add_parameter('ch1_cycle_num',
        #                   get_cmd=functools.partial(self.ask_from_serial, 1))
        #self.add_parameter('ch2_cycle_num',
        #                   get_cmd=functools.partial(self.ask_from_serial, 5))
        #self.add_parameter('measurement_done',
        #                   get_cmd=self.get_measurement_done)
        #self.add_parameter('total_cycle_num',
        #                   get_cmd=self.get_total_cycle_num,
        #                   set_cmd=self.set_total_cycle_num)
        #self.add_parameter('ch1_datapoint_num',
        #                   get_cmd=self.get_ch1_datapoint_num)
        #self.add_parameter('ch2_datapoint_num',
        #                   get_cmd=self.get_ch2_datapoint_num)
        #self.add_parameter('data',
        #                   get_cmd=self.station.alazar.data.get())
        #self.add_parameter('ch1=_data',
        #                   get_cmd=self.channel.data.get())
        #self.add_parameter('sampling_frequency',
        #                   get_cmd=self.station.alazar.get_sample_rate,
        #                   set_cmd=self.station.alazar.set_sample_rate)

        v = self.visa_handle
        # make sure term characters are ignored
        logging.debug(__name__ + ' : set termchar settings')
        v.set_visa_attribute(visa.constants.VI_ATTR_TERMCHAR_EN, 0)
        v.set_visa_attribute(visa.constants.VI_ATTR_ASRL_END_IN, 0)
        logging.debug(__name__ + ' : completed initialization')
예제 #8
0
 def setUpClass(cls):
     cls.p1 = Parameter('p1', get_cmd=None, set_cmd=None, vals=Numbers(-10, 10))
     cls.p2 = Parameter('p2', get_cmd=None, set_cmd=None,  vals=Numbers(-10, 10))
     cls.p3 = Parameter('p3', get_cmd=None, set_cmd=None,  vals=Numbers(-10, 10))
     cls.instr = DummyInstrument('dummy_bunny')
     cls.p4_crazy = NanReturningParameter('p4_crazy', instrument=cls.instr)
     Station()
예제 #9
0
def test_load_from_db_dataset_moved(meas_with_registered_param, DMM, DAC,
                                    tmp_path):
    Station(DAC, DMM)
    with meas_with_registered_param.run(
            dataset_class=DataSetType.DataSetInMem) as datasaver:
        for set_v in np.linspace(0, 25, 10):
            DAC.ch1.set(set_v)
            get_v = DMM.v1()
            datasaver.add_result((DAC.ch1, set_v), (DMM.v1, get_v))

    ds = datasaver.dataset
    ds.add_metadata("foo", "bar")
    ds.export(export_type="netcdf", path=tmp_path)

    export_path = ds.export_info.export_paths["nc"]
    new_path = str(Path(export_path).parent / "someotherfilename.nc")

    shutil.move(export_path, new_path)

    with pytest.warns(UserWarning,
                      match="Could not load raw data for dataset with guid"):
        loaded_ds = load_by_id(ds.run_id)

    assert isinstance(loaded_ds, DataSetInMem)
    assert loaded_ds.snapshot == ds.snapshot
    assert loaded_ds.export_info == ds.export_info
    assert loaded_ds.metadata == ds.metadata

    assert "foo" in loaded_ds.metadata.keys()
    assert "export_info" in loaded_ds.metadata.keys()
    assert loaded_ds.cache.data() == {}

    loaded_ds.set_netcdf_location(new_path)

    assert loaded_ds.cache.data().keys() == ds.cache.data().keys()
예제 #10
0
def test_load_from_db(meas_with_registered_param, DMM, DAC, tmp_path):
    Station(DAC, DMM)
    with meas_with_registered_param.run(
            dataset_class=DataSetType.DataSetInMem) as datasaver:
        for set_v in np.linspace(0, 25, 10):
            DAC.ch1.set(set_v)
            get_v = DMM.v1()
            datasaver.add_result((DAC.ch1, set_v), (DMM.v1, get_v))

    ds = datasaver.dataset
    ds.add_metadata("foo", "bar")
    ds.export(export_type="netcdf", path=tmp_path)

    ds.add_metadata("metadata_added_after_export", 69)

    loaded_ds = load_by_id(ds.run_id)
    assert isinstance(loaded_ds, DataSetInMem)
    assert loaded_ds.snapshot == ds.snapshot
    assert loaded_ds.export_info == ds.export_info
    assert loaded_ds.metadata == ds.metadata

    assert "foo" in loaded_ds.metadata.keys()
    assert "export_info" in loaded_ds.metadata.keys()
    assert "metadata_added_after_export" in loaded_ds.metadata.keys()
    assert loaded_ds.metadata["metadata_added_after_export"] == 69

    compare_datasets(ds, loaded_ds)
예제 #11
0
def test_station_getitem():
    bob = DummyInstrument('bob', gates=['one'])
    station = Station(bob)

    assert bob == station['bob']

    with pytest.raises(KeyError, match='bobby'):
        _ = station.components['bobby']
예제 #12
0
def test_station_config_can_be_loaded_from_snapshot(example_station):
    assert station_config_has_been_loaded(example_station)
    # ensure that we can correctly dump config which is a subclass of UserDict
    configdump = json.dumps(example_station.config, cls=NumpyJSONEncoder)
    # as this is now a regular dict we can load it back
    loaded_config = json.loads(configdump)
    # now lets ensure that we can recreate the
    # station from the loaded config
    # first we need to get a yaml repr of the data
    yaml = YAML()
    with StringIO() as output:
        yaml.dump(loaded_config, output)
        yaml_repr = output.getvalue()
    # which we can then reload into the station
    new_station = Station(default=False)
    new_station.load_config(yaml_repr)
    assert example_station.config == new_station.config
예제 #13
0
def test_station():
    bob = DummyInstrument('bob', gates=['one'])
    station = Station(bob)

    assert ['bob'] == list(station.components.keys())
    assert bob == station.components['bob']

    assert station == station.default
    assert station == Station.default
예제 #14
0
def test_snapshot():
    station = Station()

    empty_snapshot = station.snapshot()
    assert {'instruments': {},
            'parameters': {},
            'components': {},
            'config': None,
            'default_measurement': []
            } == empty_snapshot

    instrument = DummyInstrument('instrument', gates=['one'])
    station.add_component(instrument)
    instrument_snapshot = instrument.snapshot()

    parameter = Parameter('parameter', label='Label', unit='m')
    station.add_component(parameter)
    parameter_snapshot = parameter.snapshot()

    component = DumyPar('component')
    component.metadata['smth'] = 'in the way she moves'
    station.add_component(component)
    component_snapshot = component.snapshot()

    snapshot = station.snapshot()

    assert isinstance(snapshot, dict)
    assert ['instruments',
            'parameters',
            'components',
            'config',
            'default_measurement'
            ] == list(snapshot.keys())

    assert ['instrument'] == list(snapshot['instruments'].keys())
    assert instrument_snapshot == snapshot['instruments']['instrument']

    assert ['parameter'] == list(snapshot['parameters'].keys())
    assert parameter_snapshot == snapshot['parameters']['parameter']

    assert ['component'] == list(snapshot['components'].keys())
    assert component_snapshot == snapshot['components']['component']

    assert [] == snapshot['default_measurement']
예제 #15
0
    def __init__(self,
                 filename: Optional[str] = None,
                 station: Optional[Station] = None) -> None:
        self.monitor_parameters = {}

        if station is None:
            station = Station.default or Station()
        self.station = station

        self.load_file(filename)
예제 #16
0
def test_station_delegated_attributes():
    bob = DummyInstrument('bob', gates=['one'])
    station = Station(bob)

    assert bob == station.bob

    with pytest.raises(AttributeError, match="'Station' object and its "
                                             "delegates have no attribute "
                                             "'bobby'"):
        _ = station.bobby
예제 #17
0
def test_station_after_instrument_is_closed():
    """
    Test that station is aware of the fact that its components could be
    removed within the lifetime of the station. Here we instantiate an
    instrument, add it to a station, then close the instrument, and then
    perform an action on the station to ensure that the closed instrument
    does not break the work of the station object.
    """
    bob = DummyInstrument('bob', gates=['one'])

    station = Station(bob)

    assert bob == station['bob']

    bob.close()

    # 'bob' is closed, but it is still part of the station
    assert bob == station['bob']

    # check that snapshot method executes without exceptions
    snapshot = station.snapshot()

    # check that 'bob's snapshot is not here (because 'bob' is closed,
    # hence it was ignored, and even removed from the station by
    # `snapshot_base` method)
    assert {
        'instruments': {},
        'parameters': {},
        'components': {},
        'config': None,
        'default_measurement': []
    } == snapshot

    # check that 'bob' has been removed from the station
    with pytest.raises(KeyError, match='bob'):
        _ = station.components['bob']

    # check that 'bob' has been removed from the station, again
    with pytest.raises(KeyError,
                       match='Component bob is not part of the '
                       'station'):
        station.remove_component('bob')
예제 #18
0
    def test_loop_writing_2D(self):
        # pass
        station = Station()
        MockPar = MockParabola(name='Loop_writing_test_2D')
        station.add_component(MockPar)
        loop = Loop(MockPar.x[-100:100:20]).loop(MockPar.y[-50:50:10]).each(
            MockPar.skewed_parabola)
        data1 = loop.run(name='MockLoop_hdf5_test', formatter=self.formatter)
        data2 = DataSet(location=data1.location, formatter=self.formatter)
        data2.read()
        for key in data2.arrays.keys():
            self.checkArraysEqual(data2.arrays[key], data1.arrays[key])

        metadata_equal, err_msg = compare_dictionaries(data1.metadata,
                                                       data2.metadata,
                                                       'original_metadata',
                                                       'loaded_metadata')
        self.assertTrue(metadata_equal, msg='\n' + err_msg)
        self.formatter.close_file(data1)
        self.formatter.close_file(data2)
예제 #19
0
def test_config_validation_failure_on_file():
    with pytest.raises(ValidationWarning):
        test_config = """
instruments:
  mock:
    driver: qcodes.tests.instrument_mocks.DummyInstrument
invalid_keyword:
  more_errors: 42
    """
        with config_file_context(test_config) as filename:
            Station(config_file=filename)
예제 #20
0
def test_station_config_path_resolution(example_station_config):
    config = qcodes.config["station"]

    # There is no default yaml file present that defines a station
    # so we expect the station config not to be loaded.
    assert not station_config_has_been_loaded(Station())

    path = Path(example_station_config)
    config["default_file"] = str(path)
    # Now the default file with the station configuration is specified, and
    # this file exists, hence we expect the Station to have the station
    # configuration loaded upon initialization.
    assert station_config_has_been_loaded(Station())

    config["default_file"] = path.name
    config["default_folder"] = str(path.parent)
    # Here the default_file setting contains only the file name, and the
    # default_folder contains the path to the folder where this file is
    # located, hence we again expect that the station configuration is loaded
    # upon station initialization.
    assert station_config_has_been_loaded(Station())

    config["default_file"] = 'random.yml'
    config["default_folder"] = str(path.parent)
    # In this case, the station configuration file specified in the qcodes
    # config does not exist, hence the initialized station is not expected to
    # have station configuration loaded.
    assert not station_config_has_been_loaded(Station())

    config["default_file"] = str(path)
    config["default_folder"] = r'C:\SomeOtherFolder'
    # In this case, the default_file setting of the qcodes config contains
    # absolute path to the station configuration file, while the default_folder
    # setting is set to some non-existent folder.
    # In this situation, the value of the default_folder will be ignored,
    # but because the file specified in default_file setting exists,
    # the station will be initialized with the loaded configuration.
    assert station_config_has_been_loaded(Station())

    config["default_file"] = None
    config["default_folder"] = str(path.parent)
    # When qcodes config has only the default_folder setting specified to an
    # existing folder, and default_file setting is not specified, then
    # passing the name of a station configuration file, that exists in that
    # default_folder, as an argument to the Station is expected to result
    # in a station with loaded configuration.
    assert station_config_has_been_loaded(Station(config_file=path.name))

    config["default_file"] = None
    config["default_folder"] = None
    # In case qcodes config does not have default_file and default_folder
    # settings specified, passing an absolute file path as an argument to the
    # station is expected to result in a station with loaded configuration.
    assert station_config_has_been_loaded(Station(config_file=str(path)))
예제 #21
0
    def __init__(
        self,
        station: Optional[Station] = None,
        filename: str = nt.config["device_config_file"],
    ) -> None:

        self.monitor_parameters: Dict[int, Any] = {}

        if station is None:
            station = Station.default or Station()
        self.station = station

        self.filename = filename

        self.load_file()
예제 #22
0
def test_station_config_created_with_multiple_config_files():

    test_config1 = f"""
        instruments:
          mock_dac1:
            type: qcodes.tests.instrument_mocks.DummyInstrument
            enable_forced_reconnect: true
            init:
              gates: {{"ch1", "ch2"}}
            parameters:
              ch1:
                monitor: true
    """
    test_config2 = f"""
        instruments:
          mock_dac2:
            type: qcodes.tests.instrument_mocks.DummyInstrument
    """
    with config_files_context(test_config1, test_config2) as file_list:
        assert station_config_has_been_loaded(Station(config_file=file_list))
예제 #23
0
def test_snapshot_creation_for_types_not_supported_by_builtin_json(experiment):
    """
    Test that `Measurement`/`Runner`/`DataSaver` infrastructure
    successfully dumps station snapshots in JSON format in cases when the
    snapshot contains data of types that are not supported by python builtin
    `json` module, for example, numpy scalars.
    """
    p1 = ManualParameter('p_np_int32', initial_value=numpy.int32(5))
    p2 = ManualParameter('p_np_float16', initial_value=numpy.float16(5.0))
    p3 = ManualParameter('p_np_array',
                         initial_value=numpy.meshgrid((1, 2), (3, 4)))
    p4 = ManualParameter('p_np_bool', initial_value=numpy.bool_(False))

    station = Station(p1, p2, p3, p4)

    measurement = Measurement(experiment, station)

    # we need at least 1 parameter to be able to run the measurement
    measurement.register_custom_parameter('dummy')

    with measurement.run() as data_saver:
        # we do this in order to create a snapshot of the station and add it
        # to the database
        pass

    snapshot = data_saver.dataset.snapshot

    assert 5 == snapshot['station']['parameters']['p_np_int32']['value']
    assert 5 == snapshot['station']['parameters']['p_np_int32']['raw_value']

    assert 5.0 == snapshot['station']['parameters']['p_np_float16']['value']
    assert 5.0 == snapshot['station']['parameters']['p_np_float16'][
        'raw_value']

    lst = [[[1, 2], [1, 2]], [[3, 3], [4, 4]]]
    assert lst == snapshot['station']['parameters']['p_np_array']['value']
    assert lst == snapshot['station']['parameters']['p_np_array']['raw_value']

    assert False is snapshot['station']['parameters']['p_np_bool']['value']
    assert False is snapshot['station']['parameters']['p_np_bool']['raw_value']
예제 #24
0
def test_add_component_with_no_name():
    """
    Test that station comes up with a name for components without 'name'
    attribute
    """
    bob = {'name', 'bob'}
    station = Station()
    station.add_component(bob)

    assert ['component0'] == list(station.components.keys())
    assert bob == station.components['component0']

    jay = {'name', 'jay'}
    station.add_component(jay)

    assert ['component0', 'component1'] == list(station.components.keys())
    assert jay == station.components['component1']
예제 #25
0
def test_remove_component():
    bob = DummyInstrument('bob', gates=['one'])
    station = Station()
    station.add_component(bob, 'bob')

    assert ['bob'] == list(station.components.keys())
    assert bob == station.components['bob']

    bob2 = station.remove_component('bob')

    with pytest.raises(KeyError, match='bob'):
        _ = station.components['bob']
    assert bob == bob2

    with pytest.raises(KeyError, match='Component bobby is not part of the '
                                       'station'):
        _ = station.remove_component('bobby')
예제 #26
0
    def __init__(self, filename: Optional[str] = None,
                 station: Optional[Station] = None) -> None:
        self.monitor_parameters = {}

        if station is None:
            station = Station.default or Station()
        self.station = station
        self.filename = filename

        self.load_file(self.filename)
        for instrument_name in self._instrument_config.keys():
            # TODO: check if name is valid (does not start with digit, contain
            # dot, other signs etc.)
            method_name = f'load_{instrument_name}'
            if method_name.isidentifier():
                setattr(self, method_name,
                        partial(self.load_instrument,
                                identifier=instrument_name))
            else:
                log.warning(f'Invalid identifier: ' +
                            f'for the instrument {instrument_name} no ' +
                            f'lazy loading method {method_name} could be ' +
                            'created in the StationConfigurator')
예제 #27
0
def test_station_snapshot_during_measurement(experiment, dac, dmm,
                                             pass_station):
    station = Station()
    station.add_component(dac)
    station.add_component(dmm, 'renamed_dmm')

    snapshot_of_station = station.snapshot()

    if pass_station:
        measurement = Measurement(experiment, station)
    else:
        # in this branch of the `if` we expect that `Measurement` object
        # will be initialized with `Station.default` which is equal to the
        # station object that is instantiated above
        measurement = Measurement(experiment)

    measurement.register_parameter(dac.ch1)
    measurement.register_parameter(dmm.v1, setpoints=[dac.ch1])

    with measurement.run() as data_saver:
        data_saver.add_result((dac.ch1, 7), (dmm.v1, 5))

    # 1. Test `get_metadata('snapshot')` method

    json_snapshot_from_dataset = data_saver.dataset.get_metadata('snapshot')
    snapshot_from_dataset = json.loads(json_snapshot_from_dataset)

    expected_snapshot = {'station': snapshot_of_station}
    assert expected_snapshot == snapshot_from_dataset

    # 2. Test `snapshot_raw` property

    assert json_snapshot_from_dataset == data_saver.dataset.snapshot_raw

    # 3. Test `snapshot` property

    assert expected_snapshot == data_saver.dataset.snapshot
예제 #28
0
def test_config_validation_comprehensive_config():
    Station(config_file=os.path.join(
        get_qcodes_path(), 'dist', 'tests', 'station', 'example.station.yaml')
    )
예제 #29
0
def test_monitor_not_loaded_if_specified(example_station_config):
    st = Station(config_file=example_station_config, use_monitor=False)
    st.load_instrument('mock_dac')
    assert Monitor.running is None
예제 #30
0
def test_monitor_not_loaded_by_default(example_station_config):
    st = Station(config_file=example_station_config)
    st.load_instrument('mock_dac')
    assert Monitor.running is None