Ejemplo n.º 1
0
    def apply(self):
        """
        Apply the optimizer.
        """
        self.config.logger.info("DnnOptimiser::apply, input size: %d",
                                self.config.dim_input)
        loaded_model = self.load_model()

        myfile = TFile.Open("%s/output_%s_nEv%d.root" % \
                            (self.config.dirapply, self.config.suffix, self.config.train_events),
                            "recreate")
        h_dist_all_events, h_deltas_all_events, h_deltas_vs_dist_all_events =\
                plot_utils.create_apply_histos(self.config, self.config.suffix, infix="all_events_")

        for indexev in self.config.partition['apply']:
            inputs_, exp_outputs_ = load_train_apply(
                self.config.dirinput_apply, indexev, self.config.z_range,
                self.config.grid_r, self.config.grid_phi, self.config.grid_z,
                self.config.opt_train, self.config.opt_predout)
            inputs_single = np.empty(
                (1, self.config.grid_phi, self.config.grid_r,
                 self.config.grid_z, self.config.dim_input))
            exp_outputs_single = np.empty(
                (1, self.config.grid_phi, self.config.grid_r,
                 self.config.grid_z, self.config.dim_output))
            inputs_single[0, :, :, :, :] = inputs_
            exp_outputs_single[0, :, :, :, :] = exp_outputs_

            distortion_predict_group = loaded_model.predict(inputs_single)

            distortion_numeric_flat_m, distortion_predict_flat_m, deltas_flat_a, deltas_flat_m =\
                plot_utils.get_apply_results_single_event(distortion_predict_group,
                                                          exp_outputs_single)
            plot_utils.fill_apply_tree_single_event(self.config, indexev,
                                                    distortion_numeric_flat_m,
                                                    distortion_predict_flat_m,
                                                    deltas_flat_a,
                                                    deltas_flat_m)
            plot_utils.fill_apply_tree(h_dist_all_events, h_deltas_all_events,
                                       h_deltas_vs_dist_all_events,
                                       distortion_numeric_flat_m,
                                       distortion_predict_flat_m,
                                       deltas_flat_a, deltas_flat_m)

        for hist in (h_dist_all_events, h_deltas_all_events,
                     h_deltas_vs_dist_all_events):
            hist.Write()
        plot_utils.fill_profile_apply_hist(h_deltas_vs_dist_all_events,
                                           self.config.profile_name,
                                           self.config.suffix)
        plot_utils.fill_std_dev_apply_hist(h_deltas_vs_dist_all_events,
                                           self.config.h_std_dev_name,
                                           self.config.suffix, "all_events_")

        myfile.Close()
        self.config.logger.info("Done apply")
 def __data_generation(self, list_ids_temp):
     'Generates data containing batch_size samples'
     # Initialization
     inputs = np.empty((self.batch_size, self.phi_slice, self.r_row,
                        self.z_col, self.dim_input))
     exp_outputs = np.empty((self.batch_size, self.phi_slice, self.r_row,
                             self.z_col, self.dim_output))
     # Generate data
     for i, id_num in enumerate(list_ids_temp):
         # Store
         inputs_i, exp_outputs_i = load_train_apply(
             self.data_dir, id_num, self.z_range, self.r_row,
             self.phi_slice, self.z_col, self.opt_train, self.opt_predout)
         inputs[i, :, :, :, :] = inputs_i
         exp_outputs[i, :, :, :, :] = exp_outputs_i
     return inputs, exp_outputs
Ejemplo n.º 3
0
    def __data_generation(self, list_ids_temp):
        """
        Generate data corresponding to the list of indices

        :param list list_ids_temp: list of file indices for a given batch
        :return: input and output data corresponding to the indices
        :rtype: tuple(np.ndarray, np.ndarray)
        """
        # Initialization
        inputs = np.empty((self.batch_size, self.grid_phi, self.grid_r,
                           self.grid_z, self.dim_input))
        exp_outputs = np.empty((self.batch_size, self.grid_phi, self.grid_r,
                                self.grid_z, self.dim_output))
        # Generate data
        for i, id_num in enumerate(list_ids_temp):
            # Store
            inputs_i, exp_outputs_i = load_train_apply(
                self.dirinput, id_num, self.z_range, self.grid_r,
                self.grid_phi, self.grid_z, self.opt_train, self.opt_predout)
            inputs[i, :, :, :, :] = inputs_i
            exp_outputs[i, :, :, :, :] = exp_outputs_i
        return inputs, exp_outputs
