-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from dihm/shot_utils
Moving `get_shot_globals` function from runmanager to labscript_utils.
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
##################################################################### | ||
# # | ||
# __init__.py # | ||
# # | ||
# Copyright 2013, Monash University # | ||
# # | ||
# This file is part of the labscript suite (see # | ||
# http://labscriptsuite.org) and is licensed under the Simplified # | ||
# BSD License. See the license.txt file in the root of the project # | ||
# for the full license. # | ||
# # | ||
##################################################################### | ||
|
||
import h5py | ||
import numpy as np | ||
|
||
def get_shot_globals(filepath): | ||
"""Returns the evaluated globals for a shot, for use by labscript or lyse. | ||
Simple dictionary access as in dict(h5py.File(filepath).attrs) would be fine | ||
except we want to apply some hacks, so it's best to do that in one place.""" | ||
params = {} | ||
with h5py.File(filepath, 'r') as f: | ||
for name, value in f['globals'].attrs.items(): | ||
# Convert numpy bools to normal bools: | ||
if isinstance(value, np.bool_): | ||
value = bool(value) | ||
# Convert null HDF references to None: | ||
if isinstance(value, h5py.Reference) and not value: | ||
value = None | ||
# Convert numpy strings to Python ones. | ||
# DEPRECATED, for backward compat with old files. | ||
if isinstance(value, np.str_): | ||
value = str(value) | ||
if isinstance(value, bytes): | ||
value = value.decode() | ||
params[name] = value | ||
return params |