Skip to content

Check valid x and y array lenghts and xtypes in insert_scattering_quantity - #209

Merged
sbillinge merged 10 commits into
diffpy:mainfrom
bobleesj:scattering-obj-valid
Dec 12, 2024
Merged

Check valid x and y array lenghts and xtypes in insert_scattering_quantity#209
sbillinge merged 10 commits into
diffpy:mainfrom
bobleesj:scattering-obj-valid

Conversation

@bobleesj

Copy link
Copy Markdown
Contributor

Closes#200

@codecov

codecovBot commented Dec 8, 2024

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.62%. Comparing base (77c0b9e) to head (9bf5329).
Report is 11 commits behind head on main.

Additional details and impacted files
@@ Coverage Diff @@## main #209 +/- ##
==========================================
+ Coverage 96.54% 96.62% +0.08% 
==========================================
Files 8 8 Lines 347 356 +9 ==========================================
+ Hits 335 344 +9 
Misses 12 12 
Files with missing linesCoverage Δ
tests/test_diffraction_objects.py89.42% <100.00%> (+1.00%)⬆️


@xtype.setter
def xtype(self, _):
raise AttributeError(_setter_wmsg("xtype"))

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

@sbillinge Like Would you also like to prevent the user from directly modifying xtype?


if xtype is None:
xtype = self.input_xtype
xtype = self._xtype

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

If we do not want the user from directly modifying xtype, like _all_arrays, _xtype is used internally.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I like how you changed input_xtype to xtype! I do think people can directly modify xtype though (@sbillinge please confirm). For example I might want to plot my diffraction object on tth first, and then change its xtype to q so I can visualize it in qspace.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@yucongalicechen that is not how it works. the attribute was called input_xtype because we wanted to keep a record of the xtype input by the user. So I don't want to change its name. When xtype is used for plotting any xtype can be given, for example diffobj.on_xtype('q') would satisfy @yucongalicechen 's UC even if the data were input on a tth grid.

Since we only use it internally, we could change the attribute name to _input_xtype to make it private. I think it should only be set by insert_data and not re-settable.

@bobleesjbobleesjDec 9, 2024

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

@sbillinge - done. input_type is used internally and it st only gettable.

Comment threadtests/test_diffraction_objects.py Outdated

def test_xtype_getter():
do = DiffractionObject(xtype="tth")
assert do.xtype == "tth"

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Test getter for xtype

Comment threadtests/test_diffraction_objects.py Outdated
assert do.xtype == "tth"


def test_xtype_setter():

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Test setter for xtype - not allowed

@bobleesj
bobleesj marked this pull request as ready for review December 8, 2024 01:56
@bobleesj

Copy link
Copy Markdown
ContributorAuthor

@yucongalicechen could you please give me a quick review? Since you've written the issue, I want to ensure this is what was imagined.

Comment threadsrc/diffpy/utils/diffraction_objects.py
assert do.input_xtype == "tth"


def test_input_xtype_setter():

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Not allowed to reset _input_data

@bobleesj

Copy link
Copy Markdown
ContributorAuthor

Since we only use it internally, we could change the attribute name to _input_xtype to make it private. I think it should only be set by insert_data and not re-settable.

ready for review - @sbillinge

@sbillingesbillinge left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry, this disappeared of my notifications but I never submitted the review

def all_arrays(self, _):
raise AttributeError(_setter_wmsg("all_arrays"))

@property

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Didn't we decide that we don't want as setter for input_xtype? Sorry, I am getting a bit confused here.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Yes,

@input_xtype.setterdefinput_xtype(self, _):
raiseAttributeError(_setter_wmsg("input_xtype"))

This code basically, gives AttributeError when the user attempts to modify input_xtype manually.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I see. This is what I am seeing on my screen which is why I thought otherwise, but it may not be showing the latest code on GH?
image

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

not looking far enough down. Thanks for the explanation

self.wavelength = wavelength

# Check xtype is valid. An empty string is the default value.
if xtype != "":

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we delete this line? I think empty string is not in XQUANTITIES?

@bobleesjbobleesjDec 12, 2024

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

From our current init implementation, if xtype is not provided for DiffractionObjects, then xtype becomes an empty string as shown here:

classDiffractionObject:
def__init__(
self, name=None, wavelength=None, scat_quantity=None, metadata=None, xarray=None, yarray=None, xtype=None
):
...
self.metadata=metadataifxtypeisNone:
xtype=""

Hence, we need to have if xtype != "" to ensure that we do not provide the warning msg when no value is provided for xtype, as you mentioned, "" is not in XQUANTITIES

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That makes sense. I guess this is left over from our/my desire to allow people to instantiate an empty DO and then fill it later. Let's leave it like that, but we may be accumulating annoying technical debt that we wouldn't have if we went away from that choice.

@bobleesjbobleesj left a comment

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

@sbillinge ready for review - didn't modify any code but replied to your in-line comments.

@sbillingesbillinge left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please see comments. It all looks good, modulo a nasty feeling that we may have made an imperfect choice....

def all_arrays(self, _):
raise AttributeError(_setter_wmsg("all_arrays"))

@property

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I see. This is what I am seeing on my screen which is why I thought otherwise, but it may not be showing the latest code on GH?
image

def all_arrays(self, _):
raise AttributeError(_setter_wmsg("all_arrays"))

@property

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

not looking far enough down. Thanks for the explanation

self.wavelength = wavelength

# Check xtype is valid. An empty string is the default value.
if xtype != "":

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That makes sense. I guess this is left over from our/my desire to allow people to instantiate an empty DO and then fill it later. Let's leave it like that, but we may be accumulating annoying technical debt that we wouldn't have if we went away from that choice.

@sbillinge
sbillinge merged commit 3efd93b into diffpy:mainDec 12, 2024
@bobleesj
bobleesj deleted the scattering-obj-valid branch December 12, 2024 17:35
Sign up for freeto 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.

insert_scattering_quantity check valid arrays and xtypes

3 participants

@bobleesj@sbillinge@yucongalicechen