Ejemplo n.º 1
0
def load_auto_tuner(file, reserved) -> Autotuner:
    """
    Loads an Autotuner class out of the HDF5 library.
    :param file: File containing the HDF5 library.
    :param reserved: Reserved objects.
    :return: The reloaded Autotuner.
    """
    assert "experiment" in reserved
    hdf5_handle = h5py.File(file, mode="r")
    loaded_data = from_hdf5(hdf5_handle, reserved=reserved)
    return loaded_data["autotuner"]
Ejemplo n.º 2
0
    def test_numpy_serialization(self):
        data = {
            'asd': [1, 2, 3],
            'ggg': [{
                'a': 1
            }, [1, 2, 3], np.arange(2, 9)]
        }

        to_hdf5(self.temp_file.name, 'data', data)

        recovered_data = from_hdf5(self.temp_file.name, reserved=[])

        np.testing.assert_equal({'data': data}, recovered_data)
Ejemplo n.º 3
0
    def test_pure_python_serialization(self):
        data = {
            'asd': [1, 2, 3],
            'ggg': [{
                'a': 1
            }, [1, 2, 3], (1, 2, 3), 'test_stringüöäß', None]
        }

        to_hdf5(self.temp_file.name, 'data', data)

        recovered_data = from_hdf5(self.temp_file.name, reserved=[])

        self.assertEqual({'data': data}, recovered_data)
Ejemplo n.º 4
0
    def test_series_serialization(self):
        ser = pd.Series(data=[1., 53, 95.4], index=['asdf', 'b', 'hh'])
        data = {'asd': [1, 2, 3], 'ggg': [{'a': 1}, [1, 2, 3], ser]}

        to_hdf5(self.temp_file.name, 'data', data)

        recovered_data = from_hdf5(self.temp_file.name, reserved=[])

        pd.testing.assert_series_equal(recovered_data['data']['ggg'][2],
                                       data['ggg'][2])

        recovered_data['data']['ggg'].pop(2)
        data['ggg'].pop(2)
        self.assertEqual({'data': data}, recovered_data)
Ejemplo n.º 5
0
    def test_data_frame_serialization(self):
        df = pd.DataFrame(data=[[1, 2, 3], [4, 5, 6]],
                          index=['asdf', 'b'],
                          columns=[0, 1, 2])
        data = {'asd': [1, 2, 3], 'ggg': [{'a': 1}, [1, 2, 3], df]}

        to_hdf5(self.temp_file.name, 'data', data)

        recovered_data = from_hdf5(self.temp_file.name, reserved=[])

        pd.testing.assert_frame_equal(recovered_data['data']['ggg'][2],
                                      data['ggg'][2])

        recovered_data['data']['ggg'].pop(2)
        data['ggg'].pop(2)
        self.assertEqual({'data': data}, recovered_data)
Ejemplo n.º 6
0
    def test_referenced_object_serialization(self):
        l_1 = [8, 7, 6]
        data = {
            'asd': [1, 2, 3],
            'ggg': [{
                'a': 1
            }, [1, 2, 3], l_1],
            '9000': l_1
        }

        to_hdf5(self.temp_file.name, 'data', data)

        recovered_data = from_hdf5(self.temp_file.name, reserved=[])

        self.assertEqual({'data': data}, recovered_data)

        self.assertIs(recovered_data['data']['9000'],
                      recovered_data['data']['ggg'][2])
Ejemplo n.º 7
0
    def test_custom_class(self):
        with self.assertRaises(AttributeError):

            class MyClass(metaclass=HDF5Serializable):
                def __init__(self, hallo, gib, mir, argumente=None):
                    self.args = dict(hallo=hallo,
                                     gib=gib,
                                     mir=mir,
                                     argumente=argumente)

        class MyClass(metaclass=HDF5Serializable):
            def __init__(self, hallo, gib, mir, argumente=None):
                self.args = dict(hallo=hallo,
                                 gib=gib,
                                 mir=mir,
                                 argumente=argumente)

            def to_hdf5(self):
                return self.args.copy()

            def __eq__(self, other):
                try:
                    np.testing.assert_equal(self.args, other.args)
                    return True
                except AssertionError:
                    return False

        args = dict(hallo=1,
                    gib=[1, 2],
                    mir={'a': 9},
                    argumente=np.arange(6, 8))
        obj = MyClass(**args)
        data = {'asd': [1, 2, 3], 'ggg': [{'a': 1}, [1, 2, 3], obj]}

        to_hdf5(self.temp_file.name, 'data', data)

        recovered_data = from_hdf5(self.temp_file.name, reserved=[])

        np.testing.assert_equal({'data': data}, recovered_data)
Ejemplo n.º 8
0
    def test_serialization(self):
        self.assertIn('KalmanGradientEstimator', serializables)

        np.random.seed(1)
        kal_args = dict(n_values=1,
                        n_pos_dim=2,
                        state_transition_function=np.random.randn(2, 2),
                        initial_gradient=np.random.rand(1, 2),
                        initial_covariance_matrix=np.random.rand(2, 2),
                        measurement_covariance_matrix=np.random.rand(1, 1),
                        process_noise=np.random.rand(2, 2),
                        alpha=1.1)
        kal_grad = KalmanGradient(**kal_args)

        np.testing.assert_equal(kal_args, kal_grad.to_hdf5())

        est_args = dict(kalman_gradient=kal_grad,
                        current_position=pd.Series([1, 2],
                                                   index=['abba', 'xy']),
                        current_value=10.,
                        maximum_covariance=0.5,
                        epsilon=.1)

        estimator = KalmanGradientEstimator(**est_args)
        hdf5 = estimator.to_hdf5()

        self.assertIs(hdf5['kalman_gradient'], kal_grad)
        pd.testing.assert_series_equal(hdf5['current_position'],
                                       est_args['current_position'])
        self.assertEqual(hdf5['current_value'], est_args['current_value'])
        if isinstance(est_args['maximum_covariance'], float):
            pd.testing.assert_series_equal(
                hdf5['maximum_covariance'],
                pd.Series(index=est_args['current_position'].index,
                          data=est_args['maximum_covariance']))
        else:
            pd.testing.assert_series_equal(hdf5['maximum_covariance'],
                                           est_args['maximum_covariance'])

        temp_file = tempfile.NamedTemporaryFile(suffix='.hdf5', delete=False)
        temp_file.close()

        try:
            to_hdf5(temp_file.name, 'estimator', estimator)

            recovered = from_hdf5(temp_file.name, reserved=[])['estimator']

            hdf5 = recovered.to_hdf5()
            pd.testing.assert_series_equal(hdf5['current_position'],
                                           est_args['current_position'])
            self.assertEqual(hdf5['current_value'], est_args['current_value'])
            if isinstance(est_args['maximum_covariance'], float):
                pd.testing.assert_series_equal(
                    hdf5['maximum_covariance'],
                    pd.Series(index=est_args['current_position'].index,
                              data=est_args['maximum_covariance']))
            else:
                pd.testing.assert_series_equal(hdf5['maximum_covariance'],
                                               est_args['maximum_covariance'])

            np.testing.assert_equal(kal_args,
                                    hdf5['kalman_gradient'].to_hdf5())
        finally:
            os.remove(temp_file.name)