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

Crop edges update simplified #177

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Changed
for integrity check as `it replaces LGTM
<https://github.blog/2022-08-15-the-next-step-for-lgtm-com-github-code-scanning/>`_
- Added a centroid/center of mass functionality to analyse peak position of a spectrum (both in `utils`` and in `LumiSpectrum``)
- `s.crop_edges` moved to utils. It also now can take 1,2 or 4 values as input (can crop each axis independently) and also accepts percentages. The use of it under the `CommonLumiSpectrum` class is deprecated.

Maintenance
-----------
Expand Down
33 changes: 24 additions & 9 deletions doc/source/user_guide/utilities.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,34 @@ signals in the range of +/- 50 pixels around the centre of the overlapping regio

.. _spectral_map_utils:

Utilities for spectral maps
===========================

The function :py:meth:`~.signals.common_luminescence.CommonLumi.crop_edges`
removes the specified number of pixels from all four edges of a spectral map.
It is a convenience wrapper for the ``inav`` :external+hyperspy:ref:`method in
HyperSpy <signal.indexing>`.
Cropping multiple signals in the navigation axis
================================================

The function :py:meth:`~.utils.axes.crop_edges`
removes the specified number of pixels or % from the four edges of a spectral map,
from the edges inwards. It takes a list of `Signals` and cropping can happen
uniformly on all sides or by specifying the cropping range for each axis or each
side. If the navigation axes shape across the list of signals is different, all
signals can be rebinned to match the shape of the first signal in the list.
It is a convenience wrapper for the ``inav`` `method in HyperSpy
<https://hyperspy.org/hyperspy-doc/current/user_guide/signal.html#indexing>`_.

.. code-block:: python
>>> signals = [cl_map, sem_image]
>>> signals
[CLSpectrum <256,256|1024>, Signal2D <128,128|1>]
>>> signals_cropped = lum.utils.crop_edges(signals, crop_range=5, crop_units="%", rebin_nav=True)
>>> signals_cropped
[CLSpectrum <243,243|1024>, Signal2D <243,243|1>]
.. Note::
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add blank line before


Many scanning luminescence techniques result in edge defects at the edges of the scanned region.
This function enables the same cropping of the navigation axis for a list of signals in the same
region to correct for such defect.

>>> s.crop_edges(crop_px=2)
.. Note::

*[TODO: add possibility to crop different amounts of pixels on different sides]*
Before version `0.2.2` this function belonged to the class `CommonLumi` as :py:meth:`~.signals.common_luminescence.CommonLumi.crop_edges`. This use is now deprecated.


.. _unit_conversion:
Expand Down
1 change: 1 addition & 0 deletions lumispy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from lumispy.utils.axes import nm2eV, eV2nm, nm2invcm, invcm2nm, join_spectra
from lumispy.utils.io import to_array, savetxt
from lumispy.utils import crop_edges

from lumispy import signals
from lumispy import components
Expand Down
41 changes: 6 additions & 35 deletions lumispy/signals/common_luminescence.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,18 @@

from numpy import isnan
from warnings import warn
from lumispy.utils.signals import crop_edges


class CommonLumi:
"""**General luminescence signal class (dimensionless)**"""

def crop_edges(self, crop_px):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make our work for the 1.0 release easier, add a comment in the line before:
# Deprecated, to be removed for v1.0 release

"""Crop the amount of pixels from the four edges of the scanning
region, from out the edges inwards.

Parameters
----------
crop_px : int
Amount of pixels to be cropped on each side individually.

Returns
-------
signal_cropped : CommonLuminescence
A smaller cropped CL signal object. If inplace is True, the original
object is modified and no LumiSpectrum is returned.
"""

width = self.axes_manager.shape[0]
height = self.axes_manager.shape[1]

if crop_px * 2 > width or crop_px * 2 > height:
raise ValueError(
"The pixels to be cropped cannot be larger than half the width or the length!"
)
else:
signal_cropped = self.inav[
crop_px + 1 : width - crop_px + 1, crop_px + 1 : height - crop_px + 1
]

# Store transformation in metadata (or update the value if already previously transformed)

try:
signal_cropped.metadata.Signal.cropped_edges += crop_px
except AttributeError:
signal_cropped.metadata.set_item("Signal.cropped_edges", crop_px)

return signal_cropped
warn(
"This function is deprecated and will be deleted in v1.0. Please use ``sc = lum.utils.crop_edges(s)`` instead with the ``crop_range`` parameter instead.",
DeprecationWarning,
)
return crop_edges(self, crop_px=crop_px)

def remove_negative(self, basevalue=1, inplace=False):
"""Sets all negative values to 'basevalue', e.g. for logarithmic scale
Expand Down
16 changes: 5 additions & 11 deletions lumispy/tests/signals/test_common_luminescence.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,12 @@


class TestCommonLumi:
def test_crop_edges(self):
def test_crop_edges_deprecated(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comment before # Deprecated, to be removed for v1.0 release

s1 = LumiSpectrum(np.ones((10, 10, 10)))
s2 = LumiTransientSpectrum(np.ones((10, 10, 10, 10)))
s3 = LumiSpectrum(np.ones((3, 3, 10)))
s1 = s1.crop_edges(crop_px=2)
s2 = s2.crop_edges(crop_px=2)
assert s1.axes_manager.navigation_shape[0] == 6
assert s1.axes_manager.navigation_shape[1] == 6
assert s2.axes_manager.navigation_shape[0] == 6
assert s2.axes_manager.navigation_shape[1] == 6
with pytest.raises(ValueError):
s3.crop_edges(crop_px=2)
with pytest.warns(DeprecationWarning, match="This function is deprecated"):
s2 = s1.crop_edges(crop_px=1)
assert s2.axes_manager.navigation_shape[0] == 8
assert s2.axes_manager.navigation_shape[1] == 8

def test_remove_negative(self):
s1 = LumiSpectrum(np.random.random((10, 10, 10))) - 0.3
Expand Down
Loading