Class: HdfMap¶
HdfMap object, container for paths of different objects in an HDF file
with h5py.File('file.hdf') as hdf:
map = HdfMap(hdf)
map.get_path('data') -> '/entry/measurement/data'
map['data'] -> '/entry/measurement/data'
with h5py.File('another_file.hdf') as hdf:
data = map.get_data(hdf, 'data')
array = map.get_scannables_array(hdf)
metadata = map.get_metadata(hdf)
out = map.eval(hdf, 'data / 10')
outstr = map.format(hdf, 'the data looks like: {data}')
Objects within the HDF file are separated into Groups and Datasets. Each object has a defined 'path' and 'name' paramater, as well as other attribute:
- path -> '/entry/measurement/data' -> the location of an object within the file
- name -> 'data' -> a path expressed as a simple variable name
Paths are unique location within the file but can be used to identify similar objects in other files Names may not be unique within a file and are generated from the final element of the hdf path.
- When multiple paths produce the same name, the name is overwritten each time, so the last path in the file has priority.
- Names are also stored using the 'local_name' attribute, if it exists
Names of different types of datasets are stored for arrays (size > 0) and values (size 0) Names for scannables relate to all arrays of a particular size A combined list of names is provided where scannables > arrays > values
Attributes¶
- map.groups stores attributes of each group by path
- map.classes stores list of group paths by nx_class
- map.datasets stores attributes of each dataset by path
- map.arrays stores array dataset paths by name
- map.values stores value dataset paths by name
- map.metadata stores value dataset path by altname only
- map.scannables stores array dataset paths with given size, by name, all arrays have the same shape
- map.combined stores array and value paths (arrays overwrite values)
- map.image_data stores dataset paths of image data (arrays with 2+ dimensions or arrays of image files)
E.G.¶
- map.groups = {'/hdf/group': ('class', 'name', {attrs}, [datasets])}
- map.classes = {'class_name': ['/hdf/group1', '/hdf/group2']}
- map.datasets = {'/hdf/group/dataset': ('name', size, shape, {attrs})}
- map.arrays = {'name': '/hdf/group/dataset'}
- map.values = {'name': '/hdf/group/dataset'}
- map.scannables = {'name': '/hdf/group/dataset'}
- map.image_data = {'name': '/hdf/group/dataset'}
Methods¶
- map.populate(h5py.File) -> populates the dictionaries using the given file
- map.generate_scannables(array_size) -> populates scannables namespace with arrays of same size
- map.most_common_size -> returns the most common dataset size > 1
- map.get_attr('name_or_path', 'attr') -> return value of dataset attribute
- map.get_path('name_or_group_or_class') -> returns path of object with name
- map.get_image_path() -> returns default path of detector dataset (or largest dataset)
- map.get_group_path('name_or_path_or_class') -> return path of group with class
- map.get_group_datasets('name_or_path_or_class') -> return list of dataset paths in class
- map.find_groups(*names_or_classes) -> return list of group paths matching given group names or classes
- map.find_paths('string') -> return list of dataset paths containing string
- map.find_names('string') -> return list of dataset names containing string
- map.find_attr('attr_name') -> return list of paths of groups or datasets containing attribute 'attr_name'
- map.add_local(local_variable=value) -> add to the local namespace accessed by eval
- map.add_named_expression(alternate_name='expression') -> add local variables for expressions replaced during eval
File Methods¶
- map.get_metadata(h5py.File) -> returns dict of value datasets
- map.get_scannables(h5py.File) -> returns dict of scannable datasets
- map.get_scannables_array(h5py.File) -> returns numpy array of scannable datasets
- map.get_dataholder(h5py.File) -> returns dict like object with metadata and scannables
- map.get_image(h5py.File, index) -> returns image data (2D float array or str image filename)
- map.get_data(h5py.File, 'name') -> returns data from dataset
- map.get_string(h5py.File, 'name') -> returns string summary of dataset
- map.eval(h5py.File, 'expression') -> returns output of expression
- map.format(h5py.File, 'string {name}') -> returns output of str expression
Source code in src/hdfmap/hdfmap_class.py
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 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 |
|
add_local(**kwargs)
¶
add_named_expression(**kwargs)
¶
all_attrs()
¶
Return dict of all attributes in self.datasets and self.groups
Source code in src/hdfmap/hdfmap_class.py
create_dataset_summary(hdf_file)
¶
create_metadata_list(hdf_file, default=None, name_list=None, line_separator='\n', value_separator='=')
¶
Return a metadata string, using self.get_metadata
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hdf_file
|
File
|
hdf file object |
required |
default
|
Value to return for names not associated with a dataset |
None
|
|
name_list
|
list
|
if available, uses this list of dataset names to generate the metadata list |
None
|
line_separator
|
str
|
str separating each metadata parameter |
'\n'
|
value_separator
|
str
|
str separating name from value |
'='
|
Returns:
Type | Description |
---|---|
str
|
multi-line string |
Source code in src/hdfmap/hdfmap_class.py
create_scannables_table(hdf_file, delimiter=', ', string_spec='', format_spec='f', default_decimals=8)
¶
Return str representation of scannables as a table The table starts with a header row given by names of the scannables. Each row contains the numeric values for each scannable, formated by the given string spec: {value: "string_spec.decimals format_spec"} e.g. {value: "5.8f"} decimals is taken from each scannables "decimals" attribute if it exits, otherwise uses default
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hdf_file
|
File
|
h5py.File object |
required |
delimiter
|
str seperator between each column |
', '
|
|
string_spec
|
str first element of float format specifier - length of string |
''
|
|
format_spec
|
str type element of format specifier - 'f'=float, 'e'=exponential, 'g'=general |
'f'
|
|
default_decimals
|
int default number of decimals given |
8
|
Returns:
Type | Description |
---|---|
str
|
str |
Source code in src/hdfmap/hdfmap_class.py
eval(hdf_file, expression, default=DEFAULT, raise_errors=True)
¶
Evaluate an expression using the namespace of the hdf file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hdf_file
|
File
|
h5py.File object |
required |
expression
|
str
|
str expression to be evaluated |
required |
default
|
returned if varname not in namespace |
DEFAULT
|
|
raise_errors
|
bool
|
raise exceptions if True, otherwise return str error message as result and log the error |
True
|
Returns:
Type | Description |
---|---|
eval(expression) |
Source code in src/hdfmap/hdfmap_class.py
find_attr(attr_name)
¶
Find any dataset or group path with an attribute that contains attr_name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr_name
|
str
|
str name of hdfobj.attr |
required |
Returns:
Type | Description |
---|---|
list[str]
|
list of hdf paths |
Source code in src/hdfmap/hdfmap_class.py
find_datasets(*names_or_classes)
¶
Find datasets that are associated with several names or class names
[paths, ] = m.find_datasets('NXslit', 'x_gap')
Intended for use finding datasets associated with groups with a certain hierarchy
Note that arguments are checked against the dataset namespace first, so if the argument appears in both lists, it will be assumed to be a dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
names_or_classes
|
str
|
dataset names, group names or group class names |
()
|
Returns:
Type | Description |
---|---|
list[str]
|
list of hdf dataset paths |
Source code in src/hdfmap/hdfmap_class.py
find_groups(*names_or_classes)
¶
Find groups that are associated with several names or class names
[paths, ] = m.find_groups('NXslit', 'NXtransformations', 's1')
Intended for use finding groups with a certain hierarchy
Parameters:
Name | Type | Description | Default |
---|---|---|---|
names_or_classes
|
str
|
group names or group class names |
()
|
Returns:
Type | Description |
---|---|
list[str]
|
list of hdf group paths, where all groups are associated with all given names or classes. |
Source code in src/hdfmap/hdfmap_class.py
find_names(string, match_case=False)
¶
Find any dataset names that contain the given string argument, searching names in self.combined
['m1x', 'm1y', ...] = m.find_names('m1')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
str to find in list of datasets |
required |
match_case
|
if True, match must be case-sensitive |
False
|
Returns:
Type | Description |
---|---|
list[str]
|
list of names |
Source code in src/hdfmap/hdfmap_class.py
find_paths(string, name_only=True, whole_word=False)
¶
Find any dataset paths that contain the given string argument
[paths, ] = m.find_paths('en') # finds all datasets with name including 'en'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
str to find in list of datasets |
required |
name_only
|
if True, search only the name of the dataset, not the full path |
True
|
|
whole_word
|
if True, search only for whole-word names (case in-sensitive) |
False
|
Returns:
Type | Description |
---|---|
list[str]
|
list of hdf paths |
Source code in src/hdfmap/hdfmap_class.py
first_last_scannables(first_names=(), last_names=())
¶
Returns default names from scannables output first_names returns dict of N names, where N is the number of dimensions in scannable shape if fewer axes_names are provided than required, use the first items of scannables instead output signal_names returns the last dict item in the list of scannables + signal_names
Parameters:
Name | Type | Description | Default |
---|---|---|---|
first_names
|
list[str]
|
list of names of plottable axes in scannables |
()
|
last_names
|
list[str]
|
list of names of plottable values in scannables |
()
|
Returns:
Type | Description |
---|---|
tuple[dict[str, str], dict[str, str]]
|
path}, {last_names: path} |
Source code in src/hdfmap/hdfmap_class.py
format_hdf(hdf_file, expression, default=DEFAULT, raise_errors=True)
¶
Evaluate a formatted string expression using the namespace of the hdf file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hdf_file
|
File
|
h5py.File object |
required |
expression
|
str
|
str expression using {name} format specifiers |
required |
default
|
returned if varname not in namespace |
DEFAULT
|
|
raise_errors
|
bool
|
raise exceptions if True, otherwise return str error message as result and log the error |
True
|
Returns:
Type | Description |
---|---|
str
|
eval_hdf(f"expression") |
Source code in src/hdfmap/hdfmap_class.py
generate_combined()
¶
Finalise the mapped namespace by combining dataset names
Source code in src/hdfmap/hdfmap_class.py
generate_scannables(array_size)
¶
Populate self.scannables field with datasets size that match array_size
Source code in src/hdfmap/hdfmap_class.py
generate_scannables_from_group(hdf_group, group_path=None, dataset_names=None)
¶
Generate scannables list from a specific group, using the first item to define array size
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hdf_group
|
Group
|
h5py.Group |
required |
group_path
|
str
|
str path of group hdf_group if hdf_group.name is incorrect |
None
|
dataset_names
|
list[str]
|
list of names of group sub-entries to use (use all if None) |
None
|
Source code in src/hdfmap/hdfmap_class.py
generate_scannables_from_names(names)
¶
Generate scannables list from a set of dataset names, using the first item to define array size
Source code in src/hdfmap/hdfmap_class.py
get_attr(name_or_path, attr_label, default='')
¶
Return named attribute from dataset or group, or default
Source code in src/hdfmap/hdfmap_class.py
get_attrs(name_or_path)
¶
Return attributes of dataset or group
Source code in src/hdfmap/hdfmap_class.py
get_data(hdf_file, name_or_path, index=(), default=None, direct_load=False)
¶
Return data from dataset in file, converted into either datetime, str or squeezed numpy.array objects See hdfmap.eval_functions.dataset2data for more information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hdf_file
|
File
|
hdf file object |
required |
name_or_path
|
str
|
str name or path pointing to dataset in hdf file |
required |
index
|
index or slice of data in hdf file |
()
|
|
default
|
value to return if name not found in hdf file |
None
|
|
direct_load
|
return str, datetime or squeezed array if False, otherwise load data directly |
False
|
Returns:
Type | Description |
---|---|
dataset2data(dataset) -> datetime, str or squeezed array as required. |
Source code in src/hdfmap/hdfmap_class.py
get_dataholder(hdf_file, flatten_scannables=False)
¶
Return DataHolder object - a simple replication of scisoftpy.dictutils.DataHolder Also known as DLS dat format. dataholder.scannable -> array dataholder.metadata.value -> metadata dataholder['scannable'] -> array dataholder.metadata['value'] -> metadata
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hdf_file
|
File
|
h5py.File object |
required |
flatten_scannables
|
bool
|
bool, it True the scannables will be flattened arrays |
False
|
Returns:
Type | Description |
---|---|
DataHolder
|
data_object (similar to dict) |
Source code in src/hdfmap/hdfmap_class.py
get_group_classes(name_or_path)
¶
Return list of class names associated with a group or parent group of dataset
Source code in src/hdfmap/hdfmap_class.py
get_group_dataset_path(group_name, dataset_name)
¶
Return path of dataset defined by group and dataset name/attribute
Source code in src/hdfmap/hdfmap_class.py
get_group_datasets(name_or_path)
¶
Find the path associate with the given name and return all datasets in that group
Source code in src/hdfmap/hdfmap_class.py
get_group_path(name_or_path)
¶
Return group path of object in HdfMap
Source code in src/hdfmap/hdfmap_class.py
get_image(hdf_file, index=None)
¶
Get image data from file, using default image path - If the image path points to a numeric 2+D dataset, returns dataset[index, :, :] -> ndarray - If the image path points to a string dataset, returns dataset[index] -> '/path/to/image.tiff'
Image filenames may be relative to the location of the current file (this is not checked)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hdf_file
|
File
|
hdf file object |
required |
index
|
int | tuple | slice | None
|
(slice,) or None to take the middle image |
None
|
Returns:
Type | Description |
---|---|
ndarray | None
|
2D numpy array of image, or string file path of image |
Source code in src/hdfmap/hdfmap_class.py
get_image_index(index)
¶
get_image_path()
¶
Return HDF path of first dataset in self.image_data
get_image_shape()
¶
Return the scan shape of the detector dataset
get_metadata(hdf_file, default=None, direct_load=False, name_list=None, string_output=False)
¶
Return metadata dict from file, loading data for each item in the metadata list The metadata list is taken from name_list, otherwise self.metadata or self.values
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hdf_file
|
File
|
hdf file object |
required |
default
|
Value to return for names not associated with a dataset |
None
|
|
direct_load
|
if True, loads data from hdf file directory, without conversion |
False
|
|
name_list
|
list
|
if available, uses this list of dataset names to generate the metadata list |
None
|
string_output
|
if True, returns string summary of each value |
False
|
Returns:
Type | Description |
---|---|
dict
|
{name: value} |
Source code in src/hdfmap/hdfmap_class.py
get_path(name_or_path)
¶
Return hdf path of object in HdfMap
Source code in src/hdfmap/hdfmap_class.py
get_scannables(hdf_file, flatten=False, numeric_only=False)
¶
Return scannables from file (values associated with hdfmap.scannables)
Source code in src/hdfmap/hdfmap_class.py
get_scannables_array(hdf_file, return_structured_array=False)
¶
Return 2D array of all numeric scannables in file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hdf_file
|
File
|
h5py.File object |
required |
return_structured_array
|
bool, if True, return a Numpy structured array with column headers |
False
|
Returns:
Type | Description |
---|---|
ndarray
|
numpy array with a row for each scannable, shape: (no_scannables, flattened_length) |
Source code in src/hdfmap/hdfmap_class.py
get_string(hdf_file, name_or_path, index=(), default='', units=False)
¶
Return data from dataset in file, converted into string summary of data See hdfmap.eval_functions.dataset2str for more information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hdf_file
|
File
|
hdf file object |
required |
name_or_path
|
str
|
str name or path pointing to dataset in hdf file |
required |
index
|
index or slice of data in hdf file |
()
|
|
default
|
value to return if name not found in hdf file |
''
|
|
units
|
if True and attribute 'units' available, append this to the result |
False
|
Returns:
Type | Description |
---|---|
str
|
dataset2str(dataset) -> str |
Source code in src/hdfmap/hdfmap_class.py
info_classes()
¶
info_data(hdf_file)
¶
Return string showing metadata values associated with names
Source code in src/hdfmap/hdfmap_class.py
info_datasets()
¶
info_groups()
¶
Return str info on groups
Source code in src/hdfmap/hdfmap_class.py
info_names(arrays=False, values=False, combined=False, metadata=False, scannables=False, image_data=False)
¶
Return str info for different namespaces
Source code in src/hdfmap/hdfmap_class.py
load_hdf(filename=None, name_or_path=None, **kwargs)
¶
Load hdf file or hdf dataset in open state
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename
|
str | None
|
str filename of hdf file, or None to use self.filename |
None
|
name_or_path
|
str
|
if given, returns the dataset |
None
|
kwargs
|
additional key-word arguments to pass to h5py.File(...) |
{}
|
Returns:
Type | Description |
---|---|
File | Dataset
|
h5py.File object or h5py.dataset object if dataset name given |
Source code in src/hdfmap/hdfmap_class.py
most_common_shape()
¶
Return most common non-singular array shape
Source code in src/hdfmap/hdfmap_class.py
most_common_size()
¶
Return most common array size > 1
populate(hdf_file)
¶
Populate all datasets from file
Source code in src/hdfmap/hdfmap_class.py
scannables_length()
¶
Return the length of the first axis of scannables array
scannables_shape()
¶
Return the shape of the first axis of scannables array
set_image_path(name_or_path)
¶
Set the default image path, used by get_image