Skip to content

Commit

Permalink
Merge pull request #3 from ericpre/add_black
Browse files Browse the repository at this point in the history
Add black
  • Loading branch information
ericpre authored Aug 26, 2023
2 parents 79a8db8 + 1b054f0 commit f57ff38
Show file tree
Hide file tree
Showing 11 changed files with 437 additions and 293 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/black.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Lint

on: [push, pull_request]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: psf/black@stable
3 changes: 2 additions & 1 deletion holospy/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@

import matplotlib


def pytest_configure(config):
matplotlib.use('agg')
matplotlib.use("agg")
98 changes: 50 additions & 48 deletions holospy/misc/reconstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@


def estimate_sideband_position(
holo_data, holo_sampling, central_band_mask_radius=None, sb='lower', high_cf=True):
holo_data, holo_sampling, central_band_mask_radius=None, sb="lower", high_cf=True
):
"""
Finds the position of the sideband and returns its position.
Expand Down Expand Up @@ -56,37 +57,28 @@ def estimate_sideband_position(
# A small aperture masking out the centerband.
ap_cb = 1.0 - aperture_function(f_freq, central_band_mask_radius, 1e-6)
if not high_cf: # Cut out higher frequencies, if necessary:
ap_cb *= aperture_function(f_freq,
np.max(f_freq) / (2 * np.sqrt(2)),
1e-6)
ap_cb *= aperture_function(f_freq, np.max(f_freq) / (2 * np.sqrt(2)), 1e-6)
# Imitates 0:
fft_holo = fft2(holo_data) / np.prod(holo_data.shape)
fft_filtered = fft_holo * ap_cb
# Sideband position in pixels referred to unshifted FFT
cb_position = (
fft_filtered.shape[0] //
2,
fft_filtered.shape[1] //
2) # cb: center band
if sb == 'lower':
fft_sb = abs(fft_filtered[:cb_position[0], :])
sb_position = np.asarray(
np.unravel_index(
fft_sb.argmax(),
fft_sb.shape))
elif sb == 'upper':
fft_sb = abs(fft_filtered[cb_position[0]:, :])
sb_position = (np.unravel_index(fft_sb.argmax(), fft_sb.shape))
fft_filtered.shape[0] // 2,
fft_filtered.shape[1] // 2,
) # cb: center band
if sb == "lower":
fft_sb = abs(fft_filtered[: cb_position[0], :])
sb_position = np.asarray(np.unravel_index(fft_sb.argmax(), fft_sb.shape))
elif sb == "upper":
fft_sb = abs(fft_filtered[cb_position[0] :, :])
sb_position = np.unravel_index(fft_sb.argmax(), fft_sb.shape)
sb_position = np.asarray(np.add(sb_position, (cb_position[0], 0)))
elif sb == 'left':
fft_sb = abs(fft_filtered[:, :cb_position[1]])
sb_position = np.asarray(
np.unravel_index(
fft_sb.argmax(),
fft_sb.shape))
elif sb == 'right':
fft_sb = abs(fft_filtered[:, cb_position[1]:])
sb_position = (np.unravel_index(fft_sb.argmax(), fft_sb.shape))
elif sb == "left":
fft_sb = abs(fft_filtered[:, : cb_position[1]])
sb_position = np.asarray(np.unravel_index(fft_sb.argmax(), fft_sb.shape))
elif sb == "right":
fft_sb = abs(fft_filtered[:, cb_position[1] :])
sb_position = np.unravel_index(fft_sb.argmax(), fft_sb.shape)
sb_position = np.asarray(np.add(sb_position, (0, cb_position[1])))
# Return sideband position:
return sb_position
Expand All @@ -112,15 +104,29 @@ def estimate_sideband_size(sb_position, holo_shape, sb_size_ratio=0.5):
"""

h = np.array((np.asarray(sb_position) - np.asarray([0, 0]),
np.asarray(sb_position) - np.asarray([0, holo_shape[1]]),
np.asarray(sb_position) - np.asarray([holo_shape[0], 0]),
np.asarray(sb_position) - np.asarray(holo_shape))) * sb_size_ratio
h = (
np.array(
(
np.asarray(sb_position) - np.asarray([0, 0]),
np.asarray(sb_position) - np.asarray([0, holo_shape[1]]),
np.asarray(sb_position) - np.asarray([holo_shape[0], 0]),
np.asarray(sb_position) - np.asarray(holo_shape),
)
)
* sb_size_ratio
)
return np.min(np.linalg.norm(h, axis=1))


def reconstruct(holo_data, holo_sampling, sb_size, sb_position, sb_smoothness,
output_shape=None, plotting=False):
def reconstruct(
holo_data,
holo_sampling,
sb_size,
sb_position,
sb_smoothness,
output_shape=None,
plotting=False,
):
"""Core function for holographic reconstruction.
Parameters
Expand Down Expand Up @@ -148,8 +154,7 @@ def reconstruct(holo_data, holo_sampling, sb_size, sb_position, sb_smoothness,
"""

