Skip to content
58 changes: 58 additions & 0 deletions src/ess/sans/beam_center_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,64 @@ def _xy_extrema(pos: sc.Variable) -> sc.Variable:
return sc.concat([x_min, x_max, y_min, y_max], dim='extremes')


def _find_beam_center(
data,
sample_holder_radius,
sample_holder_arm_width,
):
Copy link
Member

Choose a reason for hiding this comment

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

Could you describe a bit what the algorithm is doing (maybe in a docstring)? It's quite hard to follow, also with the variable names like c and d.

If I followed a little, it seems this assumes masks will always be wedge-like (because sample holder will be an arm stretching out from one side). I am not sure we can make that assumption in all cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll update with a description.

c is the current beam center guess

d is the distance from the current beam center guess to each pixel

m = data.copy()
m.masks.clear()
s = m.bins.sum()

for i in range(20):
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain where the number 20 (and 10 below) comes from?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just iteration counts that I made up. You can probably reduce them by a lot but the iterations are quick anyway so it doesn't matter.

Copy link
Member

Choose a reason for hiding this comment

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

It would be good with either an explanation in a comment or docstring, or choose another criterion based on cost or something, to avoid magic numbers with no explanations.

It does matter to the person reading the code, wondering where it came from ;-)

c = (s.coords['position'] * sc.values(s)).sum() / sc.values(s).sum()
d = s.coords['position'] - c.data

outer = 0.9 * min(
sc.abs(d.fields.x.min()),
sc.abs(d.fields.x.max()),
sc.abs(d.fields.y.min()),
sc.abs(d.fields.y.max()),
)
s.masks['_outer'] = d.fields.x**2 + d.fields.y**2 > outer**2
s.masks['_inner'] = d.fields.x**2 + d.fields.y**2 < sample_holder_radius**2

if i > 10:
Copy link
Member

Choose a reason for hiding this comment

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

I didn't get from the description below why we start doing something different after 10 iterations.

s.coords['th'] = sc.where(
d.fields.x > sc.scalar(0.0, unit='m'),
sc.atan2(y=d.fields.y, x=d.fields.x),
sc.scalar(sc.constants.pi.value, unit='rad')
- sc.atan2(y=d.fields.y, x=-d.fields.x),
)
h = s.drop_masks(['_arm'] if '_arm' in s.masks else []).hist(th=100)
th = h.coords['th'][np.argmin(h.values)]

slope = sc.tan(th)
s.masks['_arm'] = (
d.fields.y < slope * d.fields.x + sample_holder_arm_width
) & (d.fields.y > slope * d.fields.x - sample_holder_arm_width)
return c.data


def beam_center_from_center_of_mass_alternative(
workflow,
sample_holder_radius=None,
sample_holder_arm_width=None,
) -> BeamCenter:
if sample_holder_radius is None:
sample_holder_radius = sc.scalar(0.05, unit='m')
if sample_holder_arm_width is None:
sample_holder_arm_width = sc.scalar(0.02, unit='m')

try:
beam_center = workflow.compute(BeamCenter)
except sciline.UnsatisfiedRequirement:
beam_center = sc.vector([0.0, 0.0, 0.0], unit='m')
workflow[BeamCenter] = beam_center
data = workflow.compute(MaskedData[SampleRun])
return _find_beam_center(data, sample_holder_radius, sample_holder_arm_width)


def beam_center_from_center_of_mass(workflow: sciline.Pipeline) -> BeamCenter:
"""
Estimate the beam center via the center-of-mass of the data counts.
Expand Down
Loading