├── coniii ├── ising_eqn │ ├── __init__.py │ ├── ising_eqn_2.py │ ├── ising_eqn_2_sym.py │ ├── ising_eqn_3.py │ ├── ising_eqn_3_sym.py │ ├── ising_eqn_4.py │ ├── ising_eqn_4_sym.py │ └── ising_eqn_5.py ├── version.py ├── __init__.py ├── write_ising_files.sh ├── ising │ ├── test_utils.py │ ├── automaton.py │ └── utils.py ├── test_enumerate_potts.py ├── test_enumerate.py ├── test_solvers.py └── test_samplers.py ├── docs ├── _build │ ├── html │ │ ├── _static │ │ │ ├── custom.css │ │ │ ├── up.png │ │ │ ├── down.png │ │ │ ├── file.png │ │ │ ├── minus.png │ │ │ ├── plus.png │ │ │ ├── comment.png │ │ │ ├── up-pressed.png │ │ │ ├── ajax-loader.gif │ │ │ ├── down-pressed.png │ │ │ ├── comment-bright.png │ │ │ ├── comment-close.png │ │ │ ├── documentation_options.js │ │ │ └── pygments.css │ │ ├── objects.inv │ │ ├── .buildinfo │ │ ├── _sources │ │ │ ├── index.txt │ │ │ └── index.rst.txt │ │ ├── search.html │ │ ├── coniii_rst │ │ │ ├── coniii.mc_hist.html │ │ │ ├── coniii.general_model_rmc.html │ │ │ ├── coniii.custom_maxent.html │ │ │ ├── coniii.ising.test_automaton.html │ │ │ ├── coniii.ising.html │ │ │ ├── coniii.test_samplers.html │ │ │ ├── coniii.test_solvers.html │ │ │ ├── modules.html │ │ │ ├── coniii.html │ │ │ ├── coniii.ising.automaton.html │ │ │ └── coniii.test_utils.html │ │ ├── index.html │ │ └── py-modindex.html │ └── doctrees │ │ ├── index.doctree │ │ ├── environment.pickle │ │ └── coniii_rst │ │ ├── coniii.doctree │ │ ├── modules.doctree │ │ ├── coniii.ising.doctree │ │ ├── coniii.mc_hist.doctree │ │ ├── coniii.solvers.doctree │ │ ├── coniii.utils.doctree │ │ ├── coniii.enumerate.doctree │ │ ├── coniii.samplers.doctree │ │ ├── coniii.test_solvers.doctree │ │ ├── coniii.test_utils.doctree │ │ ├── coniii.custom_maxent.doctree │ │ ├── coniii.test_samplers.doctree │ │ ├── coniii.general_model_rmc.doctree │ │ ├── coniii.ising.automaton.doctree │ │ ├── coniii.mean_field_ising.doctree │ │ ├── coniii.ising.test_automaton.doctree │ │ └── coniii.pseudo_inverse_ising.doctree ├── readthedocs.txt ├── coniii_rst │ ├── coniii.utils.rst │ ├── coniii.mc_hist.rst │ ├── coniii.solvers.rst │ ├── coniii.enumerate.rst │ ├── coniii.samplers.rst │ ├── coniii.test_utils.rst │ ├── coniii.test_solvers.rst │ ├── coniii.enumerate_potts.rst │ ├── coniii.test_samplers.rst │ ├── coniii.ising.automaton.rst │ ├── coniii.mean_field_ising.rst │ ├── coniii.general_model_rmc.rst │ ├── coniii.ising.test_automaton.rst │ ├── coniii.pseudo_inverse_ising.rst │ ├── coniii.ising.rst │ └── coniii.rst ├── Makefile ├── index.rst └── conf.py ├── images ├── mch.pdf ├── mpf.png ├── pseudo.pdf └── ising_example_lattice.pdf ├── .gitignore ├── guide ├── guide.pdf ├── architecture.pdf └── jors.cls ├── MANIFEST.in ├── profile └── boost_profile.py ├── LICENSE.txt ├── pypi_compile.sh ├── DEVREADME ├── cpp ├── py.cpp └── samplers.hpp ├── README.md ├── setup.py ├── pypi_description └── RELEASE_NOTES /coniii/ising_eqn/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /coniii/version.py: -------------------------------------------------------------------------------- 1 | version = '3.0.1' 2 | -------------------------------------------------------------------------------- /docs/_build/html/_static/custom.css: -------------------------------------------------------------------------------- 1 | /* This file intentionally left blank. */ 2 | -------------------------------------------------------------------------------- /images/mch.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/images/mch.pdf -------------------------------------------------------------------------------- /images/mpf.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/images/mpf.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .ipynb_checkpoints/ 3 | __pycache__/ 4 | 5 | *.pyc 6 | 7 | -------------------------------------------------------------------------------- /guide/guide.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/guide/guide.pdf -------------------------------------------------------------------------------- /images/pseudo.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/images/pseudo.pdf -------------------------------------------------------------------------------- /guide/architecture.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/guide/architecture.pdf -------------------------------------------------------------------------------- /docs/_build/html/objects.inv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/html/objects.inv -------------------------------------------------------------------------------- /docs/_build/html/_static/up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/html/_static/up.png -------------------------------------------------------------------------------- /docs/_build/doctrees/index.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/doctrees/index.doctree -------------------------------------------------------------------------------- /docs/_build/html/_static/down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/html/_static/down.png -------------------------------------------------------------------------------- /docs/_build/html/_static/file.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/html/_static/file.png -------------------------------------------------------------------------------- /docs/_build/html/_static/minus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/html/_static/minus.png -------------------------------------------------------------------------------- /docs/_build/html/_static/plus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/html/_static/plus.png -------------------------------------------------------------------------------- /images/ising_example_lattice.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/images/ising_example_lattice.pdf -------------------------------------------------------------------------------- /docs/_build/html/_static/comment.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/html/_static/comment.png -------------------------------------------------------------------------------- /docs/_build/doctrees/environment.pickle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/doctrees/environment.pickle -------------------------------------------------------------------------------- /docs/_build/html/_static/up-pressed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/html/_static/up-pressed.png -------------------------------------------------------------------------------- /docs/_build/html/_static/ajax-loader.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/html/_static/ajax-loader.gif -------------------------------------------------------------------------------- /docs/_build/html/_static/down-pressed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/html/_static/down-pressed.png -------------------------------------------------------------------------------- /docs/readthedocs.txt: -------------------------------------------------------------------------------- 1 | multiprocess==0.70.5 2 | jupyter>=1 3 | matplotlib 4 | scipy 5 | numpy 6 | numba>=0.39.0,<1 7 | dill 8 | joblib 9 | -------------------------------------------------------------------------------- /docs/_build/html/_static/comment-bright.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/html/_static/comment-bright.png -------------------------------------------------------------------------------- /docs/_build/html/_static/comment-close.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/html/_static/comment-close.png -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include coniii/usage_guide.ipynb 2 | include coniii/version.py 3 | include coniii/LICENSE.txt 4 | include coniii/pypi_description 5 | -------------------------------------------------------------------------------- /docs/_build/doctrees/coniii_rst/coniii.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/doctrees/coniii_rst/coniii.doctree -------------------------------------------------------------------------------- /docs/_build/doctrees/coniii_rst/modules.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/doctrees/coniii_rst/modules.doctree -------------------------------------------------------------------------------- /docs/_build/doctrees/coniii_rst/coniii.ising.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/doctrees/coniii_rst/coniii.ising.doctree -------------------------------------------------------------------------------- /docs/_build/doctrees/coniii_rst/coniii.mc_hist.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/doctrees/coniii_rst/coniii.mc_hist.doctree -------------------------------------------------------------------------------- /docs/_build/doctrees/coniii_rst/coniii.solvers.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/doctrees/coniii_rst/coniii.solvers.doctree -------------------------------------------------------------------------------- /docs/_build/doctrees/coniii_rst/coniii.utils.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/doctrees/coniii_rst/coniii.utils.doctree -------------------------------------------------------------------------------- /docs/_build/doctrees/coniii_rst/coniii.enumerate.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/doctrees/coniii_rst/coniii.enumerate.doctree -------------------------------------------------------------------------------- /docs/_build/doctrees/coniii_rst/coniii.samplers.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/doctrees/coniii_rst/coniii.samplers.doctree -------------------------------------------------------------------------------- /docs/_build/doctrees/coniii_rst/coniii.test_solvers.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/doctrees/coniii_rst/coniii.test_solvers.doctree -------------------------------------------------------------------------------- /docs/_build/doctrees/coniii_rst/coniii.test_utils.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/doctrees/coniii_rst/coniii.test_utils.doctree -------------------------------------------------------------------------------- /docs/_build/doctrees/coniii_rst/coniii.custom_maxent.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/doctrees/coniii_rst/coniii.custom_maxent.doctree -------------------------------------------------------------------------------- /docs/_build/doctrees/coniii_rst/coniii.test_samplers.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/doctrees/coniii_rst/coniii.test_samplers.doctree -------------------------------------------------------------------------------- /docs/_build/doctrees/coniii_rst/coniii.general_model_rmc.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/doctrees/coniii_rst/coniii.general_model_rmc.doctree -------------------------------------------------------------------------------- /docs/_build/doctrees/coniii_rst/coniii.ising.automaton.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/doctrees/coniii_rst/coniii.ising.automaton.doctree -------------------------------------------------------------------------------- /docs/_build/doctrees/coniii_rst/coniii.mean_field_ising.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/doctrees/coniii_rst/coniii.mean_field_ising.doctree -------------------------------------------------------------------------------- /docs/coniii_rst/coniii.utils.rst: -------------------------------------------------------------------------------- 1 | coniii.utils module 2 | =================== 3 | 4 | .. automodule:: coniii.utils 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/_build/doctrees/coniii_rst/coniii.ising.test_automaton.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/doctrees/coniii_rst/coniii.ising.test_automaton.doctree -------------------------------------------------------------------------------- /docs/_build/doctrees/coniii_rst/coniii.pseudo_inverse_ising.doctree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eltrompetero/coniii/HEAD/docs/_build/doctrees/coniii_rst/coniii.pseudo_inverse_ising.doctree -------------------------------------------------------------------------------- /docs/coniii_rst/coniii.mc_hist.rst: -------------------------------------------------------------------------------- 1 | coniii.mc\_hist module 2 | ====================== 3 | 4 | .. automodule:: coniii.mc_hist 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/coniii_rst/coniii.solvers.rst: -------------------------------------------------------------------------------- 1 | coniii.solvers module 2 | ===================== 3 | 4 | .. automodule:: coniii.solvers 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/coniii_rst/coniii.enumerate.rst: -------------------------------------------------------------------------------- 1 | coniii.enumerate module 2 | ======================= 3 | 4 | .. automodule:: coniii.enumerate 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/coniii_rst/coniii.samplers.rst: -------------------------------------------------------------------------------- 1 | coniii.samplers module 2 | ====================== 3 | 4 | .. automodule:: coniii.samplers 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/coniii_rst/coniii.test_utils.rst: -------------------------------------------------------------------------------- 1 | coniii.test\_utils module 2 | ========================= 3 | 4 | .. automodule:: coniii.test_utils 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/coniii_rst/coniii.test_solvers.rst: -------------------------------------------------------------------------------- 1 | coniii.test\_solvers module 2 | =========================== 3 | 4 | .. automodule:: coniii.test_solvers 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/coniii_rst/coniii.enumerate_potts.rst: -------------------------------------------------------------------------------- 1 | coniii.enumerate_potts module 2 | ======================= 3 | 4 | .. automodule:: coniii.enumerate_potts 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/coniii_rst/coniii.test_samplers.rst: -------------------------------------------------------------------------------- 1 | coniii.test\_samplers module 2 | ============================ 3 | 4 | .. automodule:: coniii.test_samplers 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/coniii_rst/coniii.ising.automaton.rst: -------------------------------------------------------------------------------- 1 | coniii.ising.automaton module 2 | ============================= 3 | 4 | .. automodule:: coniii.ising.automaton 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/coniii_rst/coniii.mean_field_ising.rst: -------------------------------------------------------------------------------- 1 | coniii.mean\_field\_ising module 2 | ================================ 3 | 4 | .. automodule:: coniii.mean_field_ising 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/coniii_rst/coniii.general_model_rmc.rst: -------------------------------------------------------------------------------- 1 | coniii.general\_model\_rmc module 2 | ================================= 3 | 4 | .. automodule:: coniii.general_model_rmc 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/coniii_rst/coniii.ising.test_automaton.rst: -------------------------------------------------------------------------------- 1 | coniii.ising.test\_automaton module 2 | =================================== 3 | 4 | .. automodule:: coniii.ising.test_automaton 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/coniii_rst/coniii.pseudo_inverse_ising.rst: -------------------------------------------------------------------------------- 1 | coniii.pseudo\_inverse\_ising module 2 | ==================================== 3 | 4 | .. automodule:: coniii.pseudo_inverse_ising 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/_build/html/.buildinfo: -------------------------------------------------------------------------------- 1 | # Sphinx build info version 1 2 | # This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done. 3 | config: d582b8ee7aca1b1fc78d792917fbc3cb 4 | tags: 645f666f9bcd5a90fca523b33c5a78b7 5 | -------------------------------------------------------------------------------- /docs/coniii_rst/coniii.ising.rst: -------------------------------------------------------------------------------- 1 | coniii.ising package 2 | ==================== 3 | 4 | Submodules 5 | ---------- 6 | 7 | .. toctree:: 8 | 9 | coniii.ising.automaton 10 | coniii.ising.test_automaton 11 | 12 | Module contents 13 | --------------- 14 | 15 | .. automodule:: coniii.ising 16 | :members: 17 | :undoc-members: 18 | :show-inheritance: 19 | -------------------------------------------------------------------------------- /docs/_build/html/_static/documentation_options.js: -------------------------------------------------------------------------------- 1 | var DOCUMENTATION_OPTIONS = { 2 | URL_ROOT: document.getElementById("documentation_options").getAttribute('data-url_root'), 3 | VERSION: '1.1.9', 4 | LANGUAGE: 'None', 5 | COLLAPSE_INDEX: false, 6 | BUILDER: 'html', 7 | FILE_SUFFIX: '.html', 8 | LINK_SUFFIX: '.html', 9 | HAS_SOURCE: true, 10 | SOURCELINK_SUFFIX: '.txt', 11 | NAVIGATION_WITH_KEYS: false 12 | }; -------------------------------------------------------------------------------- /profile/boost_profile.py: -------------------------------------------------------------------------------- 1 | # For profiling different variations on Metropolis sampler. 2 | from coniii.samplers import Metropolis 3 | from coniii.utils import define_ising_helper_functions 4 | import numpy as np 5 | 6 | calc_e = define_ising_helper_functions()[0] 7 | n = 10 8 | multipliers = np.random.normal(size=n+n*(n-1)//2, scale=.1) 9 | 10 | sboost = Metropolis(n, multipliers, calc_e, boost=True) 11 | spy = Metropolis(n, multipliers, calc_e) 12 | 13 | # copy following lines into ipython 14 | #%timeit sboost.generate_samples(100) 15 | #%timeit spy.generate_samples(100) 16 | #%timeit sboost.generate_samples_parallel(100) 17 | #%timeit spy.generate_samples_parallel(100) 18 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Minimal makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | SPHINXPROJ = ConIII 8 | SOURCEDIR = . 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/coniii_rst/coniii.rst: -------------------------------------------------------------------------------- 1 | coniii package 2 | ============== 3 | 4 | Subpackages 5 | ----------- 6 | 7 | .. toctree:: 8 | 9 | coniii.ising 10 | 11 | Submodules 12 | ---------- 13 | 14 | .. toctree:: 15 | 16 | coniii.custom_maxent 17 | coniii.enumerate 18 | coniii.general_model_rmc 19 | coniii.ising 20 | coniii.mc_hist 21 | coniii.mean_field_ising 22 | coniii.pseudo_inverse_ising 23 | coniii.samplers 24 | coniii.solvers 25 | coniii.test_samplers 26 | coniii.test_solvers 27 | coniii.test_utils 28 | coniii.utils 29 | 30 | Module contents 31 | --------------- 32 | 33 | .. automodule:: coniii 34 | :members: 35 | :undoc-members: 36 | :show-inheritance: 37 | -------------------------------------------------------------------------------- /docs/_build/html/_sources/index.txt: -------------------------------------------------------------------------------- 1 | .. ConIII documentation master file, created by 2 | sphinx-quickstart on Fri Oct 26 00:29:34 2018. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | Welcome to ConIII's documentation! 7 | ================================== 8 | 9 | .. automodule:: coniii.solvers 10 | .. toctree:: 11 | :maxdepth: 2 12 | :caption: Contents: 13 | 14 | coniii_rst/coniii.enumerate.rst 15 | coniii_rst/coniii.solvers.rst 16 | coniii_rst/coniii.samplers.rst 17 | coniii_rst/coniii.utils.rst 18 | 19 | Indices and tables 20 | ================== 21 | 22 | * :ref:`genindex` 23 | * :ref:`modindex` 24 | * :ref:`search` 25 | -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | .. ConIII documentation master file, created by 2 | sphinx-quickstart on Tue Apr 23 02:36:33 2019. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | Welcome to ConIII's documentation! 7 | ================================== 8 | 9 | .. toctree:: 10 | :maxdepth: 2 11 | :caption: Contents: 12 | 13 | coniii_rst/coniii.enumerate.rst 14 | coniii_rst/coniii.enumerate_potts.rst 15 | coniii_rst/coniii.ising.rst 16 | coniii_rst/coniii.solvers.rst 17 | coniii_rst/coniii.samplers.rst 18 | coniii_rst/coniii.utils.rst 19 | 20 | Indices and tables 21 | ================== 22 | 23 | * :ref:`genindex` 24 | * :ref:`modindex` 25 | * :ref:`search` 26 | -------------------------------------------------------------------------------- /docs/_build/html/_sources/index.rst.txt: -------------------------------------------------------------------------------- 1 | .. ConIII documentation master file, created by 2 | sphinx-quickstart on Tue Apr 23 02:36:33 2019. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | Welcome to ConIII's documentation! 7 | ================================== 8 | 9 | .. toctree:: 10 | :maxdepth: 2 11 | :caption: Contents: 12 | 13 | coniii_rst/coniii.enumerate.rst 14 | coniii_rst/coniii.enumerate_potts.rst 15 | coniii_rst/coniii.ising.rst 16 | coniii_rst/coniii.solvers.rst 17 | coniii_rst/coniii.samplers.rst 18 | coniii_rst/coniii.utils.rst 19 | 20 | Indices and tables 21 | ================== 22 | 23 | * :ref:`genindex` 24 | * :ref:`modindex` 25 | * :ref:`search` 26 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Edward D. Lee, Bryan C. Daniels 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /coniii/__init__.py: -------------------------------------------------------------------------------- 1 | # MIT License 2 | # 3 | # Copyright (c) 2020 Edward D. Lee, Bryan C. Daniels 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | 23 | from .solvers import MCH,MPF,Pseudo,ClusterExpansion,Enumerate,RegularizedMeanField 24 | from .utils import * 25 | from .version import version as __version__ 26 | -------------------------------------------------------------------------------- /coniii/write_ising_files.sh: -------------------------------------------------------------------------------- 1 | # MIT License 2 | # 3 | # Copyright (c) 2019 Edward D. Lee, Bryan C. Daniels 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | #!/bin/bash 23 | 24 | for i in `seq 2 9`; 25 | do 26 | python enumerate.py $i 27 | python enumerate.py $i 1 28 | done 29 | 30 | # Triplet interaction files 31 | python enumerate.py 5 0 3 32 | python enumerate.py 5 1 3 33 | -------------------------------------------------------------------------------- /guide/jors.cls: -------------------------------------------------------------------------------- 1 | %% Journal of Open Research Software Latex template -- Created By Stephen Bonner and John Brennan, Durham Universtiy, UK. 2 | 3 | \NeedsTeXFormat{LaTeX2e} 4 | \ProvidesClass{josr}[2016/03/08 Journal Of Open Software Research] 5 | 6 | %% Article options 7 | \DeclareOption{12pt}{ 8 | \PassOptionsToClass{\CurrentOption}{article} 9 | } 10 | 11 | 12 | \DeclareOption{sansserif}{ 13 | \PassOptionsToPackage{\CurrentOption}{paxcommands} 14 | } 15 | \DeclareOption{neverindent}{ 16 | \PassOptionsToPackage{\CurrentOption}{paxcommands} 17 | } 18 | 19 | %% Fallback 20 | \DeclareOption*{ 21 | \ClassWarning{josr}{Unknown option '\CurrentOption'} 22 | } 23 | 24 | 25 | \ExecuteOptions{12pt} 26 | 27 | 28 | \ProcessOptions\relax 29 | 30 | \LoadClass[a4paper]{article} 31 | 32 | %% Load additional packages and commands. 33 | \RequirePackage{xcolor} 34 | \RequirePackage{sectsty} 35 | \RequirePackage{enumitem} 36 | \RequirePackage{hyperref} 37 | \RequirePackage{fancyhdr} 38 | \RequirePackage{titlesec} 39 | 40 | %% Additional TeX/LaTeX code... 41 | 42 | %% Remove the indentation 43 | \newlength\tindent 44 | \setlength{\tindent}{\parindent} 45 | \setlength{\parindent}{0pt} 46 | \renewcommand{\indent}{\hspace*{\tindent}} 47 | 48 | %% Remove the page numbers 49 | \pagenumbering{gobble} 50 | 51 | %% Set the font too 13 for the titles 52 | \sectionfont{\fontsize{13}{15}\selectfont} 53 | 54 | %% Set indentation for the lists 55 | \setlist[description]{leftmargin=1cm,labelindent=1cm} 56 | 57 | %% Set spacing for the section headings 58 | \titlespacing\section{0pt}{12pt plus 4pt minus 2pt}{0pt plus 2pt minus 2pt} 59 | 60 | %% Set margins 61 | \usepackage[margin=1.2in,footskip=0.25in]{geometry} 62 | 63 | \endinput -------------------------------------------------------------------------------- /pypi_compile.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Code for compiling package for upload to PyPI. 4 | # Clean previous compilation results. 5 | if [ ! `command -v trash` ] 6 | then 7 | echo "trash-cli is not installed. Cannot empty dist directory safely." 8 | else 9 | if [ -d build ]; then 10 | trash build 11 | fi 12 | if [ -d dist ]; then 13 | trash dist 14 | fi 15 | fi 16 | find ./ -name *.pyc -exec rm {} \; 17 | 18 | # Update cpp code (DEPRECATED) 19 | # rsync -au ../../cpp/cppsamplers/cppsamplers/*.*pp cpp/ 20 | 21 | # Apply current conda environment 22 | conda init $CONDA_DEFAULT_ENV 23 | 24 | # Compile wheels into dist folder. 25 | python setup.py bdist_wheel 26 | # Make source available 27 | python setup.py sdist 28 | 29 | # Rename Linux wheel for upload to PyPI. 30 | unamestr=`uname` 31 | if [[ "$unamestr" == 'Linux' ]]; then 32 | if [ ! `command -v rename` ] 33 | then 34 | echo "rename is not installed. wheel not renamed." 35 | exit 1 36 | fi 37 | rename 's/linux/manylinux1/' dist/* 38 | fi 39 | 40 | # For pypi upload 41 | if [ "$1" == "--all" ] 42 | then 43 | # Update usage guide to latest version for upload to PyPI. 44 | cp ipynb/usage_guide.ipynb coniii/ 45 | 46 | # Compile docs 47 | sphinx-build ./docs/ ./docs/_build/html 48 | 49 | echo "rsync -au docs/_build/html/* ~/Dropbox/Documents/eltrompetero.github.io/coniii/" 50 | rsync -au docs/_build/html/* ~/Dropbox/Documents/eltrompetero.github.io/coniii/ 51 | fi 52 | 53 | # check if boost module compiled 54 | has_dirs() { 55 | for f do 56 | [ -d "$f" ] && return 57 | done 58 | false 59 | } 60 | 61 | if compgen -G "./build/lib.*/coniii/samplers_ext*.so" > /dev/null; then 62 | echo "********************************" 63 | echo "Boost module built successfully." 64 | echo "********************************" 65 | else 66 | echo "*****************************" 67 | echo "Failed to build Boost module." 68 | echo "*****************************" 69 | fi 70 | -------------------------------------------------------------------------------- /coniii/ising/test_utils.py: -------------------------------------------------------------------------------- 1 | # ===================================================================================== # 2 | # Distributed as part of ConIII. 3 | # Author : Edward Lee, edlee@alumni.princeton.edu 4 | # ===================================================================================== # 5 | # 6 | # MIT License 7 | # 8 | # Copyright (c) 2019 Edward D. Lee, Bryan C. Daniels 9 | # 10 | # Permission is hereby granted, free of charge, to any person obtaining a copy 11 | # of this software and associated documentation files (the "Software"), to deal 12 | # in the Software without restriction, including without limitation the rights 13 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | # copies of the Software, and to permit persons to whom the Software is 15 | # furnished to do so, subject to the following conditions: 16 | # 17 | # The above copyright notice and this permission notice shall be included in all 18 | # copies or substantial portions of the Software. 19 | # 20 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | # SOFTWARE. 27 | from .utils import * 28 | 29 | 30 | def test_Ising(): 31 | n = 5 32 | model = Ising(n, h=-10, J=.1) 33 | assert model.hJ.size==(n*(n-1)//2+n) 34 | assert model.Jmat.shape==(n,n) 35 | assert np.array_equal(model.find_basin(np.ones(n)), -np.ones(n)) 36 | assert np.array_equal(model.find_basin(-np.ones(n)), -np.ones(n)) 37 | 38 | model.correlations() 39 | model.correlations('1') 40 | model.fields() 41 | model.fields('1') 42 | model.couplings() 43 | model.couplings('1') 44 | -------------------------------------------------------------------------------- /DEVREADME: -------------------------------------------------------------------------------- 1 | # ====================================================================================== # 2 | # Troubleshooting notes. 3 | # Author: Eddie Lee, edlee@santafe.edu 4 | # ====================================================================================== # 5 | 6 | Notes for how to compile for PyPI 7 | --------------------------------- 8 | - In your project, create a folder for your Python module. Other setup files will go in 9 | the directory above that folder. 10 | - Write setup.py with options to specify compile options. Can use either 11 | distutils.core.setup or setuptools.setup (latter is supposed to be more user friendly). 12 | - Delete the dist and build directories. 13 | - Compile module with `python setup.py bdist_wheel` This will create a dist folder in the 14 | folder that will be uploaded. 15 | - For Linux distributions, the wheel must be renamed to be for manylinux1 as in 16 | PACKAGE-VERSION-cp27-cp27mu-manylinux1_x86_64.whl 17 | - Upload to PyPI test server `twine upload --repository testpypi dist/*` 18 | - Install it with `pip install --index-url https://test.pypi.org/simple/ your-package` 19 | Make sure you change out of the project direcotry otherwise pip will think the project 20 | is already installed because it's currently in your path. 21 | - Once the project has been tested, upload to PyPI test server `twine upload --repository 22 | pypi dist/*` 23 | 24 | - Settings are saved in ~/.pypirc 25 | 26 | Refs 27 | ---- 28 | https://setuptools.readthedocs.io/en/latest/setuptools.html#including-data-files 29 | 30 | 31 | Notes on Boost extension 32 | ------------------------ 33 | - Boost library for python and numpy must be on both LIBRARY_PATH and LD_LIBRARY_PATH for 34 | module to function. The former is used by gcc for compilation and the latter is used by 35 | your program after it has been compiled. 36 | - For compilation, these libraries must also be included in CPLUS_INCLUDE_PATH. While 37 | including the path to the anaconda environment include directory should work, setting this 38 | env variable explicitly is also a solution. 39 | - Header files paths (e.g., "boost/python/numpy.hpp") must be on include path variable. 40 | These might need to be specified additionally in the include_dirs argument in setup.py 41 | if the bash environment is not set up properly. 42 | - If you see an ld error, there is a problem with linking the dynamic libraries that 43 | should be on your system. Copy the g++ call and add the "-v" flag to see what is 44 | happening in detail. 45 | -------------------------------------------------------------------------------- /cpp/py.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // 3 | // Copyright (c) 2020 Edward D. Lee, Bryan C. Daniels 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #define BOOST_TEST_DYN_LINK 24 | #include 25 | #include 26 | #include 27 | #include "samplers.hpp" 28 | 29 | namespace py = boost::python; 30 | namespace np = boost::python::numpy; 31 | 32 | // thin wrappers (for keeping default args -- doesn't work) 33 | //void (Metropolis::*generate_sample1)(int, int, int) = &Metropolis::generate_sample; 34 | 35 | struct potts3_pickle_suite : py::pickle_suite 36 | { 37 | static py::tuple getinitargs(Potts3 &w) { 38 | return py::make_tuple(w.n, w.multipliers2ndarray(), -1); 39 | } 40 | }; 41 | 42 | BOOST_PYTHON_MODULE(samplers_ext) { 43 | using namespace boost::python; 44 | Py_Initialize(); 45 | np::initialize(); 46 | 47 | class_("BoostPotts3", init()) 48 | .def("generate_sample", &Potts3::generate_sample) 49 | .def("fetch_sample", &Potts3::fetch_sample) 50 | .def_pickle(potts3_pickle_suite()) 51 | ; 52 | 53 | class_("BoostIsing", init()) 54 | .def("generate_sample", &Ising::generate_sample) 55 | .def("generate_cond_sample", &Ising::generate_cond_sample) 56 | .def("fetch_sample", &Ising::fetch_sample) 57 | .def("readin_multipliers", &Ising::readin_multipliers) 58 | ; 59 | }; 60 | -------------------------------------------------------------------------------- /coniii/ising_eqn/ising_eqn_2.py: -------------------------------------------------------------------------------- 1 | # MIT License 2 | # 3 | # Copyright (c) 2019 Edward D. Lee, Bryan C. Daniels 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | 23 | # Equations for 2-spin Ising model. 24 | 25 | # Written on 2019/09/19. 26 | from numpy import zeros, exp, array, prod, isnan 27 | from ..enumerate import fast_logsumexp 28 | 29 | def calc_observables(params): 30 | """ 31 | Give all parameters concatenated into one array from lowest to highest order. 32 | Returns all correlations. 33 | """ 34 | Cout = zeros((3)) 35 | H = params[0:2] 36 | J = params[2:3] 37 | energyTerms = array([ +0, +H[1]+0, +H[0]+0, +H[0]+H[1]+J[0],]) 38 | logZ = fast_logsumexp(energyTerms)[0] 39 | num = fast_logsumexp(energyTerms, [0,0,1,1]) 40 | Cout[0] = exp( num[0] - logZ ) * num[1] 41 | num = fast_logsumexp(energyTerms, [0,1,0,1]) 42 | Cout[1] = exp( num[0] - logZ ) * num[1] 43 | num = fast_logsumexp(energyTerms, [0,0,0,1]) 44 | Cout[2] = exp( num[0] - logZ ) * num[1] 45 | Cout[isnan(Cout)] = 0. 46 | return(Cout) 47 | 48 | def p(params): 49 | """ 50 | Give all parameters concatenated into one array from lowest to highest order. 51 | Returns probabilities of all configurations. 52 | """ 53 | Cout = zeros((3)) 54 | H = params[0:2] 55 | J = params[2:3] 56 | H = params[0:2] 57 | J = params[2:3] 58 | Pout = zeros((4)) 59 | energyTerms = array([ +0, +H[1]+0, +H[0]+0, +H[0]+H[1]+J[0],]) 60 | logZ = fast_logsumexp(energyTerms)[0] 61 | Pout[0] = exp( +0 - logZ ) 62 | Pout[1] = exp( +H[1]+0 - logZ ) 63 | Pout[2] = exp( +H[0]+0 - logZ ) 64 | Pout[3] = exp( +H[0]+H[1]+J[0] - logZ ) 65 | 66 | return(Pout) 67 | -------------------------------------------------------------------------------- /coniii/ising_eqn/ising_eqn_2_sym.py: -------------------------------------------------------------------------------- 1 | # MIT License 2 | # 3 | # Copyright (c) 2019 Edward D. Lee, Bryan C. Daniels 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | 23 | # Equations for 2-spin Ising model. 24 | 25 | # Written on 2019/09/19. 26 | from numpy import zeros, exp, array, prod, isnan 27 | from ..enumerate import fast_logsumexp 28 | 29 | def calc_observables(params): 30 | """ 31 | Give all parameters concatenated into one array from lowest to highest order. 32 | Returns all correlations. 33 | """ 34 | Cout = zeros((3)) 35 | H = params[0:2] 36 | J = params[2:3] 37 | energyTerms = array([ +H[0]+H[1]+J[0], +H[0]-H[1]-J[0], -H[0]+H[1]-J[0], -H[0]-H[1]+J[0],]) 38 | logZ = fast_logsumexp(energyTerms)[0] 39 | num = fast_logsumexp(energyTerms, [ 1, 1,-1,-1]) 40 | Cout[0] = exp( num[0] - logZ ) * num[1] 41 | num = fast_logsumexp(energyTerms, [ 1,-1, 1,-1]) 42 | Cout[1] = exp( num[0] - logZ ) * num[1] 43 | num = fast_logsumexp(energyTerms, [ 1,-1,-1, 1]) 44 | Cout[2] = exp( num[0] - logZ ) * num[1] 45 | Cout[isnan(Cout)] = 0. 46 | return(Cout) 47 | 48 | def p(params): 49 | """ 50 | Give all parameters concatenated into one array from lowest to highest order. 51 | Returns probabilities of all configurations. 52 | """ 53 | Cout = zeros((3)) 54 | H = params[0:2] 55 | J = params[2:3] 56 | H = params[0:2] 57 | J = params[2:3] 58 | Pout = zeros((4)) 59 | energyTerms = array([ +H[0]+H[1]+J[0], +H[0]-H[1]-J[0], -H[0]+H[1]-J[0], -H[0]-H[1]+J[0],]) 60 | logZ = fast_logsumexp(energyTerms)[0] 61 | Pout[0] = exp( +H[0]+H[1]+J[0] - logZ ) 62 | Pout[1] = exp( +H[0]-H[1]-J[0] - logZ ) 63 | Pout[2] = exp( -H[0]+H[1]-J[0] - logZ ) 64 | Pout[3] = exp( -H[0]-H[1]+J[0] - logZ ) 65 | 66 | Pout = Pout[::-1] 67 | return(Pout) 68 | -------------------------------------------------------------------------------- /coniii/test_enumerate_potts.py: -------------------------------------------------------------------------------- 1 | # ===================================================================================== # 2 | # Test suite for enumerate_potts.py 3 | # Author : Edward Lee, edlee@alumni.princeton.edu 4 | # 5 | # MIT License 6 | # 7 | # Copyright (c) 2019 Edward D. Lee, Bryan C. Daniels 8 | # 9 | # Permission is hereby granted, free of charge, to any person obtaining a copy 10 | # of this software and associated documentation files (the "Software"), to deal 11 | # in the Software without restriction, including without limitation the rights 12 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | # copies of the Software, and to permit persons to whom the Software is 14 | # furnished to do so, subject to the following conditions: 15 | # 16 | # The above copyright notice and this permission notice shall be included in all 17 | # copies or substantial portions of the Software. 18 | # 19 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | # SOFTWARE. 26 | # ===================================================================================== # 27 | import numpy as np 28 | import mpmath as mp 29 | from .utils import pair_corr, bin_states 30 | from .enumerate_potts import * 31 | import os 32 | import importlib 33 | np.random.seed(0) 34 | 35 | 36 | def single_pass(n,k): 37 | # write equations file for testing 38 | writer = SpecificFieldGenericCouplings(n, k) 39 | writer.write('ising_eqn/_test_enumerate_potts%d.py'%n) 40 | 41 | ising = importlib.import_module('.ising_eqn._test_enumerate_potts%d'%n, package='coniii') 42 | hJ = np.random.normal(size=n*k+n*(n-1)//2, scale=.2) 43 | 44 | p = ising.p(hJ) 45 | assert np.isclose(p.sum(), 1) 46 | print("Test passed: Normalized probability.") 47 | assert ((ising.calc_observables(hJ)<=1)&(ising.calc_observables(hJ)>=0)).all() 48 | print("Test passed: correlations bounded in [0,1].") 49 | 50 | allStates = np.vstack(list(xpotts_states(n,k))).astype(np.int8) 51 | sisj = ising.calc_observables(hJ) 52 | si = sisj[:k*n] 53 | sisj = sisj[n*k:] 54 | for i in range(n): 55 | for k_ in range(k): 56 | assert np.isclose(si[n*k_+i], (allStates[:,i]==k_).dot(p)) 57 | for ijix,(i,j) in enumerate(combinations(range(n),2)): 58 | assert np.isclose(sisj[ijix], (allStates[:,i]==allStates[:,j]).dot(p)) 59 | print("Test passed: correlations calculated from probability distribution agree with direct calculation.") 60 | 61 | def test_basic(): 62 | try: 63 | n = 3 64 | k = 3 65 | single_pass(n,k) 66 | finally: 67 | # cleanup 68 | os.remove('ising_eqn/_test_enumerate_potts%d.py'%n) 69 | 70 | try: 71 | n = 4 72 | k = 3 73 | single_pass(n,k) 74 | finally: 75 | # cleanup 76 | os.remove('ising_eqn/_test_enumerate_potts%d.py'%n) 77 | -------------------------------------------------------------------------------- /coniii/ising_eqn/ising_eqn_3.py: -------------------------------------------------------------------------------- 1 | # MIT License 2 | # 3 | # Copyright (c) 2019 Edward D. Lee, Bryan C. Daniels 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | 23 | # Equations for 3-spin Ising model. 24 | 25 | # Written on 2019/09/19. 26 | from numpy import zeros, exp, array, prod, isnan 27 | from ..enumerate import fast_logsumexp 28 | 29 | def calc_observables(params): 30 | """ 31 | Give all parameters concatenated into one array from lowest to highest order. 32 | Returns all correlations. 33 | """ 34 | Cout = zeros((6)) 35 | H = params[0:3] 36 | J = params[3:6] 37 | energyTerms = array([ +0, +H[2]+0, +H[1]+0, +H[1]+H[2]+J[2], +H[0]+0, +H[0]+H[2]+J[1], +H[0]+H[1]+J[0], +H[0]+H[1]+H[2]+J[0]+ 38 | J[1]+J[2],]) 39 | logZ = fast_logsumexp(energyTerms)[0] 40 | num = fast_logsumexp(energyTerms, [0,0,0,0,1,1,1,1]) 41 | Cout[0] = exp( num[0] - logZ ) * num[1] 42 | num = fast_logsumexp(energyTerms, [0,0,1,1,0,0,1,1]) 43 | Cout[1] = exp( num[0] - logZ ) * num[1] 44 | num = fast_logsumexp(energyTerms, [0,1,0,1,0,1,0,1]) 45 | Cout[2] = exp( num[0] - logZ ) * num[1] 46 | num = fast_logsumexp(energyTerms, [0,0,0,0,0,0,1,1]) 47 | Cout[3] = exp( num[0] - logZ ) * num[1] 48 | num = fast_logsumexp(energyTerms, [0,0,0,0,0,1,0,1]) 49 | Cout[4] = exp( num[0] - logZ ) * num[1] 50 | num = fast_logsumexp(energyTerms, [0,0,0,1,0,0,0,1]) 51 | Cout[5] = exp( num[0] - logZ ) * num[1] 52 | Cout[isnan(Cout)] = 0. 53 | return(Cout) 54 | 55 | def p(params): 56 | """ 57 | Give all parameters concatenated into one array from lowest to highest order. 58 | Returns probabilities of all configurations. 59 | """ 60 | Cout = zeros((6)) 61 | H = params[0:3] 62 | J = params[3:6] 63 | H = params[0:3] 64 | J = params[3:6] 65 | Pout = zeros((8)) 66 | energyTerms = array([ +0, +H[2]+0, +H[1]+0, +H[1]+H[2]+J[2], +H[0]+0, +H[0]+H[2]+J[1], +H[0]+H[1]+J[0], +H[0]+H[1]+H[2]+J[0]+ 67 | J[1]+J[2],]) 68 | logZ = fast_logsumexp(energyTerms)[0] 69 | Pout[0] = exp( +0 - logZ ) 70 | Pout[1] = exp( +H[2]+0 - logZ ) 71 | Pout[2] = exp( +H[1]+0 - logZ ) 72 | Pout[3] = exp( +H[1]+H[2]+J[2] - logZ ) 73 | Pout[4] = exp( +H[0]+0 - logZ ) 74 | Pout[5] = exp( +H[0]+H[2]+J[1] - logZ ) 75 | Pout[6] = exp( +H[0]+H[1]+J[0] - logZ ) 76 | Pout[7] = exp( +H[0]+H[1]+H[2]+J[0]+J[1]+J[2] - logZ ) 77 | 78 | return(Pout) 79 | -------------------------------------------------------------------------------- /docs/_build/html/search.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Search — ConIII 1.1.9 documentation 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 |
32 |
33 |
34 | 35 | 36 |
37 | 38 |

