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

Getting a DelegateParameter with scale fails if it is not initialized #3653

Closed
haroldmeerwaldt opened this issue Dec 7, 2021 · 2 comments · Fixed by #3662
Closed

Getting a DelegateParameter with scale fails if it is not initialized #3653

haroldmeerwaldt opened this issue Dec 7, 2021 · 2 comments · Fixed by #3662
Labels
Milestone

Comments

@haroldmeerwaldt
Copy link
Contributor

haroldmeerwaldt commented Dec 7, 2021

If you create a DelegateParameter and pass a scale argument, it is not possible to get its cache before it is initialized. The scale gets applied to the None that resides in the source parameter and gives a TypeError.

Steps to reproduce

from qcodes import Parameter, DelegateParameter

p = Parameter("p")
dp = DelegateParameter("dp", p, scale=1.1)

print(dp.source.cache.get(False))  # gives back None
print(dp.cache.get(False))  # fails

Expected behaviour

I expected to get None back, just as you would a plain uninitialized Parameter.

A workaround is to give nan as an initial cache value. This works:

from qcodes import Parameter, DelegateParameter
import numpy as np

p = Parameter("p")
dp = DelegateParameter("dp", p, scale=1.1, initial_cache_value=np.nan)

print(dp.source.cache.get(False))
print(dp.cache.get(False))

Actual behaviour

I got a TypeError:

Traceback (most recent call last):

  File "D:\users\hw_sw_integration\projects\low-level-software\src\lls_input\amber\scripts\example_02.py", line 14, in <module>
    print(dp.cache.get(False))

  File "D:\users\hw_sw_integration\venv-3.9\lib\site-packages\qcodes\instrument\parameter.py", line 1541, in get
    return self._parameter._from_raw_value_to_value(

  File "D:\users\hw_sw_integration\venv-3.9\lib\site-packages\qcodes\instrument\parameter.py", line 601, in _from_raw_value_to_value
    value = value / self.scale

TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'

It appears that _from_raw_value_to_raw_value applies the scale on the value even if it's None. A simple solution would be to put a big if value is not None: in the method, but this may not cover all the use cases (e.g. more intricate ways of handling the scale than dividing/multiplying).

System

Windows 10
qcodes 0.30.1

@astafan8
Copy link
Contributor

astafan8 commented Dec 8, 2021

@haroldmeerwaldt thank you for reporting the issue!

i think your suggestion is good! but let's make the value None check only for offset and the scale, so on line https://github.com/QCoDeS/Qcodes/blob/c66a8af4ac8fa21498849a24734e4e1749952304/qcodes/instrument/parameter.py#L582 and https://github.com/QCoDeS/Qcodes/blob/c66a8af4ac8fa21498849a24734e4e1749952304/qcodes/instrument/parameter.py#L598.

Would you be willing to start a PR? and also add a few tests for this in this file https://github.com/QCoDeS/Qcodes/blob/master/qcodes/tests/parameter/test_parameter_scale_offset.py ?

@astafan8 astafan8 added the bug label Dec 8, 2021
@astafan8 astafan8 added this to the 0.31.0 milestone Dec 8, 2021
@haroldmeerwaldt
Copy link
Contributor Author

@astafan8 That sounds like a good plan! Allowing None for the parser and in the inverse_val_mapping dictionary makes sense. I'll start a PR with tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants