def run_calibration_gui( tiff_file: Path, poni_file: Path, *, wavelength: float = "", calibrant: str = "", detector: str = "", test: bool = False ) -> int: """Run the gui of calibration.""" args = ["pyFAI-calib2", "--poni", str(poni_file)] if wavelength: args.extend(["--wavelength", wavelength]) if calibrant: args.extend(["--calibrant", calibrant]) if detector: args.extend(["--detector", detector]) args.append(str(tiff_file)) if test: return 0 cp = subprocess.run(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if cp.returncode != 0: io.server_message("Error in Calibration. See below:") print(r"$", " ".join(args)) print(cp.stdout.decode()) print(cp.stderr.decode()) return cp.returncode
def query_ai( start: typing.Dict[str, typing.Any], calibration_md_key: str, ) -> typing.Union[None, typing.Dict[str, typing.Any]]: """Query the azimuthal integrator from the start document. If the poni_file is provided, use the poni file instead and ignore the information in start document. Parameters ---------- start : The start document. calibration_md_key : The key chain to find the calibration metadata. Returns ------- ai : The azimuthal integrator. """ if calibration_md_key not in start: io.server_message( "Missing key {} in start document.".format(calibration_md_key)) return None return io.load_ai_from_calib_result(start[calibration_md_key])
def start(self): try: server_message( "Server is started. " + "Listen to {}:{} prefix {}.".format( self._config.host, self._config.port, self._config.prefix)) super(BaseServer, self).start() except KeyboardInterrupt: server_message("Server is terminated.")
def event(self, doc, _md=None): io.server_message("Start processing the event {}".format( doc["seq_num"])) data = self.process_data(doc) io.server_message("Finish processing the event {}".format( doc["seq_num"])) return self.process_event(dict(data=data, descriptor=doc["descriptor"]))
def start(self, start_doc): io.server_message( "Copy and inject 'readable_time' in the start of {}".format( start_doc["uid"])) start_doc = copy.deepcopy(start_doc) start_doc["readable_time"] = datetime.datetime.fromtimestamp( start_doc["time"]).strftime("%Y-%m-%d_%H:%M:%S") return super(Exporter, self).start(start_doc)
def tiff_base(self): """Settings for the base folder.""" dir_path = self.get("SUITCASE", "tiff_base") if not dir_path: dir_path = "~/pdfstream_data" io.server_message( "Missing tiff_base in configuration. Use '{}'".format( dir_path)) path = Path(dir_path).expanduser() return path
def descriptor(self, doc): io.server_message("Read the stream {}".format(doc["name"])) self.image_key = from_desc.find_one_image(doc) try: self.dark_image = from_start.query_dk_img( self.start_doc, db=self.db, dk_id_key=self.config.dk_id_key, det_name=self.image_key) except ValueNotFoundError as error: self.dark_image = None io.server_message("Failed to find the dark: " + str(error)) return super(AnalysisStream, self).descriptor(doc)
def descriptor(self, doc): super(Calibration, self).descriptor(doc) self.cache["det_name"] = fd.find_one_image(doc) try: self.cache["dk_img"] = fs.query_dk_img( self.cache["start"], db=self.db, dk_id_key=self.config.dk_id_key, det_name=self.cache["det_name"] ) except ValueNotFoundError as error: self.cache["dk_img"] = None io.server_message("Failed to find dark: " + str(error))
def an_db(self) -> tp.Union[None, Broker]: name = self.get("DATABASE", "an_db", fallback=None) if no_need_to_refresh_db(self._an_db, name): pass elif name is None: self._an_db = None elif name == "temp": io.server_message( "Warning: a temporary db is created for an db. It will be destroy at the end of the session." ) self._an_db = databroker.v2.temp() else: self._an_db = databroker.catalog[name] return self._an_db
def start(self, doc, _md=None): io.server_message("Read the start of {}".format(doc["uid"])) self.clear_cache() # copy the default config and read the user config self.config = copy.deepcopy(self.init_config) self.config.read_user_config(doc.get("user_config", {})) # record start doc for later use self.start_doc = doc # find ai try: self.ai = from_start.query_ai( doc, calibration_md_key=self.config.calibration_md_key) except ValueNotFoundError as error: self.ai = None io.server_message("Failed to find calibration data: " + str(error)) # find bt info try: self.bt_info = from_start.query_bt_info( doc, composition_key=self.config.composition_key, wavelength_key=self.config.wavelength_key, default_composition="Ni") except ValueNotFoundError as error: self.bt_info = {} io.server_message("Info is missing: " + str(error)) # create new start new_start = dict(**doc, an_config=self.config.to_dict(), pdfstream_version=pdfstream.__version__) return super(AnalysisStream, self).start(new_start)
def query_bt_info(start: typing.Dict[str, typing.Any], composition_key: str, wavelength_key: str, default_composition: str = "Ni") -> dict: """Query the necessary information for the PDFGetter.""" if composition_key in start: composition = start[composition_key] if isinstance(composition, dict): composition_str = "".join( ["{}{}".format(k, v) for k, v in composition.items()]) elif isinstance(composition, str): composition_str = composition else: io.server_message( "Cannot parse composition '{}'. Use default '{}'".format( composition, default_composition)) composition_str = default_composition else: io.server_message("'{}' is not in start.".format(composition_key)) composition_str = default_composition if wavelength_key in start: wavelength = float(start[wavelength_key]) else: io.server_message("'{}' is not in start.".format(wavelength_key)) wavelength = None return {"composition": composition_str, "wavelength": wavelength}
def __call__(self, name: str, doc: dict) -> tp.Tuple[list, list]: if name == "start": if doc.get(self.config.dark_identifier): # dark frame run io.server_message("Ignore dark frame.") return [], [] elif doc.get(self.config.calib_identifier): # calibration run io.server_message("Start calibration.") return self.calibration, [] else: # light frame run io.server_message("Start data reduction.") return self.analysis, [] return [], []
def stop(self, doc): server_message("Receive the stop of run {}".format( doc.get("run_start", ""))) return super(StartStopCallback, self).stop(doc)
def test_server_message(): mod.server_message("test 1") mod.quiet() mod.server_message("test 2") mod.verbose() mod.server_message("test 3")
def stop(self, doc): io.server_message("Read the stop of {}".format(doc["run_start"])) return super(Exporter, self).stop(doc)
def event(self, doc): io.server_message("Export the event {}".format(doc["seq_num"])) return super(Exporter, self).event(doc)
def start(self, doc): server_message("Receive the start of run {}".format(doc["uid"])) return super(StartStopCallback, self).start(doc)
def descriptor(self, doc): server_message("Receive the stream {}.".format(doc["name"])) return super(StartStopCallback, self).descriptor(doc)
def process( *, raw_img: np.ndarray, ai: tp.Union[None, AzimuthalIntegrator], user_mask: np.ndarray = None, auto_mask: bool = True, dk_img: np.ndarray = None, integ_setting: dict = None, mask_setting: dict = None, pdfgetx_setting: dict = None, ) -> dict: """The function to process the data from event.""" # initialize the data dictionary data = { "dk_sub_image": raw_img, "mask": np.zeros_like(raw_img), "chi_Q": np.array([0.]), "chi_I": np.array([0.]), "chi_max": np.float(0.), "chi_argmax": np.float(0.), "iq_Q": np.array([0.]), "iq_I": np.array([0.]), "sq_Q": np.array([0.]), "sq_S": np.array([0.]), "fq_Q": np.array([0.]), "fq_F": np.array([0.]), "gr_r": np.array([0.]), "gr_G": np.array([0.]), "gr_max": np.float(0.), "gr_argmax": np.float(0.) } # dark subtraction if dk_img is not None: data["dk_sub_image"] = np.subtract(raw_img, dk_img) # if no calibration, output data now if ai is None: return data # do auto masking if specified if auto_mask: data["mask"], _ = integ.auto_mask(data["dk_sub_image"], ai, mask_setting=mask_setting, user_mask=user_mask) elif user_mask is not None: data["mask"] = user_mask # integration x, y = ai.integrate1d(data["dk_sub_image"], mask=data["mask"], **integ_setting) chi_max_ind = np.argmax(y) data.update({ "chi_Q": x, "chi_I": y, "chi_max": y[chi_max_ind], "chi_argmax": x[chi_max_ind] }) # transformation if not _PDFGETX_AVAILABLE: io.server_message( "diffpy.pdfgetx is not installed. No use [0.] for all the relevant data." ) return data pdfconfig = PDFConfig(**pdfgetx_setting) pdfgetter = PDFGetter(pdfconfig) pdfgetter(x, y) iq, sq, fq, gr = pdfgetter.iq, pdfgetter.sq, pdfgetter.fq, pdfgetter.gr gr_max_ind = np.argmax(gr[1]) data.update({ "iq_Q": iq[0], "iq_I": iq[1], "sq_Q": sq[0], "sq_S": sq[1], "fq_Q": fq[0], "fq_F": fq[1], "gr_r": gr[0], "gr_G": gr[1], "gr_max": gr[1][gr_max_ind], "gr_argmax": gr[0][gr_max_ind] }) return data
def stop(self, doc, _md=None): io.server_message("Read the stop of {}".format(doc["run_start"])) return super(AnalysisStream, self).stop(doc)
def event(self, doc): server_message("Receive the event {}.".format(doc["seq_num"])) return super(StartStopCallback, self).event(doc)
def event_page(self, doc): server_message("Receive the event page.") return super(StartStopCallback, self).event_page(doc)
def calib_base(self): dir_path = self.get("CALIBRATION", "calib_base") if not dir_path: dir_path = "~/pdfstream_calibration" io.server_message("Missing calib_base in configuration. Use '{}'.".format(dir_path)) return Path(dir_path).expanduser()