Skip to content

Commit

Permalink
Wip detection of shape
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshnielsen committed Sep 7, 2023
1 parent 9e4ba21 commit 5cee264
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions qcodes/tests/dataset/test_dataset_export.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import logging
import os
from collections.abc import Hashable
from pathlib import Path

import numpy as np
Expand Down Expand Up @@ -564,12 +565,51 @@ def test_to_xarray_da_dict_paramspec_metadata_is_preserved(
for spec_name, spec_value in expected_param_spec_attrs.items():
assert xr_da.attrs[spec_name] == spec_value

def figure_out_index(dataframe):
# heavily inspired by xarray.core.dataset.from_dataframe
import pandas as pd

Check notice on line 570 in qcodes/tests/dataset/test_dataset_export.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

qcodes/tests/dataset/test_dataset_export.py#L570

Import outside toplevel (pandas)
from xarray.core.indexes import PandasIndex, remove_unused_levels_categories

Check notice on line 571 in qcodes/tests/dataset/test_dataset_export.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

qcodes/tests/dataset/test_dataset_export.py#L571

Import outside toplevel (xarray.core.indexes.PandasIndex, xarray.core.indexes.remove_unused_levels_categories)
from xarray.core.variable import Variable, calculate_dimensions

Check notice on line 572 in qcodes/tests/dataset/test_dataset_export.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

qcodes/tests/dataset/test_dataset_export.py#L572

Import outside toplevel (xarray.core.variable.Variable, xarray.core.variable.calculate_dimensions)

if not dataframe.columns.is_unique:
raise ValueError("cannot convert DataFrame with non-unique columns")

idx = remove_unused_levels_categories(dataframe.index)

if isinstance(idx, pd.MultiIndex) and not idx.is_unique:
raise ValueError(
"cannot convert a DataFrame with a non-unique MultiIndex into xarray"
)
index_vars: dict[Hashable, Variable] = {}

if isinstance(idx, pd.MultiIndex):
dims = tuple(
name if name is not None else "level_%i" % n

Check notice on line 587 in qcodes/tests/dataset/test_dataset_export.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

qcodes/tests/dataset/test_dataset_export.py#L587

Formatting a regular string which could be a f-string
for n, name in enumerate(idx.names)
)
for dim, lev in zip(dims, idx.levels):
xr_idx = PandasIndex(lev, dim)
index_vars.update(xr_idx.create_variables())
else:
index_name = idx.name if idx.name is not None else "index"
dims = (index_name,)
xr_idx = PandasIndex(idx, index_name)
index_vars.update(xr_idx.create_variables())

expanded_shape = calculate_dimensions(index_vars)
return expanded_shape


def test_export_2d_dataset(tmp_path_factory, mock_dataset_grid: DataSet) -> None:
tmp_path = tmp_path_factory.mktemp("export_netcdf")
path = str(tmp_path)
mock_dataset_grid.export(export_type="netcdf", path=tmp_path, prefix="qcodes_")

pdf = mock_dataset_grid.to_pandas_dataframe()
dims = figure_out_index(pdf)
assert dims == {"x": 10, "y": 5}
pdf.to_xarray()

xr_ds = mock_dataset_grid.to_xarray_dataset()
assert xr_ds["z"].dims == ("x", "y")

Expand All @@ -593,6 +633,11 @@ def test_export_non_grid_dataset(
path = str(tmp_path)
mock_dataset_non_grid.export(export_type="netcdf", path=tmp_path, prefix="qcodes_")

pdf = mock_dataset_non_grid.to_pandas_dataframe()
dims = figure_out_index(pdf)
assert dims == {"x": 50, "y": 50}
pdf.to_xarray()

xr_ds = mock_dataset_non_grid.to_xarray_dataset()
assert xr_ds["z"].dims == ("x", "y")

Expand Down

0 comments on commit 5cee264

Please sign in to comment.