Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial refactoring #119

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/source/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ API Reference

lyse
lyse.analysis_subprocess
lyse.communication
lyse.dataframe_utilities
lyse.figure_manager
lyse.filebox
lyse.routines
lyse.tempfile2clipboard
lyse.utils
lyse.__main__
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# -- Project information (unique to each project) -------------------------------------

project = "lyse"
copyright = "2020, labscript suite"
copyright = "2024, labscript suite"
author = "labscript suite contributors"

# The full version, including alpha/beta/rc tags
Expand Down
51 changes: 12 additions & 39 deletions lyse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
# the project for the full license. #
# #
#####################################################################
"""Lyse analysis API
"""
Lyse analysis API
Comment on lines +13 to +14
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this newline make it inconsistent with the other docstring formatting? If any changes were to be made here, I'd suggest a single line """Lyse analysis API""" docstring given how short it is.

"""

from lyse.dataframe_utilities import get_series_from_shot as _get_singleshot
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also import lyse.dataframe_utilities explicitly below. Can we simplify some of these lyse import statements so we aren't importing from the same modules in different places?

Expand All @@ -25,7 +26,6 @@
import contextlib

import labscript_utils.h5_lock, h5py
from labscript_utils.labconfig import LabConfig
import pandas
from numpy import array, ndarray, where
import types
Expand All @@ -36,7 +36,13 @@
from labscript_utils.ls_zprocess import zmq_get

from labscript_utils.properties import get_attributes, get_attribute, set_attributes
LYSE_DIR = os.path.dirname(os.path.realpath(__file__))

# lyse imports
import lyse.dataframe_utilities
import lyse.utils
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a little bit hesitant about this lyse.utils import. lyse.utils is importing Qt packages. But the lyse package (e.g. this file) can be imported in either the GUI, or the worker process. We are probably tied to a Qt matplotlib backed in the worker process anyway? But it would be nice if we were not, or at least minimally so.

One solution would be to make a utils dir, with three files (__init__.py, gui.py, worker.py - or whatever names you like) so that the utils and imports can be split between only for gui, only for worker, or for both. That allows more specific imports to be made (note that the contents of the init file will be imported if either of the others are so that one should try to stay clean of Qt stuff as well).


# Import this way so LYSE_DIR is exposed when someone does import lyse or from lyse import *
from lyse.utils import LYSE_DIR

# If running stand-alone, and not from within lyse, the below two variables
# will be as follows. Otherwise lyse will override them with spinning_top =
Expand All @@ -55,13 +61,6 @@
# a flag to determine whether we should wait for the delay event
_delay_flag = False

# get port that lyse is using for communication
try:
_labconfig = LabConfig(required_params={"ports": ["lyse"]})
_lyse_port = int(_labconfig.get('ports', 'lyse'))
except Exception:
_lyse_port = 42519

if len(sys.argv) > 1:
path = sys.argv[1]
else:
Expand All @@ -80,7 +79,7 @@ class _RoutineStorage(object):
routine_storage = _RoutineStorage()


def data(filepath=None, host='localhost', port=_lyse_port, timeout=5, n_sequences=None, filter_kwargs=None):
def data(filepath=None, host='localhost', port=lyse.utils.LYSE_PORT, timeout=5, n_sequences=None, filter_kwargs=None):
"""Get data from the lyse dataframe or a file.

This function allows for either extracting information from a run's hdf5
Expand Down Expand Up @@ -161,7 +160,7 @@ def data(filepath=None, host='localhost', port=_lyse_port, timeout=5, n_sequence

# Allow sending 'get dataframe' (without the enclosing list) if
# n_sequences and filter_kwargs aren't provided. This is for backwards
# compatability in case the server is running an outdated version of
# compatibility in case the server is running an outdated version of
# lyse.
if n_sequences is None and filter_kwargs is None:
command = 'get dataframe'
Expand All @@ -178,35 +177,9 @@ def data(filepath=None, host='localhost', port=_lyse_port, timeout=5, n_sequence
raise ValueError(dedent(msg))
# Ensure conversion to multiindex is done, which needs to be done here
# if the server is running an outdated version of lyse.
_rangeindex_to_multiindex(df, inplace=True)
lyse.dataframe_utilities.rangeindex_to_multiindex(df, inplace=True)
return df

def _rangeindex_to_multiindex(df, inplace):
if isinstance(df.index, pandas.MultiIndex):
# The dataframe has already been converted.
return df
try:
padding = ('',)*(df.columns.nlevels - 1)
try:
integer_indexing = _labconfig.getboolean('lyse', 'integer_indexing')
except (LabConfig.NoOptionError, LabConfig.NoSectionError):
integer_indexing = False
if integer_indexing:
out = df.set_index(['sequence_index', 'run number', 'run repeat'], inplace=inplace, drop=False)
# out is None if inplace is True, and is the new dataframe is inplace is False.
if not inplace:
df = out
else:
out = df.set_index([('sequence',) + padding,('run time',) + padding], inplace=inplace, drop=False)
if not inplace:
df = out
df.index.names = ['sequence', 'run time']
except KeyError:
# Empty DataFrame or index column not found, so fall back to RangeIndex instead
pass
df.sort_index(inplace=True)
return df

def globals_diff(run1, run2, group=None):
"""Take a diff of the globals between two runs.

Expand Down
Loading