signals

This module provides the DiffractionSignal class for representing and manipulating diffraction pattern data.

class fpfunctions.signals.DiffractionSignal(diffraction: numpy.typing.NDArray.numpy.float64 | None = None, anchor_points: numpy.typing.NDArray.numpy.float64 | None = None, fmt: str | None = None, filename: str | None = None, wavelength: Tuple[float, float, float] | None = None, irf: str | None = None, irf_res: int | None = None, radiation: str | None = None, geometry: str | None = None, option_ilo: float | None = None, background: numpy.typing.NDArray.numpy.float64 | None = None, peaks: numpy.typing.NDArray.numpy.float64 | None = None, peaks_idx: numpy.typing.NDArray.numpy.float64 | None = None, max_anchor_points: int = 200)

Bases: object

Class representing a diffraction pattern signal with associated metadata and processing capabilities.

This class stores diffraction data and provides methods for manipulating background, anchor points, and peak detection. It supports various background calculation methods and peak finding algorithms.

Parameters:
  • diffraction – np.ndarray containing the diffraction data (2D array with x,y columns)

  • anchor_points – np.ndarray of anchor points for manual background definition

  • fmt – Format of the diffraction data file

  • filename – Path to the diffraction data file

  • wavelength – Tuple containing wavelength parameters (wavelength, ratio, weight)

  • irf – Path to the Instrument Resolution Function file

  • irf_res – Resolution parameter for the IRF

  • radiation – Type of radiation (‘xrd’, ‘cw’, ‘tof’)

  • geometry – Diffraction geometry (‘brag’, ‘deby’, ‘sync’, ‘psd’, ‘trmb’, ‘trmf’)

  • option_ilo – Optional parameter for specific geometries

  • background – Calculated background array

  • peaks – np.ndarray containing detected peaks (2D array with x,y columns)

  • peaks_idx – np.ndarray of indices corresponding to peak positions in the diffraction array

  • max_anchor_points – Maximum number of anchor points allowed

diffraction: numpy.typing.NDArray.numpy.float64 = None
anchor_points: numpy.typing.NDArray.numpy.float64 = None
fmt: str = None
filename: str = None
wavelength: Tuple[float, float, float] = None
irf: str = None
irf_res: int = None
radiation: str = None
geometry: str = None
option_ilo: float = None
background: numpy.typing.NDArray.numpy.float64 = None
peaks: numpy.typing.NDArray.numpy.float64 = None
peaks_idx: numpy.typing.NDArray.numpy.float64 = None
max_anchor_points: int = 200
classmethod from_file(filename: str, fmt: str) DiffractionSignal

Create a DiffractionSignal object from a diffraction data file.

This class method reads a diffraction data file using the appropriate IO factory based on the specified format, and initializes a DiffractionSignal object with the data. It automatically creates initial anchor points at the beginning and end of the diffraction pattern.

Parameters:
  • filename – Path to the diffraction data file

  • fmt – Format of the diffraction data file (e.g., ‘xys’, ‘free’, ‘socabim’)

Returns:

A new DiffractionSignal object initialized with the diffraction data

add_anchor_point(point: numpy.typing.NDArray.numpy.float64) None

Add a new anchor point to the background definition.

This method adds a new anchor point to the list of anchor points used for manual background definition. The anchor points are automatically sorted by their x-coordinates after addition.

Parameters:

point – Array containing the x,y coordinates of the new anchor point

Raises:

ValueError – If the maximum number of anchor points has been reached

remove_anchor_point(point: numpy.typing.NDArray.numpy.float64) None

Remove the anchor point closest to the specified point.

This method calculates the Euclidean distance between the input point and all existing anchor points, then removes the anchor point with the minimum distance.

Parameters:

point – Array containing the x,y coordinates of the reference point

remove_selected_anchor_points(anchor: numpy.typing.NDArray.numpy.float64) None

Remove multiple anchor points specified by their coordinates.

This method removes each anchor point in the input array by finding the closest existing anchor point to each one and removing it.

Parameters:

anchor – Array containing the x,y coordinates of points to remove. Can be a single point (1D array) or multiple points (2D array).

remove_anchor_points_in_range(xbeg: float, xfin: float) None

Remove all anchor points within a specified x-coordinate range.

This method removes all anchor points that have x-coordinates within the specified range. The range can be specified in either ascending or descending order.

Parameters:
  • xbeg – Beginning x-coordinate of the range

  • xfin – Ending x-coordinate of the range

select_anchor_point(point: numpy.typing.NDArray.numpy.float64) numpy.typing.NDArray.numpy.float64

Select the anchor point closest to the specified point.

This method calculates the distance between the input point and all existing anchor points along the x-axis, then returns the anchor point with the minimum distance.

Parameters:

point – Array containing the x,y coordinates of the reference point

