Skip to content

Commit

Permalink
Merge pull request #2688 from magnunor/gaussian2d_height_property
Browse files Browse the repository at this point in the history
Add height property to the Gaussian2D component
  • Loading branch information
jlaehne committed Mar 22, 2021
2 parents edd3b4b + b82298b commit 78c7974
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
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

0 comments on commit 78c7974

Please sign in to comment.