Esempio n. 1
0
def get_data_from_hdf_file(timestamp=None, data_file=None,
                           close_file=True, file_id=None, mode='r'):
    """
    Return the measurement data stored in Experimental Data group of the file
    specified by timestamp.
    :param timestamp: (str) measurement timestamp of form YYYYMMDD_hhmmsss
    :param data_file: (HDF file) opened HDF5 file
    :param close_file: (bool) whether to close the HDF5 file
    :return: numpy array with measurement data
    """
    if data_file is None:
        if timestamp is None:
            raise ValueError('Please provide either timestamp or data_file.')
        folder = a_tools.get_folder(timestamp)
        h5filepath = a_tools.measurement_filename(folder, file_id=file_id)
        data_file = h5py.File(h5filepath, mode)
    try:
        group = data_file['Experimental Data']
        if 'Data' in group:
            dataset = np.array(group['Data'])
        else:
            raise KeyError('Data was not found in Experimental Data.')
        if close_file:
            data_file.close()
    except Exception as e:
        data_file.close()
        raise e
    return dataset
    def _fill_experimental_values_with_ReparkingRamsey(experimental_values,
                                                       timestamp, qubit):
        """
        Fills the experimental_values dictionary with the experimental values
        from ReparkingRamsey experiments.

        NOTE if there are multiple measurements of the same transition at the
        same voltage, the (chronological) last one will be used.

        Arguments:
            experimental_values: dictionary to fill with experimental data
            timestamp: timestamp
            qubit: qubit (qubit object) or qubit name (str)
        """
        path = a_tools.data_from_time(timestamp)
        filepath = a_tools.measurement_filename(path)
        data = h5py.File(filepath, "r")

        if "_ge_" in filepath:
            transition = "ge"
        elif "_ef_" in filepath:
            transition = "ef"
        else:
            log.warning(
                "Transition not recognized. Only ge and ef transitions "
                "are supported.")
            return

        if type(qubit) is str:
            qubit_name = qubit
        else:
            qubit_name = qubit.name
        if not qubit_name in filepath:
            return

        try:
            dc_voltages = np.array(data["Experimental Data"]
                                   ["Experimental Metadata"]["dc_voltages"])
            for i in range(len(dc_voltages)):
                freq = float(
                    np.array(data["Analysis"]["Processed data"]
                             ["analysis_params_dict"][
                                 qubit_name + f"_"
                                 f"{i}"]["exp_decay"].attrs["new_qb_freq"]))
                freq_std = float(
                    np.array(data["Analysis"]["Processed data"]
                             ["analysis_params_dict"][
                                 qubit_name + f"_"
                                 f"{i}"]["exp_decay"].attrs["new_qb_freq"]))
                voltage = dc_voltages[i]
                HamiltonianFittingAnalysis._fill_experimental_values(
                    experimental_values, voltage, transition, freq)
        except:
            log.warning(
                "Could not get reparking data from file {}".format(filepath))
    def test_save_quantities_of_interest(self):
        # Test based on test below to get a dummy dataset
        ts = '20161124_162604'
        a = ba.BaseDataAnalysis()
        a.proc_data_dict['quantities_of_interest'] = {'a': 5}
        a.timestamps = [ts]
        a.save_quantities_of_interest()

        fn = a_tools.measurement_filename(a_tools.data_from_time(ts))
        with h5py.File(fn, 'r') as file:
            saved_val = float(file['Analysis']['quantities_of_interest'].attrs['a'])

        assert saved_val == 5
    def _fill_experimental_values_with_Ramsey(experimental_values, timestamp,
                                              qubit, fluxlines_dict):
        """
        Fills the experimental_values dictionary with the experimental values
        from Ramsey experiments.

        NOTE if there are multiple measurements of the same transition at the
        same voltage, the (chronological) last one will be used.

        Arguments:
            experimental_values: dictionary containing the experimental values
            timestamp: timestamp
            qubit: qubit (qubit object) or qubit name (str)
            fluxlines_dict: dictionary containing the fluxline ids (necessary
                to determine voltage)
        """
        path = a_tools.data_from_time(timestamp)
        filepath = a_tools.measurement_filename(path)
        data = h5py.File(filepath, "r")

        if "_ge_" in filepath:
            transition = "ge"
        elif "_ef_" in filepath:
            transition = "ef"
        else:
            raise ValueError("Transition not recognized. Only ge and ef "
                             "transitions are supported.")

        if type(qubit) is str:
            qubit_name = qubit
        else:
            qubit_name = qubit.name
        if not qubit_name in filepath:
            return

        try:
            freq = data["Analysis"]["Processed data"]["analysis_params_dict"][
                qubit_name]["exp_decay"].attrs["new_qb_freq"]
            freq_std = data["Analysis"]["Processed data"][
                "analysis_params_dict"][qubit_name]["exp_decay"].attrs[
                    "new_qb_freq_stderr"]
            voltage = float(data["Instrument settings"]["DCSource"].attrs[
                fluxlines_dict[qubit_name].name])
            HamiltonianFittingAnalysis._fill_experimental_values(
                experimental_values, voltage, transition, freq)
        except KeyError:
            log.warning(f"Could not get ramsey data from file {filepath}")
    def test_save_fit_results(self):
        # strictly speaking an integration test as it relies on the cond
        # oscillation analysis, but the only thing tested here is
        # if the value of the fit_result is saved.
        ts = '20181126_131143'
        a = ma2.Conditional_Oscillation_Analysis(t_start=ts)

        exp_val = a.fit_res['cos_fit_off'].params['amplitude'].value

        fn = a_tools.measurement_filename(a_tools.data_from_time(ts))
        with h5py.File(fn, 'r') as file:
            saved_val = float(file['Analysis']['cos_fit_off']['params']
                              ['amplitude'].attrs['value'])

        a.fit_res = {}
        a.save_fit_results()
        assert exp_val == saved_val