Returns:

Array containing the coordinates of the selected anchor point

select_anchor_points_in_range(xbeg: float, xfin: float, ybeg: float, yfin: float) numpy.typing.NDArray.numpy.float64

Select all anchor points within a specified rectangular region.

This method selects all anchor points that fall within a rectangular region defined by the x and y coordinate ranges. The ranges can be specified in either ascending or descending order.

Parameters:
  • xbeg – Beginning x-coordinate of the range

  • xfin – Ending x-coordinate of the range

  • ybeg – Beginning y-coordinate of the range

  • yfin – Ending y-coordinate of the range

Returns:

Array containing the coordinates of all selected anchor points

get_anchor_points(num_points: int = 30) None

Generate anchor points from the calculated background.

This method creates anchor points by sampling the calculated background at regular intervals. The anchor points can then be used for manual background adjustment.

Parameters:

num_points – Number of anchor points to generate

Raises:

ValueError – If the requested number of anchor points exceeds the maximum allowed

get_background_manual() None

Calculate background using manual anchor points.

This method calculates the background by interpolating between the defined anchor points. It requires at least 3 anchor points to perform the interpolation. The calculated background is stored in the background attribute.

get_background_asls(log_p: float, log_lambda: float) None

Calculate background using the Asymmetric Least Squares (AsLS) method.

This method calculates the background using the Asymmetric Least Squares smoothing algorithm, which is effective for extracting backgrounds from noisy data with peaks. The calculated background is stored in the background attribute.

Parameters:
  • log_p – Logarithm of “p” the penalty function that accentuates the relative importance of the baseline points

  • log_lambda – Logarithm of the smoothing parameter

get_peaks_intens(alpha: float, width: int) None

Detect peaks in the diffraction pattern.

This method uses a peak finding algorithm to detect peaks in the diffraction pattern after background subtraction. The detected peaks are stored in the peaks attribute.

Parameters:
  • alpha – Sensitivity parameter for peak detection (higher values = fewer peaks)

  • width – Width parameter for peak detection (in data points)

add_peaks(point: numpy.typing.NDArray.numpy.float64) None

Add a new peak to the list of detected peaks.

This method adds a new peak to the list of peaks and sorts them by their x-coordinates.

Parameters:

point – Array containing the x,y coordinates of the new peak

remove_peak(point: numpy.typing.NDArray.numpy.float64) None

Remove the peak closest to the specified point.

This method calculates the Euclidean distance between the input point and all existing peaks, then removes the peak with the minimum distance.

Parameters:

point – Array containing the x,y coordinates of the reference point

select_peak(point: numpy.typing.NDArray.numpy.float64) numpy.typing.NDArray.numpy.float64

Select the peak closest to the specified point.

This method calculates the distance between the input point and all existing peaks along the x-axis, then returns the peak with the minimum distance.

Parameters:

point – Array containing the x,y coordinates of the reference point

Returns:

Array containing the coordinates of the selected peak

select_peaks_in_range(xbeg: float, xfin: float, ybeg: float, yfin: float) numpy.typing.NDArray.numpy.float64

Select all peaks within a specified rectangular region.

This method selects all peaks that fall within a rectangular region defined by the x and y coordinate ranges. The ranges can be specified in either ascending or descending order.

Parameters:
  • xbeg – Beginning x-coordinate of the range

  • xfin – Ending x-coordinate of the range

  • ybeg – Beginning y-coordinate of the range

  • yfin – Ending y-coordinate of the range

Returns:

Array containing the coordinates of all selected peaks

remove_selected_peaks(anchor: numpy.typing.NDArray.numpy.float64) None

Remove multiple peaks specified by their coordinates.

This method removes each peak in the input array by finding the closest existing peak to each one and removing it.

Parameters:

anchor – Array containing the x,y coordinates of peaks to remove. Can be a single point (1D array) or multiple points (2D array).

get_background_in_peak_pos() numpy.typing.NDArray.numpy.float64

Get background values at peak positions.

This method finds the indices of the diffraction data points that correspond to each peak position, and returns the background values at those indices. The peak indices are also stored in the peaks_idx attribute.

Returns:

Array containing background values at each peak position

__init__(diffraction: numpy.typing.NDArray.numpy.float64 | None = None, anchor_points: numpy.typing.NDArray.numpy.float64 | None = None, fmt: str | None = None, filename: str | None = None, wavelength: Tuple[float, float, float] | None = None, irf: str | None = None, irf_res: int | None = None, radiation: str | None = None, geometry: str | None = None, option_ilo: float | None = None, background: numpy.typing.NDArray.numpy.float64 | None = None, peaks: numpy.typing.NDArray.numpy.float64 | None = None, peaks_idx: numpy.typing.NDArray.numpy.float64 | None = None, max_anchor_points: int = 200) None