-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrades_ultranest.py
222 lines (170 loc) · 5.46 KB
/
trades_ultranest.py
1
2
3
4
5
6
7
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# no more "zero" integer division bugs!:P
import argparse
import os
import numpy as np # array
# import h5py
import sys
import time
# import glob
# import multiprocessing as mp
# from multiprocessing import Pool
import ultranest
from ultranest import stepsampler
from pytrades.constants import Mjups, Msear
from pytrades import ancillary as anc
# from pytrades_lib import f90trades
from pytrades import pytrades
# =============================================================================
def mpi_print(l, rank, log=None):
if rank == 0:
anc.print_both(l, output=log)
return
try:
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
nthreads = comm.Get_size()
except:
comm = None
rank = 0
nthreads = 1
# from mpi4py import MPI
# comm = MPI.COMM_WORLD
# rank = comm.Get_rank()
# nthreads = comm.Get_size()
print("Hi! I am thread {} out of {}".format(rank, nthreads))
# =============================================================================
# sys.exit()
init_folder = anc.init_folder
compute_proper_sigma = anc.compute_proper_sigma
compute_initial_walkers = anc.compute_initial_walkers
# ==============================================================================
# =============================================================================
# =============================================================================
# MAIN SCRIPT - NOT IN FUNCTION DUE TO ISSUE WITH PARALLEL AND PICKLE FUNCTION OF LNPROB..
# =============================================================================
# =============================================================================
# MAIN -- TRADES + ULTRANEST
# READ COMMAND LINE ARGUMENTS
yml_file = anc.get_input_file()
cli = anc.ConfigurationRun(yml_file)
os.environ["OMP_NUM_THREADS"] = "1"
# os.environ["HDF5_USE_FILE_LOCKING"] = "FALSE"
# STARTING TIME
start = time.time()
# RENAME
working_path = cli.full_path
# nthreads = cli.nthreads
np.random.seed(cli.seed)
# INITIALISE TRADES WITH SUBROUTINE WITHIN TRADES_LIB -> PARAMETER NAMES, MINMAX, INTEGRATION ARGS, READ DATA ...
# pytrades.initialize_trades(working_path, cli.sub_folder, nthreads)
mpi_print("Init trades ... ", rank)
if rank == 0:
sim = pytrades.TRADES(
working_path,
sub_folder=cli.sub_folder,
nthreads=cli.nthreads,
seed=cli.seed,
m_type=cli.m_type
)
sim.init_trades()
sys.stdout.flush()
#
# FUNCTION NEEDED BY ULTRANEST
#
def my_prior_transform(cube):
params = np.array(cube.copy())
bounds = sim.fitting_minmax
db = bounds[:, 1] - bounds[:, 0]
params = bounds[:, 0] + np.array(cube) * db
return params
def lnL_ultranest(fitting_parameters):
(
# chi_square,
# reduced_chi_square,
# lgllhd,
# lnprior,
# ln_const,
# bic,
# check,
_,
_,
lnL,
lnp,
_,
_,
check,
) = sim.run_and_get_stats_from_parameters(fitting_parameters)
return lnL + lnp
# INITIALISE SCRIPT FOLDER/LOG FILE
working_folder, _, of_run = init_folder(working_path, cli.sub_folder)
sys.stdout.flush()
mpi_print("ULTRANEST", rank, log=of_run)
resume_flag = ["resume", "resume-similar", "overwrite", "subfolder"]
if cli.resume_flag not in resume_flag:
mpi_print(
"ERROR: ultranest resume flag not available. please select among: {}".format(
resume_flag
), rank, log=of_run
)
sys.exit()
wrapped_pars = anc.check_wrapped_parameters(sim.fitting_names)
nfit_min = 2 * sim.nfit
if cli.live_points < nfit_min:
n_live_points = nfit_min
anc.print_both("set n_live_points = {}".format(n_live_points))
else:
n_live_points = cli.live_points
sampler = ultranest.ReactiveNestedSampler(
sim.fitting_names,
lnL_ultranest,
my_prior_transform,
log_dir=working_folder, # folder where to store files
resume=cli.resume_flag, # whether to resume from there (otherwise start from scratch)
# vectorized=True, # NOT WORKING WITH TRADES ...
storage_backend="hdf5",
wrapped_params=wrapped_pars,
draw_multiple=True,
)
nsteps = 2 * sim.nfit
# create step sampler:
sampler.stepsampler = stepsampler.RegionSliceSampler(
nsteps=nsteps, adaptive_nsteps="move-distance"
)
sys.stdout.flush()
result = sampler.run(
min_num_live_points = n_live_points,
dlogz = cli.dlogz, # desired accuracy on logz
# min_ess=n_live_points, # number of effective samples
# update_interval_iter_fraction=0.4, # how often to update region !!NOT HERE?!!
# max_num_improvement_loops=3, # how many times to go back and improve
)
elapsed = time.time() - start
elapsed_d, elapsed_h, elapsed_m, elapsed_s = anc.computation_time(elapsed)
mpi_print("COMPLETED ULTRANEST", rank, log=of_run)
sys.stdout.flush()
mpi_print("", rank, log=of_run)
mpi_print(
" pyTRADES: ULTRANEST FINISHED in {0:2d} day {1:02d} hour {2:02d} min {3:.2f} sec - bye bye".format(
int(elapsed_d), int(elapsed_h), int(elapsed_m), elapsed_s
),
rank,
log=of_run,
)
mpi_print("", rank, log=of_run)
sys.stdout.flush()
sampler.print_results()
sys.stdout.flush()
sampler.plot_run()
sampler.plot_trace()
sampler.plot_corner()
mpi_print("", rank, log=of_run)
of_run.close()
sim.reset()
# return
# ==============================================================================
# ==============================================================================
# if __name__ == "__main__":
# main()