sasdata.data_util.manipulations module

Data manipulations for 2D data sets. Using the meta data information, various types of averaging are performed in Q-space

To test this module use: ` cd test PYTHONPATH=../src/ python2  -m sasdataloader.test.utest_averaging DataInfoTests.test_sectorphi_quarter `

class sasdata.data_util.manipulations.Binning(min_value, max_value, n_bins, base=None)

Bases: object

This class just creates a binning object either linear or log

__dict__ = mappingproxy({'__module__': 'sasdata.data_util.manipulations', '__doc__': '\n    This class just creates a binning object\n    either linear or log\n    ', '__init__': <function Binning.__init__>, 'get_bin_index': <function Binning.get_bin_index>, '__dict__': <attribute '__dict__' of 'Binning' objects>, '__weakref__': <attribute '__weakref__' of 'Binning' objects>, '__annotations__': {}})
__doc__ = '\n    This class just creates a binning object\n    either linear or log\n    '
__init__(min_value, max_value, n_bins, base=None)
Parameters:
  • min_value – the value defining the start of the binning interval.

  • max_value – the value defining the end of the binning interval.

  • n_bins – the number of bins.

  • base – the base used for log, linear binning if None.

Beware that min_value should always be numerically smaller than max_value. Take particular care when binning angles across the 2pi to 0 discontinuity.

__module__ = 'sasdata.data_util.manipulations'
__weakref__

list of weak references to the object

get_bin_index(value)
Parameters:

value – the value in the binning interval whose bin index should be returned. Must be between min_value and max_value.

The general formula logarithm binning is: bin = floor(N * (log(x) - log(min)) / (log(max) - log(min)))

class sasdata.data_util.manipulations.Boxavg(x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0)

Bases: Boxsum

Perform the average of counts in a 2D region of interest.

__call__(data2D)

Perform the sum in the region of interest

Parameters:

data2D – Data2D object

Returns:

average counts, error on average counts

__doc__ = '\n    Perform the average of counts in a 2D region of interest.\n    '
__init__(x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0)
__module__ = 'sasdata.data_util.manipulations'
class sasdata.data_util.manipulations.Boxcut(x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0)

Bases: object

Find a rectangular 2D region of interest.

__call__(data2D)

Find a rectangular 2D region of interest.

Parameters:

data2D – Data2D object

Returns:

mask, 1d array (len = len(data)) with Trues where the data points are inside ROI, otherwise False

__dict__ = mappingproxy({'__module__': 'sasdata.data_util.manipulations', '__doc__': '\n    Find a rectangular 2D region of interest.\n    ', '__init__': <function Boxcut.__init__>, '__call__': <function Boxcut.__call__>, '_find': <function Boxcut._find>, '__dict__': <attribute '__dict__' of 'Boxcut' objects>, '__weakref__': <attribute '__weakref__' of 'Boxcut' objects>, '__annotations__': {}})
__doc__ = '\n    Find a rectangular 2D region of interest.\n    '
__init__(x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0)
__module__ = 'sasdata.data_util.manipulations'
__weakref__

list of weak references to the object

_find(data2D)

Find a rectangular 2D region of interest.

Parameters:

data2D – Data2D object

Returns:

out, 1d array (length = len(data)) with Trues where the data points are inside ROI, otherwise Falses

class sasdata.data_util.manipulations.Boxsum(x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0)

Bases: object

Perform the sum of counts in a 2D region of interest.

__annotations__ = {}
__call__(data2D)

Perform the sum in the region of interest

Parameters:

data2D – Data2D object

Returns:

number of counts, error on number of counts, number of points summed

__dict__ = mappingproxy({'__module__': 'sasdata.data_util.manipulations', '__doc__': '\n    Perform the sum of counts in a 2D region of interest.\n    ', '__init__': <function Boxsum.__init__>, '__call__': <function Boxsum.__call__>, '_sum': <function Boxsum._sum>, '__dict__': <attribute '__dict__' of 'Boxsum' objects>, '__weakref__': <attribute '__weakref__' of 'Boxsum' objects>, '__annotations__': {}})
__doc__ = '\n    Perform the sum of counts in a 2D region of interest.\n    '
__init__(x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0)
__module__ = 'sasdata.data_util.manipulations'
__weakref__

