# Path: pyinform/error.py
# class InformError(Exception):
#     """
#     InformError signifies an error occurred in a call to inform.
#     """
# 
#     def __init__(self, e=-1, func=None):
#         msg = error_string(e)
# 
#         if func is None:
#             msg = "an inform error occurred - \"{}\"".format(msg)
#         else:
#             msg = "an inform error occurred in `{}` - \"{}\"".format(func, msg)
# 
#         super(InformError, self).__init__(msg)
# 
#         self.error_code = e if isinstance(e, ErrorCode) else ErrorCode(e)
# 
# Path: pyinform/entropyrate.py
# def entropy_rate(series, k, local=False):
#     """
#     Compute the average or local entropy rate of a time series with history
#     length *k*.
# 
#     :param series: the time series
#     :type series: sequence or ``numpy.ndarray``
#     :param int k: the history length
#     :param bool local: compute the local active information
#     :returns: the average or local entropy rate
#     :rtype: float or ``numpy.ndarray``
#     :raises ValueError: if the time series has no initial conditions
#     :raises ValueError: if the time series is greater than 2-D
#     :raises InformError: if an error occurs within the ``inform`` C call
#     """
#     xs = np.ascontiguousarray(series, np.int32)
# 
#     if xs.ndim == 0:
#         raise ValueError("empty timeseries")
#     elif xs.ndim > 2:
#         raise ValueError("dimension greater than 2")
# 
#     b = max(2, np.amax(xs) + 1)
# 
#     data = xs.ctypes.data_as(POINTER(c_int))
#     if xs.ndim == 1:
#         n, m = 1, xs.shape[0]
#     else:
#         n, m = xs.shape
# 
#     e = ErrorCode(0)
# 
#     if local is True:
#         q = max(0, m - k)
#         er = np.empty((n, q), dtype=np.float64)
#         out = er.ctypes.data_as(POINTER(c_double))
#         _local_entropy_rate(data, c_ulong(n), c_ulong(m), c_int(b), c_ulong(k), out, byref(e))
#     else:
#         er = _entropy_rate(data, c_ulong(n), c_ulong(m), c_int(b), c_ulong(k), byref(e))
# 
#     error_guard(e)
# 
#     return er


import unittest
from pyinform.error import InformError
from pyinform.entropyrate import entropy_rate
    def test_entropy_rate_negative_states(self):
        with self.assertRaises(InformError):
            entropy_rate([-1, 0, 0], k=3)

    def test_entropy_rate_base_2(self):
        self.assertAlmostEqual(0.000000,
                               entropy_rate([1, 1, 0, 0, 1, 0, 0, 1], 2), places=6)

        self.assertAlmostEqual(0.000000,
                               entropy_rate([1, 0, 0, 0, 0, 0, 0, 0, 0], 2), places=6)

        self.assertAlmostEqual(0.679270,
                               entropy_rate([0, 0, 1, 1, 1, 1, 0, 0, 0], 2), places=6)

        self.assertAlmostEqual(0.515663,
                               entropy_rate([1, 0, 0, 0, 0, 0, 0, 1, 1], 2), places=6)

        self.assertAlmostEqual(0.463587,
                               entropy_rate([0, 0, 0, 0, 0, 1, 1, 0, 0], 2), places=6)

        self.assertAlmostEqual(0.463587,
                               entropy_rate([0, 0, 0, 0, 1, 1, 0, 0, 0], 2), places=6)

        self.assertAlmostEqual(0.679270,
                               entropy_rate([1, 1, 1, 0, 0, 0, 0, 1, 1], 2), places=6)

        self.assertAlmostEqual(0.679270,
                               entropy_rate([0, 0, 0, 1, 1, 1, 1, 0, 0], 2), places=6)

        self.assertAlmostEqual(0.515663,

# Path: pyinform/error.py
# class InformError(Exception):
#     """
#     InformError signifies an error occurred in a call to inform.
#     """
# 
#     def __init__(self, e=-1, func=None):
#         msg = error_string(e)
# 
#         if func is None:
#             msg = "an inform error occurred - \"{}\"".format(msg)
#         else:
#             msg = "an inform error occurred in `{}` - \"{}\"".format(func, msg)
# 
#         super(InformError, self).__init__(msg)
# 
#         self.error_code = e if isinstance(e, ErrorCode) else ErrorCode(e)
# 
# Path: pyinform/entropyrate.py
# def entropy_rate(series, k, local=False):
#     """
#     Compute the average or local entropy rate of a time series with history
#     length *k*.
# 
#     :param series: the time series
#     :type series: sequence or ``numpy.ndarray``
#     :param int k: the history length
#     :param bool local: compute the local active information
#     :returns: the average or local entropy rate
#     :rtype: float or ``numpy.ndarray``
#     :raises ValueError: if the time series has no initial conditions
#     :raises ValueError: if the time series is greater than 2-D
#     :raises InformError: if an error occurs within the ``inform`` C call
#     """
#     xs = np.ascontiguousarray(series, np.int32)
# 
#     if xs.ndim == 0:
#         raise ValueError("empty timeseries")
#     elif xs.ndim > 2:
#         raise ValueError("dimension greater than 2")
# 
#     b = max(2, np.amax(xs) + 1)
# 
#     data = xs.ctypes.data_as(POINTER(c_int))
#     if xs.ndim == 1:
#         n, m = 1, xs.shape[0]
#     else:
#         n, m = xs.shape
# 
#     e = ErrorCode(0)
# 
#     if local is True:
#         q = max(0, m - k)
#         er = np.empty((n, q), dtype=np.float64)
#         out = er.ctypes.data_as(POINTER(c_double))
#         _local_entropy_rate(data, c_ulong(n), c_ulong(m), c_int(b), c_ulong(k), out, byref(e))
#     else:
#         er = _entropy_rate(data, c_ulong(n), c_ulong(m), c_int(b), c_ulong(k), byref(e))
# 
#     error_guard(e)
# 
#     return er


# Path: test/test_entropyrate.py
import unittest
from pyinform.error import InformError
from pyinform.entropyrate import entropy_rate

    def test_entropy_rate_negative_states(self):
        with self.assertRaises(InformError):
            entropy_rate([-1, 0, 0], k=3)

    def test_entropy_rate_base_2(self):
        self.assertAlmostEqual(0.000000,
                               entropy_rate([1, 1, 0, 0, 1, 0, 0, 1], 2), places=6)

        self.assertAlmostEqual(0.000000,
                               entropy_rate([1, 0, 0, 0, 0, 0, 0, 0, 0], 2), places=6)

        self.assertAlmostEqual(0.679270,
                               entropy_rate([0, 0, 1, 1, 1, 1, 0, 0, 0], 2), places=6)

        self.assertAlmostEqual(0.515663,
                               entropy_rate([1, 0, 0, 0, 0, 0, 0, 1, 1], 2), places=6)

        self.assertAlmostEqual(0.463587,
                               entropy_rate([0, 0, 0, 0, 0, 1, 1, 0, 0], 2), places=6)

        self.assertAlmostEqual(0.463587,
                               entropy_rate([0, 0, 0, 0, 1, 1, 0, 0, 0], 2), places=6)

        self.assertAlmostEqual(0.679270,
                               entropy_rate([1, 1, 1, 0, 0, 0, 0, 1, 1], 2), places=6)

        self.assertAlmostEqual(0.679270,
                               entropy_rate([0, 0, 0, 1, 1, 1, 1, 0, 0], 2), places=6)

        self.assertAlmostEqual(0.515663,

                               entropy_rate([0, 0, 0, 0, 0, 0, 1, 1, 0], 2), places=6)