Skip to content

Commit

Permalink
add reader for dnest4
Browse files Browse the repository at this point in the history
  • Loading branch information
qacwnfq committed Jul 16, 2024
1 parent d367a4b commit 4fef6df
Show file tree
Hide file tree
Showing 11 changed files with 4,418 additions and 1 deletion.
4 changes: 3 additions & 1 deletion anesthetic/read/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from anesthetic.read.multinest import read_multinest
from anesthetic.read.ultranest import read_ultranest
from anesthetic.read.nestedfit import read_nestedfit
from anesthetic.read.dnest4 import read_dnest4
from anesthetic.read.csv import read_csv


Expand All @@ -19,6 +20,7 @@ def read_chains(root, *args, **kwargs):
* `Nested_fit <https://github.com/martinit18/Nested_Fit>`_,
* `CosmoMC <https://github.com/cmbant/CosmoMC>`_,
* `Cobaya <https://github.com/CobayaSampler/cobaya>`_,
* `DNest4 <https://github.com/eggplantbren/DNest4/tree/master/code>`_,
* anything `GetDist <https://github.com/cmbant/getdist>`_ compatible,
* files produced using ``DataFrame.to_csv()`` from anesthetic.
Expand Down Expand Up @@ -53,7 +55,7 @@ def read_chains(root, *args, **kwargs):
)
errors = []
readers = [
read_polychord, read_multinest, read_cobaya, read_ultranest,
read_polychord, read_multinest, read_cobaya, read_ultranest, read_dnest4,
read_nestedfit, read_getdist, read_csv
]
for read in readers:
Expand Down
45 changes: 45 additions & 0 deletions anesthetic/read/dnest4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Read NestedSamples from dnest4 output files"""
import os
import numpy as np
from anesthetic.samples import MCMCSamples


def heuristically_determine_columns(n_params):
"""
heuristically determines column names. If none are given, parameters are named x_i
"""
return [f'x{i}' for i in range(n_params)]


def read_dnest4(root,
*args,
**kwargs):
"""Read dnest4 output files.
Parameters
----------
levels_file default output name from dnest4
sample_file default output name from dnest4
sample_info_file default output name from dnest4
root : str
root specify the directory only, no specific roots,
The files read files are levels_file, sample_file and sample_info.
"""
levels_file = 'levels.txt'
sample_file = 'sample.txt'
sample_info_file = 'sample_info.txt'
weights_file = 'weights.txt'

levels = np.loadtxt(os.path.join(root, levels_file), dtype=float, delimiter=' ', comments='#')
samples = np.genfromtxt(os.path.join(root, sample_file), dtype=float, delimiter=' ', comments='#', skip_header=1)
sample_info = np.loadtxt(os.path.join(root, sample_info_file), dtype=float, delimiter=' ', comments='#')
weights = np.loadtxt(os.path.join(root, weights_file), dtype=float, delimiter=' ', comments='#')
n_params = samples.shape[1]
columns = heuristically_determine_columns(n_params)

return MCMCSamples(data=samples,
columns=columns,
weights=weights,
logL=sample_info[:, 1],
labels=columns, *args,
**kwargs)
9 changes: 9 additions & 0 deletions docs/source/anesthetic.read.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ anesthetic.read.cobaya module
:undoc-members:
:show-inheritance:

anesthetic.read.dnest4 module
-----------------------------

.. automodule:: anesthetic.read.dnest4
:members:
:undoc-members:
:show-inheritance:



anesthetic.read.getdist module
------------------------------
Expand Down
2 changes: 2 additions & 0 deletions tests/example_data/dnest4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
these results are generated with https://github.com/eggplantbren/DNest4/tree/master/code/Examples/Rosenbrock
after hard-coding the number of dimensions to 3 and then postprocessing with showresults.p
28 changes: 28 additions & 0 deletions tests/example_data/dnest4/levels.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# log_X, log_likelihood, tiebreaker, accepts, tries, exceeds, visits
0 -1.79769313486e+308 0 34004 34004 11180 29004
-0.969533002366 -354500.27465 0.184673637139 23998 31610 14123 39627
-1.992093494 -51190.6265937 0.102641250532 19348 31083 15920 42045
-2.97105585853 -11243.6222298 0.10325874618 16150 32920 16626 45679
-3.97897207133 -3228.1447332 0.0925587904123 13328 34880 17901 48344
-4.97405916511 -1131.35058306 0.852351592573 11151 35275 18846 50013
-5.95575811056 -427.258976564 0.701862363078 10072 37653 19414 53338
-6.96396646863 -162.646813112 0.68451125986 9137 39867 21029 56120
-7.94957402697 -64.8403524809 0.613021892441 8647 43702 20432 61568
-9.03067901421 -26.3056692322 0.547511916734 7828 44506 24788 61778
-9.96095625455 -13.8309395233 0.334151142837 8156 49730 25984 71356
-10.9692987447 -7.50274694105 0.126042530841 7817 52371 33317 75193
-11.8132222594 -4.65369550562 0.853768724211 8272 60078 29479 90233
-12.9133196991 -2.48807948192 0.61849557121 7804 61733 25002 88050
-14.128256649 -1.16046735761 0.99591232612 7330 60886 26282 82727
-15.2498883059 -0.562856436041 0.130358708691 7127 64485 34548 87606
-16.1907059421 -0.325470154533 0.755185646839 7784 74101 16481 105486
-17.8844715844 -0.13177445907 0.64628464408 5675 57277 8900 70597
-19.6545796919 -0.0423777562198 0.393064674565 3095 33798 8676 39537
-20.9945860552 -0.0201426854991 0.336737451471 2296 26703 9618 32218
-22.1297830757 -0.0109547712599 0.186364854844 2101 23981 9711 30437
-23.2211859552 -0.00633328574242 0.761743194641 1908 22712 7536 29262
-24.4377625857 -0.00321219414708 0.379439831193 1392 17613 6798 21988
-25.5351949699 -0.00173432053459 0.0956554015875 1166 15570 3206 19207
-26.8913310896 -0.000907997439061 0.454404674518 667 8955 1351 9000
-28.133055436 -0.000638533356738 0.942850144857 290 3915 142 2105
-29.2339238102 -0.000420761812001 0.219330831487 37 592 0 0
Loading

0 comments on commit 4fef6df

Please sign in to comment.