Exemplo n.º 1
0
    def run(self,
            config='C:\\apps\\DaFy_P23\\config\\config_p23_pxrd_new.ini'):
        #extract global vars from config
        if config == None:
            #self.kwarg_global = extract_vars_from_config(self.conf_file, section_var ='Global')
            pass
        else:
            self.conf_file = config
            self.kwarg_global = extract_vars_from_config(self.conf_file,
                                                         section_var='Global')

        for each in self.kwarg_global:
            setattr(self, each, self.kwarg_global[each])
        if self.time_scan:
            self.plot_pxrd = plot_pxrd_profile_time_scan
        else:
            self.plot_pxrd = plot_pxrd_profile
        #set data to empty lib {}
        self.data = {}
        #pars lib for everything else
        self.kwarg_image = extract_vars_from_config(self.conf_file,
                                                    section_var='Image_Loader')
        self.kwarg_mask = extract_vars_from_config(self.conf_file,
                                                   section_var='Mask')
        try:
            self.kwarg_bkg = extract_vars_from_config(
                self.conf_file, section_var='Background_Subtraction')
        except:
            self.kwarg_bkg = {
                "ord_cus_s": 8,
                "ss": 5,
                "ss_factor": 0.08,
                "fct": "ah"
            }
        self.kwarg_peak_fit = extract_vars_from_config(self.conf_file,
                                                       section_var='Peak_Fit')

        #recal clip_boundary and cen(you need to remove the edges)
        self.ver_offset = self.clip_width['ver']
        self.hor_offset = self.clip_width['hor']
        self.clip_boundary = {
            "ver": [self.ver_offset, self.dim_detector[0] - self.ver_offset],
            "hor": [self.hor_offset, self.dim_detector[1] - self.hor_offset]
        }
        self.cen_clip = [
            self.cen[0] - self.ver_offset, self.cen[1] - self.hor_offset
        ]

        #init peak fit, bkg subtraction and reciprocal space and image loader instance
        self.img_loader = nexus_image_loader(clip_boundary=self.clip_boundary,
                                             kwarg=self.kwarg_image)

        self.create_mask_new = create_mask(kwarg=self.kwarg_mask)

        #build generator funcs
        self._scans = scan_generator(scans=self.scan_nos)
        self._images = image_generator_bkg(self._scans, self.img_loader,
                                           self.create_mask_new)
Exemplo n.º 2
0
 def update_parameter(self,
                      config_file,
                      sections=[
                          'Global', 'Image_Loader', 'Mask',
                          'Background_Subtraction'
                      ]):
     beamline = extract_vars_from_config(config_file,
                                         section_var='Global')['beamline']
     if beamline != self.data_type:
         return 'Data formate does not match! Your par frame take {} format, while your config file has a {} format. Fix it first!'.format(
             self.data_type, beamline)
     for section in sections:
         kwarg_temp = extract_vars_from_config(config_file,
                                               section_var=section)
         for each in kwarg_temp:
             try:
                 self.par[(section, each)] = str(kwarg_temp[each])
             except:
                 pass
Exemplo n.º 3
0
 def update_parameter(
         self,
         config_file,
         sections=['Global', 'Image_Loader', 'Mask', 'Peak_Fit']):
     for section in sections:
         kwarg_temp = extract_vars_from_config(config_file,
                                               section_var=section)
         for each in kwarg_temp:
             try:
                 self.par[(section, each)] = str(kwarg_temp[each])
             except:
                 pass
Exemplo n.º 4
0
 def update_parameter(self,
                      config_file,
                      sections=[
                          'Global', 'Data_Storage', 'Film_Lattice',
                          'Reciprocal_Mapping', 'Image_Loader', 'Mask',
                          'Peak_Fit', 'Visulization',
                          'Background_Subtraction'
                      ]):
     for section in sections:
         kwarg_temp = extract_vars_from_config(config_file,
                                               section_var=section)
         for each in kwarg_temp:
             try:
                 self.par[(section, each)] = str(kwarg_temp[each])
             except:
                 pass
