def _integrate( poni_file: str, img_file: str, bg_img_file: str = None, mask_file: str = None, output_dir: str = ".", bg_scale: float = None, mask_setting: tp.Union[dict, str] = None, integ_setting: dict = None, plot_setting: tp.Union[dict, str] = None, img_setting: tp.Union[dict, str] = None, test: bool = False ) -> str: """Sub-function for integrate.""" if integ_setting is None: integ_setting = dict() ai = io.load_ai_from_poni_file(poni_file) bg_img = io.load_img(bg_img_file) if bg_img_file else None mask = np.load(mask_file) if mask_file else None img = io.load_img(img_file) chi_name = Path(img_file).with_suffix('.chi').name chi_path = Path(output_dir).joinpath(chi_name) integ_setting.update({'filename': str(chi_path)}) integ.get_chi( ai, img, bg_img=bg_img, mask=mask, bg_scale=bg_scale, mask_setting=mask_setting, integ_setting=integ_setting, plot_setting=plot_setting, img_setting=img_setting, ) if not test: plt.show() return chi_path
def test_average(test_data, kwargs): with TemporaryDirectory() as tempdir: img_file = Path(tempdir).joinpath('new_dir/average.tiff') cli.average(img_file, test_data['white_img_file'], test_data['white_img_file'], **kwargs) avg_img = io.load_img(img_file) white_img = io.load_img(test_data['white_img_file']) assert np.array_equal(avg_img, white_img)
def average(out_file: str, *img_files, weights: tp.List[float] = None) -> None: """Average the single channel image files with weights. Parameters ---------- out_file : str The output file path. It will be the type as the first image in img_files. img_files : a tuple of str The image files to be averaged. weights : an iterable of floats The weights for the images. If None, images will not be weighted when averaged. """ img_files: tp.Tuple[str] imgs = (io.load_img(_) for _ in img_files) avg_img = integ.avg_imgs(imgs, weights=weights) io.write_img(out_file, avg_img, img_files[0]) return
def average(out_file: str, *img_files, weights: tp.List[float] = None) -> None: """Average the single channel image files with weights. Parameters ---------- out_file : str The output file path. It will be the type as the first image in img_files. img_files : a tuple of str The image files to be averaged. weights : an iterable of floats The weights for the images. If None, images will not be weighted when averaged. """ img_files: tp.Tuple[str] imgs = (io.load_img(_) for _ in img_files) avg_img = integ.avg_imgs(imgs, weights=weights) # make the directory if not exists out_file_path = Path(out_file) if not out_file_path.parent.is_dir(): out_file_path.parent.mkdir(parents=True) # write out the file io.write_tiff(out_file, avg_img)
NI_IMG = resource_filename('tests', 'test_data/Ni_img_file.tiff') NI_CIF = resource_filename('tests', 'test_data/Ni_cif_file.cif') KAPTON_IMG = resource_filename('tests', 'test_data/Kapton_img_file.tiff') BLACK_IMG = resource_filename('tests', 'test_data/black_img.tiff') WHITE_IMG = resource_filename('tests', 'test_data/white_img.tiff') NI_CONFIG = PDFConfig() NI_CONFIG.readConfig(NI_GR) NI_PDFGETTER = PDFGetter(NI_CONFIG) ZRP_CIF = resource_filename('tests', 'test_data/ZrP.cif') NI_CRYSTAL = loadCrystal(NI_CIF) ZRP_CRYSTAL = loadCrystal(ZRP_CIF) NI_DIFFPY = loadStructure(NI_CIF) DB = { 'Ni_img_file': NI_IMG, 'Ni_img': load_img(NI_IMG), 'Kapton_img_file': KAPTON_IMG, 'Kapton_img': load_img(KAPTON_IMG), 'Ni_poni_file': NI_PONI, 'Ni_gr_file': NI_GR, 'Ni_chi_file': NI_CHI, 'Ni_fgr_file': NI_FGR, 'ai': pyFAI.load(NI_PONI), 'Ni_gr': load_data(NI_GR).T, 'Ni_chi': load_data(NI_CHI).T, 'Ni_fgr': load_data(NI_FGR).T, 'black_img_file': BLACK_IMG, 'white_img_file': WHITE_IMG, 'black_img': numpy.zeros((128, 128)), 'white_img': numpy.ones((128, 128)), 'Ni_config': NI_CONFIG,
from pdfstream.callbacks.composer import gen_stream from pdfstream.io import load_img, load_array # do not show any figures in test otherwise they will block the tests plt.ioff() NI_PONI_FILE = resource_filename('tests', 'test_data/Ni_poni_file.poni') NI_GR_FILE = resource_filename('tests', 'test_data/Ni_gr_file.gr') NI_CHI_FILE = resource_filename('tests', 'test_data/Ni_chi_file.chi') NI_FGR_FILE = resource_filename('tests', 'test_data/Ni_fgr_file.fgr') NI_IMG_FILE = resource_filename('tests', 'test_data/Ni_img_file.tiff') MASK_FILE = resource_filename("tests", "test_data/mask_file.npy") KAPTON_IMG_FILE = resource_filename('tests', 'test_data/Kapton_img_file.tiff') BLACK_IMG_FILE = resource_filename('tests', 'test_data/black_img.tiff') WHITE_IMG_FILE = resource_filename('tests', 'test_data/white_img.tiff') NI_IMG = load_img(NI_IMG_FILE) NI_FRAMES = numpy.expand_dims(NI_IMG, 0) KAPTON_IMG = load_img(KAPTON_IMG_FILE) NI_GR = load_array(NI_GR_FILE) NI_CHI = load_array(NI_CHI_FILE) NI_FGR = load_array(NI_FGR_FILE) NI_CONFIG = PDFConfig() NI_CONFIG.readConfig(NI_GR_FILE) NI_PDFGETTER = PDFGetter(NI_CONFIG) AI = pyFAI.load(NI_PONI_FILE) MASK = numpy.load(MASK_FILE) BLACK_IMG = load_img(BLACK_IMG_FILE) WHITE_IMG = load_img(WHITE_IMG_FILE) START_DOC_FILE = resource_filename('tests', 'test_data/start.json') with Path(START_DOC_FILE).open("r") as f: START_DOC = json.load(f)
def instrucalib(poni_file: str, img_file: str, cfg_file: str = None, stru_file: str = None, output_dir=".", fit_range: tp.Tuple[float, float, float] = (2.0, 60.0, 0.01), qdamp0: float = 0.04, qbroad0: float = 0.02, bg_img_file: str = None, bg_scale: float = None, mask_setting: tp.Union[dict, str] = None, integ_setting: dict = None, chi_plot_setting: tp.Union[dict, str] = None, img_setting: tp.Union[dict, str] = None, pdf_plot_setting: tp.Union[dict, str] = None, ncpu: int = None, show: bool = False): """Calibrate the 'qdamp' and 'qbroad' factor of the instrument in a pipeline process. A pipeline to do image background subtraction, auto masking, integration, PDF transformation and PDF modeling to calibrate the qdamp and qbroad. Also, the accuracy of the calibration is tested by the modeling. The output will be the processed data in 'iq', 'sq', 'fq', 'gr' files (depends on 'cfg' file), the fitting results in 'res' file, the refined structure in 'cif' file, the best fits data in 'fgr' file. Parameters ---------- poni_file : str The path to the poni file. It will be read by pyFAI. img_file : str The path to the image file. It will be read by fabio. cfg_file : str The path to the PDF configuratino file. Usually, a 'cfg' file or a processed data file like 'gr' file with meta data in the header. If None, use the 'Ni_gr_file.gr' in 'pdfstream/test_data'. stru_file : str The path to the structure file. Usually, a 'cif' file. If None, use the 'Ni_cif_file.cif' in 'pdfstream/test_data'. output_dir : str The directory to save all the outputs. Default current working directory. fit_range : tuple The rmin, rmax and rstep in the unit of angstrom. qdamp0 : float The initial value for the Q damping factor. qbroad0 : float The initial vluae for the Q broadening factor. bg_img_file : str The path to the background image file. It should have the same dimension as the data image. If None, no background subtraction will be done. bg_scale : float The scale for background subtraction. If None, use 1. mask_setting : dict or 'OFF' The settings for the auto-masking. See the arguments for mask_img ( https://xpdacq.github.io/xpdtools/xpdtools.html?highlight=mask_img#xpdtools.tools.mask_img). To turn off the auto masking, enter "OFF". integ_setting : dict The settings for the integration. See the arguments for integrate1d ( https://pyfai.readthedocs.io/en/latest/api/pyFAI.html#module-pyFAI.azimuthalIntegrator). img_setting : dict or 'OFF' The keywords for the matplotlib.pyplot.imshow ( https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.imshow.html). Besides, there is a key 'z_score', which determines the range of the colormap. The range is mean +/- z_score * std in the statistics of the image. To turn of the image, enter "OFF". chi_plot_setting : dict or 'OFF' The kwargs of chi data plotting. See matplotlib.pyplot.plot( https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.plot.html). If 'OFF', skip visualization. pdf_plot_setting : dict or 'OFF' The kwargs of pdf data plotting. See matplotlib.pyplot.plot( https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.plot.html). If 'OFF', skip visualization. ncpu : int The number of cpu used in parallel computing in the modeling. If None, no parallel computing. show : bool If True, show figures. """ try: import pdfstream.transformation.io as tio except ImportError as e: print(str(e)) sys.exit(1) if cfg_file is None: cfg_file = files.NI_CFG_FILE if stru_file is None: stru_file = files.NI_CIF_FILE ai = pio.load_ai_from_poni_file(poni_file) img = pio.load_img(img_file) pdfconfig = tio.load_pdfconfig(cfg_file) stru = io.load_crystal(stru_file) bg_img = pio.load_img(bg_img_file) if bg_img_file is not None else None pdfgetter, recipe = calib.calib_pipe(ai, img, pdfconfig, stru, fit_range=fit_range, qdamp0=qdamp0, qbroad0=qbroad0, bg_img=bg_img, bg_scale=bg_scale, mask_setting=mask_setting, integ_setting=integ_setting, chi_plot_setting=chi_plot_setting, img_setting=img_setting, pdf_plot_setting=pdf_plot_setting, ncpu=ncpu) img_path = PurePath(img_file) tio.write_pdfgetter(output_dir, img_path.name, pdfgetter) md.save(recipe, base_name=img_path.name, folder=output_dir) if show: plt.show() return
def integrate(poni_file: str, *img_files: str, bg_img_file: str = None, output_dir: str = ".", bg_scale: float = None, mask_setting: tp.Union[dict, str] = None, integ_setting: dict = None, plot_setting: tp.Union[dict, str] = None, img_setting: tp.Union[dict, str] = None) -> tp.List[str]: """Conduct azimuthal integration on the two dimensional diffraction images. The image will be first subtracted by background if background image file is given. Then, it will be binned in azimuthal direction according to the geometry provided by the poni file. The pixels far away from the average in each bin will be masked. The mask will be applied on the background subtracted image and the image will be integrated again by the pyFAI. The polarization correction and pixel-splitting algorithm will be applied according to user settings before the integration. The results are saved as chi files. Parameters ---------- poni_file : str The path to the poni file. It will be read by pyFAI. img_files : str The arbitrary number of paths to the image file. It will be read by fabio. bg_img_file : str The path to the background image file. It should have the same dimension as the data image. If None, no background subtraction will be done. output_dir : str The directory to save the chi data file. Default current working directory. bg_scale : float The scale of the background. Default 1 mask_setting : dict or 'OFF' The settings for the auto-masking. See the arguments for mask_img ( https://xpdacq.github.io/xpdtools/xpdtools.html?highlight=mask_img#xpdtools.tools.mask_img). To turn off the auto masking, enter "OFF". integ_setting : dict The settings for the integration. See the arguments for integrate1d ( https://pyfai.readthedocs.io/en/latest/api/pyFAI.html#module-pyFAI.azimuthalIntegrator). plot_setting : dict or 'OFF' The keywords for the matplotlib.pyplot.plot ( https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.plot.html). To turn off the plotting, enter "OFF". img_setting : dict or 'OFF' The keywords for the matplotlib.pyplot.imshow ( https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.imshow.html). Besides, there is a key 'z_score', which determines the range of the colormap. The range is mean +/- z_score * std in the statistics of the image. To turn of the image, enter "OFF". Returns ------- chi_files : a list of strings The path to the output chi file. """ if integ_setting is None: integ_setting = dict() ai = io.load_ai_from_poni_file(poni_file) bg_img = io.load_img(bg_img_file) if bg_img_file else None chi_paths = [] for img_file in img_files: img = io.load_img(img_file) chi_name = Path(img_file).with_suffix('.chi').name chi_path = Path(output_dir).joinpath(chi_name) integ_setting.update({'filename': str(chi_path)}) integ.get_chi(ai, img, bg_img, bg_scale=bg_scale, mask_setting=mask_setting, integ_setting=integ_setting, plot_setting=plot_setting, img_setting=img_setting) chi_paths.append(str(chi_path)) return chi_paths