simweights


Namesimweights JSON
Version 0.1.2 PyPI version JSON
download
home_pageNone
SummaryPure python library for calculating the weights of Monte Carlo simulation for IceCube.
upload_time2023-11-02 22:01:54
maintainerNone
docs_urlNone
authorKevin Meagher
requires_python~=3.7
licenseNone
keywords python science astronomy astrophysics icecube neutrino simulation
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            .. SPDX-FileCopyrightText: © 2022 the SimWeights contributors
..
.. SPDX-License-Identifier: BSD-2-Clause

|pre-commit.ci| |tests| |docs| |codecov| |LICENSE| |pypi|

.. |tests| image:: https://github.com/icecube/simweights/actions/workflows/tests.yml/badge.svg?branch=main
  :target: https://github.com/icecube/simweights/actions/workflows/tests.yml
.. |docs| image:: https://github.com/icecube/simweights/actions/workflows/docs.yml/badge.svg?branch=main
  :target: https://docs.icecube.aq/simweights/main
.. |codecov| image:: https://codecov.io/gh/icecube/simweights/branch/main/graph/badge.svg?token=LQ20TAMY9C
  :target: https://codecov.io/gh/icecube/simweights
.. |LICENSE| image:: https://img.shields.io/badge/License-BSD_2--Clause-orange.svg
  :target: https://opensource.org/licenses/BSD-2-Clause
.. |pre-commit.ci| image:: https://results.pre-commit.ci/badge/github/icecube/simweights/main.svg
   :target: https://results.pre-commit.ci/latest/github/icecube/simweights/main
   :alt: pre-commit.ci status
.. |pypi| image:: https://badge.fury.io/py/simweights.svg
   :target: https://pypi.org/project/simweights/
   :alt: PyPi

.. badge-break

==========
SimWeights
==========

Pure python library for calculating the weights of Monte Carlo simulation for IceCube.

SimWeights was designed with goal of calculating weights for IceCube simulation in a way that it
is easy to combine combine datasets with different generation parameters into a single sample.
It was also designed to be a stand alone project which does not depend on IceTray in any way so that it can
be installed easily on laptops. SimWeights gathers all the information it needs form information in the
hdf5 file so there is no need for access to the simulation production database. SimWeights works with
files produced with corsika-reader, neutrino-generator, and genie-reader.

.. doc-break

Prerequisites
=============

| **Required**: `numpy`_, `scipy`_
| **Optional**: `h5py`_, `pytables`_, `pandas`_, `uproot`_, `nuflux`_

.. _numpy: https://numpy.org/
.. _scipy: https://scipy.org/
.. _h5py: https://www.h5py.org/
.. _pytables: https://www.pytables.org/
.. _pandas: https://pandas.pydata.org/
.. _uproot: https://github.com/scikit-hep/uproot5
.. _nuflux: https://github.com/icecube/nuflux

Installation
============

To install from pypi run:

.. code-block:: shell-session

  pip install simweights

Alternatively, if you need to install unreleased code from main you can run:

.. code-block:: shell-session

  pip install git+https://github.com/icecube/simweights.git

On certain installs of python on cvmfs you might get the following error:
``ModuleNotFoundError: No module named 'glob'``. If this happens you can add the
following option ``--no-build-isolation`` to the above command.

If you want to develop simweights you can install directly with flit.
The ``-s`` option will symlink the module into site-packages rather than copying it,
so that you can test changes without reinstalling the module:

.. code-block:: shell-session

  pip install flit
  git clone git@github.com:icecube/simweights.git
  cd simweights
  flit install [--user] -s

Basic Usage
===========

For triggered CORSIKA or CORSIKA produced by ``corsika-reader`` with S-Frames files use
``CorsikaWeighter()`` without any additional arguments:

.. code-block:: pycon

  >>> import simweights, pandas
  >>> simfile = pandas.HDFStore("Level2_IC86.2016_corsika.021889.000000.hdf5", "r")
  >>> flux_model = simweights.GaisserH4a()
  >>> weight_obj = simweights.CorsikaWeighter(simfile)
  >>> weights = weight_obj.get_weights(flux_model)
  >>> print(f"Rate = {weights.sum():5.2f} Hz")
  Rate = 122.84 Hz

The value returned by ``get_weights()`` is the rate of events in Hz

For traditional CORSIKA files made with ``corsika-reader`` you will also use
:code:`simweights.CorsikaWeighter()`, but you need to know the number of ``.i3``
files that contributed to create this hdf5 file and pass it as the ``nfiles`` parameter.

