コード例 #1
0
    def test_writing_and_reading_dicts_to_hdf5_int_keys(self):
        test_dict = {
            0: {
                "name": "I",
                "theta": 0,
                "phi": 0,
                "type": "ge"
            },
            1: {
                "name": "rX180",
                "theta": 180,
                "phi": 0,
                "type": "ge"
            }
        }
        data_object = h5d.Data(name='test_object', datadir=self.datadir)
        h5d.write_dict_to_hdf5(test_dict, data_object)
        data_object.close()
        filepath = data_object.filepath

        new_dict = {}
        opened_hdf5_file = h5py.File(filepath, 'r')
        h5d.read_dict_from_hdf5(new_dict, opened_hdf5_file)

        self.assertEqual(test_dict.keys(), new_dict.keys())
        self.assertEqual(test_dict[0], new_dict[0])
コード例 #2
0
    def test_storing_and_loading_station_snapshot(self):
        """
        Stores and writes a station (instrument) snapshot.
        """

        self.mock_parabola_2.x(1)
        self.mock_parabola_2.y(2.245)
        self.mock_parabola_2.array_like(np.linspace(0, 11, 23))

        snap = self.station.snapshot(update=True)
        data_object = h5d.Data(name='test_object_snap', datadir=self.datadir)
        h5d.write_dict_to_hdf5(snap, data_object)
        data_object.close()
        filepath = data_object.filepath

        new_dict = {}
        opened_hdf5_file = h5py.File(filepath, 'r')
        h5d.read_dict_from_hdf5(new_dict, opened_hdf5_file)

        self.assertEqual(snap.keys(), new_dict.keys())
        self.assertEqual(snap['instruments'].keys(),
                         new_dict['instruments'].keys())
        mock_parab_pars = snap['instruments']['mock_parabola_2']['parameters']

        self.assertEqual(mock_parab_pars['x']['value'], 1)
        self.assertEqual(mock_parab_pars['y']['value'], 2.245)
        np.testing.assert_array_equal(mock_parab_pars['array_like']['value'],
                                      np.linspace(0, 11, 23))
コード例 #3
0
ファイル: saving.py プロジェクト: QudevETH/PycQED_py3
    def dump_fit_results(self, entry_point):
        """
        Saves the fit results from data_dict['fit_dicts']
        :param entry_point: HDF5 file object to save to or a group within
            this file
        """
        try:
            group = entry_point.create_group('Fit Results')
        except ValueError:
            # If the analysis group already exists.
            group = entry_point['Fit Results']

        # Iterate over all the fit result dicts as not to overwrite
        # old/other analysis
        fit_dicts = hlp_mod.get_param('fit_dicts', self.data_dict)
        for fr_key, fit_dict in fit_dicts.items():
            fit_res = fit_dict['fit_res']
            try:
                fr_group = group.create_group(fr_key)
            except ValueError:
                # If the analysis sub group already exists
                # (each fr_key should be unique)
                # Delete the old group and create a new group
                # (overwrite).
                del group[fr_key]
                fr_group = group.create_group(fr_key)

            d = _convert_dict_rec(fit_res)
            h5d.write_dict_to_hdf5(d, entry_point=fr_group)
コード例 #4
0
def test_wr_rd_hdf5_array():
    datadir = os.path.join(pq.__path__[0], 'tests', 'test_data')
    test_dict = {
        'x': np.linspace(0, 1, 14),
        'y': np.cos(np.linspace(0, 2*np.pi, 11))}
    data_object = h5d.Data(name='test_object', datadir=datadir)
    h5d.write_dict_to_hdf5(test_dict, data_object)
    data_object.close()
    filepath = data_object.filepath

    new_dict = {}
    opened_hdf5_file = h5py.File(filepath, 'r')
    h5d.read_dict_from_hdf5(new_dict, opened_hdf5_file)

    assert test_dict.keys() == new_dict.keys()
    np.testing.assert_allclose(test_dict['x'], new_dict['x'])
    np.testing.assert_allclose(test_dict['y'], new_dict['y'])
