From 5ae411face0ae90602318c4bb6d72de1d7488255 Mon Sep 17 00:00:00 2001 From: Charles Le Losq Date: Mon, 6 Aug 2018 14:26:21 +1000 Subject: [PATCH] Solved smooth problem and adding test for mlregressor --- rampy/spectranization.py | 13 ++++++++----- rampy/tests/test_mlregressor.py | 28 ++++++++++++++++++++++++++++ rampy/tests/test_spectranization.py | 4 ++-- 3 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 rampy/tests/test_mlregressor.py diff --git a/rampy/spectranization.py b/rampy/spectranization.py index d3f2355..75a1744 100644 --- a/rampy/spectranization.py +++ b/rampy/spectranization.py @@ -6,6 +6,8 @@ from scipy.interpolate import UnivariateSpline from scipy.interpolate import interp1d +import rampy + # SPECIFIC FUNCTIONS FOR TREATMENT OF SPECTRA def spectrarray(name,sh,sf,x): @@ -163,10 +165,11 @@ def normalise(y,x=0,method="intensity"): if method == "minmax": y = (y-np.min(y))/(np.max(y)-np.min(y)) -def centroid(x,y,smooth = False,**kwargs): +def centroid(x,y,smoothing=False,**kwargs): """calculation of the y signal centroid as np.sum(y/np.sum(y)*x) + if smoothing == 1: Parameters ========== @@ -174,10 +177,10 @@ def centroid(x,y,smooth = False,**kwargs): x values y: Numpy array y values, 1 spectrum - + Options ======= - smooth : bool + smoothing : bool True or False. Smooth the signals with arguments provided as kwargs. Default method is whittaker smoothing. See the rampy.smooth function for smoothing options and arguments. Returns @@ -190,8 +193,8 @@ def centroid(x,y,smooth = False,**kwargs): x = x.reshape(-1) y = y.reshape(-1) - if smooth == True: - y_ = rp.smooth(x,y,**kwargs) + if smoothing == True: + y_ = rampy.smooth(x,y,**kwargs) else: y_ = y.copy() diff --git a/rampy/tests/test_mlregressor.py b/rampy/tests/test_mlregressor.py new file mode 100644 index 0000000..1f14c6b --- /dev/null +++ b/rampy/tests/test_mlregressor.py @@ -0,0 +1,28 @@ +import unittest + +import numpy as np +import scipy + +import rampy as rp + +class TestSmooth(unittest.TestCase): + + def test_mixing(self): + + # dummy gaussians + x = np.arange(0,100,1.0) # a dummy x axis + ref1 = 50.0*np.exp(-1/2*((x-40)/20)**2) + np.random.randn(len(x)) # a gaussian with added noise + ref2 = 70.0*np.exp(-1/2*((x-60)/15)**2) + np.random.randn(len(x)) # a gaussian with added noise + + # mixed signals + F1_true = np.array([0.80,0.60,0.40,0.20]) + obs = np.dot(ref1.reshape(-1,1),F1_true.reshape(1,-1)) + np.dot(ref2.reshape(-1,1),(1-F1_true.reshape(1,-1))) + + # calculation + F1_meas = rp.mixing_sp(obs,ref1,ref2) + + # assertion + np.testing.assert_almost_equal(F1_true,F1_meas,decimal=3) + +if __name__ == '__main__': + unittest.main() diff --git a/rampy/tests/test_spectranization.py b/rampy/tests/test_spectranization.py index 011587f..ae97307 100644 --- a/rampy/tests/test_spectranization.py +++ b/rampy/tests/test_spectranization.py @@ -40,8 +40,8 @@ def test_centroid(self): x = np.arange(0,100,1.).reshape(-1,1) y = norm.pdf(x,loc=60,scale=10) - c1 = rampy.centroid(x,y,smooth=True,method='whittaker') - c2 = rampy.centroid(x,y,smooth=False) + c1 = rampy.centroid(x,y,smoothing=True,method="whittaker") + c2 = rampy.centroid(x,y,smoothing=False) # Testing np.testing.assert_almost_equal(c1,60.,decimal=1)