holo_size = holo_data.shape
f_sampling = np.divide(
1, [a * b for a, b in zip(holo_size, holo_sampling)])
f_sampling = np.divide(1, [a * b for a, b in zip(holo_size, holo_sampling)])

fft_exp = fft2(holo_data) / np.prod(holo_size)

Expand All @@ -167,16 +172,15 @@ def reconstruct(holo_data, holo_sampling, sb_size, sb_position, sb_smoothness,
if plotting:
_, axs = plt.subplots(1, 1, figsize=(4, 4))
axs.imshow(abs(fftshift(fft_aperture)), clim=(0, 0.1))
axs.scatter(
sb_position[1],
sb_position[0],
s=10,
color='red',
marker='x')
axs.set_xlim(int(holo_size[0] / 2) - sb_size / np.mean(f_sampling), int(holo_size[0] / 2) +
sb_size / np.mean(f_sampling))
axs.set_ylim(int(holo_size[1] / 2) - sb_size / np.mean(f_sampling), int(holo_size[1] / 2) +
sb_size / np.mean(f_sampling))
axs.scatter(sb_position[1], sb_position[0], s=10, color="red", marker="x")
axs.set_xlim(
int(holo_size[0] / 2) - sb_size / np.mean(f_sampling),
int(holo_size[0] / 2) + sb_size / np.mean(f_sampling),
)
axs.set_ylim(
int(holo_size[1] / 2) - sb_size / np.mean(f_sampling),
int(holo_size[1] / 2) + sb_size / np.mean(f_sampling),
)
plt.show()

if output_shape is not None:
Expand All @@ -185,9 +189,7 @@ def reconstruct(holo_data, holo_sampling, sb_size, sb_position, sb_smoothness,
x_min = int(holo_size[1] / 2 - output_shape[1] / 2)
x_max = int(holo_size[1] / 2 + output_shape[1] / 2)

fft_aperture = fftshift(
fftshift(fft_aperture)[
y_min:y_max, x_min:x_max])
fft_aperture = fftshift(fftshift(fft_aperture)[y_min:y_max, x_min:x_max])

wav = ifft2(fft_aperture) * np.prod(holo_data.shape)

Expand All @@ -208,7 +210,7 @@ def aperture_function(r, apradius, rsmooth):
Smoothness in halfwidth. rsmooth = 1 will cause a decay from 1 to 0 over 2 pixel.
"""

return 0.5 * (1. - np.tanh((abs(r) - apradius) / (0.5 * rsmooth)))
return 0.5 * (1.0 - np.tanh((abs(r) - apradius) / (0.5 * rsmooth)))


def freq_array(shape, sampling):
Expand Down
23 changes: 12 additions & 11 deletions holospy/misc/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,19 @@ def calculate_carrier_frequency(holo_data, sb_position, scale):
"""

shape = holo_data.shape
origins = [np.array((0, 0)),
np.array((0, shape[1])),
np.array((shape[0], shape[1])),
np.array((shape[0], 0))]
origins = [
np.array((0, 0)),
np.array((0, shape[1])),
np.array((shape[0], shape[1])),
np.array((shape[0], 0)),
]
origin_index = np.argmin(
[np.linalg.norm(origin - sb_position) for origin in origins])
return np.linalg.norm(np.multiply(
origins[origin_index] - sb_position, scale))
[np.linalg.norm(origin - sb_position) for origin in origins]
)
return np.linalg.norm(np.multiply(origins[origin_index] - sb_position, scale))


def estimate_fringe_contrast_fourier(
holo_data, sb_position, apodization='hanning'):
def estimate_fringe_contrast_fourier(holo_data, sb_position, apodization="hanning"):
"""
Estimates average fringe contrast of a hologram by dividing amplitude
of maximum pixel of sideband by amplitude of FFT's origin.
Expand All @@ -77,10 +78,10 @@ def estimate_fringe_contrast_fourier(
holo_shape = holo_data.shape

if apodization:
if apodization == 'hanning':
if apodization == "hanning":
window_x = np.hanning(holo_shape[0])
window_y = np.hanning(holo_shape[1])
elif apodization == 'hamming':
elif apodization == "hamming":
window_x = np.hamming(holo_shape[0])
window_y = np.hamming(holo_shape[1])
window_2d = np.sqrt(np.outer(window_x, window_y))
Expand Down
2 changes: 1 addition & 1 deletion holospy/release_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
author = "Holospy Developers"
copyright = "Copyright 2016-2023, The holospy developers"
license = "GPLv3+"
status = "development"
status = "development"
7 changes: 4 additions & 3 deletions holospy/signals/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from .hologram_image import HologramImage, LazyHologramImage

__all__ = ["HologramImage",
"LazyHologramImage",
]
__all__ = [
"HologramImage",
"LazyHologramImage",
]
Loading

0 comments on commit f57ff38

Please sign in to comment.