Esempio n. 6
0
def get_value_names_from_timestamp(timestamp, file_id=None, mode='r'):
    """
    Returns value_names from the HDF5 file specified by timestamp.
    :param timestamp: (str) measurement timestamp of form YYYYMMDD_hhmmsss
    :return: list of value_names
    """
    folder = a_tools.get_folder(timestamp)
    h5filepath = a_tools.measurement_filename(folder, file_id=file_id)
    data_file = h5py.File(h5filepath, mode)
    try:
        channel_names = get_hdf_param_value(data_file['Experimental Data'],
                                            'value_names')
        data_file.close()
        return channel_names
    except Exception as e:
        data_file.close()
        raise e
Esempio n. 7
0
def get_param_from_metadata_group(timestamp=None, param_name=None, file_id=None,
                                  data_file=None, close_file=True, mode='r'):
    """
    Get a parameter with param_name from the Experimental Metadata group in
    the HDF5 file specified by timestamp, or return the whole group if
    param_name is None.
    :param timestamp: (str) measurement timestamp of form YYYYMMDD_hhmmsss
    :param param_name: (str) name of a key in Experimental Metadata group
    :param data_file: (HDF file) opened HDF5 file
    :param close_file: (bool) whether to close the HDF5 file
    :return: the value of the param_name or the whole experimental metadata
    dictionary
    """
    if data_file is None:
        if timestamp is None:
            raise ValueError('Please provide either timestamp or data_file.')
        folder = a_tools.get_folder(timestamp)
        h5filepath = a_tools.measurement_filename(folder, file_id=file_id)
        data_file = h5py.File(h5filepath, mode)

    try:
        if param_name is None:
            group = data_file['Experimental Data']
            return read_dict_from_hdf5({}, group['Experimental Metadata'])

        group = data_file['Experimental Data']['Experimental Metadata']
        if param_name in group:
            group = group[param_name]
            param_value = OrderedDict()
            if isinstance(group, h5py._hl.dataset.Dataset):
                param_value = list(np.array(group).flatten())
                param_value = [x.decode('utf-8') if isinstance(x, bytes)
                               else x for x in param_value]
            else:
                param_value = read_dict_from_hdf5(param_value, group)
        elif param_name in group.attrs:
            param_value = get_hdf_param_value(group, param_name)
        else:
            raise KeyError(f'{param_name} was not found in metadata.')
        if close_file:
            data_file.close()
    except Exception as e:
        data_file.close()
        raise e
    return param_value
