<commit_before>import numpy as np
import pytest

from hrv.rri import RRi, _validate_rri, _create_time_array, _validate_time
from tests.test_utils import FAKE_RRI


class TestRRiClassArguments:
    def test_transform_rri_to_numpy_array(self):
        validated_rri = _validate_rri(FAKE_RRI)
        np.testing.assert_array_equal(validated_rri, np.array(FAKE_RRI))

    def test_transform_rri_in_seconds_to_miliseconds(self):
        rri_in_seconds = [0.8, 0.9, 1.2]

        validated_rri = _validate_rri(rri_in_seconds)

        assert isinstance(validated_rri, np.ndarray)
        np.testing.assert_array_equal(
                _validate_rri(rri_in_seconds),
                [800, 900, 1200]
        )

    def test_rri_values(self):
        rri = RRi(FAKE_RRI).values

        assert isinstance(rri, np.ndarray)
        np.testing.assert_array_equal(rri, np.array(FAKE_RRI))

    def test_create_time_array(self):
        rri_time = _create_time_array(FAKE_RRI)

        assert isinstance(rri_time, np.ndarray)
        np.testing.assert_array_equal(rri_time, np.cumsum(FAKE_RRI) / 1000)

    def test_rri_time_auto_creation(self):
        rri = RRi(FAKE_RRI)

        np.testing.assert_array_equal(
                rri.time,
                np.cumsum(FAKE_RRI, dtype=np.float32) / 1000.0
        )

    def test_rri_time_passed_as_argument(self):
        rri_time = [1, 2, 3, 4]
        rri = RRi(FAKE_RRI, rri_time)

        assert isinstance(rri.time, np.ndarray)
        np.testing.assert_array_equal(rri.time, np.array([1, 2, 3, 4]))

    def test_raises_exception_if_rri_and_time_havent_same_length(self):
        with pytest.raises(ValueError) as e:
            _validate_time(FAKE_RRI, [1, 2, 3])

        with pytest.raises(ValueError):
            RRi(FAKE_RRI, [1, 2, 3])

        assert e.value.args[0] == (
                'rri and time series must have the same length')

    def test_rri_and_time_have_same_length_in_class_construction(self):
        rri = RRi(FAKE_RRI, [1, 2, 3, 4])

        np.testing.assert_array_equal(rri.time, np.array([1, 2, 3, 4]))

    def test_time_has_no_zero_value_besides_in_first_position(self):
        with pytest.raises(ValueError) as e:
            _validate_time(FAKE_RRI, [1, 2, 0, 3])

        assert e.value.args[0] == (
                'time series cannot have 0 values after first position')

    def test_time_is_monotonically_increasing(self):
        with pytest.raises(ValueError) as e:
            _validate_time(FAKE_RRI, [0, 1, 4, 3])

        assert e.value.args[0] == (
                'time series must be monotonically increasing'
        )

    def test_time_series_have_no_negative_values(self):
        with pytest.raises(ValueError) as e:
            _validate_time(FAKE_RRI, [-1, 1, 2, 3])

        assert e.value.args[0] == ('time series cannot have negative values')

    def test_rri_series_have_no_negative_values(self):
        with pytest.raises(ValueError) as e:
            _validate_rri([0.0, 1.0, 2.0, 3.0])

        with pytest.raises(ValueError):
            _validate_rri([1.0, 2.0, -3.0, 4.0])

        assert e.value.args[0] == ('rri series can only have positive values')

    def test_rri_class_encapsulation(self):
        rri = RRi(FAKE_RRI)

        with pytest.raises(AttributeError):
            rri.rri = [1, 2, 3, 4]

        with pytest.raises(AttributeError):
            rri.time = [1, 2, 3, 4]


class TestRRiClassMethods:
    def test_rri_statistical_values(self):
        rri = RRi(FAKE_RRI)

        # Use assert_almost_equal to keep using float32 instead of float64
        np.testing.assert_almost_equal(rri.mean(),  np.mean(FAKE_RRI), 3)
        np.testing.assert_almost_equal(rri.var(),  np.var(FAKE_RRI), 3)
        np.testing.assert_almost_equal(rri.std(),  np.std(FAKE_RRI), 3)
        np.testing.assert_almost_equal(rri.median(),  np.median(FAKE_RRI), 3)
        np.testing.assert_almost_equal(rri.max(),  np.max(FAKE_RRI), 3)
        np.testing.assert_almost_equal(rri.min(),  np.min(FAKE_RRI), 3)
        np.testing.assert_almost_equal(
                rri.amplitude(),
                np.max(FAKE_RRI) - np.min(FAKE_RRI),
                3
        )<commit_msg>Adding test for RMS<commit_after>import numpy as np
import pytest

from hrv.rri import RRi, _validate_rri, _create_time_array, _validate_time
from tests.test_utils import FAKE_RRI


