-
Notifications
You must be signed in to change notification settings - Fork 12
add fast_cve to labpdfprocapp.py and some other fixes #87
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
Changes from 9 commits
329a895
6954a29
97bcc00
2487ae6
7d0d574
b7e037e
a450dda
fc4d876
956408d
4bbe4a2
b91b6ec
e7d3d05
bb28204
3c6b128
377607a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,22 @@ | ||
| import math | ||
| import os | ||
|
|
||
| import numpy as np | ||
| import pandas as pd | ||
| from scipy.interpolate import interp1d | ||
|
|
||
| from diffpy.utils.scattering_objects.diffraction_objects import Diffraction_object | ||
|
|
||
| RADIUS_MM = 1 | ||
| N_POINTS_ON_DIAMETER = 300 | ||
| TTH_GRID = np.arange(1, 141, 1) | ||
| TTH_GRID = np.arange(1, 180.1, 0.1) | ||
|
|
||
| # pre-computed datasets for fast calculation | ||
| MUD_LIST = [0.5, 1, 2, 3, 4, 5, 6] | ||
| CWD = os.path.dirname(os.path.abspath(__file__)) | ||
| MULS = np.loadtxt(CWD + "/data/inverse_cve.xy") | ||
| COEFFICIENT_LIST = np.array(pd.read_csv(CWD + "/data/coefficient_list.csv", header=None)) | ||
| INTERPOLATION_FUNCTIONS = [interp1d(MUD_LIST, coefficients, kind="quadratic") for coefficients in COEFFICIENT_LIST] | ||
|
|
||
|
|
||
| class Gridded_circle: | ||
|
|
@@ -27,16 +37,6 @@ def _get_grid_points(self): | |
| self.grid = {(x, y) for x in xs for y in ys if x**2 + y**2 <= self.radius**2} | ||
| self.total_points_in_grid = len(self.grid) | ||
|
|
||
| # def get_coordinate_index(self, coordinate): # I think we probably dont need this function? | ||
| # count = 0 | ||
| # for i, target in enumerate(self.grid): | ||
| # if coordinate == target: | ||
| # return i | ||
| # else: | ||
| # count += 1 | ||
| # if count >= len(self.grid): | ||
| # raise IndexError(f"WARNING: no coordinate {coordinate} found in coordinates list") | ||
|
|
||
| def set_distances_at_angle(self, angle): | ||
| """ | ||
| given an angle, set the distances from the grid points to the entry and exit coordinates | ||
|
|
@@ -172,22 +172,9 @@ def get_path_length(self, grid_point, angle): | |
| return total_distance, primary_distance, secondary_distance | ||
|
|
||
|
|
||
| def compute_cve(diffraction_data, mud, wavelength): | ||
| def _cve_brute_force(diffraction_data, mud, wavelength): | ||
| """ | ||
| compute the cve for given diffraction data, mud and wavelength | ||
|
|
||
| Parameters | ||
| ---------- | ||
| diffraction_data Diffraction_object | ||
| the diffraction pattern | ||
| mud float | ||
| the mu*D of the diffraction object, where D is the diameter of the circle | ||
| wavelength float | ||
| the wavelength of the diffraction object | ||
|
|
||
| Returns | ||
| ------- | ||
| the diffraction object with cve curves | ||
| compute cve using brute-force method | ||
|
|
||
| it is computed as follows: | ||
| We first resample data and absorption correction to a more reasonable grid, | ||
|
|
@@ -207,7 +194,57 @@ def compute_cve(diffraction_data, mud, wavelength): | |
| distances = np.array(distances) / abs_correction.total_points_in_grid | ||
| muls = np.array(muls) / abs_correction.total_points_in_grid | ||
| cve = 1 / muls | ||
| return cve | ||
|
|
||
|
|
||
| def _cve_interp_polynomial(diffraction_data, mud, wavelength): | ||
| """ | ||
| compute cve using polynomial interpolation method, raise an error if mu*D is out of the range (0.5 to 6) | ||
| """ | ||
|
|
||
| if mud > 6 or mud < 0.5: | ||
| raise ValueError( | ||
| "mu*D is out of the acceptable range (0.5 to 6) for fast calculation. " | ||
| "Please rerun with a value within this range or use -b to enable brute-force calculation. " | ||
| ) | ||
| coeff_a, coeff_b, coeff_c, coeff_d, coeff_e = [ | ||
| interpolation_function(mud) for interpolation_function in INTERPOLATION_FUNCTIONS | ||
| ] | ||
| muls = np.array(coeff_a * MULS**4 + coeff_b * MULS**3 + coeff_c * MULS**2 + coeff_d * MULS + coeff_e) | ||
| cve = 1 / muls | ||
| return cve | ||
|
|
||
|
|
||
| def _cve_method(diffraction_data, mud, wavelength, brute_force=False): | ||
yucongalicechen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| selects the appropriate CVE calculation method | ||
| """ | ||
| if brute_force: | ||
| return _cve_brute_force(diffraction_data, mud, wavelength) | ||
| else: | ||
| return _cve_interp_polynomial(diffraction_data, mud, wavelength) | ||
|
|
||
|
|
||
| def compute_cve(diffraction_data, mud, wavelength, brute_force=False): | ||
yucongalicechen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| compute the cve for given diffraction data, mud, and wavelength, using the selected method | ||
|
|
||
| Parameters | ||
sbillinge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ---------- | ||
| diffraction_data Diffraction_object | ||
| the diffraction pattern | ||
| mud float | ||
| the mu*D of the diffraction object, where D is the diameter of the circle | ||
| wavelength float | ||
| the wavelength of the diffraction object | ||
|
|
||
| Returns | ||
| ------- | ||
| the diffraction object with cve curves | ||
|
|
||
| """ | ||
|
|
||
| cve = _cve_method(diffraction_data, mud, wavelength, brute_force) | ||
| orig_grid = diffraction_data.on_tth[0] | ||
| newcve = np.interp(orig_grid, TTH_GRID, cve) | ||
yucongalicechen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| abdo = Diffraction_object(wavelength=wavelength) | ||
|
|
@@ -220,7 +257,6 @@ def compute_cve(diffraction_data, mud, wavelength): | |
| wavelength=diffraction_data.wavelength, | ||
| scat_quantity="cve", | ||
| ) | ||
|
|
||
| return abdo | ||
|
|
||
|
|
||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -312,6 +312,7 @@ def test_load_metadata(mocker, user_filesystem): | |
| "wavelength": 0.71, | ||
| "output_directory": str(Path.cwd().resolve()), | ||
| "xtype": "tth", | ||
| "brute_force": False, | ||
| "key": "value", | ||
| "username": "cli_username", | ||
| "email": "[email protected]", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,15 +17,15 @@ def set_output_directory(args): | |
| args argparse.Namespace | ||
| the arguments from the parser | ||
|
|
||
| Returns | ||
| ------- | ||
| pathlib.PosixPath that contains the full path of the output directory | ||
|
|
||
| it is determined as follows: | ||
| If user provides an output directory, use it. | ||
| Otherwise, we set it to the current directory if nothing is provided. | ||
| We then create the directory if it does not exist. | ||
|
|
||
| Returns | ||
| ------- | ||
| pathlib.PosixPath that contains the full path of the output directory | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are we returning posixPath and not just Path? We want it to be platform independent, no? |
||
|
|
||
| """ | ||
| output_dir = Path(args.output_directory).resolve() if args.output_directory else Path.cwd().resolve() | ||
| output_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
@@ -110,13 +110,13 @@ def set_wavelength(args): | |
| args argparse.Namespace | ||
| the arguments from the parser | ||
|
|
||
| we raise an ValueError if the input wavelength is non-positive | ||
yucongalicechen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| or if the input anode_type is not one of the known sources | ||
|
|
||
| Returns | ||
| ------- | ||
| args argparse.Namespace | ||
|
|
||
| we raise an ValueError if the input wavelength is non-positive | ||
| or if the input anode_type is not one of the known sources | ||
|
|
||
| """ | ||
| if args.wavelength is not None and args.wavelength <= 0: | ||
| raise ValueError( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.