-
Notifications
You must be signed in to change notification settings - Fork 21
Do not allow an empty instance of DiffractionObject - require xarrays
yarrays
xtype
#228
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 8 commits
92a2c4c
56541e5
1144d1f
e7b4dc2
6dc2738
50b7d0d
291cc89
b8a9adc
e3c5208
db89596
de6f12d
7ccebe8
2752578
d1f767b
afe814b
46c7be8
d24aacd
e285214
4f54439
7a2603e
8693153
e68b59e
235f624
88ea61b
6b20c1a
9f0f11a
aa1af39
d09d7fb
0e362c1
c142240
24b1bbc
2c51af1
ba4b985
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 |
---|---|---|
|
@@ -36,26 +36,68 @@ def _setter_wmsg(attribute): | |
|
||
class DiffractionObject: | ||
def __init__( | ||
self, name=None, wavelength=None, scat_quantity=None, metadata=None, xarray=None, yarray=None, xtype=None | ||
self, | ||
xarray, | ||
yarray, | ||
xtype, | ||
wavelength, | ||
scat_quantity="", | ||
name="", | ||
metadata={}, | ||
): | ||
if name is None: | ||
name = "" | ||
self.name = name | ||
if metadata is None: | ||
metadata = {} | ||
self.metadata = metadata | ||
if xtype is None: | ||
xtype = "" | ||
|
||
self._id = uuid.uuid4() | ||
self.input_data(xarray, yarray, xtype, wavelength, scat_quantity, name, metadata) | ||
sbillinge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def input_data(self, xarray, yarray, xtype, wavelength, scat_quantity="", name="", metadata={}): | ||
sbillinge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Insert a new scattering quantity into the scattering object. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improved docstrings, previous version:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we make it private we may want to move the docstring to the constructor? Also, then change "insert a new scattering object blah blah, to instantiate a new scattering object or sthg. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
Parameters | ||
---------- | ||
xarray : array-like | ||
The independent variable array (e.g., "q", "tth", or "d"). | ||
yarray : array-like | ||
sbillinge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
The dependent variable array corresponding to intensity values | ||
xtype : str | ||
The type of the independent variable in `xarray`. Must be one of {*XQUANTITIES}, | ||
such as "q", "tth", or "d". | ||
sbillinge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
wavelength : float | ||
The wavelength of the incoming beam, specified in angstroms (Å). | ||
scat_quantity : str, optional | ||
The type of scattering experiment (e.g., "x-ray", "neutron"). Default is "". | ||
name : str, optional | ||
The name or label for the scattering data. Default is an empty string "". | ||
metadata : dict, optional | ||
The additional metadata associated with the diffraction object. Default is {}. | ||
sbillinge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Returns | ||
------- | ||
None | ||
This method updates the object in place and does not return a value. | ||
""" | ||
|
||
# Check xtype is valid. An empty string is the default value. | ||
if xtype not in XQUANTITIES: | ||
raise ValueError(_xtype_wmsg(xtype)) | ||
|
||
sbillinge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Check xarray and yarray have the same length | ||
if len(xarray) != len(yarray): | ||
raise ValueError( | ||
"'xarray' and 'yarray' must have the same length. " | ||
"Please re-initialize 'DiffractionObject' or re-run the method 'input_data' " | ||
"with 'xarray' and 'yarray' of identical length." | ||
) | ||
|
||
sbillinge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.scat_quantity = scat_quantity | ||
self.wavelength = wavelength | ||
self.metadata = metadata | ||
self.name = name | ||
|
||
if xarray is None: | ||
xarray = np.empty(0) | ||
if yarray is None: | ||
yarray = np.empty(0) | ||
self._input_xtype = xtype | ||
|
||
self._id = uuid.uuid4() | ||
self.input_data(xarray, yarray, xtype) | ||
self._set_xarrays(xarray, xtype) | ||
self._all_arrays[:, 0] = yarray | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be cleaner of this was moved into _set_arrays unless there was a reason not to do that because it is reused somewhere or something There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
def __eq__(self, other): | ||
if not isinstance(other, DiffractionObject): | ||
|
@@ -298,8 +340,7 @@ def get_array_index(self, value, xtype=None): | |
the index of the value in the array | ||
""" | ||
|
||
if xtype is None: | ||
xtype = self._input_xtype | ||
xtype = self._input_xtype | ||
array = self.on_xtype(xtype)[0] | ||
if len(array) == 0: | ||
raise ValueError(f"The '{xtype}' array is empty. Please ensure it is initialized.") | ||
|
@@ -327,63 +368,6 @@ def _set_xarrays(self, xarray, xtype): | |
self.dmin = np.nanmin(self._all_arrays[:, 3], initial=np.inf) | ||
self.dmax = np.nanmax(self._all_arrays[:, 3], initial=0.0) | ||
|
||
def input_data( | ||
self, | ||
xarray, | ||
yarray, | ||
xtype, | ||
metadata={}, | ||
scat_quantity=None, | ||
name=None, | ||
wavelength=None, | ||
): | ||
f""" | ||
insert a new scattering quantity into the scattering object | ||
|
||
Parameters | ||
---------- | ||
xarray array-like of floats | ||
the independent variable array | ||
yarray array-like of floats | ||
the dependent variable array | ||
xtype string | ||
the type of quantity for the independent variable from {*XQUANTITIES, } | ||
metadata, scat_quantity, name and wavelength are optional. They have the same | ||
meaning as in the constructor. Values will only be overwritten if non-empty values are passed. | ||
|
||
Returns | ||
------- | ||
Nothing. Updates the object in place. | ||
|
||
""" | ||
|
||
# Check xarray and yarray have the same length | ||
if len(xarray) != len(yarray): | ||
raise ValueError( | ||
"'xarray' and 'yarray' must have the same length. " | ||
"Please re-initialize 'DiffractionObject' or re-run the method 'input_data' " | ||
"with 'xarray' and 'yarray' of identical length." | ||
) | ||
|
||
self._set_xarrays(xarray, xtype) | ||
self._all_arrays[:, 0] = yarray | ||
self._input_xtype = xtype | ||
# only update these optional values if non-empty quantities are passed to avoid overwriting | ||
# valid data inadvertently | ||
if metadata: | ||
self.metadata = metadata | ||
if scat_quantity is not None: | ||
self.scat_quantity = scat_quantity | ||
if name is not None: | ||
self.name = name | ||
if wavelength is not None: | ||
self.wavelength = wavelength | ||
|
||
# Check xtype is valid. An empty string is the default value. | ||
if xtype != "": | ||
if xtype not in XQUANTITIES: | ||
raise ValueError(_xtype_wmsg(xtype)) | ||
|
||
def _get_original_array(self): | ||
if self._input_xtype in QQUANTITIES: | ||
return self.on_q(), "q" | ||
|
Uh oh!
There was an error while loading. Please reload this page.