コード例 #5
0
ファイル: saving.py プロジェクト: QudevETH/PycQED_py3
    def dump_to_file(self, data_dict, entry_point):
        if 'fit_dicts' in data_dict:
            self.dump_fit_results(entry_point)

        # Iterate over all the fit result dicts as not to overwrite
        # old/other analysis
        for key, value in data_dict.items():
            if key not in self.filter_keys:
                try:
                    group = entry_point.create_group(key)
                except ValueError:
                    del entry_point[key]
                    group = entry_point.create_group(key)

                if isinstance(value, dict):
                    if value.get('is_data_dict', False):
                        self.dump_to_file(value, group)
                    else:
                        value_to_save = {}
                        for k, v in value.items():
                            if k not in self.filter_keys:
                                if isinstance(v, lmfit.model.ModelResult):
                                    frd = {f'{k}': _convert_dict_rec(v)}
                                    self.dump_to_file(frd,
                                                      entry_point=group)
                                else:
                                    value_to_save[k] = v
                        h5d.write_dict_to_hdf5(value_to_save,
                                               entry_point=group)
                elif isinstance(value, np.ndarray):
                    group.create_dataset(key, data=value)
                elif isinstance(value, qtp.qobj.Qobj):
                    group.create_dataset(key, data=value.full())
                elif isinstance(value, lmfit.model.ModelResult):
                    h5d.write_dict_to_hdf5(_convert_dict_rec(value),
                                           entry_point=group)
                else:
                    try:
                        val = repr(value)
                    except KeyError:
                        val = ''
                    group.attrs[key] = val
コード例 #6
0
    def test_writing_and_reading_dicts_to_hdf5(self):
        """
        Tests dumping some random dictionary to hdf5 and reading back the
        stored values. The input dictionary contains:
            - list of ints
            - list of floats
            - nested dict
            - 1D array
            - 2D array

        """
        test_dict = {
            'list_of_ints': list(np.arange(5)),
            'list_of_floats': list(np.arange(5.1)),
            'some_bool': True,
            'weird_dict': {'a': 5},
            'dataset1': np.linspace(0, 20, 31),
            'dataset2': np.array([[2, 3, 4, 5],
                                  [2, 3, 1, 2]]),
            'list_of_mixed_type': ['hello', 4, 4.2, {'a': 5}, [4, 3]],
            'tuple_of_mixed_type': tuple(['hello', 4, 4.2, {'a': 5}, [4, 3]]),
            'a list of strings': ['my ', 'name ', 'is ', 'earl.'],
            'some_np_bool': np.bool(True),
            'list_of_dicts': [{'a': 5}, {'b': 3}],
            'some_int': 3,
            'some_float': 3.5,
            'some_np_int': np.int(3),
            'some_np_float': np.float(3.5)
        }

        data_object = h5d.Data(name='test_object', datadir=self.datadir)
        h5d.write_dict_to_hdf5(test_dict, data_object)
        data_object.close()
        filepath = data_object.filepath

        new_dict = {}
        opened_hdf5_file = h5py.File(filepath, 'r')
        try:
            h5d.read_dict_from_hdf5(new_dict, opened_hdf5_file)
            # objects are not identical but the string representation should be
            self.assertEqual(test_dict.keys(), new_dict.keys())
            self.assertEqual(test_dict['list_of_ints'], new_dict['list_of_ints'])
            self.assertEqual(test_dict['list_of_floats'],
                             new_dict['list_of_floats'])
            self.assertEqual(test_dict['weird_dict'], new_dict['weird_dict'])
            self.assertEqual(test_dict['some_bool'], new_dict['some_bool'])

            self.assertEqual(test_dict['list_of_dicts'],
                             new_dict['list_of_dicts'])

            self.assertEqual(test_dict['list_of_mixed_type'],
                             new_dict['list_of_mixed_type'])
            self.assertEqual(test_dict['list_of_mixed_type'][0],
                             new_dict['list_of_mixed_type'][0])
            self.assertEqual(test_dict['list_of_mixed_type'][2],
                             new_dict['list_of_mixed_type'][2])

            self.assertEqual(test_dict['tuple_of_mixed_type'],
                             new_dict['tuple_of_mixed_type'])
            self.assertEqual(type(test_dict['tuple_of_mixed_type']),
                             type(new_dict['tuple_of_mixed_type']))
            self.assertEqual(test_dict['tuple_of_mixed_type'][0],
                             new_dict['tuple_of_mixed_type'][0])
            self.assertEqual(test_dict['tuple_of_mixed_type'][2],
                             new_dict['tuple_of_mixed_type'][2])

            self.assertEqual(test_dict['some_np_bool'],
                             new_dict['some_np_bool'])
            self.assertEqual(test_dict['some_int'], new_dict['some_int'])
            self.assertEqual(test_dict['some_np_float'], new_dict['some_np_float'])
            self.assertEqual(test_dict['a list of strings'],
                             new_dict['a list of strings'])
            self.assertEqual(test_dict['a list of strings'][0],
                             new_dict['a list of strings'][0])
            opened_hdf5_file.close()
        except Exception as e:
            opened_hdf5_file.close()
            raise e