Skip to content

Commit

Permalink
Warn instead of Exception when I(q) fails validation
Browse files Browse the repository at this point in the history
  • Loading branch information
glass-ships committed Feb 11, 2025
1 parent 5197bb1 commit 86b592d
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/drtsans/iq.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
determine_1d_log_bins,
)

from mantid.simpleapi import logger

# To ignore warning: invalid value encountered in true_divide
np.seterr(divide="ignore", invalid="ignore")

Expand All @@ -47,19 +49,21 @@ class BinningMethod(Enum):
WEIGHTED = 2 # weighted binning


def check_iq_for_binning(i_of_q: Union[IQmod, IQazimuthal]):
def check_iq_for_binning(i_of_q: Union[IQmod, IQazimuthal]) -> bool:
"""Check I(Q) for binning.
Binning I(Q) assumes that
1. there is no NaN or Infinity in intensities
2. there is no NaN, Infinity or Zero in intensity errors
:exception : RuntimeError
raise exception if input I(Q) does not meet assumption
:param i_of_q: ~drtsans.dataobjects.IQmod or IQazimuthal
I(Q)
Parameters
----------
i_of_q: Union[IQmod, IQazimuthal]
Returns
-------
bool
True if the input I(Q) for binning meets the assumptions
"""
error_message = ""

Expand Down Expand Up @@ -87,7 +91,9 @@ def check_iq_for_binning(i_of_q: Union[IQmod, IQazimuthal]):
error_message += "Intensity error has zero {}\n".format(np.where(np.abs(i_of_q.error) < 1e-20)[0])

if len(error_message) > 0:
raise RuntimeError("Input I(Q) for binning does not meet assumption:\n{}".format(error_message))
logger.warning(f"Input I(Q) for binning does not meet assumption:\n{error_message}")

return len(error_message) == 0


def valid_wedge(min_angle, max_angle) -> List[Tuple[float, float]]:
Expand Down Expand Up @@ -480,7 +486,7 @@ def bin_intensity_into_q1d(i_of_q, q_bins, bin_method=BinningMethod.NOWEIGHT, wa
drtsans.dataobjects.IQmod
the one dimensional data as a named tuple
"""
# Check input I(Q) whether it meets assumptions
# Check whether input I(Q) meets assumptions
check_iq_for_binning(i_of_q)

# bin I(Q)
Expand Down Expand Up @@ -640,7 +646,7 @@ def bin_annular_into_q1d(i_of_q, theta_bin_params, q_min=0.001, q_max=0.4, metho
)
raise ValueError(msg)

# Check input I(Q) whether it meets assumptions
# Check whether input I(Q) meets assumptions
check_iq_for_binning(i_of_q)

# convert the data to q and azimuthal angle
Expand Down Expand Up @@ -1036,7 +1042,7 @@ class IQazimuthal(namedtuple('IQazimuthal', 'intensity error qx qy delta_qx delt
~drtsans.dataobjects.IQazimuthal
binned IQazimuthal (important: must read Note 2)
"""
# Check input I(Q) whether it meets assumptions
# Check whether input I(Q) meets assumptions
check_iq_for_binning(i_of_q)

# Check whether it needs to bin wavelength
Expand Down

0 comments on commit 86b592d

Please sign in to comment.