Beispiel #1
0
    def plot_multirun_singlegroup_sequential(self, plot_range):
        '''
        input plot_range, passed to self.plot_run together with
            pars, list of lists  of fit parameter values
            asymm, asyme 2d
            # B1 fit
        '''
        from mujpy.aux.aux import int2min, mixer
        from numpy import cos, pi, vstack

        kgroup = 0  # default single
        # dashboard must become a suite thing: each sequential fit has its own
        pars = []
        # self.suite.console('mufitplot: Inside sequential plot; debug mode')
        for k, lastfit in enumerate(self.lastfits):
            values = lastfit.values

            #print('plot_run muplotfit debug: pars = {}'.format(pars)) run {},  values = {}'.format(run[0].get_runNumber_int(),values))
            pars.append(values)
        asymm, asyme = self.suite.asymmetry_multirun(kgroup)  #
        self.multigroup = False
        if self.rotating_frame_frequencyMHz:
            for k in range(asymm.shape[0]):
                if not k:
                    time = self.suite.time
                else:
                    time = vstack((time, self.suite.time))
            self.rrf_asymm = mixer(time, asymm,
                                   self.rotating_frame_frequencyMHz)
            self.rrf_asyme = mixer(time, asyme,
                                   self.rotating_frame_frequencyMHz)
        return self.plot_run(plot_range, pars, asymm, asyme)
Beispiel #2
0
 def plot_singlerun_multigroup_userpar(self, plot_range):
     '''
     input plot_range, passed to self.plot_run together with
         pars, list of one list of fit parameter values
         # A21
         asymm, asyme 2d        
     '''
     from mujpy.aux.aux import int2min_multigroup, mixer, calib
     from numpy import cos, pi, vstack
     userpardicts = (self.dashboard["userpardicts_guess"] if self.guess else
                     self.dashboard["userpardicts_result"])
     pardict = self.dashboard["model_guess"][0]["pardicts"][0]
     pars, _, _, _, _ = int2min_multigroup(userpardicts)
     p = pars
     if calib(self.dashboard):
         for kgroup, group in enumerate(self.suite.grouping):
             group['alpha'] = eval(pardict["function_multi"][kgroup])
             self.suite.groups[kgroup]["alpha"] = eval(
                 pardict["function_multi"][kgroup])
     asymm, asyme = self.suite.asymmetry_multigroup()
     if self.rotating_frame_frequencyMHz:
         # print('mufitplot single_plot_multi debug: asymm.shape[0] = {}'.format(asymm.shape[0]))
         for k in range(asymm.shape[0]):
             if not k:
                 time = self.suite.time
             else:
                 time = vstack((time, self.suite.time))
         self.rrf_asymm = mixer(time, asymm,
                                self.rotating_frame_frequencyMHz)
         self.rrf_asyme = mixer(time, asyme,
                                self.rotating_frame_frequencyMHz)
     self.multigroup = True
     #self.suite.console('mufitplot: Inside single plot; debug mode')
     return self.plot_run(plot_range, pars, asymm, asyme)
Beispiel #3
0
 def plot_singlerun_multigroup_sequential(self, plot_range):
     '''
     inputs: 
         plot_range, passed to self.plot_run together with
         pars, list of lists  of fit parameter values
         asymm, asyme 2d
         # A20 fit
     if model_result
         self.lastfit is a list of lists 
     if model_guess
         reproduces the same from a single guesses            
     '''
     from mujpy.aux.aux import int2min, mixer
     from numpy import cos, pi, vstack
     # dashboard is a multi_sequential thing: each sequential fit has its own
     asymm, asyme = self.suite.asymmetry_multigroup()
     if self.rotating_frame_frequencyMHz:
         for k in range(asymm.shape[0]):
             if not k:
                 time = self.suite.time
             else:
                 time = vstack(
                     (time, self.suite.time
                      ))  ### TIME BECOMES MULTIDIM, NEEDED IN RRF?
         self.rrf_asymm = mixer(time, asymm,
                                self.rotating_frame_frequencyMHz)
         self.rrf_asyme = mixer(time, asyme,
                                self.rotating_frame_frequencyMHz)
     pars = []
     # self.suite.console('mufitplot single_plot_multi_sequential debug')
     for kgroup in range(asymm.shape[0]):
         if self.model == "model_result":
             #print('single_plot_multi_sequential mufitplot debug: self model = {}',format(self.dashboard[self.model][kgroup]))
             values = self.lastfits[kgroup].values
         else:
             #print('single_plot_multi_sequential mufitplot debug: self model = {}',format(self.dashboard[self.model]))
             values, _, _, _, _ = int2min(self.dashboard[self.model])
         pars.append(values)  # here pars is list of list!!
     self.multigroup = True
     return self.plot_run(plot_range, pars, asymm, asyme)
