Skip to content

fix: get_array_index ignores the input xtype #355

@ycexiao

Description

@ycexiao

Problem

The function DiffractionObject.get_array_index source code is

    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 parameter xtype 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.

Proposed solution

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions