Skip to content

Commit

Permalink
Remove unused imports and Cosmetics
Browse files Browse the repository at this point in the history
  • Loading branch information
carhc committed Oct 15, 2024
1 parent 0261c26 commit 1c3616e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 48 deletions.
4 changes: 2 additions & 2 deletions invisible_cities/core/fit_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ def fit(func, x, y, seed=(), fit_range=None, **kwargs):
Returns
-------
fitted_fun : FitFunction
Extended function containing fit parameters (fitf, vals, errors, chi2, pval,
cov) and full_output from curve_fit and leastsq (infodict, mesg and ier).
Extended function containing fit parameters (fitf, vals, errors, chi2, pval,
cov) and full_output from curve_fit and leastsq (infodict, mesg and ier).
Examples
Expand Down
1 change: 0 additions & 1 deletion invisible_cities/core/fit_functions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from hypothesis.strategies import floats
from . testing_utils import float_arrays
from . testing_utils import random_length_float_arrays
from .. evm.ic_containers import FitFunction

from . import core_functions as core
from . import fit_functions as fitf
Expand Down
39 changes: 7 additions & 32 deletions invisible_cities/reco/icaro_components.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import numpy as np
import pandas as pd

from typing import Optional
from ..types.symbols import KrFitFunction
from .. evm.ic_containers import FitFunction
from .. core.fit_functions import polynom, expo


def lin_seed(x : np.array,
y : np.array):

def lin_seed(x : np.array, y : np.array):
'''
Estimate the seed for a linear fit.
Expand All @@ -25,26 +22,20 @@ def lin_seed(x : np.array,
seed : tuple
Seed parameters (intercept, slope) for the linear fit.
'''

x0, x1 = x.min(), x.max()
y0, y1 = y.min(), y.max()

if x1 == x0: # If same x value, set slope to 0 and use the mean value of y as interceipt
b = 0
a = y.mean()

else:
b = (y1 - y0) / (x1 - x0)
a = y0 - b * x0

seed = a, b

return seed

return a, b

def expo_seed(x : np.array,
y : np.array):

def expo_seed(x : np.array, y : np.array):
'''
Estimate the seed for an exponential fit.
Expand All @@ -60,9 +51,7 @@ def expo_seed(x : np.array,
seed : tuple
Seed parameters (constant, mean) for the exponential fit.
'''

x, y = zip(*sorted(zip(x, y)))

const = y[0]

