Debian Codegraph @

tests/experiment/test_results.py

raw
1 #
2 # This file is part of the PyMeasure package.
3 #
4 # Copyright (c) 2013-2024 PyMeasure Developers
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24
25 import os
26 import pickle
27 import tempfile
28 from unittest import mock
29
30 import pandas as pd
31 import pytest
32 import numpy as np
33
34 from pymeasure.units import ureg
35 from pymeasure.experiment.results import Results, CSVFormatter
36 from pymeasure.experiment.procedure import Procedure, Parameter
37 from pymeasure.experiment import BooleanParameter
38 from data.procedure_for_testing import RandomProcedure
39
40
41 def test_procedure():
42 """ Ensure that the loaded test procedure is properly functioning
43 """
44 procedure = RandomProcedure()
45 assert procedure.iterations == 100
46 assert procedure.delay == 0.001
47 assert hasattr(procedure, 'execute')
48
49
50 def test_csv_formatter_format_header():
51 """Tests CSVFormatter.format_header() method."""
52 columns = ['t', 'x', 'y', 'z', 'V']
53 formatter = CSVFormatter(columns=columns)
54 assert formatter.format_header() == 't,x,y,z,V'
55
56
57 class Test_csv_formatter_format:
58 def test_csv_formatter_format(self):
59 """Tests CSVFormatter.format() method."""
60 columns = ['t', 'x', 'y', 'z', 'V']
61 formatter = CSVFormatter(columns=columns)
62 data = {'t': 1, 'y': 2, 'z': 3.0, 'x': -1, 'V': 'abc'}
63 assert formatter.format(data) == '1,-1,2,3.0,abc'
64
65 @pytest.mark.parametrize("head, value, result",
66 (('index', 10, "10"),
67 ('length (m)', "50 cm", "0.5"),
68 ('voltage (V)', ureg.Quantity(-7, ureg.kV), "-7000.0"),
69 ('speed (m/s)', 15 * ureg.cm / ureg.s, "0.15"),
70 ('magnetic (T)', 7, "7"),
71 ('string', "abcdef", "abcdef"),
72 ('count', 9 * ureg.dimensionless, "9"),
73 ('boolean', True, "True"),
74 ('numpy (V)', np.float64(1.1), "1.1"),
75 ('boolean nan (V)', True, "nan"),
76 ))
77 def test_unitful(self, head, value, result):
78 """Test, whether units are appended correctly"""
79 formatter = CSVFormatter(columns=[head])
80 assert formatter.format({head: value}) == result
81
82 def test_newly_unitful(self):
83 formatter = CSVFormatter(columns=["count"])
84 assert formatter.format({'count': 5 * ureg.km}) == "5000.0"
85 assert formatter.units['count'] == ureg.m
86
87 def test_no_newly_unitful(self):
88 formatter = CSVFormatter(columns=["count"])
89 assert formatter.format({'count': 5 * ureg.dimensionless}) == "5"
90 assert formatter.units.get('count') is None
91
92 def test_unitful_erroneous(self):
93 """Test, whether wrong units are rejected"""
94 columns = ['index', 'length (m)', 'voltage (V)']
95 formatter = CSVFormatter(columns=columns)
96 formatter.units['index'] = ureg.km
97 data = {'index': "10 stupid", 'length (m)': "50 cV", 'voltage (V)': True}
98 assert formatter.format(data) == "nan,nan,nan"
99
100
101 def test_procedure_filestorage():
102 assert RandomProcedure.iterations.value == 100
103 procedure = RandomProcedure()
104 procedure.iterations = 101
105 resultfile = tempfile.mktemp()
106 results = Results(procedure, resultfile)
107
108 new_results = pickle.loads(pickle.dumps(results))
109 assert hasattr(new_results, 'procedure')
110 assert new_results.procedure.iterations == 101
111 assert RandomProcedure.iterations.value == 100
112
113
114 class TestResults:
115 # TODO: add a full set of Results tests
116
117 @mock.patch('pymeasure.experiment.results.open', mock.mock_open(), create=True)
118 @mock.patch('os.path.exists', return_value=True)
119 @mock.patch('pymeasure.experiment.results.pd.read_csv')
120 def test_regression_attr_data_when_up_to_date_should_retain_dtype(self,
121 read_csv_mock,
122 path_exists_mock):
123 procedure_mock = mock.MagicMock(spec=Procedure)
124 result = Results(procedure_mock, 'test.csv')
125
126 read_csv_mock.return_value = [pd.DataFrame(data={
127 'A': [1, 2, 3, 4, 5, 6, 7],
128 'B': [2, 3, 4, 5, 6, 7, 8]
129 })]
130 first_data = result.data
131
132 # if no updates, read_csv returns a zero-row dataframe
133 read_csv_mock.return_value = [pd.DataFrame(data={
134 'A': [], 'B': []
135 }, dtype=object)]
136 second_data = result.data
137
138 assert second_data.iloc[:, 0].dtype is not object
139 assert first_data.iloc[:, 0].dtype is second_data.iloc[:, 0].dtype
140
141 def test_regression_param_str_should_not_include_newlines(self, tmpdir):
142 class DummyProcedure(Procedure):
143 par = Parameter('Generic Parameter with newline chars')
144 DATA_COLUMNS = ['Foo', 'Bar', 'Baz']
145 procedure = DummyProcedure()
146 procedure.par = np.linspace(1, 100, 17)
147 filename = os.path.join(str(tmpdir), 'header_linebreak_test.csv')
148 result = Results(procedure, filename)
149 result.reload() # assert no error
150 pd.read_csv(filename, comment="#") # assert no error
151 assert (result.parameters['par'].value == np.linspace(1, 100, 17)).all()
152
153
154 def test_parameter_reading():
155 data_path = os.path.join(os.path.dirname(__file__), "data/results_for_testing_parameters.csv")
156 test_string = "/test directory with space/test_filename.csv"
157 iterations = 101
158 delay = 0.0005
159 seed = '54321'
160
161 class DummyProcedure(RandomProcedure):
162 check_false = BooleanParameter('checkbox False')
163 check_true = BooleanParameter('checkbox True')
164 check_dir = Parameter('Directory string')
165
166 results = Results.load(data_path, procedure_class=DummyProcedure)
167
168 # Check if all parameters are correctly read from file
169 assert results.parameters["iterations"].value == iterations
170 assert results.parameters["delay"].value == delay
171 assert results.parameters["seed"].value == seed
172
173 assert results.parameters["check_true"].value is True
174 assert results.parameters["check_false"].value is False
175 assert results.parameters["check_dir"].value == test_string