nexus_scan¶
NeXus Scan Classes
NexusScan - NeXus Scan class, lazy loader of scan files NexusDataHolder - Loads scan data and meta data into attributes
NexusDataHolder
¶
Bases: DataHolder, NexusScan
Nexus data holder class - Automatically reads scannable and metadata from file - acts like the old .dat DataHolder class - has additional functions to read data from NeXus file
Example: scan = NexusDataHolder('12345.nxs') scan.eta -> returns array scan.metadata.metadata -> returns value scan('signal') -> evaluate expression
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
path to Nexus file |
required |
hdf_map
|
NexusMap | None
|
NexusMap object or None to generate |
None
|
flatten_scannables
|
bool
|
if True, flattens all scannable arrays to 1D |
True
|
Source code in mmg_toolbox/nexus/nexus_scan.py
NexusScan
¶
Bases: NexusLoader
Light-weight NeXus file reader
>>> scan = NexusScan('scan.nxs')
>>> scan('scan_command')
'scan x ...'
>>> x, y = scan('axes, signal / monitor')
>>> scan.plot() # default plot
>>> scan.plot.image() # other plot options
>>> result = scan.fit.multi_peak_fit()
>>> print(scan) # print scan default metadata
>>> print(scan.info()) # print scan namespace
>>> scan.add_roi('name', ...) # add ROI to namespace
>>> scan.image(0) # return first detector image as array
>>> scan.volume() # return image stack
>>> data = scan.get_plot_data() # return dict of plot data
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nxs_filename
|
str
|
path to nexus file |
required |
hdf_map
|
NexusMap | None
|
NexusMap object or None |
None
|
config
|
dict | None
|
configuration dict |
None
|
Source code in mmg_toolbox/nexus/nexus_scan.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 | |
add_peak_roi(name, wid_i=30, wid_j=30, image_name='IMAGE', rgn_cen_i=None, rgn_cen_j=None, rgn_wid_i=None, rgn_wid_j=None)
¶
Add an image ROI (region of interest) to the named expressions. The ROI created is centred around the largest pixel in a sub-region.
The ROI operates on the default IMAGE dataset, loading only the required region from the file.
The following expressions will be added, for use in self.eval etc. name -> returns the whole ROI array as a HDF5 dataset name_total -> returns the sum of each image in the ROI array name_max -> returns the max of each image in the ROI array name_min -> returns the min of each image in the ROI array name_mean -> returns the mean of each image in the ROI array name_bkg -> returns the background ROI array (area around ROI) name_rmbkg -> returns the total with background subtracted name_box -> returns the pixel positions of the ROI corners name_bkg_box -> returns the pixel positions of the background ROI name_rgn_box -> returns pixel positions of Peak search region name_pos -> returns i,j,k position of the largest pixel in the dataset
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
string name of the ROI |
required |
wid_i
|
int
|
full width along first dimension, in pixels |
30
|
wid_j
|
int
|
full width along second dimension, in pixels |
30
|
image_name
|
str
|
string name of the image |
'IMAGE'
|
rgn_cen_i
|
int | str | None
|
centre of the region in which to look for the largest pixel |
None
|
rgn_cen_j
|
int | str | None
|
centre of the region in which to look for the largest pixel |
None
|
rgn_wid_i
|
int | None
|
width of the region in which to look for the largest pixel |
None
|
rgn_wid_j
|
int | None
|
width of the region in which to look for the largest pixel |
None
|
Returns:
| Type | Description |
|---|---|
|
None |
Source code in mmg_toolbox/nexus/nexus_scan.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | |
add_roi(name, cen_i, cen_j, wid_i=30, wid_j=30, image_name='IMAGE')
¶
Add an image ROI (region of interest) to the named expressions The ROI operates on the default IMAGE dataset, loading only the required region from the file. The following expressions will be added, for use in self.eval etc. name -> returns the whole ROI array as a HDF5 dataset name_total -> returns the sum of each image in the ROI array name_max -> returns the max of each image in the ROI array name_min -> returns the min of each image in the ROI array name_mean -> returns the mean of each image in the ROI array name_bkg -> returns the background ROI array (area around ROI) name_rmbkg -> returns the total with background subtracted name_box -> returns the pixel positions of the ROI corners name_bkg_box -> returns the pixel positions of the background ROI
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
string name of the ROI |
required |
cen_i
|
int | str
|
central pixel index along first dimension, can be callable string |
required |
cen_j
|
int | str
|
central pixel index along second dimension, can be callable string |
required |
wid_i
|
int
|
full width along first dimension, in pixels |
30
|
wid_j
|
int
|
full width along second dimension, in pixels |
30
|
image_name
|
str
|
string name of the image |
'IMAGE'
|
Source code in mmg_toolbox/nexus/nexus_scan.py
arrays(*args, units='', default=np.array([np.nan]))
¶
Return Numpy arrays
Source code in mmg_toolbox/nexus/nexus_scan.py
copy_file(new_filename, merge_links=False, copy_linked_files=False, merge_compression='gzip')
¶
Copy NeXus file to a new location, also copy the linked files
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_filename
|
str
|
new filename |
required |
merge_links
|
bool
|
if True, copies any data from externally linked data files into the new file |
False
|
copy_linked_files
|
bool
|
if True, copies linked files to keep the same relative path. |
False
|
merge_compression
|
str
|
compression algorithm for merged datasets, default is 'gzip' |
'gzip'
|
Returns:
| Type | Description |
|---|---|
|
None |
Source code in mmg_toolbox/nexus/nexus_scan.py
datasets(*args)
¶
Return HDF5 datasets from NeXus file (leaves file in open state)
get_max_index(expression='IMAGE')
¶
Return the index and value of the maximum pixel in a detector image or volume
Source code in mmg_toolbox/nexus/nexus_scan.py
get_plot_axis(axis_name, reduce_shape=True, flatten=False)
¶
Return plot axis data and label for given axis name
data, label = scan.get_plot_axis('axes', flatten=True)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axis_name
|
str
|
axis name as given in self.map |
required |
reduce_shape
|
bool
|
reduces shape (summing additional axes) of >2D arrays to self.map.scannables_shape |
True
|
flatten
|
bool
|
flattens output if True |
False
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray, str]
|
(data, label) tuple |
Source code in mmg_toolbox/nexus/nexus_scan.py
get_plot_data(x_axis=None, *y_axis, z_axis=None)
¶
Return dict of plottable data
data = scan.get_plot_data('axes', 'signal')
plt.plot(data['x'], data['y'])
plt.xlabel(data['xlabel'])
plt.ylabel(data['ylabel'])
plt.title(data['title'])
plt.legend(data['legend'])
:param x_axis: axis name or expression as given in self.map
:param y_axis: axis name or expression as given in self.map
:param z_axis: axis name or expression as given in self.map
:returns: {
'xlabel': str label of first axes
'ylabel': str label of first signal
'xdata': flattened array of first axes
'ydata': flattened array of first signal
'axes_names': list of axes names,
'signal_names': list of signal + auxiliary signal names,
'axes_data': list of ND arrays of data for axes,
'signal_data': list of ND array of data for signal + auxiliary signals,
'axes_labels': list of axes labels as 'name [units]',
'signal_labels': list of signal labels,
'data': dict of all scannables axes,
'title': str title as 'filename
NXtitle' if dataset is a 2D grid scan, additional rows: 'grid_xlabel': str label of grid x-axis 'grid_ylabel': str label of grid y-axis 'grid_label': str label of height or colour 'grid_xdata': 2D array of x-coordinates 'grid_ydata': 2D array of y-coordinates 'grid_data': 2D array of height or colour }
Source code in mmg_toolbox/nexus/nexus_scan.py
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 | |
get_scan_time()
¶
Return array of datetimes for each scan point
Source code in mmg_toolbox/nexus/nexus_scan.py
hdf_find(*field_or_class, find_all=False)
¶
Find datasets and groups within hdf file
HdfDataset = scan.hdf_find('NXentry', ['NXdata', 'measurement'], 'signal')
Warning: HDF file stays in open state while Dataset or Group objects exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field_or_class
|
str | list[str]
|
names to search for, in hierarchical order. Lists treated as OR |
()
|
find_all
|
bool
|
whether to return all datasets or only the first match |
False
|
Returns:
| Type | Description |
|---|---|
Dataset | Group | list[Dataset | Group]
|
matching Dataset or Group, or list of matching Datasets or Groups |
Source code in mmg_toolbox/nexus/nexus_scan.py
hdf_tree_string(group='/', all_links=True, attributes=True)
¶
Generate string of the hdf file structure, similar to h5ls. Uses h5py.visititems
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
all_links
|
bool
|
bool, if True, also show links |
True
|
group
|
str
|
only display tree structure of this group (default root) |
'/'
|
attributes
|
bool
|
if True, display the attributes of groups and datasets |
True
|
Returns:
| Type | Description |
|---|---|
str
|
str |
Source code in mmg_toolbox/nexus/nexus_scan.py
image(index=None)
¶
Return image or selection from default detector
im = scan.image(0) # First image
im = scan.image(None) # middle image
im = scan.image('sum') # sum of all images
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int | str | None
|
point in the scan to show the image, 'sum' gives sum over all images, None gives the middle image |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
(n,m) array |
Source code in mmg_toolbox/nexus/nexus_scan.py
image_background(index=(), n_bins=100)
¶
Return the modal value of the detector image, which usually gives the background value.
The modal value is determined by histograming the image (or image stack) and taking the value of the largest bin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int | tuple | slice | str | None
|
index of image to return, use () for full image stack. |
()
|
n_bins
|
int
|
number of histogram bins |
100
|
Returns:
| Type | Description |
|---|---|
ndarray
|
modal value or values per image |
Source code in mmg_toolbox/nexus/nexus_scan.py
info(arrays=False, values=False, combined=False, metadata=False, scannables=True, image_data=True, local=False, alternate=True, rois=False)
¶
Return string of namespace information
Source code in mmg_toolbox/nexus/nexus_scan.py
instrument_model()
¶
load_hdf()
¶
load_local_data()
¶
If the scan file contains previously saved NexusScan information, the local data and config will be loaded.
Source code in mmg_toolbox/nexus/nexus_scan.py
metadata_str(expression=None)
¶
Generate metadata string from beamline config
replace_default_names(expression)
¶
Replace 'axes', 'signal' and 'IMAGE' in expression with correct values
rois(append='_total')
¶
Return ROI expressions available in scan namespace
rois = [scan.eval(roi) for roi in scan.rois('_total')]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
append
|
str
|
str to append to each ROI name, e.g. '_total', '_max', '_min', '_mean' |
'_total'
|
Returns:
| Type | Description |
|---|---|
list[str]
|
list of ROI names that can be used in eval. |
Source code in mmg_toolbox/nexus/nexus_scan.py
save(filename)
¶
Save object as HDF5 file
Source code in mmg_toolbox/nexus/nexus_scan.py
save_csv(filename, *scannables)
¶
Save axes and signal as CSV file
Source code in mmg_toolbox/nexus/nexus_scan.py
save_nxdata(root, name='data', x_axis=None, *y_axis, z_axis=None, default=False)
¶
Save scan data as NXdata group
Source code in mmg_toolbox/nexus/nexus_scan.py
start_end_duration()
¶
Return start and end times of scan, plus duration as datetime and timedelta objects
Source code in mmg_toolbox/nexus/nexus_scan.py
strings(*args, units=False)
¶
table(delimiter=', ', string_spec='', format_spec='f', default_decimals=8)
¶
Return data table
Source code in mmg_toolbox/nexus/nexus_scan.py
times(*args, default=None)
¶
Return datetime object
Source code in mmg_toolbox/nexus/nexus_scan.py
values(*args, value_func=np.mean, units='', default=np.array(np.nan))
¶
Return float values
Source code in mmg_toolbox/nexus/nexus_scan.py
volume()
¶
xas_spectra(sample_name=None, element_edge=None, mode='all', dls_loader=False)
¶
Load XAS Spectra from the scan file
>>> spectra = scan.xas_spectra()
>>> spectra = spectra.remove_background('slope')
>>> spectra.plot()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample_name
|
str | None
|
sample name, e.g. 'sample1' or None to load from NeXus file |
None
|
element_edge
|
str | None
|
element edge, e.g. 'FeL3' or None to determine from energy range |
None
|
mode
|
str | list[str]
|
detector values to load, 'all', 'default' or e.g. 'tey', 'tfy' as specified in file |
'all'
|
dls_loader
|
bool
|
bool, if True uses explicit loading of metadata from DLS MMG beamlines |
False
|
Returns:
| Type | Description |
|---|---|
SpectraContainer
|
SpectraContainer |
Source code in mmg_toolbox/nexus/nexus_scan.py
load_nexus_scan(filename)
¶
Loads a NexusScan object from a NeXus file, if it includes the NexusScan entry