class TestRRiClassArguments:
    def test_transform_rri_to_numpy_array(self):
        validated_rri = _validate_rri(FAKE_RRI)
        np.testing.assert_array_equal(validated_rri, np.array(FAKE_RRI))

    def test_transform_rri_in_seconds_to_miliseconds(self):
        rri_in_seconds = [0.8, 0.9, 1.2]

        validated_rri = _validate_rri(rri_in_seconds)

        assert isinstance(validated_rri, np.ndarray)
        np.testing.assert_array_equal(
                _validate_rri(rri_in_seconds),
                [800, 900, 1200]
        )

    def test_rri_values(self):
        rri = RRi(FAKE_RRI).values

        assert isinstance(rri, np.ndarray)
        np.testing.assert_array_equal(rri, np.array(FAKE_RRI))

    def test_create_time_array(self):
        rri_time = _create_time_array(FAKE_RRI)

        assert isinstance(rri_time, np.ndarray)
        np.testing.assert_array_equal(rri_time, np.cumsum(FAKE_RRI) / 1000)

    def test_rri_time_auto_creation(self):
        rri = RRi(FAKE_RRI)

        np.testing.assert_array_equal(
                rri.time,
                np.cumsum(FAKE_RRI, dtype=np.float32) / 1000.0
        )

    def test_rri_time_passed_as_argument(self):
        rri_time = [1, 2, 3, 4]
        rri = RRi(FAKE_RRI, rri_time)

        assert isinstance(rri.time, np.ndarray)
        np.testing.assert_array_equal(rri.time, np.array([1, 2, 3, 4]))

    def test_raises_exception_if_rri_and_time_havent_same_length(self):
        with pytest.raises(ValueError) as e:
            _validate_time(FAKE_RRI, [1, 2, 3])

        with pytest.raises(ValueError):
            RRi(FAKE_RRI, [1, 2, 3])

        assert e.value.args[0] == (
                'rri and time series must have the same length')

    def test_rri_and_time_have_same_length_in_class_construction(self):
        rri = RRi(FAKE_RRI, [1, 2, 3, 4])

        np.testing.assert_array_equal(rri.time, np.array([1, 2, 3, 4]))

    def test_time_has_no_zero_value_besides_in_first_position(self):
        with pytest.raises(ValueError) as e:
            _validate_time(FAKE_RRI, [1, 2, 0, 3])

        assert e.value.args[0] == (
                'time series cannot have 0 values after first position')

    def test_time_is_monotonically_increasing(self):
        with pytest.raises(ValueError) as e:
            _validate_time(FAKE_RRI, [0, 1, 4, 3])

        assert e.value.args[0] == (
                'time series must be monotonically increasing'
        )

    def test_time_series_have_no_negative_values(self):
        with pytest.raises(ValueError) as e:
            _validate_time(FAKE_RRI, [-1, 1, 2, 3])

        assert e.value.args[0] == ('time series cannot have negative values')

    def test_rri_series_have_no_negative_values(self):
        with pytest.raises(ValueError) as e:
            _validate_rri([0.0, 1.0, 2.0, 3.0])

        with pytest.raises(ValueError):
            _validate_rri([1.0, 2.0, -3.0, 4.0])

        assert e.value.args[0] == ('rri series can only have positive values')

    def test_rri_class_encapsulation(self):
        rri = RRi(FAKE_RRI)

        with pytest.raises(AttributeError):
            rri.rri = [1, 2, 3, 4]

        with pytest.raises(AttributeError):
            rri.time = [1, 2, 3, 4]


class TestRRiClassMethods:
    def test_rri_statistical_values(self):
        rri = RRi(FAKE_RRI)

        # Use assert_almost_equal to keep using float32 instead of float64
        np.testing.assert_almost_equal(rri.mean(),  np.mean(FAKE_RRI), 3)
        np.testing.assert_almost_equal(rri.var(),  np.var(FAKE_RRI), 3)
        np.testing.assert_almost_equal(rri.std(),  np.std(FAKE_RRI), 3)
        np.testing.assert_almost_equal(rri.median(),  np.median(FAKE_RRI), 3)
        np.testing.assert_almost_equal(rri.max(),  np.max(FAKE_RRI), 3)
        np.testing.assert_almost_equal(rri.min(),  np.min(FAKE_RRI), 3)
        np.testing.assert_almost_equal(
                rri.amplitude(),
                np.max(FAKE_RRI) - np.min(FAKE_RRI),
                3
        )
        np.testing.assert_almost_equal(
                rri.rms(),
                np.sqrt(np.mean(np.square(FAKE_RRI))),
                3
        )
{'fraction_non_alphanumeric': 0.10716803760282022, 'fraction_numerical': 0.02408930669800235, 'mean_word_length': 2.5987315010570824, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4356612', 'n_tokens_mistral': 3328, 'n_tokens_neox': 3077, 'n_words': 547}