Skip to content

Commit 7e80dd8

Browse files
Merge pull request #31 from andrewpaulreeves/slopecovariance_bugfix
Slopecovariance bugfix
2 parents 3dc2030 + 55ad761 commit 7e80dd8

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed

aotools/turbulence/slopecovariance.py

+6-28
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def make_covariance_matrix(self):
139139

140140
self._make_covariance_matrix_mp(self.threads)
141141

142-
self.covariance_matrix = mirror_covariance_matrix(self.covariance_matrix, self.n_subaps)
142+
self.covariance_matrix = mirror_covariance_matrix(self.covariance_matrix)
143143

144144
return self.covariance_matrix
145145

@@ -184,7 +184,7 @@ def _make_covariance_matrix(self):
184184
self.covariance_matrix[
185185
cov_mat_coord_x1: cov_mat_coord_x2,
186186
cov_mat_coord_y1 + self.n_subaps[wfs_j]: cov_mat_coord_y2 + self.n_subaps[wfs_j]
187-
] += cov_xy.T * r0_scale
187+
] += numpy.fliplr(numpy.flipud(cov_xy)) * r0_scale
188188
self.covariance_matrix[
189189
cov_mat_coord_x1 + self.n_subaps[wfs_i]: cov_mat_coord_x2 + self.n_subaps[wfs_i],
190190
cov_mat_coord_y1 + self.n_subaps[wfs_j]: cov_mat_coord_y2 + self.n_subaps[wfs_j]
@@ -239,7 +239,7 @@ def _make_covariance_matrix_mp(self, threads):
239239
self.covariance_matrix[
240240
cov_mat_coord_x1: cov_mat_coord_x2,
241241
cov_mat_coord_y1 + self.n_subaps[wfs_j]: cov_mat_coord_y2 + self.n_subaps[wfs_j]
242-
] += cov_xy.T * r0_scale
242+
] += numpy.fliplr(numpy.flipud(cov_xy)) * r0_scale
243243
self.covariance_matrix[
244244
cov_mat_coord_x1 + self.n_subaps[wfs_i]: cov_mat_coord_x2 + self.n_subaps[wfs_i],
245245
cov_mat_coord_y1 + self.n_subaps[wfs_j]: cov_mat_coord_y2 + self.n_subaps[wfs_j]
@@ -462,38 +462,16 @@ def calculate_structure_function(phase, nbOfPoint=None, step=None):
462462
return sf_x
463463

464464

465-
def mirror_covariance_matrix(cov_mat, n_subaps):
465+
def mirror_covariance_matrix(cov_mat):
466466
"""
467467
Mirrors a covariance matrix around the axis of the diagonal.
468468
469469
Parameters:
470470
cov_mat (ndarray): The covariance matrix to mirror
471471
n_subaps (ndarray): Number of sub-aperture in each WFS
472472
"""
473-
total_slopes = cov_mat.shape[0]
474-
n_wfs = n_subaps.shape[0]
475-
476-
n1 = 0
477-
for n in range(n_wfs):
478-
m1 = 0
479-
for m in range(n + 1):
480-
if n != m:
481-
n2 = n1 + 2 * n_subaps[n]
482-
m2 = m1 + 2 * n_subaps[m]
483-
484-
nn1 = total_slopes - 2 * n_subaps[n] - n1
485-
nn2 = nn1 + 2 * n_subaps[n]
486-
487-
mm1 = total_slopes - 2 * n_subaps[m] - m1
488-
mm2 = mm1 + 2 * n_subaps[m]
489-
490-
cov_mat[nn1: nn2, mm1: mm2] = (
491-
numpy.swapaxes(cov_mat[n1: n2, m1: m2], 1, 0)
492-
)
493-
494-
m1 += 2 * n_subaps[m]
495-
n1 += 2 * n_subaps[n]
496-
return cov_mat
473+
474+
return numpy.bitwise_or(cov_mat.view("int32"), cov_mat.T.view("int32")).view("float32")
497475

498476
def create_tomographic_covariance_reconstructor(covariance_matrix, n_onaxis_subaps, svd_conditioning=0):
499477
"""

test/test_slopecovariance.py

+36
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def test_slopecovmat_makecovmat():
5252
asterism_radius = 10
5353

5454
subap_diameters = [telescope_diameter / nx_subaps] * n_wfs
55+
5556
pupil_masks = [aotools.circle(nx_subaps / 2., nx_subaps)] * n_wfs
5657
gs_altitudes = [90000] * n_wfs
5758
gs_positions = [
@@ -73,6 +74,41 @@ def test_slopecovmat_makecovmat():
7374
covariance_matrix = cov_mat.make_covariance_matrix()
7475

7576

77+
def test_slopecovmat_makecovmat_uneven():
78+
threads = 1
79+
80+
n_wfs = 3
81+
telescope_diameter = 8.
82+
nx_subaps = 10
83+
84+
n_layers = 3
85+
layer_altitudes = numpy.linspace(0, 20000, n_layers)
86+
layer_r0s = [1] * n_layers
87+
layer_L0s = [25.] * n_layers
88+
89+
asterism_radius = 10
90+
# What if all WFSs don't have the same number of subaps?
91+
pupil_masks = [aotools.circle(4.5, nx_subaps), aotools.circle(nx_subaps / 2., nx_subaps), aotools.circle(nx_subaps / 2., nx_subaps)]
92+
subap_diameters = [telescope_diameter / nx_subaps] * n_wfs
93+
gs_altitudes = [90000] * n_wfs
94+
gs_positions = [
95+
[asterism_radius, 0],
96+
[numpy.sin(numpy.pi / 3.) * asterism_radius, numpy.cos(numpy.pi / 3.) * asterism_radius],
97+
[numpy.sin(numpy.pi / 3.) * asterism_radius, -numpy.cos(numpy.pi / 3.) * asterism_radius],
98+
[-numpy.sin(numpy.pi / 3.) * asterism_radius, numpy.cos(numpy.pi / 3.) * asterism_radius],
99+
[-numpy.sin(numpy.pi / 3.) * asterism_radius, -numpy.cos(numpy.pi / 3.) * asterism_radius],
100+
[-asterism_radius, 0]]
101+
wfs_magnifications = [1.] * n_wfs
102+
pupil_offsets = [[0, 0]] * n_wfs
103+
wfs_rotations = [0] * n_wfs
104+
wfs_wavelengths = [550e-9] * n_wfs
105+
106+
cov_mat = aotools.CovarianceMatrix(n_wfs, pupil_masks, telescope_diameter, subap_diameters, gs_altitudes, gs_positions,
107+
wfs_wavelengths,
108+
n_layers, layer_altitudes, layer_r0s, layer_L0s, threads)
109+
110+
covariance_matrix = cov_mat.make_covariance_matrix()
111+
76112
def test_covtomorecon():
77113
threads = 1
78114

0 commit comments

Comments
 (0)