From 352a498358de21c5c6814011c0b8e820de6bbd2a Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Thu, 30 Apr 2026 15:11:25 +0100 Subject: [PATCH] fix(tutorials): accept xp kwarg in inline model_data_from PyAutoFit#1240 added `xp=self._xp` to the bare-Model branch of `af.ex.Analysis.model_data_1d_from`. Inline tutorial classes that defined `model_data_from(self, xvalues)` without the `xp` parameter broke with `TypeError: unexpected keyword argument 'xp'` whenever the script reached `af.ex.Analysis(...).fit(...)`. Update Gaussian and Exponential in overview_1_the_basics.py and GaussianNoConfig in cookbooks/configs.py to accept `xp=np` and route their array math through `xp` rather than `np`. Other methods (`__call__`, `inverse`, `f`, `fwhm`) are not reached from `af.ex.Analysis` and remain unchanged. Notebooks regenerated via PyAutoBuild/generate.py. Two unrelated notebook diffs are included where the .ipynb had drifted from script edits that landed on main (graphical_models, overview_3_statistical_methods URL updates). Co-Authored-By: Claude Opus 4.7 (1M context) --- notebooks/cookbooks/configs.ipynb | 8 ++++---- notebooks/features/graphical_models.ipynb | 3 ++- notebooks/overview/overview_1_the_basics.ipynb | 16 ++++++++-------- .../overview_3_statistical_methods.ipynb | 2 +- scripts/cookbooks/configs.py | 10 +++++----- scripts/overview/overview_1_the_basics.py | 18 +++++++++--------- 6 files changed, 29 insertions(+), 28 deletions(-) diff --git a/notebooks/cookbooks/configs.ipynb b/notebooks/cookbooks/configs.ipynb index 09e2a99d..23bdfc03 100644 --- a/notebooks/cookbooks/configs.ipynb +++ b/notebooks/cookbooks/configs.ipynb @@ -77,15 +77,15 @@ " self.normalization = normalization\n", " self.sigma = sigma\n", "\n", - " def model_data_from(self, xvalues: np.ndarray) -> np.ndarray:\n", + " def model_data_from(self, xvalues: np.ndarray, xp=np) -> np.ndarray:\n", " \"\"\"\n", " The usual method that returns the 1D data of the `Gaussian` profile.\n", " \"\"\"\n", " transformed_xvalues = xvalues - self.centre\n", "\n", - " return np.multiply(\n", - " np.divide(self.normalization, self.sigma * np.sqrt(2.0 * np.pi)),\n", - " np.exp(-0.5 * np.square(np.divide(transformed_xvalues, self.sigma))),\n", + " return xp.multiply(\n", + " xp.divide(self.normalization, self.sigma * xp.sqrt(2.0 * xp.pi)),\n", + " xp.exp(-0.5 * xp.square(xp.divide(transformed_xvalues, self.sigma))),\n", " )\n" ], "outputs": [], diff --git a/notebooks/features/graphical_models.ipynb b/notebooks/features/graphical_models.ipynb index 4c689d91..4d37b3b1 100644 --- a/notebooks/features/graphical_models.ipynb +++ b/notebooks/features/graphical_models.ipynb @@ -24,7 +24,8 @@ "simultaneously fitting 3 noisy 1D Gaussians. However, graphical models are an extensive feature and at the end of\n", "this example we will discuss other options available in **PyAutoFit** for composing a fitting a graphical model.\n", "\n", - "The **HowToFit** tutorials contain a chapter dedicated to composing and fitting graphical models.\n", + "The **HowToFit** tutorials contain a chapter dedicated to composing and fitting graphical models, available\n", + "in the standalone repository at https://github.com/PyAutoLabs/HowToFit (see `scripts/chapter_3_graphical_models/`).\n", "\n", "__Contents__\n", "\n", diff --git a/notebooks/overview/overview_1_the_basics.ipynb b/notebooks/overview/overview_1_the_basics.ipynb index f2fa406b..9541e7a8 100644 --- a/notebooks/overview/overview_1_the_basics.ipynb +++ b/notebooks/overview/overview_1_the_basics.ipynb @@ -204,7 +204,7 @@ " self.normalization = normalization\n", " self.sigma = sigma\n", "\n", - " def model_data_from(self, xvalues: np.ndarray) -> np.ndarray:\n", + " def model_data_from(self, xvalues: np.ndarray, xp=np) -> np.ndarray:\n", " \"\"\"\n", " Returns the 1D Gaussian profile on a line of Cartesian x coordinates.\n", "\n", @@ -221,9 +221,9 @@ " \"\"\"\n", " transformed_xvalues = xvalues - self.centre\n", "\n", - " return np.multiply(\n", - " np.divide(self.normalization, self.sigma * np.sqrt(2.0 * np.pi)),\n", - " np.exp(-0.5 * np.square(np.divide(transformed_xvalues, self.sigma))),\n", + " return xp.multiply(\n", + " xp.divide(self.normalization, self.sigma * xp.sqrt(2.0 * xp.pi)),\n", + " xp.exp(-0.5 * xp.square(xp.divide(transformed_xvalues, self.sigma))),\n", " )\n", "\n", " @property\n", @@ -981,7 +981,7 @@ " self.normalization = normalization\n", " self.rate = rate\n", "\n", - " def model_data_from(self, xvalues: np.ndarray):\n", + " def model_data_from(self, xvalues: np.ndarray, xp=np):\n", " \"\"\"\n", " Returns the symmetric 1D Exponential on an input list of Cartesian x coordinates.\n", "\n", @@ -995,9 +995,9 @@ " xvalues\n", " The x coordinates in the original reference frame of the data.\n", " \"\"\"\n", - " transformed_xvalues = np.subtract(xvalues, self.centre)\n", - " return self.normalization * np.multiply(\n", - " self.rate, np.exp(-1.0 * self.rate * abs(transformed_xvalues))\n", + " transformed_xvalues = xp.subtract(xvalues, self.centre)\n", + " return self.normalization * xp.multiply(\n", + " self.rate, xp.exp(-1.0 * self.rate * abs(transformed_xvalues))\n", " )\n" ], "outputs": [], diff --git a/notebooks/overview/overview_3_statistical_methods.ipynb b/notebooks/overview/overview_3_statistical_methods.ipynb index 23880750..b4052cba 100644 --- a/notebooks/overview/overview_3_statistical_methods.ipynb +++ b/notebooks/overview/overview_3_statistical_methods.ipynb @@ -31,7 +31,7 @@ "\n", "A full description of using hierarchical models is given below:\n", "\n", - "https://github.com/Jammy2211/autofit_workspace/blob/release/notebooks/howtofit/chapter_graphical_models/tutorial_4_hierachical_models.ipynb\n", + "https://github.com/PyAutoLabs/HowToFit/blob/main/notebooks/chapter_3_graphical_models/tutorial_4_hierachical_models.ipynb\n", "\n", "Model Comparison\n", "----------------\n", diff --git a/scripts/cookbooks/configs.py b/scripts/cookbooks/configs.py index a1416cec..864fdf42 100644 --- a/scripts/cookbooks/configs.py +++ b/scripts/cookbooks/configs.py @@ -21,7 +21,7 @@ - Labels: Config files which specify the labels of model component parameters for visualization. """ -# from autoconf import setup_notebook; setup_notebook() +# from autoconf import setup_notebook; setup_notebook() import numpy as np from os import path @@ -56,15 +56,15 @@ def __init__( self.normalization = normalization self.sigma = sigma - def model_data_from(self, xvalues: np.ndarray) -> np.ndarray: + def model_data_from(self, xvalues: np.ndarray, xp=np) -> np.ndarray: """ The usual method that returns the 1D data of the `Gaussian` profile. """ transformed_xvalues = xvalues - self.centre - return np.multiply( - np.divide(self.normalization, self.sigma * np.sqrt(2.0 * np.pi)), - np.exp(-0.5 * np.square(np.divide(transformed_xvalues, self.sigma))), + return xp.multiply( + xp.divide(self.normalization, self.sigma * xp.sqrt(2.0 * xp.pi)), + xp.exp(-0.5 * xp.square(xp.divide(transformed_xvalues, self.sigma))), ) diff --git a/scripts/overview/overview_1_the_basics.py b/scripts/overview/overview_1_the_basics.py index e7a9175f..794d8332 100644 --- a/scripts/overview/overview_1_the_basics.py +++ b/scripts/overview/overview_1_the_basics.py @@ -43,7 +43,7 @@ To begin, lets import ``autofit`` (and ``numpy``) using the convention below: """ -# from autoconf import setup_notebook; setup_notebook() +# from autoconf import setup_notebook; setup_notebook() import autofit as af import autofit.plot as aplt @@ -150,7 +150,7 @@ def __init__( self.normalization = normalization self.sigma = sigma - def model_data_from(self, xvalues: np.ndarray) -> np.ndarray: + def model_data_from(self, xvalues: np.ndarray, xp=np) -> np.ndarray: """ Returns the 1D Gaussian profile on a line of Cartesian x coordinates. @@ -167,9 +167,9 @@ def model_data_from(self, xvalues: np.ndarray) -> np.ndarray: """ transformed_xvalues = xvalues - self.centre - return np.multiply( - np.divide(self.normalization, self.sigma * np.sqrt(2.0 * np.pi)), - np.exp(-0.5 * np.square(np.divide(transformed_xvalues, self.sigma))), + return xp.multiply( + xp.divide(self.normalization, self.sigma * xp.sqrt(2.0 * xp.pi)), + xp.exp(-0.5 * xp.square(xp.divide(transformed_xvalues, self.sigma))), ) @property @@ -665,7 +665,7 @@ def __init__( self.normalization = normalization self.rate = rate - def model_data_from(self, xvalues: np.ndarray): + def model_data_from(self, xvalues: np.ndarray, xp=np): """ Returns the symmetric 1D Exponential on an input list of Cartesian x coordinates. @@ -679,9 +679,9 @@ def model_data_from(self, xvalues: np.ndarray): xvalues The x coordinates in the original reference frame of the data. """ - transformed_xvalues = np.subtract(xvalues, self.centre) - return self.normalization * np.multiply( - self.rate, np.exp(-1.0 * self.rate * abs(transformed_xvalues)) + transformed_xvalues = xp.subtract(xvalues, self.centre) + return self.normalization * xp.multiply( + self.rate, xp.exp(-1.0 * self.rate * abs(transformed_xvalues)) )