Skip to content

Size Distribution calculator clean up#3962

Open
jellybean2004 wants to merge 6 commits into
mainfrom
SD_calc_reformat
Open

Size Distribution calculator clean up#3962
jellybean2004 wants to merge 6 commits into
mainfrom
SD_calc_reformat

Conversation

@jellybean2004
Copy link
Copy Markdown
Member

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

  • There is nothing that needs documenting
  • Documentation changes are in this PR
  • There is an issue open for the documentation (link?)

Installers

  • There is a chance this will affect the installers, if so
    • Windows installer (GH artifact) has been tested (installed and worked)
    • MacOSX installer (GH artifact) has been tested (installed and worked)
    • Wheels installer (GH artifact) has been tested (installed and worked)

Licensing

  • The introduced changes comply with SasView license (BSD 3-Clause)

@jellybean2004 jellybean2004 changed the title Sd calc reformat Size Distribution calculator clean up May 21, 2026
@jellybean2004 jellybean2004 requested a review from Copilot May 21, 2026 13:23
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 sizeDistribution class.
  • 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 (try block), the code logs the exception but still assigns trim_data_pars[pkey] = data_vals. Since data_vals may be undefined on error, this can raise UnboundLocalError and 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_maxEnt docstring, the function name is misspelled as "add_gausisan_noise". This should match the actual helper name add_gaussian_noise to 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.

Comment thread src/sas/sascalc/size_distribution/SizeDistribution.py Outdated
Comment thread src/sas/sascalc/size_distribution/SizeDistribution.py Outdated
Comment thread src/sas/sascalc/size_distribution/SizeDistribution.py Outdated
Comment thread src/sas/sascalc/size_distribution/SizeDistribution.py Outdated
Comment thread src/sas/sascalc/size_distribution/SizeDistribution.py Outdated
Comment thread src/sas/sascalc/size_distribution/SizeDistribution.py
Comment thread src/sas/sascalc/size_distribution/SizeDistribution.py Outdated
@DrPaulSharp DrPaulSharp self-requested a review May 21, 2026 16:39
Copy link
Copy Markdown
Contributor

@DrPaulSharp DrPaulSharp left a comment

Choose a reason for hiding this comment

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

A few things to have a look at here.

Comment thread src/sas/sascalc/size_distribution/SizeDistribution.py Outdated
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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What is your thinking behind replacing the lambda function here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thought it would be more readable + allows type annotation. It was a suggestion I got from Copilot.

Comment thread src/sas/sascalc/size_distribution/SizeDistribution.py Outdated
Comment thread src/sas/sascalc/size_distribution/SizeDistribution.py Outdated
Comment thread src/sas/sascalc/size_distribution/SizeDistribution.py Outdated
Comment thread src/sas/sascalc/size_distribution/SizeDistribution.py Outdated
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.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do you know why the last clause is separated by a semicolon rather than a comma?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I added if False in the last clause in a recent commit. Is that what you meant?

Comment thread src/sas/sascalc/size_distribution/SizeDistribution.py Outdated
Comment thread src/sas/sascalc/size_distribution/SizeDistribution.py Outdated
Comment thread src/sas/sascalc/size_distribution/SizeDistribution.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants