eval_functions.py¶
hdf eval functions
HdfMapInterpreter
¶
Bases: Interpreter
HdfMap implementation of asteval.Interpreter
Expression is parsed for patterns and loads HDF data before evaluation
m = HdfMap('file.nxs')
ii = HdfMapInterpreter(m, replace_names={}, default='', **kwargs)
out = ii.eval('expression')
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdfmap
|
HdfMap instance (including hdfmap.filename pointing to the HDF file) |
required | |
replace_names
|
dict[str, str]
|
dict of {'variable_name': expression} |
required |
default
|
Any
|
returned if varname not in namespace |
DEFAULT
|
kwargs
|
keyword arguments passed to asteval.Interpreter |
required |
Source code in src/hdfmap/eval_functions.py
build_hdf_path(*args)
¶
Build path from string or bytes arguments '/entry/measurement' = build_hdf_path(b'entry', 'measurement')
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
str | bytes
|
str or bytes arguments |
()
|
Returns:
| Type | Description |
|---|---|
str
|
str hdf path |
Source code in src/hdfmap/eval_functions.py
dataset2data(dataset, index=(), direct_load=False)
¶
Read the data from a h5py Dataset and convert to either datetime, str or squeezed numpy array
- numeric size=1 datasets return float
- numeric size>1 datasets return numpy array
- string/ byte timestamps (size=1) return datetime.datetime object
- string/ byte timestamps (size>1) return array of datetime.datetime
- all other size=1 datasets return string
- all other size>1 datasets return array of string
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Dataset
|
h5py.Dataset containing data |
required |
index
|
int | slice
|
index of array (not used if dataset is string/ bytes type) |
()
|
direct_load
|
loads the data directly without conversion if True |
False
|
Returns:
| Type | Description |
|---|---|
datetime | str | ndarray
|
if data is another numeric object |
Source code in src/hdfmap/eval_functions.py
dataset2str(dataset, index=(), units=False)
¶
Read the data from a h5py Dataset and convert to a representative string
- Strings are given with quotes
- long floats are shorted by attribute 'decimals'
- numeric arrays are summarised as "dtype (shape)"
- string arrays are summarised as "['str0', ...]
- trailing floats within strings are shortened
- if units=True and units available as attribute, the unit will be appended
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
Dataset
|
h5py.Dataset containing data |
required |
index
|
int | slice | tuple[slice, ...]
|
index of array (not used if dataset is string/ bytes type) |
()
|
units
|
bool
|
if True and attribute 'units' available, append this to the result |
False
|
Returns:
| Type | Description |
|---|---|
str
|
string representation of data |
Source code in src/hdfmap/eval_functions.py
dataset_attribute(dataset, attribute)
¶
Return attribute of dataset
eval_hdf(hdf_file, expression, hdf_namespace, data_namespace, replace_names, default=DEFAULT, use_stored_data=False, 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
Additional variables can be added to the evaluation local namespace using data_namespace.
Shorthand variables for expressions can be assigned using replace_names = {'new_name': 'favorite*expression'}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdf_file
|
File
|
h5py.File object |
required |
expression
|
str
|
str expression to be evaluated |
required |
hdf_namespace
|
dict[str, str]
|
dict of {'variable name': '/hdf/dataset/path'} |
required |
data_namespace
|
dict[str, Any]
|
dict of {'variable name': value} (note, this object will be updated) |
required |
replace_names
|
dict[str, str]
|
dict of {'variable_name': expression} |
required |
default
|
Any
|
returned if varname not in namespace |
DEFAULT
|
use_stored_data
|
bool
|
when True, preferentially takes data from data_namespace, rather than loading from file. |
False
|
raise_errors
|
bool
|
raise exceptions if True, otherwise return str error message as result and log the error |
True
|
Returns:
| Type | Description |
|---|---|
Any
|
asteval(expression) |
Source code in src/hdfmap/eval_functions.py
expression_safe_name(string, replace='_')
¶
Returns an expression safe name
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str
|
any string |
required |
replace
|
str
|
str replace special characters with this |
'_'
|
Returns:
| Type | Description |
|---|---|
str
|
string with special characters replaced |
Source code in src/hdfmap/eval_functions.py
extra_hdf_data(hdf_file)
¶
Extract filename, filepath and other additional data fom hdf file
Source code in src/hdfmap/eval_functions.py
find_identifiers(expression)
¶
format_hdf(hdf_file, expression, hdf_namespace, data_namespace, replace_names, default=DEFAULT, use_stored_data=False, 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 |
hdf_namespace
|
dict[str, str]
|
dict of {'variable name': '/hdf/dataset/path'} |
required |
data_namespace
|
dict[str, Any]
|
dict of {'variable name': value} |
required |
replace_names
|
dict[str, str]
|
dict of {'variable_name': expression} |
required |
default
|
Any
|
returned if varname not in namespace |
DEFAULT
|
use_stored_data
|
bool
|
when True, preferentially takes data from data_namespace, rather than loading from file. |
False
|
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/eval_functions.py
generate_identifier(hdf_path)
¶
Generate a valid python identifier from a hdf dataset path or other string - Decodes to ascii - omits '/value' - splits by path separator (/) and takes final element - converts special characters to '' - removes replication of strings separated by '' E.G. /entry/group/motor1 >> "motor1" /entry/group/motor/value >> "motor" /entry/group/subgroup.motor >> "subgroup_motor" motor.motor >> "motor"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdf_path
|
str | bytes
|
str hdf path address |
required |
Returns:
| Type | Description |
|---|---|
str
|
str expression safe name |
Source code in src/hdfmap/eval_functions.py
generate_image_roi_slice(start_i, stop_i, start_j, stop_j, step_i=1, step_j=1)
¶
Generate indexing slice for region of interest (ROI)
Source code in src/hdfmap/eval_functions.py
generate_namespace(hdf_file, hdf_namespace, identifiers=None, default=DEFAULT, data_namespace=None)
¶
Generate namespace dict - create a dictionary linking the name of a dataset to the dataset value
Adds additional values if not in name_path dict: 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 d_name: return dataset object warning: may result in file not closing on completion name@attr: returns attribute of dataset name
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdf_file
|
File
|
h5py.File object |
required |
hdf_namespace
|
dict[str, str]
|
locations of data in hdf file, dict[identifier]='/hdf/dataset/path' |
required |
identifiers
|
list[str] | None
|
list of names to load from hdf_file, if None, use generate all items in name_path |
None
|
default
|
Any
|
any, if varname not in name_path - return default instead |
DEFAULT
|
data_namespace
|
dict[str, Any] | None
|
dict of {'variable name': value} ** note: values will be added to this dict |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict {'name': value, '_name': '/hdf/path'} |
Source code in src/hdfmap/eval_functions.py
is_image(shape, min_dim=3)
¶
prepare_expression(hdf_file, expression, hdf_namespace, data_namespace)
¶
Prepare an expression for evaluation using the namespace of the hdf file Returns the modified expression replacing attribute names and alternates with valid identifiers. Also updates the data_namespace dict with attribute data.
The following patterns are allowed in the expression: - '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
Additional variables can be added to the evaluation local namespace using data_namespace.
Shorthand variables for expressions can be assigned using replace_names = {'new_name': 'favourite*expression'}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdf_file
|
File
|
h5py.File object |
required |
expression
|
str
|
str expression to be evaluated |
required |
hdf_namespace
|
dict[str, str]
|
dict of {'variable name': '/hdf/dataset/path'} |
required |
data_namespace
|
dict[str, Any] | None
|
dict of {'variable name': value} ** note: values will be added to this dict |
required |
Returns:
| Type | Description |
|---|---|
str
|
str expression |
Source code in src/hdfmap/eval_functions.py
prepare_expression_load_data(hdf_file, expression, hdf_namespace, data_namespace, replace_names, default=DEFAULT, use_stored_data=False)
¶
Prepare an expression for evaluation using the namespace of the hdf file Returns the modified expression replacing attribute names and alternates with valid identifiers. Also updates the data_namespace dict with attribute data and data from the hdf file.
The following patterns are allowed in the expression: - '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
Additional variables can be added to the evaluation local namespace using data_namespace.
Shorthand variables for expressions can be assigned using replace_names = {'new_name': 'favourite*expression'}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdf_file
|
File
|
h5py.File object |
required |
expression
|
str
|
str expression to be evaluated |
required |
hdf_namespace
|
dict[str, str]
|
dict of {'variable name': '/hdf/dataset/path'} |
required |
data_namespace
|
dict[str, Any]
|
dict of {'variable name': value} ** note: values will be added to this dict |
required |
replace_names
|
dict[str, str]
|
dict of {'variable_name': expression} |
required |
default
|
Any
|
returned if varname not in namespace |
DEFAULT
|
use_stored_data
|
bool
|
when True, preferentially takes data from data_namespace, rather than loading from file. |
False
|
Returns:
| Type | Description |
|---|---|
|
str expression |
Source code in src/hdfmap/eval_functions.py
round_string_floats(string)
¶
Shorten string by removing long floats
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
string, e.g. '#810002 scan eta 74.89533603616637 76.49533603616636 0.02 pil3_100k 1 roi2' |
required |
Returns:
| Type | Description |
|---|---|
|
shorter string, e.g. '#810002 scan eta 74.895 76.495 0.02 pil3_100k 1 roi2' |