예제 #1
0
    def __init__(self,
                 ecal_train=[],
                 hcal_train=[],
                 true_train=[],
                 lim_min=-1,
                 lim_max=-1,
                 lim=-1):
        Calibration.__init__(self, ecal_train, hcal_train, true_train, lim)

        self.lim_min = lim_min
        self.lim_max = lim_max

        if lim_min == -1:
            ind_min = np.ones(len(self.ecal_train), dtype=bool)
        else:
            ind_min = self.ecal_train + self.hcal_train > lim_min
        if lim_max == -1:
            ind_max = np.ones(len(self.ecal_train), dtype=bool)
        else:
            ind_max = self.ecal_train + self.hcal_train < lim_max

        #CASE : ecal != 0
        ind_0 = self.ecal_train != 0
        ind = np.logical_and(ind_min, ind_max)
        ind = np.logical_and(ind, ind_0)
        X_train = [self.ecal_train[ind], self.hcal_train[ind]]
        X_train = np.transpose(np.matrix(X_train))
        Y_train = self.true_train[ind]
        Y_train = np.transpose(np.matrix(Y_train))
        linRegr_ecal_neq_0 = linear_model.LinearRegression()
        linRegr_ecal_neq_0.fit(X_train, Y_train)

        #CASE : ecal == 0
        ind_0 = self.ecal_train == 0
        ind = np.logical_and(ind_min, ind_max)
        ind = np.logical_and(ind, ind_0)
        X_train = self.hcal_train[ind]
        X_train = np.transpose(np.matrix(X_train))
        Y_train = self.true_train[ind]
        Y_train = np.transpose(np.matrix(Y_train))
        linRegr_ecal_eq_0 = linear_model.LinearRegression()
        linRegr_ecal_eq_0.fit(X_train, Y_train)

        self.linRegr_ecal_neq_0 = linRegr_ecal_neq_0
        self.linRegr_ecal_eq_0 = linRegr_ecal_eq_0
예제 #2
0
    def __init__(self,
                 ecal_train=[],
                 hcal_train=[],
                 true_train=[],
                 n_neighbors_ecal_eq_0=2000,
                 n_neighbors_ecal_neq_0=250,
                 algorithm='auto',
                 lim=-1):
        """
        Parameters
        ----------
        ecal_train : array-like
        ecal value to train the calibration

        hcal_train : array-like
        hcal value to train the calibration

        true_train : array-like
        true value to train the calibration

        n_neighbors_ecal_eq_0: int
        Number of neighbors to use by default for k_neighbors queries.
        for ecal == 0
        
        n_neighbors_ecal_neq_0: int
        Number of neighbors to use by default for k_neighbors queries.
        for ecal != 0

        algortihm : {‘auto’, ‘ball_tree’, ‘kd_tree’, ‘brute’}, optional
        Algorithm used to compute the nearest neighbors:
        'ball_tree' will use BallTree
        'kd_tree' will use KDtree
        'brute' will use a brute-force search.
        'auto' will attempt to decide the most appropriate algorithm based on
        the values passed to fit method.

        lim : float
        to reject calibration points with ecal + hcal > lim
        if lim = - 1, there is no limit
        """

        Calibration.__init__(self, ecal_train, hcal_train, true_train, lim)

        self.n_neighbors_ecal_eq_0 = n_neighbors_ecal_eq_0
        self.n_neighbors_ecal_neq_0 = n_neighbors_ecal_neq_0
        self.algorithm = algorithm

        #Case ecal == 0
        self.neigh_ecal_eq_0 = neighbors.NearestNeighbors(
            n_neighbors=self.n_neighbors_ecal_eq_0, algorithm=algorithm)
        self.hcal_train_ecal_eq_0 = self.hcal_train[self.ecal_train == 0]
        self.hcal_train_ecal_eq_0_min = min(self.hcal_train_ecal_eq_0)
        self.true_train_ecal_eq_0 = self.true_train[self.ecal_train == 0]
        self.neigh_ecal_eq_0.fit(
            np.transpose(np.matrix(self.hcal_train_ecal_eq_0)))

        # Case ecal != 0
        self.neigh_ecal_neq_0 = neighbors.NearestNeighbors(
            n_neighbors=self.n_neighbors_ecal_neq_0, algorithm=algorithm)
        self.ecal_train_ecal_neq_0 = self.ecal_train[self.ecal_train != 0]
        self.hcal_train_ecal_neq_0 = self.hcal_train[self.ecal_train != 0]
        self.true_train_ecal_neq_0 = self.true_train[self.ecal_train != 0]
        self.hcal_train_ecal_neq_0_min = min(self.hcal_train_ecal_neq_0)
        self.ecal_train_ecal_neq_0_min = min(self.ecal_train_ecal_neq_0)
        self.neigh_ecal_neq_0.fit(
            np.transpose(
                np.matrix(
                    [self.ecal_train_ecal_neq_0, self.hcal_train_ecal_neq_0])))