Beispiel #4
0
 def plot_singlerun(self, plot_range):
     '''
     input plot_range, passed to self.plot_run together with
         pars, list of one list of fit parameter values
         asymm, asyme 1d        
     calib pops first par and passes it as alpha to standard plot
     '''
     from mujpy.aux.aux import int2min, mixer, calib
     from numpy import cos, pi
     kgroup = 0  # default single group
     pars, _, _, _, _ = int2min(self.dashboard[self.model])
     if calib(self.dashboard):
         self.suite.grouping[kgroup]['alpha'] = pars[
             0]  # from fit parameter to standard asymmetry mode
     asymm, asyme = self.suite.asymmetry_single(self.suite._the_runs_[0], 0)
     if self.rotating_frame_frequencyMHz:
         self.rrf_asymm = mixer(self.suite.time, asymm,
                                self.rotating_frame_frequencyMHz)
         self.rrf_asyme = mixer(self.suite.time, asyme,
                                self.rotating_frame_frequencyMHz)
     #self.suite.console('mufitplot: Inside single plot; debug mode')
     self.multigroup = False
     return self.plot_run(plot_range, pars, asymm, asyme)
Beispiel #5
0
    def plot_run(self, plot_range, pars, asymm, asyme):
        '''
        input :
            plot_range
                (start,stop)
                (start,stop,pack)
                (start_early,stop_early,pack_early,stop_late,pack_late)
            pars list (single, calib) or list of lists (sequence)
            asymm, asyme  1d (single, calib) or 2d (sequence)
        calls either 
            self.chi_1 or self.chi_2 (stats and model function, 
                                      deals also with plotting model function)
            set_single_fit or set_sequence_fit, from aux.plot
                                    (produces actual figures)
        Draws either static or anim figures using aux.plot functions 
        '''
        from mujpy.aux.aux import derange, rebin, multigroup_in_components
        from mujpy.aux.aux import get_run_title, userpars, mixer
        from mujpy.aux.plot import set_single_fit, set_sequence_fit
        from iminuit import Minuit
        from numpy import ones

        # print('plot_run muplotfit debug: pars = {}'.format(pars))
        # chi_1: single chisquare, different asymm.shape,
        # chi_2: many chisquare, different asymm.shape,
        chi = self.chi_1 if self.single_chi() else self.chi_2
        # print('plot_run mufitplot debug: single chi is {}'.format(self.single_chi()))

        run_title = get_run_title(self.suite)  # always a list, even for single
        string = 'global ' if userpars(self.dashboard) else ''

        if self.guess:
            run_title = [
                title + ": " + string + "guess values" for title in run_title
            ]
        else:
            run_title = [
                title + ": " + string + "fit results" for title in run_title
            ]
        plottup = derange(plot_range, self.suite.histoLength)
        #############################
        # rebinning of data as in fit
        # this works for single
        # and for sequential
        #############################
        fittup = derange(self.dashboard["fit_range"],
                         self.suite.histoLength)  # range as tuple
        fit_pack = 1
        if len(fittup) == 3:  # plot start stop pack
            fit_start, fit_stop, fit_pack = fittup[0], fittup[1], fittup[2]
        elif len(fittup) == 2:  # plot start stop
            fit_start, fit_stop = fittup[0], fittup[1]

        # load modules and reproduce fit
        t_fit, y_fit, ey_fit = rebin(self.suite.time,
                                     asymm, [fit_start, fit_stop],
                                     fit_pack,
                                     e=asyme)
        # single slices as in fit
        # print('muplotfit plot_run debug: calling chi for f_fit')
        nu_fit, f_fit, chi_fit = chi(t_fit, y_fit, ey_fit, pars)
        if self.rotating_frame_frequencyMHz:  # (t_fit,y_fit,f_fit,ey_fit) the last three must be transformed to rrf
            _, y_fit, ey_fit = rebin(self.suite.time,
                                     self.rrf_asymm, [fit_start, fit_stop],
                                     fit_pack,
                                     e=self.rrf_asyme)
            f_fit = mixer(t_fit, f_fit, self.rotating_frame_frequencyMHz)
        if not nu_fit:
            return False
        # function as in fit

        if len(plottup) == 5:
            ###################
            # double range plot
            ###################
            early_late = True
            start, stop, pack, last, packlate = plottup

            t_late, y_late, ey_late = rebin(self.suite.time,
                                            asymm, [stop, last],
                                            packlate,
                                            e=asyme)
            # rebinned late slices
            packfit = int(packlate / 2)
            tfl, dum = rebin(self.suite.time, asymm, [stop, last], packfit)
            # late time slice for function
            # tfl,fl for late plot curve
            _, f_late_res, _ = chi(
                t_late, None, None,
                pars)  # for f_late_res calculating residues (on data points)
            _, fl, _ = chi(tfl, None, None,
                           pars)  # t_late,fls for plotting residues
            if self.rotating_frame_frequencyMHz:  # (t_late,y_late,f_late_res,ey_late) the last three must be transformed to rrf
                # (tfl,fl) non rebinned, the last must be transformed to rrf
                _, y_late, ey_late = rebin(self.suite.time,
                                           self.rrf_asymm, [start, stop],
                                           packlate,
                                           e=self.rrf_asyme)
                f_late_res = mixer(t_late, f_late_res,
                                   self.rotating_frame_frequencyMHz)
                fl = mixer(tfl, fl, self.rotating_frame_frequencyMHz)

            fit_late_start = int(
                stop / packlate *
                fit_pack)  # divides fit_range in early and late

            # redo the same with the original fit binning fit_pack, only for histo and chi2
            t_fit_late, y_fit_late, ey_fit_late = rebin(
                self.suite.time,
                asymm, [fit_late_start, fit_stop],
                fit_pack,
                e=asyme)
            # no rotating frame version for this!
            nu_fit_late, f_fit_late, chi_fit_late = chi(
                t_fit_late, y_fit_late, ey_fit_late, pars)
            if self.rotating_frame_frequencyMHz:  # (t_fit_late,y_fit_late,f_fit_late,ey_fit_late) the last three must be transformed to rrf
                _, y_fit_late, ey_fit_late = rebin(self.suite.time,
                                                   self.rrf_asymm,
                                                   [fit_start, fit_late_start],
                                                   fit_pack,
                                                   e=self.rrf_asyme)
                f_fit_late = mixer(t_fit_late, f_fit_late,
                                   self.rotating_frame_frequencyMHz)
            # print('mufitplot plot_run debug: y_fit_late ey_fit_late f_fit_late shape = {},  {},  {}'.format(y_fit_late.shape, ey_fit_late.shape, f_fit_late.shape))

            if not nu_fit_late:
                return False
            # function as in fit

            t_fit_early, y_fit_early, ey_fit_early = rebin(
                self.suite.time,
                asymm, [fit_start, fit_late_start],
                fit_pack,
                e=asyme)
            # single slices as in fit
            nu_fit_early, f_fit_early, chi_fit_early = chi(
                t_fit_early, y_fit_early, ey_fit_early, pars)
            if self.rotating_frame_frequencyMHz:
                _, y_fit_early, ey_fit_early = rebin(
                    self.suite.time,
                    self.rrf_asymm, [fit_start, fit_late_start],
                    fit_pack,
                    e=self.rrf_asyme)
                f_fit_early = mixer(t_fit_early, f_fit_early,
                                    self.rotating_frame_frequencyMHz)

            if not nu_fit_early:
                return False
            # function as in fit

        else:
            ###################
            # single range plot
            ###################
            early_late = False
            pack = 1
            chi_fit_late, nu_fit_late, chi_fit_early, nu_fit_early = None, None, None, None
            if len(plottup) == 3:  # plot start stop pack
                start, stop, pack = plottup
            elif len(plottup) == 2:  # plot start stop
                start, stop = plottup