Exemplo n.º 5
0
def run(conf_file_names=config_file):
    #make compatibility of py 2 and py 3#
    if (sys.version_info > (3, 0)):
        raw_input = input

    print("CV_XRD is running on rank{} now ...".format(rank))

    #config files
    conf_file = os.path.join(DaFy_path, 'config', conf_file_names['CV_XRD'])
    #conf_file_plot = os.path.join(DaFy_path, 'config', conf_file_names['ploter'])
    #conf_file_mpi = os.path.join(DaFy_path, 'config', conf_file_names['mpi'])

    #extract global vars from config
    kwarg_global = extract_vars_from_config(conf_file, section_var='Global')
    #pars lib for everything else
    kwarg_visulization = extract_vars_from_config(conf_file,
                                                  section_var='Visulization')
    kwarg_film = extract_vars_from_config(conf_file,
                                          section_var='Film_Lattice')
    kwarg_data = extract_vars_from_config(conf_file,
                                          section_var='Data_Storage')
    kwarg_peak_fit = extract_vars_from_config(conf_file,
                                              section_var='Peak_Fit')
    kwarg_rsp = extract_vars_from_config(conf_file,
                                         section_var='Reciprocal_Mapping')
    kwarg_image = extract_vars_from_config(conf_file,
                                           section_var='Image_Loader')
    kwarg_mask = extract_vars_from_config(conf_file, section_var='Mask')
    if run_scan != None:
        kwarg_global['scan_nos'] = [kwarg_global['scan_nos'][run_scan]]
        kwarg_global['live_image'] = False
        kwarg_global['phs'] = [kwarg_global['phs'][run_scan]]
        kwarg_data['ids_files'] = [kwarg_data['ids_files'][run_scan]]
    else:
        pass
    for each in kwarg_global:
        globals()[each] = kwarg_global[each]

    #recal clip_boundary and cen
    clip_boundary = {
        "ver": [cen[0] - clip_width['ver'], cen[0] + clip_width['ver'] + 1],
        "hor": [cen[1] - clip_width['hor'], cen[1] + clip_width['hor'] + 1]
    }
    cen_clip = [clip_width['ver'], clip_width['hor']]
    plt.ion()
    fig = plt.figure(figsize=(14, 10))

    #data file
    data = {}
    for key in data_keys:
        data[key] = []

    #init peak fit, bkg subtraction and reciprocal space and image loader instance
    peak_fitting_instance = XRD_Peak_Fitting(img=None,
                                             cen=cen_clip,
                                             kwarg=kwarg_peak_fit)
    bkg_sub_instance = background_subtraction_single_img(
        cen_clip, conf_file, sections=['Background_Subtraction'])
    rsp_instance = Reciprocal_Space_Mapping(img=None,
                                            cen=cen_clip,
                                            kwarg=kwarg_rsp)
    img_loader = nexus_image_loader(clip_boundary=clip_boundary,
                                    kwarg=kwarg_image)
    create_mask_new = create_mask(kwarg=kwarg_mask)
    lattice_skin = rsp.lattice.from_cif(os.path.join(DaFy_path, 'util','cif',"{}".format(kwarg_film['film_material_cif'])),
                                        HKL_normal=kwarg_film['film_hkl_normal'],\
                                        HKL_para_x=kwarg_film['film_hkl_x'],\
                                        E_keV=rsp_instance.e_kev, offset_angle=0)

    #build generator funcs
    _scans = scan_generator(scans=scan_nos)
    _images = image_generator(_scans, img_loader, rsp_instance,
                              peak_fitting_instance, create_mask_new)
    i = 0
    scan_number = img_loader.scan_number
    for img in _images:
        if img_loader.scan_number != scan_number:
            i = 0
            scan_number = img_loader.scan_number
        peak_fitting_instance.reset_fit(img, check=False)
        bkg_sub_instance.fit_background(None,
                                        img,
                                        plot_live=False,
                                        freeze_sf=True)
        data = merge_data(data, img_loader, peak_fitting_instance,
                          bkg_sub_instance, kwarg_global)
        data = cal_strain_and_grain(data,
                                    HKL=kwarg_film['film_hkl_bragg_peak'][0],
                                    lattice=lattice_skin)

        #make nice looking status bar
        finish_percent = (i + 1) / float(img_loader.total_frame_number)
        column_size = get_console_size()[0] - 22
        left_abnormal = int((img_loader.abnormal_range[0] + 1) /
                            float(img_loader.total_frame_number) *
                            column_size + 8)
        right_abnormal = int((img_loader.abnormal_range[1] + 1) /
                             float(img_loader.total_frame_number) *
                             column_size + 8)
        output_text = list('{}{}{}{}{}'.format(
            'BEGIN(0)', '=' * int(finish_percent * column_size), '==>',
            ' ' * int((1 - finish_percent) * column_size),
            '>|END(' + str(img_loader.total_frame_number) + ')'))
        for index_text in range(len(output_text)):
            if output_text[
                    index_text] != ' ' and index_text > left_abnormal and index_text < right_abnormal:
                output_text[index_text] = 'x'
            else:
                pass
        print(''.join(output_text), end="\r")
        time.sleep(0.003)

        i = i + 1
        if live_image:
            fig = show_all_plots_new(fig=fig,fit_engine_instance=peak_fitting_instance,\
                                    bkg_int=bkg_sub_instance,\
                                    processed_data_container=data,\
                                    title='Frame_{}, E ={:04.2f}V'.format(i,data['potential'][-1]),\
                                    kwarg =kwarg_visulization)
            fig.canvas.draw()
            fig.tight_layout()
            plt.pause(0.05)
            plt.show()
    df = pd.DataFrame(data)
    df.to_csv('temp_current_result.csv')
    df.to_excel('temp_current_result.xlsx', columns=kwarg_global['data_keys'])
    make_data_config_file(DaFy_path=DaFy_path,
                          data_folder=os.path.join(DaFy_path, 'data'),
                          data=df,
                          film_material_cif=kwarg_film['film_material_cif'],
                          hkls=kwarg_film['film_hkl_bragg_peak'],
                          pot_step=kwarg_peak_fit['pot_step_scan'],
                          beamline=name,
                          beamtime=beamtime,
                          kwarg=kwarg_data)
    if not live_image:
        fig = show_all_plots_new(fig=fig,fit_engine_instance=peak_fitting_instance,\
                                bkg_int=bkg_sub_instance,\
                                processed_data_container=data,\
                                title='Frame_{}, E ={:04.2f}V'.format(i,data['potential'][-1]),\
                                kwarg =kwarg_visulization)
        fig.canvas.draw()
        fig.tight_layout()
        plt.pause(500.05)
        plt.show()
        print('\nRun is finished!')
    return df