Search

39 |
40 | 41 |

42 | Please activate JavaScript to enable the search 43 | functionality. 44 |

45 |
46 |

47 | Searching for multiple words only shows matches that contain 48 | all words. 49 |

50 |
51 | 52 | 53 | 54 |
55 | 56 |
57 | 58 |
59 | 60 |
61 | 62 |
63 |
64 | 103 |
104 |
105 | 113 | 114 | 115 | 116 | 117 | 118 | -------------------------------------------------------------------------------- /docs/_build/html/coniii_rst/coniii.mc_hist.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | coniii.mc_hist module — ConIII 1.1.9 documentation 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 |
29 |
30 | 31 | 32 |
33 | 34 |
35 |

coniii.mc_hist module

36 |
37 | 38 | 39 |
40 | 41 |
42 |
43 | 92 |
93 |
94 | 105 | 106 | 107 | 108 | 109 | 110 | -------------------------------------------------------------------------------- /docs/_build/html/coniii_rst/coniii.general_model_rmc.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | coniii.general_model_rmc module — ConIII 1.1.9 documentation 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 |
29 |
30 | 31 | 32 |
33 | 34 |
35 |

coniii.general_model_rmc module

36 |
37 | 38 | 39 |
40 | 41 |
42 |
43 | 92 |
93 |
94 | 105 | 106 | 107 | 108 | 109 | 110 | -------------------------------------------------------------------------------- /coniii/ising_eqn/ising_eqn_3_sym.py: -------------------------------------------------------------------------------- 1 | # MIT License 2 | # 3 | # Copyright (c) 2019 Edward D. Lee, Bryan C. Daniels 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | 23 | # Equations for 3-spin Ising model. 24 | 25 | # Written on 2019/09/19. 26 | from numpy import zeros, exp, array, prod, isnan 27 | from ..enumerate import fast_logsumexp 28 | 29 | def calc_observables(params): 30 | """ 31 | Give all parameters concatenated into one array from lowest to highest order. 32 | Returns all correlations. 33 | """ 34 | Cout = zeros((6)) 35 | H = params[0:3] 36 | J = params[3:6] 37 | energyTerms = array([ +H[0]+H[1]+H[2]+J[0]+J[1]+J[2], +H[0]+H[1]-H[2]+J[0]-J[1]-J[2], +H[0]-H[1]+H[2]-J[0]+J[1]-J[2], +H[0]-H[1]-H[2]-J[0]-J[1]+ 38 | J[2], -H[0]+H[1]+H[2]-J[0]-J[1]+J[2], -H[0]+H[1]-H[2]-J[0]+J[1]-J[2], -H[0]-H[1]+H[2]+J[0]-J[1]-J[2], -H[0]-H[1]-H[2]+ 39 | J[0]+J[1]+J[2],]) 40 | logZ = fast_logsumexp(energyTerms)[0] 41 | num = fast_logsumexp(energyTerms, [ 1, 1, 1, 1,-1,-1,-1,-1]) 42 | Cout[0] = exp( num[0] - logZ ) * num[1] 43 | num = fast_logsumexp(energyTerms, [ 1, 1,-1,-1, 1, 1,-1,-1]) 44 | Cout[1] = exp( num[0] - logZ ) * num[1] 45 | num = fast_logsumexp(energyTerms, [ 1,-1, 1,-1, 1,-1, 1,-1]) 46 | Cout[2] = exp( num[0] - logZ ) * num[1] 47 | num = fast_logsumexp(energyTerms, [ 1, 1,-1,-1,-1,-1, 1, 1]) 48 | Cout[3] = exp( num[0] - logZ ) * num[1] 49 | num = fast_logsumexp(energyTerms, [ 1,-1, 1,-1,-1, 1,-1, 1]) 50 | Cout[4] = exp( num[0] - logZ ) * num[1] 51 | num = fast_logsumexp(energyTerms, [ 1,-1,-1, 1, 1,-1,-1, 1]) 52 | Cout[5] = exp( num[0] - logZ ) * num[1] 53 | Cout[isnan(Cout)] = 0. 54 | return(Cout) 55 | 56 | def p(params): 57 | """ 58 | Give all parameters concatenated into one array from lowest to highest order. 59 | Returns probabilities of all configurations. 60 | """ 61 | Cout = zeros((6)) 62 | H = params[0:3] 63 | J = params[3:6] 64 | H = params[0:3] 65 | J = params[3:6] 66 | Pout = zeros((8)) 67 | energyTerms = array([ +H[0]+H[1]+H[2]+J[0]+J[1]+J[2], +H[0]+H[1]-H[2]+J[0]-J[1]-J[2], +H[0]-H[1]+H[2]-J[0]+J[1]-J[2], +H[0]-H[1]-H[2]-J[0]-J[1]+ 68 | J[2], -H[0]+H[1]+H[2]-J[0]-J[1]+J[2], -H[0]+H[1]-H[2]-J[0]+J[1]-J[2], -H[0]-H[1]+H[2]+J[0]-J[1]-J[2], -H[0]-H[1]-H[2]+ 69 | J[0]+J[1]+J[2],]) 70 | logZ = fast_logsumexp(energyTerms)[0] 71 | Pout[0] = exp( +H[0]+H[1]+H[2]+J[0]+J[1]+J[2] - logZ ) 72 | Pout[1] = exp( +H[0]+H[1]-H[2]+J[0]-J[1]-J[2] - logZ ) 73 | Pout[2] = exp( +H[0]-H[1]+H[2]-J[0]+J[1]-J[2] - logZ ) 74 | Pout[3] = exp( +H[0]-H[1]-H[2]-J[0]-J[1]+J[2] - logZ ) 75 | Pout[4] = exp( -H[0]+H[1]+H[2]-J[0]-J[1]+J[2] - logZ ) 76 | Pout[5] = exp( -H[0]+H[1]-H[2]-J[0]+J[1]-J[2] - logZ ) 77 | Pout[6] = exp( -H[0]-H[1]+H[2]+J[0]-J[1]-J[2] - logZ ) 78 | Pout[7] = exp( -H[0]-H[1]-H[2]+J[0]+J[1]+J[2] - logZ ) 79 | 80 | Pout = Pout[::-1] 81 | return(Pout) 82 | -------------------------------------------------------------------------------- /docs/_build/html/coniii_rst/coniii.custom_maxent.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | coniii.custom_maxent module — ConIII 1.1.9 documentation 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 |
29 |
30 | 31 | 32 |
33 | 34 |
35 |

