diff --git a/sed/core/processor.py b/sed/core/processor.py index 263f1ca1..9b90fe8b 100644 --- a/sed/core/processor.py +++ b/sed/core/processor.py @@ -1117,7 +1117,13 @@ def save_energy_calibration( "Energy calibration parameters not found, need to generate parameters first!", ) from exc - config = {"energy": {"calibration": calibration}} + config = { + "energy": { + "calibration": calibration, + }, + } + if isinstance(self.ec.offset, dict): + config["energy"]["offset"] = self.ec.offset save_config(config, filename, overwrite) # 4. Apply energy calibration to the dataframe @@ -1204,7 +1210,8 @@ def add_energy_offset( metadata, "add_energy_offset", # TODO: allow only appending when no offset along this column(s) was applied - duplicate_policy="raise", + # TODO: clear memory of modifications if the energy axis is recalculated + duplicate_policy="append", ) self._dataframe = df else: diff --git a/tutorial/5 - hextof workflow.ipynb b/tutorial/5 - hextof workflow.ipynb index b15b7ade..e17a8a73 100644 --- a/tutorial/5 - hextof workflow.ipynb +++ b/tutorial/5 - hextof workflow.ipynb @@ -44,7 +44,8 @@ }, "outputs": [], "source": [ - "config_file = Path(sed.__file__).parent.parent/'tutorial/hextof_config.yaml'\n", + "local_path = Path(sed.__file__).parent.parent/'tutorial/'\n", + "config_file = local_path/'hextof_config.yaml'\n", "assert config_file.exists()" ] }, @@ -148,19 +149,7 @@ "metadata": {}, "outputs": [], "source": [ - "plt.figure()\n", - "dres = res.copy()\n", - "dres.data = np.gradient(res.data, axis=1)\n", - "dres.plot.line(x='dldTimeSteps')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "sp.load_bias_series(binned_data=dres)" + "sp.load_bias_series(binned_data=res)" ] }, { @@ -180,85 +169,17 @@ "metadata": {}, "outputs": [], "source": [ - "axes = ['sampleBias', 'dldTimeSteps']\n", - "bins = [5, 2000]\n", - "ranges = [[28,33], [0, 2000]]\n", - "res = sp.compute(bins=bins, axes=axes, ranges=ranges)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "plt.figure()\n", - "res.plot.line(x='dldTimeSteps')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# ref_id=0\n", - "# ref_energy=0\n", - "# sp.calibrate_energy_axis(\n", - "# ref_id=ref_id,\n", - "# ref_energy=ref_energy,\n", - "# method=\"lmfit\",\n", - "# energy_scale='kinetic',\n", - "# )" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from sed.calibrator.energy import tof2ev, tof2ns" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "ph_peak = 1e-9 * tof2ns(binwidth=sp.config['dataframe']['tof_binwidth'], binning=3,t=219)\n", "sp.calibrate_energy_axis(\n", - " ref_id=0,\n", - " ref_energy=0,\n", + " ref_id=2,\n", + " ref_energy=-1,\n", " method=\"lmfit\",\n", " energy_scale='kinetic',\n", - " d={'value':1.,'min': .8, 'max':5, 'vary':True},\n", - " t0={'value':ph_peak, 'min': ph_peak*0.1, 'max': ph_peak*1.9, 'vary':False},\n", + " d={'value':1.,'min': .2, 'max':1.0, 'vary':True},\n", + " t0={'value':5e-7, 'min': 1e-7, 'max': 1e-6, 'vary':True},\n", " E0={'value': 0., 'min': -100, 'max': 100, 'vary': True},\n", - "\n", ")" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# tof = np.linspace(4000,5000,500)\n", - "# plt.figure()\n", - "# plt.plot(tof, tof2ev(\n", - "# t=tof, \n", - "# tof_distance=1, \n", - "# time_offset=1e-9*tof2ns(binwidth=sp.config['dataframe']['tof_binwidth'], binning=3,t=218), \n", - "# energy_offset=0,\n", - "# binning=3,\n", - "# energy_scale='kinetic',\n", - "# binwidth=sp.config['dataframe']['tof_binwidth'])\n", - "# )" - ] - }, { "cell_type": "code", "execution_count": null, @@ -268,15 +189,6 @@ "sp.append_energy_axis()" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "sp.ec.calibration" - ] - }, { "cell_type": "code", "execution_count": null, @@ -324,14 +236,23 @@ "metadata": {}, "outputs": [], "source": [ - "sp.apply_energy_offset(\n", - " constant=2,\n", + "sp.add_energy_offset(\n", + " constant=0,\n", " columns=['sampleBias','monochromatorPhotonEnergy','tofVoltage'],\n", " signs=[1,-1,-1],\n", - " subtract_mean=True,\n", + " preserve_mean=True,\n", ")" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sp.dataframe" + ] + }, { "cell_type": "code", "execution_count": null, @@ -361,7 +282,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Fit the falling edge at E=0 to evaluate the quality of the energy calibration" + "### save the calibration parameters\n", + "This is currently overwriting all other values in the config file you point at. Do not overwrite local settings! \n", + "This should be fixed in the future." ] }, { @@ -370,60 +293,7 @@ "metadata": {}, "outputs": [], "source": [ - "from lmfit.models import Gaussian2dModel, LorentzianModel, LinearModel" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "curve = res_fit.mean('sampleBias').sel(energy=slice(-0.15,1))\n", - "x = curve.energy\n", - "y = -np.gradient(curve.data)\n", - "full_y = np.gradient(res_fit.data, axis=1)\n", - "gm = LorentzianModel()\n", - "# lin = LinearModel()\n", - "model = gm #+ lin\n", - "params = gm.guess(y, x=x)\n", - "# params.update(lin.make_params())\n", - "result = model.fit(y, params, x=x)\n", - "\n", - "plt.figure()\n", - "plt.plot(x, y, 'bo')\n", - "plt.plot(x, result.best_fit, 'r-')\n", - "result.best_values\n", - "best = result.params" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "{'amplitude': 62.649384173227375,\n", - " 'center': 0.07618539040289204,\n", - " 'sigma': 0.0948828689156287}" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "plt.figure()\n", - "for i in range(len(res_fit.sampleBias)):\n", - " x = res_fit.isel(sampleBias=i).sel(energy=slice(-0.15,1)).energy\n", - " y = res_fit.isel(sampleBias=i).sel(energy=slice(-0.15,1)).data\n", - " y = -np.gradient(y)\n", - " y = y/np.max(y)\n", - " result = model.fit(y, best, x=x)\n", - " print(f'{i}: {result.best_values}')\n", - " plt.plot(x, y+i, 'bo')\n", - " plt.plot(x, result.best_fit+i, 'r-')\n" + "sp.save_energy_calibration(filename=local_path/'energy_calibration.yaml')" ] }, { @@ -443,11 +313,12 @@ }, "outputs": [], "source": [ - "config={\"core\": {\"paths\": {\n", + "config_dict={\"core\": {\"paths\": {\n", " \"data_raw_dir\": \"/asap3/flash/gpfs/pg2/2023/data/11019101/raw/hdf/offline/fl1user3\", \n", " \"data_parquet_dir\": \"/home/agustsss/temp/sed_parquet/\"\n", "}}}\n", - "sp = SedProcessor(runs=[44797], config=config, user_config=config_file, system_config={}, collect_metadata=False)" + "# config = sed.core.config.parse_config(config=config_dict, folder_config=local_path/'energy_calibration.yaml', user_config={}, system_config={})\n", + "sp = SedProcessor(runs=[44797], config=config_dict, folder_config=local_path/'energy_calibration.yaml', user_config=config_file, collect_metadata=False)" ] }, { diff --git a/tutorial/hextof_config.yaml b/tutorial/hextof_config.yaml index 6b31514e..4e16ecca 100644 --- a/tutorial/hextof_config.yaml +++ b/tutorial/hextof_config.yaml @@ -119,22 +119,3 @@ dataframe: pg2: "/asap3/flash/gpfs/pg2/" hextof: "/asap3/fs-flash-o/gpfs/hextof/" wespe: "/asap3/fs-flash-o/gpfs/wespe/" - -energy: - calibration: - d: 2.7342492951998603 - t0: 3.6049383256584405e-08 - E0: -51.289659014865784 # flip sign if switching between kinetic and binding energy - energy_scale: kinetic - refid: 3 - offset: - constant: 2.0 - sampleBias: - sign: 1 - preserve_mean: True - monochromatorPhotonEnergy: - sign: -1 - preserve_mean: True - tofVoltage: - sign: -1 - preserve_mean: True