66from math import prod
77from typing import TYPE_CHECKING , Literal
88
9- from packaging import version as pversion
9+ from packaging import version as p_version
1010
1111from qcodes .dataset .linked_datasets .links import links_to_str
1212
@@ -72,42 +72,42 @@ def _load_to_xarray_dataset_dict_no_metadata(
7272 f"Invalid value for use_multi_index. Expected one of 'auto', 'always', 'never' but got { use_multi_index } "
7373 )
7474
75- data_xrdarray_dict : dict [str , xr .Dataset ] = {}
75+ xr_dataset_dict : dict [str , xr .Dataset ] = {}
7676
77- for name , subdict in datadict .items ():
77+ for name , sub_dict in datadict .items ():
7878 shape_is_consistent = (
7979 dataset .description .shapes is not None
8080 and name in dataset .description .shapes
81- and subdict [name ].shape == dataset .description .shapes [name ]
81+ and sub_dict [name ].shape == dataset .description .shapes [name ]
8282 )
8383
8484 if shape_is_consistent and use_multi_index != "always" :
8585 _LOG .info ("Exporting %s to xarray using direct method" , name )
86- data_xrdarray_dict [name ] = _xarray_data_array_direct (dataset , name , subdict )
86+ xr_dataset_dict [name ] = _xarray_data_array_direct (dataset , name , sub_dict )
8787 else :
8888 _LOG .info ("Exporting %s to xarray via pandas index" , name )
8989 index = _generate_pandas_index (
90- subdict , dataset .description .interdeps , top_level_param_name = name
90+ sub_dict , dataset .description .interdeps , top_level_param_name = name
9191 )
9292 index_is_unique = (
9393 len (index .unique ()) == len (index ) if index is not None else False
9494 )
9595
9696 if index is None :
97- xrdarray : xr .Dataset = _data_to_dataframe (
98- subdict , index = index
97+ xr_dataset : xr .Dataset = _data_to_dataframe (
98+ data = sub_dict , index = index
9999 ).to_xarray ()
100- data_xrdarray_dict [name ] = xrdarray
100+ xr_dataset_dict [name ] = xr_dataset
101101 elif index_is_unique :
102- df = _data_to_dataframe (subdict , index )
103- data_xrdarray_dict [name ] = _xarray_data_array_from_pandas_multi_index (
102+ df = _data_to_dataframe (sub_dict , index )
103+ xr_dataset_dict [name ] = _xarray_data_array_from_pandas_multi_index (
104104 dataset , use_multi_index , name , df , index
105105 )
106106 else :
107- df = _data_to_dataframe (subdict , index )
108- data_xrdarray_dict [name ] = df .reset_index ().to_xarray ()
107+ df = _data_to_dataframe (sub_dict , index )
108+ xr_dataset_dict [name ] = df .reset_index ().to_xarray ()
109109
110- return data_xrdarray_dict
110+ return xr_dataset_dict
111111
112112
113113def _xarray_data_array_from_pandas_multi_index (
@@ -142,15 +142,15 @@ def _xarray_data_array_from_pandas_multi_index(
142142 )
143143
144144 coords = xr .Coordinates .from_pandas_multiindex (df .index , "multi_index" )
145- xrdarray = xr .DataArray (df [name ], coords = coords ).to_dataset (name = name )
145+ xr_dataset = xr .DataArray (df [name ], coords = coords ).to_dataset (name = name )
146146 else :
147- xrdarray = df .to_xarray ()
147+ xr_dataset = df .to_xarray ()
148148
149- return xrdarray
149+ return xr_dataset
150150
151151
152152def _xarray_data_array_direct (
153- dataset : DataSetProtocol , name : str , subdict : Mapping [str , npt .NDArray ]
153+ dataset : DataSetProtocol , name : str , sub_dict : Mapping [str , npt .NDArray ]
154154) -> xr .Dataset :
155155 import xarray as xr
156156
@@ -161,7 +161,7 @@ def _xarray_data_array_direct(
161161 # Build coordinate axes from direct dependencies preserving their order
162162 dep_axis : dict [str , npt .NDArray ] = {}
163163 for axis , dep in enumerate (deps ):
164- dep_array = subdict [dep .name ]
164+ dep_array = sub_dict [dep .name ]
165165 dep_axis [dep .name ] = dep_array [
166166 tuple (slice (None ) if i == axis else 0 for i in range (dep_array .ndim ))
167167 ]
@@ -173,7 +173,7 @@ def _xarray_data_array_direct(
173173 if inf .name in dep_axis :
174174 continue
175175 # add only if data for this parameter is available
176- if inf .name not in subdict :
176+ if inf .name not in sub_dict :
177177 continue
178178
179179 inf_related = dataset .description .interdeps .find_all_parameters_in_tree (inf )
@@ -184,12 +184,12 @@ def _xarray_data_array_direct(
184184 if len (related_top_level ) > 0 :
185185 # If inferred param is related to the top-level measurement parameter,
186186 # add it as a data variable with the full dependency dimensions
187- inf_data_full = subdict [inf .name ]
187+ inf_data_full = sub_dict [inf .name ]
188188 inf_dims_full = tuple (dep_axis .keys ())
189189 extra_data_vars [inf .name ] = (inf_dims_full , inf_data_full )
190190 else :
191191 # Otherwise, add as a coordinate along the related dependency axes only
192- inf_data = subdict [inf .name ][
192+ inf_data = sub_dict [inf .name ][
193193 tuple (slice (None ) if dep in related_deps else 0 for dep in deps )
194194 ]
195195 inf_coords = [dep .name for dep in deps if dep in related_deps ]
@@ -202,7 +202,7 @@ def _xarray_data_array_direct(
202202
203203 # Compose data variables dict including measured var and any inferred data vars
204204 data_vars : dict [str , tuple [tuple [str , ...], npt .NDArray ]] = {
205- name : (tuple (dep_axis .keys ()), subdict [name ])
205+ name : (tuple (dep_axis .keys ()), sub_dict [name ])
206206 }
207207 data_vars .update (extra_data_vars )
208208
@@ -233,9 +233,9 @@ def load_to_xarray_dataarray_dict(
233233
234234
235235def _add_metadata_to_xarray (
236- dataset : DataSetProtocol , xrdataset : xr .Dataset | xr .DataArray
236+ dataset : DataSetProtocol , xr_dataset : xr .Dataset | xr .DataArray
237237) -> None :
238- xrdataset .attrs .update (
238+ xr_dataset .attrs .update (
239239 {
240240 "ds_name" : dataset .name ,
241241 "sample_name" : dataset .sample_name ,
@@ -252,12 +252,12 @@ def _add_metadata_to_xarray(
252252 }
253253 )
254254 if dataset .run_timestamp_raw is not None :
255- xrdataset .attrs ["run_timestamp_raw" ] = dataset .run_timestamp_raw
255+ xr_dataset .attrs ["run_timestamp_raw" ] = dataset .run_timestamp_raw
256256 if dataset .completed_timestamp_raw is not None :
257- xrdataset .attrs ["completed_timestamp_raw" ] = dataset .completed_timestamp_raw
257+ xr_dataset .attrs ["completed_timestamp_raw" ] = dataset .completed_timestamp_raw
258258 if len (dataset .metadata ) > 0 :
259259 for metadata_tag , metadata in dataset .metadata .items ():
260- xrdataset .attrs [metadata_tag ] = metadata
260+ xr_dataset .attrs [metadata_tag ] = metadata
261261
262262
263263def load_to_xarray_dataset (
@@ -282,20 +282,20 @@ def load_to_xarray_dataset(
282282
283283
284284def _add_param_spec_to_xarray_coords (
285- dataset : DataSetProtocol , xrdataset : xr .Dataset | xr .DataArray
285+ dataset : DataSetProtocol , xr_dataset : xr .Dataset | xr .DataArray
286286) -> None :
287- for coord in xrdataset .coords :
287+ for coord in xr_dataset .coords :
288288 if coord not in ("index" , "multi_index" ):
289289 paramspec_dict = _paramspec_dict_with_extras (dataset , str (coord ))
290- xrdataset .coords [str (coord )].attrs .update (paramspec_dict .items ())
290+ xr_dataset .coords [str (coord )].attrs .update (paramspec_dict .items ())
291291
292292
293293def _add_param_spec_to_xarray_data_vars (
294- dataset : DataSetProtocol , xrdataset : xr .Dataset
294+ dataset : DataSetProtocol , xr_dataset : xr .Dataset
295295) -> None :
296- for data_var in xrdataset .data_vars :
296+ for data_var in xr_dataset .data_vars :
297297 paramspec_dict = _paramspec_dict_with_extras (dataset , str (data_var ))
298- xrdataset .data_vars [str (data_var )].attrs .update (paramspec_dict .items ())
298+ xr_dataset .data_vars [str (data_var )].attrs .update (paramspec_dict .items ())
299299
300300
301301def _paramspec_dict_with_extras (
@@ -313,7 +313,7 @@ def _paramspec_dict_with_extras(
313313def xarray_to_h5netcdf_with_complex_numbers (
314314 xarray_dataset : xr .Dataset , file_path : str | Path , compute : bool = True
315315) -> None :
316- import cf_xarray as cfxr
316+ import cf_xarray as cf_xr
317317 from pandas import MultiIndex
318318
319319 has_multi_index = any (
@@ -324,7 +324,7 @@ def xarray_to_h5netcdf_with_complex_numbers(
324324 if has_multi_index :
325325 # as of xarray 2023.8.0 there is no native support
326326 # for multi index so use cf_xarray for that
327- internal_ds = cfxr .coding .encode_multi_index_as_compress (
327+ internal_ds = cf_xr .coding .encode_multi_index_as_compress (
328328 xarray_dataset ,
329329 )
330330 else :
@@ -338,11 +338,15 @@ def xarray_to_h5netcdf_with_complex_numbers(
338338 # these are the versions of xarray / h5netcdf respectively required to support complex
339339 # values without fallback to invalid features. Once these are the min versions supported
340340 # we can drop the fallback code here including the warning suppression.
341- xarry_too_old = pversion .Version (version ("xarray" )) < pversion .Version ("2024.10.0" )
342- h5netcdf_too_old = pversion .Version (version ("h5netcdf" )) < pversion .Version ("1.4.0" )
341+ xarray_too_old = p_version .Version (version ("xarray" )) < p_version .Version (
342+ "2024.10.0"
343+ )
344+ h5netcdf_too_old = p_version .Version (version ("h5netcdf" )) < p_version .Version (
345+ "1.4.0"
346+ )
343347
344348 allow_invalid_netcdf = dataset_has_complex_vals and (
345- xarry_too_old or h5netcdf_too_old
349+ xarray_too_old or h5netcdf_too_old
346350 )
347351
348352 with warnings .catch_warnings ():
0 commit comments