Skip to content

Commit e1f6017

Browse files
committed
improved type annotations and doc strings
1 parent bb0e422 commit e1f6017

14 files changed

+94
-78
lines changed

Diff for: mcsim/analysis/camera_noise.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def _compute(arr):
154154
def get_gain_map(means: np.ndarray,
155155
vars: np.ndarray,
156156
vars_sd: Optional[np.ndarray] = None,
157-
max_mean: float = np.inf) -> (np.ndarray, np.ndarray, np.ndarray):
157+
max_mean: float = np.inf) -> tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
158158
"""
159159
Compute camera pixel gains by finding the best-fit line describing the variance as a function of the mean
160160
@@ -193,14 +193,15 @@ def plot_camera_noise_results(gains: np.ndarray,
193193
means_err: Optional[np.ndarray] = None,
194194
vars_err: Optional[np.ndarray] = None,
195195
nbins: int = 600,
196-
**kwargs) -> (list[Figure], list[str]):
196+
**kwargs) -> tuple[list[Figure], list[str]]:
197197
"""
198198
Display gain curve for single pixel vs. illumination data.
199199
Additional keyword arguments are passed through to plt.figure()
200200
201201
:param gains: ny x nx
202202
:param means: nill x ny x nx
203203
:param vars: nill x ny x nx
204+
:param gains_err:
204205
:param gains: estimated uncertainty (standard dev) of each gain value
205206
:param means_err: estimated uncertainty (standard dev) of each mean value
206207
:param vars_err: estimated uncertainty (standard dev) of each variance value

Diff for: mcsim/analysis/dmd_patterns.py

