Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utility to denoise laser profile and adding a test for the same #315

Open
wants to merge 45 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
7ff5913
D1: Utility to denoise laser profile
Paaaaarth Oct 23, 2024
04d4e34
trial change
Paaaaarth Nov 7, 2024
f20666f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 7, 2024
59457f5
Merge branch 'LASY-org:development' into utility
Paaaaarth Nov 7, 2024
b4e5490
Merge branch 'LASY-org:development' into utility
Paaaaarth Nov 12, 2024
3f9d979
Changes to mode_decomposition code and documentation
Nov 7, 2024
f4dd7c8
D2: Utility to denoise laser profile
Nov 12, 2024
92b662a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 12, 2024
ecf3cb0
Fixing accidental commits
Nov 12, 2024
829e9e6
Merge branch 'development' into utility
Paaaaarth Nov 12, 2024
2670372
minor changes
Paaaaarth Nov 12, 2024
285973a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 12, 2024
0859fd6
minor changes
Paaaaarth Nov 12, 2024
9e0cf03
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 12, 2024
eff919b
minor changes
Paaaaarth Nov 12, 2024
51a7269
D1_test_denoise
Nov 19, 2024
1379fb4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 19, 2024
f01c74b
D2_test_denoise
Nov 19, 2024
e410bce
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 19, 2024
f996b4d
D3: Utility to denoise laser profile
Nov 19, 2024
e7fe531
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 19, 2024
be798e3
Minor change
Paaaaarth Nov 20, 2024
2bd3392
D3: Adding additional user-defined parameters.
Nov 21, 2024
aa01d24
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 21, 2024
a42264c
Merge branch 'LASY-org:development' into utility
Paaaaarth Nov 21, 2024
24dc4d5
D4: adding capability to add an img file as input.
Paaaaarth Nov 22, 2024
bbba42d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 22, 2024
5d24d74
D5: Minor changes
Paaaaarth Nov 22, 2024
4506458
D6: Changes to if else statement
Paaaaarth Nov 22, 2024
0f4bf4e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 22, 2024
bf128c6
Minor changes
Paaaaarth Nov 22, 2024
d7a0237
parameter order as per #326
Paaaaarth Dec 2, 2024
3377eef
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 2, 2024
f0827ce
D7:utility:denoise:remove skimage package
Paaaaarth Dec 3, 2024
f811464
Minor changes
Paaaaarth Dec 3, 2024
117f069
Merge branch 'LASY-org:development' into utility
Paaaaarth Dec 5, 2024
ba87f81
Requested changes implemented
Jan 2, 2025
9d371ac
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 2, 2025
111cf2a
Removing unnecessary outputs(new laser energy)
Paaaaarth Jan 2, 2025
c66c567
Minor changes
Paaaaarth Jan 2, 2025
1bd7d3a
Minor Changes
Paaaaarth Jan 2, 2025
e87b2dc
Minor changes
Paaaaarth Jan 2, 2025
41a1ceb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 2, 2025
b796669
Minor change
Paaaaarth Jan 2, 2025
5357182
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions lasy/utils/denoise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
from lasy.profiles.transverse.hermite_gaussian_profile import (
HermiteGaussianTransverseProfile,
)
from lasy.profiles.transverse.transverse_profile import TransverseProfile
from lasy.utils.mode_decomposition import hermite_gauss_decomposition


