Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: CI

on:
push:
branches:
- master
pull_request:
branches:
- master

permissions:
contents: write

jobs:
test:
name: Python ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
version:
- "3.12"
os:
- ubuntu-latest
arch:
- x64
- x86

steps:
- uses: actions/checkout@v2

- name: Set up Python ${{ matrix.version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install coverage
pip install pytest pytest-cov
pip install -r requirements.txt

- name: Run tests
run: |
export PYTHONPATH=src
pytest --cov --cov-report=xml

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}

docs:
name: Documentation
runs-on: ubuntu-latest
needs: test

steps:
- uses: actions/checkout@v2

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install sphinx
pip install -r requirements.txt

- name: Build documentation
run: |
cd docs
sphinx-apidoc -o source ../src
python ../remove_prefix.py
make html

- name: Deploy documentation to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: docs/build/html
55 changes: 0 additions & 55 deletions .github/workflows/generate_docs

This file was deleted.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# MCintegration
[![alpha](https://img.shields.io/badge/docs-alpha-blue.svg)](https://numericaleft.github.io/MCintegration.py/)
[![codecov](https://codecov.io/gh/numericalEFT/MCintegration.py/graph/badge.svg?token=851N2CNOTN)](https://codecov.io/gh/numericalEFT/MCintegration.py)
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = source
BUILDDIR = build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
35 changes: 35 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=source
set BUILDDIR=build

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)

if "%1" == "" goto help

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
62 changes: 62 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
import os
import sys

sys.path.insert(0, os.path.abspath('../../src'))

project = 'MCintegration'
copyright = '2024, Authors'
author = 'Authors'
release = '1.0.0'

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.viewcode',
'sphinx.ext.napoleon',
'sphinx.ext.autosummary'
]

templates_path = ['_templates']
exclude_patterns = []



# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

templates_path = ['_templates']

exclude_trees = ['_build']

exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']

source_suffix = '.rst'

source_encoding = 'utf-8'

master_doc = 'index'

napoleon_google_docstring = True
napoleon_numpy_docstring = True
napoleon_include_private_with_doc = False
napoleon_include_special_with_doc = True
napoleon_use_admonition_for_examples = False
napoleon_use_admonition_for_notes = False
napoleon_use_admonition_for_references = False
napoleon_use_ivar = False
napoleon_use_param = True
napoleon_use_rtype = True

html_theme = 'sphinxdoc'
html_theme = 'nature'
html_theme = 'pyramid'
html_static_path = ['_static']
17 changes: 17 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.. MCintegration documentation master file, created by
sphinx-quickstart on Tue Oct 15 10:32:44 2024.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.

MCintegration documentation
===============

Add your content using ``reStructuredText`` syntax. See the
`reStructuredText <https://www.sphinx-doc.org/en/master/usage/restructuredtext/index.html>`_
documentation for details.


.. toctree::
:maxdepth: 2
:caption: Contents:

93 changes: 93 additions & 0 deletions examples/benchmark_vegas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import vegas
import numpy as np
import gvar
import torch

dim = 4
nitn = 10
ninc = 1000


@vegas.lbatchintegrand
def f_batch(x):
dx2 = 0.0
for d in range(dim):
dx2 += (x[:, d] - 0.5) ** 2
return np.exp(-200 * dx2)
# ans = np.empty((x.shape[0], 3), float)
# dx2 = 0.0
# for d in range(dim):
# dx2 += (x[:, d] - 0.5) ** 2
# ans[:, 0] = np.exp(-200 * dx2)
# ans[:, 1] = x[:, 0] * ans[:, 0]
# ans[:, 2] = x[:, 0] ** 2 * ans[:, 0]
# return ans


def smc(f, map, neval, dim):
"integrates f(y) over dim-dimensional unit hypercube"
y = np.random.uniform(0, 1, (neval, dim))
jac = np.empty(y.shape[0], float)
x = np.empty(y.shape, float)
map.map(y, x, jac)
fy = jac * f(x)
return (np.average(fy), np.std(fy) / neval**0.5)


def mc(f, neval, dim):
"integrates f(y) over dim-dimensional unit hypercube"
y = np.random.uniform(0, 1, (neval, dim))
fy = f(y)
return (np.average(fy), np.std(fy) / neval**0.5)


m = vegas.AdaptiveMap(dim * [[0, 1]], ninc=ninc)
ny = 20000
# torch.manual_seed(0)
# y = torch.rand((ny, dim), dtype=torch.float64).numpy()
y = np.random.uniform(0.0, 1.0, (ny, dim)) # 1000 random y's

x = np.empty(y.shape, float) # work space
jac = np.empty(y.shape[0], float)
f2 = np.empty(y.shape[0], float)

for itn in range(10): # 5 iterations to adapt
m.map(y, x, jac) # compute x's and jac

f2 = (jac * f_batch(x)) ** 2
m.add_training_data(y, f2) # adapt
# if itn == 0:
# print(np.array(memoryview(m.sum_f)))
# print(np.array(memoryview(m.n_f)))
m.adapt(alpha=0.5)


# with map
r = smc(f_batch, m, 50_000, dim)
print(" SMC + map:", f"{r[0]} +- {r[1]}")

# without map
r = mc(f_batch, 50_000, dim)
print("SMC (no map):", f"{r[0]} +- {r[1]}")


# vegas with adaptive stratified sampling
print("VEGAS using adaptive stratified sampling")
integ = vegas.Integrator(dim * [[0, 1]])
training = integ(f_batch, nitn=10, neval=20000) # adapt grid

# final analysis
result = integ(f_batch, nitn=1, neval=50_000, adapt=False)
print(result)
result = integ(f_batch, nitn=5, neval=10_000, adapt=False)
print(result)
result = integ(f_batch, nitn=5, neval=10_000)
print(result)
# print("I[0] =", result[0], " I[1] =", result[1], " I[2] =", result[2])
# print("Q = %.2f\n" % result.Q)
# print("<x> =", result[1] / result[0])
# print(
# "sigma_x**2 = <x**2> - <x>**2 =",
# result[2] / result[0] - (result[1] / result[0]) ** 2,
# )
# print("\ncorrelation matrix:\n", gv.evalcorr(result))
14 changes: 14 additions & 0 deletions remove_prefix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os

docs_source_dir = './source'

target_file = 'src.rst'

target_file_path = os.path.join(docs_source_dir, target_file)

if os.path.exists(target_file_path):
with open(target_file_path, 'r') as f:
content = f.read()
content = content.replace('src.', '')
with open(target_file_path, 'w') as f:
f.write(content)
8 changes: 8 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
torch
numpy
normflows
vegas
mpmath
matplotlib
tqdm
absl-py
Loading