list of weak references to the object

_sum(data2D)

Perform the sum in the region of interest

Parameters:

data2D – Data2D object

Returns:

number of counts, error on number of counts, number of entries summed

class sasdata.data_util.manipulations.CircularAverage(r_min=0.0, r_max=0.0, bin_width=0.0005)

Bases: object

Perform circular averaging on 2D data

The data returned is the distribution of counts as a function of Q

__call__(data2D, ismask=False)

Perform circular averaging on the data

Parameters:

data2D – Data2D object

Returns:

Data1D object

__dict__ = mappingproxy({'__module__': 'sasdata.data_util.manipulations', '__doc__': '\n    Perform circular averaging on 2D data\n\n    The data returned is the distribution of counts\n    as a function of Q\n    ', '__init__': <function CircularAverage.__init__>, '__call__': <function CircularAverage.__call__>, '__dict__': <attribute '__dict__' of 'CircularAverage' objects>, '__weakref__': <attribute '__weakref__' of 'CircularAverage' objects>, '__annotations__': {}})
__doc__ = '\n    Perform circular averaging on 2D data\n\n    The data returned is the distribution of counts\n    as a function of Q\n    '
__init__(r_min=0.0, r_max=0.0, bin_width=0.0005)
__module__ = 'sasdata.data_util.manipulations'
__weakref__

list of weak references to the object

class sasdata.data_util.manipulations.Ring(r_min=0, r_max=0, center_x=0, center_y=0, nbins=36)

Bases: object

Defines a ring on a 2D data set. The ring is defined by r_min, r_max, and the position of the center of the ring.

The data returned is the distribution of counts around the ring as a function of phi.

Phi_min and phi_max should be defined between 0 and 2*pi in anti-clockwise starting from the x- axis on the left-hand side

__call__(data2D)

Apply the ring to the data set. Returns the angular distribution for a given q range

Parameters:

data2D – Data2D object

Returns:

Data1D object

__dict__ = mappingproxy({'__module__': 'sasdata.data_util.manipulations', '__doc__': '\n    Defines a ring on a 2D data set.\n    The ring is defined by r_min, r_max, and\n    the position of the center of the ring.\n\n    The data returned is the distribution of counts\n    around the ring as a function of phi.\n\n    Phi_min and phi_max should be defined between 0 and 2*pi\n    in anti-clockwise starting from the x- axis on the left-hand side\n    ', '__init__': <function Ring.__init__>, '__call__': <function Ring.__call__>, '__dict__': <attribute '__dict__' of 'Ring' objects>, '__weakref__': <attribute '__weakref__' of 'Ring' objects>, '__annotations__': {}})
__doc__ = '\n    Defines a ring on a 2D data set.\n    The ring is defined by r_min, r_max, and\n    the position of the center of the ring.\n\n    The data returned is the distribution of counts\n    around the ring as a function of phi.\n\n    Phi_min and phi_max should be defined between 0 and 2*pi\n    in anti-clockwise starting from the x- axis on the left-hand side\n    '
__init__(r_min=0, r_max=0, center_x=0, center_y=0, nbins=36)
__module__ = 'sasdata.data_util.manipulations'
__weakref__

list of weak references to the object

class sasdata.data_util.manipulations.Ringcut(r_min=0, r_max=0, center_x=0, center_y=0)

Bases: object

Defines a ring on a 2D data set. The ring is defined by r_min, r_max, and the position of the center of the ring.

The data returned is the region inside the ring

Phi_min and phi_max should be defined between 0 and 2*pi in anti-clockwise starting from the x- axis on the left-hand side

__call__(data2D)

Apply the ring to the data set. Returns the angular distribution for a given q range

Parameters:

data2D – Data2D object

Returns:

index array in the range