coniii.custom_maxent module

36 |
37 | 38 | 39 |
40 | 41 |
42 |
43 | 85 |
86 |
87 | 98 | 99 | 100 | 101 | 102 | 103 | -------------------------------------------------------------------------------- /cpp/samplers.hpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // 3 | // Copyright (c) 2020 Edward D. Lee, Bryan C. Daniels 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #ifndef samplers_hpp 24 | #define samplers_hpp 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | #endif /* samplers_hpp */ 39 | 40 | namespace py = boost::python; 41 | namespace np = boost::python::numpy; 42 | 43 | 44 | // Base sampling class 45 | class Sampler { 46 | public: 47 | int n; // system size 48 | std::vector> couplingMat; 49 | std::vector multipliers; // fields and couplings 50 | std::vector> sample; // stored sample 51 | int seed; 52 | std::mt19937_64 rd; 53 | std::uniform_real_distribution unitrng; 54 | 55 | Sampler(); 56 | Sampler(int, std::vector, int=-1); // init with multipliers 57 | Sampler(int, np::ndarray, int=-1); 58 | 59 | virtual double calc_e(std::vector const&) = 0; 60 | virtual double sample_metropolis(std::vector&, int const) = 0; 61 | void generate_sample(int const, 62 | int const, 63 | int const, 64 | bool const=false); 65 | void generate_cond_sample(np::ndarray, 66 | np::ndarray, 67 | int const, 68 | int const, 69 | int const, 70 | bool const=false); 71 | np::ndarray fetch_sample(); 72 | std::vector means(); 73 | void print(int const); 74 | void readin_fixed_set(std::vector&, std::vector&, np::ndarray, np::ndarray); 75 | 76 | private: 77 | virtual std::vector init_sample() = 0; 78 | }; 79 | 80 | 81 | // sampling for Ising model 82 | class Ising : public Sampler { 83 | public: 84 | Ising(); 85 | Ising(int, std::vector, int=-1); 86 | Ising(int, np::ndarray, int=-1); 87 | 88 | double calc_e(std::vector const&); 89 | double sample_metropolis(std::vector&, int const); 90 | void readin_multipliers(np::ndarray); 91 | private: 92 | std::vector init_sample(); 93 | }; 94 | 95 | 96 | // 3-state Potts sampling 97 | class Potts3 : public Sampler { 98 | public: 99 | Potts3(); 100 | Potts3(int, std::vector, int=-1); 101 | Potts3(int, np::ndarray, int=-1); 102 | 103 | double calc_e(std::vector const&); 104 | double sample_metropolis(std::vector&, int const); 105 | py::tuple get_value(); 106 | py::tuple get_state(); 107 | void set_state(py::tuple); 108 | np::ndarray multipliers2ndarray(); 109 | private: 110 | std::uniform_int_distribution staterng; 111 | std::vector init_sample(); 112 | void readin_multipliers(np::ndarray); 113 | }; 114 | -------------------------------------------------------------------------------- /coniii/test_enumerate.py: -------------------------------------------------------------------------------- 1 | # ===================================================================================== # 2 | # Test suite for enumerate.py 3 | # Author : Edward Lee, edlee@alumni.princeton.edu 4 | # 5 | # MIT License 6 | # 7 | # Copyright (c) 2019 Edward D. Lee, Bryan C. Daniels 8 | # 9 | # Permission is hereby granted, free of charge, to any person obtaining a copy 10 | # of this software and associated documentation files (the "Software"), to deal 11 | # in the Software without restriction, including without limitation the rights 12 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | # copies of the Software, and to permit persons to whom the Software is 14 | # furnished to do so, subject to the following conditions: 15 | # 16 | # The above copyright notice and this permission notice shall be included in all 17 | # copies or substantial portions of the Software. 18 | # 19 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | # SOFTWARE. 26 | # ===================================================================================== # 27 | import numpy as np 28 | import mpmath as mp 29 | from .utils import pair_corr, bin_states 30 | from .enumerate import fast_logsumexp, mp_fast_logsumexp 31 | np.random.seed(0) 32 | 33 | 34 | def test_basic(): 35 | hJ = np.random.normal(size=6,scale=.2) 36 | 37 | # make sure probability distribution is normalized, p and correlations agree for both symmetrized and 38 | # unsymmetrized bases 39 | # n=3 40 | from .ising_eqn import ising_eqn_3_sym as ising 41 | p = ising.p(hJ) 42 | assert np.isclose(p.sum(), 1) 43 | assert ((ising.calc_observables(hJ)<=1)&(ising.calc_observables(hJ)>=-1)).all() 44 | assert np.isclose(ising.calc_observables(hJ), 45 | pair_corr(bin_states(3,True), weights=ising.p(hJ), concat=True)).all() 46 | 47 | from .ising_eqn import ising_eqn_3 as ising 48 | p = ising.p(hJ) 49 | assert np.isclose(p.sum(), 1) 50 | assert ((ising.calc_observables(hJ)<=1)&(ising.calc_observables(hJ)>=0)).all() 51 | assert np.isclose(ising.calc_observables(hJ), 52 | pair_corr(bin_states(3), weights=ising.p(hJ), concat=True)).all() 53 | 54 | 55 | # n=4 56 | hJ = np.random.normal(size=10, scale=.2) 57 | 58 | from .ising_eqn import ising_eqn_4_sym as ising 59 | p = ising.p(hJ) 60 | assert np.isclose(p.sum(), 1) 61 | assert ((ising.calc_observables(hJ)<=1)&(ising.calc_observables(hJ)>=-1)).all() 62 | assert np.isclose(ising.calc_observables(hJ), 63 | pair_corr(bin_states(4,True), weights=ising.p(hJ), concat=True)).all() 64 | 65 | from .ising_eqn import ising_eqn_4 as ising 66 | p = ising.p(hJ) 67 | assert np.isclose(p.sum(), 1) 68 | assert ((ising.calc_observables(hJ)<=1)&(ising.calc_observables(hJ)>=0)).all() 69 | assert np.isclose(ising.calc_observables(hJ), 70 | pair_corr(bin_states(4), weights=ising.p(hJ), concat=True)).all() 71 | 72 | # n=4, high precision 73 | hJ = np.array(list(map(mp.mpf, np.random.normal(size=10, scale=.2)))) 74 | 75 | from .ising_eqn import ising_eqn_4_sym_hp as ising 76 | p = ising.p(hJ) 77 | assert np.isclose(float(p.sum()), 1) 78 | assert ((ising.calc_observables(hJ)<=1)&(ising.calc_observables(hJ)>=-1)).all() 79 | assert np.isclose(ising.calc_observables(hJ).astype(float), 80 | pair_corr(bin_states(4,sym=True), weights=ising.p(hJ).astype(float), concat=True)).all() 81 | 82 | def test_fast_logsumexp(): 83 | from scipy.special import logsumexp 84 | 85 | X = np.random.normal(size=10, scale=10, loc=1000) 86 | coeffs = np.random.choice([-1,1], size=X.size) 87 | 88 | npval = logsumexp(X, b=coeffs, return_sign=True) 89 | assert np.array_equal(fast_logsumexp(X, coeffs), npval) 90 | 91 | X = np.array(list(map(mp.mpf, X))) 92 | assert abs(float(mp_fast_logsumexp(X, coeffs)[0])-npval[0])<1e-16 93 | -------------------------------------------------------------------------------- /docs/_build/html/coniii_rst/coniii.ising.test_automaton.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | coniii.ising.test_automaton module — ConIII 1.1.9 documentation 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 |
31 |
32 | 33 | 34 |
35 | 36 |
37 |