#        self.suite.console('plot_range= {}'.format(plot_range))
#        self.suite.console('start, stop, pack = {},{},{}'.format(start, stop, pack))

        t, y, ey = rebin(self.suite.time, asymm, [start, stop], pack, e=asyme)
        # rebinned single or early slices
        # print('muplotfit plot_run debug: calling chi for f_res')
        nudum, f_res, _ = chi(t, None, None, pars)
        packfit = int(pack / 2)
        tf, dum = rebin(self.suite.time, asymm, [start, stop], packfit)
        # single or early time slice for plot function
        # print('muplotfit plot_run debug: calling chi for f (None)')
        _, f, _ = chi(tf, None, None, pars)
        # print('mufitplot plot_run debug: min max f_res {}, min max f {}'.format([f_res.min(),f_res.max()],[f.min(),f.max()]))
        if self.rotating_frame_frequencyMHz:  # (t,y,f_res,ey) the last three must be transformed to rrf
            # (tf,f) non rebinned, the last must be transformed to rrf
            _, y, ey = rebin(self.suite.time,
                             self.rrf_asymm, [start, stop],
                             pack,
                             e=self.rrf_asyme)
            f_res = mixer(t, f_res, self.rotating_frame_frequencyMHz)
            f = mixer(tf, f, self.rotating_frame_frequencyMHz)

        if not nudum:
            return False
        # print('nu = {}, chi_res = {}'.format(nu,chi_res))
        # t,fres calculated on plot points for residues

        # assume self.suite.single()
        # prepare figure
        fgroup = [
            self.suite.groups[k]['forward']
            for k in range(len(self.suite.groups))
        ]
        bgroup = [
            self.suite.groups[k]['backward']
            for k in range(len(self.suite.groups))
        ]
        alpha = [
            self.suite.groups[k]['alpha']
            for k in range(len(self.suite.groups))
        ]
        group = [fgroup, bgroup, alpha]
        dy_fit = (y_fit - f_fit) / ey_fit

        data = [t, y, ey, f_res, tf, f, dy_fit]

        chi_dof = [nu_fit, chi_fit]

        if self.suite.single() and not self.multigroup:
            set_figure_fit = set_single_fit
        else:
            set_figure_fit = set_sequence_fit
        # single draws a static plot, many runs are dealt by animation
        # invoke ploting of dat, fit and residues

        if early_late:
            dy_fit_early = (y_fit_early - f_fit_early) / ey_fit_early
            dy_fit_late = (y_fit_late - f_fit_late) / ey_fit_late
            w_early = nu_fit * ones(t_fit_early.shape[0]) / nu_fit_early
            w_late = nu_fit * ones(t_fit_late.shape[0]) / nu_fit_late
            data_late = [
                t_late, y_late, ey_late, f_late_res, tfl, fl, dy_fit_early,
                dy_fit_late
            ]
            chi_dof_late = [nu_fit_early,nu_fit_late,\
                            chi_fit_early,chi_fit_late,
                            w_early,w_late]
        else:
            data_late, chi_dof_late = None, None
        # self.suite.console('Debug-mufitplot: set_figure  = {}'.format(set_figure_fit))
        self.fig = set_figure_fit(self.fig,
                                  self.model_name(),
                                  early_late,
                                  data,
                                  group,
                                  run_title,
                                  chi_dof,
                                  data_late,
                                  chi_dof_late,
                                  rrf=self.rotating_frame_frequencyMHz)
        return True