Skip to content

Commit

Permalink
fix energy calibration and hextof notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
steinnymir committed Oct 25, 2023
1 parent 4cad800 commit 5b866ca
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 258 deletions.
9 changes: 7 additions & 2 deletions sed/calibrator/energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1987,7 +1987,7 @@ def residual(pars, time, data, binwidth, binning, energy_scale):
value=E0_pars.get("value", min(vals)),
min=E0_pars.get("min", -np.inf),
max=E0_pars.get("max", np.inf),
vary=d_pars.get("vary", True),
vary=E0_pars.get("vary", True),
)
fit = Minimizer(
residual,
Expand Down Expand Up @@ -2225,6 +2225,7 @@ def apply_energy_offset(
df: Union[pd.DataFrame, dask.dataframe.DataFrame],
columns: Union[str, Sequence[str]],
signs: Union[int, Sequence[int]],
subtract_mean: Union[bool, Sequence[bool]] = True,
energy_column: str = None,
reductions: Union[str, Sequence[str]] = None,
config: dict = None,
Expand Down Expand Up @@ -2255,9 +2256,12 @@ def apply_energy_offset(
columns = [columns]
if isinstance(signs, int):
signs = [signs]
if len(signs) != len(columns):
raise ValueError("signs and columns must have the same length.")
if isinstance(subtract_mean, bool):
subtract_mean = [subtract_mean] * len(columns)
if reductions is None:
reductions = [None] * len(columns)

columns_: List[str] = []
reductions_: List[str] = []
to_roll: List[str] = []
Expand All @@ -2280,6 +2284,7 @@ def apply_energy_offset(
target_column=energy_column,
offset_columns=columns_,
signs=signs,
subtract_mean=subtract_mean,
reductions=reductions_,
inplace=True,
)
Expand Down
5 changes: 4 additions & 1 deletion sed/core/dfops.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ def apply_offset_from_columns(
offset_columns: Union[str, Sequence[str]],
signs: Union[int, Sequence[int]],
reductions: Union[str, Sequence[str]],
subtract_mean: Union[bool, Sequence[bool]],
inplace: bool = True,
) -> Union[pd.DataFrame, dask.dataframe.DataFrame]:
"""Apply an offset to a column based on the values of other columns.
Expand Down Expand Up @@ -359,10 +360,12 @@ def apply_offset_from_columns(
if len(signs) != len(offset_columns):
raise ValueError("signs and offset_columns must have the same length!")

for col, sign, red in zip(offset_columns, signs, reductions):
for col, sign, red, submean in zip(offset_columns, signs, reductions, subtract_mean):
assert col in df.columns, f"{col} not in dataframe!"
if red is not None:
df[target_column] = df[target_column] + sign * df[col].agg(red)
else:
df[target_column] = df[target_column] + sign * df[col]
if submean:
df[target_column] = df[target_column] - sign * df[col].mean()
return df
38 changes: 22 additions & 16 deletions sed/core/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,7 @@ def apply_energy_offset(
columns: Union[str, Sequence[str]] = None,
signs: Union[int, Sequence[int]] = None,
reductions: Union[str, Sequence[str]] = None,
subtract_mean: Union[bool, Sequence[bool]] = None,
) -> None:
"""Shift the energy axis of the dataframe by a given amount.
Expand All @@ -1190,22 +1191,27 @@ def apply_energy_offset(
f"Energy column {energy_column} not found in dataframe! "
"Run energy calibration first",
)
self._dataframe, metadata = energy.apply_energy_offset(
df=self._dataframe,
columns=columns,
energy_column=energy_column,
signs=signs,
reductions=reductions,
config=self._config,
)
self._dataframe[energy_column] += constant
metadata["offset"] = constant
self._attributes.add(
metadata,
"apply_energy_offset",
# TODO: allow only appending when no offset along this column(s) was applied
duplicate_policy="append",
)
metadata = {}
if columns is not None:
self._dataframe, metadata = energy.apply_energy_offset(
df=self._dataframe,
columns=columns,
energy_column=energy_column,
signs=signs,
reductions=reductions,
subtract_mean=subtract_mean,
config=self._config,
)
if constant is not None:
self._dataframe[energy_column] += constant
metadata["offset"] = constant
if len(metadata) > 0:
self._attributes.add(
metadata,
"apply_energy_offset",
# TODO: allow only appending when no offset along this column(s) was applied
duplicate_policy="append",
)

def append_tof_ns_axis(
self,
Expand Down
Loading

0 comments on commit 5b866ca

Please sign in to comment.