def _calibration(img, calibration, calib_ref_fp=None, **kwargs): """engine for performing calibration on a image with geometry correction software. current backend is ``pyFAI``. Parameters ---------- img : ndarray image will be used for calibration process. calibration : pyFAI.calibration.Calibration instance Calibration instance with wavelength, calibrant and detector configured. calib_ref_fp : str full file path to where the native pyFAI calibration information will be saved. kwargs: additional keyword argument for calibration. please refer to pyFAI documentation for all options. """ print('{:=^20}'.format("INFO: you are able to perform calibration, " "please refer to pictorial guide here:\n")) print('{:^20}'.format( "http://xpdacq.github.io/usb_Running.html#calib-manual\n")) # default params interactive = True # calibration c = calibration # shorthand notation timestr = _timestampstr(time.time()) c.gui = interactive # annoying pyFAI logic, you need valid fn to start calibration _is_tmp_dir = False if calib_ref_fp is None: _is_tmp_dir = True td = TemporaryDirectory() calib_ref_fp = os.path.join(td.name, 'from_calib_func') basename, ext = os.path.splitext(calib_ref_fp) poni_fn = basename + ".npt" c.basename = basename c.pointfile = poni_fn c.peakPicker = PeakPicker(img, reconst=True, pointfile=c.pointfile, calibrant=c.calibrant, wavelength=c.wavelength, **kwargs) c.peakPicker.gui(log=True, maximize=True, pick=True) update_fig(c.peakPicker.fig) c.gui_peakPicker() # TODO: open issue at pyFAI on this crazyness c.ai.setPyFAI(**c.geoRef.getPyFAI()) c.ai.wavelength = c.geoRef.wavelength if _is_tmp_dir: td.cleanup() return c, timestr
def templater2_func(doc, template, aux=None, short_aux=None): """format with auxiliary and time""" if aux is None: aux = ['temperature', 'diff_x', 'diff_y', 'eurotherm'] if short_aux is None: short_aux = ['temp', 'x', 'y', 'euro'] aux_res = ['{}={}'.format(b, doc['data'].get(a, '')) for a, b in zip(aux, short_aux)] aux_res_str = '_'.join(aux_res) # Add a separator between timestamp and extras if aux_res_str: aux_res_str = '_' + aux_res_str return template.format( # Change to include name as well auxiliary=aux_res_str, human_timestamp=_timestampstr(doc['time']))
def _calibration(img, calibration, calib_ref_fp, **kwargs): """engine for performing calibration on a image with geometry correction software. current backend is ``pyFAI``. Parameters ---------- img : ndarray image will be used for calibration process. calibration : pyFAI.calibration.Calibration instance Calibration instance with wavelength, calibrant and detector configured. calib_ref_fp : str full file path to where the native pyFAI calibration information will be saved. kwargs: additional keyword argument for calibration. please refer to pyFAI documentation for all options. """ print('{:=^20}'.format("INFO: you are able to perform calibration, " "please refer to pictorial guide here:\n")) print('{:^20}' .format("http://xpdacq.github.io/usb_Running.html#calib-manual\n")) # default params interactive = True # calibration c = calibration # shorthand notation timestr = _timestampstr(time.time()) c.gui = interactive # annoying pyFAI logic, you need valid fn to start calibration # TODO: send to tmp folder then delete tmp folder if calib_ref_fp is None: calib_ref_fp = os.path.join(os.getcwd(), 'from_calib_func') basename, ext = os.path.splitext(calib_ref_fp) poni_fn = basename + ".npt" c.basename = basename c.pointfile = poni_fn c.peakPicker = PeakPicker(img, reconst=True, pointfile=c.pointfile, calibrant=c.calibrant, wavelength=c.wavelength, **kwargs) c.peakPicker.gui(log=True, maximize=True, pick=True) update_fig(c.peakPicker.fig) c.gui_peakPicker() return c, timestr
def templater2_func(doc, template, aux=None, short_aux=None): """format with auxiliary and time""" if aux is None: aux = ['temperature', 'diff_x', 'diff_y', 'eurotherm'] if short_aux is None: short_aux = ['temp', 'x', 'y', 'euro'] aux_res = [ '{}={}'.format(b, doc['data'].get(a, '')) for a, b in zip(aux, short_aux) ] aux_res_str = '_'.join(aux_res) # Add a separator between timestamp and extras if aux_res_str: aux_res_str = '_' + aux_res_str return template.format( # Change to include name as well auxiliary=aux_res_str, human_timestamp=_timestampstr(doc['time']))
def dark_template_func(timestamp, template): """Format template for dark images Parameters ---------- timestamp: float The time in unix epoch template: str The string to be formatted Returns ------- str: """ d = {'human_timestamp': _timestampstr(timestamp), 'ext': '.tiff'} t = template.format(**d) os.makedirs(os.path.split(t)[0]) return t
def event(self, doc): if self.dark_img is not None: doc = next(self.db.fill_events([doc], self.descs)) if "cryostat_T" in doc["data"]: doc["data"]["temperature"] = doc["data"].pop("cryostat_T") # human readable timestamp h_timestamp = _timestampstr(doc["time"]) # dark subtraction img = doc["data"][self.image_data_key] if str(img.dtype) == "uint16": img = img.astype("float32") if self.dark_img is not None: img -= self.dark_img if self.vis: self.vis_callbacks["dark_sub_iq"]("event", format_event(img=img)) if self.write_to_disk: tiff_name = render_clean_makedir( self.light_template, human_timestamp=h_timestamp, raw_event=doc, raw_start=self.start_doc, raw_descriptor=self.descriptor_doc, analysis_stage="dark_sub", ext=".tiff", ) tifffile.imsave(tiff_name, img) if self.analysis_setting == "full": # background correction if self.background_img is not None: img -= self.background_img # get calibration if self.is_calibration: calibration, geo = img_calibration(img, self.wavelength, self.calibrant, self.detector) _save_calib_param( calibration, h_timestamp, os.path.join(self.calibration_md_folder, "xpdAcq_calib_info.yml"), ) if self.write_to_disk: poni_name = render_clean_makedir( self.light_template, human_timestamp=h_timestamp, raw_event=doc, raw_start=self.start_doc, raw_descriptor=self.descriptor_doc, analysis_stage="calib", ext=".poni", ) poni_saver(poni_name, calibration) elif self.calibrant: geo = load_geo(self.calibrant) else: geo = None if geo: img = polarization_correction(img, geo, self.polarization_factor) # Masking if doc["seq_num"] == 1: if (self.start_doc["sample_name"] == "Setup" or self.mask_setting is None): self.mask = np.ones(img.shape, dtype=bool) else: binner = generate_binner(geo, img.shape, self.mask) self.mask = mask_img(img, binner, **self.mask_kwargs) if self.write_to_disk: mask_name = render_clean_makedir( self.light_template, human_timestamp=h_timestamp, raw_event=doc, raw_start=self.start_doc, raw_descriptor=self.descriptor_doc, analysis_stage="mask", ext="", ) fit2d_save(self.mask, mask_name) if self.vis: overlay = overlay_mask(img, self.mask) self.vis_callbacks["masked_img"]( "event", format_event(overlay_mask=overlay)) # binner binner = generate_binner(geo, img.shape, self.mask) q, iq = ( binner.bin_centers, np.nan_to_num(binner(img.flatten())), ) if self.vis: self.vis_callbacks["iq"]("event", format_event(q=q, iq=iq)) self.vis_callbacks["zscore"]( "event", format_event(img=z_score_image(img, binner)), ) if self.write_to_disk: iq_name = render_clean_makedir( self.light_template, human_timestamp=h_timestamp, raw_event=doc, raw_start=self.start_doc, raw_descriptor=self.descriptor_doc, analysis_stage="iq_q", ext="_Q.chi", ) save_output(q, iq, iq_name, "Q") tth = np.rad2deg(q_to_twotheta(q, self.wavelength)) if self.vis: self.vis_callbacks["itth"]("event", format_event(tth=tth, iq=iq)) if self.write_to_disk: itth_name = render_clean_makedir( self.light_template, human_timestamp=h_timestamp, raw_event=doc, raw_start=self.start_doc, raw_descriptor=self.descriptor_doc, analysis_stage="iq_tth", ext="_tth.chi", ) save_output(tth, iq, itth_name, "2theta") if self.composition: fq_q, fq, fq_config = fq_getter( q, iq, composition=self.composition, **self.fq_kwargs) if self.vis: self.vis_callbacks["fq"]("event", format_event(q=fq_q, fq=fq)) r, gr, pdf_config = pdf_getter( q, iq, composition=self.composition, **self.pdf_kwargs) if self.vis: self.vis_callbacks["pdf"]("event", format_event(r=r, pdf=gr)) if self.write_to_disk: pdf_name = render_clean_makedir( self.light_template, human_timestamp=h_timestamp, raw_event=doc, raw_start=self.start_doc, raw_descriptor=self.descriptor_doc, analysis_stage="pdf", ext=".gr", ) pdf_saver(r, gr, pdf_config, pdf_name)
def event(self, doc): if self.dark_img is not None: doc = next(self.db.fill_events([doc], self.descs)) if 'cryostat_T' in doc['data']: doc['data']['temperature'] = doc['data'].pop('cryostat_T') # human readable timestamp h_timestamp = _timestampstr(doc['time']) # dark subtraction img = doc['data'][self.image_data_key] if str(img.dtype) == 'uint16': img = img.astype('float32') if self.dark_img is not None: img -= self.dark_img if self.vis: self.vis_callbacks['dark_sub_iq']('event', format_event(img=img)) if self.write_to_disk: tiff_name = render_clean_makedir( self.light_template, human_timestamp=h_timestamp, raw_event=doc, raw_start=self.start_doc, raw_descriptor=self.descriptor_doc, analysis_stage='dark_sub', ext='.tiff') tifffile.imsave(tiff_name, img) if self.analysis_setting == 'full': # background correction if self.background_img is not None: img -= self.background_img # get calibration if self.is_calibration: calibration, geo = img_calibration(img, self.wavelength, self.calibrant, self.detector) _save_calib_param( calibration, h_timestamp, os.path.join(self.calibration_md_folder, 'xpdAcq_calib_info.yml')) if self.write_to_disk: poni_name = render_clean_makedir( self.light_template, human_timestamp=h_timestamp, raw_event=doc, raw_start=self.start_doc, raw_descriptor=self.descriptor_doc, analysis_stage='calib', ext='.poni') poni_saver(poni_name, calibration) elif self.calibrant: geo = load_geo(self.calibrant) else: geo = None if geo: img = polarization_correction(img, geo, self.polarization_factor) # Masking if doc['seq_num'] == 1: if (self.start_doc['sample_name'] == 'Setup' or self.mask_setting is None): self.mask = np.ones(img.shape, dtype=bool) else: binner = generate_binner(geo, img.shape, self.mask) self.mask = mask_img(img, binner, **self.mask_kwargs) if self.write_to_disk: mask_name = render_clean_makedir( self.light_template, human_timestamp=h_timestamp, raw_event=doc, raw_start=self.start_doc, raw_descriptor=self.descriptor_doc, analysis_stage='mask', ext='') fit2d_save(self.mask, mask_name) if self.vis: overlay = overlay_mask(img, self.mask) self.vis_callbacks['masked_img']( 'event', format_event(overlay_mask=overlay)) # binner binner = generate_binner(geo, img.shape, self.mask) q, iq = binner.bin_centers, np.nan_to_num( binner(img.flatten())) if self.vis: self.vis_callbacks['iq']('event', format_event(q=q, iq=iq)) self.vis_callbacks['zscore']( 'event', format_event(img=z_score_image(img, binner))) if self.write_to_disk: iq_name = render_clean_makedir( self.light_template, human_timestamp=h_timestamp, raw_event=doc, raw_start=self.start_doc, raw_descriptor=self.descriptor_doc, analysis_stage='iq_q', ext='_Q.chi') save_output(q, iq, iq_name, 'Q') tth = np.rad2deg(q_to_twotheta(q, self.wavelength)) if self.vis: self.vis_callbacks['itth']('event', format_event(tth=tth, iq=iq)) if self.write_to_disk: itth_name = render_clean_makedir( self.light_template, human_timestamp=h_timestamp, raw_event=doc, raw_start=self.start_doc, raw_descriptor=self.descriptor_doc, analysis_stage='iq_tth', ext='_tth.chi') save_output(tth, iq, itth_name, '2theta') if self.composition: fq_q, fq, fq_config = fq_getter( q, iq, composition=self.composition, **self.fq_kwargs) if self.vis: self.vis_callbacks['fq']('event', format_event(q=fq_q, fq=fq)) r, gr, pdf_config = pdf_getter( q, iq, composition=self.composition, **self.pdf_kwargs) if self.vis: self.vis_callbacks['pdf']('event', format_event(r=r, pdf=gr)) if self.write_to_disk: pdf_name = render_clean_makedir( self.light_template, human_timestamp=h_timestamp, raw_event=doc, raw_start=self.start_doc, raw_descriptor=self.descriptor_doc, analysis_stage='pdf', ext='.gr') pdf_saver(r, gr, pdf_config, pdf_name)