Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions src/ess/reflectometry/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,22 +440,42 @@ def combine_curves(
if len({c.coords['Q'].unit for c in curves}) != 1:
raise ValueError('The Q-coordinates must have the same unit for each curve')

r = _interpolate_on_qgrid(map(sc.values, curves), q_bin_edges).values
v = _interpolate_on_qgrid(map(sc.variances, curves), q_bin_edges).values
r = _interpolate_on_qgrid(map(sc.values, curves), q_bin_edges)
v = _interpolate_on_qgrid(map(sc.variances, curves), q_bin_edges)

v[v == 0] = np.nan
v = sc.where(v == sc.scalar(0.0, unit=v.unit), sc.scalar(np.nan, unit=v.unit), v)
inv_v = 1.0 / v
r_avg = np.nansum(r * inv_v, axis=0) / np.nansum(inv_v, axis=0)
v_avg = 1 / np.nansum(inv_v, axis=0)
return sc.DataArray(
r_avg = sc.nansum(r * inv_v, dim='curves') / sc.nansum(inv_v, dim='curves')
v_avg = 1 / sc.nansum(inv_v, dim='curves')

out = sc.DataArray(
data=sc.array(
dims='Q',
values=r_avg,
variances=v_avg,
values=r_avg.values,
variances=v_avg.values,
unit=next(iter(curves)).data.unit,
),
coords={'Q': q_bin_edges},
)
if any('Q_resolution' in c.coords for c in curves):
# This might need to be revisited. The question about how to combine curves
# with different Q-resolution is not completely resolved.
# However, in practice the difference in Q-resolution between different curves
# is small so it's not likely to make a big difference.
Comment on lines +461 to +464
Copy link
Member

Choose a reason for hiding this comment

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

Maybe this should be discussed in the docstring instead?

q_res = (
sc.DataArray(
data=c.coords.get(
'Q_resolution', sc.full_like(c.coords['Q'], value=np.nan)
),
coords={'Q': c.coords['Q']},
)
for c in curves
)
qs = _interpolate_on_qgrid(q_res, q_bin_edges)
out.coords['Q_resolution'] = sc.nansum(qs * inv_v, dim='curves') / sc.nansum(
sc.where(sc.isnan(qs), sc.scalar(0.0, unit=inv_v.unit), inv_v), dim='curves'
)
return out


def batch_processor(
Expand Down
29 changes: 29 additions & 0 deletions tests/reflectometry/tools_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,35 @@ def test_combined_curves():
)


@pytest.mark.filterwarnings("ignore:invalid value encountered in divide")
def test_combined_curves_resolution():
qgrid = sc.linspace('Q', 0, 1, 26)
data = sc.concat(
(
sc.ones(dims=['Q'], shape=[10], with_variances=True),
0.5 * sc.ones(dims=['Q'], shape=[15], with_variances=True),
),
dim='Q',
)

def curve(d, qmin, qmax):
return sc.DataArray(
data=d, coords={'Q': sc.linspace('Q', qmin, qmax, len(d) + 1)}
)

data.variances[:] = 0.1
curves = (
curve(data, 0, 0.3),
curve(0.5 * data, 0.2, 0.7),
curve(0.25 * data, 0.6, 1.0),
)
curves[0].coords['Q_resolution'] = sc.midpoints(curves[0].coords['Q']) / 5
combined = combine_curves(curves, qgrid)
assert 'Q_resolution' in combined.coords
assert combined.coords['Q_resolution'][0] == curves[0].coords['Q_resolution'][1]
assert sc.isnan(combined.coords['Q_resolution'][-1])


def test_linlogspace_linear():
q_lin = linlogspace(
dim='qz', edges=[0.008, 0.08], scale='linear', num=50, unit='1/angstrom'
Expand Down
Loading