# Path: cotede/qctests/cum_rate_of_change.py
# def cum_rate_of_change(x, memory):
#     """Cummulative rate of change
#     """
#     if isinstance(x, ma.MaskedArray):
#         x[x.mask] = np.nan
#         x = x.data
# 
#     y = np.nan * np.ones_like(x)
#     y[1:] = np.absolute(np.diff(x))
# 
#     for i in range(2, y.size):
#         if y[i] < y[i - 1]:
#             y[i] = (1 - memory) * y[i] + memory * y[i - 1]
# 
#     return y
# 
# class CumRateOfChange(QCCheckVar):
#     def set_features(self):
#         module_logger.debug("Feature: cummulative rate of change")
#         self.features = {
#             "cum_rate_of_change": cum_rate_of_change(
#                 self.data[self.varname], self.cfg["memory"]
#             )
#         }
# 
#     def test(self):
#         self.flags = {}
#         try:
#             threshold = self.cfg["threshold"]
#         except KeyError:
#             print("Deprecated cfg format. It should contain a threshold item.")
#             threshold = self.cfg
# 
#         assert np.size(threshold) == 1, "Threshold should be a single value"
#         assert threshold is not None, "Threshold can't be None"
#         assert np.isfinite(threshold), "Threshold must be a valid number"
# 
#         flag = np.zeros(self.data[self.varname].shape, dtype="i1")
#         feature = np.absolute(self.features["cum_rate_of_change"])
#         flag[np.nonzero(feature > threshold)] = self.flag_bad
#         flag[np.nonzero(feature <= threshold)] = self.flag_good
#         x = self.data[self.varname]
#         flag[ma.getmaskarray(x) | ~np.isfinite(x)] = 9
#         self.flags["cum_rate_of_change"] = flag
# 
# Path: tests/data.py
# class DummyData(object):
#     def __init__(self):
#         self.attrs = {
#                 'datetime': datetime(2016,6,4),
#                 'LATITUDE': 15, 'LONGITUDE': -38}
#         self.data = {
#                 'PRES': ma.fix_invalid(
#                     [2, 6, 10, 21, 44, 79, 100, 150,
#                         200, 400, 410, 650, 1000, 2000, 5000]),
#                 'TEMP': ma.fix_invalid(
#                     [25.32, 25.34, 25.34, 25.31, 24.99, 23.46, 21.85, 17.95,
#                         15.39, 11.08, 6.93, 7.93, 5.71, 3.58, np.nan]),
#                 'PSAL': ma.fix_invalid(
#                     [36.49, 36.51, 36.52, 36.53, 36.59, 36.76, 36.81, 36.39,
#                         35.98, 35.30, 35.28, 34.93, 34.86, np.nan, np.nan])}
# 
#     def __getitem__(self, key):
#         return self.data[key]
# 
#     def keys(self):
#         return self.data.keys()
# 
#     @property
#     def attributes(self):
#         print('attributes will be removed. Use attrs instead!')
#         return self.attrs


import numpy as np
from cotede.qctests import CumRateOfChange, cum_rate_of_change
from ..data import DummyData
#!/usr/bin/env python
# -*- coding: utf-8 -*-

""" Verify the Cummulative Rate of Change QC test
"""



def test_cum_rate_of_change():
    x = [1, -1, 2, 2, 3, 2, 4]
    memory = 0.8
    y = cum_rate_of_change(x, memory)

    output = [np.nan, 2.0, 3.0, 2.4, 2.12, 1.896, 2.0]

    assert isinstance(y, np.ndarray)
    assert np.allclose(y, output, equal_nan=True)


def test_standard_dataset():
    """Test CumRateOfChange with a standard dataset
    """

# Path: cotede/qctests/cum_rate_of_change.py
# def cum_rate_of_change(x, memory):
#     """Cummulative rate of change
#     """
#     if isinstance(x, ma.MaskedArray):
#         x[x.mask] = np.nan
#         x = x.data
# 
#     y = np.nan * np.ones_like(x)
#     y[1:] = np.absolute(np.diff(x))
# 
#     for i in range(2, y.size):
#         if y[i] < y[i - 1]:
#             y[i] = (1 - memory) * y[i] + memory * y[i - 1]
# 
#     return y
# 
# class CumRateOfChange(QCCheckVar):
#     def set_features(self):
#         module_logger.debug("Feature: cummulative rate of change")
#         self.features = {
#             "cum_rate_of_change": cum_rate_of_change(
#                 self.data[self.varname], self.cfg["memory"]
#             )
#         }
# 
#     def test(self):
#         self.flags = {}
#         try:
#             threshold = self.cfg["threshold"]
#         except KeyError:
#             print("Deprecated cfg format. It should contain a threshold item.")
#             threshold = self.cfg
# 
#         assert np.size(threshold) == 1, "Threshold should be a single value"
#         assert threshold is not None, "Threshold can't be None"
#         assert np.isfinite(threshold), "Threshold must be a valid number"
# 
#         flag = np.zeros(self.data[self.varname].shape, dtype="i1")
#         feature = np.absolute(self.features["cum_rate_of_change"])
#         flag[np.nonzero(feature > threshold)] = self.flag_bad
#         flag[np.nonzero(feature <= threshold)] = self.flag_good
#         x = self.data[self.varname]
#         flag[ma.getmaskarray(x) | ~np.isfinite(x)] = 9
#         self.flags["cum_rate_of_change"] = flag
# 
# Path: tests/data.py
# class DummyData(object):
#     def __init__(self):
#         self.attrs = {
#                 'datetime': datetime(2016,6,4),
#                 'LATITUDE': 15, 'LONGITUDE': -38}
#         self.data = {
#                 'PRES': ma.fix_invalid(
#                     [2, 6, 10, 21, 44, 79, 100, 150,
#                         200, 400, 410, 650, 1000, 2000, 5000]),
#                 'TEMP': ma.fix_invalid(
#                     [25.32, 25.34, 25.34, 25.31, 24.99, 23.46, 21.85, 17.95,
#                         15.39, 11.08, 6.93, 7.93, 5.71, 3.58, np.nan]),
#                 'PSAL': ma.fix_invalid(
#                     [36.49, 36.51, 36.52, 36.53, 36.59, 36.76, 36.81, 36.39,
#                         35.98, 35.30, 35.28, 34.93, 34.86, np.nan, np.nan])}
# 
#     def __getitem__(self, key):
#         return self.data[key]
# 
#     def keys(self):
#         return self.data.keys()
# 
#     @property
#     def attributes(self):
#         print('attributes will be removed. Use attrs instead!')
#         return self.attrs


# Path: tests/qctests/test_qc_cum_rate_of_change.py
import numpy as np
from cotede.qctests import CumRateOfChange, cum_rate_of_change
from ..data import DummyData

#!/usr/bin/env python
# -*- coding: utf-8 -*-

""" Verify the Cummulative Rate of Change QC test
"""



def test_cum_rate_of_change():
    x = [1, -1, 2, 2, 3, 2, 4]
    memory = 0.8
    y = cum_rate_of_change(x, memory)

    output = [np.nan, 2.0, 3.0, 2.4, 2.12, 1.896, 2.0]

    assert isinstance(y, np.ndarray)
    assert np.allclose(y, output, equal_nan=True)


def test_standard_dataset():
    """Test CumRateOfChange with a standard dataset
    """

    profile = DummyData()