Skip to content

Commit

Permalink
Deal with the interpolation when bg q and sample q values are the sam…
Browse files Browse the repository at this point in the history
…e or very close
  • Loading branch information
yrshang authored Aug 9, 2024
1 parent 08c8a84 commit 9982b8b
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions src/usansred/reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,28 @@ def subtractBg(self, background, vScale=1.0):
return
"""

def _match_or_interpolate(q_data, q_bg, i_bg, e_bg, tolerance=1e-5):
"""Match q_bg values to q_data directly if close enough, otherwise interpolate"""

i_bg_matched = np.zeros_like(q_data)
e_bg_matched = np.zeros_like(q_data)

Check warning on line 691 in src/usansred/reduce.py

View check run for this annotation

Codecov / codecov/patch

src/usansred/reduce.py#L690-L691

Added lines #L690 - L691 were not covered by tests

for i, q in enumerate(q_data):

Check warning on line 693 in src/usansred/reduce.py

View check run for this annotation

Codecov / codecov/patch

src/usansred/reduce.py#L693

Added line #L693 was not covered by tests
# Find the index in q_bg that is closest to the current q_data value
idx = np.argmin(np.abs(q_bg - q))
if abs((q_bg[idx] - q) / q) <= tolerance:

Check warning on line 696 in src/usansred/reduce.py

View check run for this annotation

Codecov / codecov/patch

src/usansred/reduce.py#L695-L696

Added lines #L695 - L696 were not covered by tests
# If the q_bg value is close enough to the q_data value, use it directly
i_bg_matched[i] = i_bg[idx]
e_bg_matched[i] = e_bg[idx]

Check warning on line 699 in src/usansred/reduce.py

View check run for this annotation

Codecov / codecov/patch

src/usansred/reduce.py#L698-L699

Added lines #L698 - L699 were not covered by tests
else:
# Otherwise, interpolate
i_bg_matched[i] = np.interp(q, q_bg, i_bg)
e_bg_matched[i] = np.interp(q, q_bg, e_bg)

Check warning on line 703 in src/usansred/reduce.py

View check run for this annotation

Codecov / codecov/patch

src/usansred/reduce.py#L702-L703

Added lines #L702 - L703 were not covered by tests

return i_bg_matched, e_bg_matched

Check warning on line 705 in src/usansred/reduce.py

View check run for this annotation

Codecov / codecov/patch

src/usansred/reduce.py#L705

Added line #L705 was not covered by tests


if self.experiment.logbin:
assert self.isLogBinned
msg = (
Expand Down Expand Up @@ -716,12 +738,26 @@ def subtractBg(self, background, vScale=1.0):
# only the first bank is processed
dataScaled = self.dataScaled[0]
bgScaled = self.experiment.background.dataScaled[0]
interBg = numpy.interp(dataScaled["Q"], bgScaled["Q"], bgScaled["I"])
interBgE = numpy.interp(dataScaled["Q"], bgScaled["Q"], bgScaled["E"])
self.dataBgSubtracted["Q"] = dataScaled["Q"].copy()
self.dataBgSubtracted["I"] = (dataScaled["I"] - interBg).copy()
# FIXME The error bar got intepolated as well
self.dataBgSubtracted["E"] = ((numpy.array(dataScaled["E"]) ** 2 + interBgE**2) ** 0.5).copy()

# Convert things to numpy arrays
q_data = np.array(dataScaled['Q'])
i_data = np.array(dataScaled['I'])
e_data = np.array(dataScaled['E'])

Check warning on line 745 in src/usansred/reduce.py

View check run for this annotation

Codecov / codecov/patch

src/usansred/reduce.py#L744-L745

Added lines #L744 - L745 were not covered by tests

q_bg = np.array(bgScaled['Q'])
i_bg = np.array(bgScaled['I'])
e_bg = np.array(bgScaled['E'])

Check warning on line 749 in src/usansred/reduce.py

View check run for this annotation

Codecov / codecov/patch

src/usansred/reduce.py#L747-L749

Added lines #L747 - L749 were not covered by tests

# Interpolate bg I and E values at data Q points
i_bg_interp, e_bg_interp = _match_or_interpolate(q_data, q_bg, i_bg, e_bg)

Check warning on line 752 in src/usansred/reduce.py

View check run for this annotation

Codecov / codecov/patch

src/usansred/reduce.py#L752

Added line #L752 was not covered by tests

# Subtract background
i_subtracted = i_data - i_bg_interp
e_subtracted = np.sqrt(e_data**2 + e_bg_interp**2)

Check warning on line 756 in src/usansred/reduce.py

View check run for this annotation

Codecov / codecov/patch

src/usansred/reduce.py#L755-L756

Added lines #L755 - L756 were not covered by tests

self.dataBgSubtracted["Q"] = q_data
self.dataBgSubtracted["I"] = i_subtracted
self.dataBgSubtracted["E"] = e_subtracted

Check warning on line 760 in src/usansred/reduce.py

View check run for this annotation

Codecov / codecov/patch

src/usansred/reduce.py#L758-L760

Added lines #L758 - L760 were not covered by tests

msg = f"background subtracted from sample {self.name}, (background sample {background.name})"
logging.info(msg)
Expand Down

0 comments on commit 9982b8b

Please sign in to comment.