Skip to content

Commit b8c5ee4

Browse files
authored
Merge pull request #8 from numericalEFT/pchou
Support multiple integrands and update vegas map
2 parents 14843fc + 686c3b7 commit b8c5ee4

File tree

17 files changed

+778
-214
lines changed

17 files changed

+778
-214
lines changed

.github/workflows/CI.yml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
permissions:
12+
contents: write
13+
14+
jobs:
15+
test:
16+
name: Python ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
17+
runs-on: ${{ matrix.os }}
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
version:
22+
- "3.12"
23+
os:
24+
- ubuntu-latest
25+
arch:
26+
- x64
27+
- x86
28+
29+
steps:
30+
- uses: actions/checkout@v2
31+
32+
- name: Set up Python ${{ matrix.version }}
33+
uses: actions/setup-python@v2
34+
with:
35+
python-version: ${{ matrix.version }}
36+
37+
- name: Install dependencies
38+
run: |
39+
python -m pip install --upgrade pip
40+
pip install coverage
41+
pip install pytest pytest-cov
42+
pip install -r requirements.txt
43+
44+
- name: Run tests
45+
run: |
46+
export PYTHONPATH=src
47+
pytest --cov --cov-report=xml
48+
49+
- name: Upload coverage to Codecov
50+
uses: codecov/codecov-action@v4
51+
with:
52+
token: ${{ secrets.CODECOV_TOKEN }}
53+
54+
docs:
55+
name: Documentation
56+
runs-on: ubuntu-latest
57+
needs: test
58+
59+
steps:
60+
- uses: actions/checkout@v2
61+
62+
- name: Install dependencies
63+
run: |
64+
python -m pip install --upgrade pip
65+
pip install sphinx
66+
pip install -r requirements.txt
67+
68+
- name: Build documentation
69+
run: |
70+
cd docs
71+
sphinx-apidoc -o source ../src
72+
python ../remove_prefix.py
73+
make html
74+
75+
- name: Deploy documentation to GitHub Pages
76+
uses: peaceiris/actions-gh-pages@v3
77+
with:
78+
github_token: ${{ secrets.GITHUB_TOKEN }}
79+
publish_dir: docs/build/html

.github/workflows/generate_docs