Esempio n. 8
0
def open_hdf_file(timestamp=None, folder=None, filepath=None, mode='r', file_id=None):
    """
    Opens the hdf5 file with flexible input parameters. If no parameter is given,
    opens the  hdf5 of the last measurement in reading mode.
    Args:
        :param timestamp: (str) measurement timestamp of form YYYYMMDD_hhmmsss
        :param folder: (str) path to file location
        :param mode filepath: (str) path to hdf5 file. Overwrites timestamp
            and folder
        :param mode: (str) mode to open the file ('r' for read),
            ('r+' for read/write)
        :param file_id: (str) file id
    :return: opened HDF5 file

    """
    if filepath is None:
        if folder is None:
            assert timestamp is not None
            folder = a_tools.get_folder(timestamp)
        filepath = a_tools.measurement_filename(folder, file_id=file_id)
    return h5py.File(filepath, mode)
Esempio n. 9
0
def read_analysis_file(timestamp=None, filepath=None, data_dict=None,
                       file_id=None, ana_file=None, close_file=True, mode='r'):
    """
    Creates a data_dict from an AnalysisResults file as generated by analysis_v3
    :param timestamp: str with a measurement timestamp
    :param filepath: (str) path to file
    :param data_dict: dict where to store the file entries
    :param file_id: suffix to the usual HDF measurement file found from giving
        a measurement timestamp. Defaults to '_AnalysisResults,' the standard
        suffix created by analysis_v3
    :param ana_file: HDF file instance
    :param close_file: whether to close the HDF file at the end
    :param mode: str specifying the HDF read mode (if ana_file is None)
    :return: the data dictionary
    """
    if data_dict is None:
        data_dict = {}
    try:
        if ana_file is None:
            if filepath is None:
                if file_id is None:
                    file_id = 'AnalysisResults'
                folder = a_tools.get_folder(timestamp)
                filepath = a_tools.measurement_filename(folder,
                                                        file_id=f'_{file_id}')
            ana_file = h5py.File(filepath, mode)
        read_from_hdf(data_dict, ana_file)
        if close_file:
            ana_file.close()
    except Exception as e:
        if close_file:
            try:
                ana_file.close()
            except AttributeError:
                # there was an exception before reaching the line above where
                # ana_file is opened
                pass
        raise e
    return data_dict
Esempio n. 10
0
def load_settings_onto_instrument(instrument, load_from_instr=None, folder=None,
                                  label=None,
                                  timestamp=None, **kw):
        '''
        Loads settings from an hdf5 file onto the instrument handed to the
        function.
        By default uses the last hdf5 file in the datadirectory.
        By giving a label or timestamp another file can be chosen as the
        settings file.
        '''

        older_than = None
        instrument_name = instrument.name
        success = False
        count = 0
        while success is False and count < 10:
            try:
                if folder is None:
                    folder = a_tools.get_folder(timestamp, older_than, **kw)
                else:
                    folder = folder
                filepath = a_tools.measurement_filename(folder)
                f = h5py.File(filepath, 'r')
                sets_group = f['Instrument settings']
                if load_from_instr is None:
                    ins_group = sets_group[instrument_name]
                else:
                    ins_group = sets_group[load_from_instr]
                print('Loaded Settings Successfully')
                success = True
            except:
                older_than = os.path.split(folder)[0][-8:] \
                             +'_'+ os.path.split(folder)[1][:6]
                folder = None
                success = False
            count += 1

        if not success:
            print('Could not open settings for instrument "%s"' % (
                instrument_name))
            return False

        for parameter, value in ins_group.attrs.items():
            if value != 'None':  # None is saved as string in hdf5
                if type(value) == str:
                    if value == 'False':
                        instrument.set(parameter, False)
                    else:
                        try:
                            instrument.set(parameter, float(value))
                        except Exception:
                            try:
                                instrument.set(parameter, value)
                            except:
                                print('Could not set parameter: "%s" to "%s" for instrument "%s"' % (
                                    parameter, value, instrument_name))
                        except Exception:
                            try:
                                instrument.set(parameter, int(value))
                            except:
                                print('Could not set parameter: "%s" to "%s" for instrument "%s"' % (
                                    parameter, value, instrument_name))
                else:
                    instrument.set(parameter, value)
        f.close()
        return True
