51 sh = shape[0], a.shape[0] // shape[0], shape[1], a.shape[1] // shape[1]
52 return np.mean(a.reshape(sh), axis=(1, 3))
56 """find best size for a fft from size s
62 return 2**(int(np.log2(s)) + 1)
66 def bin2d(data_in, binfact):
68 Returns the input 2D array "array", binned with the binning factor "binfact".
69 The input array X and/or Y dimensions needs not to be a multiple of
70 "binfact"; The final/edge pixels are in effect replicated if needed.
71 This routine prepares the parameters and calls the C routine _bin2d.
72 The input array can be of type int, float or double.
73 Last modified: Dec 15, 2003.
79 data_in: (np.ndarray) : data to binned
81 binfact: (int) : binning factor
84 raise ValueError(
"binfact has to be >= 1")
88 fx = int(np.ceil(nx / float(binfact)))
89 fy = int(np.ceil(ny / float(binfact)))
91 data_out = np.zeros((fx, fy), dtype=data_in.dtype)
95 for i2
in range(binfact):
96 for j2
in range(binfact):
103 data_out[i1, j1] += data_in[i, j]
117 padded = np.zeros((N, N))
118 padded[D1:D1 + S[0], D2:D2 + S[1]] = A
122 def dist(dim, xc=-1, yc=-1):
133 dx = np.tile(np.arange(dim) - xc, (dim, 1))
134 dy = np.tile(np.arange(dim) - yc, (dim, 1)).T
136 d = np.sqrt(dx**2 + dy**2)
142 Returns a centered gaussian of specified size and fwhm.
143 norm returns normalized 2d gaussian
147 :param fwhm: (float) :
149 :param xc: (float) : (optional) center position on x axis
151 :param yc: (float) : (optional) center position on y axis
153 :param norm: (int) : (optional) normalization
155 tmp = np.exp(-(
dist(size, xc, yc) / (fwhm / 1.66))**2.)
157 tmp = tmp / (fwhm**2. * 1.140075)
162 """ Generate modulation points positions following a square pattern
165 radius : (float) : half the length of a side in lambda/D
167 density : (float), optional) : number of psf per lambda/D. Default is 1
170 cx : (np.ndarray) : X-positions of the modulation points
172 cy : (np.ndarray) : Y-positions of the modulation points
174 x = np.linspace(-radius, radius, 1 + 2 * int(radius * density))
175 cx, cy = np.meshgrid(x, x, indexing=
'ij')
182 """ Generate modulation points positions following a circular pattern
185 radius : (float) : half the length of a side in lambda/D
187 density : (float), optional) : number of psf per lambda/D. Default is 1
190 cx : (np.ndarray) : X-positions of the modulation points
192 cy : (np.ndarray) : Y-positions of the modulation points
195 r = cx * cx + cy * cy <= radius**2
196 return (cx[r], cy[r])
199 """ Used to generate a pseudo source for PYRWFS
202 radius : (float) : TODO description
204 additional_psf : (int) :TODO description
206 density : (float, optional) :TODO description
209 ox : TODO description & explicit naming
211 oy : TODO description & explicit naming
213 w : TODO description & explicit naming
215 xc : TODO description & explicit naming
217 yc : TODO description & explicit naming
219 struct_size = (1 + 2 * additional_psf)**2
221 center_weight = (1 + 2 * int(additional_psf * density))**2 * [1]
222 center_size = 1 + 2 * int(additional_psf * density)
224 weight_edge = [(1 + 2 * int(radius * density) - center_size) // 2]
226 for k
in range(additional_psf):
227 line_length = np.sum(yc == (k + 1))
229 weight_edge.append((line_length - center_size) // 2)
231 edge_dist = (radius + additional_psf) // 2
237 V_edge_y.append(m * edge_dist)
238 V_edge_weight.append(weight_edge[0])
239 for k, val
in enumerate(weight_edge[1:]):
242 V_edge_x.append(l * (k + 1) * density)
243 V_edge_y.append(m * edge_dist)
244 V_edge_weight.append(val)
249 H_edge_x.append(m * edge_dist)
251 H_edge_weight.append(weight_edge[0])
252 for k, val
in enumerate(weight_edge[1:]):
255 H_edge_x.append(m * edge_dist)
256 H_edge_y.append(l * (k + 1) * density)
257 H_edge_weight.append(val)
260 pup_cent_weight = 4 * [(len(xc) - 2 * np.sum(H_edge_weight) - struct_size) / 4]
261 pup_cent_dist = int(edge_dist // np.sqrt(2))
264 pup_cent_x.append(l * pup_cent_dist)
265 pup_cent_y.append(m * pup_cent_dist)
266 ox = np.concatenate((center_x, V_edge_x, H_edge_x, pup_cent_x))
267 oy = np.concatenate((center_y, V_edge_y, H_edge_y, pup_cent_y))
268 w = np.concatenate((center_weight, V_edge_weight, H_edge_weight,
270 return (ox, oy, w, xc, yc)
273 def first_non_zero(array: np.ndarray, axis: int, invalid_val: int = -1) -> np.ndarray:
274 """ Find the first non zero element of an array
277 array : (np.ndarray) : input array
279 axis : (int) : axis index
281 invalid_val : (int, optional) : Default is -1
284 non_zeros_pos : (np.ndarray) : Index of the first non-zero element
285 for each line or column following the axis
288 return np.where(mask.any(axis=axis), mask.argmax(axis=axis), invalid_val)
def generate_square(float radius, float density=1.)
Generate modulation points positions following a square pattern.
def bin2d(data_in, binfact)
Returns the input 2D array "array", binned with the binning factor "binfact".
np.ndarray first_non_zero(np.ndarray array, int axis, int invalid_val=-1)
Find the first non zero element of an array.
def dist(dim, xc=-1, yc=-1)
TODO docstring.
def pad_array(A, N)
TODO docstring.
def generate_pseudo_source(float radius, additional_psf=0, density=1.)
Used to generate a pseudo source for PYRWFS.
def fft_goodsize(s)
find best size for a fft from size s
def makegaussian(size, fwhm, xc=-1, yc=-1, norm=0)
Returns a centered gaussian of specified size and fwhm.
def generate_circle(float radius, float density=1.)
Generate modulation points positions following a circular pattern s.
def rebin(a, shape)
TODO docstring.