Class: HdfLoader¶
HDF Loader contains the filename and hdfmap for a HDF file, the hdfmap contains all the dataset paths and a namespace, allowing data to be called from the file using variable names, loading only the required datasets for each operation.
E.G.¶
hdf = HdfLoader('file.hdf')
[data1, data2] = hdf.get_data(*['dataset_name_1', 'dataset_name_2'])
data = hdf.eval('dataset_name_1 * 100 + 2')
string = hdf.format('my data is {dataset_name_1:.2f}')
print(hdf.summary())
Source code in src/hdfmap/reloader_class.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 | |
add_local(**kwargs)
¶
eval(expression, default=DEFAULT, prefer_local=None, raise_errors=True)
¶
Evaluate an expression using the namespace of the hdf file
The following patterns are allowed: - 'filename': str, name of hdf_file - 'filepath': str, full path of hdf_file - 'name': str hdf path of name - '__name': str internal name of name (e.g. for 'axes') - 'sname': string representation of dataset (includes units if available) - 'd_name': return dataset object. warning: may result in file not closing on completion - 'name@attr': returns attribute of dataset name - 'name?(default)': returns default if name doesn't exist - '(name1|name2|name3)': returns the first available of the names - '(name1|name2?(default))': returns the first available name or default
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression
|
str
|
str expression to be evaluated |
required |
default
|
returned if varname not in namespace |
DEFAULT
|
|
prefer_local
|
bool | None
|
if True, uses values in local_data first if available |
None
|
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/reloader_class.py
find_hdf_paths(string, name_only=True, whole_word=False)
¶
Find any dataset paths that contain the given string argument
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str
|
str to find in list of datasets |
required |
name_only
|
bool
|
if True, search only the name of the dataset, not the full path |
True
|
whole_word
|
bool
|
if True, search only for case in-sensitive name |
False
|
Returns:
| Type | Description |
|---|---|
list[str]
|
list of hdf paths |
Source code in src/hdfmap/reloader_class.py
find_names(string)
¶
Find any dataset names that contain the given string argument, searching names in self.combined
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str
|
str to find in list of datasets |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
list of names |
Source code in src/hdfmap/reloader_class.py
format(expression, default=DEFAULT, prefer_local=None, raise_errors=True)
¶
Evaluate a formatted string expression using the namespace of the hdf file Identifiers from the namespace can be called inside {} as a formatted f-string.
E.G. expression = '{scan_command} E={mean(incident_energy):.2f}' output = scan.format(expression)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression
|
str
|
str expression using {name} format specifiers |
required |
default
|
returned if varname not in namespace |
DEFAULT
|
|
prefer_local
|
bool | None
|
if True, uses values in local_data first if available |
None
|
raise_errors
|
bool
|
raise exceptions if True, otherwise return str error message |
True
|
Returns:
| Type | Description |
|---|---|
str
|
eval_hdf(f"expression") |
Source code in src/hdfmap/reloader_class.py
get_data(*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 |
|---|---|---|---|
name_or_path
|
str name or path pointing to dataset in hdf file |
()
|
|
index
|
slice
|
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/reloader_class.py
get_hdf_path(name_or_path)
¶
get_image(index=None)
¶
Get image data from file, using default image path
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
slice
|
(slice,) or None to take the middle image |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
numpy array of image |
Source code in src/hdfmap/reloader_class.py
get_scannables()
¶
get_string(*name_or_path, index=(), default='', units=False)
¶
Return data from dataset in file, converted into summary string See hdfmap.eval_functions.dataset2data for more information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name_or_path
|
str name or path pointing to dataset in hdf file |
()
|
|
index
|
slice
|
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 |
|---|---|
|
dataset2str(dataset) -> str |
Source code in src/hdfmap/reloader_class.py
live_mode(live_mode=True)
¶
Activate the option to reload data from the file each time, rather than from local data
self.eval('cmd') -> default will load 'cmd' from local storage if available, or from the file self.live_mode() -> self.eval('cmd') will return 'cmd' from the file using hdfmap self.live_mode(False) -> returns to default behavior