For ``neutrino-generator`` you can use :code:`NuGenWeighter()` which also
requires you to know the number of files.
Flux models from `nuflux <https://github.com/icecube/nuflux>`_ can be used:

.. code-block:: pycon

  >>> import nuflux
  >>> simfile = pandas.HDFStore("Level2_IC86.2016_NuMu.020878.000000.hdf5")
  >>> flux_model = nuflux.makeFlux("CORSIKA_GaisserH3a_QGSJET-II")
  >>> weight_obj = simweights.NuGenWeighter(simfile, nfiles=1)
  >>> weights = weight_obj.get_weights(flux_model)
  >>> print(f"Rate = {weights.sum():5.2e} Hz")
  Rate = 1.41e-02 Hz

To weight a spectrum with a function you can also pass a callable to :code:`get_weights()`

.. code-block:: pycon

  >>> weights = weight_obj.get_weights(lambda energy: 7.2e-8 * energy**-2.2)
  >>> print(f"Rate = {weights.sum():5.2e} Hz")
  Rate = 2.34e-05 Hz

You can also pass flux values as a numpy array with the same length as the sample

.. code-block:: pycon

  >>> fluxes = 7.2e-8 * simfile["I3MCWeightDict"]["PrimaryNeutrinoEnergy"] ** -2.2
  >>> weights = weight_obj.get_weights(fluxes)
  >>> print(f"Rate = {weights.sum():5.2e} Hz")
  Rate = 2.34e-05 Hz

You can also pass a scalar to weight all events with the same flux. Passing
a value of ``1.0`` will result in the well known quantity OneWeight divided
by the number of events.

.. code-block:: pycon

  >>> OneWeight = weight_obj.get_weights(1.0)
  >>> OldOneWeight = simfile["I3MCWeightDict"]["OneWeight"] / (simfile["I3MCWeightDict"]["NEvents"] / 2)
  >>> (OneWeight - OldOneWeight).median()
  0.0

Simulation created with ``genie-reader`` can be weighted with :code:`GenieWeighter()`:

.. code-block:: pycon

  >>> simfile = pandas.HDFStore("genie_reader_NuE.hdf5")
  >>> flux_model = nuflux.makeFlux("IPhonda2014_spl_solmax")
  >>> weight_obj = simweights.GenieWeighter(simfile)
  >>> weights = weight_obj.get_weights(flux_model)
  >>> print(f"Rate = {weights.sum():5.2e} Hz")
  Rate = 3.78e+00 Hz

Also note that these examples use ``pandas``. SimWeights will work equally well with
``pandas``, ``h5py``, or ``pytables``.

Documentation
=============

Full documentation is available on the
`IceCube Documentation Server <https://docs.icecube.aq/simweights/main/>`_.

Getting Help
============

Please direct any questions to ``@kjm`` on the slack channel
`#software <https://icecube-spno.slack.com/channels/software>`_.

.. contrib-break

Contributing
============

