Source code for bragi_quantum.datalayer.managing.dataset

# ══════════════════════════════════════════════════════════════════════════
#     ___   ___ ___   _      _   ___
#    / _ \ / __|_  ) | |    /_\ | _ )
#   | (_) | (__ / /  | |__ / _ \| _ \
#    \__\_\\___/___| |____/_/ \_\___/
#
#   QC2 @ CHALMERS CALIBRATION TOOLKIT
#   (c) 2026 Chalmers University of Technology and Project Contributors
# ──────────────────────────────────────────────────────────────────────────
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       https://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
# ──────────────────────────────────────────────────────────────────────────
#   CONTRIBUTORS list:
#
#   Michele Faucci Giannelli, faucci@chalmers.se
#   Matteo Robbiati, robbiati@chalmers.se
#   Axel Andersson, axean@chalmers.se
# ══════════════════════════════════════════════════════════════════════════


# This code is part of Tergite
#
# (C) Copyright Eleftherios Moschandreou 2024
# (C) Copyright Liangyu Chen 2024
# (C) Copyright Tong Liu 2024
# (C) Copyright Michele Faucci Giannelli 2024, 2025
# (C) Copyright Stefan Hill 2024, 2025
# (C) Copyright Pontus Vikstål 2024
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
#
# Modified from tergite-autocalibration
# (https://github.com/tergite/tergite-autocalibration), file:
#   tergite_autocalibration/utils/io/dataset.py


from pathlib import Path

import xarray


[docs] def to_real_dataset(iq_dataset: xarray.Dataset) -> xarray.Dataset: ds = iq_dataset.expand_dims("ReIm", axis=-1) # Add ReIm axis at the end ds = xarray.concat([ds.real, ds.imag], dim="ReIm") return ds
[docs] def save_dataset( result_dataset: xarray.Dataset, node_name: str, data_path: Path ) -> None: """ Save the measurement dataset to a file. Args: result_dataset (xarray.Dataset): The dataset to save. node_name (str): Name of the node being measured. data_path (Path): Path where the dataset will be saved. """ data_path.mkdir(parents=True, exist_ok=True) measurement_id = data_path.stem[0:19] result_dataset = result_dataset.assign_attrs( {"name": node_name, "tuid": measurement_id} ) # to_netcdf doesn't like complex numbers, convert to real/imag to save: result_dataset_real = to_real_dataset(result_dataset) count = 0 dataset_name = f"dataset_{node_name}_{count}.hdf5" while (data_path / dataset_name).is_file(): count += 1 dataset_name = f"dataset_{node_name}_{count}.hdf5" result_dataset_real.to_netcdf(data_path / dataset_name)