Skip to content

Fix: let DiffractionObject.get_array_index to use xtype from its inputs. #356

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 3 commits into from
Jul 25, 2025

Conversation

ycexiao
Copy link
Contributor

@ycexiao ycexiao commented Jul 25, 2025

What problem does this PR address

Closes #355

What should the reviewer(s) do

Please check the tests and implementation

Copy link
Contributor Author

@ycexiao ycexiao left a comment

Choose a reason for hiding this comment

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

@sbillinge, the test code is ready for review.

@@ -390,7 +390,7 @@ def test_scale_to_bad(org_do_args, target_do_args, scale_inputs):
},
0,
),
(
( # 2. different xtype
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There was a test for the function. I modified it so that it can capture the bug now.

@ycexiao ycexiao marked this pull request as ready for review July 25, 2025 15:27
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.

I feel that the unfinished discussion is the behavior that we want and the function signature. The way the docstring is written it should be get_array_index(value, xtype=None), i.e., value is required but xtype not. If xtype is None: xtype=_initial_xtype.

Arguably, we could make xtype required. I think that this may make sense. I am not sure the UC where it is implicit. What do you think?

( # C2: Target value lies in the array, expect the (first) closest
# index
# C2: Target value lies in the array, expect the closest index
( # 1. same xtype
Copy link
Contributor

Choose a reason for hiding this comment

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

what does "same xtype" mean? same as what?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It means that the xtype of the original DiffractionObject and the xtype of the data from which the get_array_index gets the array index are the same.

I will modify the comment.

Copy link
Contributor

Choose a reason for hiding this comment

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

This may not need to be changed depending on the outcome of the discussion above. I am thinking that we may want to make both variables required. Do you know of a particular UC that requires the other? Do we now anywhere where this is being used actually?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you know of a particular UC that requires the other?

No. I think what the function does is to find the index. xtype is not actually needed.

And if users want to locate a specific q or tth that is different from the _input_xtype, they can use diff_obj.on_xtype("<xtype>"). Also, this might not be a common need.

Do we now anywhere where this is being used actually?

At least in diffpy.utils, it is not referenced. It is just a helper function we provide to users.

@ycexiao
Copy link
Contributor Author

ycexiao commented Jul 25, 2025

Arguably, we could make xtype required. I think that this may make sense. I am not sure the UC where it is implicit. What do you think?

I think xtype should be optional. DiffractionObject keeps a property _input_xtype. The expected behavior is that when xtype is not specified, diffpy.utils will operate on the data in its original format.

@sbillinge
Copy link
Contributor

Arguably, we could make xtype required. I think that this may make sense. I am not sure the UC where it is implicit. What do you think?

I think xtype should be optional. DiffractionObject keeps a property _input_xtype. The expected behavior is that when xtype is not specified, diffpy.utils will operate on the data in its original format.

I guess my question is what is the UC for that? Why not make it explicit? Even if you want the index in the original quantity, you can specify it? I mean I made that up in the first place, but I don't really think I know why. Sometimes simpler is better.

@sbillinge
Copy link
Contributor

On reflection, let's leave it as it is. I don't think there is a strong reason to change it and I may have had a reason I wanted it that way.

@sbillinge
Copy link
Contributor

Next, for the tests, can we dynamically load a list of xtypes that will automatically update in the future as we add more? Is there a way to do that? It would be good if that list could also be dynamically loaded in the docstring.

@ycexiao
Copy link
Contributor Author

ycexiao commented Jul 25, 2025

Next, for the tests, can we dynamically load a list of xtypes that will automatically update in the future as we add more? Is there a way to do that? It would be good if that list could also be dynamically loaded in the docstring.

Yes, we can. The allowed xtypes are stored in a variable, and we can use a format string to write those docstrings.

Copy link
Contributor Author

@ycexiao ycexiao left a comment

Choose a reason for hiding this comment

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

@sbillinge, It's ready for review.

Copy link

codecov bot commented Jul 25, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (93bc105) to head (eaeafe5).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #356   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            9         9           
  Lines          516       520    +4     
=========================================
+ Hits           516       520    +4     
Files with missing lines Coverage Δ
tests/test_diffraction_objects.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sbillinge sbillinge merged commit 44328a2 into diffpy:main Jul 25, 2025
5 checks passed
@sbillinge
Copy link
Contributor

Thanks for catching that and fixing the tests to catch the bad case

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.

fix: get_array_index ignores the input xtype
2 participants