-
Notifications
You must be signed in to change notification settings - Fork 300
/
legacy_import.py
83 lines (71 loc) · 3.16 KB
/
legacy_import.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from typing import List
import json
from qcodes.dataset.measurements import Measurement
from qcodes.data.data_set import load_data
from qcodes.data.data_set import DataSet as OldDataSet
import numpy as np
def setup_measurement(dataset: OldDataSet) -> Measurement:
"""
Register parameters for all DataArrays in a given QCoDeS legacy dataset
This tries to infer the name, label and unit along with any setpoints
for the given array.
"""
meas = Measurement()
for arrayname, array in dataset.arrays.items():
if array.is_setpoint:
setarrays = None
else:
setarrays = [setarray.array_id for setarray in array.set_arrays]
meas.register_custom_parameter(name=array.array_id,
label=array.label,
unit=array.unit,
setpoints = setarrays
)
return meas
def store_array_to_database(datasaver, array):
dims = len(array.shape)
if dims == 2:
for index1, i in enumerate(array.set_arrays[0]):
for index2, j in enumerate(array.set_arrays[1][index1]):
datasaver.add_result((array.set_arrays[0].array_id, i),
(array.set_arrays[1].array_id, j),
(array.array_id, array[index1,index2]))
elif dims == 1:
for index, i in enumerate(array.set_arrays[0]):
datasaver.add_result((array.set_arrays[0].array_id, i),
(array.array_id, array[index]))
else:
raise NotImplementedError('The exporter only currently handles 1 and 2 Dimentional data')
return datasaver.run_id
def store_array_to_database_alt(meas, array):
dims = len(array.shape)
if dims == 2:
outer_data = np.empty(array.shape[1])
with meas.run() as datasaver:
for index1, i in enumerate(array.set_arrays[0]):
outer_data[:] = i
datasaver.add_result((array.set_arrays[0].array_id, outer_data),
(array.set_arrays[1].array_id, array.set_arrays[1][index1,:]),
(array.array_id, array[index1,:]))
elif dims == 1:
with meas.run() as datasaver:
for index, i in enumerate(array.set_arrays[0]):
datasaver.add_result((array.set_arrays[0].array_id, i),
(array.array_id, array[index]))
else:
raise NotImplementedError('The exporter only currently handles 1 and 2 Dimentional data')
return datasaver.run_id
def import_dat_file(location: str) -> List[int]:
"""
This imports a QCoDeS legacy DataSet
"""
loaded_data = load_data(location)
meas = setup_measurement(loaded_data)
run_ids = []
with meas.run() as datasaver:
datasaver.dataset.add_metadata('snapshot', json.dumps(loaded_data.snapshot()))
for arrayname, array in loaded_data.arrays.items():
if not array.is_setpoint:
run_id = store_array_to_database(datasaver, array)
run_ids.append(run_id)
return run_ids