+21-21
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def get_sim_pattern(dmd_size: Sequence[int, int],
4040
vec_b: array,
4141
nphases: int,
4242
phase_index: int,
43-
geometry: str = "orthogonal") -> (array, array):
43+
geometry: str = "orthogonal") -> tuple[array, array]:
4444
"""
4545
Convenience function for generating SIM patterns from the tile_patterns() function.
4646
This function can run either on the CPU or the GPU. If vec_a is a NumPy array, it will
@@ -233,7 +233,7 @@ def double_cell(cell: array,
233233
vec_a: array,
234234
vec_b: array,
235235
na: int = 1,
236-
nb: int = 0) -> (np.ndarray, np.ndarray, np.ndarray):
236+
nb: int = 0) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
237237
"""
238238
Create new unit cell by doubling the original one by a factor of na along vec_a and nb along vec_b
239239
@@ -290,7 +290,7 @@ def double_cell(cell: array,
290290
def get_sim_unit_cell(vec_a: array,
291291
vec_b: array,
292292
nphases: int,
293-
phase_index: int = 0) -> (array, array, array):
293+
phase_index: int = 0) -> tuple[array, array, array]:
294294
"""
295295
Get unit cell, which can be repeated to form SIM pattern.
296296
@@ -339,7 +339,7 @@ def get_sim_unit_cell(vec_a: array,
339339

340340

341341
def get_unit_cell(vec_a: array,
342-
vec_b: array) -> (array, array, array):
342+
vec_b: array) -> tuple[array, array, array]:
343343
"""
344344
Generate a mask which represents one unit cell of a pattern for given vectors.
345345
This mask is a square array with NaNs at positions outside the unit cell, and
@@ -470,7 +470,7 @@ def line(x, p1, p2): return ((p2[1] - p1[1]) * x + p1[1] * p2[0] - p1[0] * p2[1]
470470

471471
def reduce2cell(point: array,
472472
va: array,
473-
vb: array) -> (array, array, array):
473+
vb: array) -> tuple[array, array, array]:
474474
"""
475475
Given a vector, reduce it to coordinates within the unit cell.
476476
@@ -507,7 +507,7 @@ def convert_cell(cell1: array,
507507
va1: array,
508508
vb1: array,
509509
va2: array,
510-
vb2: array) -> (array, array, array):
510+
vb2: array) -> tuple[array, array, array]:
511511
"""
512512
Given a unit cell described by vectors va1 and vb2, convert to equivalent description
513513
in terms of va2, vb2
@@ -550,7 +550,7 @@ def get_minimal_cell(cell: array,
550550
x: array,
551551
y: array,
552552
va: array,
553-
vb: array) -> (array, array, array, array):
553+
vb: array) -> tuple[array, array, array, array, array]:
554554
"""
555555
Convert to cell using the smallest lattice vectors
556556
@@ -636,7 +636,7 @@ def show_cell(v1: np.ndarray,
636636

637637
# determine parameters of SIM patterns
638638
def get_reciprocal_vects(vec_a: array,
639-
vec_b: array) -> (array, array):
639+
vec_b: array) -> tuple[array, array]:
640640
"""
641641
Compute the reciprocal vectors for (real-space) lattice vectors vec_a and vec_b.
642642
If we call the lattice vectors a_i and the reciprocal vectors b_j, then these should be defined such that
@@ -711,7 +711,7 @@ def get_sim_period(vec_a: array,
711711

712712

713713
def get_sim_frqs(vec_a: array,
714-
vec_b: array) -> (float, float):
714+
vec_b: array) -> tuple[float, float]:
715715
"""
716716
Get spatial frequency of SIM pattern constructed from periodicity vectors.
717717
@@ -772,7 +772,7 @@ def get_sim_phase(vec_a: array,
772772

773773

774774
def ldftfreq(vec_a: np.ndarray,
775-
vec_b: np.ndarray) -> (array, array, array):
775+
vec_b: np.ndarray) -> tuple[array, array, array]:
776776
"""
777777
Get the Fourier frequencies for the lattice fourier transform (LDFT). See ldft2() for more details.
778778
This function is an analog of fftfreq().
@@ -862,7 +862,7 @@ def get_pattern_fourier_component(unit_cell: array,
862862
nphases: int = 3,
863863
phase_index: int = 0,
864864
use_fft_origin: bool = True,
865-
dmd_size: Optional[Sequence[int]] = None) -> (np.ndarray, np.ndarray):
865+
dmd_size: Optional[Sequence[int]] = None) -> tuple[np.ndarray, np.ndarray]:
866866
"""
867867
Get fourier component at f = n * recp_vec_a + m * recp_vec_b.
868868
@@ -1038,7 +1038,7 @@ def get_intensity_fourier_components(unit_cell: np.ndarray,
10381038
use_fft_origin: bool = True,
10391039
include_blaze_correction: bool = True,
10401040
dmd_params: Optional[dict] = None) -> \
1041-
(np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray):
1041+
tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
10421042
"""
10431043
Utility function for computing many electric field and intensity components of the Fourier pattern, including the
10441044
effect of the Blaze angle and system numerical aperture
@@ -1501,7 +1501,7 @@ def show_fourier_components(vec_a: np.ndarray,
15011501

15021502
# Lagrange-Gauss basis reduction
15031503
def reduce_basis(va: array,
1504-
vb: array) -> (array, array):
1504+
vb: array) -> tuple[array, array]:
15051505
"""
15061506
Find the "smallest" set of basis vectors using Lagrange-Gauss basis reduction.
15071507
@@ -1544,7 +1544,7 @@ def reduce_basis(va: array,
15441544

15451545

15461546
def reduce_recp_basis(va: array,
1547-
vb: array) -> (array, array):
1547+
vb: array) -> tuple[array, array]:
15481548
"""
15491549
Compute the shortest pair of reciprocal basis vectors. These vectors may not be dual to the lattice vectors
15501550
in the sense that vi * rsj = delta_{ij}, but they do form a basis for the reciprocal lattice vectors.
@@ -1559,7 +1559,7 @@ def reduce_recp_basis(va: array,
15591559

15601560
def get_closest_lattice_vec(point: np.ndarray,
15611561
va: np.ndarray,
1562-
vb: np.ndarray) -> (np.ndarray, int, int):
1562+
vb: np.ndarray) -> tuple[np.ndarray, int, int]:
15631563
"""
15641564
Find the closest lattice vector to point
15651565
@@ -1610,7 +1610,7 @@ def get_closest_lattice_vec(point: np.ndarray,
16101610

16111611
def get_closest_recip_vec(recp_point: np.ndarray,
16121612
va: np.ndarray,
1613-
vb: np.ndarray) -> (np.ndarray, int, int):
1613+
vb: np.ndarray) -> tuple[np.ndarray, int, int]:
16141614
"""
16151615
Find the closest reciprocal lattive vector, f = na * rva + nb * rvb, to a given point in reciprocal space,
16161616
recp_point.
@@ -1819,7 +1819,7 @@ def find_closest_multicolor_set(period: float,
18191819
angle_sep_tol: float = 5*np.pi/180,
18201820
max_solutions_to_search: int = 20,
18211821
pitch: float = 7560.,
1822-
minimize_leakage: bool = True) -> (np.ndarray, np.ndarray):
1822+
minimize_leakage: bool = True) -> tuple[np.ndarray, np.ndarray]:
18231823
"""
18241824
Generate set of SIM patterns for multiple colors with period close to specified value and maximizing distance
18251825
between angles. The patterns are determined such that the diffracted orders will pass through the same positions
@@ -2173,7 +2173,7 @@ def find_rational_approx_angle(angle: float,
21732173

21742174
def find_allowed_periods(angle: float,
21752175
nphases: int,
2176-
nmax: int) -> (list[float], list[int], list):
2176+
nmax: int) -> tuple[array, array, array]:
21772177
"""
21782178
Given a DMD pattern with fixed angle, get allowed pattern periods which allow perfect phase shifting for nphases
21792179
@@ -2295,7 +2295,7 @@ def vects2pattern_data(dmd_size: Sequence[int, int],
22952295
invert: bool = False,
22962296
pitch: float = 7560,
22972297
generate_patterns: bool = True,
2298-
geometry: str = "orthogonal") -> (np.ndarray, dict):
2298+
geometry: str = "orthogonal") -> tuple[np.ndarray, dict]:
22992299
"""
23002300
Generate pattern and useful data (angles, phases, frequencies, reciprocal vectors, ...) from the lattice
23012301
vectors for a given pattern set.
@@ -2504,7 +2504,7 @@ def export_pattern_set(dmd_size: Sequence[int, int],
25042504
wavelength: float = 1.,
25052505
save_dir: Union[str, Path] = 'sim_patterns',
25062506
plot_results: bool = False,
2507-
geometry: str = "orthogonal") -> (np.ndarray, dict, Figure):
2507+
geometry: str = "orthogonal") -> tuple[np.ndarray, dict, Figure]:
25082508
"""
25092509
Export a single set of SIM patterns, i.e. single wavelength, single period
25102510
@@ -2854,7 +2854,7 @@ def export_otf_test_set(dmd_size: Sequence[int],
28542854
bvec_max_size: int = 40,
28552855
phase_index: int = 0,
28562856
save_dir: Optional[Union[str, Path]] = None,
2857-
verbose: bool = True) -> (np.ndarray, dict):
2857+
verbose: bool = True) -> tuple[np.ndarray, dict]:
28582858
"""
28592859
Generate many SIM-like patterns at a variety of different periods and angles
28602860

Diff for: mcsim/analysis/fft.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
CPU/GPU agnostic FFT functions using our preferred idioms
33
"""
44

5-
from typing import Union, Optional
5+
from typing import Union, Optional, Any
66
from collections.abc import Sequence
77
import numpy as np
88
import numpy.fft as fft_cpu
@@ -26,7 +26,7 @@ def _noshift(arr: array, **kwargs) -> array:
2626

2727
def ft2(m: array,
2828
axes: tuple[int, int] = (-1, -2),
29-
plan: Optional = None,
29+
plan: Optional[Any] = None,
3030
no_cache: bool = False,
3131
shift: bool = True,
3232
adjoint: bool = False) -> array:
@@ -52,7 +52,7 @@ def ft2(m: array,
5252

5353
def ift2(m: array,
5454
axes: tuple[int, int] = (-1, -2),
55-
plan: Optional = None,
55+
plan: Optional[Any] = None,
5656
no_cache: bool = False,
5757
shift: bool = True,
5858
adjoint: bool = False) -> array:
@@ -72,7 +72,7 @@ def ift2(m: array,
7272

7373
def irft2(m: array,
7474
axes: tuple[int, int] = (-2, -1),
75-
plan: Optional = None,
75+
plan: Optional[Any] = None,
7676
no_cache: bool = False,
7777
shift: bool = True,
7878
adjoint: bool = False) -> array:
@@ -120,7 +120,7 @@ def irft2(m: array,
120120

121121
def ft3(m: array,
122122
axes: tuple[int, int, int] = (-1, -2, -3),
123-
plan: Optional = None,
123+
plan: Optional[Any] = None,
124124
no_cache: bool = False,
125125
shift: bool = True,
126126
adjoint: bool = False) -> array:
@@ -141,7 +141,7 @@ def ft3(m: array,
141141

142142
def ift3(m: array,
143143
axes: tuple[int, int, int] = (-1, -2, -3),
144-
plan: Optional = None,
144+
plan: Optional[Any] = None,
145145
no_cache: bool = False,
146146
shift: bool = True,
147147
adjoint: bool = False) -> array:
@@ -161,7 +161,7 @@ def ift3(m: array,
161161

162162
def ftn(m: array,
163163
axes: tuple[int],
164-
plan: Optional = None,
164+
plan: Optional[Any] = None,
165165
no_cache: bool = False,
166166
shift: bool = True,
167167
adjoint: bool = False) -> array:
@@ -209,7 +209,7 @@ def ftn(m: array,
209209

210210
def iftn(m: array,
211211
axes: tuple[int],
212-
plan: Optional = None,
212+
plan: Optional[Any] = None,
213213
no_cache: bool = False,
214214
shift: bool = True,
215215
adjoint: bool = False) -> array:

Diff for: mcsim/analysis/field_prop.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def get_v(n: array,
6666

6767

6868
def frqs2angles(frqs: array,
69-
magnitude: float = 1.) -> (array, array):
69+
magnitude: float = 1.) -> tuple[array, array]:
7070
"""
7171
Convert from frequency vectors to angle vectors
7272
@@ -1505,7 +1505,7 @@ def gradient(self,
15051505
return phi_fwd[..., :-2, :, :, 0]
15061506

15071507
def get_estart(self,
1508-
inds: Optional[Sequence[int]] = None) -> (array, array):
1508+
inds: Optional[Sequence[int]] = None) -> tuple[array, array]:
15091509
if inds is None:
15101510
inds = list(range(self.n_samples))
15111511

Diff for: mcsim/analysis/fit_dmd_affine.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def fit_pattern_peaks(img: np.ndarray,
138138
chi_squared_relative_max: float = np.inf,
139139
max_position_err: float = 0.1,
140140
img_sd: Optional[np.ndarray] = None,
141-
debug: bool = False) -> (np.ndarray, np.ndarray, np.ndarray):
141+
debug: bool = False) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
142142
"""
143143
Fit peaks of fluorescence image corresponding to affine calibration pattern.
144144
@@ -385,6 +385,7 @@ def plot_affine_summary(img: np.ndarray,
385385
:param vmin_percentile:
386386
:param vmax_percentile:
387387
:param gamma: gamma used in displaying image
388+
:param cmap:
388389
:param kwargs: passed through to figure
389390
:return fig: figure handle to summary figure
390391
"""
@@ -598,7 +599,7 @@ def estimate_xform(img: np.ndarray,
598599
vmax_percentile: float = 99.,
599600
gamma: float = 1.,
600601
figsize: Sequence[float, float] = (16., 12.),
601-
**kwargs) -> (dict, Figure):
602+
**kwargs) -> tuple[dict, Figure]:
602603
"""
603604
Estimate affine transformation from DMD space to camera image space from an image.
604605

Diff for: mcsim/analysis/gauss_beam.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,11 @@ def get_q(wo: np.ndarray,
8383
assuming we use the phasor convention assuming phasor convention exp(-iwt). If using the other phasor convention,
8484
then take the complex conjugate of q
8585
86+
:param wo:
8687
:param z:
87-
:param p: [wo, zc, wavelength, n]
88+
:param wavelength:
89+
:param zc:
90+
:param n:
8891
:return:
8992
"""
9093
zr = np.pi * wo ** 2 / wavelength * n
@@ -100,6 +103,7 @@ def q2beam_params(qz,
100103
101104
:param qz: complex beam parameter
102105
:param wavelength:
106+
:param n:
103107
:return R(z), w(z)^2, w(o)^2, z, zr:
104108
"""
105109

Diff for: mcsim/analysis/mie_fields.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def jn(n: int,
227227

228228

229229
# helper function
230-
def spherical_hn(n: int, z: np.ndarray) -> (np.ndarray, np.ndarray):
230+
def spherical_hn(n: int, z: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
231231
hn = spherical_jn(n, z) + 1j * spherical_yn(n, z)
232232
dhn = spherical_jn(n, z, derivative=True) + 1j * spherical_yn(n, z, derivative=True)
233233
return hn, dhn
@@ -244,7 +244,7 @@ def mie_efield(wavelength: float,
244244
beam_phi: float = 0.,
245245
beam_psi: float = 0.,
246246
use_gpu: bool = False,
247-
**kwargs) -> (array, array, array, array):
247+
**kwargs) -> tuple[array, array, array, array]:
248248
"""
249249
Use miepython to compute multipolar coefficients, and use either scipy special functions
250250
or GPU accelerated custom CuPy kernels to calculate fields. There are many good references

Diff for: mcsim/analysis/odt_patterns.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def loss(c, include_line_penalty=True):
213213

214214
def add_position_multiplexing(centers: list[np.ndarray],
215215
frqs: np.ndarray,
216-
) -> (list[np.ndarray], list[np.ndarray]):
216+
) -> tuple[list[np.ndarray], list[np.ndarray]]:
217217
"""
218218
Given a set of spot positions (which may or may not include angle multiplexing), generate a set of
219219
frequency multiplexed patterns. To do this, start from our original set of patterns, and
@@ -304,7 +304,7 @@ def get_odt_patterns(pupil_positions: Sequence[np.ndarray],
304304
pupil_radius_mirrors: float,
305305
frqs: Sequence[np.ndarray],
306306
phase: Union[Sequence[np.ndarray], float] = 0.,
307-
use_off_mirrors: bool = True) -> (np.ndarray, list[dict]):
307+
use_off_mirrors: bool = True) -> tuple[np.ndarray, list[dict]]:
308308
"""
309309
Generate DMD patterns from a list of center positions
310310

Diff for: mcsim/analysis/optimize.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def test_gradient(self,
182182
x: array,
183183
jind: int = 0,
184184
inds: Optional[Sequence[int]] = None,
185-
dx: float = 1e-5) -> (array, array):
185+
dx: float = 1e-5) -> tuple[array, array]:
186186
"""
187187
Numerically test the gradient computation at a single coordinate of x.
188188

0 commit comments

Comments
 (0)