Esempio n. 11
0
def load_settings_onto_instrument_v2(
    instrument,
    load_from_instr: str = None,
    label: str = "",
    filepath: str = None,
    timestamp: str = None,
    ignore_pars: set = None,
):
    """
    Loads settings from an hdf5 file onto the instrument handed to the
    function. By default uses the last hdf5 file in the datadirectory.
    By giving a label or timestamp another file can be chosen as the
    settings file.

    Args:
        instrument (instrument) : instrument onto which settings should be
            loaded
        load_from_instr (str) : optional name of another instrument from
            which to load the settings.
        label (str)           : label used for finding the last datafile
        filepath (str)        : exact filepath of the hdf5 file to load.
            if filepath is specified, this takes precedence over the file
            locating options (label, timestamp etc.).
        timestamp (str)       : timestamp of file in the datadir


    """

    older_than = None
    folder = None
    instrument_name = instrument.name
    success = False
    count = 0
    # Will try multiple times in case the last measurements failed and
    # created corrupt data files.
    while success is False and count < 3:
        if filepath is None:
            folder = a_tools.get_folder(
                timestamp=timestamp, label=label, older_than=older_than
            )
            filepath = a_tools.measurement_filename(folder)
        try:

            f = h5py.File(filepath, "r")
            snapshot = {}
            read_dict_from_hdf5(snapshot, h5_group=f["Snapshot"])

            if load_from_instr is None:
                ins_group = snapshot["instruments"][instrument_name]
            else:
                ins_group = snapshot["instruments"][load_from_instr]
            success = True
        except Exception as e:
            logging.warning("Exception occured reading from {}".format(folder))
            logging.warning(e)
            # This check makes this snippet a bit more robust
            if folder is not None:
                older_than = (
                    os.path.split(folder)[0][-8:] + "_" + os.path.split(folder)[1][:6]
                )
            # important to set all to None, otherwise the try except loop
            # will not look for an earlier data file
            folder = None
            filepath = None
            success = False
        count += 1

    if not success:
        logging.warning(
            'Could not open settings for instrument "%s"' % (instrument_name)
        )
        return False

    for parname, par in ins_group["parameters"].items():
        try:
            if hasattr(instrument.parameters[parname], "set") and (
                par["value"] is not None
            ):
                if ignore_pars is None or parname not in ignore_pars:
                    par_value = par["value"]
                    if type(par_value) == str:
                        try:
                            instrument.parameters[parname].validate(par_value)
                        except TypeError:
                            # This detects that in the hdf5 file the parameter
                            # was saved as string due to type incompatibility
                            par_value = eval(par_value)
                    instrument.set(parname, par_value)
        except Exception as e:
            print(
                'Could not set parameter: "{}" to "{}" '
                'for instrument "{}"'.format(parname, par["value"], instrument_name)
            )
            logging.warning(e)
    f.close()
    return True
Esempio n. 12
0
def load_settings_onto_instrument(
    instrument, load_from_instr=None, folder=None, label=None, timestamp=None, **kw
):
    """
    Loads settings from an hdf5 file onto the instrument handed to the
    function.
    By default uses the last hdf5 file in the datadirectory.
    By giving a label or timestamp another file can be chosen as the
    settings file.
    """

    older_than = None
    instrument_name = instrument.name
    success = False
    count = 0
    while success is False and count < 10:
        try:
            if folder is None:
                folder = a_tools.get_folder(
                    timestamp=timestamp, older_than=older_than, **kw
                )
            else:
                folder = folder
            filepath = a_tools.measurement_filename(folder)
            f = h5py.File(filepath, "r")
            sets_group = f["Instrument settings"]
            if load_from_instr is None:
                ins_group = sets_group[instrument_name]
            else:
                ins_group = sets_group[load_from_instr]
            print("Loaded Settings Successfully")
            success = True
        except:
            older_than = (
                os.path.split(folder)[0][-8:] + "_" + os.path.split(folder)[1][:6]
            )
            folder = None
            success = False
        count += 1

    if not success:
        print('Could not open settings for instrument "%s"' % (instrument_name))
        return False

    for parameter, value in ins_group.attrs.items():
        if value != "None":  # None is saved as string in hdf5
            if type(value) == str:
                if value == "False":
                    try:
                        instrument.set(parameter, False)
                    except:
                        print(
                            'Could not set parameter: "%s" to "%s" for instrument "%s"'
                            % (parameter, value, instrument_name)
                        )
                else:
                    try:
                        instrument.set(parameter, float(value))
                    except Exception:
                        try:
                            instrument.set(parameter, value)
                        except:
                            try:
                                instrument.set(parameter, int(value))
                            except:
                                print(
                                    'Could not set parameter: "%s" to "%s" for instrument "%s"'
                                    % (parameter, value, instrument_name)
                                )
            else:
                instrument.set(parameter, value)
    f.close()
    return True
