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 | None
|
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
Example: >>> 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.map.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
28 29 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 | |
arrays(*args, units='', default=np.array([np.nan]))
¶
Return Numpy arrays
Source code in mmg_toolbox/nexus/nexus_scan.py
datasets(*args)
¶
Return HDF5 datasets from NeXus file (leaves file in open state)
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
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 | |
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
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)
¶
Return string of namespace information
Source code in mmg_toolbox/nexus/nexus_scan.py
instrument_model()
¶
load_hdf()
¶
metadata_str(expression=None)
¶
Generate metadata string from beamline config
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)
¶
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 |