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

Port beam center finder notebook from old docs and fix bug in center finder #72

Merged
merged 16 commits into from
Feb 7, 2024

Conversation

nvaytet
Copy link
Member

@nvaytet nvaytet commented Feb 6, 2024

  • Porting the old notebook from https://scipp.github.io/ess/techniques/sans/sans-beam-center-finder.html

  • Also fixed a bug in the beam center finder based on I(Q) where phi positions for selecting pixels in the 4 quadrants were being computed on the raw uncalibrated data, instead of the calibrated data. This meant that while we were computing I(Q) using the correct pixel positions, when comparing 4 quadrants to get the curves to overlap, we were comparing data from quadrants not symmetrically divided around the newly found center, but around the origin [0, 0].

"id": "35ba00f1-705e-4307-b338-893323bf3e20",
"metadata": {},
"source": [
"## Method 2: computing $I(Q)$ inside 4 quadrants"
Copy link
Member

Choose a reason for hiding this comment

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

Isn't a lot of the code below in the Python module? Why is it repeated here? If it is to explain the methodology, that did not seem clear to me. If you intend to keep it, maybe first compute and plot using the Python module, and move the detailed explanation to a follow-up section?

Copy link
Member Author

Choose a reason for hiding this comment

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

See update

Comment on lines 257 to 270
out = (sc.sum(ref * c) / sc.sum(ref)).value
logger = get_logger('sans')
logger.info(f'Beam center finder: x={xy[0]}, y={xy[1]}, cost={out}')
if not np.isfinite(out):
raise ValueError(
iofq_values = all_q.values
out = 1.0e6 * iofq_values[np.isfinite(iofq_values)].max()
logger.info(
'Non-finite value computed in cost. This is likely due to a division by '
'zero. Try restricting your Q range, or increasing the size of your Q bins '
'to improve statistics in the denominator.'
'zero. The value was artificially changed to a large finite number. '
'If the results are not satisfactory, try restricting your Q range, or '
'increasing the size of your Q bins to improve statistics in the '
'denominator.'
)
logger.info(f'Beam center finder: x={xy[0]}, y={xy[1]}, cost={out}')
return out
Copy link
Member

Choose a reason for hiding this comment

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

Is this an intentional double use of out? So this is returning something very different if it is not finite?

Copy link
Member Author

Choose a reason for hiding this comment

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

If out is NaN, the optimizer will either fail or go round in circles and never finds a converged value.
So I'm returning a large cost instead of a NaN. I don't think I can modify the value anywhere else, because this function is used directly by the scipy wrapper.

Before, we used to raise as soon as the optimizer was trying to probe a region of space where there was almost no data. But actually, returning a large cost allows it to continue and look elsewhere.

Now that I think of it, it may work if I return np.inf instead of a large number. That would probably be better.

Comment on lines 161 to 162
phi = data.transform_coords(
'phi', graph=graph, keep_intermediate=False, keep_inputs=False
).coords['phi']
phi_bins = sc.linspace('phi', -pi, pi, 5, unit='rad')
Copy link
Member

Choose a reason for hiding this comment

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

Take care here when resolving the conflict.

Copy link
Member Author

Choose a reason for hiding this comment

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

I hope I didn't mess up

@nvaytet nvaytet merged commit be5fbc7 into main Feb 7, 2024
3 checks passed
@nvaytet nvaytet deleted the bcf-notebook branch February 7, 2024 13:52
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.

2 participants