Skip to content

Commit

Permalink
Merge branch 'master' into guenp/parameter_types
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshnielsen committed May 20, 2021
2 parents a1d83b8 + 3d92c9d commit 8f3ecc5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 26 deletions.
6 changes: 3 additions & 3 deletions docs_requirements.txt
Expand Up @@ -54,7 +54,7 @@ pytz==2021.1
PyVISA==1.11.3
PyVISA-sim==0.4.0
pywin32==300; sys_platform == 'win32'
pywinpty==0.5.7; sys_platform == 'win32'
pywinpty==1.1.1; sys_platform == 'win32'
PyYAML==5.4.1
pyzmq==22.0.3
qtconsole==5.1.0
Expand All @@ -74,7 +74,7 @@ sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.4
stringparser==0.5
terminado==0.9.5
terminado==0.10.0
testpath==0.5.0
tornado==6.1
traitlets==5.0.5
Expand All @@ -84,5 +84,5 @@ wcwidth==0.2.5
webencodings==0.5.1
widgetsnbextension==3.5.1
wincertstore==0.2
xarray==0.18.0
xarray==0.18.2
zipp==3.4.1
46 changes: 26 additions & 20 deletions qcodes/instrument/parameter.py
Expand Up @@ -503,7 +503,7 @@ def _from_value_to_raw_value(self, value: ParamDataType
in zip(raw_value, self.scale))
else:
# Use single scale for all values
raw_value *= self.scale
raw_value = raw_value * self.scale

# apply offset next
if self.offset is not None:
Expand All @@ -513,7 +513,7 @@ def _from_value_to_raw_value(self, value: ParamDataType
in zip(raw_value, self.offset))
else:
# Use single offset for all values
raw_value += self.offset
raw_value = raw_value + self.offset

# parser last
if self.set_parser is not None:
Expand All @@ -533,28 +533,34 @@ def _from_raw_value_to_value(self, raw_value: ParamRawDataType
# apply offset first (native scale)
if self.offset is not None:
# offset values
if isinstance(self.offset, collections.abc.Iterable):
# offset contains multiple elements, one for each value
value = tuple(val - offset for val, offset
in zip(value, self.offset))
elif isinstance(value, collections.abc.Iterable):
# Use single offset for all values
value = tuple(val - self.offset for val in value)
else:
value -= self.offset
try:
value = value - self.offset
except TypeError:
if isinstance(self.offset, collections.abc.Iterable):
# offset contains multiple elements, one for each value
value = tuple(val - offset for val, offset
in zip(value, self.offset))
elif isinstance(value, collections.abc.Iterable):
# Use single offset for all values
value = tuple(val - self.offset for val in value)
else:
raise

# scale second
if self.scale is not None:
# Scale values
if isinstance(self.scale, collections.abc.Iterable):
# Scale contains multiple elements, one for each value
value = tuple(val / scale for val, scale
in zip(value, self.scale))
elif isinstance(value, collections.abc.Iterable):
# Use single scale for all values
value = tuple(val / self.scale for val in value)
else:
value /= self.scale
try:
value = value / self.scale
except TypeError:
if isinstance(self.scale, collections.abc.Iterable):
# Scale contains multiple elements, one for each value
value = tuple(val / scale for val, scale in zip(value,
self.scale))
elif isinstance(value, collections.abc.Iterable):
# Use single scale for all values
value = tuple(val / self.scale for val in value)
else:
raise

if self.inverse_val_mapping is not None:
if value in self.inverse_val_mapping:
Expand Down
33 changes: 33 additions & 0 deletions qcodes/tests/parameter/test_parameter_scale_offset.py
Expand Up @@ -172,3 +172,36 @@ def test_scale_and_offset_raw_value_iterable_for_set_cache(values,
event('Scale is array and also offset')
if isinstance(scales, Iterable) and not isinstance(offsets, Iterable):
event('Scale is array but not offset')


def test_numpy_array_valued_parameter_preserves_type_if_scale_and_offset_are_set():

def rands():
return np.random.randn(5)

param = Parameter(name='test_param',
set_cmd=None,
get_cmd=rands)

param.scale = 10
param.offset = 7

values = param()

assert isinstance(values, np.ndarray)


def test_setting_numpy_array_valued_param_if_scale_and_offset_are_not_none():

param = Parameter(name='test_param',
set_cmd=None,
get_cmd=None)

values = np.array([1, 2, 3, 4, 5])

param.scale = 100
param.offset = 10

param(values)

assert isinstance(param.raw_value, np.ndarray)
6 changes: 3 additions & 3 deletions requirements.txt
Expand Up @@ -64,7 +64,7 @@ python-dateutil~=2.8.1
pytz~=2021.1
PyVISA~=1.11.3
pywin32==300; sys_platform == 'win32'
pywinpty~=0.5.7; sys_platform == 'win32'
pywinpty~=1.1.1; sys_platform == 'win32'
pyzmq~=22.0.3
requests~=2.25.1
requirements-parser~=0.2.0
Expand All @@ -76,7 +76,7 @@ Send2Trash~=1.5.0
six~=1.16.0
slack-sdk~=3.5.1
tabulate~=0.8.9
terminado~=0.9.5
terminado~=0.10.0
testpath~=0.5.0
tornado~=6.1
tqdm~=4.60.0
Expand All @@ -90,7 +90,7 @@ websockets~=9.0
widgetsnbextension~=3.5.1
wincertstore~=0.2
wrapt~=1.12.1
xarray~=0.18.0
xarray~=0.18.2
zipp~=3.4.1
# not strict requirements but needed for the legacy pyqtgraph based plotting
PyQt5<5.16
Expand Down

0 comments on commit 8f3ecc5

Please sign in to comment.