forked from microsoft/Qcodes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request microsoft#1629 from WilliamHPNielsen/feature/time_…
…parameter Feature : an elapsed time parameter
- Loading branch information
Showing
6 changed files
with
303 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
qcodes.instrument.specialized_parameters | ||
---------------------------------------- | ||
|
||
|
||
.. automodule:: qcodes.instrument.specialized_parameters | ||
:members: |
182 changes: 182 additions & 0 deletions
182
docs/examples/DataSet/Measuring X as a function of time.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
""" | ||
The dataset module contains code related to storage and retrieval of data to | ||
and from disk | ||
""" | ||
|
||
# flake8: noqa (we don't need the "<...> imported but unused" error) | ||
|
||
from qcodes.dataset.sqlite.database import initialise_or_create_database_at |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
""" | ||
Module for specialized parameters. The qcodes.instrument.parameters module | ||
provides generic parameters for different generic cases. This module provides | ||
useful/convenient specializations of such generic parameters. | ||
""" | ||
|
||
from time import perf_counter | ||
|
||
from qcodes.instrument.parameter import Parameter | ||
|
||
|
||
class ElapsedTimeParameter(Parameter): | ||
""" | ||
Parameter to measure elapsed time. Measures wall clock time since the | ||
last reset of the instance's clock. The clock is reset upon creation of the | ||
instance. The constructor passes kwargs along to the Parameter constructor. | ||
Args: | ||
name: the local name of the parameter. See the documentation of | ||
:class:`qcodes.instrument.parameter.Parameter` for more details. | ||
""" | ||
|
||
def __init__(self, name: str, label: str = 'Elapsed time', **kwargs): | ||
|
||
hardcoded_kwargs = ['unit', 'get_cmd', 'set_cmd'] | ||
|
||
for hck in hardcoded_kwargs: | ||
if hck in kwargs: | ||
raise ValueError(f'Can not set "{hck}" for an ' | ||
'ElapsedTimeParameter.') | ||
|
||
super().__init__(name=name, | ||
label=label, | ||
unit='s', | ||
set_cmd=False, | ||
**kwargs) | ||
|
||
self._t0: float = perf_counter() | ||
|
||
def get_raw(self) -> float: | ||
return perf_counter() - self.t0 | ||
|
||
def reset_clock(self) -> None: | ||
self._t0 = perf_counter() | ||
|
||
@property | ||
def t0(self) -> float: | ||
return self._t0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
""" | ||
Tests for the specialized_parameters module | ||
""" | ||
|
||
from time import sleep | ||
|
||
import pytest | ||
|
||
from qcodes.instrument.specialized_parameters import ElapsedTimeParameter | ||
|
||
|
||
def test_elapsed_time_parameter_init(): | ||
|
||
tp1 = ElapsedTimeParameter('time1') | ||
sleep(0.01) | ||
tp2 = ElapsedTimeParameter('time2') | ||
|
||
assert tp1() > tp2() | ||
|
||
|
||
def test_elapsed_time_parameter_monotonic(): | ||
|
||
tp = ElapsedTimeParameter('time') | ||
|
||
times = [tp() for _ in range(25)] | ||
|
||
assert sorted(times) == times | ||
|
||
|
||
def test_elapsed_time_parameter_reset_clock(): | ||
|
||
tp = ElapsedTimeParameter('time') | ||
|
||
sleep(0.01) | ||
t1 = tp() | ||
|
||
tp.reset_clock() | ||
t2 = tp() | ||
|
||
assert t1 > t2 | ||
|
||
|
||
def test_elapsed_time_parameter_not_settable(): | ||
|
||
tp = ElapsedTimeParameter('time') | ||
|
||
with pytest.raises(NotImplementedError): | ||
tp(0) | ||
|
||
|
||
def test_elapsed_time_parameter_forbidden_kwargs(): | ||
|
||
forbidden_kwargs = ['unit', 'get_cmd', 'set_cmd'] | ||
|
||
for fb_kwarg in forbidden_kwargs: | ||
match = f'Can not set "{fb_kwarg}" for an ElapsedTimeParameter' | ||
with pytest.raises(ValueError, match=match): | ||
ElapsedTimeParameter('time', **{fb_kwarg: None}) |