Skip to content

Commit

Permalink
add tutorial notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
steinnymir committed Oct 18, 2023
1 parent 9a3677f commit 006921c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 149 deletions.
10 changes: 6 additions & 4 deletions sed/calibrator/energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2183,8 +2183,8 @@ def apply_energy_offset(
columns (Union[str, Sequence[str]]): Name of the column(s) to apply the shift to.
signs (Union[int, Sequence[int]]): Sign of the shift to apply. (+1 or -1)
energy_column (str, optional): Name of the column containing the energy values.
reduce (str): The reduction to apply to the column. If "rolled" it searches for columns with
suffix "_rolled", e.g. "sampleBias_rolled", as those generated by the
reductions (str): The reduction to apply to the column. If "rolled" it searches for columns
with suffix "_rolled", e.g. "sampleBias_rolled", as those generated by the
``SedProcessor.smooth_columns()`` function. Otherwise should be an available method of
dask.dataframe.Series. For example "mean". In this case the function is applied to the
column to generate a single value for the whole dataset. If None, the shift is applied
Expand All @@ -2199,7 +2199,8 @@ def apply_energy_offset(
columns = [columns]
if isinstance(signs, int):
signs = [signs]

if reductions is None:
reductions = [None] * len(columns)
columns_ = []
reductions_ = []
to_roll = []
Expand All @@ -2219,7 +2220,8 @@ def apply_energy_offset(
raise RuntimeError(f"Columns {to_roll} have not been smoothed. please run `smooth_column`")

df = dfops.apply_offset_from_columns(
tartget_column=energy_column,
df=df,
target_column=energy_column,
offset_columns=columns_,
signs=signs,
reductions=reductions_,
Expand Down
2 changes: 1 addition & 1 deletion sed/core/dfops.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ 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):
for col, sign, red in zip(offset_columns, signs, reductions):
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)
Expand Down
45 changes: 9 additions & 36 deletions sed/core/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,48 +1132,23 @@ def apply_energy_offset(
constant: float = None,
columns: Union[str, Sequence[str]] = None,
signs: Union[int, Sequence[int]] = None,
mode: Union[str, Sequence[str]] = "direct",
window: float = None,
sigma: float = None,
rolling_group_channel: str = None,
reductions: Union[str, Sequence[str]] = None,
) -> None:
"""Shift the energy axis of the dataframe by a given amount.
Args:
constant (float, optional): The constant to shift the energy axis by.
columns (Union[str, Sequence[str]]): The columns to shift.
signs (Union[int, Sequence[int]]): The sign of the shift.
mode (Union[str, Sequence[str]], optional): The mode of the shift.
Defaults to "direct".
window (float, optional): The window size for the rolling mean.
Defaults to None.
sigma (float, optional): The sigma for the rolling mean.
Defaults to 2.
rolling_group_channel (str, optional): The channel to use for the rolling
mean. Defaults to None.
reductions (str): The reduction to apply to the column. If "rolled" it searches for
columns with suffix "_rolled", e.g. "sampleBias_rolled", as those generated by the
``SedProcessor.smooth_columns()`` function. Otherwise should be an available method
of dask.dataframe.Series. For example "mean". In this case the function is applied
to the column to generate a single value for the whole dataset. If None, the shift
is applied per-dataframe-row. Defaults to None.
Raises:
ValueError: If the energy column is not in the dataframe.
"""
if columns is None and constant is None:
offset_dict = self._config["energy"].get("offset", None)
if offset_dict is None:
raise ValueError(
"No offset parameters provided and no offset found in config file!",
)
constant = offset_dict["constant"]
columns = []
signs = []
modes = []
windows = []
sigmas = []
for k, v in offset_dict:
columns.append(k)
signs.append(v["sign"])
modes.append(v["mode"])
windows.append(v.get("window", None))
sigmas.append(v.get("sigma", None))

energy_column = self._config["dataframe"]["energy_column"]
if energy_column not in self._dataframe.columns:
raise ValueError(
Expand All @@ -1183,11 +1158,9 @@ def apply_energy_offset(
self._dataframe, metadata = energy.apply_energy_offset(
df=self._dataframe,
columns=columns,
energy_column=energy_column,
signs=signs,
mode=mode,
window=window,
sigma=sigma,
rolling_group_channel=rolling_group_channel,
reductions=reductions,
config=self._config,
)
self._dataframe[energy_column] += constant
Expand Down
142 changes: 34 additions & 108 deletions tutorial/5 - hextof workflow.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@
"metadata": {},
"outputs": [],
"source": [
"# axes = ['sampleBias', 'dldTimeSteps']\n",
"# bins = [5, 500]\n",
"# ranges = [[28,33], [4000, 6500]]\n",
"# res = sp.compute(bins=bins, axes=axes, ranges=ranges)"
"sp.append_energy_axis()\n"
]
},
{
Expand All @@ -72,79 +69,11 @@
"metadata": {},
"outputs": [],
"source": [
"# plt.figure()\n",
"# res.plot.line(x='dldTimeSteps');"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# posMax = np.zeros(5)\n",
"# bias = np.zeros(5)\n",
"# for i in range(5):\n",
"# posMax[i] = res['dldTimeSteps'][np.argmax(res[i,:].values)]\n",
"# bias[i] = res['sampleBias'][i]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# parameters = sed.calibrator.energy.poly_energy_calibration(\n",
"# pos=posMax, \n",
"# vals=bias, \n",
"# order=2, \n",
"# ref_id = 3, \n",
"# ref_energy=0.0,\n",
"# t = 0)\n",
"# #t=42720.0)\n",
"# print(\"parameters:\")\n",
"# print(parameters.keys())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# parameters"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sp.append_energy_axis()#calibration=parameters)\n",
"# sp_enCal._dataframe['energy'] = -sp_enCal._dataframe['energy']-4.0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# %matplotlib widget"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# axes = ['sampleBias', 'energy']\n",
"# bins = [5, 500]\n",
"# ranges = [[28,33], [-5, 10]]\n",
"# res = sp.compute(bins=bins, axes=axes, ranges=ranges)"
"sp.apply_energy_offset(\n",
" constant=31.6, \n",
" columns=['sampleBias'],\n",
" signs=[-1],\n",
")"
]
},
{
Expand All @@ -153,8 +82,10 @@
"metadata": {},
"outputs": [],
"source": [
"# plt.figure()\n",
"# res.plot.line(x='energy');"
"axes = ['sampleBias', 'energy']\n",
"bins = [5, 500]\n",
"ranges = [[28,33], [-1,5]]\n",
"res = sp.compute(bins=bins, axes=axes, ranges=ranges)"
]
},
{
Expand All @@ -163,7 +94,7 @@
"metadata": {},
"outputs": [],
"source": [
"# sp.attributes.pop('apply_energy_offset')"
"%matplotlib widget"
]
},
{
Expand All @@ -172,12 +103,9 @@
"metadata": {},
"outputs": [],
"source": [
"sp.apply_energy_offset(\n",
" constant=32, \n",
" columns=['sampleBias'],# 'tofVoltage'], \n",
" signs=[-1],#, +1], \n",
" mode='direct'\n",
")"
"plt.figure()\n",
"res.mean('sampleBias').plot.line(x='energy',linewidth=3);\n",
"res.plot.line(x='energy',linewidth=1,alpha=.5,label='all');\n"
]
},
{
Expand All @@ -186,8 +114,7 @@
"metadata": {},
"outputs": [],
"source": [
"h = sp.dataframe[['energy','sampleBias','tofVoltage','monochromatorPhotonEnergy']].head()\n",
"h"
"sp.dataframe['binding_energy'] = -sp.dataframe['energy']"
]
},
{
Expand All @@ -196,9 +123,9 @@
"metadata": {},
"outputs": [],
"source": [
"axes = ['sampleBias', 'energy']\n",
"axes = ['sampleBias', 'binding_energy']\n",
"bins = [5, 500]\n",
"ranges = [[28,33], [h['energy'][0]-5,h['energy'][0]+5]]\n",
"ranges = [[28,33], [-5,1]]\n",
"res = sp.compute(bins=bins, axes=axes, ranges=ranges)"
]
},
Expand All @@ -209,38 +136,37 @@
"outputs": [],
"source": [
"plt.figure()\n",
"res.mean('sampleBias').plot.line(x='energy',linewidth=3);\n",
"res.plot.line(x='energy',linewidth=1,alpha=.5,label='all');\n"
"ax = plt.subplot(111)\n",
"res.binding_energy.attrs['unit'] = 'eV'\n",
"res.mean('sampleBias').plot.line(x='binding_energy',linewidth=3, ax=ax);\n",
"res.plot.line(x='binding_energy',linewidth=1,alpha=.5,label='all',ax=ax);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"# sp._dataframe['energy'] = -sp._dataframe['energy'] - 9"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"axes = ['sampleBias', 'energy']\n",
"bins = [5, 500]\n",
"ranges = [[28,33], [h['energy'][0]-5,h['energy'][0]+5]]\n",
"res = sp.compute(bins=bins, axes=axes, ranges=ranges)"
]
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "sed38",
"language": "python",
"name": "sed38"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.18"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 006921c

Please sign in to comment.