Skip to content

Commit d41b0ea

Browse files
committed
chunk delayed export to limit the number of files
1 parent 306d542 commit d41b0ea

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/qcodes/dataset/data_set.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from typing import TYPE_CHECKING, Any, Literal
1515

1616
import numpy
17-
from tqdm.auto import trange
17+
from tqdm.auto import tqdm
1818

1919
import qcodes
2020
from qcodes.dataset.data_set_protocol import (
@@ -246,7 +246,8 @@ def __init__(
246246
#: In memory representation of the data in the dataset.
247247
self._cache: DataSetCacheWithDBBackend = DataSetCacheWithDBBackend(self)
248248
self._results: list[dict[str, VALUE]] = []
249-
self._in_memory_cache = in_memory_cache
249+
self._in_memory_cache: bool = in_memory_cache
250+
self._max_num_files_export = 100
250251
self._export_limit = 1000
251252

252253
if run_id is not None:
@@ -1484,6 +1485,21 @@ def _set_export_info(self, export_info: ExportInfo) -> None:
14841485

14851486
def _export_as_netcdf(self, path: Path, file_name: str) -> Path:
14861487
"""Export data as netcdf to a given path with file prefix"""
1488+
1489+
def generate_steps(num_rows, max_num_steps) -> list[tuple[int, int]]:
1490+
if max_num_steps >= num_rows:
1491+
return [(i + 1, i + 1) for i in range(num_rows)]
1492+
1493+
step_size, remainder = divmod(num_rows, max_num_steps)
1494+
limits = [
1495+
(i * step_size + 1, (i + 1) * step_size) for i in range(max_num_steps)
1496+
]
1497+
1498+
if remainder > 0:
1499+
limits[-1] = (limits[-1][0], (step_size) * max_num_steps + remainder)
1500+
1501+
return limits
1502+
14871503
import xarray as xr
14881504

14891505
file_path = path / file_name
@@ -1514,12 +1530,16 @@ def _export_as_netcdf(self, path: Path, file_name: str) -> Path:
15141530
"temp_dir": temp_dir,
15151531
},
15161532
)
1517-
num_files = len(self)
1533+
num_rows = len(self)
1534+
steps = generate_steps(num_rows, self._max_num_files_export)
1535+
num_files = len(steps)
15181536
num_digits = len(str(num_files))
15191537
file_name_template = f"ds_{{:0{num_digits}d}}.nc"
1520-
for i in trange(num_files, desc="Writing individual files"):
1538+
for i, (start, stop) in tqdm(
1539+
enumerate(steps), total=num_files, desc="Writing individual files"
1540+
):
15211541
xarray_to_h5netcdf_with_complex_numbers(
1522-
self.to_xarray_dataset(start=i + 1, end=i + 1),
1542+
self.to_xarray_dataset(start=start, end=stop),
15231543
temp_path / file_name_template.format(i),
15241544
)
15251545
files = tuple(temp_path.glob("*.nc"))

0 commit comments

Comments
 (0)