def denoise_transverse_hg(
transverse_profile,
wavelength,
resolution=0.2e-6,
lo=[-2e-4, -2e-4],
hi=[2e-4, 2e-4],
n_modes_x=2,
n_modes_y=2,
Paaaaarth marked this conversation as resolved.
Show resolved Hide resolved
):
"""
Denoise the transverse profile by decomposing it into a set of Hermite-Gaussian modes.

The profiles are weighted according to mode coefficients and then added.

Parameters
----------
transverse_profile : class instance
Paaaaarth marked this conversation as resolved.
Show resolved Hide resolved
An instance of a class or sub-class of TransverseProfile. Or it could be
a path to an image file from which intensity data will be read.
Paaaaarth marked this conversation as resolved.
Show resolved Hide resolved
Defines the transverse envelope of the laser.

wavelength : float (in meter)
Central wavelength at which the Hermite-Gauss beams are to be defined.

resolution : float
Paaaaarth marked this conversation as resolved.
Show resolved Hide resolved
The resolution of grid points in x and y that will be used
during the decomposition calculation.
Paaaaarth marked this conversation as resolved.
Show resolved Hide resolved

lo, hi : array of floats
The lower and upper bounds of the spatial grid on which the
decomposition will be performed.
Paaaaarth marked this conversation as resolved.
Show resolved Hide resolved

n_modes_x, n_modes_y : ints
The maximum values of `n_x` and `n_y` up to which the
expansion will be performed.

Returns
-------
transverse_profile_cleaned : class instance
Denoised transverse profile after decomposition and recombination.

waist : float (meter)
Beam waist for which the decomposition is calculated.
It is computed as the waist for which the weight of order 0 is maximum.

laser_energy_new : float
The total energy of the laser pulse after decomposition.
"""
laser_energy_new = 0
if isinstance(transverse_profile, TransverseProfile):
pass
else:
import numpy as np
import skimage

from lasy.profiles.transverse.transverse_profile_from_data import (
TransverseProfileFromData,
)

intensity_data = skimage.io.imread(transverse_profile)
intensity_scale = np.max(intensity_data) # Maximum value of the intensity
intensity_data[intensity_data < intensity_scale / 100] = 0
nx, ny = intensity_data.shape
lb = (0, 0) # Lower bounds in x and y
ub = (ny * resolution, nx * resolution) # Upper bounds in x and y

# Create the transverse profile. This also centers the data by default
transverse_profile = TransverseProfileFromData(
intensity_data, [lb[0], lb[1]], [ub[0], ub[1]]
)

# Calculate the decomposition and waist of the laser pulse
modeCoeffs, waist = hermite_gauss_decomposition(
transverse_profile, wavelength, resolution, lo, hi, n_modes_x, n_modes_y
)
Fixed Show fixed Hide fixed

# Denosing the laser profile
for i, mode_key in enumerate(list(modeCoeffs)):
transverse_profile_temp = HermiteGaussianTransverseProfile(
waist, waist, mode_key[0], mode_key[1], wavelength
) # Create a new profile for each mode

print(f"Mode {i}: {mode_key} with coefficient {modeCoeffs[mode_key]}")
laser_energy_new += modeCoeffs[mode_key] ** 2 # Energy fraction of the mode

if i == 0: # First mode (0,0)
transverse_profile_cleaned = modeCoeffs[mode_key] * transverse_profile_temp
else: # All other modes
transverse_profile_cleaned += modeCoeffs[mode_key] * transverse_profile_temp

# Energy loss due to decomposition
energy_loss = 1 - laser_energy_new
print(f"Energy loss: {energy_loss * 100:.2f}%")

return transverse_profile_cleaned, waist, laser_energy_new
37 changes: 37 additions & 0 deletions tests/test_denoise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Test the implementation of the denoise.

Test checks the implementation of the denoise
by initializing a super-Gaussian pulse and denoise it. It then
checks that the error remains positive and less than the predefined
value.
"""

import numpy as np

from lasy.profiles.transverse.super_gaussian_profile import (
SuperGaussianTransverseProfile,
)
from lasy.utils.denoise import denoise_transverse_hg


def test_denoise_transverse_hg():
# Parameters
waist = 20e-6
shape_parameter = 3
wavelength = 8e-7

# Define the transverse profile
transverse_profile = SuperGaussianTransverseProfile(
waist, shape_parameter
Fixed Show fixed Hide fixed
) # Super-Gaussian profile
transverse_profile_cleaned, waist, laser_energy_new = denoise_transverse_hg(
transverse_profile, wavelength
) # Denoised profile

# Calculate the error
x = np.linspace(-5 * waist, 5 * waist, 500)
X, Y = np.meshgrid(x, x)
prof1 = np.abs(transverse_profile.evaluate(X, Y)) ** 2
prof2 = np.abs(transverse_profile_cleaned.evaluate(X, Y)) ** 2
error = (prof1 - prof2) / np.max(prof1)
assert 0 < np.max(error) < 0.2
Loading