def test_overlay_mask(): img = np.ones(100) mask = np.random.randint(0, 2, 100, dtype=bool) img2 = overlay_mask(img, mask) img[~mask] = np.nan assert img2 is not img assert_equal(img2, img)
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)