__dict__ = mappingproxy({'__module__': 'sasdata.data_util.manipulations', '__doc__': '\n    Defines a ring on a 2D data set.\n    The ring is defined by r_min, r_max, and\n    the position of the center of the ring.\n\n    The data returned is the region inside the ring\n\n    Phi_min and phi_max should be defined between 0 and 2*pi\n    in anti-clockwise starting from the x- axis on the left-hand side\n    ', '__init__': <function Ringcut.__init__>, '__call__': <function Ringcut.__call__>, '__dict__': <attribute '__dict__' of 'Ringcut' objects>, '__weakref__': <attribute '__weakref__' of 'Ringcut' objects>, '__annotations__': {}})
__doc__ = '\n    Defines a ring on a 2D data set.\n    The ring is defined by r_min, r_max, and\n    the position of the center of the ring.\n\n    The data returned is the region inside the ring\n\n    Phi_min and phi_max should be defined between 0 and 2*pi\n    in anti-clockwise starting from the x- axis on the left-hand side\n    '
__init__(r_min=0, r_max=0, center_x=0, center_y=0)
__module__ = 'sasdata.data_util.manipulations'
__weakref__

list of weak references to the object

class sasdata.data_util.manipulations.SectorPhi(r_min, r_max, phi_min=0, phi_max=6.283185307179586, nbins=20, base=None)

Bases: _Sector

Sector average as a function of phi. I(phi) is return and the data is averaged over Q.

A sector is defined by r_min, r_max, phi_min, phi_max. The number of bin in phi also has to be defined.

__call__(data2D)

Perform sector average and return I(phi).

Parameters:

data2D – Data2D object

Returns:

Data1D object

__doc__ = '\n    Sector average as a function of phi.\n    I(phi) is return and the data is averaged over Q.\n\n    A sector is defined by r_min, r_max, phi_min, phi_max.\n    The number of bin in phi also has to be defined.\n    '
__module__ = 'sasdata.data_util.manipulations'
class sasdata.data_util.manipulations.SectorQ(r_min, r_max, phi_min=0, phi_max=6.283185307179586, nbins=20, base=None)

Bases: _Sector

Sector average as a function of Q for both wings. setting the _Sector.fold attribute determines whether or not the two sectors are averaged together (folded over) or separate. In the case of separate (not folded), the qs for the “minor wing” are arbitrarily set to a negative value. I(Q) is returned and the data is averaged over phi.

A sector is defined by r_min, r_max, phi_min, phi_max. where r_min, r_max, phi_min, phi_max >0. The number of bin in Q also has to be defined.

__annotations__ = {}
__call__(data2D)

Perform sector average and return I(Q).

Parameters:

data2D – Data2D object

Returns:

Data1D object

__doc__ = '\n    Sector average as a function of Q for both wings. setting the _Sector.fold\n    attribute determines whether or not the two sectors are averaged together\n    (folded over) or separate.  In the case of separate (not folded), the\n    qs for the "minor wing" are arbitrarily set to a negative value.\n    I(Q) is returned and the data is averaged over phi.\n\n    A sector is defined by r_min, r_max, phi_min, phi_max.\n    where r_min, r_max, phi_min, phi_max >0.\n    The number of bin in Q also has to be defined.\n    '
__module__ = 'sasdata.data_util.manipulations'
class sasdata.data_util.manipulations.Sectorcut(phi_min=0, phi_max=3.141592653589793)

Bases: object

Defines a sector (major + minor) region on a 2D data set. The sector is defined by phi_min, phi_max, where phi_min and phi_max are defined by the right and left lines wrt central line.

Phi_min and phi_max are given in units of radian and (phi_max-phi_min) should not be larger than pi

__call__(data2D)

Find a rectangular 2D region of interest.

Parameters:

data2D – Data2D object

Returns:

mask, 1d array (len = len(data))

with Trues where the data points are inside ROI, otherwise False

