Repository navigation
Expand file tree
/
Copy pathpolarisation.py
More file actions
289 lines (214 loc) · 9.1 KB
/
Copy pathpolarisation.py
File metadata and controls
289 lines (214 loc) · 9.1 KB
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
""" Functions for defining polarisation conventions. These include definitions via classes and
conversion functions.
For example::
stokes = numpy.array(random.uniform(-1.0, 1.0, [3, 4, 128, 128]))
ipf = PolarisationFrame('stokesIQUV')
opf = PolarisationFrame('circular')
cir = convert_pol_frame(stokes, ipf, opf)
st = convert_pol_frame(cir, opf, ipf)
or::
stokes = numpy.array([1, 0.5, 0.2, -0.1])
circular = convert_stokes_to_circular(stokes)
These function operate on Numpy arrays. These are packaged for use in Images. The Image functions are
probably more useful.
"""
import numpy
import logging
log = logging.getLogger(__name__)
class ReceptorFrame:
"""Define polarisation frames for receptors
circular, linear, and stokesI. The latter is non-physical but useful for some types of testing.
"""
rec_frames = {
'circular': {'R': 0, 'L': 1},
'linear': {'X': 0, 'Y': 1},
'stokesI': {'I': 0}
}
def __init__(self, name):
""" create ReceptorFrame
:param name:
"""
if name in self.rec_frames.keys():
self.type = name
self.translations = self.rec_frames[name]
else:
raise ValueError("Unknown receptor frame %s" % str(name))
@property
def nrec(self):
""" Number of receptors (should be 2)
"""
return len(list(self.translations.keys()))
def valid(self, name):
return name in self.rec_frames.keys()
def __eq__(self, a):
return self.type == a.type
class PolarisationFrame:
""" Define polarisation frames post correlation
"""
fits_codes = {
'circular': [-1, -2, -3, -4],
'circularnp': [-1, -4],
'linear': [-5, -6, -7, -8],
'linearnp': [-5, -8],
'stokesIQUV': [1, 2, 3, 4],
'stokesIV': [1, 4],
'stokesIQ': [1, 2],
'stokesI': [1]
}
polarisation_frames = {
'circular': {'RR': 0, 'RL': 1, 'LR': 2, 'LL': 3},
'circularnp': {'RR': 0, 'LL': 1},
'linear': {'XX': 0, 'XY': 1, 'YX': 2, 'YY': 3},
'linearnp': {'XX': 0, 'YY': 1},
'stokesIQUV': {'I': 0, 'Q': 1, 'U': 2, 'V': 3},
'stokesIV': {'I': 0, 'V': 1},
'stokesIQ': {'I': 0, 'Q': 1},
'stokesI': {'I': 0}
}
def __init__(self, name):
""" create PolarisationFrame
:param name:
"""
if name in self.polarisation_frames.keys():
self.type = name
self.translations = self.polarisation_frames[name]
else:
raise ValueError("Unknown polarisation frame %s" % str(name))
def __eq__(self, a):
if a is None:
return False
return self.type == a.type
@property
def npol(self):
""" Number of correlated polarisations
"""
return len(list(self.translations.keys()))
def polmatrixmultiply(cm, vec, polaxis=1):
"""Matrix multiply of appropriate axis of vec [...,:] by cm
For an image vec has axes [nchan, npol, ny, nx] and polaxis=1
For visibility vec has axes [row, nchan, npol] and polaxis=2
:param cm: matrix to apply
:param vec: array to be multiplied [...,:]
:param polaxis: which axis contains the polarisation
:return: multiplied vec
"""
if len(vec.shape) == 1:
return numpy.dot(cm, vec)
else:
# This tensor swaps the first two axes so we need to tranpose back
result = numpy.tensordot(cm, vec, axes=(1, polaxis))
permut = list(range(len(result.shape)))
permut[0], permut[polaxis] = permut[polaxis], permut[0]
return numpy.transpose(result, axes=permut)
def convert_stokes_to_linear(stokes, polaxis=1):
""" Convert Stokes IQUV to Linear
:param stokes: [...,4] Stokes vector in I,Q,U,V (can be complex)
:param polaxis: Axis of stokes with polarisation (default 1)
:return: linear vector in XX, XY, YX, YY sequence
Equation 4.58 TMS
"""
conversion_matrix = numpy.array([[1, 1, 0, 0],
[0, 0, 1, 1j],
[0, 0, 1, -1j],
[1, -1, 0, 0]])
return polmatrixmultiply(conversion_matrix, stokes, polaxis)
def convert_linear_to_stokes(linear, polaxis=1):
""" Convert Linear to Stokes IQUV
:param linear: [...,4] linear vector in XX, XY, YX, YY sequence
:param polaxis: Axis of linear with polarisation (default 1)
:return: Complex I,Q,U,V
Equation 4.58 TMS, inverted with numpy.linalg.inv
"""
conversion_matrix = numpy.array([[0.5 + 0.j, 0.0 + 0.j, 0.0 + 0.j, 0.5 + 0.j],
[0.5 + 0.j, 0.0 + 0.j, 0.0 + 0.j, -0.5 - 0.j],
[0.0 + 0.j, 0.5 + 0.j, 0.5 + 0.j, 0.0 + 0.j],
[0.0 + 0.j, 0.0 - 0.5j, 0.0 + 0.5j, 0.0 + 0.j]])
return polmatrixmultiply(conversion_matrix, linear, polaxis)
def convert_linear_to_stokesI(linear, polaxis=1):
""" Convert Linear to Stokes I
:param linear: [...,4] linear vector in XX, XY, YX, YY sequence
:param polaxis: Axis of linear with polarisation (default 1)
:return: Complex I
Equation 4.58 TMS, inverted with numpy.linalg.inv
"""
return 0.5 * (linear[...,0]+linear[...,3])[..., numpy.newaxis]
def convert_stokes_to_circular(stokes, polaxis=1):
""" Convert Stokes IQUV to Circular
:param stokes: [...,4] Stokes vector in I,Q,U,V (can be complex)
:param polaxis: Axis of stokes with polarisation (default 1)
:return: circular vector in RR, RL, LR, LL sequence
Equation 4.59 TMS
"""
conversion_matrix = numpy.array([[1, 0, 0, 1],
[0, -1j, 1, 0],
[0, -1j, -1, 0],
[1, 0, 0, -1]])
return polmatrixmultiply(conversion_matrix, stokes, polaxis)
def convert_circular_to_stokes(circular, polaxis=1):
""" Convert Circular to Stokes IQUV
:param circular: [...,4] linear vector in RR, RL, LR, LL sequence
:param polaxis: Axis of circular with polarisation (default 1)
:return: Complex I,Q,U,V
Equation 4.58 TMS, inverted with numpy.linalg.inv
"""
conversion_matrix = numpy.array([[0.5 + 0.j, 0.0 + 0.j, 0.0 + 0.j, 0.5 + 0.j],
[0.0 + 0.j, -0.0 + 0.5j, -0.0 + 0.5j, 0.0 + 0.j],
[0.0 + 0.j, 0.5 + 0.j, -0.5 - 0.j, 0.0 + 0.j],
[0.5 + 0.j, 0.0 + 0.j, 0.0 + 0.j, -0.5 - 0.j]])
return polmatrixmultiply(conversion_matrix, circular, polaxis)
def convert_circular_to_stokesI(circular, polaxis=1):
""" Convert Circular to Stokes I
:param circular: [...,4] linear vector in RR, RL, LR, LL sequence
:param polaxis: Axis of circular with polarisation (default 1)
:return: Complex I
Equation 4.58 TMS, inverted with numpy.linalg.inv
"""
return 0.5 * (circular[...,0] + circular[...,3])[..., numpy.newaxis]
def convert_pol_frame(polvec, ipf: PolarisationFrame, opf: PolarisationFrame, polaxis=1):
if ipf == opf:
return polvec
if ipf == PolarisationFrame("linear"):
if opf == PolarisationFrame("stokesIQUV"):
return convert_linear_to_stokes(polvec, polaxis)
else:
raise ValueError("Unknown polarisation conversion")
if ipf == PolarisationFrame("circular"):
if opf == PolarisationFrame("stokesIQUV"):
return convert_circular_to_stokes(polvec, polaxis)
else:
raise ValueError("Unknown polarisation conversion")
if ipf == PolarisationFrame("stokesIQUV"):
if opf == PolarisationFrame("linear"):
return convert_stokes_to_linear(polvec, polaxis)
elif opf == PolarisationFrame("circular"):
return convert_stokes_to_circular(polvec, polaxis)
else:
raise ValueError("Unknown polarisation conversion")
if ipf == PolarisationFrame("stokesI"):
if opf == PolarisationFrame("stokesI"):
return polvec
raise ValueError("Unknown polarisation conversion")
def correlate_polarisation(rec_frame: ReceptorFrame):
""" Gives the polarisation frame corresponding to a receptor frame
:param rec_frame: Receptor frame
:return: PolarisationFrame
"""
if rec_frame == ReceptorFrame("circular"):
correlation = PolarisationFrame("circular")
elif rec_frame == ReceptorFrame("linear"):
correlation = PolarisationFrame("linear")
elif rec_frame == ReceptorFrame("stokesI"):
correlation = PolarisationFrame("stokesI")
else:
raise ValueError("Unknown receptor frame %s for correlation" % rec_frame)
return correlation
def congruent_polarisation(rec_frame: ReceptorFrame, polarisation_frame: PolarisationFrame):
"""Are these receptor and polarisation frames congruent?
"""
if rec_frame.type == "linear":
return polarisation_frame.type in ["linear", "linearnp"]
elif rec_frame.type == "circular":
return polarisation_frame.type in ["circular", "circularnp"]
elif rec_frame.type == "stokesI":
return polarisation_frame.type == "stokesI"
return False