Skip to content

Commit

Permalink
Solved smooth problem and adding test for mlregressor
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesll committed Aug 6, 2018
1 parent d551bd1 commit 5ae411f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
13 changes: 8 additions & 5 deletions rampy/spectranization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -163,21 +165,22 @@ 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
==========
x: Numpy array
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
Expand All @@ -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()

Expand Down
28 changes: 28 additions & 0 deletions rampy/tests/test_mlregressor.py
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 2 additions & 2 deletions rampy/tests/test_spectranization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5ae411f

Please sign in to comment.