Esempio n. 13
0
def load_settings_onto_instrument_v2(instrument,
                                     load_from_instr: str = None,
                                     label: str = '',
                                     filepath: str = None,
                                     timestamp: str = None):
    '''
    Loads settings from an hdf5 file onto the instrument handed to the
    function. By default uses the last hdf5 file in the datadirectory.
    By giving a label or timestamp another file can be chosen as the
    settings file.

    Args:
        instrument (instrument) : instrument onto which settings should be
            loaded
        load_from_instr (str) : optional name of another instrument from
            which to load the settings.
        label (str)           : label used for finding the last datafile
        filepath (str)        : exact filepath of the hdf5 file to load.
            if filepath is specified, this takes precedence over the file
            locating options (label, timestamp etc.).
        timestamp (str)       : timestamp of file in the datadir


    '''

    older_than = None
    folder = None
    instrument_name = instrument.name
    success = False
    count = 0
    # Will try multiple times in case the last measurements failed and
    # created corrupt data files.
    while success is False and count < 3:
        if filepath is None:
            folder = a_tools.get_folder(timestamp=timestamp,
                                        label=label,
                                        older_than=older_than)
            filepath = a_tools.measurement_filename(folder)
        try:

            f = h5py.File(filepath, 'r')
            snapshot = {}
            h5d.read_dict_from_hdf5(snapshot, h5_group=f['Snapshot'])

            if load_from_instr is None:
                ins_group = snapshot['instruments'][instrument_name]
            else:
                ins_group = snapshot['instruments'][load_from_instr]
            success = True
        except Exception as e:
            logging.warning('Exception occured reading from {}'.format(folder))
            logging.warning(e)
            # This check makes this snippet a bit more robust
            if folder is not None:
                older_than = os.path.split(folder)[0][-8:] \
                    + '_' + os.path.split(folder)[1][:6]
            # important to set all to None, otherwise the try except loop
            # will not look for an earlier data file
            folder = None
            filepath = None
            success = False
        count += 1

    if not success:
        logging.warning('Could not open settings for instrument "%s"' %
                        (instrument_name))
        return False

    for parname, par in ins_group['parameters'].items():
        try:
            if hasattr(instrument.parameters[parname], 'set'):
                instrument.set(parname, par['value'])
        except Exception as e:
            print('Could not set parameter: "{}" to "{}" '
                  'for instrument "{}"'.format(parname, par['value'],
                                               instrument_name))
            logging.warning(e)
    f.close()
    return True
Esempio n. 14
0
def add_measured_data_hdf(data_dict,
                          folder=None,
                          append_data=False,
                          replace_data=False,
                          **params):
    """
    Extracts the dataset from the "Experimental Data" data of an HDF file and
    stores it in data_dict under "measured_data".

    Args:
        data_dict (dict): extracted dataset will be stored here under the key
            "measured_data"
        folder (str): full path to an hdf file
        append_data (bool): if True, and "measured_data" exists in data_dict,
            the new dataset will be appended
        replace_data (bool): if True, and "measured_data" exists in data_dict,
            it will be replaced by the new dataset

    Keyword args (**params)
        passed to helper_functions.py/add_param, see docstring there

    Returns
        data_dict containing the extracted dataset
    """
    if folder is None:
        folder = hlp_mod.get_param('folders',
                                   data_dict,
                                   raise_error=True,
                                   **params)
        if len(folder) > 0:
            folder = folder[-1]

    h5mode = hlp_mod.get_param('h5mode',
                               data_dict,
                               default_value='r+',
                               **params)
    dtype = hlp_mod.get_param('meas_data_dtype',
                              data_dict,
                              default_value=None,
                              **params)
    if dtype is not None:
        log.warning(f'Setting Experimental data type: {dtype}')
    h5filepath = a_tools.measurement_filename(folder)
    data_file = h5py.File(h5filepath, h5mode)
    meas_data_array = np.array(data_file['Experimental Data']['Data'],
                               dtype=dtype).T
    if 'measured_data' in data_dict:
        if replace_data:
            data_dict['measured_data'] = meas_data_array
            data_dict['data_replaced'] = True
        elif append_data:
            if not isinstance(data_dict['measured_data'], list):
                data_dict['measured_data'] = [data_dict['measured_data']]
            data_dict['measured_data'] += [meas_data_array]
            data_dict['data_appended'] = True
        else:
            raise ValueError(
                'Both "append_data" and "replace_data" are False. '
                'Unclear how to add "measured_data" to data_dict.')
    else:
        data_dict['measured_data'] = meas_data_array
    return data_dict
