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

rename x to dz in mud_calculator.py and decrease error tolerance for tests #147

Merged
merged 2 commits into from
Dec 28, 2024
Merged
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
23 changes: 23 additions & 0 deletions news/muD2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* no news added - rename variables in `mud_calculator.py` and edit tests

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
14 changes: 7 additions & 7 deletions src/diffpy/labpdfproc/mud_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ def _top_hat(z, half_slit_width):
def _model_function(z, diameter, z0, I0, mud, slope):
"""
Compute the model function with the following steps:
1. Recenter z to x by subtracting z0 (so that the circle is centered at 0 and it is easier to compute length l)
1. Let dz = z-z0, so that dz is centered at 0
sbillinge marked this conversation as resolved.
Show resolved Hide resolved
2. Compute length l that is the effective length for computing intensity I = I0 * e^{-mu * l}:
- For x within the diameter range, l is the chord length of the circle at position x
- For x outside this range, l = 0
- For dz within the capillary diameter, l is the chord length of the circle at position dz
- For dz outside this range, l = 0
3. Apply a linear adjustment to I0 by taking I0 as I0 - slope * z
"""
min_radius = -diameter / 2
max_radius = diameter / 2
x = z - z0
dz = z - z0
length = np.piecewise(
x,
[x < min_radius, (min_radius <= x) & (x <= max_radius), x > max_radius],
[0, lambda x: 2 * np.sqrt((diameter / 2) ** 2 - x**2), 0],
dz,
[dz < min_radius, (min_radius <= dz) & (dz <= max_radius), dz > max_radius],
[0, lambda dz: 2 * np.sqrt((diameter / 2) ** 2 - dz**2), 0],
)
return (I0 - slope * z) * np.exp(-mud / diameter * length)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_mud_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ def test_compute_mud(tmp_path):

expected_mud = 3
actual_mud = compute_mud(file)
assert actual_mud == pytest.approx(expected_mud, rel=0.01, abs=0.1)
assert actual_mud == pytest.approx(expected_mud, rel=1e-4, abs=1e-3)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

decrease the error tolerance. I think previously I changed this to 0.01 and 0.1 because I thought about the edge cases like HfO2 wire data. But for the other better data (pretty much all other real data we ran) the error is much lower. So we can have a smaller tolerance here.

Copy link
Contributor

@sbillinge sbillinge Dec 28, 2024

Choose a reason for hiding this comment

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

This is a test, not code, so the logic is that we set the tolerance we want for the test, not some worst case scenario I think, right?

Loading