diff --git a/evenflow/3_removePeriodicMean.py b/evenflow/3_removePeriodicMean.py new file mode 100644 index 0000000..0710598 --- /dev/null +++ b/evenflow/3_removePeriodicMean.py @@ -0,0 +1,27 @@ +import numpy as np +from function4 import getSkipMean +def removePeriodicMean(signalMat, period, sliderWidth, NoDataCode): + """ + :param signalMat: n - column matrix where variables represent timeseries + :param period: length of repeating pattern of data (i.e. seasons, years, etc). + :param sliderWidth: Number of periods to base the anomaly on. + :param NoDatCode: data value to be ignored + :return: matrix of values that accounts for periodic averages + """ + nData, nSignals = np.shape(signalMat) + newLength = nData - period * (sliderWidth - 1) + anomalyMat = np.nan * np.zeros(shape = (newLength, nSignals)) + avg = np.nan * np.zeros(shape=(nData, nSignals)) + + for i in range(newLength): + for j in range(nSignals): + end = i + (sliderWidth*period) - 1 + avg[i,j] = getSkipMean(signalMat[i:end:period, j], NoDataCode) + + if avg[i,j] == NoDataCode or signalMat[i,j] == NoDataCode: + anomalyMat[i,j] = NoDataCode + else: + anomalyMat[i,j] = signalMat[i,j] - avg[i,j] + + return anomalyMat + diff --git a/evenflow/4_getSkipMean.py b/evenflow/4_getSkipMean.py new file mode 100644 index 0000000..a41b527 --- /dev/null +++ b/evenflow/4_getSkipMean.py @@ -0,0 +1,19 @@ +import numpy as np +def getSkipMean(vector, NoDataCode): + """ + :param vector: vector of values. + :param NoDataCode: indicates value to be ignored + :return: average of the values in the vector ignoring NoData values + """ + N, _ = np.shape(vector) + sum = 0 + nVals = 0 + for i in range(N): + if vector[i] != NoDataCode: + sum+=vector[i] + nVals += 1 + if nVals > 0: + avg = sum/nVals + else: + avg = NoDataCode + return avg \ No newline at end of file diff --git a/evenflow/tests/test_2_trimNoData.py b/evenflow/tests/test_2_trimNoData.py new file mode 100644 index 0000000..824b9fd --- /dev/null +++ b/evenflow/tests/test_2_trimNoData.py @@ -0,0 +1,17 @@ +import numpy as np, nose +from function2 import trimNoData + +def function2GeneralTest(): + a = np.zeros(shape=(3,4)) + a[0,0] = 1 + a[1,0] = 1 + a[2,1] = 1 + b = trimNoData(a, 1) + x = np.zeros(shape=(3,4)) + x[:] = 1 + print(x == b) + y = np.zeros(shape=(3,4)) + c = trimNoData(a, 0) + print(c==y) + + diff --git a/evenflow/tests/test_3_removePeriodicMean.py b/evenflow/tests/test_3_removePeriodicMean.py new file mode 100644 index 0000000..e69de29 diff --git a/evenflow/tests/test_4_getsSkipMean.py b/evenflow/tests/test_4_getsSkipMean.py new file mode 100644 index 0000000..e69de29