Size Distribution calculator clean up#3962
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors the Size Distribution calculator implementation to improve readability and maintainability by reformatting code, adding type hints, and clarifying docstrings/logging, while aiming to preserve existing behavior.
Changes:
- Added module/class docstrings and extensive type annotations across helper functions and the
sizeDistributionclass. - Refactored/cleaned up several methods (e.g., background fit, MaxEnt preparation/execution) and improved logging output.
- Reorganized initialization and internal state fields for binning, weighting, and result storage.
Comments suppressed due to low confidence (2)
src/sas/sascalc/size_distribution/SizeDistribution.py:570
- In
prep_maxEnt, if an exception occurs while trimming (tryblock), the code logs the exception but still assignstrim_data_pars[pkey] = data_vals. Sincedata_valsmay be undefined on error, this can raiseUnboundLocalErrorand hide the original problem. Consider re-raising,continue, or setting a safe fallback when trimming fails.
if check_data:
item = self._data.__dict__[pkey]
try:
if pkey == "y":
item = item - sub_intensities.y
elif pkey == "dy":
item = item + sub_intensities.dy
data_vals = item[self.ndx_qmin : self.ndx_qmax]
except Exception as e:
logger.exception("Error trimming data in prep_maxEnt: %s", e)
trim_data_pars[pkey] = data_vals
src/sas/sascalc/size_distribution/SizeDistribution.py:539
- In the
prep_maxEntdocstring, the function name is misspelled as "add_gausisan_noise". This should match the actual helper nameadd_gaussian_noiseto avoid confusion.
"""
1. Subtract intensities from the raw data.
2. Trim the data to the correct q-range for maxEnt; Create new trimmed Data1D object to return after MaxEnt.
3. Generate Model Data based of the trimmed data.
4. Create a list of intensities for maxEnt, if full_fit == True , call add_gausisan_noise nreps times;
pass just subtracted intensities.
5. Calculate initial bin weights, sigma, and return.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
DrPaulSharp
left a comment
There was a problem hiding this comment.
A few things to have a look at here.
| fit_func = lambda x,b:line_func(x, b, power) | ||
| init_guess = (linearized_data.y[0]) | ||
| def fit_func(x: npt.ArrayLike, b: float) -> npt.ArrayLike: | ||
| return line_func(x, b, power) |
There was a problem hiding this comment.
What is your thinking behind replacing the lambda function here?
There was a problem hiding this comment.
Thought it would be more readable + allows type annotation. It was a suggestion I got from Copilot.
| 5. calculate initial bin weights, sigma, and return | ||
| 3. Generate Model Data based of the trimmed data. | ||
| 4. Create a list of intensities for maxEnt, if full_fit == True , call add_gaussian_noise nreps times; | ||
| pass just subtracted intensities. |
There was a problem hiding this comment.
Do you know why the last clause is separated by a semicolon rather than a comma?
There was a problem hiding this comment.
I added if False in the last clause in a recent commit. Is that what you meant?
Description
Reformat, clean up and type hints for the Size distribution calculator.
How Has This Been Tested?
Manually tested functionality, same as before.
Review Checklist:
Documentation
Installers
Licensing