예제 #3
0
    def __init__(self,
                 ecal_train=[],
                 hcal_train=[],
                 true_train=[],
                 n_neighbors_ecal_eq_0=2000,
                 n_neighbors_ecal_neq_0=250,
                 weights='gaussian',
                 algorithm='auto',
                 sigma=1,
                 lim=-1,
                 energystep=1,
                 kind='cubic',
                 cut=2):
        """
        Parameters
        ----------
        ecal_train : array-like
        ecal value to train the calibration

        hcal_train : array-like
        hcal value to train the calibration

        true_train : array-like
        true value to train the calibration

        n_neighbors_ecal_eq_0: int
        Number of neighbors to use by default for k_neighbors queries.
        for ecal == 0
        
        n_neighbors_ecal_neq_0: int
        Number of neighbors to use by default for k_neighbors queries.
        for ecal != 0

        weight : str or callable
        weight function used in prediction. Possible values:
        'uniform' : uniform weights. All points in each neighborhood are
        weighted equally.
        'distance' : weight points by the inverse of their distance. in this
        case, closer neighbors of a query point will have a greater influence
        than neighbors which are further away.
        [callable] : a user-defined function which accepts an array of
        distances, and returns an array of the same shape containing the weights.
        'gaussian'
        Gaussian weights are used by default.

        algortihm : {‘auto’, ‘ball_tree’, ‘kd_tree’, ‘brute’}, optional
        Algorithm used to compute the nearest neighbors:
        'ball_tree' will use BallTree
        'kd_tree' will use KDtree
        'brute' will use a brute-force search.
        'auto' will attempt to decide the most appropriate algorithm based on
        the values passed to fit method.

        sigma : float
        sigma for the gaussian if weight == 'gaussian'

        lim : float
        to reject calibration points with ecal + hcal > lim
        if lim = - 1, there is no limit

        energystep : float
        step between two points of evaluation

        kind : str or int, optional
        Specifies the kind of interpolation as a string (‘linear’, ‘nearest’,
        ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’ where ‘zero’, ‘slinear’,
        ‘quadratic’ and ‘cubic’ refer to a spline interpolation of zeroth,
        first, second or third order) or as an integer specifying the order of
        the spline interpolator to use. Default is ‘linear’

        cut : float
        cut to reject points
        we only consider the points with true energy between
        mu - cut * sigma and mu - cut * sigma (mu and sigma the mean and std
        of the gaussian fit)
        """
        Calibration.__init__(self, ecal_train, hcal_train, true_train, lim)

        self.n_neighbors_ecal_eq_0 = n_neighbors_ecal_eq_0
        self.n_neighbors_ecal_neq_0 = n_neighbors_ecal_neq_0
        self.algorithm = algorithm
        self.sigma = sigma
        self.kind = kind
        self.cut = cut
        self.evaluatedPoint_hcal_ecal_eq_0 = []
        self.evaluatedPoint_true_ecal_eq_0 = []
        self.evaluatedPoint_neighbours_hcal_ecal_eq_0 = []
        self.evaluatedPoint_neighbours_true_ecal_eq_0 = []
        self.evaluatedPoint_parameters_ecal_eq_0 = []
        self.evaluatedPoint_entries_ecal_eq_0 = []
        self.evaluatedPoint_bin_middles_ecal_eq_0 = []
        self.evaluatedPoint_true_min_ecal_eq_0 = []
        self.evaluatedPoint_true_max_ecal_eq_0 = []
        self.evaluatedPoint_ecal = []
        self.evaluatedPoint_hcal = []
        self.evaluatedPoint_true = []
        self.evaluatedPoint_neighbours_ecal = []
        self.evaluatedPoint_neighbours_hcal = []
        self.evaluatedPoint_neighbours_true = []
        self.evaluatedPoint_parameters = []
        self.evaluatedPoint_entries = []
        self.evaluatedPoint_bin_middles = []
        self.evaluatedPoint_true_min = []
        self.evaluatedPoint_true_max = []

        # we define the weight
        if weights == 'gaussian':
            self.weights = lambda x: np.exp(-(x**2) / (sigma**2) / 2)
        else:
            self.weights = weights

        #Case ecal == 0
        self.neigh_ecal_eq_0 = neighbors.NearestNeighbors(
            n_neighbors=n_neighbors_ecal_eq_0, algorithm=algorithm)
        y = self.hcal_train[self.ecal_train == 0]
        z = self.true_train[self.ecal_train == 0]
        self.neigh_ecal_eq_0.fit(np.transpose(np.matrix(y)))

        def forOnePoint_ecal_eq_0(h):
            # the neighbours of the point (ecal,hcal) = (0,h)
            dist, ind = self.neigh_ecal_eq_0.kneighbors(X=h)
            true = z[ind][0]
            hcal = y[ind][0]
            nbins = int(max(true))
            with warnings.catch_warnings():
                try:
                    #we create the histogram
                    warnings.simplefilter("error", OptimizeWarning)
                    entries, bin_edges = np.histogram(true, bins=nbins)
                    bin_middles = 0.5 * (bin_edges[1:] + bin_edges[:-1])

                    # we fit the histogram
                    p0 = np.sqrt(np.std(entries)), bin_middles[np.argmax(
                        entries)], max(entries)
                    parameters, cov_matrix = curve_fit(gaussian_param,
                                                       bin_middles,
                                                       entries,
                                                       p0=p0)

                    true_max = parameters[1] + self.cut * parameters[0]
                    true_min = parameters[1] - self.cut * parameters[0]
                    # we select the good neighbours
                    selected = np.logical_and(true >= true_min,
                                              true <= true_max)
                    hcal = hcal[selected]
                    true = true[selected]

                    # pondered mean of the neighbourhood
                    true = np.transpose(np.matrix(true))
                    hcal = np.transpose(np.matrix(hcal))
                    regr = neighbors.KNeighborsRegressor(
                        n_neighbors=len(true),
                        weights=self.weights,
                        algorithm=self.algorithm)
                    regr.fit(hcal, true)
                    res = regr.predict(h)
                except:
                    parameters = p0
                    true = np.transpose(np.matrix(z[ind][0]))
                    hcal = np.transpose(np.matrix(y[ind][0]))
                    regr = neighbors.KNeighborsRegressor(
                        n_neighbors=len(true),
                        weights=self.weights,
                        algorithm=self.algorithm)
                    regr.fit(hcal, true)
                    res = regr.predict(h)
                    true_min = min(z[ind][0])
                    true_max = max(z[ind][0])
                finally:
                    # we save the values in the attributs
                    self.evaluatedPoint_parameters_ecal_eq_0.append(parameters)
                    self.evaluatedPoint_neighbours_hcal_ecal_eq_0.append(hcal)
                    self.evaluatedPoint_neighbours_true_ecal_eq_0.append(true)
                    self.evaluatedPoint_entries_ecal_eq_0.append(entries)
                    self.evaluatedPoint_bin_middles_ecal_eq_0.append(
                        bin_middles)
                    self.evaluatedPoint_true_min_ecal_eq_0.append(true_min)
                    self.evaluatedPoint_true_max_ecal_eq_0.append(true_max)
                    self.evaluatedPoint_hcal_ecal_eq_0.append(h)
                    self.evaluatedPoint_true_ecal_eq_0.append(res)

                    return res

        #we define the first point of evaluation
        dist, ind = self.neigh_ecal_eq_0.kneighbors(X=0)
        hcal = y[ind][0]
        hcal_min = (max(hcal) + min(hcal)) / 2
        # we evaluate the true energies
        hcal = np.linspace(hcal_min, self.lim,
                           (self.lim - hcal_min) / energystep)
        vect = np.vectorize(forOnePoint_ecal_eq_0)
        true = vect(hcal)
        # we create the interpolation
        self.interpolation_ecal_eq_0 = interp1d(hcal,
                                                true,
                                                kind=kind,
                                                fill_value='extrapolate')

        # Case ecal != 0
        self.neigh_ecal_neq_0 = neighbors.NearestNeighbors(
            n_neighbors=n_neighbors_ecal_neq_0, algorithm=algorithm)
        x = self.ecal_train[self.ecal_train != 0]
        y = self.hcal_train[self.ecal_train != 0]
        z = self.true_train[self.ecal_train != 0]
        self.neigh_ecal_neq_0.fit(np.transpose(np.matrix([x, y])))

        def forOnePoint_ecal_neq_0(e, h):
            # the neighbours of the point (ecal,hcal) = (e,h)
            dist, ind = self.neigh_ecal_neq_0.kneighbors([[e, h]])
            true = z[ind][0]
            hcal = y[ind][0]
            ecal = x[ind][0]
            nbins = int(max(true))
            with warnings.catch_warnings():
                try:
                    #we create the histogram
                    warnings.simplefilter("error", OptimizeWarning)
                    entries, bin_edges = np.histogram(true, bins=nbins)
                    bin_middles = 0.5 * (bin_edges[1:] + bin_edges[:-1])

                    # we fit the histogram
                    p0 = np.sqrt(np.std(entries)), bin_middles[np.argmax(
                        entries)], max(entries)
                    parameters, cov_matrix = curve_fit(gaussian_param,
                                                       bin_middles,
                                                       entries,
                                                       p0=p0)
                    # we define the max and the min te reject points
                    true_max = parameters[1] + self.cut * parameters[0]
                    true_min = parameters[1] - self.cut * parameters[0]
                    # we select the good neighbours
                    selected = np.logical_and(true >= true_min,
                                              true <= true_max)
                    hcal = hcal[selected]
                    true = true[selected]
                    ecal = ecal[selected]
                    # gaussian mean of the neighbourhood
                    true = np.transpose(np.matrix(true))
                    Ecal = np.transpose(np.matrix([ecal, hcal]))
                    regr = neighbors.KNeighborsRegressor(
                        n_neighbors=len(true),
                        weights=self.weights,
                        algorithm=self.algorithm)
                    regr.fit(Ecal, true)
                    res = regr.predict([[e, h]])
                    res = res[0][0]

                except:
                    parameters = p0
                    true_min = min(z[ind][0])
                    true_max = max(z[ind][0])
                    true = np.transpose(np.matrix(z[ind][0]))
                    Ecal = np.transpose(np.matrix([x[ind][0], y[ind][0]]))
                    regr = neighbors.KNeighborsRegressor(
                        n_neighbors=len(true),
                        weights=self.weights,
                        algorithm=self.algorithm)
                    regr.fit(Ecal, true)
                    res = regr.predict([[e, h]])
                    res = res[0][0]
                finally:
                    # we save the values in the attributs
                    self.evaluatedPoint_neighbours_ecal.append(ecal)
                    self.evaluatedPoint_neighbours_hcal.append(hcal)
                    self.evaluatedPoint_neighbours_true.append(true)
                    self.evaluatedPoint_entries.append(entries)
                    self.evaluatedPoint_bin_middles.append(bin_middles)
                    self.evaluatedPoint_ecal.append(e)
                    self.evaluatedPoint_hcal.append(h)
                    self.evaluatedPoint_true.append(res)
                    self.evaluatedPoint_parameters.append(parameters)
                    self.evaluatedPoint_true_min.append(true_min)
                    self.evaluatedPoint_true_max.append(true_max)
                    return res

        #we define the first point of evaluation
        dist, ind = self.neigh_ecal_neq_0.kneighbors(X=[[0, 0]])
        hcal = y[ind][0]
        ecal = x[ind][0]
        hcal_min = (max(hcal) + min(hcal)) / 2
        ecal_min = (max(ecal) + min(ecal)) / 2
        # we evaluate the true energies
        hcal = np.linspace(hcal_min, self.lim,
                           (self.lim - hcal_min) / energystep)
        ecal = np.linspace(ecal_min, self.lim,
                           (self.lim - ecal_min) / energystep)
        eecal, hhcal = np.meshgrid(ecal, hcal)
        vect = np.vectorize(forOnePoint_ecal_neq_0)
        true = vect(eecal, hhcal)
        # we create the interpolation
        self.interpolation_ecal_neq_0 = interp2d(ecal, hcal, true, kind=kind)