if const <= 0 or y[-1] <= 0:
Expand All @@ -74,18 +63,10 @@ def expo_seed(x : np.array,
return seed


def select_fit_variables(fittype : KrFitFunction,
dst : pd.DataFrame):

def select_fit_variables(fittype : KrFitFunction, dst : pd.DataFrame):
'''
Select the data for fitting based on the specified fit type.
NOTES: Since x axis (DT) is never altered, maybe we can just
return the y values. However, when we implement the binned fit,
the profile could be done here (?) so it would make sense to
always provide both x and y. We could rename parameters and have
fittype (binned / unbinned) and fitfunction (lin, expo, log-lin...)
Parameters
----------
fittype : KrFitFunction
Expand All @@ -100,7 +81,6 @@ def select_fit_variables(fittype : KrFitFunction,
y_data : pd.Series
The dependent variable data prepared for fitting.
'''

if fittype is KrFitFunction.linear : return dst.DT, dst.S2e
elif fittype is KrFitFunction.expo : return dst.DT, dst.S2e
elif fittype is KrFitFunction.log_lin: return dst.DT, -np.log(dst.S2e)
Expand All @@ -123,17 +103,15 @@ def get_function_and_seed_lt(fittype : KrFitFunction):
seed_function : function
The seed function corresponding to the specified fit type.
'''

linear_function = lambda x, y0, slope: polynom(x, y0, slope)
expo_function = lambda x, e0, lt: expo (x, e0, -lt)

if fittype is KrFitFunction.linear: return linear_function, lin_seed
elif fittype is KrFitFunction.log_lin: return linear_function, lin_seed
elif fittype is KrFitFunction.expo: return expo_function, expo_seed
if fittype is KrFitFunction.linear: return linear_function, lin_seed
elif fittype is KrFitFunction.log_lin: return linear_function, lin_seed
elif fittype is KrFitFunction.expo: return expo_function, expo_seed


def transform_parameters(fit_output : FitFunction):

'''
Transform the parameters obtained from the fitting output into EO and LT.
When using log_lin fit, we need to convert the intermediate variables into
Expand All @@ -154,7 +132,6 @@ def transform_parameters(fit_output : FitFunction):
cov : float
Transformed covariance value.
'''

par = fit_output.values
err = fit_output.errors
cov = fit_output.cov[0, 1]
Expand All @@ -164,10 +141,8 @@ def transform_parameters(fit_output : FitFunction):

E0 = np.exp(-a)
s_E0 = np.abs(E0 * u_a)

lt = 1 / b
s_lt = np.abs(lt**2 * u_b)

cov = E0 * lt**2 * cov # Not sure about this

par = [ E0, lt]
Expand Down
20 changes: 7 additions & 13 deletions invisible_cities/reco/icaro_components_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import numpy as np
import numpy.testing as npt
import pandas as pd
import pandas.testing as pdt
import scipy.optimize as so

from hypothesis import given, settings
Expand All @@ -23,6 +22,7 @@ def test_lin_function_output_values(x_min, x_max, a, b):
x = np.array([x_min, x_max])
y = a + b * x
a_test, b_test = icarcomp.lin_seed(x, y)

assert np.isclose(a_test, a)
assert np.isclose(b_test, b)

Expand All @@ -35,20 +35,19 @@ def test_expo_seed_output_values(zmin, zmax, elt, e0):
x = np.array( [ zmin, zmax ] )
y = e0 * np.exp( - x / elt )
e0_test, elt_test = icarcomp.expo_seed(x, y)

assert np.isclose(e0_test, e0, rtol=0.1)
assert np.isclose(elt_test, elt, rtol=0.1)


@pytest.fixture
def sample_df():

data = {
'DT' : [10, 20, 30, 40, 50],
'S2e': [50, 45, 42, 41, 41]
}
data = {'DT' : [10, 20, 30, 40, 50],
'S2e': [50, 45, 42, 41, 41]}
return pd.DataFrame(data)
def test_select_fit_variables(sample_df):


def test_select_fit_variables(sample_df):
x_linear, y_linear = icarcomp.select_fit_variables(KrFitFunction.linear, sample_df)
x_expo, y_expo = icarcomp.select_fit_variables(KrFitFunction.expo, sample_df)
x_log_lin, y_log_lin = icarcomp.select_fit_variables(KrFitFunction.log_lin, sample_df)
Expand All @@ -69,7 +68,6 @@ def test_select_fit_variables(sample_df):
floats (min_value = 1e3, max_value = 1e5),
floats (min_value = 5e3, max_value = 1e5))
def test_get_function_and_seed_lt_with_data(x_min, x_max, steps, e0, lt):

x = np.linspace(x_min, x_max, steps)
y = expo(x, e0, lt)

Expand All @@ -81,21 +79,17 @@ def test_get_function_and_seed_lt_with_data(x_min, x_max, steps, e0, lt):
popt_expo, _ = so.curve_fit(fit_func_expo, x, y, p0=seed_func_expo (x, y))
popt_log_lin, _ = so.curve_fit(fit_func_log_lin, x, y, p0=seed_func_log_lin(x, y))


assert np.isclose(popt_lin[0], popt_expo[0], rtol=1e-1) # The interceipt should be close between lin and expo
assert not np.isclose(popt_lin[1], popt_expo[1], rtol=1e-1) # The "lifetime" should be different between lin and expo

assert np.isclose(popt_lin[0], popt_log_lin[0], rtol=1e-10) # The lin and log_lin are the same (the only difference is their
assert np.isclose(popt_lin[1], popt_log_lin[1], rtol=1e-10) # inputs: s2e or -log(s2e)) so both parameters should be the same
# for the purpose of testing this function


@given(floats(min_value=1, max_value=1e5),
floats(min_value=10, max_value=1e5))
def test_transform_parameters(a, b):

errors = 0.2*np.array([a, b])

errors = 0.2*np.array([a, b])
fit_output = FitFunction(values=[a, b], errors=errors, cov=np.array([[0.04, 0.02], [0.02, 0.04]]),
fn=None, chi2=None, pvalue=None, infodict=None, mesg=None, ier=None)

Expand Down

0 comments on commit 1c3616e

Please sign in to comment.