-
Notifications
You must be signed in to change notification settings - Fork 21
test function for get_angle_index #191
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 5 commits
e4b78fd
e2b38e0
1acc80f
5257ee2
56e7278
3dac06a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -248,15 +248,45 @@ def _set_array_from_range(self, begin, end, step_size=None, n_steps=None): | |
| array = np.linspace(begin, end, n_steps) | ||
| return array | ||
|
|
||
| def get_angle_index(self, angle): | ||
| count = 0 | ||
| for i, target in enumerate(self.angles): | ||
| if angle == target: | ||
| return i | ||
| else: | ||
| count += 1 | ||
| if count >= len(self.angles): | ||
| raise IndexError(f"WARNING: no angle {angle} found in angles list") | ||
| def get_array_index(self, value, xtype=None): | ||
| """ | ||
| returns the index of the closest value in the array associated with the specified xtype | ||
|
|
||
| Parameters | ||
| ---------- | ||
| xtype str | ||
| the xtype used to access the array | ||
| value float | ||
| the target value to search for | ||
|
|
||
| Returns | ||
| ------- | ||
| the index of the value in the array | ||
| """ | ||
|
|
||
| if xtype is None: | ||
| xtype = self.input_xtype | ||
| if self.on_xtype(xtype) is None or len(self.on_xtype(xtype)[0]) == 0: | ||
| raise ValueError( | ||
| f"The '{xtype}' array is empty. " "Please ensure it is initialized and the correct xtype is used." | ||
| ) | ||
| array = self.on_xtype(xtype)[0] | ||
| i = (np.abs(array - value)).argmin() | ||
| nearest_value = np.abs(array[i] - value) | ||
| distance = min(np.abs(value - array.min()), np.abs(value - array.max())) | ||
| threshold = 0.5 * (array.max() - array.min()) | ||
|
||
|
|
||
| if nearest_value != 0 and (array.min() <= value <= array.max() or distance <= threshold): | ||
|
||
| warnings.warn( | ||
| f"WARNING: The value {value} is not an exact match of the '{xtype}' array. " | ||
| f"Returning the index of the closest value." | ||
| ) | ||
| elif distance > threshold: | ||
| raise IndexError( | ||
| f"The value {value} is too far from any value in the '{xtype}' array. " | ||
| f"Please check if you have specified the correct xtype. " | ||
| ) | ||
| return i | ||
|
|
||
| def _set_xarrays(self, xarray, xtype): | ||
| self.all_arrays = np.empty(shape=(len(xarray), 4)) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current
on_xtypefunction does not raise an error when the specified xtype is invalid. I was thinking about letting that function raise an error in those cases, but it looks like we can handle invalid xtype together with empty array.