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

Add height property to the Gaussian2D component #2688

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Expand Up @@ -15,6 +15,7 @@ New
* Plot overlayed images (`#2599 <https://github.com/hyperspy/hyperspy/pull/2599>`_)
* Support for reading JEOL EDS data (`#2488 <https://github.com/hyperspy/hyperspy/pull/2488>`_,
`#2607 <https://github.com/hyperspy/hyperspy/pull/2607>`_, `#2620 <https://github.com/hyperspy/hyperspy/pull/2620>`_)
* Add `height` property to the `Gaussian2D` component (`#2688 <https://github.com/hyperspy/hyperspy/pull/2688>`_)

Enhancements
------------
Expand Down
11 changes: 11 additions & 0 deletions hyperspy/_components/gaussian2d.py
Expand Up @@ -63,6 +63,8 @@ class Gaussian2D(Expression):
fwhm_x, fwhm_y : float
Convenience attributes to get and set the full-with-half-maxima along
the two axes.
height : float
Convenience attribute to get height of the Gaussian.
"""

def __init__(self, A=1., sigma_x=1., sigma_y=1., centre_x=0.,
Expand Down Expand Up @@ -108,6 +110,15 @@ def fwhm_y(self):
def fwhm_y(self, value):
self.sigma_y.value = value / sigma2fwhm

@property
def height(self):
"""float: Height of the Gaussian"""
return self.A.value / (2 * np.pi * self.sigma_x.value * self.sigma_y.value)

@height.setter
def height(self, value):
self.A.value = value * 2 * np.pi * self.sigma_x.value * self.sigma_y.value

@property
def sigma_major(self):
"""float: The sigma value of the major axis (axis with the largest
Expand Down
34 changes: 34 additions & 0 deletions hyperspy/tests/component/test_gaussian2d.py
Expand Up @@ -19,6 +19,7 @@
import math

import numpy as np
from pytest import approx

from hyperspy.components2d import Gaussian2D

Expand Down Expand Up @@ -63,6 +64,39 @@ def test_util_fwhm_getset():
assert g1.fwhm_y == 0.33


def test_height_value():
g = Gaussian2D()
g.sigma_x.value = 0.1
g.sigma_y.value = 0.5
g.A.value = 99
x = np.arange(-2, 2, 0.01)
y = np.arange(-2, 2, 0.01)
xx, yy = np.meshgrid(x, y)
g_image = g.function(xx, yy)
assert approx(g_image.max()) == g.height


def test_util_height_set():
g = Gaussian2D()
g.height = 0.33
np.testing.assert_allclose(
g.height, g.A.value / (2 * np.pi * g.sigma_x.value * g.sigma_y.value)
)


def test_util_height_get():
g = Gaussian2D(A=55)
np.testing.assert_allclose(
g.height, g.A.value / (2 * np.pi * g.sigma_x.value * g.sigma_y.value)
)


def test_util_height_getset():
g = Gaussian2D()
g.height = 0.165
assert g.height == 0.165


def test_properties():
g = Gaussian2D(add_rotation=True)
angle = np.radians(20)
Expand Down