Exemplo n.º 6
0
    def run(self, config):
        self.conf_file = config
        #extract global vars from config
        self.kwarg_global = extract_vars_from_config(self.conf_file,
                                                     section_var='Global')
        for each in self.kwarg_global:
            setattr(self, each, self.kwarg_global[each])

        #pars lib for everything else
        self.kwarg_visulization = extract_vars_from_config(
            self.conf_file, section_var='Visulization')
        self.kwarg_film = extract_vars_from_config(self.conf_file,
                                                   section_var='Film_Lattice')
        self.kwarg_data = extract_vars_from_config(self.conf_file,
                                                   section_var='Data_Storage')
        self.kwarg_peak_fit = extract_vars_from_config(self.conf_file,
                                                       section_var='Peak_Fit')
        self.kwarg_rsp = extract_vars_from_config(
            self.conf_file, section_var='Reciprocal_Mapping')
        self.kwarg_image = extract_vars_from_config(self.conf_file,
                                                    section_var='Image_Loader')
        self.kwarg_mask = extract_vars_from_config(self.conf_file,
                                                   section_var='Mask')

        #recal clip_boundary and cen
        self.clip_boundary = {
            "ver": [
                self.cen[0] - self.clip_width['ver'],
                self.cen[0] + self.clip_width['ver'] + 1
            ],
            "hor": [
                self.cen[1] - self.clip_width['hor'],
                self.cen[1] + self.clip_width['hor'] + 1
            ]
        }
        self.cen_clip = [self.clip_width['ver'], self.clip_width['hor']]

        #data file
        for key in self.data_keys:
            self.data[key] = []

        #init peak fit, bkg subtraction and reciprocal space and image loader instance
        self.bkg_sub = background_subtraction_single_img(
            self.cen_clip, self.conf_file, sections=['Background_Subtraction'])
        self.peak_fitting_instance = XRD_Peak_Fitting(
            img=None, cen=self.cen_clip, kwarg=self.kwarg_peak_fit)
        self.rsp_instance = Reciprocal_Space_Mapping(img=None,
                                                     cen=self.cen_clip,
                                                     kwarg=self.kwarg_rsp)
        self.img_loader = nexus_image_loader(clip_boundary=self.clip_boundary,
                                             kwarg=self.kwarg_image)
        self.create_mask_new = create_mask(kwarg=self.kwarg_mask)
        self.lattice_skin = rsp.lattice.from_cif(os.path.join(DaFy_path, 'util','cif',"{}".format(self.kwarg_film['film_material_cif'])),
                                            HKL_normal=self.kwarg_film['film_hkl_normal'],\
                                            HKL_para_x=self.kwarg_film['film_hkl_x'],\
                                            E_keV=self.rsp_instance.e_kev, offset_angle=0)

        #build generator funcs
        self._scans = scan_generator(scans=self.scan_nos)
        self._images = image_generator(self._scans, self.img_loader,
                                       self.rsp_instance,
                                       self.peak_fitting_instance,
                                       self.create_mask_new)
