{ "cells": [ { "cell_type": "markdown", "id": "449cb409-2af4-4caa-a3e0-c80828e9da47", "metadata": {}, "source": [ "## Appendix I: Generating the synthetic data" ] }, { "cell_type": "markdown", "id": "c8a62cde-00cd-4a60-ad8b-49af53992c52", "metadata": {}, "source": [ "\n", "### Two peaks\n", "[back to top] \n" ] }, { "cell_type": "code", "execution_count": null, "id": "6410dd9d-ffe1-4333-9140-1d6db9607a2b", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import hyperspy.api as hs\n", "\n", "domain = 32 #size of the square domain\n", "hfactor = 600\n", "cent = (domain//2, domain//2)\n", "y,x = np.ogrid[-cent[0]:domain-cent[0], -cent[1]:domain-cent[1]]\n", "\n", "def gaussian2d(x, y, A=1, x0=0, y0=0, sigmax=20, sigmay=10):\n", " return A * np.exp(-((x-x0)**2 / 2 / sigmax ** 2 + (y-y0)**2 / 2 / sigmay ** 2))\n", "\n", "center_narrow = 50 + 10 * np.sin(3 * np.pi * x / domain) * np.cos(4 * np.pi * y / domain)\n", "center_wide = 50 + 10 * (-0.1 * np.sin(3 * np.pi * x / domain) * np.cos(4 * np.pi * y / domain))\n", "\n", "r = np.sqrt(x**2 + y**2)\n", "h_narrow = .5 * (.5 + np.sin(r)**2) * gaussian2d(x, y) * hfactor\n", "h_wide = (.5 + np.cos(r)**2) * gaussian2d(x, y) * hfactor\n", "\n", "s = hs.signals.Signal1D(np.ones((domain,domain, 1024)))\n", "s.metadata.General.title = 'Two gaussians'\n", "s.axes_manager[0].name = \"x\"\n", "s.axes_manager[0].units = \"nm\"\n", "s.axes_manager[1].name = \"y\"\n", "s.axes_manager[1].units = \"nm\"\n", "\n", "s.axes_manager[2].name = \"Energy\"\n", "s.axes_manager[2].name = \"eV\"\n", "s.axes_manager[2].scale = 0.1\n", "m0 = s.create_model()\n", "\n", "gs01 = hs.model.components1D.GaussianHF()\n", "gs01.name = \"wide\"\n", "m0.append(gs01)\n", "gs01.fwhm.value = 60\n", "gs01.centre.map['values'][:] = center_wide\n", "gs01.centre.map['is_set'][:] = True\n", "gs01.height.map['values'][:] = h_wide\n", "gs01.height.map['is_set'][:] = True\n", "\n", "gs02 = hs.model.components1D.GaussianHF()\n", "gs02.name = \"narrow\"\n", "m0.append(gs02)\n", "gs02.fwhm.value = 6\n", "gs02.centre.map['values'][:] = center_narrow\n", "gs02.centre.map['is_set'][:] = True\n", "gs02.height.map['values'][:] = h_narrow\n", "gs02.height.map['is_set'][:] = True\n", "s.data = m0.as_signal().data\n", "s.add_poissonian_noise(random_state=0)\n", "m0.store(\"ground truth\")\n", "s.save(\"two_peaks.hspy\", overwrite=True)" ] }, { "cell_type": "markdown", "id": "5af2ac12-ec43-4fba-9a63-ae3a43ceb360", "metadata": {}, "source": [ "\n", "### Wobbly peak\n", "[back to top] \n" ] }, { "cell_type": "code", "execution_count": null, "id": "630dcc09-01ec-4e33-a073-ea0352f5541b", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import hyperspy.api as hs\n", "\n", "k = 1\n", "alpha = 15\n", "amp = 3\n", "\n", "gaus_position = 15\n", "gaus_width = 5\n", "gaus_A = 300\n", "\n", "gradient = 0.6\n", "offset= 3\n", "\n", "sin_component = hs.model.components1D.Expression('A * sin(k*x + alpha)', name='sin', k=k,\n", " alpha=alpha, A=amp)\n", "gaus = hs.model.components1D.Gaussian(A=gaus_A, sigma=gaus_width, centre=gaus_position)\n", "\n", "poly = hs.model.components1D.Polynomial(1)\n", "\n", "poly.coefficients.value = (gradient, offset)\n", "\n", "axis = np.linspace(0, 30, 3000, dtype='double')\n", "\n", "result = sin_component.function(axis)+ gaus.function(axis) + poly.function(axis)\n", "s = hs.signals.Signal1D(result)\n", "s.axes_manager[0].name = 'x'\n", "s.axes_manager[0].scale = 0.1\n", "s.axes_manager[0].offset = 0\n", "\n", "s.metadata.General.author = 'Tomas Ostasevicius'\n", "s.metadata.General.title = 'Sin + poly(2) + Gaussian'\n", "s.save('wobbly_peak', overwrite=True)" ] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:hyperspy_EMC2024]", "language": "python", "name": "conda-env-hyperspy_EMC2024-py" }, "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.11.9" } }, "nbformat": 4, "nbformat_minor": 5 }