Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • 698042a
  • /
  • notebooks
  • /
  • simulators
  • /
  • simulators_sample.ipynb
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge Iframe embedding
swh:1:cnt:caceb27a88d1804bb4c9d42a7a8762c7f8285a16
directory badge Iframe embedding
swh:1:dir:3b4becb9805f5d7f3655fb7047f85f35d2277fa1

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
simulators_sample.ipynb
{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "__Simulators__\n",
        "\n",
        "These scripts simulates many 1D Gaussian datasets with a low signal to noise ratio, which are used to demonstrate\n",
        "model-fitting."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "%matplotlib inline\n",
        "from pyprojroot import here\n",
        "workspace_path = str(here())\n",
        "%cd $workspace_path\n",
        "print(f\"Working Directory has been set to `{workspace_path}`\")\n",
        "\n",
        "import numpy as np\n",
        "from os import path\n",
        "\n",
        "import autofit as af\n",
        "import util"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "__Gaussian x1 low snr (centre fixed to 50.0)__\n",
        "\n",
        "This is used for demonstrating expectation propagation, whereby a shared `centre` parameter is inferred from a sample \n",
        "of `total_datasets` 1D Gaussian datasets."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "total_datasets = 50\n",
        "\n",
        "for i in range(total_datasets):\n",
        "    dataset_path = path.join(\n",
        "        \"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",
        "        gaussian=gaussian, dataset_path=dataset_path\n",
        "    )"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "__Gaussian x1 low snr (centre drawn from parent Gaussian distribution to 50.0)__\n",
        "\n",
        "This is used for demonstrating expectation propagation and hierachical modeling, whereby a the `centre` parameters \n",
        "of a sample of `total_datasets` 1D Gaussian datasets are drawn from a Gaussian distribution."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "\n",
        "total_datasets = 10\n",
        "\n",
        "gaussian_parent_model = af.Model(\n",
        "    af.ex.Gaussian,\n",
        "    centre=af.GaussianPrior(mean=50.0, sigma=10.0, lower_limit=0.0, upper_limit=100.0),\n",
        "    normalization=0.5,\n",
        "    sigma=5.0,\n",
        ")\n",
        "\n",
        "for i in range(total_datasets):\n",
        "    dataset_path = path.join(\n",
        "        \"dataset\", \"example_1d\", f\"gaussian_x1__hierarchical\", f\"dataset_{i}\"\n",
        "    )\n",
        "\n",
        "    gaussian = gaussian_parent_model.random_instance()\n",
        "\n",
        "    util.simulate_dataset_1d_via_gaussian_from(\n",
        "        gaussian=gaussian, dataset_path=dataset_path\n",
        "    )\n"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "__Gaussian x2 offset centre__\n",
        "\n",
        "This is used for demonstrating the benefits of graphical models over fitting one-by-one, because it creates a \n",
        "degeneracy in the offset of the centres of the two Gaussians."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "total_datasets = 10\n",
        "\n",
        "for i in range(total_datasets):\n",
        "    dataset_path = path.join(\n",
        "        \"dataset\", \"example_1d\", f\"gaussian_x2__offset_centres\", f\"dataset_{i}\"\n",
        "    )\n",
        "\n",
        "    sigma_0_prior = af.GaussianPrior(\n",
        "        lower_limit=0.0, upper_limit=20.0, mean=10.0, sigma=10.0\n",
        "    )\n",
        "    while True:\n",
        "        try:\n",
        "            sigma_0_value = sigma_0_prior.value_for(unit=np.random.random(1))\n",
        "            break\n",
        "        except af.exc.PriorLimitException:\n",
        "            continue\n",
        "\n",
        "    sigma_1_prior = af.GaussianPrior(\n",
        "        lower_limit=0.0, upper_limit=20.0, mean=10.0, sigma=10.0\n",
        "    )\n",
        "    while True:\n",
        "        try:\n",
        "            sigma_1_value = sigma_1_prior.value_for(unit=np.random.random(1))\n",
        "            break\n",
        "        except af.exc.PriorLimitException:\n",
        "            continue\n",
        "\n",
        "    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",
        "        profile_1d_list=[gaussian_0, gaussian_1], dataset_path=dataset_path\n",
        "    )\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
}

back to top

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API