coniii.ising.test_automaton module

38 |
39 | 40 | 41 |
42 | 43 |
44 |
45 | 102 |
103 |
104 | 115 | 116 | 117 | 118 | 119 | 120 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![PyPI version fury.io](https://badge.fury.io/py/coniii.svg)](https://pypi.python.org/pypi/coniii/) [![PyPI license](https://img.shields.io/pypi/l/coniii.svg)](https://pypi.python.org/pypi/coniii/) 2 | 3 | # Convenient Interface to Inverse Ising 4 | 5 | ConIII is a Python package for solving maximum entropy problems with a focus on the 6 | pairwise maximum entropy model, also known as the inverse Ising problem. 7 | 8 | If you use ConIII for your research, please consider citing the following: 9 | > Lee, E.D. and Daniels, B.C., 2019. Convenient Interface to Inverse Ising (ConIII): A 10 | > Python 3 Package for Solving Ising-Type Maximum Entropy Models. Journal of Open Research 11 | > Software, 7(1), p.3. DOI: http://doi.org/10.5334/jors.217. 12 | 13 | The paper also contains an overview of the modules. For code documentation, see 14 | [here](https://eddielee.co/coniii/index.html "Documentation"). 15 | 16 | ## Installation 17 | 18 | To set up an Anaconda environment called "test" and install from pip, run the following code. The openblas package is only recommended for AMD users. 19 | ```bash 20 | $ conda create -n test -c conda-forge python=3.10 numpy scipy numba cython jupyter ipython multiprocess boost==1.74 matplotlib mpmath blas=*=openblas 21 | $ pip install coniii 22 | ``` 23 | If you have trouble using `pip`, then you can always build this package from 24 | source. The following code will down download the latest release from GitHub and install 25 | the package. Make sure that you are running Python 3.10 and have boost v1.74.0 26 | installed. 27 | ```bash 28 | $ git clone https://github.com/eltrompetero/coniii.git 29 | $ cd coniii 30 | $ ./pypi_compile.sh 31 | $ pip install dist/*.whl 32 | ``` 33 | 34 | #### Setting up exact solution for systems *N > 9* 35 | If you would like to use the `Enumerate` solver for system sizes greater than 9 spins, you 36 | must run enumerate.py to write those files yourself. This can be run from the install 37 | directory. If you do not know where the installation directory is, you can find it by 38 | starting a Python terminal and running 39 | ```python 40 | >>> import coniii 41 | >>> coniii.__path__ 42 | ``` 43 | 44 | Once inside the install directory, you can run in your bash shell 45 | ```bash 46 | $ python enumerate.py [N] 1 47 | ``` 48 | 49 | where `[N]` should be replaced by the size of the system. This specifies that the system should be written for the {-1,1} basis. Note that the package uses the {-1,1} basis by default. For more details, see the `__main__` block at the end of the file enumerate.py. 50 | 51 | For the {0,1} basis, use 52 | ```bash 53 | $ python enumerate.py [N] 54 | ``` 55 | 56 | ## Quick guide with Jupyter notebook 57 | 58 | A [Jupyter 59 | notebook](https://github.com/eltrompetero/coniii/blob/py3/ipynb/usage_guide.ipynb) with a 60 | brief introduction and examples for how to use ConIII is available. The 61 | notebook is also installed into your package directory if you used pip. 62 | 63 | To use the notebook, install jupyter such as by following the setup instructions above. Then, copy the notebook file "usage_guide.ipynb" into a directory outside the "coniii" directory. Change to this directory and run 64 | ```bash 65 | $ jupyter notebook 66 | ``` 67 | 68 | This should open the notebook in your default web browser. 69 | 70 | ## Troubleshooting 71 | 72 | This package is only maintained for Python 3 and has only been tested for Python 73 | 3.10. Check which version of Python you are running in your terminal with 74 | ```bash 75 | $ python --version 76 | ``` 77 | 78 | ConIII has been tested on the following systems 79 | * Ubuntu 20.04.5 80 | 81 | Trouble compiling the Boost extension manually? Check if your Boost library is 82 | included in your path. If it is not, then you can add an include directory entry 83 | into the `EXTRA_COMPILE_ARGS` variable in "setup.py" before compiling. 84 | 85 | 86 | ### Support 87 | 88 | Please file an issue on the GitHub if you have any problems or feature requests. Provide a 89 | stack trace or other information that would be helpful in debugging. For example, OS, 90 | system configuration details, and the results of unit tests. Unit tests can be run by 91 | navigating to the package directory and running 92 | 93 | ```bash 94 | $ pytest -q 95 | ``` 96 | 97 | The package directory can be found by running inside python 98 | ```python 99 | >>> import coniii 100 | >>> coniii.__path__ 101 | ``` 102 | 103 | You may also need to install pytest. 104 | ```bash 105 | $ conda install -c conda-forge pytest 106 | ``` 107 | 108 | ### Updating 109 | 110 | When updating, please read the [RELEASE_NOTES](https://github.com/eltrompetero/coniii/blob/py3/RELEASE_NOTES). There may 111 | be modifications to the interface including parameter names as we make future versions 112 | more user friendly. 113 | 114 | [Documentation](https://eddielee.co/coniii/index.html "Documentation"). 115 | -------------------------------------------------------------------------------- /coniii/ising/automaton.py: -------------------------------------------------------------------------------- 1 | # =============================================================================================== # 2 | # Module for simulations of Ising model on a lattice. Distributed as part of ConIII package. 3 | # Author: Eddie Lee, edl56@cornell.edu 4 | # =============================================================================================== # 5 | import numpy as np 6 | import multiprocess as mp 7 | from numba import njit,jit 8 | 9 | 10 | class Ising2D(): 11 | """Simulation of the ferromagnetic Ising model on a 2D periodic lattice with quenched disorder 12 | in the local fields. 13 | """ 14 | 15 | def __init__(self, dim, J, h=0, rng=None): 16 | """ 17 | Parameters 18 | ---------- 19 | dim : tuple 20 | Pair describing the length of the system along the x and y dimensions. 21 | J : float 22 | h : ndarray or float,0 23 | Field at every lattice point. 24 | rng : np.random.RandomState,None 25 | """ 26 | 27 | assert len(dim)==2, "Must specify only x and y dimensions." 28 | self.dim = dim 29 | self.lattice = ((np.random.rand(*dim)<.5)*2.-1).astype(np.int8) 30 | self.J = J 31 | if type(h) is float or type(h) is type(int): 32 | self.h = np.zeros(dim)+h 33 | else: 34 | self.h = h or np.zeros(dim) 35 | self.rng = rng or np.random.RandomState() 36 | 37 | def iterate(self, n_iters, systematic=True): 38 | """ 39 | Parameters 40 | ---------- 41 | n_iters : int 42 | systematic : bool,True 43 | If True, iterate through each spin on the lattice in sequence. 44 | """ 45 | 46 | flip_metropolis=self.flip_metropolis 47 | 48 | if not systematic: 49 | @njit 50 | def single_iteration(lattice, dim=self.dim, h=self.h, J=self.J): 51 | for i in range(n_iters): 52 | i, j=np.random.randint(dim[0]), np.random.randint(dim[1]) 53 | flip_metropolis(i, j, h[i,j], J, lattice) 54 | else: 55 | # Fast iteration using jit. 56 | @njit 57 | def single_iteration(lattice, dim=self.dim, h=self.h, J=self.J, size=self.dim[0]*self.dim[1]): 58 | # Randomly order the lattice points for flipping or else lattice will be very important. 59 | ix = np.random.permutation( np.arange(dim[0]*dim[1]) )[:size] 60 | for ix_ in ix: 61 | i, j = ix_//dim[1], ix_%dim[1] 62 | flip_metropolis(i, j, h[i,j], J, lattice) 63 | 64 | lattice=self.lattice 65 | counter = 0 66 | while counter < n_iters: 67 | single_iteration(lattice) 68 | counter += self.dim[0]*self.dim[1] 69 | self.lattice = lattice 70 | 71 | @staticmethod 72 | @njit 73 | def flip_metropolis(i, j, h, J, lattice): 74 | """Flip a single lattice spin using Metropolis sampling. 75 | 76 | Parameters 77 | ---------- 78 | i : int 79 | j : int 80 | """ 81 | 82 | dE=0 83 | dim=lattice.shape 84 | 85 | # If same value as neighbor, will incur energy cost for flipping but if anti-aligned energy will 86 | # decrease 87 | if lattice[(i-1)%dim[0],j]==lattice[i,j]: 88 | dE+=2*J 89 | else: 90 | dE-=2*J 91 | if lattice[(i+1)%dim[0],j]==lattice[i,j]: 92 | dE+=2*J 93 | else: 94 | dE-=2*J 95 | if lattice[i,(j+1)%dim[1]]==lattice[i,j]: 96 | dE+=2*J 97 | else: 98 | dE-=2*J 99 | if lattice[i,(j-1)%dim[1]]==lattice[i,j]: 100 | dE+=2*J 101 | else: 102 | dE-=2*J 103 | 104 | # Local field. 105 | dE-=2*lattice[i,j]*h 106 | 107 | if dE<=0: 108 | lattice[i,j]*=-1 109 | elif np.random.rand() 3 | 4 | 5 | 6 | 7 | 8 | coniii.ising package — ConIII 1.1.9 documentation 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 |
31 |
32 | 33 | 34 |
35 | 36 |
37 |

coniii.ising package

38 |
39 |

Submodules

40 | 46 |
47 |
48 |

Module contents

49 |
50 |
51 | 52 | 53 |
54 | 55 |
56 |
57 | 112 |
113 |
114 | 125 | 126 | 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /docs/_build/html/_static/pygments.css: -------------------------------------------------------------------------------- 1 | pre { line-height: 125%; } 2 | td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } 3 | span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } 4 | td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } 5 | span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } 6 | .highlight .hll { background-color: #ffffcc } 7 | .highlight { background: #f8f8f8; } 8 | .highlight .c { color: #8f5902; font-style: italic } /* Comment */ 9 | .highlight .err { color: #a40000; border: 1px solid #ef2929 } /* Error */ 10 | .highlight .g { color: #000000 } /* Generic */ 11 | .highlight .k { color: #004461; font-weight: bold } /* Keyword */ 12 | .highlight .l { color: #000000 } /* Literal */ 13 | .highlight .n { color: #000000 } /* Name */ 14 | .highlight .o { color: #582800 } /* Operator */ 15 | .highlight .x { color: #000000 } /* Other */ 16 | .highlight .p { color: #000000; font-weight: bold } /* Punctuation */ 17 | .highlight .ch { color: #8f5902; font-style: italic } /* Comment.Hashbang */ 18 | .highlight .cm { color: #8f5902; font-style: italic } /* Comment.Multiline */ 19 | .highlight .cp { color: #8f5902 } /* Comment.Preproc */ 20 | .highlight .cpf { color: #8f5902; font-style: italic } /* Comment.PreprocFile */ 21 | .highlight .c1 { color: #8f5902; font-style: italic } /* Comment.Single */ 22 | .highlight .cs { color: #8f5902; font-style: italic } /* Comment.Special */ 23 | .highlight .gd { color: #a40000 } /* Generic.Deleted */ 24 | .highlight .ge { color: #000000; font-style: italic } /* Generic.Emph */ 25 | .highlight .gr { color: #ef2929 } /* Generic.Error */ 26 | .highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */ 27 | .highlight .gi { color: #00A000 } /* Generic.Inserted */ 28 | .highlight .go { color: #888888 } /* Generic.Output */ 29 | .highlight .gp { color: #745334 } /* Generic.Prompt */ 30 | .highlight .gs { color: #000000; font-weight: bold } /* Generic.Strong */ 31 | .highlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ 32 | .highlight .gt { color: #a40000; font-weight: bold } /* Generic.Traceback */ 33 | .highlight .kc { color: #004461; font-weight: bold } /* Keyword.Constant */ 34 | .highlight .kd { color: #004461; font-weight: bold } /* Keyword.Declaration */ 35 | .highlight .kn { color: #004461; font-weight: bold } /* Keyword.Namespace */ 36 | .highlight .kp { color: #004461; font-weight: bold } /* Keyword.Pseudo */ 37 | .highlight .kr { color: #004461; font-weight: bold } /* Keyword.Reserved */ 38 | .highlight .kt { color: #004461; font-weight: bold } /* Keyword.Type */ 39 | .highlight .ld { color: #000000 } /* Literal.Date */ 40 | .highlight .m { color: #990000 } /* Literal.Number */ 41 | .highlight .s { color: #4e9a06 } /* Literal.String */ 42 | .highlight .na { color: #c4a000 } /* Name.Attribute */ 43 | .highlight .nb { color: #004461 } /* Name.Builtin */ 44 | .highlight .nc { color: #000000 } /* Name.Class */ 45 | .highlight .no { color: #000000 } /* Name.Constant */ 46 | .highlight .nd { color: #888888 } /* Name.Decorator */ 47 | .highlight .ni { color: #ce5c00 } /* Name.Entity */ 48 | .highlight .ne { color: #cc0000; font-weight: bold } /* Name.Exception */ 49 | .highlight .nf { color: #000000 } /* Name.Function */ 50 | .highlight .nl { color: #f57900 } /* Name.Label */ 51 | .highlight .nn { color: #000000 } /* Name.Namespace */ 52 | .highlight .nx { color: #000000 } /* Name.Other */ 53 | .highlight .py { color: #000000 } /* Name.Property */ 54 | .highlight .nt { color: #004461; font-weight: bold } /* Name.Tag */ 55 | .highlight .nv { color: #000000 } /* Name.Variable */ 56 | .highlight .ow { color: #004461; font-weight: bold } /* Operator.Word */ 57 | .highlight .w { color: #f8f8f8; text-decoration: underline } /* Text.Whitespace */ 58 | .highlight .mb { color: #990000 } /* Literal.Number.Bin */ 59 | .highlight .mf { color: #990000 } /* Literal.Number.Float */ 60 | .highlight .mh { color: #990000 } /* Literal.Number.Hex */ 61 | .highlight .mi { color: #990000 } /* Literal.Number.Integer */ 62 | .highlight .mo { color: #990000 } /* Literal.Number.Oct */ 63 | .highlight .sa { color: #4e9a06 } /* Literal.String.Affix */ 64 | .highlight .sb { color: #4e9a06 } /* Literal.String.Backtick */ 65 | .highlight .sc { color: #4e9a06 } /* Literal.String.Char */ 66 | .highlight .dl { color: #4e9a06 } /* Literal.String.Delimiter */ 67 | .highlight .sd { color: #8f5902; font-style: italic } /* Literal.String.Doc */ 68 | .highlight .s2 { color: #4e9a06 } /* Literal.String.Double */ 69 | .highlight .se { color: #4e9a06 } /* Literal.String.Escape */ 70 | .highlight .sh { color: #4e9a06 } /* Literal.String.Heredoc */ 71 | .highlight .si { color: #4e9a06 } /* Literal.String.Interpol */ 72 | .highlight .sx { color: #4e9a06 } /* Literal.String.Other */ 73 | .highlight .sr { color: #4e9a06 } /* Literal.String.Regex */ 74 | .highlight .s1 { color: #4e9a06 } /* Literal.String.Single */ 75 | .highlight .ss { color: #4e9a06 } /* Literal.String.Symbol */ 76 | .highlight .bp { color: #3465a4 } /* Name.Builtin.Pseudo */ 77 | .highlight .fm { color: #000000 } /* Name.Function.Magic */ 78 | .highlight .vc { color: #000000 } /* Name.Variable.Class */ 79 | .highlight .vg { color: #000000 } /* Name.Variable.Global */ 80 | .highlight .vi { color: #000000 } /* Name.Variable.Instance */ 81 | .highlight .vm { color: #000000 } /* Name.Variable.Magic */ 82 | .highlight .il { color: #990000 } /* Literal.Number.Integer.Long */ -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # MIT License 2 | # 3 | # Copyright (c) 2020 Edward D. Lee, Bryan C. Daniels 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | 23 | # Always prefer setuptools over distutils 24 | from setuptools import setup, find_packages 25 | # To use a consistent encoding 26 | from codecs import open 27 | import os 28 | from os import path, environ 29 | from distutils.extension import Extension 30 | from coniii.version import version as __version__ 31 | from shutil import copyfile 32 | import platform, sys 33 | 34 | 35 | # flags 36 | NO_BOOST = False 37 | 38 | # default args (that are modified per system specs below) 39 | EXTRA_COMPILE_ARGS = ['-std=c++11',f'-I{os.environ["CONDA_PREFIX"]}/include'] 40 | if 'CONDA_PREFIX' in os.environ: 41 | # includes places to search for boost lib 42 | DEFAULT_LIBRARY_DR = [f'{os.environ["CONDA_PREFIX"]}/include'] 43 | 44 | # setup 45 | here = path.abspath(path.dirname(__file__)) 46 | system = platform.system() 47 | py_version = str(sys.version_info.major) + str(sys.version_info.minor) 48 | dylibNames = [f'boost_python{py_version}', f'boost_numpy{py_version}'] 49 | 50 | # copy license into package 51 | copyfile('LICENSE.txt','coniii/LICENSE.txt') 52 | 53 | # Get the long description from the README file 54 | with open(path.join(here, 'README.md'), encoding='utf-8') as f: 55 | long_description = f.read() 56 | 57 | # setup C++ extension 58 | # make sure libraries exist if C++ extension is to be compiled 59 | if not NO_BOOST: 60 | samplersModule = Extension('coniii.samplers_ext', 61 | include_dirs = ['./cpp'], 62 | library_dirs=DEFAULT_LIBRARY_DR, 63 | sources=['./cpp/samplers.cpp', './cpp/py.cpp'], 64 | extra_objects=['-l%s'%f for f in dylibNames], 65 | extra_compile_args=EXTRA_COMPILE_ARGS, 66 | language='c++') 67 | ext_modules = [samplersModule] 68 | else: 69 | print("*******************************************") 70 | print("Boost not compiled because flag is not set.") 71 | print("*******************************************") 72 | 73 | 74 | # compile 75 | kwargs = {'name':'coniii', 76 | 'version':__version__, 77 | 'description':'Convenient Interface to Inverse Ising (ConIII)', 78 | 'long_description':long_description, 79 | 'long_description_content_type':'text/markdown', 80 | 'url':'https://github.com/eltrompetero/coniii', 81 | 'author':'Edward D. Lee, Bryan C Daniels', 82 | 'author_email':'edlee@santafe.edu', 83 | 'license':'MIT', 84 | 'classifiers':['Development Status :: 5 - Production/Stable', 85 | 'Intended Audience :: Science/Research', 86 | 'Topic :: Scientific/Engineering :: Information Analysis', 87 | 'License :: OSI Approved :: MIT License', 88 | 'Programming Language :: Python :: 3 :: Only', 89 | ], 90 | 'python_requires':'>=3.8.3', 91 | 'keywords':'inverse Ising maxent maximum entropy inference', 92 | 'packages':find_packages(), 93 | 'install_requires':['multiprocess>=0.70.7,<1', 94 | 'scipy', 95 | 'matplotlib', 96 | 'numpy>=1.16.2,<2', 97 | 'numba>=0.45.1,<1', 98 | 'mpmath>=1.1.0', 99 | 'dill'], 100 | 'include_package_data':True, # see MANIFEST.in 101 | 'py_modules':['coniii.enumerate', 102 | 'coniii.enumerate_potts', 103 | 'coniii.mean_field_ising', 104 | 'coniii.pseudo_inverse_ising', 105 | 'coniii.samplers', 106 | 'coniii.solvers', 107 | 'coniii.utils'], 108 | 'ext_modules':ext_modules} 109 | 110 | try: 111 | setup(**kwargs) 112 | except: 113 | print("*****************************************************") 114 | print("Boost not compiled. See above errors for g++ message.") 115 | print("*****************************************************") 116 | 117 | kwargs['ext_modules'] = [] 118 | setup(**kwargs) 119 | -------------------------------------------------------------------------------- /coniii/ising/utils.py: -------------------------------------------------------------------------------- 1 | # ===================================================================================== # 2 | # Module with useful functions for Ising models. 3 | # Distributed as part of ConIII. 4 | # Author : Edward Lee, edlee@alumni.princeton.edu 5 | # ===================================================================================== # 6 | # 7 | # MIT License 8 | # 9 | # Copyright (c) 2019 Edward D. Lee, Bryan C. Daniels 10 | # 11 | # Permission is hereby granted, free of charge, to any person obtaining a copy 12 | # of this software and associated documentation files (the "Software"), to deal 13 | # in the Software without restriction, including without limitation the rights 14 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 15 | # copies of the Software, and to permit persons to whom the Software is 16 | # furnished to do so, subject to the following conditions: 17 | # 18 | # The above copyright notice and this permission notice shall be included in all 19 | # copies or substantial portions of the Software. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 26 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 27 | # SOFTWARE. 28 | import numpy as np 29 | from scipy.spatial.distance import squareform 30 | import itertools 31 | import importlib 32 | from ..utils import * 33 | 34 | 35 | class Ising(): 36 | """A nice front end for the pairwise maxent (Ising) model in the {-1,1} basis. 37 | """ 38 | def __init__(self, n, h=None, J=None): 39 | """ 40 | Parameters 41 | ---------- 42 | n : int 43 | System size. 44 | h : list-like, None 45 | Fields. 46 | J : list-like, None 47 | Couplings. 48 | """ 49 | 50 | # check args 51 | assert n>1 52 | if h is None: 53 | h = np.zeros(n) 54 | elif not hasattr(h, '__len__'): 55 | h = np.zeros(n)+h 56 | else: 57 | assert len(h)==n, "Number of fields should be equal to n." 58 | if J is None: 59 | J = np.zeros(n*(n-1)//2) 60 | elif not hasattr(J, '__len__'): 61 | J = np.zeros(n*(n-1)//2)+J 62 | else: 63 | assert len(J)==(n*(n-1)//2), "Number of couplings should be equal to n choose 2." 64 | assert h.ndim==1 and J.ndim==1, "Both h and J must be provided as vectors." 65 | 66 | self.n = n 67 | self.hJ = np.concatenate((h,J)) 68 | self.Jmat = squareform(J) 69 | self.hJ01 = convert_params(h, J, '01', concat=True) 70 | self.ising_eqns = importlib.import_module('coniii.ising_eqn.ising_eqn_%d_sym'%n) 71 | self.calc_e,_,_ = define_ising_helper_functions() 72 | 73 | def correlations(self, basis='1'): 74 | """ 75 | Parmeters 76 | --------- 77 | basis : str, '1' 78 | 79 | Returns 80 | ------- 81 | ndarray 82 | Means and pairwise correlations. 83 | """ 84 | 85 | sisj = self.ising_eqns.calc_observables(self.hJ) 86 | if basis=='1': 87 | return sisj 88 | return convert_corr(sisj[:self.n], sisj[self.n:], convert_to='01', concat=True) 89 | 90 | def fields(self, basis='1'): 91 | """ 92 | Parameters 93 | ---------- 94 | basis : str, '1' 95 | '0' or '1' 96 | 97 | Returns 98 | ------- 99 | ndarray 100 | """ 101 | 102 | if basis=='1': 103 | return self.hJ[:self.n] 104 | return self.hJ01[:self.n] 105 | 106 | def couplings(self, basis='1'): 107 | """ 108 | Parameters 109 | ---------- 110 | basis : str, '1' 111 | '0' or '1' 112 | 113 | Returns 114 | ------- 115 | ndarray 116 | """ 117 | 118 | if basis=='1': 119 | return self.hJ[self.n:] 120 | return self.hJ01[self.n:] 121 | 122 | def find_basin(self, s): 123 | """Return energy basins for given state using single spin flips. 124 | 125 | Parameters 126 | ---------- 127 | s : ndarray 128 | 129 | Returns 130 | ------- 131 | ndarray 132 | """ 133 | 134 | assert s.size==self.n 135 | atMin = False 136 | thisState = s.astype(np.int8) 137 | 138 | while not atMin: 139 | dE = self.neighbor_dE(thisState) 140 | if np.any( dE<0 ): 141 | ix = dE.argmin() 142 | thisState[ix] *= -1 143 | else: 144 | atMin = True 145 | return thisState 146 | 147 | def neighbor_dE(self, state): 148 | """dE to get to single flip neighbors.""" 149 | 150 | dE = np.zeros(self.n) 151 | for i in range(self.n): 152 | dE[i] = 2*state[i]*self.hJ[i] +2*state[i]*(state*self.Jmat[i]).sum() 153 | return dE 154 | 155 | @staticmethod 156 | def resort_couplings(J,sortIx): 157 | """Reorder given couplings into a desired order. 158 | 159 | Params: 160 | ------- 161 | J (ndarray) 162 | vector of length n*(n-1)/2 163 | sortIx (ndarray) 164 | """ 165 | return 166 | #end Ising 167 | -------------------------------------------------------------------------------- /docs/_build/html/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Welcome to ConIII’s documentation! — ConIII 1.1.9 documentation 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 |
30 |
31 | 32 | 33 |
34 | 35 |
36 |

Welcome to ConIII’s documentation!

37 | 52 |
53 |
54 |

Indices and tables

55 | 60 |
61 | 62 | 63 |
64 | 65 |
66 |
67 | 117 |
118 |
119 | 130 | 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /pypi_description: -------------------------------------------------------------------------------- 1 | [![PyPI version fury.io](https://badge.fury.io/py/coniii.svg)](https://pypi.python.org/pypi/coniii/) [![PyPI license](https://img.shields.io/pypi/l/coniii.svg)](https://pypi.python.org/pypi/coniii/) 2 | 3 | # Convenient Interface to Inverse Ising 4 | 5 | ConIII is a Python package for solving maximum entropy problems with a focus on the 6 | pairwise maximum entropy model, also known as the inverse Ising problem. Support for 7 | Python 3.8.3 and higher. 8 | 9 | If you use ConIII for your research, please consider citing the following: 10 | > Lee, E.D. and Daniels, B.C., 2019. Convenient Interface to Inverse Ising (ConIII): A 11 | > Python 3 Package for Solving Ising-Type Maximum Entropy Models. Journal of Open Research 12 | > Software, 7(1), p.3. DOI: http://doi.org/10.5334/jors.217. 13 | 14 | The paper also contains an overview of the modules. For code documentation, see 15 | [here](https://eddielee.co/coniii/index.html "Documentation"). 16 | 17 | ## Installation 18 | 19 | This package is available on PyPI. It can be installed by first installing the needed 20 | Boost C++ libraries and using pip. 21 | ```bash 22 | $ conda install -c conda-forge boost==1.74 23 | $ pip install coniii 24 | ``` 25 | If you have trouble using `pip`, then you can always build this package from 26 | source. The following code will down download the latest release from GitHub and install 27 | the package. Make sure that you are running Python 3.8.3 or higher and have boost v1.74.0 28 | installed. 29 | ```bash 30 | $ git clone https://github.com/eltrompetero/coniii.git 31 | $ cd coniii 32 | $ ./pypi_compile.sh 33 | $ pip install dist/*.whl 34 | ``` 35 | (Note: Using setuptools in the usual way of `python 36 | setup.py install` will not work because eggs are incompatible with cached jit functions 37 | generated using numba.) 38 | 39 | #### Setting up exact solution for systems *N > 9* 40 | If you would like to use the `Enumerate` solver for system sizes greater than 9 spins, you 41 | must run enumerate.py to write those files yourself. This can be run from the install 42 | directory. If you do not know where the installation directory is, you can find it by 43 | starting a Python terminal and running 44 | ```python 45 | >>> import coniii 46 | >>> coniii.__path__ 47 | ``` 48 | 49 | Once inside the install directory, you can run in your bash shell 50 | ```bash 51 | $ python enumerate.py [N] 52 | ``` 53 | 54 | where `[N]` should be replaced by the size of the system. This will write the equations 55 | for the Ising model in the {0,1} basis. On the other hand, 56 | 57 | ```bash 58 | $ python enumerate.py [N] 1 59 | ``` 60 | 61 | specifies that the system should be written for the {-1,1} basis. Note that the package 62 | uses the {-1,1} basis by default. For more details, see the `__main__` block at the end of 63 | the file enumerate.py. 64 | 65 | ## Quick guide (with Jupyter notebook) 66 | 67 | A [Jupyter 68 | notebook](https://github.com/eltrompetero/coniii/blob/py3/ipynb/usage_guide.ipynb) with a 69 | brief introduction and examples for how to use ConIII is available. An HTML version is 70 | [here](https://github.com/eltrompetero/coniii/blob/py3/ipynb/usage_guide.html). The 71 | notebook is installed into your package directory if you used pip. 72 | 73 | To use the notebook, install jupyter. 74 | ```bash 75 | $ pip install jupyter 76 | ``` 77 | or if you are using the Conda package manager 78 | ```bash 79 | $ conda install jupyter 80 | ``` 81 | 82 | Then, first copy the notebook file "usage_guide.ipynb" into a directory outside the 83 | "coniii" directory. Change to this directory and run 84 | ```bash 85 | $ jupyter notebook 86 | ``` 87 | 88 | This should open the notebook in your default web browser. 89 | 90 | ## Quick guide (console) 91 | 92 | First, install iPython for a console-based interpreter and start it. 93 | ```bash 94 | $ pip install ipython 95 | ``` 96 | or if you are using the Conda package manager 97 | ```bash 98 | $ conda install ipython 99 | ``` 100 | 101 | Then, first copy the notebook file 102 | ["usage_guide.py"](https://github.com/eltrompetero/coniii/blob/py3/ipynb/usage_guide.py) 103 | into a directory outside the "coniii" directory. Change to this directory and run 104 | ```bash 105 | $ ipython 106 | ``` 107 | 108 | Once inside the iPython interpreter, run 109 | ```python 110 | >>> %run usage_guide.py 111 | ``` 112 | This will run all the examples sequentially, so you may want to comment out unwanted lines. 113 | 114 | ## Troubleshooting 115 | 116 | This package is only maintained for Python 3 and has only been tested for Python 117 | 3.8.3. Check which version of Python you are running in your terminal with 118 | ```bash 119 | $ python --version 120 | ``` 121 | 122 | ConIII has been tested on the following systems 123 | * Ubuntu 18.04 124 | * Ubuntu 20.04.1 125 | 126 | Trouble compiling the Boost extension manually? Check if your Boost library is 127 | included in your path. If it is not, then you can add an include directory entry 128 | into the `EXTRA_COMPILE_ARGS` variable in "setup.py" before compiling. 129 | 130 | 131 | ### Support 132 | 133 | Please file an issue on the GitHub if you have any problems or feature requests. Provide a 134 | stack trace or other information that would be helpful in debugging. For example, OS, 135 | system configuration details, and the results of unit tests. Unit tests can be run by 136 | navigating to the package directory and running 137 | 138 | ```bash 139 | $ pytest -q 140 | ``` 141 | 142 | The package directory can be found by running inside python 143 | ```python 144 | >>> import coniii 145 | >>> coniii.__path__ 146 | ``` 147 | 148 | You may also need to install pytest. 149 | ```bash 150 | $ pip install pytest 151 | ``` 152 | 153 | ### Updating 154 | 155 | When updating, please read the [RELEASE_NOTES](https://github.com/eltrompetero/coniii/blob/py3/RELEASE_NOTES). There may 156 | be modifications to the interface including parameter names as we make future versions 157 | more user friendly. 158 | 159 | [Documentation](https://eddielee.co/coniii/index.html "Documentation"). 160 | -------------------------------------------------------------------------------- /docs/_build/html/coniii_rst/coniii.test_samplers.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | coniii.test_samplers module — ConIII 1.1.9 documentation 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 |
29 |
30 | 31 | 32 |
33 | 34 |
35 |

coniii.test_samplers module

36 |
37 |
38 | coniii.test_samplers.compare_samplers()
39 |
40 | 41 |
42 |
43 | coniii.test_samplers.test_Metropolis(run_timing=False)
44 |
45 | 46 |
47 |
48 | coniii.test_samplers.test_ParallelTempering()
49 |
50 | 51 |
52 |
53 | coniii.test_samplers.test_Potts3()
54 |
55 | 56 |
57 |
58 | coniii.test_samplers.test_sample_ising()
59 |
60 | 61 |
62 | 63 | 64 |
65 | 66 |
67 |
68 | 117 |
118 |
119 | 130 | 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Configuration file for the Sphinx documentation builder. 4 | # 5 | # This file does only contain a selection of the most common options. For a 6 | # full list see the documentation: 7 | # http://www.sphinx-doc.org/en/master/config 8 | 9 | # -- Path setup -------------------------------------------------------------- 10 | 11 | # If extensions (or modules to document with autodoc) are in another directory, 12 | # add these directories to sys.path here. If the directory is relative to the 13 | # documentation root, use os.path.abspath to make it absolute, like shown here. 14 | # 15 | import os 16 | import sys 17 | sys.path.insert(0, os.path.abspath('..')) 18 | 19 | 20 | # -- Project information ----------------------------------------------------- 21 | 22 | project = 'ConIII' 23 | copyright = '2018, Edward D. Lee, Bryan C. Daniels' 24 | author = 'Edward D. Lee, Bryan C. Daniels' 25 | 26 | # The short X.Y version 27 | version = '1.1' 28 | # The full version, including alpha/beta/rc tags 29 | release = '1.1.9' 30 | 31 | 32 | # -- General configuration --------------------------------------------------- 33 | 34 | # If your documentation needs a minimal Sphinx version, state it here. 35 | # 36 | # needs_sphinx = '1.8.1' 37 | 38 | # Add any Sphinx extension module names here, as strings. They can be 39 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom 40 | # ones. 41 | extensions = [ 42 | 'sphinx.ext.autodoc', 43 | 'sphinx.ext.doctest', 44 | 'sphinx.ext.coverage', 45 | ] 46 | 47 | # Add any paths that contain templates here, relative to this directory. 48 | templates_path = ['_templates'] 49 | 50 | # The suffix(es) of source filenames. 51 | # You can specify multiple suffix as a list of string: 52 | # 53 | # source_suffix = ['.rst', '.md'] 54 | source_suffix = '.rst' 55 | 56 | # The master toctree document. 57 | master_doc = 'index' 58 | 59 | # The language for content autogenerated by Sphinx. Refer to documentation 60 | # for a list of supported languages. 61 | # 62 | # This is also used if you do content translation via gettext catalogs. 63 | # Usually you set "language" from the command line for these cases. 64 | language = None 65 | 66 | # List of patterns, relative to source directory, that match files and 67 | # directories to ignore when looking for source files. 68 | # This pattern also affects html_static_path and html_extra_path. 69 | exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] 70 | 71 | # The name of the Pygments (syntax highlighting) style to use. 72 | pygments_style = None 73 | 74 | 75 | # -- Options for HTML output ------------------------------------------------- 76 | 77 | # The theme to use for HTML and HTML Help pages. See the documentation for 78 | # a list of builtin themes. 79 | # 80 | html_theme = 'alabaster' 81 | 82 | # Theme options are theme-specific and customize the look and feel of a theme 83 | # further. For a list of options available for each theme, see the 84 | # documentation. 85 | # 86 | # html_theme_options = {} 87 | 88 | # Add any paths that contain custom static files (such as style sheets) here, 89 | # relative to this directory. They are copied after the builtin static files, 90 | # so a file named "default.css" will overwrite the builtin "default.css". 91 | html_static_path = ['_static'] 92 | 93 | # Custom sidebar templates, must be a dictionary that maps document names 94 | # to template names. 95 | # 96 | # The default sidebars (for documents that don't match any pattern) are 97 | # defined by theme itself. Builtin themes are using these templates by 98 | # default: ``['localtoc.html', 'relations.html', 'sourcelink.html', 99 | # 'searchbox.html']``. 100 | # 101 | # html_sidebars = {} 102 | 103 | 104 | # -- Options for HTMLHelp output --------------------------------------------- 105 | 106 | # Output file base name for HTML help builder. 107 | htmlhelp_basename = 'ConIIIdoc' 108 | 109 | 110 | # -- Options for LaTeX output ------------------------------------------------ 111 | 112 | latex_elements = { 113 | # The paper size ('letterpaper' or 'a4paper'). 114 | # 115 | # 'papersize': 'letterpaper', 116 | 117 | # The font size ('10pt', '11pt' or '12pt'). 118 | # 119 | # 'pointsize': '10pt', 120 | 121 | # Additional stuff for the LaTeX preamble. 122 | # 123 | # 'preamble': '', 124 | 125 | # Latex figure (float) alignment 126 | # 127 | # 'figure_align': 'htbp', 128 | } 129 | 130 | # Grouping the document tree into LaTeX files. List of tuples 131 | # (source start file, target name, title, 132 | # author, documentclass [howto, manual, or own class]). 133 | latex_documents = [ 134 | (master_doc, 'ConIII.tex', 'ConIII Documentation', 135 | 'Edward D. Lee, Bryan C. Daniels', 'manual'), 136 | ] 137 | 138 | 139 | # -- Options for manual page output ------------------------------------------ 140 | 141 | # One entry per manual page. List of tuples 142 | # (source start file, name, description, authors, manual section). 143 | man_pages = [ 144 | (master_doc, 'coniii', 'ConIII Documentation', 145 | [author], 1) 146 | ] 147 | 148 | 149 | # -- Options for Texinfo output ---------------------------------------------- 150 | 151 | # Grouping the document tree into Texinfo files. List of tuples 152 | # (source start file, target name, title, author, 153 | # dir menu entry, description, category) 154 | texinfo_documents = [ 155 | (master_doc, 'ConIII', 'ConIII Documentation', 156 | author, 'ConIII', 'One line description of project.', 157 | 'Miscellaneous'), 158 | ] 159 | 160 | 161 | # -- Options for Epub output ------------------------------------------------- 162 | 163 | # Bibliographic Dublin Core info. 164 | epub_title = project 165 | 166 | # The unique identifier of the text. This can be a ISBN number 167 | # or the project homepage. 168 | # 169 | # epub_identifier = '' 170 | 171 | # A unique identification for the text. 172 | # 173 | # epub_uid = '' 174 | 175 | # A list of files that should not be packed into the epub file. 176 | epub_exclude_files = ['search.html'] 177 | 178 | 179 | # -- Extension configuration ------------------------------------------------- 180 | -------------------------------------------------------------------------------- /docs/_build/html/coniii_rst/coniii.test_solvers.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | coniii.test_solvers module — ConIII 1.1.9 documentation 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 |
29 |
30 | 31 | 32 |
33 | 34 |
35 |

coniii.test_solvers module

36 |
37 |
38 | coniii.test_solvers.test_Enumerate()
39 |
40 | 41 |
42 |
43 | coniii.test_solvers.test_MPF()
44 |

Check MPF.

45 |
46 | 47 |
48 |
49 | coniii.test_solvers.test_Pseudo()
50 |
51 | 52 |
53 |
54 | coniii.test_solvers.test_SparseEnumerate()
55 |
56 | 57 |
58 |
59 | coniii.test_solvers.test_init()
60 |

Check that all derived Solver classes can be initialized.

61 |
62 | 63 |
64 |
65 | coniii.test_solvers.test_pickling()
66 |
67 | 68 |
69 | 70 | 71 |
72 | 73 |
74 |
75 | 124 |
125 |
126 | 137 | 138 | 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /docs/_build/html/coniii_rst/modules.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | coniii — ConIII 1.1.9 documentation 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 78 | 127 |
128 |
129 | 140 | 141 | 142 | 143 | 144 | 145 | -------------------------------------------------------------------------------- /coniii/test_solvers.py: -------------------------------------------------------------------------------- 1 | # ====================================================================================== # 2 | # Testing for solvers.py module. See usage_guide.ipynb for comprehensive examples of 3 | # testing algorithms on test data. 4 | # Released with ConIII package. 5 | # Author : Eddie Lee, edlee@alumni.princeton.edu 6 | # 7 | # MIT License 8 | # 9 | # Copyright (c) 2020 Edward D. Lee, Bryan C. Daniels 10 | # 11 | # Permission is hereby granted, free of charge, to any person obtaining a copy 12 | # of this software and associated documentation files (the "Software"), to deal 13 | # in the Software without restriction, including without limitation the rights 14 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 15 | # copies of the Software, and to permit persons to whom the Software is 16 | # furnished to do so, subject to the following conditions: 17 | # 18 | # The above copyright notice and this permission notice shall be included in all 19 | # copies or substantial portions of the Software. 20 | # 21 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 26 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 27 | # SOFTWARE. 28 | # ====================================================================================== # 29 | from .solvers import * 30 | from .ising_eqn import ising_eqn_3_sym as ising 31 | import numpy as np 32 | calc_observables_multipliers = ising.calc_observables 33 | 34 | 35 | # Generate example data set to use in tests 36 | n = 3 # system size 37 | np.random.seed(0) # standardize random seed 38 | h = np.random.normal(scale=.1, size=n) # random couplings 39 | J = np.random.normal(scale=.1, size=n*(n-1)//2) # random fields 40 | hJ = np.concatenate((h, J)) 41 | p = ising.p(hJ) # probability distribution of all states p(s) 42 | sisjTrue = calc_observables_multipliers(hJ) # exact means and pairwise correlations 43 | 44 | allstates = bin_states(n, True) # all 2^n possible binary states in {-1,1} basis 45 | sample = allstates[np.random.choice(range(2**n), 46 | size=10000, 47 | replace=True, 48 | p=p)] # random sample from p(s) 49 | sisj = pair_corr(sample, concat=True) # means and pairwise correlations 50 | 51 | # Define common functions. 52 | calc_e, calc_observables, mchApproximation = define_ising_helper_functions() 53 | get_multipliers_r,calc_observables_r = define_pseudo_ising_helper_functions(n) 54 | 55 | 56 | def test_init(): 57 | """Check that all derived Solver classes can be initialized.""" 58 | from .utils import pair_corr, define_ising_helper_functions, define_pseudo_ising_helper_functions 59 | 60 | # Define function specifically needed for creating Enumerate class. 61 | def calc_observables_multipliers(J): 62 | """ 63 | Calculate observables from probability distribution given Langrangian multipliers. For 64 | the Ising model, these are the means of each spin and the pairwise correlations. 65 | """ 66 | E = calc_e(allstates, J) 67 | return pair_corr( allstates, np.exp(-E-logsumexp(-E)), concat=True ) 68 | 69 | # try initializing the solvers 70 | solver = Enumerate(sample) 71 | solver = SparseEnumerate(sample, parameter_ix=np.array([0,1,2])) 72 | solver = MPF(sample) 73 | solver = Pseudo(sample) 74 | solver = ClusterExpansion(sample) 75 | solver = MCH(sample, sample_size=1000) 76 | solver = RegularizedMeanField(sample) 77 | 78 | solver = Enumerate(n) 79 | solver = SparseEnumerate(n, parameter_ix=np.array([0,1,2])) 80 | solver = MPF(n) 81 | solver = Pseudo(n) 82 | solver = ClusterExpansion(n) 83 | solver = MCH(n, sample_size=1000) 84 | solver = RegularizedMeanField(n) 85 | 86 | def test_Enumerate(): 87 | from .utils import pair_corr 88 | 89 | # Enumerate should be able to find exact solution when passed the exact correlations 90 | solver = Enumerate(sample) 91 | soln = solver.solve(initial_guess=hJ/2, constraints=sisjTrue) 92 | assert np.isclose(hJ, soln).all() 93 | 94 | def test_SparseEnumerate(): 95 | from .utils import pair_corr 96 | 97 | # SparseEnumerate should be able to find exact solution when passed the exact correlations 98 | solver = SparseEnumerate(sample, parameter_ix=np.array([0,1,2,3,4,5])) 99 | soln = solver.solve(initial_guess=hJ/2, constraints=sisjTrue) 100 | assert np.isclose(hJ, soln).all() 101 | 102 | def test_MPF(): 103 | """Check MPF.""" 104 | 105 | solver = MPF(sample) 106 | 107 | # compare log objective function with non-log version 108 | # Convert from {0,1} to {+/-1} basis. 109 | X = sample 110 | X = (X+1)//2 111 | 112 | # Get list of unique data states and how frequently they appear. 113 | Xuniq = X[unique_rows(X)] 114 | ix = unique_rows(X, return_inverse=True) 115 | Xcount = np.bincount(ix) 116 | adjacentStates = solver.list_adjacent_states(Xuniq, True) 117 | 118 | # Interface to objective function. 119 | def f(params): 120 | return solver.logK( Xuniq, Xcount, adjacentStates, params ) 121 | def g(params): 122 | return np.log( solver.K( Xuniq, Xcount, adjacentStates, params ) ) 123 | 124 | # check that they evaluate to the same values once the log transform is accounted for 125 | assert np.isclose(f(hJ), g(hJ)), (f(hJ), g(hJ)) 126 | 127 | # Check that found solutions agree closely 128 | assert np.isclose( solver.solve(solver_kwargs={'disp':False}), 129 | solver.solve(solver_kwargs={'disp':False}, uselog=False), 130 | atol=1e-3 ).all() 131 | 132 | def test_Pseudo(): 133 | solver = Pseudo(sample) 134 | solver.solve(initial_guess=np.zeros(6)) 135 | assert np.isclose(ising.calc_observables(solver.multipliers), sisj, atol=1e-2).all() 136 | 137 | solver.solve(force_general=True, initial_guess=np.zeros(6)) 138 | assert np.isclose(ising.calc_observables(solver.multipliers), sisj, atol=1e-2).all() 139 | 140 | def test_pickling(): 141 | pass 142 | 143 | if __name__=='__main__': 144 | pass 145 | -------------------------------------------------------------------------------- /coniii/ising_eqn/ising_eqn_4_sym.py: -------------------------------------------------------------------------------- 1 | # MIT License 2 | # 3 | # Copyright (c) 2019 Edward D. Lee, Bryan C. Daniels 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | 23 | # Equations for 4-spin Ising model. 24 | 25 | # Written on 2019/09/19. 26 | from numpy import zeros, exp, array, prod, isnan 27 | from ..enumerate import fast_logsumexp 28 | 29 | def calc_observables(params): 30 | """ 31 | Give all parameters concatenated into one array from lowest to highest order. 32 | Returns all correlations. 33 | """ 34 | Cout = zeros((10)) 35 | H = params[0:4] 36 | J = params[4:10] 37 | energyTerms = array([ +H[0]+H[1]+H[2]+H[3]+J[0]+J[1]+J[2]+J[3]+J[4]+J[5], +H[0]+H[1]+H[2]-H[3]+J[0]+J[1]-J[2]+J[3]-J[4]-J[5], + 38 | H[0]+H[1]-H[2]+H[3]+J[0]-J[1]+J[2]-J[3]+J[4]-J[5], +H[0]+H[1]-H[2]-H[3]+J[0]-J[1]-J[2]-J[3]-J[4]+J[5], + 39 | H[0]-H[1]+H[2]+H[3]-J[0]+J[1]+J[2]-J[3]-J[4]+J[5], +H[0]-H[1]+H[2]-H[3]-J[0]+J[1]-J[2]-J[3]+J[4]-J[5], + 40 | H[0]-H[1]-H[2]+H[3]-J[0]-J[1]+J[2]+J[3]-J[4]-J[5], +H[0]-H[1]-H[2]-H[3]-J[0]-J[1]-J[2]+J[3]+J[4]+J[5], -H[0]+ 41 | H[1]+H[2]+H[3]-J[0]-J[1]-J[2]+J[3]+J[4]+J[5], -H[0]+H[1]+H[2]-H[3]-J[0]-J[1]+J[2]+J[3]-J[4]-J[5], -H[0]+ 42 | H[1]-H[2]+H[3]-J[0]+J[1]-J[2]-J[3]+J[4]-J[5], -H[0]+H[1]-H[2]-H[3]-J[0]+J[1]+J[2]-J[3]-J[4]+J[5], -H[0]-H[1]+ 43 | H[2]+H[3]+J[0]-J[1]-J[2]-J[3]-J[4]+J[5], -H[0]-H[1]+H[2]-H[3]+J[0]-J[1]+J[2]-J[3]+J[4]-J[5], -H[0]-H[1]-H[2]+ 44 | H[3]+J[0]+J[1]-J[2]+J[3]-J[4]-J[5], -H[0]-H[1]-H[2]-H[3]+J[0]+J[1]+J[2]+J[3]+J[4]+J[5],]) 45 | logZ = fast_logsumexp(energyTerms)[0] 46 | num = fast_logsumexp(energyTerms, [ 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,-1,-1,-1,-1,-1,-1]) 47 | Cout[0] = exp( num[0] - logZ ) * num[1] 48 | num = fast_logsumexp(energyTerms, [ 1, 1, 1, 1,-1,-1,-1,-1, 1, 1, 1, 1,-1,-1,-1,-1]) 49 | Cout[1] = exp( num[0] - logZ ) * num[1] 50 | num = fast_logsumexp(energyTerms, [ 1, 1,-1,-1, 1, 1,-1,-1, 1, 1,-1,-1, 1, 1,-1,-1]) 51 | Cout[2] = exp( num[0] - logZ ) * num[1] 52 | num = fast_logsumexp(energyTerms, [ 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1, 1,-1]) 53 | Cout[3] = exp( num[0] - logZ ) * num[1] 54 | num = fast_logsumexp(energyTerms, [ 1, 1, 1, 1,-1,-1,-1,-1,-1,-1,-1,-1, 1, 1, 1, 1]) 55 | Cout[4] = exp( num[0] - logZ ) * num[1] 56 | num = fast_logsumexp(energyTerms, [ 1, 1,-1,-1, 1, 1,-1,-1,-1,-1, 1, 1,-1,-1, 1, 1]) 57 | Cout[5] = exp( num[0] - logZ ) * num[1] 58 | num = fast_logsumexp(energyTerms, [ 1,-1, 1,-1, 1,-1, 1,-1,-1, 1,-1, 1,-1, 1,-1, 1]) 59 | Cout[6] = exp( num[0] - logZ ) * num[1] 60 | num = fast_logsumexp(energyTerms, [ 1, 1,-1,-1,-1,-1, 1, 1, 1, 1,-1,-1,-1,-1, 1, 1]) 61 | Cout[7] = exp( num[0] - logZ ) * num[1] 62 | num = fast_logsumexp(energyTerms, [ 1,-1, 1,-1,-1, 1,-1, 1, 1,-1, 1,-1,-1, 1,-1, 1]) 63 | Cout[8] = exp( num[0] - logZ ) * num[1] 64 | num = fast_logsumexp(energyTerms, [ 1,-1,-1, 1, 1,-1,-1, 1, 1,-1,-1, 1, 1,-1,-1, 1]) 65 | Cout[9] = exp( num[0] - logZ ) * num[1] 66 | Cout[isnan(Cout)] = 0. 67 | return(Cout) 68 | 69 | def p(params): 70 | """ 71 | Give all parameters concatenated into one array from lowest to highest order. 72 | Returns probabilities of all configurations. 73 | """ 74 | Cout = zeros((10)) 75 | H = params[0:4] 76 | J = params[4:10] 77 | H = params[0:4] 78 | J = params[4:10] 79 | Pout = zeros((16)) 80 | energyTerms = array([ +H[0]+H[1]+H[2]+H[3]+J[0]+J[1]+J[2]+J[3]+J[4]+J[5], +H[0]+H[1]+H[2]-H[3]+J[0]+J[1]-J[2]+J[3]-J[4]-J[5], + 81 | H[0]+H[1]-H[2]+H[3]+J[0]-J[1]+J[2]-J[3]+J[4]-J[5], +H[0]+H[1]-H[2]-H[3]+J[0]-J[1]-J[2]-J[3]-J[4]+J[5], + 82 | H[0]-H[1]+H[2]+H[3]-J[0]+J[1]+J[2]-J[3]-J[4]+J[5], +H[0]-H[1]+H[2]-H[3]-J[0]+J[1]-J[2]-J[3]+J[4]-J[5], + 83 | H[0]-H[1]-H[2]+H[3]-J[0]-J[1]+J[2]+J[3]-J[4]-J[5], +H[0]-H[1]-H[2]-H[3]-J[0]-J[1]-J[2]+J[3]+J[4]+J[5], -H[0]+ 84 | H[1]+H[2]+H[3]-J[0]-J[1]-J[2]+J[3]+J[4]+J[5], -H[0]+H[1]+H[2]-H[3]-J[0]-J[1]+J[2]+J[3]-J[4]-J[5], -H[0]+ 85 | H[1]-H[2]+H[3]-J[0]+J[1]-J[2]-J[3]+J[4]-J[5], -H[0]+H[1]-H[2]-H[3]-J[0]+J[1]+J[2]-J[3]-J[4]+J[5], -H[0]-H[1]+ 86 | H[2]+H[3]+J[0]-J[1]-J[2]-J[3]-J[4]+J[5], -H[0]-H[1]+H[2]-H[3]+J[0]-J[1]+J[2]-J[3]+J[4]-J[5], -H[0]-H[1]-H[2]+ 87 | H[3]+J[0]+J[1]-J[2]+J[3]-J[4]-J[5], -H[0]-H[1]-H[2]-H[3]+J[0]+J[1]+J[2]+J[3]+J[4]+J[5],]) 88 | logZ = fast_logsumexp(energyTerms)[0] 89 | Pout[0] = exp( +H[0]+H[1]+H[2]+H[3]+J[0]+J[1]+J[2]+J[3]+J[4]+J[5] - logZ ) 90 | Pout[1] = exp( +H[0]+H[1]+H[2]-H[3]+J[0]+J[1]-J[2]+J[3]-J[4]-J[5] - logZ ) 91 | Pout[2] = exp( +H[0]+H[1]-H[2]+H[3]+J[0]-J[1]+J[2]-J[3]+J[4]-J[5] - logZ ) 92 | Pout[3] = exp( +H[0]+H[1]-H[2]-H[3]+J[0]-J[1]-J[2]-J[3]-J[4]+J[5] - logZ ) 93 | Pout[4] = exp( +H[0]-H[1]+H[2]+H[3]-J[0]+J[1]+J[2]-J[3]-J[4]+J[5] - logZ ) 94 | Pout[5] = exp( +H[0]-H[1]+H[2]-H[3]-J[0]+J[1]-J[2]-J[3]+J[4]-J[5] - logZ ) 95 | Pout[6] = exp( +H[0]-H[1]-H[2]+H[3]-J[0]-J[1]+J[2]+J[3]-J[4]-J[5] - logZ ) 96 | Pout[7] = exp( +H[0]-H[1]-H[2]-H[3]-J[0]-J[1]-J[2]+J[3]+J[4]+J[5] - logZ ) 97 | Pout[8] = exp( -H[0]+H[1]+H[2]+H[3]-J[0]-J[1]-J[2]+J[3]+J[4]+J[5] - logZ ) 98 | Pout[9] = exp( -H[0]+H[1]+H[2]-H[3]-J[0]-J[1]+J[2]+J[3]-J[4]-J[5] - logZ ) 99 | Pout[10] = exp( -H[0]+H[1]-H[2]+H[3]-J[0]+J[1]-J[2]-J[3]+J[4]-J[5] - logZ ) 100 | Pout[11] = exp( -H[0]+H[1]-H[2]-H[3]-J[0]+J[1]+J[2]-J[3]-J[4]+J[5] - logZ ) 101 | Pout[12] = exp( -H[0]-H[1]+H[2]+H[3]+J[0]-J[1]-J[2]-J[3]-J[4]+J[5] - logZ ) 102 | Pout[13] = exp( -H[0]-H[1]+H[2]-H[3]+J[0]-J[1]+J[2]-J[3]+J[4]-J[5] - logZ ) 103 | Pout[14] = exp( -H[0]-H[1]-H[2]+H[3]+J[0]+J[1]-J[2]+J[3]-J[4]-J[5] - logZ ) 104 | Pout[15] = exp( -H[0]-H[1]-H[2]-H[3]+J[0]+J[1]+J[2]+J[3]+J[4]+J[5] - logZ ) 105 | 106 | Pout = Pout[::-1] 107 | return(Pout) 108 | -------------------------------------------------------------------------------- /docs/_build/html/coniii_rst/coniii.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | coniii package — ConIII 1.1.9 documentation 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 |
29 |
30 | 31 | 32 |
33 | 34 |
35 |

coniii package

36 |
37 |

Subpackages

38 | 51 |
52 |
53 |

Submodules

54 | 78 |
79 |
80 |

Module contents

81 |
82 |
83 | 84 | 85 |
86 | 87 |
88 |
89 | 138 |
139 |
140 | 151 | 152 | 153 | 154 | 155 | 156 | -------------------------------------------------------------------------------- /docs/_build/html/py-modindex.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Python Module Index — ConIII 1.1.9 documentation 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 |
31 |
32 | 33 | 34 |
35 | 36 | 37 |

Python Module Index

38 | 39 |
40 | c 41 |
42 | 43 | 44 | 45 | 47 | 48 | 50 | 53 | 54 | 55 | 58 | 59 | 60 | 63 | 64 | 65 | 68 | 69 | 70 | 73 | 74 | 75 | 78 | 79 | 80 | 83 | 84 | 85 | 88 | 89 | 90 | 93 | 94 | 95 | 98 | 99 | 100 | 103 | 104 | 105 | 108 | 109 | 110 | 113 |
 
46 | c
51 | coniii 52 |
    56 | coniii.enumerate 57 |
    61 | coniii.enumerate_potts 62 |
    66 | coniii.ising 67 |
    71 | coniii.ising.automaton 72 |
    76 | coniii.mean_field_ising 77 |
    81 | coniii.pseudo_inverse_ising 82 |
    86 | coniii.samplers 87 |
    91 | coniii.solvers 92 |
    96 | coniii.test_samplers 97 |
    101 | coniii.test_solvers 102 |
    106 | coniii.test_utils 107 |
    111 | coniii.utils 112 |
114 | 115 | 116 |
117 | 118 |
119 |
120 | 169 |
170 |
171 | 179 | 180 | 181 | 182 | 183 | 184 | -------------------------------------------------------------------------------- /RELEASE_NOTES: -------------------------------------------------------------------------------- 1 | Log for major changes in releases 2 | 3 | 4 | v3.0.1 (Released 2022/10) 5 | ---------------------------------------------------- 6 | - Bug fix in setup script. 7 | 8 | v3.0.0 (Released 2022/06) 9 | ---------------------------------------------------- 10 | - Conditional (fixed spin) Metropolis sampling now in C++). 11 | - Standardized method names for generating samples and data member names for 12 | sample to "generate_sample" and the singular "sample". 13 | - Better handling of conda include paths and Python version in setup. 14 | 15 | v2.4.4 (Released 2021/10) 16 | ---------------------------------------------------- 17 | - Updated conditional Metropolis sampling. 18 | 19 | v2.4.3 (Released 2021/05) 20 | ---------------------------------------------------- 21 | - Maintenance release to quash multiple bugs including one introduced in last 22 | release. 23 | 24 | v2.4.2 (Released 2021/04) 25 | ---------------------------------------------------- 26 | - Fixed instantiation bug in model.Ising (thanks to saynbabul for pointing this 27 | out). 28 | 29 | v2.4.1 (Released 2021/01) 30 | ---------------------------------------------------- 31 | - Fixed new bugs in installation scripts. 32 | 33 | v2.4.0 (Released 2021/01) 34 | ---------------------------------------------------- 35 | - Update for Python 3.8.3 and Boost 1.74.0. 36 | - New SparseEnumerate class for handling sparsely constrained system. 37 | 38 | v2.3.1 (Released 2020/10) 39 | ---------------------------------------------------- 40 | - Automated linking of Boost C++ module using conda-forge. 41 | 42 | v2.3.0 (Released 2020/05) 43 | ---------------------------------------------------- 44 | - Sped up Metropolis sampling using Boost C++ extension. Up to 1000x speedup 45 | achievable compared to Python version. This must be compiled separately and is 46 | not yet available through a PyPI installation. 47 | - Added variation of 3-state Potts model. 48 | 49 | v2.2.1 (Released 2019/12/2) 50 | ---------------------------------------------------- 51 | - Random number generator use update in RegularizedMeanField. 52 | 53 | v2.1.1 (Released 2019/11/29) 54 | ---------------------------------------------------- 55 | - Added convenient multiplier transformation functions utils.vec2mat and 56 | utils.mat2vec. 57 | - Output from ClusterExpansion.solve is of different order when 58 | full_output=True. Now, entropy estimate comes second instead of first to 59 | maintain consistency for when full_output=False. 60 | 61 | v2.1.0 (Released 2019/11/28) 62 | ---------------------------------------------------- 63 | - Fixed bug in Pseudo. It was using a non-standard algorithm for solving the 64 | Ising model. Find more details in Issue #8 65 | (https://github.com/eltrompetero/coniii/issues/8). 66 | 67 | v2.0.0 (Released 2019/11/28) VERSION RELEASE, BREAKS COMPATIBILITY, READ NOTES 68 | ---------------------------------------------------- 69 | - Simpler interface for using solvers with Ising model. No custom function 70 | definitions required. All are defined by default. Note that this will break 71 | compatibility with code relying on version 1. See usage_guide.ipynb for 72 | examples with new interface. 73 | - New "models.py" module to wrap evaluation of and sampling from maxent models. 74 | - Added a convenient Metropolis sampler call, samplers.sample_ising. 75 | 76 | v1.2.2 (Released 2019/09/19) 77 | ---------------------------------------------------- 78 | - Minor maintenance. 79 | 80 | v1.2.1 (Released 2019/05/31) 81 | ---------------------------------------------------- 82 | - Pseudo solver sped up and Ising-specific calculation removed. 83 | - Bugs. 84 | 85 | v1.2.0 (Released 2019/05/02) 86 | ---------------------------------------------------- 87 | - Bugs fixed in package setup for PyPI. 88 | 89 | v1.1.9 (Released 2019/04/22) 90 | ---------------------------------------------------- 91 | - Added support for Potts models in new module enumerate_potts.py. 92 | - Typos fixed in usage guide Jupyter notebook. 93 | - Added Ising class in .ising module for wrapping some useful functions for use with the 94 | Ising model. 95 | 96 | v1.1.8 (Released 2019/04/08) 97 | ---------------------------------------------------- 98 | - Bug fixes. 99 | 100 | v1.1.7 (Released 2019/03/20) 101 | ---------------------------------------------------- 102 | - enumerate.py can now write files that handle arbitrary precision using mpmath. These are 103 | written to files using the prefix '_hp.py'. 104 | - Interface bug fixes. 105 | - Updated dependency requirements to later versions of numba and numpy and new dependency 106 | on mpmath. 107 | - Some revisions to usage guide notebook. 108 | 109 | v1.1.6 (Released 2019/03/12) 110 | ---------------------------------------------------- 111 | - Bug fixes. 112 | - License date update. 113 | 114 | v1.1.5 (Released 2019/02/24) 115 | ---------------------------------------------------- 116 | - Bug fixes. 117 | 118 | v1.1.4 (Released 2019/01/06) 119 | ---------------------------------------------------- 120 | - Enumerate.solve() now uses scipy.optimize.root to find solution and is MUCH faster. 121 | 122 | v1.1.3 (Released 2018/12/17) 123 | ---------------------------------------------------- 124 | - Update to Ising equation files in v1.1.0 to make them much smaller used Scipy's 125 | logsumexp function which is slow! With own light implementation, code is at least 126 | several times faster to evaluate. 127 | 128 | v1.1.2 (Released 2018/12/16) 129 | ---------------------------------------------------- 130 | - Bug fix in type casting in Metropolis.generate_samples_parallel(). 131 | - convertTo kwarg for utils.convert_corr has been changed to convert_to to make it uniform 132 | with other functions. 133 | - convert_to is no longer a keyword argument and is now a regular argument that must be 134 | specified as a string. 135 | 136 | v1.1.0 (Released 2018/12/13) 137 | ---------------------------------------------------- 138 | - Bug fix in type casting in Metropolis.generate_samples_parallel(). 139 | - Now compatible with multiprocess v0.70.6 140 | - Updated sampling method for Metropolis to be more efficient by running fewer systems for 141 | longer and taking iterative samples instead of starting with a completely new system for 142 | every sample (which takes longer to burn in and requires starting new processes). This 143 | is automated such that the number of samples is divided evenly amongst all running 144 | processes).. 145 | - Added working version of ParallelTempering sampler. 146 | - n_cpus and nCpus are now used to refer to number of allowed processes to spawn and got 147 | rid of cpu_count which can be confused in `multiprocess.cpu_count`. 148 | - Improvements to enumerate.py: More compact Ising equation files and better precision by 149 | using scipy.special.logsumexp. 150 | - Jupyter is no longer required for installation of ConIII by default. 151 | - Ising functions (calc_e and functions that use it) assume that spins are now integer 152 | types. TypeError in jit is thrown if not. Updates to other functions for compatibility 153 | (e.g. utils.bin_states() return integer types). 154 | - Removed use_numba switch for samplers. 155 | - Removed FastMCIsing sampler. 156 | 157 | v1.0.3 (Released 2018/11) 158 | ---------------------------------------------------- 159 | - First official release. 160 | -------------------------------------------------------------------------------- /docs/_build/html/coniii_rst/coniii.ising.automaton.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | coniii.ising.automaton module — ConIII 1.1.9 documentation 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 |
31 |
32 | 33 | 34 |
35 | 36 |
37 |

coniii.ising.automaton module

38 |
39 |
40 | class coniii.ising.automaton.Ising2D(dim, J, h=0, rng=None)
41 |

Bases: object

42 |

Simulation of the ferromagnetic Ising model on a 2D periodic lattice with quenched disorder 43 | in the local fields.

44 |
45 |
46 | static flip_metropolis(i, j, h, J, lattice)
47 |

Flip a single lattice spin using Metropolis sampling.

48 |

i : int 49 | j : int

50 |
51 | 52 |
53 |
54 | iterate(n_iters, systematic=True)
55 |

n_iters : int 56 | systematic : bool,True

57 |
58 |

If True, iterate through each spin on the lattice in sequence.

59 |
60 |
61 | 62 |
63 | 64 |
65 |
66 | coniii.ising.automaton.coarse_grain(lattice, factor)
67 |

Block spin renormalization with majority rule.

68 |
69 |
latticendarray

+/-1

70 |
71 |
72 |

factor : int

73 |

renormalized_lattice : ndarray

74 |
75 | 76 |
77 | 78 | 79 |
80 | 81 |
82 |
83 | 140 |
141 |
142 | 153 | 154 | 155 | 156 | 157 | 158 | -------------------------------------------------------------------------------- /docs/_build/html/coniii_rst/coniii.test_utils.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | coniii.test_utils module — ConIII 1.1.9 documentation 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 |
29 |
30 | 31 | 32 |
33 | 34 |
35 |

coniii.test_utils module

36 |
37 |
38 | coniii.test_utils.test_adj()
39 |
40 | 41 |
42 |
43 | coniii.test_utils.test_base_repr()
44 |
45 | 46 |
47 |
48 | coniii.test_utils.test_calc_de()
49 |
50 | 51 |
52 |
53 | coniii.test_utils.test_convert_corr()
54 |
55 | 56 |
57 |
58 | coniii.test_utils.test_convert_params()
59 |
60 | 61 |
62 |
63 | coniii.test_utils.test_define_ising_helper_functions()
64 |
65 | 66 |
67 |
68 | coniii.test_utils.test_pair_corr()
69 |
70 | 71 |
72 |
73 | coniii.test_utils.test_state_gen_and_count()
74 |

Test generation of binary states using bin_states() and xbin_states().

75 |
76 | 77 |
78 |
79 | coniii.test_utils.test_sub_to_ind()
80 |
81 | 82 |
83 |
84 | coniii.test_utils.test_vec2mat()
85 |
86 | 87 |
88 | 89 | 90 |
91 | 92 |
93 |
94 | 143 |
144 |
145 | 156 | 157 | 158 | 159 | 160 | 161 | -------------------------------------------------------------------------------- /coniii/ising_eqn/ising_eqn_5.py: -------------------------------------------------------------------------------- 1 | # MIT License 2 | # 3 | # Copyright (c) 2019 Edward D. Lee, Bryan C. Daniels 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | 23 | # Equations for 5-spin Ising model. 24 | 25 | # Written on 2019/09/19. 26 | from numpy import zeros, exp, array, prod, isnan 27 | from ..enumerate import fast_logsumexp 28 | 29 | def calc_observables(params): 30 | """ 31 | Give all parameters concatenated into one array from lowest to highest order. 32 | Returns all correlations. 33 | """ 34 | Cout = zeros((15)) 35 | H = params[0:5] 36 | J = params[5:15] 37 | energyTerms = array([ +0, +H[4]+0, +H[3]+0, +H[3]+H[4]+J[9], +H[2]+0, +H[2]+H[4]+J[8], +H[2]+H[3]+J[7], +H[2]+H[3]+H[4]+J[7]+ 38 | J[8]+J[9], +H[1]+0, +H[1]+H[4]+J[6], +H[1]+H[3]+J[5], +H[1]+H[3]+H[4]+J[5]+J[6]+J[9], +H[1]+H[2]+J[4], + 39 | H[1]+H[2]+H[4]+J[4]+J[6]+J[8], +H[1]+H[2]+H[3]+J[4]+J[5]+J[7], +H[1]+H[2]+H[3]+H[4]+J[4]+J[5]+J[6]+J[7]+ 40 | J[8]+J[9], +H[0]+0, +H[0]+H[4]+J[3], +H[0]+H[3]+J[2], +H[0]+H[3]+H[4]+J[2]+J[3]+J[9], +H[0]+H[2]+J[1], + 41 | H[0]+H[2]+H[4]+J[1]+J[3]+J[8], +H[0]+H[2]+H[3]+J[1]+J[2]+J[7], +H[0]+H[2]+H[3]+H[4]+J[1]+J[2]+J[3]+J[7]+ 42 | J[8]+J[9], +H[0]+H[1]+J[0], +H[0]+H[1]+H[4]+J[0]+J[3]+J[6], +H[0]+H[1]+H[3]+J[0]+J[2]+J[5], +H[0]+H[1]+ 43 | H[3]+H[4]+J[0]+J[2]+J[3]+J[5]+J[6]+J[9], +H[0]+H[1]+H[2]+J[0]+J[1]+J[4], +H[0]+H[1]+H[2]+H[4]+J[0]+J[1]+ 44 | J[3]+J[4]+J[6]+J[8], +H[0]+H[1]+H[2]+H[3]+J[0]+J[1]+J[2]+J[4]+J[5]+J[7], +H[0]+H[1]+H[2]+H[3]+H[4]+J[0]+ 45 | J[1]+J[2]+J[3]+J[4]+J[5]+J[6]+J[7]+J[8]+J[9],]) 46 | logZ = fast_logsumexp(energyTerms)[0] 47 | num = fast_logsumexp(energyTerms, [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) 48 | Cout[0] = exp( num[0] - logZ ) * num[1] 49 | num = fast_logsumexp(energyTerms, [0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1]) 50 | Cout[1] = exp( num[0] - logZ ) * num[1] 51 | num = fast_logsumexp(energyTerms, [0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1]) 52 | Cout[2] = exp( num[0] - logZ ) * num[1] 53 | num = fast_logsumexp(energyTerms, [0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1]) 54 | Cout[3] = exp( num[0] - logZ ) * num[1] 55 | num = fast_logsumexp(energyTerms, [0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]) 56 | Cout[4] = exp( num[0] - logZ ) * num[1] 57 | num = fast_logsumexp(energyTerms, [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1]) 58 | Cout[5] = exp( num[0] - logZ ) * num[1] 59 | num = fast_logsumexp(energyTerms, [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1]) 60 | Cout[6] = exp( num[0] - logZ ) * num[1] 61 | num = fast_logsumexp(energyTerms, [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1]) 62 | Cout[7] = exp( num[0] - logZ ) * num[1] 63 | num = fast_logsumexp(energyTerms, [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]) 64 | Cout[8] = exp( num[0] - logZ ) * num[1] 65 | num = fast_logsumexp(energyTerms, [0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1]) 66 | Cout[9] = exp( num[0] - logZ ) * num[1] 67 | num = fast_logsumexp(energyTerms, [0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1]) 68 | Cout[10] = exp( num[0] - logZ ) * num[1] 69 | num = fast_logsumexp(energyTerms, [0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,1]) 70 | Cout[11] = exp( num[0] - logZ ) * num[1] 71 | num = fast_logsumexp(energyTerms, [0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1]) 72 | Cout[12] = exp( num[0] - logZ ) * num[1] 73 | num = fast_logsumexp(energyTerms, [0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1]) 74 | Cout[13] = exp( num[0] - logZ ) * num[1] 75 | num = fast_logsumexp(energyTerms, [0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1]) 76 | Cout[14] = exp( num[0] - logZ ) * num[1] 77 | Cout[isnan(Cout)] = 0. 78 | return(Cout) 79 | 80 | def p(params): 81 | """ 82 | Give all parameters concatenated into one array from lowest to highest order. 83 | Returns probabilities of all configurations. 84 | """ 85 | Cout = zeros((15)) 86 | H = params[0:5] 87 | J = params[5:15] 88 | H = params[0:5] 89 | J = params[5:15] 90 | Pout = zeros((32)) 91 | energyTerms = array([ +0, +H[4]+0, +H[3]+0, +H[3]+H[4]+J[9], +H[2]+0, +H[2]+H[4]+J[8], +H[2]+H[3]+J[7], +H[2]+H[3]+H[4]+J[7]+ 92 | J[8]+J[9], +H[1]+0, +H[1]+H[4]+J[6], +H[1]+H[3]+J[5], +H[1]+H[3]+H[4]+J[5]+J[6]+J[9], +H[1]+H[2]+J[4], + 93 | H[1]+H[2]+H[4]+J[4]+J[6]+J[8], +H[1]+H[2]+H[3]+J[4]+J[5]+J[7], +H[1]+H[2]+H[3]+H[4]+J[4]+J[5]+J[6]+J[7]+ 94 | J[8]+J[9], +H[0]+0, +H[0]+H[4]+J[3], +H[0]+H[3]+J[2], +H[0]+H[3]+H[4]+J[2]+J[3]+J[9], +H[0]+H[2]+J[1], + 95 | H[0]+H[2]+H[4]+J[1]+J[3]+J[8], +H[0]+H[2]+H[3]+J[1]+J[2]+J[7], +H[0]+H[2]+H[3]+H[4]+J[1]+J[2]+J[3]+J[7]+ 96 | J[8]+J[9], +H[0]+H[1]+J[0], +H[0]+H[1]+H[4]+J[0]+J[3]+J[6], +H[0]+H[1]+H[3]+J[0]+J[2]+J[5], +H[0]+H[1]+ 97 | H[3]+H[4]+J[0]+J[2]+J[3]+J[5]+J[6]+J[9], +H[0]+H[1]+H[2]+J[0]+J[1]+J[4], +H[0]+H[1]+H[2]+H[4]+J[0]+J[1]+ 98 | J[3]+J[4]+J[6]+J[8], +H[0]+H[1]+H[2]+H[3]+J[0]+J[1]+J[2]+J[4]+J[5]+J[7], +H[0]+H[1]+H[2]+H[3]+H[4]+J[0]+ 99 | J[1]+J[2]+J[3]+J[4]+J[5]+J[6]+J[7]+J[8]+J[9],]) 100 | logZ = fast_logsumexp(energyTerms)[0] 101 | Pout[0] = exp( +0 - logZ ) 102 | Pout[1] = exp( +H[4]+0 - logZ ) 103 | Pout[2] = exp( +H[3]+0 - logZ ) 104 | Pout[3] = exp( +H[3]+H[4]+J[9] - logZ ) 105 | Pout[4] = exp( +H[2]+0 - logZ ) 106 | Pout[5] = exp( +H[2]+H[4]+J[8] - logZ ) 107 | Pout[6] = exp( +H[2]+H[3]+J[7] - logZ ) 108 | Pout[7] = exp( +H[2]+H[3]+H[4]+J[7]+J[8]+J[9] - logZ ) 109 | Pout[8] = exp( +H[1]+0 - logZ ) 110 | Pout[9] = exp( +H[1]+H[4]+J[6] - logZ ) 111 | Pout[10] = exp( +H[1]+H[3]+J[5] - logZ ) 112 | Pout[11] = exp( +H[1]+H[3]+H[4]+J[5]+J[6]+J[9] - logZ ) 113 | Pout[12] = exp( +H[1]+H[2]+J[4] - logZ ) 114 | Pout[13] = exp( +H[1]+H[2]+H[4]+J[4]+J[6]+J[8] - logZ ) 115 | Pout[14] = exp( +H[1]+H[2]+H[3]+J[4]+J[5]+J[7] - logZ ) 116 | Pout[15] = exp( +H[1]+H[2]+H[3]+H[4]+J[4]+J[5]+J[6]+J[7]+J[8]+J[9] - logZ ) 117 | Pout[16] = exp( +H[0]+0 - logZ ) 118 | Pout[17] = exp( +H[0]+H[4]+J[3] - logZ ) 119 | Pout[18] = exp( +H[0]+H[3]+J[2] - logZ ) 120 | Pout[19] = exp( +H[0]+H[3]+H[4]+J[2]+J[3]+J[9] - logZ ) 121 | Pout[20] = exp( +H[0]+H[2]+J[1] - logZ ) 122 | Pout[21] = exp( +H[0]+H[2]+H[4]+J[1]+J[3]+J[8] - logZ ) 123 | Pout[22] = exp( +H[0]+H[2]+H[3]+J[1]+J[2]+J[7] - logZ ) 124 | Pout[23] = exp( +H[0]+H[2]+H[3]+H[4]+J[1]+J[2]+J[3]+J[7]+J[8]+J[9] - logZ ) 125 | Pout[24] = exp( +H[0]+H[1]+J[0] - logZ ) 126 | Pout[25] = exp( +H[0]+H[1]+H[4]+J[0]+J[3]+J[6] - logZ ) 127 | Pout[26] = exp( +H[0]+H[1]+H[3]+J[0]+J[2]+J[5] - logZ ) 128 | Pout[27] = exp( +H[0]+H[1]+H[3]+H[4]+J[0]+J[2]+J[3]+J[5]+J[6]+J[9] - logZ ) 129 | Pout[28] = exp( +H[0]+H[1]+H[2]+J[0]+J[1]+J[4] - logZ ) 130 | Pout[29] = exp( +H[0]+H[1]+H[2]+H[4]+J[0]+J[1]+J[3]+J[4]+J[6]+J[8] - logZ ) 131 | Pout[30] = exp( +H[0]+H[1]+H[2]+H[3]+J[0]+J[1]+J[2]+J[4]+J[5]+J[7] - logZ ) 132 | Pout[31] = exp( +H[0]+H[1]+H[2]+H[3]+H[4]+J[0]+J[1]+J[2]+J[3]+J[4]+J[5]+J[6]+J[7]+J[8]+J[9] - logZ ) 133 | 134 | return(Pout) 135 | -------------------------------------------------------------------------------- /coniii/test_samplers.py: -------------------------------------------------------------------------------- 1 | # =============================================================================================== # 2 | # Testing for samplers.py module. 3 | # Released with ConIII package. 4 | # Author : Eddie Lee, edlee@alumni.princeton.edu 5 | # 6 | # MIT License 7 | # 8 | # Copyright (c) 2020 Edward D. Lee, Bryan C. Daniels 9 | # 10 | # Permission is hereby granted, free of charge, to any person obtaining a copy 11 | # of this software and associated documentation files (the "Software"), to deal 12 | # in the Software without restriction, including without limitation the rights 13 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | # copies of the Software, and to permit persons to whom the Software is 15 | # furnished to do so, subject to the following conditions: 16 | # 17 | # The above copyright notice and this permission notice shall be included in all 18 | # copies or substantial portions of the Software. 19 | # 20 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | # SOFTWARE. 27 | # =============================================================================================== # 28 | import numpy as np 29 | import time 30 | 31 | from .samplers import * 32 | from .utils import define_ising_helper_functions 33 | 34 | n = 5 35 | 36 | 37 | def test_sample_ising(): 38 | assert sample_ising(np.zeros(n+n*(n-1)//2), 100).shape==(100,n) 39 | 40 | def test_Metropolis(run_timing=False): 41 | # Check that everything compiles and runs. 42 | theta = np.random.normal(size=15, scale=.1) 43 | calc_e, _, _ = define_ising_helper_functions() 44 | print("Running timing suite for Metropolis sampling functions for n=%d..."%n) 45 | 46 | sampler = Metropolis(n, theta, calc_e) 47 | print("Running sampler.generate_sample(n)") 48 | sampler.generate_sample(100) 49 | print("Done.") 50 | 51 | print("Running sampler.generate_sample(n, systematic_iter=True)") 52 | sampler.generate_sample(100, systematic_iter=True) 53 | print("Done.") 54 | 55 | # test control over rng 56 | sampler = Metropolis(n, theta, calc_e, rng=np.random.RandomState(0)) 57 | sampler.generate_sample(100, systematic_iter=True) 58 | X1 = sampler.sample.copy() 59 | 60 | sampler = Metropolis(n, theta, calc_e, rng=np.random.RandomState(0)) 61 | sampler.generate_sample(100, systematic_iter=True) 62 | X2 = sampler.sample.copy() 63 | 64 | assert np.array_equal(X1, X2), (X1, X2) 65 | 66 | # parallelization 67 | print("Running sampler.generate_sample_parallel(100, systematic_iter=True)") 68 | sampler = Metropolis(n, theta, calc_e, rng=np.random.RandomState(0)) 69 | sampler.generate_sample_parallel(100, systematic_iter=True) 70 | X1 = sampler.sample.copy() 71 | 72 | print("Running sampler.generate_sample_parallel(100, systematic_iter=True)") 73 | sampler = Metropolis(n, theta, calc_e, rng=np.random.RandomState(0)) 74 | sampler.generate_sample_parallel(100, systematic_iter=True) 75 | X2 = sampler.sample.copy() 76 | assert np.array_equal(X1, X2) 77 | 78 | # parallelization without systematic iter 79 | print("Running sampler.generate_sample_parallel(100, systematic_iter=False)") 80 | sampler = Metropolis(n, theta, calc_e, rng=np.random.RandomState(0)) 81 | sampler.generate_sample_parallel(100, systematic_iter=False) 82 | X1 = sampler.sample.copy() 83 | 84 | print("Running sampler.generate_sample_parallel(100, systematic_iter=False)") 85 | sampler = Metropolis(n, theta, calc_e, rng=np.random.RandomState(0)) 86 | sampler.generate_sample_parallel(100, systematic_iter=False) 87 | X2 = sampler.sample.copy() 88 | assert np.array_equal(X1, X2) 89 | 90 | if run_timing: 91 | # Some basic timing checks 92 | print("Timing sequential sampling") 93 | sampler = Metropolis(n, theta, calc_e) 94 | t0=time.perf_counter() 95 | sampler.generate_sample(100, n_iters=10000, systematic_iter=True) 96 | print(time.perf_counter()-t0) 97 | 98 | print("Timing parallel sampling") 99 | sampler = Metropolis(n, theta, calc_e) 100 | t0=time.perf_counter() 101 | sampler.generate_sample_parallel(100, n_iters=10000, systematic_iter=True) 102 | print(time.perf_counter()-t0) 103 | 104 | def test_Potts3(): 105 | np.random.seed(0) 106 | 107 | # Check that everything compiles and runs. 108 | n = 5 109 | theta = np.random.normal(size=n*3 + n*(n-1)//2, scale=.1) 110 | calc_e = define_potts_helper_functions(3)[0] 111 | print("Running timing suite for Potts3 sampling functions for n=%d..."%n) 112 | 113 | sampler = Potts3(n, theta, calc_e, n_cpus=1) 114 | print("Running sampler.generate_sample(n)") 115 | sampler.generate_sample(100) 116 | print("Done.") 117 | 118 | print("Running sampler.generate_sample(n, systematic_iter=True)") 119 | sampler.generate_sample(100, systematic_iter=True) 120 | print("Done.") 121 | 122 | # test control over rng 123 | sampler = Potts3(n, theta, calc_e, n_cpus=1, rng=np.random.RandomState(0)) 124 | sampler.generate_sample(100, n_iters=10, systematic_iter=True) 125 | X1 = sampler.sample.copy() 126 | 127 | sampler = Potts3(n, theta, calc_e, n_cpus=1, rng=np.random.RandomState(0)) 128 | sampler.generate_sample(100, n_iters=10, systematic_iter=True) 129 | X2 = sampler.sample.copy() 130 | assert np.array_equal(X1, X2), (X1[:10], X2[:10]) 131 | 132 | # parallelization 133 | print("Running sampler.generate_sample_parallel(100, systematic_iter=True)") 134 | sampler = Potts3(n, theta, calc_e, rng=np.random.RandomState(0)) 135 | sampler.generate_sample_parallel(100, systematic_iter=True) 136 | X1 = sampler.sample.copy() 137 | 138 | print("Running sampler.generate_sample_parallel(100, systematic_iter=True)") 139 | sampler = Potts3(n, theta, calc_e, rng=np.random.RandomState(0)) 140 | sampler.generate_sample_parallel(100, systematic_iter=True) 141 | X2 = sampler.sample.copy() 142 | assert np.array_equal(X1, X2) 143 | 144 | # parallelization without systematic iter 145 | print("Running sampler.generate_sample_parallel(100, systematic_iter=False)") 146 | sampler = Potts3(n, theta, calc_e, rng=np.random.RandomState(0)) 147 | sampler.generate_sample_parallel(100, systematic_iter=False) 148 | X1 = sampler.sample.copy() 149 | 150 | print("Running sampler.generate_sample_parallel(100, systematic_iter=False)") 151 | sampler = Potts3(n, theta, calc_e, rng=np.random.RandomState(0)) 152 | sampler.generate_sample_parallel(100, systematic_iter=False) 153 | X2 = sampler.sample.copy() 154 | assert np.array_equal(X1, X2) 155 | 156 | def test_ParallelTempering(): 157 | # basic functionality 158 | theta = np.random.normal(size=n+n*(n-1)//2, scale=.1) 159 | calc_e = define_ising_helper_functions()[0] 160 | sampler = ParallelTempering(n, theta, calc_e, 4, (1,3)) 161 | sampler.generate_sample(100) 162 | 163 | 164 | #if __name__=='__main__': 165 | def compare_samplers(): 166 | import time 167 | 168 | 169 | # check for similarity in results between different samplers 170 | np.random.seed(0) 171 | theta = np.random.normal(size=n+n*(n-1)//2, scale=.1) 172 | calc_e,_,_ = define_ising_helper_functions() 173 | nSamples = 1_000 174 | 175 | t0 = time.perf_counter() 176 | sampler1 = ParallelTempering(n, theta, calc_e, 4, (1,3), replica_burnin=n*100, rep_ex_burnin=n*10) 177 | sampler1.generate_sample(nSamples, save_exchange_trajectory=True) 178 | print("Sampler 1 took %1.2f s."%(time.perf_counter()-t0)) 179 | 180 | t0 = time.perf_counter() 181 | sampler2 = Metropolis(n, theta, calc_e) 182 | sampler2.generate_sample_parallel(nSamples) 183 | print("Sampler 2 took %1.2f s."%(time.perf_counter()-t0)) 184 | 185 | from .ising_eqn.ising_eqn_5_sym import calc_observables 186 | print(sampler1.sample[-1].mean(0), sampler2.sample.mean(0), calc_observables(theta)[:n]) 187 | print(sampler1.sample[-1].std(axis=0)/np.sqrt(nSamples), sampler2.sample.std(axis=0)/np.sqrt(nSamples)) 188 | 189 | return sampler1, sampler2 190 | --------------------------------------------------------------------------------