Lines changed: 0 additions & 55 deletions
This file was deleted.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
# MCintegration
2+
[![alpha](https://img.shields.io/badge/docs-alpha-blue.svg)](https://numericaleft.github.io/MCintegration.py/)
3+
[![codecov](https://codecov.io/gh/numericalEFT/MCintegration.py/graph/badge.svg?token=851N2CNOTN)](https://codecov.io/gh/numericalEFT/MCintegration.py)

docs/Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Minimal makefile for Sphinx documentation
2+
#
3+
4+
# You can set these variables from the command line, and also
5+
# from the environment for the first two.
6+
SPHINXOPTS ?=
7+
SPHINXBUILD ?= sphinx-build
8+
SOURCEDIR = source
9+
BUILDDIR = build
10+
11+
# Put it first so that "make" without argument is like "make help".
12+
help:
13+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
14+
15+
.PHONY: help Makefile
16+
17+
# Catch-all target: route all unknown targets to Sphinx using the new
18+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
19+
%: Makefile
20+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

docs/make.bat

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@ECHO OFF
2+
3+
pushd %~dp0
4+
5+
REM Command file for Sphinx documentation
6+
7+
if "%SPHINXBUILD%" == "" (
8+
set SPHINXBUILD=sphinx-build
9+
)
10+
set SOURCEDIR=source
11+
set BUILDDIR=build
12+
13+
%SPHINXBUILD% >NUL 2>NUL
14+
if errorlevel 9009 (
15+
echo.
16+
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
17+
echo.installed, then set the SPHINXBUILD environment variable to point
18+
echo.to the full path of the 'sphinx-build' executable. Alternatively you
19+
echo.may add the Sphinx directory to PATH.
20+
echo.
21+
echo.If you don't have Sphinx installed, grab it from
22+
echo.https://www.sphinx-doc.org/
23+
exit /b 1
24+
)
25+
26+
if "%1" == "" goto help
27+
28+
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
29+
goto end
30+
31+
:help
32+
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
33+
34+
:end
35+
popd

docs/source/conf.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Configuration file for the Sphinx documentation builder.
2+
#
3+
# For the full list of built-in configuration values, see the documentation:
4+
# https://www.sphinx-doc.org/en/master/usage/configuration.html
5+
6+
# -- Project information -----------------------------------------------------
7+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
8+
import os
9+
import sys
10+
11+
sys.path.insert(0, os.path.abspath('../../src'))
12+
13+
project = 'MCintegration'
14+
copyright = '2024, Authors'
15+
author = 'Authors'
16+
release = '1.0.0'
17+
18+
# -- General configuration ---------------------------------------------------
19+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
20+
21+
extensions = [
22+
'sphinx.ext.autodoc',
23+
'sphinx.ext.viewcode',
24+
'sphinx.ext.napoleon',
25+
'sphinx.ext.autosummary'
26+
]
27+
28+
templates_path = ['_templates']
29+
exclude_patterns = []
30+
31+
32+
33+
# -- Options for HTML output -------------------------------------------------
34+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
35+
36+
templates_path = ['_templates']
37+
38+
exclude_trees = ['_build']
39+
40+
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
41+
42+
source_suffix = '.rst'
43+
44+
source_encoding = 'utf-8'
45+
46+
master_doc = 'index'
47+
48+
napoleon_google_docstring = True
49+
napoleon_numpy_docstring = True
50+
napoleon_include_private_with_doc = False
51+
napoleon_include_special_with_doc = True
52+
napoleon_use_admonition_for_examples = False
53+
napoleon_use_admonition_for_notes = False
54+
napoleon_use_admonition_for_references = False
55+
napoleon_use_ivar = False
56+
napoleon_use_param = True
57+
napoleon_use_rtype = True
58+
59+
html_theme = 'sphinxdoc'
60+
html_theme = 'nature'
61+
html_theme = 'pyramid'
62+
html_static_path = ['_static']

docs/source/index.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.. MCintegration documentation master file, created by
2+
sphinx-quickstart on Tue Oct 15 10:32:44 2024.
3+
You can adapt this file completely to your liking, but it should at least
4+
contain the root `toctree` directive.
5+
6+
MCintegration documentation
7+
===============
8+
9+
Add your content using ``reStructuredText`` syntax. See the
10+
`reStructuredText <https://www.sphinx-doc.org/en/master/usage/restructuredtext/index.html>`_
11+
documentation for details.
12+
13+
14+
.. toctree::
15+
:maxdepth: 2
16+
:caption: Contents:
17+

examples/benchmark_vegas.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import vegas
2+
import numpy as np
3+
import gvar
4+
import torch
5+
6+
dim = 4
7+
nitn = 10
8+
ninc = 1000
9+
10+
11+
@vegas.lbatchintegrand
12+
def f_batch(x):
13+
dx2 = 0.0
14+
for d in range(dim):
15+
dx2 += (x[:, d] - 0.5) ** 2
16+
return np.exp(-200 * dx2)
17+
# ans = np.empty((x.shape[0], 3), float)
18+
# dx2 = 0.0
19+
# for d in range(dim):
20+
# dx2 += (x[:, d] - 0.5) ** 2
21+
# ans[:, 0] = np.exp(-200 * dx2)
22+
# ans[:, 1] = x[:, 0] * ans[:, 0]
23+
# ans[:, 2] = x[:, 0] ** 2 * ans[:, 0]
24+
# return ans
25+
26+
27+
def smc(f, map, neval, dim):
28+
"integrates f(y) over dim-dimensional unit hypercube"
29+
y = np.random.uniform(0, 1, (neval, dim))
30+
jac = np.empty(y.shape[0], float)
31+
x = np.empty(y.shape, float)
32+
map.map(y, x, jac)
33+
fy = jac * f(x)
34+
return (np.average(fy), np.std(fy) / neval**0.5)
35+
36+
37+
def mc(f, neval, dim):
38+
"integrates f(y) over dim-dimensional unit hypercube"
39+
y = np.random.uniform(0, 1, (neval, dim))
40+
fy = f(y)
41+
return (np.average(fy), np.std(fy) / neval**0.5)
42+
43+
44+
m = vegas.AdaptiveMap(dim * [[0, 1]], ninc=ninc)
45+
ny = 20000
46+
# torch.manual_seed(0)
47+
# y = torch.rand((ny, dim), dtype=torch.float64).numpy()
48+
y = np.random.uniform(0.0, 1.0, (ny, dim)) # 1000 random y's
49+
50+
x = np.empty(y.shape, float) # work space
51+
jac = np.empty(y.shape[0], float)
52+
f2 = np.empty(y.shape[0], float)
53+
54+
for itn in range(10): # 5 iterations to adapt
55+
m.map(y, x, jac) # compute x's and jac
56+
57+
f2 = (jac * f_batch(x)) ** 2
58+
m.add_training_data(y, f2) # adapt
59+
# if itn == 0:
60+
# print(np.array(memoryview(m.sum_f)))
61+
# print(np.array(memoryview(m.n_f)))
62+
m.adapt(alpha=0.5)
63+
64+
65+
# with map
66+
r = smc(f_batch, m, 50_000, dim)
67+
print(" SMC + map:", f"{r[0]} +- {r[1]}")
68+
69+
# without map
70+
r = mc(f_batch, 50_000, dim)
71+
print("SMC (no map):", f"{r[0]} +- {r[1]}")
72+
73+
74+
# vegas with adaptive stratified sampling
75+
print("VEGAS using adaptive stratified sampling")
76+
integ = vegas.Integrator(dim * [[0, 1]])
77+
training = integ(f_batch, nitn=10, neval=20000) # adapt grid
78+
79+
# final analysis
80+
result = integ(f_batch, nitn=1, neval=50_000, adapt=False)
81+
print(result)
82+
result = integ(f_batch, nitn=5, neval=10_000, adapt=False)
83+
print(result)
84+
result = integ(f_batch, nitn=5, neval=10_000)
85+
print(result)
86+
# print("I[0] =", result[0], " I[1] =", result[1], " I[2] =", result[2])
87+
# print("Q = %.2f\n" % result.Q)
88+
# print("<x> =", result[1] / result[0])
89+
# print(
90+
# "sigma_x**2 = <x**2> - <x>**2 =",
91+
# result[2] / result[0] - (result[1] / result[0]) ** 2,
92+
# )
93+
# print("\ncorrelation matrix:\n", gv.evalcorr(result))

remove_prefix.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import os
2+
3+
docs_source_dir = './source'
4+
5+
target_file = 'src.rst'
6+
7+
target_file_path = os.path.join(docs_source_dir, target_file)
8+
9+
if os.path.exists(target_file_path):
10+
with open(target_file_path, 'r') as f:
11+
content = f.read()
12+
content = content.replace('src.', '')
13+
with open(target_file_path, 'w') as f:
14+
f.write(content)

requirements.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
torch
2+
numpy
3+
normflows
4+
vegas
5+
mpmath
6+
matplotlib
7+
tqdm
8+
absl-py

0 commit comments

Comments
 (0)