Skip to content

Commit

Permalink
Add selection_in_band function
Browse files Browse the repository at this point in the history
  • Loading branch information
bpalmeiro committed Dec 26, 2024
1 parent f448ee6 commit b0f7f55
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion invisible_cities/reco/icaro_components.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import numpy as np
import pandas as pd

from typing import Tuple, Optional
from typing import Tuple, Optional
from sklearn.linear_model import RANSACRegressor

from . corrections import ASectorMap
from . corrections import apply_geo_correction

from .. types.symbols import type_of_signal
from .. types.symbols import Strictness
from .. types.symbols import NormStrategy
from .. types.symbols import KrFitFunction
from .. core.core_functions import check_if_values_in_interval
from .. core.core_functions import in_range
from .. core.fit_functions import profileX
from .. core.fit_functions import fit
from . krmap_functions import get_function_and_seed_lt


def selection_nS_mask_and_checking(dst : pd.DataFrame ,
Expand Down Expand Up @@ -134,4 +138,48 @@ def band_selector_and_check(dst : pd.DataFrame,
return sel_krband


def selection_in_band(z : np.array,
e : np.array,
range_z : Tuple[float, float],
range_e : Tuple[float, float],
nsigma : float = 3.5) ->np.array:
"""
This function returns a mask for the selection of the events that are inside the Kr E vz Z
Parameters
----------
z: np.array
axial (z) values
e: np.array
energy values
range_z: Tuple[np.array, np.array]
Range in Z-axis
range_e: Tuple[np.array, np.array]
Range in Energy-axis
nsigma: float
Number of sigmas to set the band width
Returns
----------
A mask corresponding to the selection made.
"""
# Reshapes and flattens are needed for RANSAC function

z_sel = z[in_range(z, *range_z)]
e_sel = e[in_range(e, *range_e)]

res_fit = RANSACRegressor().fit(z_sel, np.log(e_sel).reshape(-1, 1))

in_mask = res_fit.inlier_mask_
residuals_ln = e_sel[in_mask] - np.exp(res_fit.predict(z_sel[in_mask].reshape(-1, 1))).flatten()
resy, resx = np.histogram(residuals_ln, 100)
resx = resx[:-1] + np.diff(resx)
fitres = fit(gauss, resx, resy, seed=[4e3,0,10])
fitsigma = fitres.val[2]

prefict_fun = lambda z: res_fit.predict(z.reshape(-1, 1)).flatten()
upper_band = lambda z: prefict_fun(z) + nsigma * fitsigma
lower_band = lambda z: prefict_fun(z) - nsigma * fitsigma

sel_inband = in_range(np.log(e_sel), lower_band(z_sel), upper_band(z_sel))

return sel_inband

0 comments on commit b0f7f55

Please sign in to comment.