Skip to content
Snippets Groups Projects
Commit 58362d46 authored by Mauro Rovezzi's avatar Mauro Rovezzi
Browse files

wip: safe pre_edge

parent db20acaa
No related branches found
No related tags found
2 merge requests!4Minor updates and bug fixes,!3Fix10 rebin second dataset
%% Cell type:markdown id: tags:
# Development sandbox notebook
This notebook is for development and testing of new features and bug fixes only.
%% Cell type:code id: tags:
``` python
%load_ext autoreload
%autoreload 2
# Uncomment the following two lines if the plots do not show in the notebook
# import plotly.io as pio
# pio.renderers.default = "iframe"
import os
from dataclasses import asdict
from IPython.display import display
from famewoks import __version__ as wkflver
from famewoks.datamodel import (
ExpSession,
ExpCounters,
CNTS_FLUO_XMAP,
CNTS_FLUO,
CNTS_CAS,
)
from famewoks.tests.datainfos import DATAINFOS, get_exp_session
from famewoks.plots import plot_data, plot_eshift
from famewoks.bliss2larch import (
search_samples,
show_samples_info,
search_datasets,
load_data,
get_scans,
get_group,
set_enealign,
apply_eshift,
set_bad_fluo_channels,
set_bad_scans,
set_bad_samples,
merge_data,
save_data,
)
from famewoks.bliss2larch import _logger
# adjust the logger level:
# "DEBUG" -> show all messages
# "INFO" -> useful messages
# "WARNING" -> warnings only
# "ERROR" -> only errors
_logger.setLevel("INFO")
_logger.setLevel("DEBUG")
# show workflow version (and famewoks branch information)
branch = os.popen("cd ~/devel/famewoks; git branch --show-current").read()[:-1]
_logger.info(f"--> Using famewoks version: {wkflver} (branch: {branch})")
# define the counters names or use those already defined for BM16: CNTS_FLUO_XMAP, CNTS_FLUO, CNTS_CAS
CNTS_CAS_XPAD = ExpCounters(
ene="energy_enc",
ix=["p201_1_bkg_sub", "p201_3_bkg_sub", "p201_5_bkg_sub"], #: I0, I1, I2
fluo_roi=["xpad_roi1"], # all detector names for ROI1
fluo_corr=["xpad_roi1"], # all detector names (DT corrected)
fluo_time=["sec"], # elapsed time, which is different for the spikes
time="sec", # "musst_timer"
)
# define the experimental session metadata manually
session = ExpSession(
flag=1,
datadir="/data/visitor/a161838/bm16/20241113",
proposal="a161838",
session="20241113",
proposer="Quantin",
lc="AA, IK",
elem="V",
edge="K",
comment="",
counters=CNTS_CAS, # *NOTE* give here the correct counters names!
samples=[],
bad_samples=[],
bad_fluo_channels=None,
enealign=None,
)
# (testing) otherwise it is possible to get the session from a collection of previous experiments
# session = get_exp_session(DATAINFOS, proposer="Isaure",session="20241003")
# session = get_exp_session(DATAINFOS, proposer="Rimondi",session="20241001")
# session = get_exp_session(DATAINFOS, proposer="Biswas",session="20240924")
# session = get_exp_session(DATAINFOS, proposer="Sanchez",session="20240716")
# session = get_exp_session(DATAINFOS, proposer="Stellato",session="20240625")
# session = get_exp_session(DATAINFOS, proposer="Bertrand",session="20240910")
# display the session metadata
#display(asdict(session))
```
%% Cell type:code id: tags:
``` python
samples = search_samples(session, ignore_names=["rack", "mount", "align", "bl_", "sample"])
```
%% Cell type:code id: tags:
``` python
sel_sample = 10
datasets = search_datasets(session, sample=sel_sample)
```
%% Cell type:code id: tags:
``` python
sel_dataset = 0
dataset = datasets[sel_dataset]
load_data(
session,
dataset,
use_fluo_corr=False,
iskip=1, #: ignore the first point
istrip=1, #: ignore the last point
calc_eshift=True,
merge=True,
skip_scans=[],
)
```
%% Cell type:code id: tags:
``` python
fig = plot_data(
dataset,
data="fluo",
ynorm="area",
show_slide=True,
show_i0=False,
show_deriv=False,
show_e0=True,
show_merge="rebin",
show_merge=True,
)
```
%% Cell type:code id: tags:
``` python
fig.add_vline(x=dataset.fluo.e0)
```
%% Cell type:code id: tags:
``` python
from famewoks.bliss2larch import safe_pre_edge
sg = safe_pre_edge(session, group=dataset.fluo)
```
%% Cell type:code id: tags:
``` python
```
......
......@@ -32,6 +32,8 @@ from larch.io import AthenaProject
from larch.xafs import pre_edge
from larch.xafs.rebin_xafs import rebin_xafs
from xraydb import xray_edge
# from larch.xafs.pre_edge import energy_align
from famewoks.energy import energy_align
......@@ -45,6 +47,29 @@ _logger.setLevel("INFO") # adjust logger level .DEBUG, .INFO, .WARNING, .ERROR
_logger.debug(f"DataSourceSpecH5 version: {ds_version}")
_logger.debug(f"workflow version: {wkfl_version}")
def safe_pre_edge(session: ExpSession, group: Group, verbose: bool = True) -> Group:
"""Apply `pre_edge` to a group, safely catching errors and checking for correct E0"""
infolab = group.filename
glabel = group.label if not None else ""
try:
e0ref = xray_edge(session.elem, session.edge).energy
_logger.debug(f"{infolab}_{glabel} -> safe_pre_ede() -> E0 for {session.elem} {session.edge} edge: {e0ref:.2f} eV")
except Exception as err:
_logger.error(f"{infolab}_{glabel} -> cannot get edge energy from session -> check element and edge")
_logger.debug(f"ERR---> {err}")
e0ref = 0
try:
pre_edge(group)
except Exception as err:
_logger.warning(f"{infolab}_{glabel} -> `pre_edge()` failed")
_logger.debug(f"ERR---> {err}")
e0thr = e0ref * 0.01
if abs(group.e0 - e0ref) > e0thr:
_logger.debug(f"{infolab}_{glabel} -> found E0 is out of 1% threshold from theory ({e0ref:.2f} eV) -> forced from {group.e0:.2f} to {e0ref:.2f} eV")
group.e0 = e0ref
return group
def search_samples(
session: ExpSession,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment