Skip to content

Make data saver accept numpy type floats/ints #1225

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

Merged
merged 9 commits into from
Aug 15, 2018
18 changes: 14 additions & 4 deletions qcodes/dataset/measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from time import monotonic
from collections import OrderedDict
from typing import (Callable, Union, Dict, Tuple, List, Sequence, cast,
MutableMapping, MutableSequence, Optional)
MutableMapping, MutableSequence, Optional, Any)
from inspect import signature
from numbers import Number

Expand All @@ -19,13 +19,23 @@
log = logging.getLogger(__name__)

array_like_types = (tuple, list, np.ndarray)
non_array_like_types = (int, float, str)


class ParameterTypeError(Exception):
pass


def is_number(thing: Any) -> bool:
"""
Test if an object can be converted to a number
"""
try:
float(thing)
return True
except (ValueError, TypeError):
return False


class DataSaver:
"""
The class used byt the Runner context manager to handle the
Expand Down Expand Up @@ -56,7 +66,7 @@ def __init__(self, dataset: DataSet, write_period: float,

def add_result(self,
*res_tuple: Tuple[Union[_BaseParameter, str],
Union[str, int, float, np.ndarray]])-> None:
Union[str, int, float, np.dtype, np.ndarray]])-> None:
"""
Add a result to the measurement results. Represents a measurement
point in the space of measurement parameters, e.g. in an experiment
Expand Down Expand Up @@ -114,7 +124,7 @@ def add_result(self,
f'and {array_size}')
else:
input_size = array_size
elif any(isinstance(value, t) for t in non_array_like_types):
elif is_number(value) or isinstance(value, str):
pass
else:
raise ValueError('Wrong value type received. '
Expand Down
20 changes: 20 additions & 0 deletions qcodes/tests/dataset/test_datasaver.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import pytest
import os
import tempfile
import numpy as np

import qcodes as qc
from qcodes.dataset.measurements import DataSaver
from qcodes.dataset.param_spec import ParamSpec
from qcodes.dataset.sqlite_base import connect, init_db
from qcodes.dataset.database import initialise_database

Expand Down Expand Up @@ -71,3 +74,20 @@ def test_default_callback(experiment):
DataSaver.default_callback = None
if test_set is not None:
test_set.conn.close()


def test_numpy_types(experiment):
"""
Test that we can save numpy types in the dataset
"""

p = ParamSpec("p", "numeric")
test_set = qc.new_data_set("test-dataset")
data_saver = DataSaver(
dataset=test_set, write_period=0, parameters={"p": p})

dtypes = [np.int8, np.int16, np.int32, np.int64, np.float16, np.float32,
np.float64]

for dtype in dtypes:
data_saver.add_result(("p", dtype(2)))