__dict__ = mappingproxy({'__module__': 'sasdata.data_util.manipulations', '__doc__': '\n    Defines a sector (major + minor) region on a 2D data set.\n    The sector is defined by phi_min, phi_max,\n    where phi_min and phi_max are defined by the right\n    and left lines wrt central line.\n\n    Phi_min and phi_max are given in units of radian\n    and (phi_max-phi_min) should not be larger than pi\n    ', '__init__': <function Sectorcut.__init__>, '__call__': <function Sectorcut.__call__>, '_find': <function Sectorcut._find>, '__dict__': <attribute '__dict__' of 'Sectorcut' objects>, '__weakref__': <attribute '__weakref__' of 'Sectorcut' objects>, '__annotations__': {}})
__doc__ = '\n    Defines a sector (major + minor) region on a 2D data set.\n    The sector is defined by phi_min, phi_max,\n    where phi_min and phi_max are defined by the right\n    and left lines wrt central line.\n\n    Phi_min and phi_max are given in units of radian\n    and (phi_max-phi_min) should not be larger than pi\n    '
__init__(phi_min=0, phi_max=3.141592653589793)
__module__ = 'sasdata.data_util.manipulations'
__weakref__

list of weak references to the object

_find(data2D)

Find a rectangular 2D region of interest.

Parameters:

data2D – Data2D object

Returns:

out, 1d array (length = len(data))

with Trues where the data points are inside ROI, otherwise Falses

class sasdata.data_util.manipulations.SlabX(x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0, bin_width=0.001, fold=False)

Bases: _Slab

Compute average I(Qx) for a region of interest

__call__(data2D)

Compute average I(Qx) for a region of interest :param data2D: Data2D object :return: Data1D object

__doc__ = '\n    Compute average I(Qx) for a region of interest\n    '
__module__ = 'sasdata.data_util.manipulations'
class sasdata.data_util.manipulations.SlabY(x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0, bin_width=0.001, fold=False)

Bases: _Slab

Compute average I(Qy) for a region of interest

__annotations__ = {}
__call__(data2D)

Compute average I(Qy) for a region of interest

Parameters:

data2D – Data2D object

Returns:

Data1D object

__doc__ = '\n    Compute average I(Qy) for a region of interest\n    '
__module__ = 'sasdata.data_util.manipulations'
class sasdata.data_util.manipulations._Sector(r_min, r_max, phi_min=0, phi_max=6.283185307179586, nbins=20, base=None)

Bases: object

Defines a sector region on a 2D data set. The sector is defined by r_min, r_max, phi_min and phi_max. phi_min and phi_max are defined by the right and left lines wrt a central line such that phi_max could be less than phi_min if they straddle the discontinuity from 2pi to 0.

Phi is defined between 0 and 2*pi in anti-clockwise starting from the negative x-axis.

__annotations__ = {}
__dict__ = mappingproxy({'__module__': 'sasdata.data_util.manipulations', '__doc__': '\n    Defines a sector region on a 2D data set.\n    The sector is defined by r_min, r_max, phi_min and phi_max.\n    phi_min and phi_max are defined by the right and left lines wrt a central\n    line such that phi_max could be less than phi_min if they straddle the\n    discontinuity from 2pi to 0.\n\n    Phi is defined between 0 and 2*pi in anti-clockwise\n    starting from the negative x-axis.\n    ', '__init__': <function _Sector.__init__>, '_agv': <function _Sector._agv>, '__dict__': <attribute '__dict__' of '_Sector' objects>, '__weakref__': <attribute '__weakref__' of '_Sector' objects>, '__annotations__': {}})
__doc__ = '\n    Defines a sector region on a 2D data set.\n    The sector is defined by r_min, r_max, phi_min and phi_max.\n    phi_min and phi_max are defined by the right and left lines wrt a central\n    line such that phi_max could be less than phi_min if they straddle the\n    discontinuity from 2pi to 0.\n\n    Phi is defined between 0 and 2*pi in anti-clockwise\n    starting from the negative x-axis.\n    '
__init__(r_min, r_max, phi_min=0, phi_max=6.283185307179586, nbins=20, base=None)
Parameters:

base – must be a valid base for an algorithm, i.e.,

a positive number

__module__ = 'sasdata.data_util.manipulations'
__weakref__

list of weak references to the object

_agv(data2D, run='phi')

Perform sector averaging.

Parameters:
  • data2D – Data2D object

  • run – define the varying parameter (‘phi’ , or ‘sector’)

Returns:

Data1D object

