lyse.Run
- class lyse.Run(h5_path, no_write=False)[source]
Bases:
object
A class for saving/retrieving data to/from a shot’s hdf5 file.
This class implements methods that allow the user to retrieve data from a shot’s hdf5 file such as images, traces, and the values of globals. It also provides methods for saving and retrieving results from analysis.
- Parameters:
Methods
__init__
(h5_path[, no_write])Return all existing images labels in the h5 file.
get_attrs
(group)Returns all attributes of the specified group as a dictionary.
get_globals
([group])Get globals from the shot.
Get the expansion type of each global.
get_globals_raw
([group])Get the raw global values from the shot.
get_image
(orientation, label, image)Get previously saved image from the h5 file.
get_image_attributes
(orientation)Return the attributes of a saved orientation image group.
get_images
(orientation, label, *images)Get multiple saved images from orientation and label.
get_images_dict
(orientation, label, *images)Get multiple saved images from orientation and label.
get_result
(group, name)Retrieve result from prior calculation.
get_result_array
(group, name)Returns saved results data.
get_result_arrays
(group, *names)Retrieve multiple result arrays from the same group.
get_results
(group, *names)Return multiple results from the same group.
get_trace
(name[, raw_data])Return the saved data trace
name
.get_traces
(*names)Retrieve multiple data traces.
get_units
([group])Get the units of globals.
get_wait
(name)Returns the wait parameters: label, timeout, duration, and time out status.
Returns the parameters of all waits in the experiment.
globals_diff
(other_run[, group])Take a diff between this run and another run.
Get names of all the globals groups.
open
(mode)Context manager to open the Run's h5 file for successive reads/writes.
save_result
(name, value[, group, overwrite])Save a result to the hdf5 file.
save_result_array
(name, data[, group, ...])Save an array of data to the hdf5 h5 file.
save_result_arrays
(*args, **kwargs)Save multiple result arrays.
save_results
(*args, **kwargs)Save multiple results to the hdf5 file.
save_results_dict
(results_dict[, uncertainties])Save results dictionary.
set_group
(groupname)Set the default hdf5 file group for saving results.
Return a list of all saved data traces in Run.
Attributes
The group in the hdf5 file in which results are saved by default.
opened h5py file handle for the shot
The value provided for
h5_path
during instantiation.The value provided for
no_write
during instantiation.- get_all_image_labels()[source]
Return all existing images labels in the h5 file.
- Returns:
Dictionary of the form
{orientation:[label1,label2]}
- Return type:
- get_globals_expansion()[source]
Get the expansion type of each global.
This will skip global variables that do not have an expansion.
- get_image(orientation, label, image)[source]
Get previously saved image from the h5 file.
h5 path to saved image is
/images/orientation/label/image
- Parameters:
- Raises:
Exception – If the image or paths do not exist.
- Returns:
2-D image array.
- Return type:
- get_image_attributes(orientation)[source]
Return the attributes of a saved orientation image group.
- get_images(orientation, label, *images)[source]
Get multiple saved images from orientation and label.
Iteratively calls
self.get_image(orientation,label,image)
for each image argument.- Parameters:
- Returns:
List of 2-D images.
- Return type:
- get_images_dict(orientation, label, *images)[source]
Get multiple saved images from orientation and label.
Iteratively calls
self.get_image(orientation,label,image)
for each image argument.- Parameters:
- Returns:
Dictionary of 2-D images.
- Return type:
- get_result(group, name)[source]
Retrieve result from prior calculation.
- Parameters:
- Raises:
Exception – If
group
orname
do not already exist.- Returns:
Result with appropriate type, as determined by
labscript_utils.properties.get_attribute
.
- get_result_array(group, name)[source]
Returns saved results data.
- Parameters:
- Raises:
Exception – If
group
orname
do not already exist.- Returns:
Numpy array of the saved data.
- Return type:
- get_result_arrays(group, *names)[source]
Retrieve multiple result arrays from the same group.
Iteratively calls
self.get_result_array(group,name)
with default arguments.
- get_results(group, *names)[source]
Return multiple results from the same group.
Iteratively calls get_result(group,name) for each name provided.
- Parameters:
- Returns:
List of the results, in the same order as specified by names. If
names
does not preserve order, return order is not guaranteed.- Return type:
- get_trace(name, raw_data=False)[source]
Return the saved data trace
name
.- Parameters:
- Raises:
Exception – If
name
trace does not exist.- Returns:
Returns 2-D timetrace of times
't'
and values'values'
.- Return type:
- get_units(group=None)[source]
Get the units of globals.
This method retrieves the values in the “Units” column of runmanager for this shot. The values are returned in a dictionary where the keys are the names of globals and the values are the corresponding units.
- Parameters:
group (str, optional) – The name of the globals group for which the units will be retrieved. Globals and units from other globals groups will not be included in the returned dictionary. If set to
None
then all globals from all globals groups will be returned. Ifgroup
is set to a value that isn’t the name of a globals group, then an empty dictionary will be returned, but no error will be raised. Defaults toNone
.- Returns:
A dictionary in which each key is a string giving the name of a global, and each value is a string specifying the corresponding value in the “Units” column of runmanager. An empty dictionary will be returned if
group
is set to a value that isn’t the name of a globals group.- Return type:
- get_waits()[source]
Returns the parameters of all waits in the experiment.
- Raises:
Exception – If the experiment has no waits.
- Returns:
Returns 2D structured numpy array of the waits and their parameters.
- Return type:
- globals_diff(other_run, group=None)[source]
Take a diff between this run and another run.
This calls
globals_diff(self, other_run, group)
.
- globals_groups()[source]
Get names of all the globals groups.
- Returns:
List of global group names.
- Return type:
- property group
The group in the hdf5 file in which results are saved by default.
When a
Run
instance is created from within a lyse singleshot or multishot routine,group
will be set to the name of the running routine. If created from outside a lyse script it will be set toNone
. To change the default group for saving results, use theset_group()
method. Note that ifself.group
isNone
and no value is provided for the optionalgroup
argument used by thesave...()
methods, aValueError
will be raised.Attempting to directly set
self.group
’s value will automatically callself.set_group()
.- Type:
- open(mode)[source]
Context manager to open the Run’s h5 file for successive reads/writes.
Supports all h5 modes, though only
'r'
and'r+'
/'a'
are typically used within lyse itself.- Parameters:
mode (str) – which
h5py.File
mode to open the h5 file with. Must be ‘r’, ‘a’, ‘r+’, ‘w’, ‘w-’, or ‘x’. Lyse typically only uses ‘r’ and ‘r+’.- Yields:
Run
– Handle to openedRun
object.- Raises:
PermissionError – If the Run is set as read-only but a write mode is requested
Examples
Trivial example that selectively opens the file during analysis. For better performance, it would be better to combine these two openings into one.
>>> from lyse import * >>> shot = Run(path) >>> with shot.open('r'): >>> # shot processing that requires reads/writes >>> _, vals = shot.get_trace('my_trace') >>> with shot.open('r+'): >>> results = vals**2 >>> shot.save_result_array('my_result', results) >>> _, vals2 = shot.get_trace('my_other_trace') >>> shot.save_result('my_result2', vals2.mean()) >>> # Shot processing that doesn't require h5 reads/writes >>> print(f'Max of results is {results.max():.3f}') Max of results is 5.003
Open and create the shot handle for the whole analysis in a single line.
>>> from lyse import * >>> with Run(path).open('r+') as shot: >>> # shot processing that requires reads/writes >>> t, vals = shot.get_trace('my_trace') >>> results = vals**2 >>> shot.save_result_array('my_result', results) >>> # Shot processing that doesn't require h5 reads/writes >>> # could be outside the context >>> print(f'Max of results is {results.max():.3f}') Max of results is 5.003
- save_result(name, value, group=None, overwrite=True)[source]
Save a result to the hdf5 file.
With the default argument values this method saves to
self.group
in the'/results'
group and overwrites any existing value. Note that the result is saved as an attribute and overwriting attributes causes hdf5 file size bloat.- Parameters:
name (str) – The name of the result. This will be the name of the attribute added to the hdf5 file’s group.
value (any) – The value of the result, which will be saved as the value of the hdf5 group’s attribute set by
name
. However note that when saving large arrays, it is better to use theself.save_result_array()
method which will store the results as a dataset in the hdf5 file.group (str, optional) – The group in the hdf5 file to which the result will be saved as an attribute. If set to
None
, then the result will be saved toself.group
in'/results'
. Note that if a value is passed forgroup
here then it will NOT have'/result'
prepended to it which allows the caller to save results anywhere in the hdf5 file. This is in contrast to using the default group set withself.set_group()
; when the default group is set with that method it WILL have'/results'
prepended to it when saving results. Defaults toNone
.overwrite (bool, optional) – Sets whether or not to overwrite the previous value if the attribute already exists. If set to
False
and the attribute already exists, aPermissionError
is raised. Defaults toTrue
.
- Raises:
PermissionError – A
PermissionError
is raised ifself.no_write
isTrue
because saving the result would edit the file.ValueError – A
ValueError
is raised ifself.group
isNone
and no value is provided forgroup
because the method then doesn’t know where to save the result.PermissionError – A
PermissionError
is raised if an attribute with namename
already exists butoverwrite
is set toFalse
.
- save_result_array(name, data, group=None, overwrite=True, keep_attrs=False, **kwargs)[source]
Save an array of data to the hdf5 h5 file.
With the default argument values this method saves to
self.group
in the'/results'
group and overwrites any existing value without keeping the dataset’s previous attributes. Additional keyword arguments are passed directly toh5py.Group.create_dataset
.- Parameters:
name (str) – The name of the result. This will be the name of the dataset added to the hdf5 file.
data (
numpy.array
) – The data to save to the hdf5 file.group (str, optional) – The group in the hdf5 file in which the result will be saved as a dataset. If set to
None
, then the result will be saved inself.group
in'/results'
. Note that if a value is passed forgroup
here then it will NOT have'/result'
prepended to it which allows the caller to save results anywhere in the hdf5 file. This is in contrast to using the default group set withself.set_group()
; when the default group is set with that method it WILL have'/results'
prepended to it when saving results. Defaults toNone
..overwrite (bool, optional) – Sets whether or not to overwrite the previous value if the dataset already exists. If set to
False
and the dataset already exists, aPermissionError
is raised. Defaults toTrue
.keep_attrs (bool, optional) – Whether or not to keep the dataset’s attributes when overwriting it, i.e. if the dataset already existed. Defaults to
False
.**kwargs – Optional kwargs passed directly to h5_file.create_dataset()
- Raises:
PermissionError – A
PermissionError
is raised ifself.no_write
isTrue
because saving the result would edit the file.ValueError – A
ValueError
is raised ifself.group
isNone
and no value is provided forgroup
because the method then doesn’t know where to save the result.PermissionError – A
PermissionError
is raised if a dataset with namename
already exists butoverwrite
is set toFalse
.
- save_result_arrays(*args, **kwargs)[source]
Save multiple result arrays.
Iteratively calls
save_result_array()
on multiple data sets. Assumes arguments are ordered such that each dataset to be saved is preceded by the name to save it as. All keyword arguments are passed to each call of save_result_array().- Parameters:
*args – Ordered arguments such that each dataset to be saved is preceded by the name to save it as.
**kwargs – Passed through to
save_result_array
as kwargs.
- save_results(*args, **kwargs)[source]
Save multiple results to the hdf5 file.
This method iteratively calls
self.save_result()
on multiple results. It assumes arguments are ordered such that each result to be saved is preceded by the name of the attribute to save it under. Keywords arguments are passed to each call ofself.save_result()
.- Parameters:
*args – The names and values of results to be saved. The first entry should be a string giving the name of the first result, and the second entry should be the value for that result. After that, an arbitrary number of additional pairs of result name strings and values can be included, e.g.
'name0', value0, 'name1', value1,...
.**kwargs – Keyword arguments are passed to
self.save_result()
. Note that the names and values of keyword arguments are NOT saved as results to the hdf5 file; they are only used to provide values for the optional arguments ofself.save_result()
.
Examples
>>> run = Run('path/to/an/hdf5/file.h5') >>> a = 5 >>> b = 2.48 >>> run.save_results('result', a, 'other_result', b, overwrite=False)
- save_results_dict(results_dict, uncertainties=False, **kwargs)[source]
Save results dictionary.
Iteratively calls
self.save_result(key,value)
on the provided dictionary. If uncertainties isTrue
,value
is a two-element list where the second element is the uncertainty in the result and saved with to the same key withu_
prepended.- Parameters:
results_dict (dict) – Dictionary of results to save. If uncertainties is
False
, form is{name:value}
. IfTrue
, for is{name,[value,uncertainty]}
.uncertainties (bool, optional) – Marks if uncertainties are provided.
**kwargs – Extra arguments provided to
save_result
.
- set_group(groupname)[source]
Set the default hdf5 file group for saving results.
The
save...()
methods will save their results toself.group
if an explicit value for their optionalgroup
argument is not given. This method updatesself.group
, making sure to create the group in the hdf5 file if it does not already exist.- Parameters:
groupname (str) – The name of the hdf5 file group in which to save results by default. The group will be created in the
'/results'
group of the hdf5 file.