Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute Operation Flat Field #2125

Merged
merged 1 commit into from
Jun 20, 2024
Merged

Compute Operation Flat Field #2125

merged 1 commit into from
Jun 20, 2024

Conversation

ashmeigh
Copy link
Collaborator

@ashmeigh ashmeigh commented Mar 12, 2024

Issue

Closes #2051

Description

This pull request refactors the Flat Field operation to transition from the partial function style to the compute_function approach. The changes enhance code clarity, maintainability, and performance.

Changes
Refactored FlatFieldFilter: Replaced the partial function with a structured compute_function method to encapsulate the core logic.
Introduced _compute_flat_field: Encapsulated core computation logic for parallel execution.
Shared Memory Allocation: Utilized shared memory for necessary arrays to improve performance.

Testing

Verified the refactored FlatFieldFilter by running all existing unit tests.
Ensured that _compute_flat_field computes flat field normalization correctly in parallel.

Acceptance Criteria

All unit tests pass.
FlatFieldFilter performs as expected with various image inputs.

Documentation

Updated the relevant documentation to reflect changes in the FlatFieldFilter operation.
Noted all changes in the appropriate file in docs/release_notes.

@ashmeigh ashmeigh marked this pull request as draft March 12, 2024 14:46
@ashmeigh ashmeigh force-pushed the Compute_flat_field branch from 1fdbaef to e8040be Compare June 10, 2024 15:29
@ashmeigh ashmeigh requested a review from samtygier-stfc June 10, 2024 15:36
@ashmeigh ashmeigh marked this pull request as ready for review June 11, 2024 10:41
Comment on lines 238 to 252
def execute_wrapper( # type: ignore
flat_before_widget: DatasetSelectorWidgetView, flat_after_widget: DatasetSelectorWidgetView,
dark_before_widget: DatasetSelectorWidgetView, dark_after_widget: DatasetSelectorWidgetView,
selected_flat_fielding_widget: QComboBox, use_dark_widget: QCheckBox) -> partial:
def execute_wrapper(**kwargs):
flat_before_widget = kwargs['flat_before_widget']
flat_after_widget = kwargs['flat_after_widget']
dark_before_widget = kwargs['dark_before_widget']
dark_after_widget = kwargs['dark_after_widget']
selected_flat_fielding_widget = kwargs['selected_flat_fielding_widget']
use_dark_widget = kwargs['use_dark_widget']
Copy link
Collaborator

Choose a reason for hiding this comment

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

These should stay as normal arguments. Going though kwargs makes type checking less useful.

Comment on lines 143 to 155
def _divide(data, norm_divide):
np.true_divide(data, norm_divide, out=data)

def _subtract(data, dark=None):
# specify out to do in place, otherwise the data is copied
np.subtract(data, dark, out=data)

def _norm_divide(flat: np.ndarray, dark: np.ndarray) -> np.ndarray:
# subtract dark from flat
return np.subtract(flat, dark)
Copy link
Collaborator

Choose a reason for hiding this comment

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

These need to be @staticmethods or to move outside the class.

task_name='Background Correction')
_execute(images, flat_avg, dark_avg, progress)
params = {'flat_avg': flat_avg, 'dark_avg': dark_avg}
ps.run_compute_func(FlatFieldFilter.compute_function, len(images.data), [images.shared_array], params)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would be good to keep the dimension checks.

@samtygier-stfc
Copy link
Collaborator

The current set of changes have several things that seem unrelated to switching to a compute function and seem to be the source of the test errors: Changes to the logic that checks if the flats and darks are present, changes to execute_wrapper, changes to validate_execute_kwargs.

@ashmeigh ashmeigh force-pushed the Compute_flat_field branch from 63a69ba to 8ea30a7 Compare June 19, 2024 11:26
@coveralls
Copy link

Coverage Status

coverage: 72.93% (-0.03%) from 72.96%
when pulling 8ea30a7 on Compute_flat_field
into 86644da on main.

@samtygier-stfc
Copy link
Collaborator

The 2 test fails seem odd, so I have set them to re run

Comment on lines 49 to 62
"""Uses the flat (open beam) and dark images to normalise a stack of images (radiograms, projections),
and to correct for a beam profile, scintillator imperfections and/or detector inhomogeneities. This
operation produces images of transmission values.

In practice, several open beam and dark images are averaged in the flat-fielding process.

Intended to be used on: Projections

When: As one of the first pre-processing steps

Caution: Make sure the correct stacks are selected for flat and dark.

Caution: Check that the flat and dark images don't have any very bright pixels,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you avoid removing the spaces here. They are needed for nice formatting in the docs.

https://mantidproject.github.io/mantidimaging/user_guide/operations/index.html#flat-fielding
With spaces
image

Without spaces
image

Comment on lines 143 to 155
@staticmethod
def _divide(data, norm_divide):
np.true_divide(data, norm_divide, out=data)

@staticmethod
def _subtract(data, dark=None):
# specify out to do in place, otherwise the data is copied
np.subtract(data, dark, out=data)

@staticmethod
def _norm_divide(flat: np.ndarray, dark: np.ndarray) -> np.ndarray:
# subtract dark from flat
return np.subtract(flat, dark)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think these can be removed

@ashmeigh ashmeigh requested a review from samtygier-stfc June 20, 2024 11:12
@coveralls
Copy link

Coverage Status

coverage: 72.992% (+0.03%) from 72.96%
when pulling afb62c8 on Compute_flat_field
into 86644da on main.

samtygier-stfc
samtygier-stfc previously approved these changes Jun 20, 2024
Copy link
Collaborator

@samtygier-stfc samtygier-stfc left a comment

Choose a reason for hiding this comment

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

Thanks. Looking good now. I am going to squish the commits and tweak the spacing to reduce churn.

@samtygier-stfc samtygier-stfc enabled auto-merge June 20, 2024 13:17
@coveralls
Copy link

Coverage Status

coverage: 72.992% (-0.03%) from 73.019%
when pulling 6220d70 on Compute_flat_field
into b866326 on main.

@samtygier-stfc samtygier-stfc added this pull request to the merge queue Jun 20, 2024
Merged via the queue into main with commit f0433e5 Jun 20, 2024
8 checks passed
@samtygier-stfc samtygier-stfc deleted the Compute_flat_field branch June 20, 2024 13:54
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.

Move operations to compute_function style
3 participants