Skip to content

Commit 460fbbb

Browse files
authored
Merge pull request #437 from einsmein/qcodes-sqlite-readonly
Use read_only flag to load qcodes dataset
2 parents aadbe9e + e626943 commit 460fbbb

3 files changed

Lines changed: 31 additions & 15 deletions

File tree

plottr/apps/inspectr.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,10 @@ def setDateSelection(self, dates: Sequence[str]) -> None:
579579
@Slot(int)
580580
def setRunSelection(self, runId: int) -> None:
581581
assert self.filepath is not None
582-
ds = load_dataset_from(self.filepath, runId)
582+
if sys.version_info >= (3, 11):
583+
ds = load_dataset_from(self.filepath, runId, read_only=True)
584+
else:
585+
ds = load_dataset_from(self.filepath, runId)
583586
snap = None
584587
if hasattr(ds, 'snapshot'):
585588
snap = ds.snapshot
@@ -607,7 +610,10 @@ def setTag(self, item: QtWidgets.QTreeWidgetItem, tag: str) -> None:
607610
# set tag in the database
608611
assert self.filepath is not None
609612
runId = int(item.text(0))
610-
ds = load_dataset_from(self.filepath, runId)
613+
if sys.version_info >= (3, 11):
614+
ds = load_dataset_from(self.filepath, runId, read_only=False)
615+
else:
616+
ds = load_dataset_from(self.filepath, runId)
611617
ds.add_metadata('inspectr_tag', tag)
612618

613619
# set tag in self.dbdf

plottr/data/qcodes_dataset.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
Dealing with qcodes dataset (the database) data in plottr.
55
"""
66
import os
7+
import sys
8+
from contextlib import closing
79
from itertools import chain
810
from operator import attrgetter
911
from typing import Dict, List, Set, Union, TYPE_CHECKING, Any, Tuple, Optional, cast
@@ -14,7 +16,7 @@
1416

1517
from qcodes.dataset.data_set import load_by_id
1618
from qcodes.dataset.experiment_container import experiments
17-
from qcodes.dataset.sqlite.database import initialise_or_create_database_at
19+
from qcodes.dataset.sqlite.database import conn_from_dbpath_or_conn, initialise_or_create_database_at
1820

1921
from .datadict import DataDictBase, DataDict, combine_datadicts
2022
from ..node.node import Node, updateOption
@@ -160,7 +162,7 @@ def get_ds_info(ds: 'DataSetProtocol', get_structure: bool = True) -> DataSetInf
160162
return data
161163

162164

163-
def load_dataset_from(path: str, run_id: int) -> 'DataSetProtocol':
165+
def load_dataset_from(path: str, run_id: int, read_only: bool = True) -> 'DataSetProtocol':
164166
"""
165167
Loads ``DataSet`` with the given ``run_id`` from a database file that
166168
is located in in the given ``path``.
@@ -169,6 +171,8 @@ def load_dataset_from(path: str, run_id: int) -> 'DataSetProtocol':
169171
qcodes config of the current python process is changed to ``path``.
170172
"""
171173
initialise_or_create_database_at(path)
174+
if sys.version_info >= (3, 11):
175+
return load_by_id(run_id=run_id, read_only=read_only)
172176
return load_by_id(run_id=run_id)
173177

174178

@@ -187,18 +191,24 @@ def get_runs_from_db(path: str, start: int = 0,
187191
in the return dict.
188192
"""
189193
initialise_or_create_database_at(path)
194+
if sys.version_info >= (3, 11):
195+
conn = conn_from_dbpath_or_conn(conn=None, path_to_db=path, read_only=True)
196+
else:
197+
conn = conn_from_dbpath_or_conn(conn=None, path_to_db=path)
198+
with closing(conn) as conn_:
199+
exps = experiments(conn=conn_)
190200

191-
datasets = sorted(
192-
chain.from_iterable(exp.data_sets() for exp in experiments()),
193-
key=attrgetter('run_id')
194-
)
201+
datasets = sorted(
202+
chain.from_iterable(exp.data_sets() for exp in exps),
203+
key=attrgetter('run_id')
204+
)
195205

196-
# There is no need for checking whether ``stop`` is ``None`` because if
197-
# it is the following is simply equivalent to ``datasets[start:]``
198-
datasets = datasets[start:stop]
206+
# There is no need for checking whether ``stop`` is ``None`` because if
207+
# it is the following is simply equivalent to ``datasets[start:]``
208+
datasets = datasets[start:stop]
199209

200-
overview = {ds.run_id: get_ds_info(ds, get_structure=get_structure)
201-
for ds in datasets}
210+
overview = {ds.run_id: get_ds_info(ds, get_structure=get_structure)
211+
for ds in datasets}
202212
return overview
203213

204214

@@ -286,7 +296,7 @@ def process(self, dataIn: Optional[DataDictBase] = None) -> Optional[Dict[str, A
286296
path, runId = cast(Tuple[str, int], self._pathAndId)
287297

288298
if self._dataset is None:
289-
self._dataset = load_dataset_from(path, runId)
299+
self._dataset = load_dataset_from(path, runId, read_only=True)
290300

291301
if self._dataset.number_of_results > self.nLoadedRecords:
292302

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Tracker = "https://github.com/toolsforexperiments/plottr/issues"
5252
pyqt5 = ["PyQt5"]
5353
pyqt6 = ["PyQt6"]
5454
pyside2 = ["PySide2>=5.12"]
55-
qcodes = ["qcodes"]
55+
qcodes = ["qcodes>=0.54.1"]
5656

5757
[project.scripts]
5858
plottr-monitr = "plottr.apps.monitr:script"

0 commit comments

Comments
 (0)