from diffpy.utils.transforms import d_to_tth
wavelength = 0.71
d = np.array([1.0, 0.8, 0.6, 0.4, 0.2])
tth = d_to_tth(d, wavelength)
will raise
ValueError: The supplied input array and wavelength will result in an impossible two-theta. Please check these values and re-instantiate the DiffractionObject with correct values.
def get_array_index(self, xtype, xvalue):
"""Return the index of the closest value in the array associated with
the specified xtype and the value provided.
...
xtype = self._input_xtype
xarray = self.on_xtype(xtype)[0]
if len(xarray) == 0:
raise ValueError(
f"The '{xtype}' array is empty. "
"Please ensure it is initialized."
)
index = (np.abs(xarray - xvalue)).argmin()
return index
"""
So the input parameterxtype doesn't have an effect at all. Thus, the following code
yes, this looks like a bug. The intent is that the user wants the index of, for example, Q=23.4 a-1, or tth=45 deg. so inside we want to use the relevant xarray, be it Q or tth or whatever.
Let's write some tests for this then fix the code.
Please check everywhere in the code where this is used, and also the docs so everything agrees. Maybe do all that work on separate PR clsing a separate issue.
Please also fix all the other documentation issues that you found.
Activity
ycexiao commented on Jul 24, 2025
The function DiffractionObject.get_array_index
def get_array_index(self, xtype, xvalue): """Return the index of the closest value in the array associated with the specified xtype and the value provided. ... xtype = self._input_xtype xarray = self.on_xtype(xtype)[0] if len(xarray) == 0: raise ValueError( f"The '{xtype}' array is empty. " "Please ensure it is initialized." ) index = (np.abs(xarray - xvalue)).argmin() return index """So the input parameterxtype doesn't have an effect at all. Thus, the following code
x = np.linspace(0, 90, 1000) y = np.random.rand(1000) tmp = DiffractionObject( xarray=x, yarray=y, xtype="angle", wavelength=1.54, ) index_angle = tmp.get_array_index(xvalue=0.24, xtype="angle") index_q_from_function = tmp.get_array_index(xvalue=0.24, xtype="q") real_index_q = np.argmin(np.abs(tmp.on_xtype("q")[0] - 0.24)) print(index_angle == index_q_from_function) print(index_q_from_function == real_index_q)returns True and False.
@sbillinge, is it a bug we want to fix?
sbillinge commented on Jul 25, 2025
yes, this looks like a bug. The intent is that the user wants the index of, for example, Q=23.4 a-1, or tth=45 deg. so inside we want to use the relevant xarray, be it Q or tth or whatever.
Let's write some tests for this then fix the code.
Please check everywhere in the code where this is used, and also the docs so everything agrees. Maybe do all that work on separate PR clsing a separate issue.
Please also fix all the other documentation issues that you found.