*** Wartungsfenster jeden ersten Mittwoch vormittag im Monat ***

Skip to content
Snippets Groups Projects
Commit a20fab3d authored by chrisjbillington's avatar chrisjbillington
Browse files

Resolve issue with Run object choosing group name based on script name

Unsure since when this has been broken, but lyse routines used to work
fine outside of lyse, as long as you specified the h5 file. However,
prior to this fix, when instantiating a `Run` object whilst running a
script outside of lyse, `Run.__init__()` fails to introspect the name
of the main script, in order to select the default group name to which
results should be saved.

The previous introspection code likely predates some changes we made
such that traversing a call stack in order to find the top-level script
is no longer necessary. The analysis routine appears in `sys.modules`
as the `__main__` module, as does the top-level script when running an
analysis routine from outside of lyse. So we can just use that. As
before, if the `Run` object finds it is being instantiated in an
unusual environment where there is no `__main__` module in
`sys.modules` or it doesn't have a `__file__`, then we just skip
setting the group name. We used to cough a warning about this, but
that's no longer needed as the user will instead get an exception if
they try to save a result, telling them they they need to set the group
first.
parent ada29b79
No related branches found
No related tags found
No related merge requests found
......@@ -18,7 +18,7 @@ from labscript_utils.dict_diff import dict_diff
import os
import socket
import pickle as pickle
import inspect
from pathlib import Path
import sys
import threading
......@@ -244,48 +244,15 @@ class Run(object):
if not self.no_write:
self._create_group_if_not_exists(h5_path, '/', 'results')
try:
if not self.no_write:
# The group where this run's results will be stored in the h5
# file will be the name of the python script which is
# instantiating this Run object. Iterate from innermost caller
# to outermost. The name of the script will be one frame in
# from analysis_subprocess.py.
analysis_subprocess_path = os.path.join(
LYSE_DIR,
'analysis_subprocess.py',
)
group = None
inner_frame = inspect.currentframe()
inner_path = self._frame_to_path(inner_frame)
inner_file_name = self._path_to_file_name(inner_path)
while group is None:
# self._frame_to_path() will raise a KeyError if this loop
# reaches the outermost caller.
outer_frame = inner_frame.f_back
outer_path = self._frame_to_path(outer_frame)
outer_file_name = self._path_to_file_name(outer_path)
if outer_path == analysis_subprocess_path:
group = inner_file_name
inner_frame = outer_frame
inner_path = outer_path
inner_file_name = outer_file_name
self.set_group(group)
except KeyError:
# sys.stderr.write('Warning: to write results, call '
# 'Run.set_group(groupname), specifying the name of the group '
# 'you would like to save results to. This normally comes from '
# 'the filename of your script, but since you\'re in interactive '
# 'mode, there is no script name.\n')
pass
def _frame_to_path(self, frame):
path = frame.f_globals['__file__']
return path
def _path_to_file_name(self, path):
file_name = os.path.basename(path).split('.py')[0]
return file_name
if not self.no_write:
# The group where this run's results will be stored in the h5 file will be
# the name of the python script which is instantiating this Run object. If
# the user is running interactively or in an unusual environment such that
# the __main__ module isn't in sys.modules or doesn't have a non-None
# __file__ attribute, then self.group will not be set.
main_module_path = getattr(sys.modules.get('__main__'), '__file__', None)
if main_module_path is not None:
self.set_group(Path(main_module_path).stem)
@property
def h5_path(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment