6
6
import numpy as np
7
7
8
8
from diffpy .utils .tools import get_package_info
9
- from diffpy .utils .transforms import d_to_q , d_to_tth , q_to_d , q_to_tth , tth_to_d , tth_to_q
9
+ from diffpy .utils .transforms import (
10
+ d_to_q ,
11
+ d_to_tth ,
12
+ q_to_d ,
13
+ q_to_tth ,
14
+ tth_to_d ,
15
+ tth_to_q ,
16
+ )
10
17
11
18
QQUANTITIES = ["q" ]
12
19
ANGLEQUANTITIES = ["angle" , "tth" , "twotheta" , "2theta" ]
13
20
DQUANTITIES = ["d" , "dspace" ]
14
21
XQUANTITIES = ANGLEQUANTITIES + DQUANTITIES + QQUANTITIES
15
- XUNITS = ["degrees" , "radians" , "rad" , "deg" , "inv_angs" , "inv_nm" , "nm-1" , "A-1" ]
22
+ XUNITS = [
23
+ "degrees" ,
24
+ "radians" ,
25
+ "rad" ,
26
+ "deg" ,
27
+ "inv_angs" ,
28
+ "inv_nm" ,
29
+ "nm-1" ,
30
+ "A-1" ,
31
+ ]
16
32
17
33
x_values_not_equal_emsg = (
18
34
"The two objects have different values in x arrays (my_do.all_arrays[:, [1, 2, 3]]). "
@@ -129,9 +145,13 @@ def __init__(
129
145
"""
130
146
131
147
self ._uuid = uuid .uuid4 ()
132
- self ._input_data (xarray , yarray , xtype , wavelength , scat_quantity , name , metadata )
148
+ self ._input_data (
149
+ xarray , yarray , xtype , wavelength , scat_quantity , name , metadata
150
+ )
133
151
134
- def _input_data (self , xarray , yarray , xtype , wavelength , scat_quantity , name , metadata ):
152
+ def _input_data (
153
+ self , xarray , yarray , xtype , wavelength , scat_quantity , name , metadata
154
+ ):
135
155
if xtype not in XQUANTITIES :
136
156
raise ValueError (_xtype_wmsg (xtype ))
137
157
if len (xarray ) != len (yarray ):
@@ -152,8 +172,12 @@ def _input_data(self, xarray, yarray, xtype, wavelength, scat_quantity, name, me
152
172
def __eq__ (self , other ):
153
173
if not isinstance (other , DiffractionObject ):
154
174
return NotImplemented
155
- self_attributes = [key for key in self .__dict__ if not key .startswith ("_" )]
156
- other_attributes = [key for key in other .__dict__ if not key .startswith ("_" )]
175
+ self_attributes = [
176
+ key for key in self .__dict__ if not key .startswith ("_" )
177
+ ]
178
+ other_attributes = [
179
+ key for key in other .__dict__ if not key .startswith ("_" )
180
+ ]
157
181
if not sorted (self_attributes ) == sorted (other_attributes ):
158
182
return False
159
183
for key in self_attributes :
@@ -167,8 +191,13 @@ def __eq__(self, other):
167
191
or not np .isclose (value , other_value , rtol = 1e-5 )
168
192
):
169
193
return False
170
- elif isinstance (value , list ) and all (isinstance (i , np .ndarray ) for i in value ):
171
- if not all (np .allclose (i , j , rtol = 1e-5 ) for i , j in zip (value , other_value )):
194
+ elif isinstance (value , list ) and all (
195
+ isinstance (i , np .ndarray ) for i in value
196
+ ):
197
+ if not all (
198
+ np .allclose (i , j , rtol = 1e-5 )
199
+ for i , j in zip (value , other_value )
200
+ ):
172
201
return False
173
202
else :
174
203
if value != other_value :
@@ -303,7 +332,9 @@ def _check_operation_compatibility(self, other):
303
332
if isinstance (other , DiffractionObject ):
304
333
if self .all_arrays .shape != other .all_arrays .shape :
305
334
raise ValueError (x_values_not_equal_emsg )
306
- if not np .allclose (self .all_arrays [:, [1 , 2 , 3 ]], other .all_arrays [:, [1 , 2 , 3 ]]):
335
+ if not np .allclose (
336
+ self .all_arrays [:, [1 , 2 , 3 ]], other .all_arrays [:, [1 , 2 , 3 ]]
337
+ ):
307
338
raise ValueError (x_values_not_equal_emsg )
308
339
309
340
@property
@@ -381,7 +412,9 @@ def get_array_index(self, xtype, xvalue):
381
412
xtype = self ._input_xtype
382
413
xarray = self .on_xtype (xtype )[0 ]
383
414
if len (xarray ) == 0 :
384
- raise ValueError (f"The '{ xtype } ' array is empty. Please ensure it is initialized." )
415
+ raise ValueError (
416
+ f"The '{ xtype } ' array is empty. Please ensure it is initialized."
417
+ )
385
418
index = (np .abs (xarray - xvalue )).argmin ()
386
419
return index
387
420
@@ -447,7 +480,9 @@ def on_d(self):
447
480
"""
448
481
return [self .all_arrays [:, 3 ], self .all_arrays [:, 0 ]]
449
482
450
- def scale_to (self , target_diff_object , q = None , tth = None , d = None , offset = None ):
483
+ def scale_to (
484
+ self , target_diff_object , q = None , tth = None , d = None , offset = None
485
+ ):
451
486
"""Return a new diffraction object which is the current object but
452
487
rescaled in y to the target.
453
488
@@ -486,7 +521,10 @@ def scale_to(self, target_diff_object, q=None, tth=None, d=None, offset=None):
486
521
if count == 0 :
487
522
q_target_max = max (target_diff_object .on_q ()[1 ])
488
523
q_self_max = max (self .on_q ()[1 ])
489
- scaled_do ._all_arrays [:, 0 ] = scaled_do ._all_arrays [:, 0 ] * q_target_max / q_self_max + offset
524
+ scaled_do ._all_arrays [:, 0 ] = (
525
+ scaled_do ._all_arrays [:, 0 ] * q_target_max / q_self_max
526
+ + offset
527
+ )
490
528
return scaled_do
491
529
492
530
xtype = "q" if q is not None else "tth" if tth is not None else "d"
@@ -497,7 +535,9 @@ def scale_to(self, target_diff_object, q=None, tth=None, d=None, offset=None):
497
535
498
536
xindex_data = (np .abs (data [0 ] - xvalue )).argmin ()
499
537
xindex_target = (np .abs (target [0 ] - xvalue )).argmin ()
500
- scaled_do ._all_arrays [:, 0 ] = data [1 ] * target [1 ][xindex_target ] / data [1 ][xindex_data ] + offset
538
+ scaled_do ._all_arrays [:, 0 ] = (
539
+ data [1 ] * target [1 ][xindex_target ] / data [1 ][xindex_data ] + offset
540
+ )
501
541
return scaled_do
502
542
503
543
def on_xtype (self , xtype ):
@@ -561,12 +601,16 @@ def dump(self, filepath, xtype=None):
561
601
if xtype in QQUANTITIES :
562
602
data_to_save = np .column_stack ((self .on_q ()[0 ], self .on_q ()[1 ]))
563
603
elif xtype in ANGLEQUANTITIES :
564
- data_to_save = np .column_stack ((self .on_tth ()[0 ], self .on_tth ()[1 ]))
604
+ data_to_save = np .column_stack (
605
+ (self .on_tth ()[0 ], self .on_tth ()[1 ])
606
+ )
565
607
elif xtype in DQUANTITIES :
566
608
data_to_save = np .column_stack ((self .on_d ()[0 ], self .on_d ()[1 ]))
567
609
else :
568
610
warnings .warn (_xtype_wmsg (xtype ))
569
- self .metadata .update (get_package_info ("diffpy.utils" , metadata = self .metadata ))
611
+ self .metadata .update (
612
+ get_package_info ("diffpy.utils" , metadata = self .metadata )
613
+ )
570
614
self .metadata ["creation_time" ] = datetime .datetime .now ()
571
615
572
616
with open (filepath , "w" ) as f :
0 commit comments