From ff1131c067cb1bb64d86b5c8fc886b1524d7a183 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Mon, 3 Aug 2026 18:29:20 +0100 Subject: [PATCH] fix: simulators call af.ex.util, and chain-run resolves without __file__ (PyAutoFit#1444) Two failures in the same notebook, the second masked by the first. 1. scripts/simulators/util.py could only ever be imported by the .py scripts: running one puts scripts/simulators/ on sys.path[0], but a notebook kernel has no script directory, so both simulator notebooks failed workspace smoke with `ModuleNotFoundError: No module named 'util'`. The four helpers now live in af.ex.util, so the same call works from a script, a notebook and Colab alike. util.py is deleted along with the util.ipynb it generated. 2. simulators.py chain-ran simulators_sample.py through a path derived from the script's own file path, which a notebook kernel does not define - and no .py exists in the notebooks tree in any case. That cell would have failed the instant the import above was fixed. It is now resolved relative to the repository root, which the script run, the pinned notebook kernel and Colab all share. PENDING RELEASE: needs the af.ex.util helpers from PyAutoFit#1444. Verified: both simulator notebooks execute clean (the chain-run cell included), both .py siblings pass, smoke 10/10, navigator check OK. Co-Authored-By: Claude Opus 5 --- llms-full.txt | 1 - notebooks/simulators/simulators.ipynb | 82 ++-- notebooks/simulators/simulators_sample.ipynb | 9 +- notebooks/simulators/util.ipynb | 418 ------------------- scripts/simulators/simulators.py | 80 ++-- scripts/simulators/simulators_sample.py | 7 +- scripts/simulators/util.py | 346 --------------- workspace_index.json | 8 - 8 files changed, 120 insertions(+), 831 deletions(-) delete mode 100644 notebooks/simulators/util.ipynb delete mode 100644 scripts/simulators/util.py diff --git a/llms-full.txt b/llms-full.txt index f650add..53a12d4 100644 --- a/llms-full.txt +++ b/llms-full.txt @@ -44,4 +44,3 @@ AUTO-GENERATED by PyAutoHands — do not edit by hand; regenerate with generate. - Contents: Gaussian x1, Gaussian x1 (0), Gaussian x1 (1), Gaussian x1 (2), Gaussian x1 (Identical 0), Gaussian x1 (Identical 1), Gaussian x1 (Identical 2), Gaussian x1 + Exponential x1, Gaussian x2 + Exponential x1, Gaussian x2, Gaussian x3, Gaussian x5, Gaussian x1 unconvolved, Gaussian x1 convolved, Gaussian x1 with feature, Gaussian x2 split, Gaussian x1 time - [__Simulators__](scripts/simulators/simulators_sample.py): These scripts simulates many 1D Gaussian datasets with a low signal to noise ratio, which are used to demonstrate model-fitting. - Contents: Gaussian x1 low snr (centre fixed to 50.0), Gaussian x1 low snr (centre drawn from parent Gaussian distribution to 50.0), Gaussian x2 offset centre -- [util](scripts/simulators/util.py): (no summary in script docstring) diff --git a/notebooks/simulators/simulators.ipynb b/notebooks/simulators/simulators.ipynb index 2a02e79..8deaf49 100644 --- a/notebooks/simulators/simulators.ipynb +++ b/notebooks/simulators/simulators.ipynb @@ -76,7 +76,6 @@ "\n", "# from autofit import setup_notebook; setup_notebook()\n", "\n", - "import util\n", "from os import path\n", "\n", "import autofit as af" @@ -97,7 +96,9 @@ "source": [ "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x1\")\n", "gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=10.0)\n", - "util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path)" + "af.ex.util.simulate_dataset_1d_via_gaussian_from(\n", + " gaussian=gaussian, dataset_path=dataset_path\n", + ")" ], "outputs": [], "execution_count": null @@ -115,7 +116,9 @@ "source": [ "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x1_0\")\n", "gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=1.0)\n", - "util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path)" + "af.ex.util.simulate_dataset_1d_via_gaussian_from(\n", + " gaussian=gaussian, dataset_path=dataset_path\n", + ")" ], "outputs": [], "execution_count": null @@ -133,7 +136,9 @@ "source": [ "gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=5.0)\n", "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x1_1\")\n", - "util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path)" + "af.ex.util.simulate_dataset_1d_via_gaussian_from(\n", + " gaussian=gaussian, dataset_path=dataset_path\n", + ")" ], "outputs": [], "execution_count": null @@ -151,7 +156,9 @@ "source": [ "gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=10.0)\n", "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x1_2\")\n", - "util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path)" + "af.ex.util.simulate_dataset_1d_via_gaussian_from(\n", + " gaussian=gaussian, dataset_path=dataset_path\n", + ")" ], "outputs": [], "execution_count": null @@ -169,7 +176,9 @@ "source": [ "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x1_identical_0\")\n", "gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=10.0)\n", - "util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path)" + "af.ex.util.simulate_dataset_1d_via_gaussian_from(\n", + " gaussian=gaussian, dataset_path=dataset_path\n", + ")" ], "outputs": [], "execution_count": null @@ -187,7 +196,9 @@ "source": [ "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x1_identical_1\")\n", "gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=10.0)\n", - "util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path)" + "af.ex.util.simulate_dataset_1d_via_gaussian_from(\n", + " gaussian=gaussian, dataset_path=dataset_path\n", + ")" ], "outputs": [], "execution_count": null @@ -205,7 +216,9 @@ "source": [ "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x1_identical_2\")\n", "gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=10.0)\n", - "util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path)" + "af.ex.util.simulate_dataset_1d_via_gaussian_from(\n", + " gaussian=gaussian, dataset_path=dataset_path\n", + ")" ], "outputs": [], "execution_count": null @@ -224,7 +237,7 @@ "gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=10.0)\n", "exponential = af.ex.Exponential(centre=50.0, normalization=40.0, rate=0.05)\n", "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x1__exponential_x1\")\n", - "util.simulate_dataset_1d_via_profile_1d_list_from(\n", + "af.ex.util.simulate_dataset_1d_via_profile_1d_list_from(\n", " profile_1d_list=[gaussian, exponential], dataset_path=dataset_path\n", ")" ], @@ -246,7 +259,7 @@ "gaussian_1 = af.ex.Gaussian(centre=20.0, normalization=30.0, sigma=5.0)\n", "exponential = af.ex.Exponential(centre=70.0, normalization=40.0, rate=0.005)\n", "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x2__exponential_x1\")\n", - "util.simulate_dataset_1d_via_profile_1d_list_from(\n", + "af.ex.util.simulate_dataset_1d_via_profile_1d_list_from(\n", " profile_1d_list=[gaussian_0, gaussian_1, exponential], dataset_path=dataset_path\n", ")" ], @@ -267,7 +280,7 @@ "gaussian_0 = af.ex.Gaussian(centre=50.0, normalization=20.0, sigma=1.0)\n", "gaussian_1 = af.ex.Gaussian(centre=50.0, normalization=40.0, sigma=5.0)\n", "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x2\")\n", - "util.simulate_dataset_1d_via_profile_1d_list_from(\n", + "af.ex.util.simulate_dataset_1d_via_profile_1d_list_from(\n", " profile_1d_list=[gaussian_0, gaussian_1], dataset_path=dataset_path\n", ")" ], @@ -289,7 +302,7 @@ "gaussian_1 = af.ex.Gaussian(centre=50.0, normalization=40.0, sigma=5.0)\n", "gaussian_2 = af.ex.Gaussian(centre=50.0, normalization=60.0, sigma=10.0)\n", "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x3\")\n", - "util.simulate_dataset_1d_via_profile_1d_list_from(\n", + "af.ex.util.simulate_dataset_1d_via_profile_1d_list_from(\n", " profile_1d_list=[gaussian_0, gaussian_1, gaussian_2], dataset_path=dataset_path\n", ")" ], @@ -313,7 +326,7 @@ "gaussian_3 = af.ex.Gaussian(centre=50.0, normalization=80.0, sigma=15.0)\n", "gaussian_4 = af.ex.Gaussian(centre=50.0, normalization=100.0, sigma=20.0)\n", "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x5\")\n", - "util.simulate_dataset_1d_via_profile_1d_list_from(\n", + "af.ex.util.simulate_dataset_1d_via_profile_1d_list_from(\n", " profile_1d_list=[gaussian_0, gaussian_1, gaussian_2, gaussian_3, gaussian_4],\n", " dataset_path=dataset_path,\n", ")" @@ -334,7 +347,9 @@ "source": [ "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x1_unconvolved\")\n", "gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=3.0)\n", - "util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path)" + "af.ex.util.simulate_dataset_1d_via_gaussian_from(\n", + " gaussian=gaussian, dataset_path=dataset_path\n", + ")" ], "outputs": [], "execution_count": null @@ -352,7 +367,7 @@ "source": [ "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x1_convolved\")\n", "gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=3.0)\n", - "util.simulate_data_1d_with_kernel_via_gaussian_from(\n", + "af.ex.util.simulate_data_1d_with_kernel_via_gaussian_from(\n", " gaussian=gaussian, dataset_path=dataset_path\n", ")" ], @@ -373,7 +388,7 @@ "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x1_with_feature\")\n", "gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=10.0)\n", "gaussian_feature = af.ex.Gaussian(centre=70.0, normalization=0.3, sigma=0.5)\n", - "util.simulate_dataset_1d_via_profile_1d_list_from(\n", + "af.ex.util.simulate_dataset_1d_via_profile_1d_list_from(\n", " profile_1d_list=[gaussian, gaussian_feature], dataset_path=dataset_path\n", ")" ], @@ -394,7 +409,7 @@ "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x2_split\")\n", "gaussian_0 = af.ex.Gaussian(centre=25.0, normalization=50.0, sigma=12.5)\n", "gaussian_1 = af.ex.Gaussian(centre=75.0, normalization=50.0, sigma=12.5)\n", - "util.simulate_dataset_1d_via_profile_1d_list_from(\n", + "af.ex.util.simulate_dataset_1d_via_profile_1d_list_from(\n", " profile_1d_list=[gaussian_0, gaussian_1], dataset_path=dataset_path\n", ")\n" ], @@ -414,15 +429,21 @@ "source": [ "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x1_time\", \"time_0\")\n", "gaussian = af.ex.Gaussian(centre=40.0, normalization=50.0, sigma=20.0)\n", - "util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path)\n", + "af.ex.util.simulate_dataset_1d_via_gaussian_from(\n", + " gaussian=gaussian, dataset_path=dataset_path\n", + ")\n", "\n", "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x1_time\", \"time_1\")\n", "gaussian = af.ex.Gaussian(centre=50.0, normalization=50.0, sigma=20.0)\n", - "util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path)\n", + "af.ex.util.simulate_dataset_1d_via_gaussian_from(\n", + " gaussian=gaussian, dataset_path=dataset_path\n", + ")\n", "\n", "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x1_time\", \"time_2\")\n", "gaussian = af.ex.Gaussian(centre=60.0, normalization=50.0, sigma=20.0)\n", - "util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path)\n" + "af.ex.util.simulate_dataset_1d_via_gaussian_from(\n", + " gaussian=gaussian, dataset_path=dataset_path\n", + ")\n" ], "outputs": [], "execution_count": null @@ -440,15 +461,21 @@ "source": [ "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x1_variable\", \"sigma_0\")\n", "gaussian = af.ex.Gaussian(centre=50.0, normalization=50.0, sigma=10.0)\n", - "util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path)\n", + "af.ex.util.simulate_dataset_1d_via_gaussian_from(\n", + " gaussian=gaussian, dataset_path=dataset_path\n", + ")\n", "\n", "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x1_variable\", \"sigma_1\")\n", "gaussian = af.ex.Gaussian(centre=50.0, normalization=50.0, sigma=20.0)\n", - "util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path)\n", + "af.ex.util.simulate_dataset_1d_via_gaussian_from(\n", + " gaussian=gaussian, dataset_path=dataset_path\n", + ")\n", "\n", "dataset_path = path.join(\"dataset\", \"example_1d\", \"gaussian_x1_variable\", \"sigma_2\")\n", "gaussian = af.ex.Gaussian(centre=50.0, normalization=50.0, sigma=30.0)\n", - "util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path)" + "af.ex.util.simulate_dataset_1d_via_gaussian_from(\n", + " gaussian=gaussian, dataset_path=dataset_path\n", + ")" ], "outputs": [], "execution_count": null @@ -460,7 +487,12 @@ "__Sample Datasets (Chapter 3)__\n", "\n", "Chain-run the sample simulator so every tutorial's `simulators.py` subprocess\n", - "call also produces the `dataset_*/` sample folders used by chapter 3." + "call also produces the `dataset_*/` sample folders used by chapter 3.\n", + "\n", + "The path is relative to the repository root, which every execution context sets as the working\n", + "directory: scripts are run from the root, the notebook kernel is pinned there, and Colab chdirs\n", + "there. It must not be derived from the script's own file path, which a notebook kernel does not\n", + "define." ] }, { @@ -470,7 +502,7 @@ "import runpy\n", "\n", "runpy.run_path(\n", - " path.join(path.dirname(path.abspath(__file__)), \"simulators_sample.py\"),\n", + " path.join(\"scripts\", \"simulators\", \"simulators_sample.py\"),\n", " run_name=\"__main__\",\n", ")\n" ], diff --git a/notebooks/simulators/simulators_sample.ipynb b/notebooks/simulators/simulators_sample.ipynb index 384c5b7..18e0da0 100644 --- a/notebooks/simulators/simulators_sample.ipynb +++ b/notebooks/simulators/simulators_sample.ipynb @@ -66,8 +66,7 @@ "import numpy as np\n", "from os import path\n", "\n", - "import autofit as af\n", - "import util" + "import autofit as af" ], "outputs": [], "execution_count": null @@ -93,7 +92,7 @@ " \"dataset\", \"example_1d\", f\"gaussian_x1__low_snr\", f\"dataset_{i}\"\n", " )\n", " gaussian = af.ex.Gaussian(centre=50.0, normalization=0.5, sigma=5.0)\n", - " util.simulate_dataset_1d_via_gaussian_from(\n", + " af.ex.util.simulate_dataset_1d_via_gaussian_from(\n", " gaussian=gaussian, dataset_path=dataset_path\n", " )" ], @@ -133,7 +132,7 @@ "\n", " gaussian = gaussian_parent_model.random_instance()\n", "\n", - " util.simulate_dataset_1d_via_gaussian_from(\n", + " af.ex.util.simulate_dataset_1d_via_gaussian_from(\n", " gaussian=gaussian, dataset_path=dataset_path\n", " )\n" ], @@ -184,7 +183,7 @@ " gaussian_0 = af.ex.Gaussian(centre=40.0, normalization=1.0, sigma=sigma_0_value)\n", " gaussian_1 = af.ex.Gaussian(centre=60.0, normalization=1.0, sigma=sigma_1_value)\n", "\n", - " util.simulate_dataset_1d_via_profile_1d_list_from(\n", + " af.ex.util.simulate_dataset_1d_via_profile_1d_list_from(\n", " profile_1d_list=[gaussian_0, gaussian_1], dataset_path=dataset_path\n", " )\n" ], diff --git a/notebooks/simulators/util.ipynb b/notebooks/simulators/util.ipynb deleted file mode 100644 index 5db1bbe..0000000 --- a/notebooks/simulators/util.ipynb +++ /dev/null @@ -1,418 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "__Google Colab Setup__\n", - "\n", - "This cell sets up the environment when the notebook is run on Google Colab: it installs the\n", - "required PyAuto packages, clones the workspace (configuration files and example datasets) and\n", - "points the configuration at it. If you are running the notebook elsewhere (e.g. locally via\n", - "your own installation) it does nothing, and you can run it safely.\n", - "\n", - "Colab tip: model-fits run much faster on a GPU \u2014 enable one via \"Runtime\" -> \"Change runtime\n", - "type\" -> \"Hardware accelerator\" before running the notebook." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "try:\n", - " import google.colab\n", - "except ImportError:\n", - " from autofit import setup_colab as _setup_colab\n", - "else:\n", - " import importlib\n", - " import subprocess\n", - " import sys\n", - "\n", - " subprocess.check_call(\n", - " [sys.executable, \"-m\", \"pip\", \"install\", \"autonerves\", \"--no-deps\"]\n", - " )\n", - " _setup_colab = importlib.import_module(\"autonerves.setup_colab\")\n", - "\n", - "_setup_colab.setup(\"howtofit\")" - ] - }, - { - "cell_type": "code", - "metadata": {}, - "source": [ - "from autofit import to_dict\n", - "\n", - "import autofit as af\n", - "\n", - "import json\n", - "from os import path\n", - "import numpy as np\n", - "import matplotlib.pyplot as plt\n", - "\n", - "\n", - "def simulate_dataset_1d_via_gaussian_from(gaussian, dataset_path):\n", - " \"\"\"\n", - " Specify the number of pixels used to create the xvalues on which the 1D line of the profile is generated using and\n", - " thus defining the number of data-points in our data.\n", - " \"\"\"\n", - " pixels = 100\n", - " xvalues = np.arange(pixels)\n", - "\n", - " \"\"\"\n", - " Evaluate this `Gaussian` model instance at every xvalues to create its model profile.\n", - " \"\"\"\n", - " model_data_1d = gaussian.model_data_from(xvalues=xvalues)\n", - "\n", - " \"\"\"\n", - " Determine the noise (at a specified signal to noise level) in every pixel of our model profile.\n", - " \"\"\"\n", - " signal_to_noise_ratio = 25.0\n", - " noise = np.random.normal(0.0, 1.0 / signal_to_noise_ratio, pixels)\n", - "\n", - " \"\"\"\n", - " Add this noise to the model line to create the line data that is fitted, using the signal-to-noise ratio to compute\n", - " noise-map of our data which is required when evaluating the chi-squared value of the likelihood.\n", - " \"\"\"\n", - " data = model_data_1d + noise\n", - " noise_map = (1.0 / signal_to_noise_ratio) * np.ones(pixels)\n", - "\n", - " \"\"\"\n", - " Output the data and noise-map to the `HowToFit/dataset` folder so they can be loaded and used \n", - " in other example scripts.\n", - " \"\"\"\n", - " af.util.numpy_array_to_json(\n", - " array=data, file_path=path.join(dataset_path, \"data.json\"), overwrite=True\n", - " )\n", - " af.util.numpy_array_to_json(\n", - " array=noise_map,\n", - " file_path=path.join(dataset_path, \"noise_map.json\"),\n", - " overwrite=True,\n", - " )\n", - " plt.errorbar(\n", - " x=xvalues,\n", - " y=data,\n", - " yerr=noise_map,\n", - " linestyle=\"\",\n", - " color=\"k\",\n", - " ecolor=\"k\",\n", - " elinewidth=1,\n", - " capsize=2,\n", - " )\n", - " plt.title(\"1D Gaussian Dataset.\")\n", - " plt.xlabel(\"x values of profile\")\n", - " plt.ylabel(\"Profile normalization\")\n", - " plt.savefig(path.join(dataset_path, \"image.png\"))\n", - " plt.close()\n", - "\n", - " \"\"\"\n", - " __Model Json__\n", - " \n", - " Output the model to a .json file so we can refer to its parameters in the future.\n", - " \"\"\"\n", - " model_file = path.join(dataset_path, \"model.json\")\n", - "\n", - " with open(model_file, \"w+\") as f:\n", - " try:\n", - " json.dump(to_dict(gaussian), f, indent=4)\n", - " except (TypeError, ValueError):\n", - " pass\n", - "\n", - "\n", - "def simulate_data_1d_with_kernel_via_gaussian_from(gaussian, dataset_path):\n", - " \"\"\"\n", - " Specify the number of pixels used to create the xvalues on which the 1D line of the profile is generated using and\n", - " thus defining the number of data-points in our data.\n", - " \"\"\"\n", - " pixels = 100\n", - " xvalues = np.arange(pixels)\n", - "\n", - " \"\"\"\n", - " Evaluate this `Gaussian` model instance at every xvalues to create its model profile.\n", - " \"\"\"\n", - " model_data_1d = gaussian.model_data_from(xvalues=xvalues)\n", - "\n", - " \"\"\"\n", - " Determine the noise (at a specified signal to noise level) in every pixel of our model profile.\n", - " \"\"\"\n", - " kernel_pixels = 21\n", - " kernel_xvalues = np.arange(kernel_pixels)\n", - " kernel_sigma = 5.0\n", - " kernel_centre = 10.0\n", - " kernel_xvalues = np.subtract(kernel_xvalues, kernel_centre)\n", - " kernel = np.multiply(\n", - " np.divide(1.0, kernel_sigma * np.sqrt(2.0 * np.pi)),\n", - " np.exp(-0.5 * np.square(np.divide(kernel_xvalues, kernel_sigma))),\n", - " )\n", - " kernel = kernel / np.sum(kernel)\n", - "\n", - " \"\"\"\n", - " Convolve the model line with this kernel.\n", - " \"\"\"\n", - " blurred_model_data_1d = np.convolve(model_data_1d, kernel, mode=\"same\")\n", - "\n", - " \"\"\"\n", - " Create a Gaussian kernel which the model line will be convolved with.\n", - " \"\"\"\n", - " signal_to_noise_ratio = 25.0\n", - " noise = np.random.normal(0.0, 1.0 / signal_to_noise_ratio, pixels)\n", - "\n", - " \"\"\"\n", - " Add this noise to the model line to create the line data that is fitted, using the signal-to-noise ratio to compute\n", - " noise-map of our data which is required when evaluating the chi-squared value of the likelihood.\n", - " \"\"\"\n", - " data = blurred_model_data_1d + noise\n", - " noise_map = (1.0 / signal_to_noise_ratio) * np.ones(pixels)\n", - "\n", - " \"\"\"\n", - " Output the data and noise-map to the `HowToFit/dataset` folder so they can be loaded and used \n", - " in other example scripts.\n", - " \"\"\"\n", - " af.util.numpy_array_to_json(\n", - " array=data, file_path=path.join(dataset_path, \"data.json\"), overwrite=True\n", - " )\n", - " af.util.numpy_array_to_json(\n", - " array=noise_map,\n", - " file_path=path.join(dataset_path, \"noise_map.json\"),\n", - " overwrite=True,\n", - " )\n", - " af.util.numpy_array_to_json(\n", - " array=kernel, file_path=path.join(dataset_path, \"kernel.json\"), overwrite=True\n", - " )\n", - " plt.errorbar(\n", - " x=xvalues,\n", - " y=data,\n", - " yerr=noise_map,\n", - " linestyle=\"\",\n", - " color=\"k\",\n", - " ecolor=\"k\",\n", - " elinewidth=1,\n", - " capsize=2,\n", - " )\n", - " plt.title(\"1D Gaussian Dataset with Convolver Blurring.\")\n", - " plt.xlabel(\"x values of profile\")\n", - " plt.ylabel(\"Profile normalization\")\n", - " plt.savefig(path.join(dataset_path, \"image.png\"))\n", - " plt.close()\n", - "\n", - " \"\"\"\n", - " __Model Json__\n", - "\n", - " Output the model to a .json file so we can refer to its parameters in the future.\n", - " \"\"\"\n", - " model_file = path.join(dataset_path, \"model.json\")\n", - "\n", - " with open(model_file, \"w+\") as f:\n", - " json.dump(to_dict(gaussian), f, indent=4)\n", - "\n", - "\n", - "def simulate_dataset_1d_via_profile_1d_list_from(profile_1d_list, dataset_path):\n", - " \"\"\"\n", - " Specify the number of pixels used to create the xvalues on which the 1D line of the profile is generated using and\n", - " thus defining the number of data-points in our data.\n", - " \"\"\"\n", - " pixels = 100\n", - " xvalues = np.arange(pixels)\n", - "\n", - " \"\"\"\n", - " Evaluate the `Gaussian` and Exponential model instances at every xvalues to create their model profile and sum\n", - " them together to create the overall model profile.\n", - " \"\"\"\n", - " model_data_1d_list = [\n", - " profile_1d.model_data_from(xvalues=xvalues) for profile_1d in profile_1d_list\n", - " ]\n", - "\n", - " model_data_1d = sum(model_data_1d_list)\n", - "\n", - " \"\"\"\n", - " Determine the noise (at a specified signal to noise level) in every pixel of our model profile.\n", - " \"\"\"\n", - " signal_to_noise_ratio = 25.0\n", - " noise = np.random.normal(0.0, 1.0 / signal_to_noise_ratio, pixels)\n", - "\n", - " \"\"\"\n", - " Add this noise to the model line to create the line data that is fitted, using the signal-to-noise ratio to compute\n", - " noise-map of our data which is required when evaluating the chi-squared value of the likelihood.\n", - " \"\"\"\n", - " data = model_data_1d + noise\n", - " noise_map = (1.0 / signal_to_noise_ratio) * np.ones(pixels)\n", - "\n", - " \"\"\"\n", - " Output the data and noise-map to the `HowToFit/dataset` folder so they can be loaded and used \n", - " in other example scripts.\n", - " \"\"\"\n", - " af.util.numpy_array_to_json(\n", - " array=data, file_path=path.join(dataset_path, \"data.json\"), overwrite=True\n", - " )\n", - " af.util.numpy_array_to_json(\n", - " array=noise_map,\n", - " file_path=path.join(dataset_path, \"noise_map.json\"),\n", - " overwrite=True,\n", - " )\n", - " plt.errorbar(\n", - " x=xvalues,\n", - " y=data,\n", - " yerr=noise_map,\n", - " linestyle=\"\",\n", - " color=\"k\",\n", - " ecolor=\"k\",\n", - " elinewidth=1,\n", - " capsize=2,\n", - " )\n", - " plt.plot(range(data.shape[0]), model_data_1d, color=\"r\")\n", - " for model_data_1d_individual in model_data_1d_list:\n", - " plt.plot(range(data.shape[0]), model_data_1d_individual, \"--\")\n", - " plt.title(\"1D Profiles Dataset.\")\n", - " plt.xlabel(\"x values of profile\")\n", - " plt.ylabel(\"Profile normalization\")\n", - " plt.savefig(path.join(dataset_path, \"image.png\"))\n", - " plt.close()\n", - "\n", - " \"\"\"\n", - " __Model Json__\n", - "\n", - " Output the model to a .json file so we can refer to its parameters in the future.\n", - " \"\"\"\n", - " for i, profile in enumerate(profile_1d_list):\n", - " model_file = path.join(dataset_path, f\"model_{i}.json\")\n", - "\n", - " with open(model_file, \"w+\") as f:\n", - " try:\n", - " json.dump(to_dict(profile), f, indent=4)\n", - " except (TypeError, ValueError):\n", - " pass\n", - "\n", - " \"\"\"\n", - " __Max Log Likelihood__\n", - " \"\"\"\n", - " chi_squared = np.sum(((data - model_data_1d) / noise_map) ** 2)\n", - " noise_normalization = np.sum(np.log(2 * np.pi * noise_map**2.0))\n", - " log_likelihood = -0.5 * (chi_squared + noise_normalization)\n", - "\n", - " with open(path.join(dataset_path, \"max_log_likelihood.json\"), \"w+\") as f:\n", - " json.dump({\"log_likelihood\": log_likelihood}, f, indent=4)\n", - "\n", - "\n", - "def simulate_data_1d_with_kernel_via_profile_1d_list_from(\n", - " profile_1d_list, dataset_path\n", - "):\n", - " \"\"\"\n", - " Specify the number of pixels used to create the xvalues on which the 1D line of the profile is generated using and\n", - " thus defining the number of data-points in our data.\n", - " \"\"\"\n", - " pixels = 100\n", - " xvalues = np.arange(pixels)\n", - "\n", - " \"\"\"\n", - " Evaluate the `Gaussian` and Exponential model instances at every xvalues to create their model profile and sum\n", - " them together to create the overall model profile.\n", - " \"\"\"\n", - " model_data_1d = np.zeros(shape=pixels)\n", - "\n", - " for profile in profile_1d_list:\n", - " model_data_1d += profile.model_data_from(xvalues=xvalues)\n", - "\n", - " \"\"\"\n", - " Create a Gaussian kernel which the model line will be convolved with.\n", - " \"\"\"\n", - " kernel_pixels = 21\n", - " kernel_xvalues = np.arange(kernel_pixels)\n", - " kernel_sigma = 5.0\n", - " kernel_centre = 10.0\n", - " kernel_xvalues = np.subtract(kernel_xvalues, kernel_centre)\n", - " kernel = np.multiply(\n", - " np.divide(1.0, kernel_sigma * np.sqrt(2.0 * np.pi)),\n", - " np.exp(-0.5 * np.square(np.divide(kernel_xvalues, kernel_sigma))),\n", - " )\n", - " kernel = kernel / np.sum(kernel)\n", - "\n", - " \"\"\"\n", - " Convolve the model line with this kernel.\n", - " \"\"\"\n", - "\n", - " blurred_model_data_1d = np.convolve(model_data_1d, kernel, mode=\"same\")\n", - "\n", - " \"\"\"\n", - " Determine the noise (at a specified signal to noise level) in every pixel of our model profile.\n", - " \"\"\"\n", - " signal_to_noise_ratio = 25.0\n", - " noise = np.random.normal(0.0, 1.0 / signal_to_noise_ratio, pixels)\n", - "\n", - " \"\"\"\n", - " Add this noise to the model line to create the line data that is fitted, using the signal-to-noise ratio to compute\n", - " noise-map of our data which is required when evaluating the chi-squared value of the likelihood.\n", - " \"\"\"\n", - " data = blurred_model_data_1d + noise\n", - " noise_map = (1.0 / signal_to_noise_ratio) * np.ones(pixels)\n", - "\n", - " \"\"\"\n", - " Output the data and noise-map to the `HowToFit/dataset` folder so they can be loaded and used \n", - " in other example scripts.\n", - " \"\"\"\n", - " af.util.numpy_array_to_json(\n", - " array=data, file_path=path.join(dataset_path, \"data.json\"), overwrite=True\n", - " )\n", - " af.util.numpy_array_to_json(\n", - " array=noise_map,\n", - " file_path=path.join(dataset_path, \"noise_map.json\"),\n", - " overwrite=True,\n", - " )\n", - " af.util.numpy_array_to_json(\n", - " array=kernel, file_path=path.join(dataset_path, \"kernel.json\"), overwrite=True\n", - " )\n", - "\n", - " plt.errorbar(\n", - " x=xvalues,\n", - " y=data,\n", - " yerr=noise_map,\n", - " linestyle=\"\",\n", - " color=\"k\",\n", - " ecolor=\"k\",\n", - " elinewidth=1,\n", - " capsize=2,\n", - " )\n", - " plt.title(\"1D Profiles Dataset with Convolver Blurring.\")\n", - " plt.xlabel(\"x values of profile\")\n", - " plt.ylabel(\"Profile normalization\")\n", - " plt.savefig(path.join(dataset_path, \"image.png\"))\n", - " plt.close()\n", - "\n", - " \"\"\"\n", - " __Model Json__\n", - "\n", - " Output the model to a .json file so we can refer to its parameters in the future.\n", - " \"\"\"\n", - " for i, profile in enumerate(profile_1d_list):\n", - " model_file = path.join(dataset_path, f\"model_{i}.json\")\n", - "\n", - " with open(model_file, \"w+\") as f:\n", - " json.dump(to_dict(profile), f, indent=4)\n" - ], - "outputs": [], - "execution_count": null - } - ], - "metadata": { - "anaconda-cloud": {}, - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "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.6.1" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} \ No newline at end of file diff --git a/scripts/simulators/simulators.py b/scripts/simulators/simulators.py index 422c4c5..ee3d269 100644 --- a/scripts/simulators/simulators.py +++ b/scripts/simulators/simulators.py @@ -28,7 +28,6 @@ # from autofit import setup_notebook; setup_notebook() -import util from os import path import autofit as af @@ -38,49 +37,63 @@ """ dataset_path = path.join("dataset", "example_1d", "gaussian_x1") gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=10.0) -util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path) +af.ex.util.simulate_dataset_1d_via_gaussian_from( + gaussian=gaussian, dataset_path=dataset_path +) """ __Gaussian x1 (0)__ """ dataset_path = path.join("dataset", "example_1d", "gaussian_x1_0") gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=1.0) -util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path) +af.ex.util.simulate_dataset_1d_via_gaussian_from( + gaussian=gaussian, dataset_path=dataset_path +) """ __Gaussian x1 (1)__ """ gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=5.0) dataset_path = path.join("dataset", "example_1d", "gaussian_x1_1") -util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path) +af.ex.util.simulate_dataset_1d_via_gaussian_from( + gaussian=gaussian, dataset_path=dataset_path +) """ __Gaussian x1 (2)__ """ gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=10.0) dataset_path = path.join("dataset", "example_1d", "gaussian_x1_2") -util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path) +af.ex.util.simulate_dataset_1d_via_gaussian_from( + gaussian=gaussian, dataset_path=dataset_path +) """ __Gaussian x1 (Identical 0)__ """ dataset_path = path.join("dataset", "example_1d", "gaussian_x1_identical_0") gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=10.0) -util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path) +af.ex.util.simulate_dataset_1d_via_gaussian_from( + gaussian=gaussian, dataset_path=dataset_path +) """ __Gaussian x1 (Identical 1)__ """ dataset_path = path.join("dataset", "example_1d", "gaussian_x1_identical_1") gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=10.0) -util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path) +af.ex.util.simulate_dataset_1d_via_gaussian_from( + gaussian=gaussian, dataset_path=dataset_path +) """ __Gaussian x1 (Identical 2)__ """ dataset_path = path.join("dataset", "example_1d", "gaussian_x1_identical_2") gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=10.0) -util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path) +af.ex.util.simulate_dataset_1d_via_gaussian_from( + gaussian=gaussian, dataset_path=dataset_path +) """ __Gaussian x1 + Exponential x1__ @@ -88,7 +101,7 @@ gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=10.0) exponential = af.ex.Exponential(centre=50.0, normalization=40.0, rate=0.05) dataset_path = path.join("dataset", "example_1d", "gaussian_x1__exponential_x1") -util.simulate_dataset_1d_via_profile_1d_list_from( +af.ex.util.simulate_dataset_1d_via_profile_1d_list_from( profile_1d_list=[gaussian, exponential], dataset_path=dataset_path ) @@ -99,7 +112,7 @@ gaussian_1 = af.ex.Gaussian(centre=20.0, normalization=30.0, sigma=5.0) exponential = af.ex.Exponential(centre=70.0, normalization=40.0, rate=0.005) dataset_path = path.join("dataset", "example_1d", "gaussian_x2__exponential_x1") -util.simulate_dataset_1d_via_profile_1d_list_from( +af.ex.util.simulate_dataset_1d_via_profile_1d_list_from( profile_1d_list=[gaussian_0, gaussian_1, exponential], dataset_path=dataset_path ) @@ -109,7 +122,7 @@ gaussian_0 = af.ex.Gaussian(centre=50.0, normalization=20.0, sigma=1.0) gaussian_1 = af.ex.Gaussian(centre=50.0, normalization=40.0, sigma=5.0) dataset_path = path.join("dataset", "example_1d", "gaussian_x2") -util.simulate_dataset_1d_via_profile_1d_list_from( +af.ex.util.simulate_dataset_1d_via_profile_1d_list_from( profile_1d_list=[gaussian_0, gaussian_1], dataset_path=dataset_path ) @@ -120,7 +133,7 @@ gaussian_1 = af.ex.Gaussian(centre=50.0, normalization=40.0, sigma=5.0) gaussian_2 = af.ex.Gaussian(centre=50.0, normalization=60.0, sigma=10.0) dataset_path = path.join("dataset", "example_1d", "gaussian_x3") -util.simulate_dataset_1d_via_profile_1d_list_from( +af.ex.util.simulate_dataset_1d_via_profile_1d_list_from( profile_1d_list=[gaussian_0, gaussian_1, gaussian_2], dataset_path=dataset_path ) @@ -133,7 +146,7 @@ gaussian_3 = af.ex.Gaussian(centre=50.0, normalization=80.0, sigma=15.0) gaussian_4 = af.ex.Gaussian(centre=50.0, normalization=100.0, sigma=20.0) dataset_path = path.join("dataset", "example_1d", "gaussian_x5") -util.simulate_dataset_1d_via_profile_1d_list_from( +af.ex.util.simulate_dataset_1d_via_profile_1d_list_from( profile_1d_list=[gaussian_0, gaussian_1, gaussian_2, gaussian_3, gaussian_4], dataset_path=dataset_path, ) @@ -143,14 +156,16 @@ """ dataset_path = path.join("dataset", "example_1d", "gaussian_x1_unconvolved") gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=3.0) -util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path) +af.ex.util.simulate_dataset_1d_via_gaussian_from( + gaussian=gaussian, dataset_path=dataset_path +) """ __Gaussian x1 convolved__ """ dataset_path = path.join("dataset", "example_1d", "gaussian_x1_convolved") gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=3.0) -util.simulate_data_1d_with_kernel_via_gaussian_from( +af.ex.util.simulate_data_1d_with_kernel_via_gaussian_from( gaussian=gaussian, dataset_path=dataset_path ) @@ -160,7 +175,7 @@ dataset_path = path.join("dataset", "example_1d", "gaussian_x1_with_feature") gaussian = af.ex.Gaussian(centre=50.0, normalization=25.0, sigma=10.0) gaussian_feature = af.ex.Gaussian(centre=70.0, normalization=0.3, sigma=0.5) -util.simulate_dataset_1d_via_profile_1d_list_from( +af.ex.util.simulate_dataset_1d_via_profile_1d_list_from( profile_1d_list=[gaussian, gaussian_feature], dataset_path=dataset_path ) @@ -170,7 +185,7 @@ dataset_path = path.join("dataset", "example_1d", "gaussian_x2_split") gaussian_0 = af.ex.Gaussian(centre=25.0, normalization=50.0, sigma=12.5) gaussian_1 = af.ex.Gaussian(centre=75.0, normalization=50.0, sigma=12.5) -util.simulate_dataset_1d_via_profile_1d_list_from( +af.ex.util.simulate_dataset_1d_via_profile_1d_list_from( profile_1d_list=[gaussian_0, gaussian_1], dataset_path=dataset_path ) @@ -180,15 +195,21 @@ """ dataset_path = path.join("dataset", "example_1d", "gaussian_x1_time", "time_0") gaussian = af.ex.Gaussian(centre=40.0, normalization=50.0, sigma=20.0) -util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path) +af.ex.util.simulate_dataset_1d_via_gaussian_from( + gaussian=gaussian, dataset_path=dataset_path +) dataset_path = path.join("dataset", "example_1d", "gaussian_x1_time", "time_1") gaussian = af.ex.Gaussian(centre=50.0, normalization=50.0, sigma=20.0) -util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path) +af.ex.util.simulate_dataset_1d_via_gaussian_from( + gaussian=gaussian, dataset_path=dataset_path +) dataset_path = path.join("dataset", "example_1d", "gaussian_x1_time", "time_2") gaussian = af.ex.Gaussian(centre=60.0, normalization=50.0, sigma=20.0) -util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path) +af.ex.util.simulate_dataset_1d_via_gaussian_from( + gaussian=gaussian, dataset_path=dataset_path +) """ @@ -196,25 +217,36 @@ """ dataset_path = path.join("dataset", "example_1d", "gaussian_x1_variable", "sigma_0") gaussian = af.ex.Gaussian(centre=50.0, normalization=50.0, sigma=10.0) -util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path) +af.ex.util.simulate_dataset_1d_via_gaussian_from( + gaussian=gaussian, dataset_path=dataset_path +) dataset_path = path.join("dataset", "example_1d", "gaussian_x1_variable", "sigma_1") gaussian = af.ex.Gaussian(centre=50.0, normalization=50.0, sigma=20.0) -util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path) +af.ex.util.simulate_dataset_1d_via_gaussian_from( + gaussian=gaussian, dataset_path=dataset_path +) dataset_path = path.join("dataset", "example_1d", "gaussian_x1_variable", "sigma_2") gaussian = af.ex.Gaussian(centre=50.0, normalization=50.0, sigma=30.0) -util.simulate_dataset_1d_via_gaussian_from(gaussian=gaussian, dataset_path=dataset_path) +af.ex.util.simulate_dataset_1d_via_gaussian_from( + gaussian=gaussian, dataset_path=dataset_path +) """ __Sample Datasets (Chapter 3)__ Chain-run the sample simulator so every tutorial's `simulators.py` subprocess call also produces the `dataset_*/` sample folders used by chapter 3. + +The path is relative to the repository root, which every execution context sets as the working +directory: scripts are run from the root, the notebook kernel is pinned there, and Colab chdirs +there. It must not be derived from the script's own file path, which a notebook kernel does not +define. """ import runpy runpy.run_path( - path.join(path.dirname(path.abspath(__file__)), "simulators_sample.py"), + path.join("scripts", "simulators", "simulators_sample.py"), run_name="__main__", ) diff --git a/scripts/simulators/simulators_sample.py b/scripts/simulators/simulators_sample.py index 1a7621f..09f1056 100644 --- a/scripts/simulators/simulators_sample.py +++ b/scripts/simulators/simulators_sample.py @@ -19,7 +19,6 @@ from os import path import autofit as af -import util """ __Gaussian x1 low snr (centre fixed to 50.0)__ @@ -34,7 +33,7 @@ "dataset", "example_1d", f"gaussian_x1__low_snr", f"dataset_{i}" ) gaussian = af.ex.Gaussian(centre=50.0, normalization=0.5, sigma=5.0) - util.simulate_dataset_1d_via_gaussian_from( + af.ex.util.simulate_dataset_1d_via_gaussian_from( gaussian=gaussian, dataset_path=dataset_path ) @@ -63,7 +62,7 @@ gaussian = gaussian_parent_model.random_instance() - util.simulate_dataset_1d_via_gaussian_from( + af.ex.util.simulate_dataset_1d_via_gaussian_from( gaussian=gaussian, dataset_path=dataset_path ) @@ -104,6 +103,6 @@ gaussian_0 = af.ex.Gaussian(centre=40.0, normalization=1.0, sigma=sigma_0_value) gaussian_1 = af.ex.Gaussian(centre=60.0, normalization=1.0, sigma=sigma_1_value) - util.simulate_dataset_1d_via_profile_1d_list_from( + af.ex.util.simulate_dataset_1d_via_profile_1d_list_from( profile_1d_list=[gaussian_0, gaussian_1], dataset_path=dataset_path ) diff --git a/scripts/simulators/util.py b/scripts/simulators/util.py deleted file mode 100644 index f68b6bd..0000000 --- a/scripts/simulators/util.py +++ /dev/null @@ -1,346 +0,0 @@ -from autofit import to_dict - -import autofit as af - -import json -from os import path -import numpy as np -import matplotlib.pyplot as plt - - -def simulate_dataset_1d_via_gaussian_from(gaussian, dataset_path): - """ - Specify the number of pixels used to create the xvalues on which the 1D line of the profile is generated using and - thus defining the number of data-points in our data. - """ - pixels = 100 - xvalues = np.arange(pixels) - - """ - Evaluate this `Gaussian` model instance at every xvalues to create its model profile. - """ - model_data_1d = gaussian.model_data_from(xvalues=xvalues) - - """ - Determine the noise (at a specified signal to noise level) in every pixel of our model profile. - """ - signal_to_noise_ratio = 25.0 - noise = np.random.normal(0.0, 1.0 / signal_to_noise_ratio, pixels) - - """ - Add this noise to the model line to create the line data that is fitted, using the signal-to-noise ratio to compute - noise-map of our data which is required when evaluating the chi-squared value of the likelihood. - """ - data = model_data_1d + noise - noise_map = (1.0 / signal_to_noise_ratio) * np.ones(pixels) - - """ - Output the data and noise-map to the `HowToFit/dataset` folder so they can be loaded and used - in other example scripts. - """ - af.util.numpy_array_to_json( - array=data, file_path=path.join(dataset_path, "data.json"), overwrite=True - ) - af.util.numpy_array_to_json( - array=noise_map, - file_path=path.join(dataset_path, "noise_map.json"), - overwrite=True, - ) - plt.errorbar( - x=xvalues, - y=data, - yerr=noise_map, - linestyle="", - color="k", - ecolor="k", - elinewidth=1, - capsize=2, - ) - plt.title("1D Gaussian Dataset.") - plt.xlabel("x values of profile") - plt.ylabel("Profile normalization") - plt.savefig(path.join(dataset_path, "image.png")) - plt.close() - - """ - __Model Json__ - - Output the model to a .json file so we can refer to its parameters in the future. - """ - model_file = path.join(dataset_path, "model.json") - - with open(model_file, "w+") as f: - try: - json.dump(to_dict(gaussian), f, indent=4) - except (TypeError, ValueError): - pass - - -def simulate_data_1d_with_kernel_via_gaussian_from(gaussian, dataset_path): - """ - Specify the number of pixels used to create the xvalues on which the 1D line of the profile is generated using and - thus defining the number of data-points in our data. - """ - pixels = 100 - xvalues = np.arange(pixels) - - """ - Evaluate this `Gaussian` model instance at every xvalues to create its model profile. - """ - model_data_1d = gaussian.model_data_from(xvalues=xvalues) - - """ - Determine the noise (at a specified signal to noise level) in every pixel of our model profile. - """ - kernel_pixels = 21 - kernel_xvalues = np.arange(kernel_pixels) - kernel_sigma = 5.0 - kernel_centre = 10.0 - kernel_xvalues = np.subtract(kernel_xvalues, kernel_centre) - kernel = np.multiply( - np.divide(1.0, kernel_sigma * np.sqrt(2.0 * np.pi)), - np.exp(-0.5 * np.square(np.divide(kernel_xvalues, kernel_sigma))), - ) - kernel = kernel / np.sum(kernel) - - """ - Convolve the model line with this kernel. - """ - blurred_model_data_1d = np.convolve(model_data_1d, kernel, mode="same") - - """ - Create a Gaussian kernel which the model line will be convolved with. - """ - signal_to_noise_ratio = 25.0 - noise = np.random.normal(0.0, 1.0 / signal_to_noise_ratio, pixels) - - """ - Add this noise to the model line to create the line data that is fitted, using the signal-to-noise ratio to compute - noise-map of our data which is required when evaluating the chi-squared value of the likelihood. - """ - data = blurred_model_data_1d + noise - noise_map = (1.0 / signal_to_noise_ratio) * np.ones(pixels) - - """ - Output the data and noise-map to the `HowToFit/dataset` folder so they can be loaded and used - in other example scripts. - """ - af.util.numpy_array_to_json( - array=data, file_path=path.join(dataset_path, "data.json"), overwrite=True - ) - af.util.numpy_array_to_json( - array=noise_map, - file_path=path.join(dataset_path, "noise_map.json"), - overwrite=True, - ) - af.util.numpy_array_to_json( - array=kernel, file_path=path.join(dataset_path, "kernel.json"), overwrite=True - ) - plt.errorbar( - x=xvalues, - y=data, - yerr=noise_map, - linestyle="", - color="k", - ecolor="k", - elinewidth=1, - capsize=2, - ) - plt.title("1D Gaussian Dataset with Convolver Blurring.") - plt.xlabel("x values of profile") - plt.ylabel("Profile normalization") - plt.savefig(path.join(dataset_path, "image.png")) - plt.close() - - """ - __Model Json__ - - Output the model to a .json file so we can refer to its parameters in the future. - """ - model_file = path.join(dataset_path, "model.json") - - with open(model_file, "w+") as f: - json.dump(to_dict(gaussian), f, indent=4) - - -def simulate_dataset_1d_via_profile_1d_list_from(profile_1d_list, dataset_path): - """ - Specify the number of pixels used to create the xvalues on which the 1D line of the profile is generated using and - thus defining the number of data-points in our data. - """ - pixels = 100 - xvalues = np.arange(pixels) - - """ - Evaluate the `Gaussian` and Exponential model instances at every xvalues to create their model profile and sum - them together to create the overall model profile. - """ - model_data_1d_list = [ - profile_1d.model_data_from(xvalues=xvalues) for profile_1d in profile_1d_list - ] - - model_data_1d = sum(model_data_1d_list) - - """ - Determine the noise (at a specified signal to noise level) in every pixel of our model profile. - """ - signal_to_noise_ratio = 25.0 - noise = np.random.normal(0.0, 1.0 / signal_to_noise_ratio, pixels) - - """ - Add this noise to the model line to create the line data that is fitted, using the signal-to-noise ratio to compute - noise-map of our data which is required when evaluating the chi-squared value of the likelihood. - """ - data = model_data_1d + noise - noise_map = (1.0 / signal_to_noise_ratio) * np.ones(pixels) - - """ - Output the data and noise-map to the `HowToFit/dataset` folder so they can be loaded and used - in other example scripts. - """ - af.util.numpy_array_to_json( - array=data, file_path=path.join(dataset_path, "data.json"), overwrite=True - ) - af.util.numpy_array_to_json( - array=noise_map, - file_path=path.join(dataset_path, "noise_map.json"), - overwrite=True, - ) - plt.errorbar( - x=xvalues, - y=data, - yerr=noise_map, - linestyle="", - color="k", - ecolor="k", - elinewidth=1, - capsize=2, - ) - plt.plot(range(data.shape[0]), model_data_1d, color="r") - for model_data_1d_individual in model_data_1d_list: - plt.plot(range(data.shape[0]), model_data_1d_individual, "--") - plt.title("1D Profiles Dataset.") - plt.xlabel("x values of profile") - plt.ylabel("Profile normalization") - plt.savefig(path.join(dataset_path, "image.png")) - plt.close() - - """ - __Model Json__ - - Output the model to a .json file so we can refer to its parameters in the future. - """ - for i, profile in enumerate(profile_1d_list): - model_file = path.join(dataset_path, f"model_{i}.json") - - with open(model_file, "w+") as f: - try: - json.dump(to_dict(profile), f, indent=4) - except (TypeError, ValueError): - pass - - """ - __Max Log Likelihood__ - """ - chi_squared = np.sum(((data - model_data_1d) / noise_map) ** 2) - noise_normalization = np.sum(np.log(2 * np.pi * noise_map**2.0)) - log_likelihood = -0.5 * (chi_squared + noise_normalization) - - with open(path.join(dataset_path, "max_log_likelihood.json"), "w+") as f: - json.dump({"log_likelihood": log_likelihood}, f, indent=4) - - -def simulate_data_1d_with_kernel_via_profile_1d_list_from( - profile_1d_list, dataset_path -): - """ - Specify the number of pixels used to create the xvalues on which the 1D line of the profile is generated using and - thus defining the number of data-points in our data. - """ - pixels = 100 - xvalues = np.arange(pixels) - - """ - Evaluate the `Gaussian` and Exponential model instances at every xvalues to create their model profile and sum - them together to create the overall model profile. - """ - model_data_1d = np.zeros(shape=pixels) - - for profile in profile_1d_list: - model_data_1d += profile.model_data_from(xvalues=xvalues) - - """ - Create a Gaussian kernel which the model line will be convolved with. - """ - kernel_pixels = 21 - kernel_xvalues = np.arange(kernel_pixels) - kernel_sigma = 5.0 - kernel_centre = 10.0 - kernel_xvalues = np.subtract(kernel_xvalues, kernel_centre) - kernel = np.multiply( - np.divide(1.0, kernel_sigma * np.sqrt(2.0 * np.pi)), - np.exp(-0.5 * np.square(np.divide(kernel_xvalues, kernel_sigma))), - ) - kernel = kernel / np.sum(kernel) - - """ - Convolve the model line with this kernel. - """ - - blurred_model_data_1d = np.convolve(model_data_1d, kernel, mode="same") - - """ - Determine the noise (at a specified signal to noise level) in every pixel of our model profile. - """ - signal_to_noise_ratio = 25.0 - noise = np.random.normal(0.0, 1.0 / signal_to_noise_ratio, pixels) - - """ - Add this noise to the model line to create the line data that is fitted, using the signal-to-noise ratio to compute - noise-map of our data which is required when evaluating the chi-squared value of the likelihood. - """ - data = blurred_model_data_1d + noise - noise_map = (1.0 / signal_to_noise_ratio) * np.ones(pixels) - - """ - Output the data and noise-map to the `HowToFit/dataset` folder so they can be loaded and used - in other example scripts. - """ - af.util.numpy_array_to_json( - array=data, file_path=path.join(dataset_path, "data.json"), overwrite=True - ) - af.util.numpy_array_to_json( - array=noise_map, - file_path=path.join(dataset_path, "noise_map.json"), - overwrite=True, - ) - af.util.numpy_array_to_json( - array=kernel, file_path=path.join(dataset_path, "kernel.json"), overwrite=True - ) - - plt.errorbar( - x=xvalues, - y=data, - yerr=noise_map, - linestyle="", - color="k", - ecolor="k", - elinewidth=1, - capsize=2, - ) - plt.title("1D Profiles Dataset with Convolver Blurring.") - plt.xlabel("x values of profile") - plt.ylabel("Profile normalization") - plt.savefig(path.join(dataset_path, "image.png")) - plt.close() - - """ - __Model Json__ - - Output the model to a .json file so we can refer to its parameters in the future. - """ - for i, profile in enumerate(profile_1d_list): - model_file = path.join(dataset_path, f"model_{i}.json") - - with open(model_file, "w+") as f: - json.dump(to_dict(profile), f, indent=4) diff --git a/workspace_index.json b/workspace_index.json index c328c50..157c883 100644 --- a/workspace_index.json +++ b/workspace_index.json @@ -314,13 +314,5 @@ "path": "scripts/simulators/simulators_sample.py", "summary": "These scripts simulates many 1D Gaussian datasets with a low signal to noise ratio, which are used to demonstrate model-fitting.", "title": "__Simulators__" - }, - { - "contents": [], - "cross_refs": [], - "notebook": "notebooks/simulators/util.ipynb", - "path": "scripts/simulators/util.py", - "summary": "(no summary in script docstring)", - "title": "util" } ]