LAL  7.5.0.1-bede9b2
__init__.py
Go to the documentation of this file.
1 # Import SWIG wrappings, if available
2 from .lal import *
3 
4 # Redirect standard output/error when running under IPython
5 from contextlib import contextmanager
6 try:
7  get_ipython()
8 except:
9  @contextmanager
11  yield
12 else:
13  lal.swig_redirect_standard_output_error(True)
14  @contextmanager
16  state = lal.swig_redirect_standard_output_error(False)
17  yield
18  lal.swig_redirect_standard_output_error(state)
19  import warnings
20  warnings.warn("""Wswiglal-redir-stdio:
21 
22 SWIGLAL standard output/error redirection is enabled in IPython.
23 This may lead to performance penalties. To disable locally, use:
24 
25 with lal.no_swig_redirect_standard_output_error():
26  ...
27 
28 To disable globally, use:
29 
30 lal.swig_redirect_standard_output_error(False)
31 
32 Note however that this will likely lead to error messages from
33 LAL functions being either misdirected or lost when called from
34 Jupyter notebooks.
35 
36 To suppress this warning, use:
37 
38 import warnings
39 warnings.filterwarnings("ignore", "Wswiglal-redir-stdio")
40 import lal
41 """, stacklevel=2)
42 
43 __version__ = "7.5.0.1"
44 
45 ## \addtogroup lal_python
46 """This package provides Python wrappings and extensions to LAL"""
47 
48 #
49 # =============================================================================
50 #
51 # CachedDetectors Look-up Tables
52 #
53 # =============================================================================
54 #
55 
56 
57 cached_detector_by_prefix = dict((cd.frDetector.prefix, cd) for cd in CachedDetectors)
58 # make sure there were no duplicates
59 assert len(cached_detector_by_prefix) == len(CachedDetectors)
60 
61 
62 cached_detector_by_name = dict((cd.frDetector.name, cd) for cd in CachedDetectors)
63 # make sure there were no duplicates
64 assert len(cached_detector_by_name) == len(CachedDetectors)
65 
66 
67 name_to_prefix = dict((name, detector.frDetector.prefix) for name, detector in cached_detector_by_name.items())
68 prefix_to_name = dict((prefix, name) for name, prefix in name_to_prefix.items())
69 
70 #
71 # =============================================================================
72 #
73 # Make common LAL datatypes picklable
74 #
75 # =============================================================================
76 #
77 
78 
79 import copyreg
80 
81 numpy_to_lal_types = {'char': 'CHAR',
82  'int16': 'INT2',
83  'int32': 'INT4',
84  'int64': 'INT8',
85  'uint16': 'UINT2',
86  'uint32': 'UINT4',
87  'uint64': 'UINT8',
88  'float32': 'REAL4',
89  'float64': 'REAL8',
90  'complex64': 'COMPLEX8',
91  'complex128': 'COMPLEX16'}
92 
93 
94 def pickle_gps(obj):
95  return LIGOTimeGPS, (obj.gpsSeconds, obj.gpsNanoSeconds)
96 
97 
98 def pickle_unit(obj):
99  return Unit, (str(obj),)
100 
101 
102 def unpickle_vector(data):
103  lal_type = numpy_to_lal_types[data.dtype.name]
104  creator = globals()['Create{}Vector'.format(lal_type)]
105  result = creator(len(data))
106  result.data = data
107  return result
108 
109 
110 def pickle_vector(obj):
111  return unpickle_vector, (obj.data,)
112 
113 
114 def unpickle_series(attrs):
115  lal_type = numpy_to_lal_types[attrs['data'].data.dtype.name]
116  kind = 'Frequency' if 'deltaF' in attrs else 'Time'
117  creator = globals()['{}{}Series'.format(lal_type, kind)]
118  result = creator()
119  for key, value in attrs.items():
120  setattr(result, key, value)
121  return result
122 
123 
124 def pickle_series(obj):
125  attrs = {'name': obj.name, 'epoch': obj.epoch, 'f0': obj.f0,
126  'sampleUnits': obj.sampleUnits, 'data': obj.data}
127  if hasattr(obj, 'deltaF'):
128  attrs['deltaF'] = obj.deltaF
129  else:
130  attrs['deltaT'] = obj.deltaT
131  return unpickle_series, (attrs,)
132 
133 
134 copyreg.pickle(LIGOTimeGPS, pickle_gps)
135 copyreg.pickle(Unit, pickle_unit)
136 for datatype in numpy_to_lal_types.values():
137  clazz = globals().get('{}Vector'.format(datatype))
138  if clazz:
139  copyreg.pickle(clazz, pickle_vector)
140  clazz = globals().get('{}FrequencySeries'.format(datatype))
141  if clazz:
142  copyreg.pickle(clazz, pickle_series)
143  clazz = globals().get('{}TimeSeries'.format(datatype))
144  if clazz:
145  copyreg.pickle(clazz, pickle_series)
def no_swig_redirect_standard_output_error()
Definition: __init__.py:10
def pickle_unit(obj)
Definition: __init__.py:98
def pickle_series(obj)
Definition: __init__.py:124
def unpickle_vector(data)
Definition: __init__.py:102
def pickle_gps(obj)
Definition: __init__.py:94
def unpickle_series(attrs)
Definition: __init__.py:114
def pickle_vector(obj)
Definition: __init__.py:110