예제 #4
0
    def __init__(self,
                 ecal_train=[],
                 hcal_train=[],
                 true_train=[],
                 n_neighbors_ecal_eq_0=2000,
                 n_neighbors_ecal_neq_0=250,
                 algorithm='auto',
                 lim=-1,
                 energystep_ecal_eq_0=1,
                 energystep_ecal_neq_0=5,
                 kind='cubic'):
        """
        Parameters
        ----------
        ecal_train : array-like
        ecal value to train the calibration

        hcal_train : array-like
        hcal value to train the calibration

        true_train : array-like
        true value to train the calibration

        n_neighbors_ecal_eq_0: int
        Number of neighbors to use by default for k_neighbors queries.
        for ecal == 0
        
        n_neighbors_ecal_neq_0: int
        Number of neighbors to use by default for k_neighbors queries.
        for ecal != 0

        algortihm : {‘auto’, ‘ball_tree’, ‘kd_tree’, ‘brute’}, optional
        Algorithm used to compute the nearest neighbors:
        'ball_tree' will use BallTree
        'kd_tree' will use KDtree
        'brute' will use a brute-force search.
        'auto' will attempt to decide the most appropriate algorithm based on
        the values passed to fit method.

        lim : float
        to reject calibration points with ecal + hcal > lim
        if lim = - 1, there is no limit

        energystep_ecal_eq_0 : float
        step between two points of evaluation
        for ecal == 0
        
        energystep_ecal_neq_0 : float
        step between two points of evaluation
        for ecal != 0

        kind : str or int, optional
        Specifies the kind of interpolation as a string (‘linear’, ‘nearest’,
        ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’ where ‘zero’, ‘slinear’,
        ‘quadratic’ and ‘cubic’ refer to a spline interpolation of zeroth,
        first, second or third order) or as an integer specifying the order of
        the spline interpolator to use. Default is ‘linear’

        """
        Calibration.__init__(self, ecal_train, hcal_train, true_train, lim)

        self.n_neighbors_ecal_eq_0 = n_neighbors_ecal_eq_0
        self.n_neighbors_ecal_neq_0 = n_neighbors_ecal_neq_0
        self.algorithm = algorithm
        self.kind = kind
        self.evaluatedPoint_hcal_ecal_eq_0 = []
        self.evaluatedPoint_true_ecal_eq_0 = []
        self.evaluatedPoint_neighbours_hcal_ecal_eq_0 = []
        self.evaluatedPoint_neighbours_true_ecal_eq_0 = []
        self.evaluatedPoint_parameters_ecal_eq_0 = []
        self.evaluatedPoint_entries_ecal_eq_0 = []
        self.evaluatedPoint_bin_middles_ecal_eq_0 = []
        self.evaluatedPoint_ecal = []
        self.evaluatedPoint_hcal = []
        self.evaluatedPoint_true = []
        self.evaluatedPoint_neighbours_ecal = []
        self.evaluatedPoint_neighbours_hcal = []
        self.evaluatedPoint_neighbours_true = []
        self.evaluatedPoint_parameters = []
        self.evaluatedPoint_entries = []
        self.evaluatedPoint_bin_middles = []
        self.evaluatedPoint_reducedchi2 = []
        self.evaluatedPoint_reducedchi2_ecal_eq_0 = []

        #Case ecal == 0
        self.neigh_ecal_eq_0 = neighbors.NearestNeighbors(
            n_neighbors=self.n_neighbors_ecal_eq_0, algorithm=algorithm)
        y = self.hcal_train[self.ecal_train == 0]
        self.hcal_train_ecal_eq_0_min = min(y)
        z = self.true_train[self.ecal_train == 0]
        self.neigh_ecal_eq_0.fit(np.transpose(np.matrix(y)))

        def forOnePoint_ecal_eq_0(h):
            # the neighbours of the point (ecal,hcal) = (0,h)
            dist, ind = self.neigh_ecal_eq_0.kneighbors(X=h)
            dist = dist[0]
            ind = ind[0]
            dlim = h - self.hcal_train_ecal_eq_0_min + 0.1
            ind = ind[dist <= dlim]
            true = z[ind]
            hcal = y[ind]
            binwidth = 1
            reduced = math.nan
            with warnings.catch_warnings():
                try:
                    if len(true) == 0:
                        raise UserWarning
                    nbins = np.arange(min(true),
                                      max(true) + binwidth, binwidth)
                    #we create the histogram
                    warnings.simplefilter("error", OptimizeWarning)
                    entries, bin_edges = np.histogram(true, bins=nbins)
                    bin_middles = 0.5 * (bin_edges[1:] + bin_edges[:-1])
                    bin_middles = bin_middles[entries != 0]
                    entries = entries[entries != 0]

                    # we fit the histogram
                    p0 = np.sqrt(np.std(entries)), bin_middles[np.argmax(
                        entries)], max(entries)
                    error = np.sqrt(entries)
                    parameters, cov_matrix = curve_fit(gaussian_param,
                                                       bin_middles,
                                                       entries,
                                                       sigma=error,
                                                       p0=p0)
                    res = parameters[1]

                    chi2 = np.sum(
                        ((gaussian_param(bin_middles, *parameters) - entries) /
                         error)**2)
                    reduced = chi2 / (len(bin_middles) - len(parameters))

                    if reduced > 10:
                        raise OptimizeWarning
                except UserWarning:
                    parameters = math.nan
                    entries = math.nan
                    res = math.nan
                    bin_middles = math.nan
                    print("calibration issue for ecal = 0, hcal = ", h,
                          "reduced chi2 = ", reduced)
                except (OptimizeWarning, RuntimeError):
                    parameters = p0
                    res = parameters[1]
                    print("calibration issue for ecal = 0, hcal = ", h,
                          "reduced chi2 = ", reduced)
                finally:
                    # we save the values in the attributs
                    self.evaluatedPoint_hcal_ecal_eq_0.append(h)
                    self.evaluatedPoint_true_ecal_eq_0.append(res)
                    self.evaluatedPoint_parameters_ecal_eq_0.append(parameters)
                    self.evaluatedPoint_neighbours_hcal_ecal_eq_0.append(hcal)
                    self.evaluatedPoint_neighbours_true_ecal_eq_0.append(true)
                    self.evaluatedPoint_entries_ecal_eq_0.append(entries)
                    self.evaluatedPoint_bin_middles_ecal_eq_0.append(
                        bin_middles)
                    self.evaluatedPoint_reducedchi2_ecal_eq_0.append(reduced)

                    return res

        #we define the first point of evaluation
        dist, ind = self.neigh_ecal_eq_0.kneighbors(X=0)
        hcal = y[ind][0]
        hcal_min = (max(hcal) + min(hcal)) / 2
        # we evaluate the true energies
        hcal = np.arange(hcal_min, self.lim + energystep_ecal_eq_0,
                         energystep_ecal_eq_0)
        vect = np.vectorize(forOnePoint_ecal_eq_0)
        true = vect(hcal)
        hcal = hcal[np.invert(np.isnan(true))]
        true = true[np.invert(np.isnan(true))]
        # we create the interpolation
        self.interpolation_ecal_eq_0 = interp1d(hcal,
                                                true,
                                                kind=kind,
                                                fill_value='extrapolate')

        # Case ecal != 0
        self.neigh_ecal_neq_0 = neighbors.NearestNeighbors(
            n_neighbors=self.n_neighbors_ecal_neq_0, algorithm=algorithm)
        x = self.ecal_train[self.ecal_train != 0]
        y = self.hcal_train[self.ecal_train != 0]
        z = self.true_train[self.ecal_train != 0]
        self.neigh_ecal_neq_0.fit(np.transpose(np.matrix([x, y])))

        def forOnePoint_ecal_neq_0(e, h):
            # the neighbours of the point (ecal,hcal) = (e,h)
            dist, ind = self.neigh_ecal_neq_0.kneighbors([[e, h]])
            true = z[ind][0]
            hcal = y[ind][0]
            ecal = x[ind][0]
            binwidth = 1
            nbins = np.arange(min(true), max(true) + binwidth, binwidth)
            with warnings.catch_warnings():
                try:
                    #we create the histogram
                    warnings.simplefilter("error", OptimizeWarning)
                    entries, bin_edges = np.histogram(true, bins=nbins)
                    bin_middles = 0.5 * (bin_edges[1:] + bin_edges[:-1])
                    bin_middles = bin_middles[entries != 0]
                    entries = entries[entries != 0]
                    reduced = math.nan

                    # we fit the histogram
                    p0 = np.sqrt(np.std(entries)), bin_middles[np.argmax(
                        entries)], max(entries)
                    error = np.sqrt(entries)
                    parameters, cov_matrix = curve_fit(gaussian_param,
                                                       bin_middles,
                                                       entries,
                                                       sigma=error,
                                                       p0=p0)
                    res = parameters[1]

                    chi2 = np.sum(
                        ((gaussian_param(bin_middles, *parameters) - entries) /
                         error)**2)
                    reduced = chi2 / (len(bin_middles) - len(parameters))

                    if reduced > 10:
                        raise OptimizeWarning

                except (OptimizeWarning, RuntimeError):
                    parameters = p0
                    res = parameters[1]
                    if e + h < self.lim:
                        print("calibration issue for ecal = ", e, " hcal = ",
                              h)
                finally:
                    # we save the values in the attributs
                    self.evaluatedPoint_neighbours_ecal.append(ecal)
                    self.evaluatedPoint_neighbours_hcal.append(hcal)
                    self.evaluatedPoint_neighbours_true.append(true)
                    self.evaluatedPoint_entries.append(entries)
                    self.evaluatedPoint_bin_middles.append(bin_middles)
                    self.evaluatedPoint_ecal.append(e)
                    self.evaluatedPoint_hcal.append(h)
                    self.evaluatedPoint_true.append(res)
                    self.evaluatedPoint_parameters.append(parameters)
                    self.evaluatedPoint_reducedchi2.append(reduced)
                    return res

        #we define the first point of evaluation
        dist, ind = self.neigh_ecal_neq_0.kneighbors(X=[[0, 0]])
        hcal = y[ind][0]
        ecal = x[ind][0]
        hcal_min = (max(hcal) + min(hcal)) / 2
        ecal_min = (max(ecal) + min(ecal)) / 2
        # we evaluate the true energies
        hcal = np.linspace(hcal_min, self.lim,
                           (self.lim - hcal_min) / energystep_ecal_neq_0)
        ecal = np.linspace(ecal_min, self.lim,
                           (self.lim - ecal_min) / energystep_ecal_neq_0)
        eecal, hhcal = np.meshgrid(ecal, hcal)
        vect = np.vectorize(forOnePoint_ecal_neq_0)
        true = vect(eecal, hhcal)
        # we create the interpolation
        self.interpolation_ecal_neq_0 = interp2d(ecal, hcal, true, kind=kind)