Exemplo n.º 7
0
    def run(self, config=None):
        #extract global vars from config
        if config == None:
            pass
        else:
            self.conf_file = config
            self.kwarg_global = extract_vars_from_config(self.conf_file,
                                                         section_var='Global')
        for each in self.kwarg_global:
            #flatten [[1,5],15] to [1,2,3,4,5,15] if necessarily
            if each == 'scan_nos':
                temp_scans = []
                for each_item in self.kwarg_global['scan_nos']:
                    if type(each_item) == list:
                        temp_scans = temp_scans + list(
                            range(each_item[0], each_item[1] + 1))
                    else:
                        temp_scans.append(each_item)
                setattr(self, 'scan_nos', temp_scans)
            else:
                setattr(self, each, self.kwarg_global[each])

        #pars lib for everything else
        self.kwarg_image = extract_vars_from_config(self.conf_file,
                                                    section_var='Image_Loader')
        self.kwarg_mask = extract_vars_from_config(self.conf_file,
                                                   section_var='Mask')

        #recal clip_boundary and cen
        self.clip_boundary = {
            "ver": [
                self.cen[0] - self.clip_width['ver'],
                self.cen[0] + self.clip_width['ver'] + 1
            ],
            "hor": [
                self.cen[1] - self.clip_width['hor'],
                self.cen[1] + self.clip_width['hor'] + 1
            ]
        }
        self.cen_clip = [self.clip_width['ver'], self.clip_width['hor']]

        self.img = None
        #data file
        self.data = {}
        if 'noise' not in self.data_keys:
            self.data_keys.append('noise')
        for key in self.data_keys:
            self.data[key] = []
        # print(data)
        #init peak fit, bkg subtraction and reciprocal space and image loader instance
        self.bkg_sub = background_subtraction_single_img(
            self.cen_clip, self.conf_file, sections=['Background_Subtraction'])
        if self.beamline == 'PETRA3_P23':
            self.img_loader = nexus_image_loader(
                clip_boundary=self.clip_boundary, kwarg=self.kwarg_image)
        elif self.beamline == 'APS_13IDC':
            self.img_loader = gsecars_image_loader(
                clip_boundary=self.clip_boundary,
                kwarg=self.kwarg_image,
                scan_numbers=self.scan_nos)
        self.create_mask_new = create_mask(kwarg=self.kwarg_mask)
        self.setup_frames()