17 """Methods to generate LAL Time- and FrequencySeries objects in python
25 __author__ =
"Duncan Macleod <duncan.macleod@ligo.org>"
32 lal.I2_TYPE_CODE:
'INT2',
33 lal.I4_TYPE_CODE:
'INT4',
34 lal.I8_TYPE_CODE:
'INT8',
35 lal.U2_TYPE_CODE:
'UINT2',
36 lal.U4_TYPE_CODE:
'UINT4',
37 lal.U8_TYPE_CODE:
'UINT8',
38 lal.S_TYPE_CODE:
'REAL4',
39 lal.D_TYPE_CODE:
'REAL8',
40 lal.C_TYPE_CODE:
'COMPLEX8',
41 lal.Z_TYPE_CODE:
'COMPLEX16',
43 LAL_TYPE_FROM_STR = dict((v, k)
for k, v
in LAL_TYPE_STR.items())
44 LAL_TYPE_STR_REGEX = re.compile(
45 '(?P<dtype>(%s))' % (
'|'.join(LAL_TYPE_FROM_STR.keys())), re.I)
48 LAL_TYPE_FROM_NUMPY = {
49 numpy.int16: lal.I2_TYPE_CODE,
50 numpy.int32: lal.I4_TYPE_CODE,
51 numpy.int64: lal.I8_TYPE_CODE,
52 numpy.uint16: lal.U2_TYPE_CODE,
53 numpy.uint32: lal.U4_TYPE_CODE,
54 numpy.uint64: lal.U8_TYPE_CODE,
55 numpy.float32: lal.S_TYPE_CODE,
56 numpy.float64: lal.D_TYPE_CODE,
57 numpy.complex64: lal.C_TYPE_CODE,
58 numpy.complex128: lal.Z_TYPE_CODE,
60 NUMPY_TYPE_FROM_LAL = dict((v, k)
for k, v
in LAL_TYPE_FROM_NUMPY.items())
64 SERIES_OPERATIONS = [
'create',
'destroy',
'cut',
'resize',
'shrink',
'add']
65 SERIES_TYPES = [
'Time',
'Frequency']
66 STRUCT_TYPES = [
'Sequence',
'Vector']
68 SERIES_REGEX = re.compile(
69 r'%s(?P<stype>(%s))Series\Z'
70 % (LAL_TYPE_STR_REGEX.pattern,
'|'.join(SERIES_TYPES)), re.I)
71 ARRAY_REGEX = re.compile(
72 r'%sArray(?:(?P<dir>(L|V))?)' % LAL_TYPE_STR_REGEX.pattern, re.I)
73 STRUCT_REGEX = re.compile(
74 r'%s(?P<struct>(%s))\Z'
75 % (LAL_TYPE_STR_REGEX.pattern,
'|'.join(STRUCT_TYPES)), re.I)
82 """Returns the LAL function to perform the given operation for the
87 >>> create = func_factory('create', 'real8timeseries')
89 lal.CreateREAL8TimeSeries
90 >>> ts = create(name, epoch, f0, deltaT, sampleUnits, length)
91 >>> func_factory('resize', ts)
92 lal.ResizeREAL8TimeSeries
96 SERIES_OPERATIONS.index(operation.lower())
97 except ValueError
as e:
98 e.args(
"Operation '%s' not understood for LAL series. "
99 "Please select one of: %s"
100 % (operation,
", ".join(SERIES_OPERATIONS)),)
104 return getattr(lal,
''.join([operation.title(), struct]))
108 """Format a structure name into the understood type for LAL
112 >>> get_struct_name('real8timeseries')
116 if isinstance(series, str):
119 typestr = type(series).__name__
123 match = SERIES_REGEX.match(typestr).groupdict()
124 except AttributeError:
127 return '%s%sSeries' % (match[
'dtype'].upper(), match[
'stype'].title())
131 match = ARRAY_REGEX.match(typestr).groupdict()
132 except AttributeError:
135 return '%sArray%s' % (match[
'dtype'].upper(),
136 match[
'dir']
and match[
'dir'].upper()
or '')
140 match = STRUCT_REGEX.match(typestr).groupdict()
141 except AttributeError:
143 "Input %s cannot be parsed into LAL struct name" % series)
145 return '%s%s' % (match[
'dtype'].upper(), match[
'struct'].title())
149 """Find the LAL type enum for this series
152 a LAL series object (e.g. REAL8TimeSeries)
154 @returns the LAL type enum (integer) for the series
157 match = STRUCT_REGEX.match(type(series).__name__).groupdict()
158 except AttributeError:
159 raise ValueError(
"Data type for series type %r unknown."
160 % type(series).__name__)
166 """Return the LAL type str for the given `datatype`
169 a dtype representation, normally a string, or a python/numpy type
172 @returns the LAL type str for the given datatype
176 >>> get_lal_type_str('uint32')
178 >>> get_lal_type_str(float)
185 """Return the LAL type enum for the given `datatype`
188 a dtype representation, normally a string, or a python/numpy type
191 @returns the LAL type enum (integer) for the given datatype
195 >>> get_lal_type('uint32')
197 >>> get_lal_type(float)
202 LAL_TYPE_STR[datatype]
209 return LAL_TYPE_FROM_STR[datatype]
214 if re.match(
r'real(4|8)\Z', str(datatype), re.I):
215 return LAL_TYPE_FROM_STR[datatype.upper()]
218 dtype = numpy.dtype(datatype).type
223 return LAL_TYPE_FROM_NUMPY[dtype]
224 except KeyError
as e:
225 e.args = (
'LAL has no support for numpy.%s' % dtype.__name__,)
227 raise ValueError(
"Cannot interpret datatype %r" % datatype)
231 """Return the numpy type for the given `datatype`
234 a dtype representation, normally a LAL type enum (int),
235 a LAL type string, or a python/numpy type object
237 @returns the numpy type corresponding to the given datatype
241 >>> get_numpy_type(float)
243 >>> get_numpy_type('REAL8')
245 >>> get_numpy_type(11)
250 except KeyError
as e:
251 e.args(
'numpy has no support for %s'
258 Duplicate a TimeSeries or FrequencySeries.
262 series : [ TimeSeries | FrequencySeries ]
263 input series to duplicate
266 stype = series.__class__.__name__
267 if stype.endswith(
'FrequencySeries'):
268 out = create(series.name, series.epoch, series.f0, series.deltaF,
269 series.sampleUnits, series.data.length)
270 elif stype.endswith(
'TimeSeries'):
271 out = create(series.name, series.epoch, series.f0, series.deltaT,
272 series.sampleUnits, series.data.length)
274 raise NotImplementedError(
"A duplicator for the %s has not been "
275 "implemented: here's your chance!" % series)
276 out.data.data = series.data.data
def get_series_type(series)
Find the LAL type enum for this series.
def get_numpy_type(datatype)
Return the numpy type for the given datatype
def get_lal_type_str(datatype)
Return the LAL type str for the given datatype
def func_factory(operation, dtype)
Returns the LAL function to perform the given operation for the relevant data type.
def get_lal_type(datatype)
Return the LAL type enum for the given datatype
def duplicate(series)
Duplicate a TimeSeries or FrequencySeries.
def get_struct_name(series)
Format a structure name into the understood type for LAL.