-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaveClass.py
115 lines (100 loc) · 4.84 KB
/
saveClass.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import holoviews as hv
import numpy as np
from IPython.display import display
class savedData:
def __init__(self, result, metadata, name, description):
"""Object used to hold resulting plot from a measurement, basic description of measurement, and metadata about system state."""
self._plot = result
self._metadata = metadata
self.name = name
#This assumes that object always created with the date as the name (which is the current functionality)
self.date = name
self.description = description
self.comment = None
return
@property
def data(self):
"""Returns raw data as pandas dataframe"""
return self.plot.dframe()
@property
def state(self):
"""Returns state of all instruments at time of measurement as a dictionary, based off snapshot feature of QCoDeS instruments"""
return self._metadata
@property
def readableState(self):
"""Prints clean and simplified state of all instruments."""
for inst in self._metadata:
snapshot = self._metadata[inst]
if type(snapshot) == dict:
self._print_readable_snapshot(snapshot)
else:
print(inst + ':\n')
display(snapshot)
print('\n')
return
@property
def plot(self):
"""Returns the plot associated with the given measurement. With Holoviews objects this should also display the plot inline"""
if type(self._plot) == hv.Image:
return self._plot.opts(norm=dict(framewise=True), plot=dict(colorbar=True), style=dict(cmap='jet'))
return self._plot
def __repr__(self):
if self.comment:
return self.comment
else:
return self.description
def _print_readable_snapshot(self, snapshot, update: bool=False,
max_chars: int=80) -> None:
"""
Prints a readable version of the snapshot.
The readable snapshot includes the name, value and unit of each
parameter.
A convenience function to quickly get an overview of the
status of an instrument.
Args:
update: If True, update the state by querying the
instrument. If False, just use the latest values in memory.
This argument gets passed to the snapshot function.
max_chars: the maximum number of characters per line. The
readable snapshot will be cropped if this value is exceeded.
Defaults to 80 to be consistent with default terminal width.
"""
floating_types = (float, np.integer, np.floating)
#snapshot = self.snapshot(update=update)
par_lengths = [len(p) for p in snapshot['parameters']]
# Min of 50 is to prevent a super long parameter name to break this
# function
par_field_len = min(max(par_lengths)+1, 50)
print(snapshot['parameters']['IDN']['instrument_name'] + ':')
print('{0:<{1}}'.format('\tparameter ', par_field_len) + 'value')
print('-'*max_chars)
for par in sorted(snapshot['parameters']):
name = snapshot['parameters'][par]['name']
msg = '{0:<{1}}:'.format(name, par_field_len)
# in case of e.g. ArrayParameters, that usually have
# snapshot_value == False, the parameter may not have
# a value in the snapshot
val = snapshot['parameters'][par].get('value', 'Not available')
unit = snapshot['parameters'][par].get('unit', None)
if unit is None:
# this may be a multi parameter
unit = snapshot['parameters'][par].get('units', None)
if isinstance(val, floating_types):
msg += '\t{:.5g} '.format(val)
else:
msg += '\t{} '.format(val)
if unit is not '': # corresponds to no unit
msg += '({})'.format(unit)
# Truncate the message if it is longer than max length
if len(msg) > max_chars and not max_chars == -1:
msg = msg[0:max_chars-3] + '...'
print(msg)
for submodule in snapshot['submodules'].values():
print_readable_snapshot(submodule)
#if hasattr(submodule, '_channels'):
# submodule = cast('ChannelList', submodule)
# if submodule._snapshotable:
# for channel in submodule._channels:
# channel.print_readable_snapshot()
#else:
# submodule.print_readable_snapshot(update, max_chars)