class sasdata.data_util.manipulations._Slab(x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0, bin_width=0.001, fold=False)

Bases: object

Compute average I(Q) for a region of interest

__annotations__ = {}
__call__(data2D)

Call self as a function.

__dict__ = mappingproxy({'__module__': 'sasdata.data_util.manipulations', '__doc__': '\n    Compute average I(Q) for a region of interest\n    ', '__init__': <function _Slab.__init__>, '__call__': <function _Slab.__call__>, '_avg': <function _Slab._avg>, '__dict__': <attribute '__dict__' of '_Slab' objects>, '__weakref__': <attribute '__weakref__' of '_Slab' objects>, '__annotations__': {}})
__doc__ = '\n    Compute average I(Q) for a region of interest\n    '
__init__(x_min=0.0, x_max=0.0, y_min=0.0, y_max=0.0, bin_width=0.001, fold=False)
__module__ = 'sasdata.data_util.manipulations'
__weakref__

list of weak references to the object

_avg(data2D, maj)

Compute average I(Q_maj) for a region of interest. The major axis is defined as the axis of Q_maj. The minor axis is the axis that we average over.

Parameters:
  • data2D – Data2D object

  • maj_min – min value on the major axis

Returns:

Data1D object

sasdata.data_util.manipulations.flip_phi(phi: float) float

Force phi to be within the 0 <= to <= 2pi range by adding or subtracting 2pi as necessary

Returns:

phi in >=0 and <=2Pi

sasdata.data_util.manipulations.get_dq_data(data2d: Data2D) array

Get the dq for resolution averaging The pinholes and det. pix contribution present in both direction of the 2D which must be subtracted when converting to 1D: dq_overlap should calculated ideally at q = 0. Note This method works on only pinhole geometry. Extrapolate dqx(r) and dqy(phi) at q = 0, and take an average.

sasdata.data_util.manipulations.get_intercept(q: float, q_0: float, q_1: float) float | None

Returns the fraction of the side at which the q-value intercept the pixel, None otherwise. The values returned is the fraction ON THE SIDE OF THE LOWEST Q.

    A           B
+-----------+--------+    <--- pixel size
0                    1
Q_0 -------- Q ----- Q_1   <--- equivalent Q range
if Q_1 > Q_0, A is returned
if Q_1 < Q_0, B is returned
if Q is outside the range of [Q_0, Q_1], None is returned
sasdata.data_util.manipulations.get_pixel_fraction(q_max: float, q_00: float, q_01: float, q_10: float, q_11: float) float

Returns the fraction of the pixel defined by the four corners (q_00, q_01, q_10, q_11) that has q < q_max.:

        q_01                q_11
y=1         +--------------+
            |              |
            |              |
            |              |
y=0         +--------------+
        q_00                q_10

            x=0            x=1
sasdata.data_util.manipulations.get_pixel_fraction_square(x: float, x_min: float, x_max: float) float

Return the fraction of the length from xmin to x.:

    A            B
+-----------+---------+
xmin        x         xmax
Parameters:
  • x – x-value

  • x_min – minimum x for the length considered

  • x_max – minimum x for the length considered

Returns:

(x-xmin)/(xmax-xmin) when xmin < x < xmax

sasdata.data_util.manipulations.get_q_compo(dx: float, dy: float, detector_distance: float, wavelength: float, compo: str | None = None) float

This reduces tiny error at very large q. Implementation of this func is not started yet.<–ToDo

sasdata.data_util.manipulations.position_and_wavelength_to_q(dx: float, dy: float, detector_distance: float, wavelength: float) float
Parameters:
  • dx – x-distance from beam center [mm]

  • dy – y-distance from beam center [mm]

  • detector_distance – sample to detector distance [mm]

  • wavelength – neutron wavelength [nm]

Returns:

q-value at the given position

sasdata.data_util.manipulations.reader2D_converter(data2d: Data2D | None = None) Data2D

convert old 2d format opened by IhorReader or danse_reader to new Data2D format This is mainly used by the Readers

Parameters:

data2d – 2d array of Data2D object

Returns:

1d arrays of Data2D object