Esempio n. 15
0
def get_params_from_hdf_file(data_dict, params_dict=None, numeric_params=None,
                             add_param_method=None, folder=None, **params):
    """
    Extracts the parameter provided in params_dict from an HDF file
    and saves them in data_dict.
    :param data_dict: OrderedDict where parameters and their values are saved
    :param params_dict: OrderedDict with key being the parameter name that will
        be used as key in data_dict for this parameter, and value being a
        parameter name or a path + parameter name indie the HDF file.
    :param numeric_params: list of parameter names from amount the keys of
        params_dict. This specifies that those parameters are numbers and will
        be converted to floats.
    :param folder: path to file from which data will be read
    :param params: keyword arguments:
        append_value (bool, default: True): whether to append an
            already-existing key
        update_value (bool, default: False): whether to replace an
            already-existing key
        h5mode (str, default: 'r+'): reading mode of the HDF file
        close_file (bool, default: True): whether to close the HDF file(s)
    """
    if params_dict is None:
        params_dict = get_param('params_dict', data_dict, raise_error=True,
                                **params)
    if numeric_params is None:
        numeric_params = get_param('numeric_params', data_dict,
                                   default_value=[], **params)

    # if folder is not specified, will take the last folder in the list from
    # data_dict['folders']
    if folder is None:
        folder = get_param('folders', data_dict, raise_error=True, **params)
        if len(folder) > 0:
            folder = folder[-1]

    h5mode = get_param('h5mode', data_dict, default_value='r', **params)
    h5filepath = a_tools.measurement_filename(folder, **params)
    data_file = h5py.File(h5filepath, h5mode)

    try:
        for save_par, file_par in params_dict.items():
            epd = data_dict
            all_keys = save_par.split('.')
            for i in range(len(all_keys)-1):
                if all_keys[i] not in epd:
                    epd[all_keys[i]] = OrderedDict()
                epd = epd[all_keys[i]]

            if isinstance(epd, list):
                epd = epd[-1]

            if file_par == 'measurementstring':
                add_param(all_keys[-1],
                          [os.path.split(folder)[1][7:]],
                          epd, add_param_method='append')
                continue

            group_name = '/'.join(file_par.split('.')[:-1])
            par_name = file_par.split('.')[-1]
            if group_name == '':
                group = data_file
                attrs = []
            else:
                group = data_file[group_name]
                attrs = list(group.attrs)

            if group_name in data_file or group_name == '':
                if par_name in attrs:
                    add_param(all_keys[-1],
                              get_hdf_param_value(group,
                                                  par_name),
                              epd, add_param_method=add_param_method)
                elif par_name in list(group.keys()) or file_par == '':
                    par = group[par_name] if par_name != '' else group
                    if isinstance(par,
                                  h5py._hl.dataset.Dataset):
                        add_param(all_keys[-1],
                                  np.array(par),
                                  epd, add_param_method=add_param_method)
                    else:
                        add_param(all_keys[-1],
                                  read_dict_from_hdf5(
                                      {}, par),
                                  epd, add_param_method=add_param_method)

            if all_keys[-1] not in epd:
                # search through the attributes of all groups
                for group_name in data_file.keys():
                    if par_name in list(data_file[group_name].attrs):
                        add_param(all_keys[-1],
                                  get_hdf_param_value(data_file[group_name],
                                                      par_name),
                                  epd, add_param_method=add_param_method)

            if all_keys[-1] not in epd:
                log.warning(f'Parameter {file_par} was not found.')
        data_file.close()
    except Exception as e:
        data_file.close()
        raise e

    for par_name in data_dict:
        if par_name in numeric_params:
            if hasattr(data_dict[par_name], '__iter__'):
                data_dict[par_name] = [np.float(p) for p
                                       in data_dict[par_name]]
                data_dict[par_name] = np.asarray(data_dict[par_name])
            else:
                data_dict[par_name] = np.float(data_dict[par_name])

    if get_param('close_file', data_dict, default_value=True, **params):
        data_file.close()
    else:
        if 'data_files' in data_dict:
            data_dict['data_files'] += [data_file]
        else:
            data_dict['data_files'] = [data_file]
    return data_dict
