Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:save scaled value, perform initial get #774

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions qcodes/instrument/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def __init__(self, name, instrument, snapshot_get=True, metadata=None,
# record of latest value and when it was set or measured
# what exactly this means is different for different subclasses
# but they all use the same attributes so snapshot is consistent.
self._latest = {'value': None, 'ts': None}
self._latest = {'value': None, 'ts': None, 'raw_value': None}
self.get_latest = GetLatest(self, max_val_age=max_val_age)

if hasattr(self, 'get'):
Expand All @@ -188,7 +188,7 @@ def __init__(self, name, instrument, snapshot_get=True, metadata=None,

# subclasses should extend this list with extra attributes they
# want automatically included in the snapshot
self._meta_attrs = ['name', 'instrument', 'step', 'scale', 'raw_value',
self._meta_attrs = ['name', 'instrument', 'step', 'scale',
'inter_delay', 'post_delay', 'val_mapping', 'vals']

# Specify time of last set operation, used when comparing to delay to
Expand Down Expand Up @@ -243,6 +243,7 @@ def snapshot_base(self, update=False):

if not self._snapshot_value:
state.pop('value')
state.pop('raw_value', None)

if isinstance(state['ts'], datetime):
state['ts'] = state['ts'].strftime('%Y-%m-%d %H:%M:%S')
Expand All @@ -267,7 +268,8 @@ def snapshot_base(self, update=False):
def _save_val(self, value, validate=False):
if validate:
self.validate(value)
self._latest = {'value': value, 'ts': datetime.now()}
self._latest = {'value': value, 'ts': datetime.now(),
'raw_value': self.raw_value}

def _wrap_get(self, get_function):
@wraps(get_function)
Expand Down Expand Up @@ -347,8 +349,13 @@ def set_wrapper(value, **kwargs):

set_function(val, **kwargs)
self.raw_value = val
self._save_val(val, validate=(self.val_mapping is None and
self.set_parser is None))
if self.scale is not None:
scaled_val = val / self.scale
else:
scaled_val = val
self._save_val(scaled_val,
validate=(self.val_mapping is None and
self.set_parser is None))

# Update last set time (used for calculating delays)
self._t_last_set = time.perf_counter()
Expand Down Expand Up @@ -379,7 +386,9 @@ def get_ramp_values(self, value, step=None):
if step is None:
return [value]
else:
start_value = self.get_latest()
if self.get_latest() is None:
self.get()
start_value = self.raw_value

self.validate(start_value)

Expand Down Expand Up @@ -647,7 +656,8 @@ def __init__(self, name, instrument=None, label=None, unit=None,
if max_val_age is not None:
raise SyntaxError('Must have get method or specify get_cmd '
'when max_val_age is set')
self.get = self.get_latest
self.get = lambda: self._latest['value' if self.scale is None
else 'raw_value']
else:
exec_str = instrument.ask if instrument else None
self.get = Command(arg_count=0, cmd=get_cmd, exec_str=exec_str)
Expand All @@ -667,7 +677,7 @@ def __init__(self, name, instrument=None, label=None, unit=None,
self.unit = unit if unit is not None else ''

if initial_value is not None:
self._save_val(initial_value, validate=True)
self.set(initial_value)

# generate default docstring
self.__doc__ = os.linesep.join((
Expand Down