Ejemplo n.º 4
0
    def apply(self):
        self.logger.info("DnnOptimizer::apply, input size: %d", self.dim_input)

        json_file = open("%s/model_%s_nEv%d.json" % \
                         (self.dirmodel, self.suffix, self.train_events), "r")
        loaded_model_json = json_file.read()
        json_file.close()
        loaded_model = \
            model_from_json(loaded_model_json, {'SymmetryPadding3d' : SymmetryPadding3d})
        loaded_model.load_weights("%s/model_%s_nEv%d.h5" % \
                                  (self.dirmodel, self.suffix, self.train_events))

        myfile = TFile.Open("%s/output_%s_nEv%d.root" % \
                            (self.dirval, self.suffix, self.train_events), "recreate")
        h_dist_all_events = TH2F(
            "%s_all_events_%s" % (self.h_dist_name, self.suffix), "", 500, -5,
            5, 500, -5, 5)
        h_deltas_all_events = TH1F(
            "%s_all_events_%s" % (self.h_deltas_name, self.suffix), "", 1000,
            -1., 1.)
        h_deltas_vs_dist_all_events = TH2F("%s_all_events_%s" % \
                                           (self.h_deltas_vs_dist_name, self.suffix),
                                           "", 500, -5.0, 5.0, 100, -0.5, 0.5)

        for iexperiment in self.partition['apply']:
            indexev = iexperiment
            inputs_, exp_outputs_ = load_train_apply(
                self.dirinput_apply, indexev, self.selopt_input,
                self.selopt_output, self.grid_r, self.grid_phi, self.grid_z,
                self.opt_train, self.opt_predout)
            inputs_single = np.empty(
                (1, self.grid_phi, self.grid_r, self.grid_z, self.dim_input))
            exp_outputs_single = np.empty(
                (1, self.grid_phi, self.grid_r, self.grid_z, self.dim_output))
            inputs_single[0, :, :, :, :] = inputs_
            exp_outputs_single[0, :, :, :, :] = exp_outputs_

            distortion_predict_group = loaded_model.predict(inputs_single)
            distortion_predict_flat_m = distortion_predict_group.reshape(-1, 1)
            distortion_predict_flat_a = distortion_predict_group.flatten()

            distortion_numeric_group = exp_outputs_single
            distortion_numeric_flat_m = distortion_numeric_group.reshape(-1, 1)
            distortion_numeric_flat_a = distortion_numeric_group.flatten()
            deltas_flat_a = (distortion_predict_flat_a -
                             distortion_numeric_flat_a)
            deltas_flat_m = (distortion_predict_flat_m -
                             distortion_numeric_flat_m)

            h_suffix = "Ev%d_Mean%d_%s" % (iexperiment[0], iexperiment[1],
                                           self.suffix)
            h_dist = TH2F("%s_%s" % (self.h_dist_name, h_suffix), "", 500, -5,
                          5, 500, -5, 5)
            h_deltas = TH1F("%s_%s" % (self.h_deltas_name, h_suffix), "", 1000,
                            -1., 1.)
            h_deltas_vs_dist = TH2F(
                "%s_%s" % (self.h_deltas_vs_dist_name, h_suffix), "", 500,
                -5.0, 5.0, 100, -0.5, 0.5)

            fill_hist(h_dist_all_events, np.concatenate((distortion_numeric_flat_m, \
                                distortion_predict_flat_m), axis=1))
            fill_hist(
                h_dist,
                np.concatenate(
                    (distortion_numeric_flat_m, distortion_predict_flat_m),
                    axis=1))
            fill_hist(h_deltas, deltas_flat_a)
            fill_hist(h_deltas_all_events, deltas_flat_a)
            fill_hist(
                h_deltas_vs_dist,
                np.concatenate((distortion_numeric_flat_m, deltas_flat_m),
                               axis=1))
            fill_hist(
                h_deltas_vs_dist_all_events,
                np.concatenate((distortion_numeric_flat_m, deltas_flat_m),
                               axis=1))

            prof = h_deltas_vs_dist.ProfileX()
            prof.SetName("%s_%s" % (self.profile_name, h_suffix))

            h_dist.Write()
            h_deltas.Write()
            h_deltas_vs_dist.Write()
            prof.Write()

            h1tmp = h_deltas_vs_dist.ProjectionX("h1tmp")
            h_std_dev = h1tmp.Clone("%s_%s" % (self.h_std_dev_name, h_suffix))
            h_std_dev.Reset()
            h_std_dev.SetXTitle("Numerical distortion fluctuation (cm)")
            h_std_dev.SetYTitle(
                "std.dev. of (Pred. - Num.) distortion fluctuation (cm)")
            nbin = int(h_std_dev.GetNbinsX())
            for ibin in range(0, nbin):
                h1diff = h_deltas_vs_dist.ProjectionY("h1diff", ibin + 1,
                                                      ibin + 1, "")
                stddev = h1diff.GetStdDev()
                stddev_err = h1diff.GetStdDevError()
                h_std_dev.SetBinContent(ibin + 1, stddev)
                h_std_dev.SetBinError(ibin + 1, stddev_err)
            h_std_dev.Write()

        h_dist_all_events.Write()
        h_deltas_all_events.Write()
        h_deltas_vs_dist_all_events.Write()
        prof_all_events = h_deltas_vs_dist_all_events.ProfileX()
        prof_all_events.SetName("%s_all_events_%s" %
                                (self.profile_name, self.suffix))
        prof_all_events.Write()

        h1tmp = h_deltas_vs_dist_all_events.ProjectionX("h1tmp")
        h_std_dev_all_events = h1tmp.Clone("%s_all_events_%s" %
                                           (self.h_std_dev_name, self.suffix))
        h_std_dev_all_events.Reset()
        h_std_dev_all_events.SetXTitle("Numerical distortion fluctuation (cm)")
        h_std_dev_all_events.SetYTitle(
            "std.dev. of (Pred. - Num.) distortion fluctuation (cm)")
        nbin = int(h_std_dev_all_events.GetNbinsX())
        for ibin in range(0, nbin):
            h1diff = h_deltas_vs_dist_all_events.ProjectionY(
                "h1diff", ibin + 1, ibin + 1, "")
            stddev = h1diff.GetStdDev()
            stddev_err = h1diff.GetStdDevError()
            h_std_dev_all_events.SetBinContent(ibin + 1, stddev)
            h_std_dev_all_events.SetBinError(ibin + 1, stddev_err)
        h_std_dev_all_events.Write()

        myfile.Close()
        self.logger.info("Done apply")