Esempio n. 16
0
def load_settings(instrument,
                  label: str = '',
                  folder: str = None,
                  timestamp: str = None,
                  update=True,
                  **kw):
    '''
    Loads settings from an hdf5 file onto the instrument handed to the
    function. By default uses the last hdf5 file in the datadirectory.
    By giving a label or timestamp another file can be chosen as the
    settings file.

    Args:
        instrument (instrument) : instrument onto which settings
            should be loaded. Can be an instrument name (str) if update is
            set to False.
        label (str)           : label used for finding the last datafile
        folder (str)        : exact filepath of the hdf5 file to load.
            if filepath is specified, this takes precedence over the file
            locating options (label, timestamp etc.).
        timestamp (str)       : timestamp of file in the datadir
        update (bool, default True): if set to False, the loaded settings
            will be returned instead of updating them in the instrument.

    Kwargs:
        params_to_set (list)    : list of strings referring to the parameters
            that should be set for the instrument
    '''
    from numpy import array  # DO not remove. Used in eval(array(...))
    if folder is None:
        folder_specified = False
    else:
        folder_specified = True

    if isinstance(instrument, str) and not update:
        instrument_name = instrument
    else:
        instrument_name = instrument.name
    verbose = kw.pop('verbose', True)
    older_than = kw.pop('older_than', None)
    success = False
    count = 0
    # Will try multiple times in case the last measurements failed and
    # created corrupt data files.
    while success is False and count < 10:
        if folder is None:
            folder = a_tools.get_folder(timestamp=timestamp,
                                        label=label,
                                        older_than=older_than)
        if verbose:
            print('Folder used: {}'.format(folder))

        try:
            filepath = a_tools.measurement_filename(folder)
            f = h5py.File(filepath, 'r')
            sets_group = f['Instrument settings']
            ins_group = sets_group[instrument_name]

            if verbose:
                print('Loaded settings successfully from the HDF file.')

            params_to_set = kw.pop('params_to_set', None)
            if params_to_set is not None:
                if len(params_to_set) == 0:
                    log.warning('The list of parameters to update is empty.')
                if verbose and update:
                    print('Setting parameters {} for {}.'.format(
                        params_to_set, instrument_name))
                params_to_set = [(param, val)
                                 for (param, val) in ins_group.attrs.items()
                                 if param in params_to_set]
            else:
                if verbose and update:
                    print('Setting parameters for {}.'.format(instrument_name))
                params_to_set = [(param, val)
                                 for (param, val) in ins_group.attrs.items()
                                 if param not in getattr(
                                     instrument, '_params_to_not_load', {})]

            if not update:
                params_dict = {parameter : value for parameter, value in \
                        params_to_set}
                f.close()
                return params_dict

            for parameter, value in params_to_set:
                if parameter in instrument.parameters.keys() and \
                        hasattr(instrument.parameters[parameter], 'set'):
                    if value == 'None':  # None is saved as string in hdf5
                        try:
                            instrument.set(parameter, None)
                        except Exception:
                            print('Could not set parameter "%s" to "%s" for '
                                  'instrument "%s"' %
                                  (parameter, value, instrument_name))
                    elif value == 'False':
                        try:
                            instrument.set(parameter, False)
                        except Exception:
                            print('Could not set parameter "%s" to "%s" for '
                                  'instrument "%s"' %
                                  (parameter, value, instrument_name))
                    elif value == 'True':
                        try:
                            instrument.set(parameter, True)
                        except Exception:
                            print('Could not set parameter "%s" to "%s" for '
                                  'instrument "%s"' %
                                  (parameter, value, instrument_name))
                    else:
                        try:
                            instrument.set(parameter, int(value))
                        except Exception:
                            try:
                                instrument.set(parameter, float(value))
                            except Exception:
                                try:
                                    instrument.set(parameter, eval(value))
                                except Exception:
                                    try:
                                        instrument.set(parameter, value)
                                    except Exception:
                                        log.error('Could not set parameter '
                                                  '"%s" to "%s" '
                                                  'for instrument "%s"' %
                                                  (parameter, value,
                                                   instrument_name))

            success = True
            f.close()
        except Exception as e:
            logging.warning(e)
            success = False
            try:
                f.close()
            except:
                pass
            if timestamp is None and not folder_specified:
                print('Trying next folder.')
                older_than = os.path.split(folder)[0][-8:] \
                             + '_' + os.path.split(folder)[1][:6]
                folder = None
            else:
                break
        count += 1

    if not success:
        log.error('Could not open settings for instrument {}.'.format(
            instrument_name))
    print()
    return