Skip to content
Snippets Groups Projects
Commit 236a94c7 authored by David Verelst's avatar David Verelst
Browse files

Merge branch 'master' of gitlab.windenergy.dtu.dk:toolbox/WindEnergyToolbox

parents 4cee4bcc 6685fc32
No related branches found
No related tags found
No related merge requests found
'''
Created on 02/11/2015
@author: MMPE
'''
import unittest
import numpy as np
from wetb.signal.nan_replace import replace_by_mean, replace_by_line, \
replace_by_polynomial, max_no_nan
from matplotlib.pyplot import plot, show
class TestNan_replace(unittest.TestCase):
def test_nan_replace_by_mean(self):
a = np.array([1, 5, 6, 4, 5, np.nan, 3, 1, 5, 6, 4.])
np.testing.assert_array_equal(replace_by_mean(a), [1, 5, 6, 4, 5, 4, 3, 1, 5, 6, 4.])
a = np.array([1, 5, 6, 4, 5, np.nan, np.nan, 1, 5, 6, 4.])
np.testing.assert_array_equal(replace_by_mean(a), [1, 5, 6, 4, 5, 3, 3, 1, 5, 6, 4.])
a = np.array([np.nan, 5, 6, 4, 5, np.nan, 3, 1, 5, 6, np.nan])
np.testing.assert_array_equal(replace_by_mean(a), [5, 5, 6, 4, 5, 4, 3, 1, 5, 6, 6])
def test_nan_replace_by_line(self):
a = np.array([1, 5, 6, 4, 5, np.nan, 3, 1, 5, 6, 4.])
np.testing.assert_array_equal(replace_by_line(a), [1, 5, 6, 4, 5, 4, 3, 1, 5, 6, 4.])
a = np.array([1, 5, 6, 4, 5, np.nan, np.nan, 2, 5, 6, 4.])
np.testing.assert_array_equal(replace_by_line(a), [1, 5, 6, 4, 5, 4, 3, 2, 5, 6, 4.])
a = np.array([np.nan, 5, 6, 4, 5, np.nan, 3, 1, 5, 6, np.nan])
np.testing.assert_array_equal(replace_by_line(a), [5, 5, 6, 4, 5, 4, 3, 1, 5, 6, 6])
def test_nan_replace_by_polynomial(self):
a = np.array([np.nan, 5, 6, 4, 5, np.nan, 3, 1, 5, 6, np.nan])
np.testing.assert_array_equal(replace_by_polynomial(a), [5, 5, 6, 4, 5, 4, 3, 1, 5, 6, 6])
a = np.array([1, 5, 6, 4, 5, np.nan, np.nan, 1, 5, 6, 4.])
np.testing.assert_array_almost_equal(replace_by_polynomial(a, 3, 2), [1, 5, 6, 4, 5, 3.3, 1.2, 1, 5, 6, 4.])
if 0:
plot(a)
plot(replace_by_polynomial(a, 3, 2))
show()
def test_nan_replace_by_polynomial2(self):
a = np.r_[np.arange(10), np.repeat(np.nan, 100), 10 - np.arange(10)]
self.assertLessEqual(replace_by_polynomial(a, 3, 9).max(), np.nanmax(a))
if 0:
plot(a, '.r')
plot(replace_by_polynomial(a, 3, 9), 'g-')
show()
def test_max_no_nan(self):
a = np.array([1, 5, 6, 4, 5, np.nan, 3, 1, 5, np.nan, 4.])
self.assertEqual(max_no_nan(a), 1)
a = np.array([1, 5, 6, 4, 5, np.nan, np.nan, 1, 5, np.nan, 4.])
self.assertEqual(max_no_nan(a), 2)
a = np.array([np.nan, np.nan, np.nan, 4, 5, np.nan, np.nan, 1, 5, np.nan, 4.])
self.assertEqual(max_no_nan(a), 3)
a = np.array([1, 5, 6, 4, 5, np.nan, 4, 1, 5, np.nan, np.nan])
self.assertEqual(max_no_nan(a), 2)
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.test_nanreplace']
unittest.main()
......@@ -9,7 +9,7 @@ import unittest
import numpy as np
from wetb import gtsdf
from wetb.signal.subset_mean import time_trigger, subset_mean, \
non_nan_index_trigger, revolution_trigger
non_nan_index_trigger, revolution_trigger_old, revolution_trigger
from wetb.utils.geometry import rpm2rads
......@@ -68,10 +68,10 @@ class TestSubsetMean(unittest.TestCase):
ds = gtsdf.Dataset(tfp+'azi.hdf5')
azi, rpm, time = [ds(x)[8403:8803] for x in ['azi','Rot_cor','Time']]
trigger = revolution_trigger(azi)
trigger = revolution_trigger_old(azi)
np.testing.assert_array_equal(trigger, [ 17, 128, 241, 354])
azi[64] = 358
trigger = revolution_trigger(azi, (ds('Rot_cor'), np.diff(time).mean()))
trigger = revolution_trigger_old(azi, (ds('Rot_cor'), np.diff(time).mean()))
# import matplotlib.pyplot as plt
# t = np.arange(len(azi))
......@@ -82,5 +82,31 @@ class TestSubsetMean(unittest.TestCase):
np.testing.assert_array_equal(trigger, [ (128,241),(241,354)])
def test_revolution_trigger(self):
rotor_position = np.arange(0.,360*10,4)
rotor_position += np.random.random(len(rotor_position))
rotor_position = rotor_position % 360
if 0:
x1 = np.random.randint(0, len(rotor_position),10)
print (list(x1))
x2 = np.random.randint(0, len(rotor_position),10)
print (list(x2))
else:
x1 = [447, 854, 595, 804, 847, 488, 412, 199, 675, 766]
x2 = [92, 647, 821, 422, 33, 159, 369, 99, 157, 464]
rotor_position[x1] += 360
rotor_position[x2] -= 360
rotor_position[90] = 180
indexes = revolution_trigger(rotor_position, 20,1/(90/20/60))
np.testing.assert_array_equal(indexes, [ 91, 180, 270, 360, 450, 540, 630, 720, 810])
if 0:
import matplotlib.pyplot as plt
plt.plot(rotor_position)
plt.plot(indexes, np.zeros_like(indexes),'.')
plt.show()
if __name__ == "__main__":
unittest.main()
......@@ -99,7 +99,7 @@ def fit_power_shear_ref(z_u_lst, z_ref, plt=None):
"""
def shear_error(x, z_u_lst, z_ref):
alpha, u_ref = x
return np.sum([(u - u_ref * (z / z_ref) ** alpha) ** 2 for z, u in z_u_lst])
return np.nansum([(u - u_ref * (z / z_ref) ** alpha) ** 2 for z, u in z_u_lst])
z_u_lst = [(z, np.mean(u)) for z, u in z_u_lst]
alpha, u_ref = fmin(shear_error, (.1, 10), (z_u_lst, z_ref), disp=False)
if plt:
......@@ -107,6 +107,9 @@ def fit_power_shear_ref(z_u_lst, z_ref, plt=None):
plt.plot(u, z, '.')
z = np.linspace(min(z), max(z), 100)
plt.plot(power_shear(alpha, z_ref, u_ref)(z), z)
plt.margins(.1)
if alpha==.1 and u_ref==10: # Initial conditions
return np.nan, np.nan
return alpha, u_ref
......
......@@ -49,6 +49,24 @@ class TestShear(unittest.TestCase):
14: WSP gl. coo.,Vz 21
"""
def test_power_shear_fit(self):
z = [30,50,70]
a,u_ref = .3,10
u = power_shear(a,50,u_ref)(z)
np.testing.assert_array_almost_equal(fit_power_shear_ref(zip(z,u), 50), [a,u_ref],3)
if 0:
import matplotlib.pyplot as plt
fit_power_shear_ref(zip(z,u), 50, plt)
plt.show()
def test_power_shear_fit_nan(self):
z = [30,50,70]
a,u_ref = .3,10
u = power_shear(a,50,u_ref)(z)
u[2] = np.nan
np.testing.assert_array_almost_equal(fit_power_shear_ref(zip(z,u), 50), [a,u_ref],3)
u[:] = np.nan
np.testing.assert_array_almost_equal(fit_power_shear_ref(zip(z,u), 50), [np.nan, np.nan],3)
def test_power_shear(self):
if all:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment