Skip to content

Commit

Permalink
ran black with line-length 120
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDoornbos committed Oct 3, 2024
1 parent 8e26819 commit 6187e5d
Show file tree
Hide file tree
Showing 37 changed files with 359 additions and 835 deletions.
20 changes: 5 additions & 15 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,9 @@
# basic project information
project = "OpenWFS"
copyright = "2023-, Ivo Vellekoop, Daniël W. S. Cox, and Jeroen H. Doornbos, University of Twente"
author = (
"Jeroen H. Doornbos, Daniël W. S. Cox, Tom Knop, Harish Sasikumar, Ivo M. Vellekoop"
)
author = "Jeroen H. Doornbos, Daniël W. S. Cox, Tom Knop, Harish Sasikumar, Ivo M. Vellekoop"
release = "0.1.0rc2"
html_title = (
"OpenWFS - a library for conducting and simulating wavefront shaping experiments"
)
html_title = "OpenWFS - a library for conducting and simulating wavefront shaping experiments"
# \renewenvironment{sphinxtheindex}{\setbox0\vbox\bgroup\begin{theindex}}{\end{theindex}}

# latex configuration
Expand Down Expand Up @@ -167,23 +163,17 @@ def setup(app):
def source_read(app, docname, source):
if docname == "readme" or docname == "conclusion":
if (app.builder.name == "latex") == (docname == "conclusion"):
source[0] = source[0].replace(
"%endmatter%", ".. include:: acknowledgements.rst"
)
source[0] = source[0].replace("%endmatter%", ".. include:: acknowledgements.rst")
else:
source[0] = source[0].replace("%endmatter%", "")


def builder_inited(app):
if app.builder.name == "html":
exclude_patterns.extend(
["conclusion.rst", "index_latex.rst", "index_markdown.rst"]
)
exclude_patterns.extend(["conclusion.rst", "index_latex.rst", "index_markdown.rst"])
app.config.master_doc = "index"
elif app.builder.name == "latex":
exclude_patterns.extend(
["auto_examples/*", "index_markdown.rst", "index.rst", "api*"]
)
exclude_patterns.extend(["auto_examples/*", "index_markdown.rst", "index.rst", "api*"])
app.config.master_doc = "index_latex"
elif app.builder.name == "markdown":
include_patterns.clear()
Expand Down
8 changes: 2 additions & 6 deletions examples/sample_microscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@

# Code
img = set_pixel_size(
np.maximum(
np.random.randint(-10000, 100, (img_size_y, img_size_x), dtype=np.int16), 0
),
np.maximum(np.random.randint(-10000, 100, (img_size_y, img_size_x), dtype=np.int16), 0),
60 * u.nm,
)
src = StaticSource(img)
Expand Down Expand Up @@ -76,7 +74,5 @@
mic.xy_stage.x = p * 1 * u.um
mic.numerical_aperture = 1.0 * (p + 1) / p_limit # NA increases to 1.0
ax = grab_and_show(cam, ax)
plt.title(
f"NA: {mic.numerical_aperture}, δ: {mic.abbe_limit.to_value(u.um):2.2} μm"
)
plt.title(f"NA: {mic.numerical_aperture}, δ: {mic.abbe_limit.to_value(u.um):2.2} μm")
plt.pause(0.2)
4 changes: 1 addition & 3 deletions examples/slm_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
p4.phases = 1
p4.additive_blend = False

pf.phases = patterns.lens(
100, f=1 * u.m, wavelength=0.8 * u.um, extent=(10 * u.mm, 10 * u.mm)
)
pf.phases = patterns.lens(100, f=1 * u.m, wavelength=0.8 * u.um, extent=(10 * u.mm, 10 * u.mm))
rng = np.random.default_rng()
for n in range(200):
random_data = rng.random([10, 10], np.float32) * 2.0 * np.pi
Expand Down
4 changes: 1 addition & 3 deletions examples/troubleshooter_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,5 @@
roi_background = SingleRoi(cam, radius=10)

# Run WFS troubleshooter and output a report to the console
trouble = troubleshoot(
algorithm=alg, background_feedback=roi_background, frame_source=cam, shutter=shutter
)
trouble = troubleshoot(algorithm=alg, background_feedback=roi_background, frame_source=cam, shutter=shutter)
trouble.report()
8 changes: 2 additions & 6 deletions examples/wfs_demonstration_experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@

# constructs the actual slm for wavefront shaping, and a monitor window to display the current phase pattern
slm = SLM(monitor_id=2, duration=2)
monitor = slm.clone(
monitor_id=0, pos=(0, 0), shape=(slm.shape[0] // 4, slm.shape[1] // 4)
)
monitor = slm.clone(monitor_id=0, pos=(0, 0), shape=(slm.shape[0] // 4, slm.shape[1] // 4))

# we are using a setup with an SLM that produces 2pi phase shift
# at a gray value of 142
slm.lookup_table = range(142)
alg = FourierDualReference(
feedback=roi_detector, slm=slm, slm_shape=[800, 800], k_radius=7
)
alg = FourierDualReference(feedback=roi_detector, slm=slm, slm_shape=[800, 800], k_radius=7)

result = alg.execute()
print(result)
Expand Down
4 changes: 2 additions & 2 deletions openwfs/algorithms/basic_fourier.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ def __init__(
self.k_step = k_step
self._slm_shape = slm_shape
group_mask = np.zeros(slm_shape, dtype=bool)
group_mask[:, slm_shape[1] // 2:] = True
group_mask[:, slm_shape[1] // 2 :] = True
super().__init__(
feedback=feedback,
slm=slm,
phase_patterns=None,
group_mask=group_mask,
phase_steps=phase_steps,
amplitude='uniform',
amplitude="uniform",
iterations=iterations,
optimized_reference=optimized_reference,
analyzer=analyzer,
Expand Down
76 changes: 49 additions & 27 deletions openwfs/algorithms/custom_iter_dual_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,16 @@ class IterativeDualReference:
https://opg.optica.org/oe/ abstract.cfm?uri=oe-27-8-1167
"""

def __init__(self, feedback: Detector, slm: PhaseSLM, phase_patterns: tuple[nd, nd], group_mask: nd,
phase_steps: int = 4, iterations: int = 4, analyzer: Optional[callable] = analyze_phase_stepping):
def __init__(
self,
feedback: Detector,
slm: PhaseSLM,
phase_patterns: tuple[nd, nd],
group_mask: nd,
phase_steps: int = 4,
iterations: int = 4,
analyzer: Optional[callable] = analyze_phase_stepping,
):
"""
Args:
feedback: The feedback source, usually a detector that provides measurement data.
Expand Down Expand Up @@ -79,8 +87,9 @@ def __init__(self, feedback: Detector, slm: PhaseSLM, phase_patterns: tuple[nd,
self.masks = (~mask, mask) # masks[0] is True for group A, mask[1] is True for group B

# Pre-compute the conjugate modes for reconstruction
self.modes = [np.exp(-1j * self.phase_patterns[side]) * np.expand_dims(self.masks[side], axis=2) for side in
range(2)]
self.modes = [
np.exp(-1j * self.phase_patterns[side]) * np.expand_dims(self.masks[side], axis=2) for side in range(2)
]

def execute(self, capture_intermediate_results: bool = False, progress_bar=None) -> WFSResult:
"""
Expand Down Expand Up @@ -109,8 +118,10 @@ def execute(self, capture_intermediate_results: bool = False, progress_bar=None)

# Prepare progress bar
if progress_bar:
num_measurements = np.ceil(self.iterations / 2) * self.modes[0].shape[2] \
+ np.floor(self.iterations / 2) * self.modes[1].shape[2]
num_measurements = (
np.ceil(self.iterations / 2) * self.modes[0].shape[2]
+ np.floor(self.iterations / 2) * self.modes[1].shape[2]
)
progress_bar.total = num_measurements

# Switch the phase sets back and forth multiple times
Expand All @@ -119,8 +130,12 @@ def execute(self, capture_intermediate_results: bool = False, progress_bar=None)
ref_phases = -np.angle(t_full) # use the best estimate so far to construct an optimized reference
side_mask = self.masks[side]
# Perform WFS experiment on one side, keeping the other side sized at the ref_phases
result = self._single_side_experiment(mod_phases=self.phase_patterns[side], ref_phases=ref_phases,
mod_mask=side_mask, progress_bar=progress_bar)
result = self._single_side_experiment(
mod_phases=self.phase_patterns[side],
ref_phases=ref_phases,
mod_mask=side_mask,
progress_bar=progress_bar,
)

# Compute transmission matrix for the current side and update
# estimated transmission matrix
Expand All @@ -139,32 +154,39 @@ def execute(self, capture_intermediate_results: bool = False, progress_bar=None)
intermediate_results[it] = self.feedback.read()

# Compute average fidelity factors
fidelity_noise = weighted_average(results_latest[0].fidelity_noise,
results_latest[1].fidelity_noise, results_latest[0].n,
results_latest[1].n)
fidelity_amplitude = weighted_average(results_latest[0].fidelity_amplitude,
results_latest[1].fidelity_amplitude, results_latest[0].n,
results_latest[1].n)
fidelity_calibration = weighted_average(results_latest[0].fidelity_calibration,
results_latest[1].fidelity_calibration, results_latest[0].n,
results_latest[1].n)

result = WFSResult(t=t_full,
t_f=None,
n=self.modes[0].shape[2] + self.modes[1].shape[2],
axis=2,
fidelity_noise=fidelity_noise,
fidelity_amplitude=fidelity_amplitude,
fidelity_calibration=fidelity_calibration)
fidelity_noise = weighted_average(
results_latest[0].fidelity_noise, results_latest[1].fidelity_noise, results_latest[0].n, results_latest[1].n
)
fidelity_amplitude = weighted_average(
results_latest[0].fidelity_amplitude,
results_latest[1].fidelity_amplitude,
results_latest[0].n,
results_latest[1].n,
)
fidelity_calibration = weighted_average(
results_latest[0].fidelity_calibration,
results_latest[1].fidelity_calibration,
results_latest[0].n,
results_latest[1].n,
)

result = WFSResult(
t=t_full,
t_f=None,
n=self.modes[0].shape[2] + self.modes[1].shape[2],
axis=2,
fidelity_noise=fidelity_noise,
fidelity_amplitude=fidelity_amplitude,
fidelity_calibration=fidelity_calibration,
)

# TODO: document the t_set_all and results_all attributes
result.t_set_all = t_set_all
result.results_all = results_all
result.intermediate_results = intermediate_results
return result

def _single_side_experiment(self, mod_phases: nd, ref_phases: nd, mod_mask: nd,
progress_bar=None) -> WFSResult:
def _single_side_experiment(self, mod_phases: nd, ref_phases: nd, mod_mask: nd, progress_bar=None) -> WFSResult:
"""
Conducts experiments on one part of the SLM.
Expand Down
58 changes: 16 additions & 42 deletions openwfs/algorithms/dual_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ def __init__(
if iterations < 2:
raise ValueError("The number of iterations must be at least 2.")
if not optimized_reference and iterations != 2:
raise ValueError(
"When not using an optimized reference, the number of iterations must be 2."
)
raise ValueError("When not using an optimized reference, the number of iterations must be 2.")

self.slm = slm
self.feedback = feedback
Expand All @@ -108,7 +106,7 @@ def __init__(
~mask,
mask,
) # self.masks[0] is True for group A, self.masks[1] is True for group B
self.amplitude = amplitude # Note: when 'uniform' is passed, the shape of self.masks[0] is used.
self.amplitude = amplitude # Note: when 'uniform' is passed, the shape of self.masks[0] is used.
self.phase_patterns = phase_patterns

@property
Expand All @@ -121,19 +119,17 @@ def amplitude(self, value):
self._amplitude = None
return

if value == 'uniform':
if value == "uniform":
self._amplitude = tuple(
(np.ones(shape=self._shape) / np.sqrt(self.masks[side].sum())).astype(np.float32) for side in range(2))
(np.ones(shape=self._shape) / np.sqrt(self.masks[side].sum())).astype(np.float32) for side in range(2)
)
return

if value[0].shape != self._shape or value[1].shape != self._shape:
raise ValueError(
"The amplitude and group mask must all have the same shape."
)
raise ValueError("The amplitude and group mask must all have the same shape.")

self._amplitude = value


@property
def phase_patterns(self) -> tuple[nd, nd]:
return self._phase_patterns
Expand All @@ -148,26 +144,14 @@ def phase_patterns(self, value):
if not self.optimized_reference:
# find the modes in A and B that correspond to flat wavefronts with phase 0
try:
a0_index = next(
i
for i in range(value[0].shape[2])
if np.allclose(value[0][:, :, i], 0)
)
b0_index = next(
i
for i in range(value[1].shape[2])
if np.allclose(value[1][:, :, i], 0)
)
a0_index = next(i for i in range(value[0].shape[2]) if np.allclose(value[0][:, :, i], 0))
b0_index = next(i for i in range(value[1].shape[2]) if np.allclose(value[1][:, :, i], 0))
self.zero_indices = (a0_index, b0_index)
except StopIteration:
raise (
"For multi-target optimization, the both sets must contain a flat wavefront with phase 0."
)
raise ("For multi-target optimization, the both sets must contain a flat wavefront with phase 0.")

if (value[0].shape[0:2] != self._shape) or (value[1].shape[0:2] != self._shape):
raise ValueError(
"The phase patterns and group mask must all have the same shape."
)
raise ValueError("The phase patterns and group mask must all have the same shape.")

self._phase_patterns = (
value[0].astype(np.float32),
Expand Down Expand Up @@ -200,7 +184,7 @@ def _compute_cobasis(self):
denotes the matrix inverse, and ⁺ denotes the Moore-Penrose pseudo-inverse.
"""
if self.phase_patterns is None:
raise('The phase_patterns must be set before computing the cobasis.')
raise ("The phase_patterns must be set before computing the cobasis.")

cobasis = [None, None]
for side in range(2):
Expand All @@ -215,9 +199,7 @@ def _compute_cobasis(self):

self._cobasis = cobasis

def execute(
self, *, capture_intermediate_results: bool = False, progress_bar=None
) -> WFSResult:
def execute(self, *, capture_intermediate_results: bool = False, progress_bar=None) -> WFSResult:
"""
Executes the blind focusing dual reference algorithm and compute the SLM transmission matrix.
capture_intermediate_results: When True, measures the feedback from the optimized wavefront after each iteration.
Expand All @@ -237,9 +219,7 @@ def execute(

# Initialize storage lists
results_all = [None] * self.iterations # List to store all results
intermediate_results = np.zeros(
self.iterations
) # List to store feedback from full patterns
intermediate_results = np.zeros(self.iterations) # List to store feedback from full patterns

# Prepare progress bar
if progress_bar:
Expand Down Expand Up @@ -284,9 +264,7 @@ def execute(
relative = results_all[0].t[self.zero_indices[0], ...] + np.conjugate(
results_all[1].t[self.zero_indices[1], ...]
)
factor = (relative / np.abs(relative)).reshape(
(1, *self.feedback.data_shape)
)
factor = (relative / np.abs(relative)).reshape((1, *self.feedback.data_shape))

t_full = self.compute_t_set(results_all[0].t, self.cobasis[0]) + self.compute_t_set(
factor * results_all[1].t, self.cobasis[1]
Expand All @@ -304,9 +282,7 @@ def execute(
result.intermediate_results = intermediate_results
return result

def _single_side_experiment(
self, mod_phases: nd, ref_phases: nd, mod_mask: nd, progress_bar=None
) -> WFSResult:
def _single_side_experiment(self, mod_phases: nd, ref_phases: nd, mod_mask: nd, progress_bar=None) -> WFSResult:
"""
Conducts experiments on one part of the SLM.
Expand All @@ -322,9 +298,7 @@ def _single_side_experiment(
WFSResult: An object containing the computed SLM transmission matrix and related data.
"""
num_modes = mod_phases.shape[2]
measurements = np.zeros(
(num_modes, self.phase_steps, *self.feedback.data_shape)
)
measurements = np.zeros((num_modes, self.phase_steps, *self.feedback.data_shape))

for m in range(num_modes):
phases = ref_phases.copy()
Expand Down
4 changes: 1 addition & 3 deletions openwfs/algorithms/genetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ def __init__(
self.elite_size = elite_size
self.generations = generations
self.generator = generator or np.random.default_rng()
self.mutation_count = round(
(population_size - elite_size) * np.prod(shape) * mutation_probability
)
self.mutation_count = round((population_size - elite_size) * np.prod(shape) * mutation_probability)

def _generate_random_phases(self, shape):
return self.generator.random(size=shape, dtype=np.float32) * (2 * np.pi)
Expand Down
4 changes: 1 addition & 3 deletions openwfs/algorithms/ssa.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ def execute(self) -> WFSResult:
WFSResult: An object containing the computed transmission matrix and statistics.
"""
phase_pattern = np.zeros((self.n_y, self.n_x), "float32")
measurements = np.zeros(
(self.n_y, self.n_x, self.phase_steps, *self.feedback.data_shape)
)
measurements = np.zeros((self.n_y, self.n_x, self.phase_steps, *self.feedback.data_shape))

for y in range(self.n_y):
for x in range(self.n_x):
Expand Down
Loading

0 comments on commit 6187e5d

Please sign in to comment.