Skip to content

rename variables in mud_calculator.py and write more informative docstring #146

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

Merged
merged 5 commits into from
Dec 27, 2024

Conversation

yucongalicechen
Copy link
Collaborator

closes #122
@sbillinge ready for review. This PR is just some small edit in functions and docstring.

@@ -5,11 +5,11 @@
from diffpy.utils.parsers.loaddata import loadData


def _top_hat(x, slit_width):
def _top_hat(x, half_slit_width):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

change slit_width to half_slit_width to avoid confusion


This function loads z-scan data and fits it to a model
that convolves a top-hat function with I = I0 * e^{-mu * l}.
The fitting procedure is run multiple times, and we return the best-fit parameters based on the lowest rmse.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

add description to docstring

Copy link
Contributor

Choose a reason for hiding this comment

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

add a reference to your paper.

Also, I wonder if we should move this function out to diffpy.utils.tools?

"""
x_data, I_data = loadData(filepath, unpack=True)
best_mud, _ = min((_compute_single_mud(x_data, I_data) for _ in range(10)), key=lambda pair: pair[1])
best_mud, _ = min((_compute_single_mud(x_data, I_data) for _ in range(20)), key=lambda pair: pair[1])
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

change number of loops from 10 to 20

Copy link

codecov bot commented Dec 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.31%. Comparing base (7f264d2) to head (bd1dd40).
Report is 6 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #146   +/-   ##
=======================================
  Coverage   99.31%   99.31%           
=======================================
  Files           6        6           
  Lines         293      293           
=======================================
  Hits          291      291           
  Misses          2        2           
Files with missing lines Coverage Δ
tests/test_mud_calculator.py 100.00% <100.00%> (ø)

Copy link
Contributor

@sbillinge sbillinge left a comment

Choose a reason for hiding this comment

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

please see inline comments. Since we have the theoretical mu calculator in diffpy.utils, I wonder whether we should move the z-scan mu calculator there too?

@@ -32,7 +32,7 @@ def _model_function(x, diameter, x0, I0, mud, slope):
return (I0 - slope * x) * np.exp(-mud / diameter * length)


def _extend_x_and_convolve(x, diameter, slit_width, x0, I0, mud, slope):
def _extend_x_and_convolve(x, diameter, half_slit_width, x0, I0, mud, slope):
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these x's should be z's right?


This function loads z-scan data and fits it to a model
that convolves a top-hat function with I = I0 * e^{-mu * l}.
The fitting procedure is run multiple times, and we return the best-fit parameters based on the lowest rmse.
Copy link
Contributor

Choose a reason for hiding this comment

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

add a reference to your paper.

Also, I wonder if we should move this function out to diffpy.utils.tools?

@yucongalicechen
Copy link
Collaborator Author

Yeah we can move it - I think this'll be more convenient. Shall I move it under tools or should we add a new module for these two functions?

@sbillinge
Copy link
Contributor

Yeah we can move it - I think this'll be more convenient. Shall I move it under tools or should we add a new module for these two functions?

Let's put them in tools for now. They are "tools". We can break them out later if we want.

@sbillinge
Copy link
Contributor

Let's finish this PR and merge it, then make another PR that moves it out, which is just a cut and paste.

@yucongalicechen
Copy link
Collaborator Author

@sbillinge ready for review

@@ -5,96 +5,105 @@
from diffpy.utils.parsers.loaddata import loadData


def _top_hat(x, slit_width):
def _top_hat(z, half_slit_width):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

change all x to z

"""
min_radius = -diameter / 2
max_radius = diameter / 2
h = x - x0
x = z - z0
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

change h to x to avoid confusion, since we used h as full slit width in the paper

Copy link
Contributor

Choose a reason for hiding this comment

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

please see my comment above. We shouldn't use x here. For ease of reading the code, we could just leave it everywhere as (z-z0). This is how it is in the paper I think. If we want to define a new variable it would be better to call it dz, delta_z or z_s or something like that?

The full mathematical details are described in the paper:
An ad hoc Absorption Correction for Reliable Pair-Distribution Functions from Low Energy x-ray Sources,
Yucong Chen, Till Schertenleib, Andrew Yang, Pascal Schouwink, Wendy L. Queen and Simon J. L. Billinge,
in preparation.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

add reference

Copy link
Contributor

@sbillinge sbillinge left a comment

Choose a reason for hiding this comment

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

much better. A couple of more comments.

compute the model function with the following steps:
1. Recenter x to h by subtracting x0 (so that the circle is centered at 0 and it is easier to compute length l)
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we are using x correctly here either, right? In the paper, z is the vertical direction centered on the center of the beam. x is the direction of the x-ray beam. We didn't define an x0 (and don't need to) but it could be something like the center of the sample. So I am not sure that it makes sense to say "recentering z to x..."

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes I will change x to dz and change this docstring here

"""
min_radius = -diameter / 2
max_radius = diameter / 2
h = x - x0
x = z - z0
Copy link
Contributor

Choose a reason for hiding this comment

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

please see my comment above. We shouldn't use x here. For ease of reading the code, we could just leave it everywhere as (z-z0). This is how it is in the paper I think. If we want to define a new variable it would be better to call it dz, delta_z or z_s or something like that?

f.write(f"{x}\t{I}\n")

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

Choose a reason for hiding this comment

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

is there a reason you are making this so loose? In general, it doesn't give me huge confidence if we need it to be so loose t pass tests....

@bobleesj
Copy link
Contributor

bobleesj commented Dec 27, 2024

closes #122 @sbillinge ready for review. This PR is just some small edit in functions and docstring.

General comments for PRs, while there is an issue connected, could you pls also make titles more descriptive so that I (co-maintainers) can later identify the PR for debugging/understanding by scrolling? I like using gh CLI and great titles are great.

(meant to write this comment in diffpy.utils below)

@sbillinge
Copy link
Contributor

I will merge this, but @yucongalicechen maybe we could edit the title of the closed PR. Since we are working on "best practices" let's do that. A good thing would be for you two to figure out what the best title would be for future maintainers... So we can learn best practices for docstring titles...

@sbillinge sbillinge merged commit e8572fa into diffpy:main Dec 27, 2024
5 checks passed
@yucongalicechen yucongalicechen changed the title finalize mud_calculator.py rename variables in mud_calculator.py and write more informative docstring Dec 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change slope bounds and number of loops in mud_calculator.py
3 participants