예제 #5
0
    def __init__(self,
                 ecal_train=[],
                 hcal_train=[],
                 true_train=[],
                 n_neighbors_ecal_eq_0=2000,
                 n_neighbors_ecal_neq_0=250,
                 weights='gaussian',
                 algorithm='auto',
                 sigma=5,
                 lim=-1):
        """
        Parameters
        ----------
        ecal_train : array-like
        ecal value to train the calibration

        hcal_train : array-like
        hcal value to train the calibration

        true_train : array-like
        true value to train the calibration

        n_neighbors_ecal_eq_0: int
        Number of neighbors to use by default for k_neighbors queries.
        for ecal == 0
        
        n_neighbors_ecal_neq_0: int
        Number of neighbors to use by default for k_neighbors queries.
        for ecal != 0

        weight : str or callable
        weight function used in prediction. Possible values:
        'uniform' : uniform weights. All points in each neighborhood are
        weighted equally.
        'distance' : weight points by the inverse of their distance. in this
        case, closer neighbors of a query point will have a greater influence
        than neighbors which are further away.
        [callable] : a user-defined function which accepts an array of
        distances, and returns an array of the same shape containing the weights.
        'gaussian'
        Gaussian weights are used by default.

        algortihm : {‘auto’, ‘ball_tree’, ‘kd_tree’, ‘brute’}, optional
        Algorithm used to compute the nearest neighbors:
        'ball_tree' will use BallTree
        'kd_tree' will use KDtree
        'brute' will use a brute-force search.
        'auto' will attempt to decide the most appropriate algorithm based on
        the values passed to fit method.

        sigma : float
        sigma for the gaussian if weight == 'gaussian'

        lim : float
        if ecal + hcal > lim, the calibrated energy ecalib = math.nan
        if lim = - 1, there is no limit
        """

        # We use the constructor of the mother class
        Calibration.__init__(self, ecal_train, hcal_train, true_train, lim)

        # we define the weight
        if weights == 'gaussian':
            self.weights = lambda x: np.exp(-(x**2) / (sigma**2) / 2)
        else:
            self.weights = weights

        self.n_neighbors_ecal_eq_0 = n_neighbors_ecal_eq_0
        self.n_neighbors_ecal_neq_0 = n_neighbors_ecal_neq_0
        self.algorithm = algorithm
        self.sigma = sigma

        # Case ecal != 0
        X_train = [ecal_train[ecal_train != 0], hcal_train[ecal_train != 0]]
        self.X_train1 = X_train
        X_train = np.transpose(np.matrix(X_train))
        Y_train = true_train[ecal_train != 0]
        self.Y_train1 = Y_train
        Y_train = np.transpose(np.matrix(Y_train))
        self.neigh_ecal_neq_0 = neighbors.KNeighborsRegressor(
            n_neighbors=n_neighbors_ecal_neq_0,
            weights=self.weights,
            algorithm=self.algorithm)
        self.neigh_ecal_neq_0.fit(X_train, Y_train)

        #case ecal == 0

        X_train = hcal_train[ecal_train == 0]
        self.X_train2 = X_train
        X_train = np.transpose(np.matrix(X_train))
        Y_train = true_train[ecal_train == 0]
        self.Y_train2 = Y_train
        Y_train = np.transpose(np.matrix(Y_train))
        self.neigh_ecal_eq_0 = neighbors.KNeighborsRegressor(
            n_neighbors=n_neighbors_ecal_eq_0,
            weights=self.weights,
            algorithm=algorithm)
        self.neigh_ecal_eq_0.fit(X_train, Y_train)
    def __init__(self,ecal_train=[],hcal_train=[],true_train=[],lim_min=-1,lim_max=-1,lim=-1):
        """
        Constructor of the class
        
        Parameters
        ---------
        ecal_train : array
        ecal value to train the calibration
    
        hcal_train : array
        ecal value to train the calibration
    
        true_train : array
        ecal value to train the calibration
        
        lim_min : float
        linear regression is done with points with ecal + hcal > lim_min
        if lim_min = - 1, there is no limit
        
        lim_max : float
        linear regression is done with points with ecal + hcal < lim_max
        if lim_max = - 1, there is no limit
        
        lim : float
        if ecal + hcal > lim, the calibrated energy ecalib = math.nan
        if lim = - 1, there is no limit
        """
        
        # We use the constructor of the mother class 
        Calibration.__init__(self,ecal_train,hcal_train,true_train,lim)
        
        self.lim_min = lim_min
        self.lim_max = lim_max
        
        if lim_min == -1:
            ind_min = np.ones(len(self.ecal_train),dtype=bool)
        else:
            ind_min = self.ecal_train + self.hcal_train > lim_min
        if lim_max == -1:
            ind_max = np.ones(len(self.ecal_train),dtype=bool)
        else:
            ind_max = self.ecal_train+ self.hcal_train < lim_max

        #CASE : ecal != 0
        ind_0 = self.ecal_train != 0
        ind = np.logical_and(ind_min,ind_max)
        ind = np.logical_and(ind,ind_0)
        X_train = [self.ecal_train[ind],self.hcal_train[ind]]
        X_train = np.transpose(np.matrix(X_train))
        Y_train = self.true_train[ind]
        Y_train = np.transpose(np.matrix(Y_train))
        linRegr_ecal_neq_0 = linear_model.LinearRegression()
        linRegr_ecal_neq_0.fit(X_train,Y_train)

        #CASE : ecal == 0
        ind_0 = self.ecal_train == 0
        ind = np.logical_and(ind_min,ind_max)
        ind = np.logical_and(ind,ind_0)
        X_train = self.hcal_train[ind]
        X_train = np.transpose(np.matrix(X_train))
        Y_train = self.true_train[ind]
        Y_train = np.transpose(np.matrix(Y_train))
        linRegr_ecal_eq_0 = linear_model.LinearRegression()
        linRegr_ecal_eq_0.fit(X_train,Y_train)
        
        self.linRegr_ecal_neq_0 = linRegr_ecal_neq_0
        self.linRegr_ecal_eq_0 = linRegr_ecal_eq_0
    def __init__(self,
                 ecal_train=[],
                 hcal_train=[],
                 true_train=[],
                 nbLego=60,
                 lim=150):
        """
        Parameters
        ----------
        ecal_train : array
        ecal value to train the calibration
        
        hcal_train : array
        ecal value to train the calibration
        
        true_train : array
        ecal value to train the calibration
        
        lim : float
        if ecal + hcal > lim, the calibrated energy ecalib = math.nan
        if lim = - 1, there is no limit
        
        nbLego : int
        The plane (ecal,hcal) is divided in nbLego*nbLego
        """
        Calibration.__init__(self, ecal_train, hcal_train, true_train, lim)
        nbLego = int(nbLego)
        self.nbLego = nbLego

        ener_max = max(max(self.hcal_train), max(self.ecal_train))
        ecal = np.linspace(0, ener_max, nbLego)
        hcal = np.linspace(0, ener_max, nbLego)
        self.delta = hcal[1] - hcal[0]
        self.nbLego = nbLego
        ecal_calib = []
        hcal_calib = []
        true_calib = []
        # when ecal == 0
        true_calib_lim = []
        precision = []

        bins_ecal = np.arange(nbLego - 1)
        bins_hcal = np.arange(nbLego - 1)
        for i in bins_ecal:
            ind_ecal = np.logical_and(self.ecal_train >= ecal[i],
                                      self.ecal_train < ecal[i + 1])
            ind_ecal = np.logical_and(self.ecal_train != 0, ind_ecal)
            for j in bins_hcal:
                ind_hcal = np.logical_and(self.hcal_train >= hcal[j],
                                          self.hcal_train < hcal[j + 1])
                ind_true = np.logical_and(ind_ecal, ind_hcal)
                true = self.true_train[ind_true]
                if true.size != 0:
                    true_calib.append(np.mean(true))
                    precision.append(
                        np.std(true) / np.sqrt(len(true)) / np.mean(true))
                else:
                    true_calib.append(math.nan)
                    precision.append(None)
                ecal_calib.append(np.mean([ecal[i], ecal[i + 1]]))
                hcal_calib.append(np.mean([hcal[j], hcal[j + 1]]))
        # when ecal == 0
        for i in bins_hcal:
            ind_hcal = np.logical_and(self.hcal_train >= hcal[i],
                                      self.hcal_train < hcal[i + 1])
            ind_true = np.logical_and(self.ecal_train == 0, ind_hcal)
            true = self.true_train[ind_true]
            if true.size != 0:
                true_calib_lim.append(np.mean(true))
            else:
                true_calib_lim.append(math.nan)

        self.ecal = np.array(ecal_calib)
        self.ecal_max = max(self.ecal)
        self.hcal = np.array(hcal_calib)
        self.hcal_max = max(self.hcal)
        self.true = np.array(true_calib)
        self.true_lim = np.array(true_calib_lim)
        self.precision = np.array(precision)