Skip to content

Commit

Permalink
Merge pull request #147 from yucongalicechen/mudcalc
Browse files Browse the repository at this point in the history
rename x to dz in `mud_calculator.py` and decrease error tolerance for tests
  • Loading branch information
sbillinge authored Dec 28, 2024
2 parents e8572fa + 11d2ccd commit a9fbbe5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
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
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)

0 comments on commit a9fbbe5

Please sign in to comment.