/
__init__.py
332 lines (209 loc) · 8.19 KB
/
__init__.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# We import matplotlib first, because we need control on the backend
# Indeed, if no DISPLAY variable is set, matplotlib 2.0 crashes (at the moment, 05/26/2017)
import pandas as pd
pd.set_option("max_columns", None)
import os
import warnings
if os.environ.get("DISPLAY") is None:
warnings.warn(
"No DISPLAY variable set. Using backend for graphics without display (Agg)"
)
import matplotlib as mpl
mpl.use("Agg")
# Workaround to a CFITSIO issue
try:
import ROOT
except ImportError:
pass
try:
import pyLikelihood
except ImportError:
pass
# Import version (this has to be placed before the import of serialization
# since __version__ needs to be defined at that stage)
from ._version import get_versions
__version__ = get_versions()["version"]
del get_versions
# Finally import the serialization machinery
from .io.serialization import *
from .exceptions.custom_exceptions import custom_warnings
import glob
import imp
import traceback
# Import everything from astromodels
from astromodels import *
# Now import the optimizers first (to avoid conflicting libraries problems)
from .minimizer.minimization import _minimizers, LocalMinimization, GlobalMinimization
# This must be here before the automatic import of subpackages,
# otherwise we will incur in weird issues with other packages
# using similar names (for example, the io package)
from .exceptions import custom_exceptions
from .plugin_prototype import PluginPrototype
try:
# noinspection PyUnresolvedReferences
from cthreeML.pyModelInterfaceCache import pyToCppModelInterfaceCache
except ImportError:
custom_warnings.warn(
"The cthreeML package is not installed. You will not be able to use plugins which require "
"the C/C++ interface (currently HAWC)",
custom_exceptions.CppInterfaceNotAvailable,
)
# Now look for plugins
# This verifies if a module is importable
def is_module_importable(module_full_path):
try:
_ = imp.load_source("__", module_full_path)
except:
return False, traceback.format_exc()
else:
return True, "%s imported ok" % module_full_path
plugins_dir = os.path.join(os.path.dirname(__file__), "plugins")
found_plugins = glob.glob(os.path.join(plugins_dir, "*.py"))
# Filter out __init__
found_plugins = filter(lambda x: x.find("__init__") < 0, found_plugins)
_working_plugins = {}
_not_working_plugins = {}
# Loop over each candidates plugins and check if it is importable
for i, module_full_path in enumerate(found_plugins):
plugin_name = os.path.splitext(os.path.basename(module_full_path))[0]
is_importable, failure_traceback = is_module_importable(module_full_path)
if not is_importable:
custom_warnings.warn(
"Could not import plugin %s. Do you have the relative instrument software installed "
"and configured?" % os.path.basename(module_full_path),
custom_exceptions.CannotImportPlugin,
)
_not_working_plugins[plugin_name] = failure_traceback
continue
else:
# First get the instrument name
try:
exec("from threeML.plugins.%s import __instrument_name" % plugin_name)
except ImportError:
# This module does not contain a plugin, continue
continue
# Now import the plugin itself
import_command = "from threeML.plugins.%s import %s" % (
plugin_name,
plugin_name,
)
try:
exec(import_command)
except ImportError:
pass
else:
_working_plugins[__instrument_name] = plugin_name
# Now some convenience functions
def get_available_plugins():
"""
Print a list of available plugins
:return:
"""
print("Available plugins:\n")
for instrument, class_name in _working_plugins.items():
print("%s for %s" % (class_name, instrument))
def _display_plugin_traceback(plugin):
print("#############################################################")
print("\nCouldn't import plugin %s" % plugin)
print("\nTraceback:\n")
print(_not_working_plugins[plugin])
print("#############################################################")
def is_plugin_available(plugin):
"""
Test whether the plugin for the provided instrument is available
:param plugin: the name of the plugin class
:return: True or False
"""
if plugin in _working_plugins.values():
# FIXME
if plugin == "FermipyLike":
try:
_ = FermipyLike.__new__(FermipyLike, test=True)
except:
# Do not register it
_not_working_plugins[plugin] = traceback.format_exc()
_display_plugin_traceback(plugin)
return False
return True
else:
if plugin in _not_working_plugins:
_display_plugin_traceback(plugin)
return False
else:
raise RuntimeError("Plugin %s is not known" % plugin)
# Import the classic Maximum Likelihood Estimation package
from .classicMLE.joint_likelihood import JointLikelihood
# Import the Bayesian analysis
from .bayesian.bayesian_analysis import BayesianAnalysis
# Import the DataList class
from .data_list import DataList
from threeML.io.plotting.model_plot import plot_spectra, plot_point_source_spectra
from threeML.io.plotting.light_curve_plots import plot_tte_lightcurve
from threeML.io.plotting.post_process_data_plots import (
display_spectrum_model_counts,
display_photometry_model_magnitudes,
)
# Import the joint likelihood set
from .classicMLE.joint_likelihood_set import (
JointLikelihoodSet,
JointLikelihoodSetAnalyzer,
)
from .classicMLE.likelihood_ratio_test import LikelihoodRatioTest
from .classicMLE.goodness_of_fit import GoodnessOfFit
from .io.calculate_flux import calculate_point_source_flux
# Added by JM. step generator for time-resolved fits
from .utils.step_parameter_generator import step_generator
from .parallel.parallel_client import parallel_computation
#
from threeML.io.uncertainty_formatter import interval_to_errors
# Import optical filters
# from threeML.plugins.photometry.filter_factory import threeML_filter_library
# import time series builder, soon to replace the Fermi plugins
from threeML.utils.data_builders import *
# Import catalogs
from threeML.catalogs import *
# Import GBM downloader
from threeML.utils.data_download.Fermi_GBM.download_GBM_data import (
download_GBM_trigger_data,
)
# Import LLE downloader
from threeML.utils.data_download.Fermi_LAT.download_LLE_data import (
download_LLE_trigger_data,
)
# Now read the configuration and make it available as threeML_config
from .config.config import threeML_config
import astropy.units as u
import os
# Import the LAT data downloader
from threeML.utils.data_download.Fermi_LAT.download_LAT_data import download_LAT_data
# Import the results loader
from threeML.analysis_results import load_analysis_results
# Import the plot_style context manager and the function to create new styles
from .io.plotting.plot_style import (
plot_style,
create_new_plotting_style,
get_available_plotting_styles,
)
# Check that the number of threads is set to 1 for all multi-thread libraries
# otherwise numpy operations will be way slower than what they could be, since
# we never perform huge numpy operations, we instead perform many of them. In this
# situation, opening threads introduces overhead with no performance gain. This solution
# allows cores to be used for multi-cpu computation with the parallel client
var_to_check = ["OMP_NUM_THREADS", "MKL_NUM_THREADS", "NUMEXPR_NUM_THREADS"]
for var in var_to_check:
num_threads = os.environ.get(var)
if num_threads is not None:
try:
num_threads = int(num_threads)
except ValueError:
custom_warnings.warn(
"Your env. variable %s is not an integer, which doesn't make sense. Set it to 1 "
"for optimum performances." % var,
RuntimeWarning,
)
else:
custom_warnings.warn(
"Env. variable %s is not set. Please set it to 1 for optimal performances in 3ML"
% var,
RuntimeWarning,
)