See the `contributing guide <CONTRIBUTING.rst>`_

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "simweights",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "~=3.7",
    "maintainer_email": null,
    "keywords": "python,science,astronomy,astrophysics,IceCube,neutrino,simulation",
    "author": "Kevin Meagher",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/17/18/130e1a94cc413c60461c723b7d61dc0d153910bf89446581a30ab2d44310/simweights-0.1.2.tar.gz",
    "platform": null,
    "description": ".. SPDX-FileCopyrightText: \u00a9 2022 the SimWeights contributors\n..\n.. SPDX-License-Identifier: BSD-2-Clause\n\n|pre-commit.ci| |tests| |docs| |codecov| |LICENSE| |pypi|\n\n.. |tests| image:: https://github.com/icecube/simweights/actions/workflows/tests.yml/badge.svg?branch=main\n  :target: https://github.com/icecube/simweights/actions/workflows/tests.yml\n.. |docs| image:: https://github.com/icecube/simweights/actions/workflows/docs.yml/badge.svg?branch=main\n  :target: https://docs.icecube.aq/simweights/main\n.. |codecov| image:: https://codecov.io/gh/icecube/simweights/branch/main/graph/badge.svg?token=LQ20TAMY9C\n  :target: https://codecov.io/gh/icecube/simweights\n.. |LICENSE| image:: https://img.shields.io/badge/License-BSD_2--Clause-orange.svg\n  :target: https://opensource.org/licenses/BSD-2-Clause\n.. |pre-commit.ci| image:: https://results.pre-commit.ci/badge/github/icecube/simweights/main.svg\n   :target: https://results.pre-commit.ci/latest/github/icecube/simweights/main\n   :alt: pre-commit.ci status\n.. |pypi| image:: https://badge.fury.io/py/simweights.svg\n   :target: https://pypi.org/project/simweights/\n   :alt: PyPi\n\n.. badge-break\n\n==========\nSimWeights\n==========\n\nPure python library for calculating the weights of Monte Carlo simulation for IceCube.\n\nSimWeights was designed with goal of calculating weights for IceCube simulation in a way that it\nis easy to combine combine datasets with different generation parameters into a single sample.\nIt was also designed to be a stand alone project which does not depend on IceTray in any way so that it can\nbe installed easily on laptops. SimWeights gathers all the information it needs form information in the\nhdf5 file so there is no need for access to the simulation production database. SimWeights works with\nfiles produced with corsika-reader, neutrino-generator, and genie-reader.\n\n.. doc-break\n\nPrerequisites\n=============\n\n| **Required**: `numpy`_, `scipy`_\n| **Optional**: `h5py`_, `pytables`_, `pandas`_, `uproot`_, `nuflux`_\n\n.. _numpy: https://numpy.org/\n.. _scipy: https://scipy.org/\n.. _h5py: https://www.h5py.org/\n.. _pytables: https://www.pytables.org/\n.. _pandas: https://pandas.pydata.org/\n.. _uproot: https://github.com/scikit-hep/uproot5\n.. _nuflux: https://github.com/icecube/nuflux\n\nInstallation\n============\n\nTo install from pypi run:\n\n.. code-block:: shell-session\n\n  pip install simweights\n\nAlternatively, if you need to install unreleased code from main you can run:\n\n.. code-block:: shell-session\n\n  pip install git+https://github.com/icecube/simweights.git\n\nOn certain installs of python on cvmfs you might get the following error:\n``ModuleNotFoundError: No module named 'glob'``. If this happens you can add the\nfollowing option ``--no-build-isolation`` to the above command.\n\nIf you want to develop simweights you can install directly with flit.\nThe ``-s`` option will symlink the module into site-packages rather than copying it,\nso that you can test changes without reinstalling the module:\n\n.. code-block:: shell-session\n\n  pip install flit\n  git clone git@github.com:icecube/simweights.git\n  cd simweights\n  flit install [--user] -s\n\nBasic Usage\n===========\n\nFor triggered CORSIKA or CORSIKA produced by ``corsika-reader`` with S-Frames files use\n``CorsikaWeighter()`` without any additional arguments:\n\n.. code-block:: pycon\n\n  >>> import simweights, pandas\n  >>> simfile = pandas.HDFStore(\"Level2_IC86.2016_corsika.021889.000000.hdf5\", \"r\")\n  >>> flux_model = simweights.GaisserH4a()\n  >>> weight_obj = simweights.CorsikaWeighter(simfile)\n  >>> weights = weight_obj.get_weights(flux_model)\n  >>> print(f\"Rate = {weights.sum():5.2f} Hz\")\n  Rate = 122.84 Hz\n\nThe value returned by ``get_weights()`` is the rate of events in Hz\n\nFor traditional CORSIKA files made with ``corsika-reader`` you will also use\n:code:`simweights.CorsikaWeighter()`, but you need to know the number of ``.i3``\nfiles that contributed to create this hdf5 file and pass it as the ``nfiles`` parameter.\n\nFor ``neutrino-generator`` you can use :code:`NuGenWeighter()` which also\nrequires you to know the number of files.\nFlux models from `nuflux <https://github.com/icecube/nuflux>`_ can be used:\n\n.. code-block:: pycon\n\n  >>> import nuflux\n  >>> simfile = pandas.HDFStore(\"Level2_IC86.2016_NuMu.020878.000000.hdf5\")\n  >>> flux_model = nuflux.makeFlux(\"CORSIKA_GaisserH3a_QGSJET-II\")\n  >>> weight_obj = simweights.NuGenWeighter(simfile, nfiles=1)\n  >>> weights = weight_obj.get_weights(flux_model)\n  >>> print(f\"Rate = {weights.sum():5.2e} Hz\")\n  Rate = 1.41e-02 Hz\n\nTo weight a spectrum with a function you can also pass a callable to :code:`get_weights()`\n\n.. code-block:: pycon\n\n  >>> weights = weight_obj.get_weights(lambda energy: 7.2e-8 * energy**-2.2)\n  >>> print(f\"Rate = {weights.sum():5.2e} Hz\")\n  Rate = 2.34e-05 Hz\n\nYou can also pass flux values as a numpy array with the same length as the sample\n\n.. code-block:: pycon\n\n  >>> fluxes = 7.2e-8 * simfile[\"I3MCWeightDict\"][\"PrimaryNeutrinoEnergy\"] ** -2.2\n  >>> weights = weight_obj.get_weights(fluxes)\n  >>> print(f\"Rate = {weights.sum():5.2e} Hz\")\n  Rate = 2.34e-05 Hz\n\nYou can also pass a scalar to weight all events with the same flux. Passing\na value of ``1.0`` will result in the well known quantity OneWeight divided\nby the number of events.\n\n.. code-block:: pycon\n\n  >>> OneWeight = weight_obj.get_weights(1.0)\n  >>> OldOneWeight = simfile[\"I3MCWeightDict\"][\"OneWeight\"] / (simfile[\"I3MCWeightDict\"][\"NEvents\"] / 2)\n  >>> (OneWeight - OldOneWeight).median()\n  0.0\n\nSimulation created with ``genie-reader`` can be weighted with :code:`GenieWeighter()`:\n\n.. code-block:: pycon\n\n  >>> simfile = pandas.HDFStore(\"genie_reader_NuE.hdf5\")\n  >>> flux_model = nuflux.makeFlux(\"IPhonda2014_spl_solmax\")\n  >>> weight_obj = simweights.GenieWeighter(simfile)\n  >>> weights = weight_obj.get_weights(flux_model)\n  >>> print(f\"Rate = {weights.sum():5.2e} Hz\")\n  Rate = 3.78e+00 Hz\n\nAlso note that these examples use ``pandas``. SimWeights will work equally well with\n``pandas``, ``h5py``, or ``pytables``.\n\nDocumentation\n=============\n\nFull documentation is available on the\n`IceCube Documentation Server <https://docs.icecube.aq/simweights/main/>`_.\n\nGetting Help\n============\n\nPlease direct any questions to ``@kjm`` on the slack channel\n`#software <https://icecube-spno.slack.com/channels/software>`_.\n\n.. contrib-break\n\nContributing\n============\n\nSee the `contributing guide <CONTRIBUTING.rst>`_\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Pure python library for calculating the weights of Monte Carlo simulation for IceCube.",
    "version": "0.1.2",
    "project_urls": {
        "Collaboration": "https://icecube.wisc.edu",
        "Documentation": "https://docs.icecube.aq/simweights/main",
        "Source": "https://github.com/icecube/simweights"
    },
    "split_keywords": [
        "python",
        "science",
        "astronomy",
        "astrophysics",
        "icecube",
        "neutrino",
        "simulation"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "92492a41957905987bd744736adf7ff1e7d70a094a65d11a9da6c6a54ca5b887",
                "md5": "3aabd25a00216a125479425ead0e60c4",
                "sha256": "290f36c93966cf5febad52977e36518195770f55a7462d52ecd4dd27b14af117"
            },
            "downloads": -1,
            "filename": "simweights-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3aabd25a00216a125479425ead0e60c4",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": "~=3.7",
            "size": 118534,
            "upload_time": "2023-11-02T22:01:49",
            "upload_time_iso_8601": "2023-11-02T22:01:49.512010Z",
            "url": "https://files.pythonhosted.org/packages/92/49/2a41957905987bd744736adf7ff1e7d70a094a65d11a9da6c6a54ca5b887/simweights-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "1718130e1a94cc413c60461c723b7d61dc0d153910bf89446581a30ab2d44310",
                "md5": "f5cc859e2ce3e4b44e79335588dfde2e",
                "sha256": "179997c397e45eda67f6c881ec229e48be5dd6a3060d3ae5ced861319b90eec1"
            },
            "downloads": -1,
            "filename": "simweights-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "f5cc859e2ce3e4b44e79335588dfde2e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "~=3.7",
            "size": 301176,
            "upload_time": "2023-11-02T22:01:54",
            "upload_time_iso_8601": "2023-11-02T22:01:54.579031Z",
            "url": "https://files.pythonhosted.org/packages/17/18/130e1a94cc413c60461c723b7d61dc0d153910bf89446581a30ab2d44310/simweights-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-02 22:01:54",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "icecube",
    "github_project": "simweights",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "simweights"
}
        
Elapsed time: 3.28839s