##// END OF EJS Templates
Added basic tests around Amda plugin Python wrapper...
jeandet -
r1340:02cea38fdcb4
parent child
Show More
@@ -0,0 +1,54
1 import sys
2 import os
3 if not hasattr(sys, 'argv'):
4 sys.argv = ['']
5 current_script_path = os.path.dirname(os.path.realpath(__file__))
6 sys.path.append(current_script_path)
7 import amda
8 import pytestamda
9
10 import numpy as np
11 import datetime
12 import time
13 import unittest
14 import ddt
15
16 def wait_for_downloads():
17 while pytestamda.VariableController.hasPendingDownloads():
18 time.sleep(0.1)
19
20 def extract_vector(variable):
21 return zip(*[(pt.x, pt.value(0), pt.value(1), pt.value(2)) for pt in variable])
22
23 def compare_with_ref(var, ref):
24 t_ref, x_ref, y_ref, z_ref = ref
25 t,x,y,z = extract_vector(var)
26 return all([
27 all([t_ref[i].astype(float)/1000000 == t[i] for i in range(len(t))]),
28 all([x_ref[i] == x[i] for i in range(len(x))]),
29 all([y_ref[i] == y[i] for i in range(len(y))]),
30 all([z_ref[i] == z[i] for i in range(len(z))])
31 ])
32
33 @ddt.ddt
34 class FunctionalTests(unittest.TestCase):
35 def setUp(self):
36 pass
37
38 @ddt.data(
39 (datetime.datetime(2012,10,20,8,10,00),datetime.datetime(2012,10,20,12,0,0)),
40 (datetime.datetime(2025,1,1,15,0,0),datetime.datetime(2025,1,1,16,0,0)),
41 (datetime.datetime(2000,1,1,0,0,0),datetime.datetime(2000,1,1,12,0,0))
42 )
43 def test_simple_download(self, case):
44 tstart = case[0]
45 tstop = case[1]
46 pytestamda.TimeController.setTime(pytestamda.SqpRange(tstart, tstop))
47 variable = pytestamda.VariableController.createVariable("bx_gse",pytestamda.amda_provider())
48 wait_for_downloads()
49 t_ref, x_ref, y_ref, z_ref = amda.generate_data(np.datetime64(tstart), np.datetime64(tstop), 4)
50 self.assertTrue( compare_with_ref(variable,(t_ref, x_ref, y_ref, z_ref) ) )
51
52
53 if __name__ == '__main__':
54 unittest.main(exit=False)
@@ -0,0 +1,39
1 import sys
2 import os
3 if not hasattr(sys, 'argv'):
4 sys.argv = ['']
5 current_script_path = os.path.dirname(os.path.realpath(__file__))
6 sys.path.append(current_script_path)
7 import pytestamda
8 import amda
9
10 import numpy as np
11 import datetime
12 import time
13 import unittest
14 import ddt
15
16 path = current_script_path+'/../tests-resources/TestAmdaResultParser/ValidScalar1.txt'
17
18 @ddt.ddt
19 class FunctionalTests(unittest.TestCase):
20 def setUp(self):
21 pass
22
23 @ddt.data(
24 current_script_path+'/../tests-resources/TestAmdaResultParser/ValidScalar1.txt'
25 )
26 def test_correct_scalars(self, case):
27 scalar_sciqlop = pytestamda.AmdaResultParser.readScalarTxt(case)
28 scalar_ref = amda.load_scalar(case)
29 self.assertTrue(len(scalar_ref) == len(scalar_sciqlop))
30 self.assertTrue(all(
31 [scalar_ref[i][1] == scalar_sciqlop[i].value()
32 for i in range(len(scalar_sciqlop))]))
33 self.assertTrue(all(
34 [scalar_ref[i][0].timestamp() == scalar_sciqlop[i].x
35 for i in range(len(scalar_sciqlop))]))
36
37
38 if __name__ == '__main__':
39 unittest.main(exit=False)
@@ -0,0 +1,29
1 import sys
2 import os
3 import numpy as np
4 import datetime
5 import time
6
7 os.environ['TZ'] = 'UTC'
8 epoch_2000 = np.datetime64('2000-01-01T00:00:00',tzinfo=datetime.timezone.utc)
9
10 def load_scalar(fname):
11 with open(fname, "r") as f:
12 return [[
13 datetime.datetime(*(time.strptime(line.split()[0], '%Y-%m-%dT%H:%M:%S.%f')[0:6]),
14 tzinfo=datetime.timezone.utc),
15 float(line.split()[1])]
16 for line in f if "#" not in line]
17
18 """
19 Copied from myAMDA should be factored in somehow
20 """
21 def generate_data(tstart, tstop, dt):
22 delta = np.timedelta64(dt, 's')
23 vector_size = int(np.round((tstop-tstart)/delta)) + 1
24 t = [tstart+i*delta for i in range(vector_size)]
25 x0 = tstart-epoch_2000
26 x = [(x0 + i * delta).astype('float')/1000000 for i in range(vector_size)]
27 y = [(x0 + (i+1) * delta).astype('float')/1000000 for i in range(vector_size)]
28 z = [(x0 + (i+2) * delta).astype('float')/1000000 for i in range(vector_size)]
29 return t,x,y,z
@@ -55,6 +55,9 public:
55 55 QList<std::shared_ptr<Variable> > variablesForMimeData(const QByteArray &mimeData) const;
56 56
57 57 static AcquisitionZoomType getZoomType(const SqpRange &range, const SqpRange &oldRange);
58
59 /// Returns True if there are pending downloads
60 bool hasPendingDownloads();
58 61 signals:
59 62 /// Signal emitted when a variable is about to be deleted from the controller
60 63 void variableAboutToBeDeleted(std::shared_ptr<Variable> variable);
@@ -141,4 +144,5 private:
141 144 spimpl::unique_impl_ptr<VariableControllerPrivate> impl;
142 145 };
143 146
147
144 148 #endif // SCIQLOP_VARIABLECONTROLLER_H
@@ -135,7 +135,7 struct VariableController::VariableControllerPrivate {
135 135 void updateVariableRequest(QUuid varRequestId);
136 136 void cancelVariableRequest(QUuid varRequestId);
137 137 void executeVarRequest(std::shared_ptr<Variable> var, VariableRequest &varRequest);
138
138 bool hasPendingDownloads();
139 139 template <typename VariableIterator>
140 140 void desynchronize(VariableIterator variableIt, const QUuid &syncGroupId);
141 141
@@ -656,6 +656,11 void VariableController::waitForFinish()
656 656 QMutexLocker locker{&impl->m_WorkingMutex};
657 657 }
658 658
659 bool VariableController::hasPendingDownloads()
660 {
661 return impl->hasPendingDownloads();
662 }
663
659 664 AcquisitionZoomType VariableController::getZoomType(const SqpRange &range, const SqpRange &oldRange)
660 665 {
661 666 // t1.m_TStart <= t2.m_TStart && t2.m_TEnd <= t1.m_TEnd
@@ -1079,6 +1084,11 void VariableController::VariableControllerPrivate::executeVarRequest(std::share
1079 1084 }
1080 1085 }
1081 1086
1087 bool VariableController::VariableControllerPrivate::hasPendingDownloads()
1088 {
1089 return !m_VarGroupIdToVarIds.empty();
1090 }
1091
1082 1092 template <typename VariableIterator>
1083 1093 void VariableController::VariableControllerPrivate::desynchronize(VariableIterator variableIt,
1084 1094 const QUuid &syncGroupId)
@@ -48,15 +48,26 target_link_libraries(pytestamdalib PUBLIC pybind11::module)
48 48 target_link_libraries(pytestamdalib PUBLIC pybind11::embed)
49 49 target_link_libraries(pytestamdalib PUBLIC amdaplugin)
50 50
51 declare_test(TestPytestamda TestPytestamda "tests/PyTestAmdaWrapperExe.cpp" "amdaplugin;pytestamdalib")
52 target_compile_definitions(TestPytestamda PRIVATE -DPYTESTAMDA_SCRIPT="${CMAKE_CURRENT_LIST_DIR}/tests/pyamdatests.py")
51 declare_test(TestAmdaFileParserEmbed TestAmdaFileParserEmbed "tests/PyTestAmdaWrapperExe.cpp" "amdaplugin;pytestamdalib")
52 target_compile_definitions(TestAmdaFileParserEmbed PRIVATE -DPYTESTAMDA_SCRIPT="${CMAKE_CURRENT_LIST_DIR}/tests/TestAmdaFileParser.py")
53
54 declare_test(TestAmdaDownloadEmbed TestAmdaDownloadEmbed "tests/PyTestAmdaWrapperExe.cpp" "amdaplugin;pytestamdalib")
55 target_compile_definitions(TestAmdaDownloadEmbed PRIVATE -DPYTESTAMDA_SCRIPT="${CMAKE_CURRENT_LIST_DIR}/tests/TestAmdaDownload.py")
56
53 57
54 58 find_package(PythonInterp 3 REQUIRED)
55 59
56 add_test(NAME pyamdatests
60 add_test(NAME TestAmdaFileParser
61 COMMAND ${PYTHON_EXECUTABLE}
62 ${CMAKE_CURRENT_LIST_DIR}/tests/TestAmdaFileParser.py
63 TestAmdaFileParser)
64 set_tests_properties(TestAmdaFileParser PROPERTIES ENVIRONMENT PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR})
65
66
67 add_test(NAME TestAmdaDownload
57 68 COMMAND ${PYTHON_EXECUTABLE}
58 ${CMAKE_CURRENT_LIST_DIR}/tests/pyamdatests.py
59 pyamdatests)
69 ${CMAKE_CURRENT_LIST_DIR}/tests/TestAmdaDownload.py
70 TestAmdaDownload)
71 set_tests_properties(TestAmdaDownload PROPERTIES ENVIRONMENT PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR})
60 72
61 set_tests_properties(pyamdatests PROPERTIES ENVIRONMENT PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR})
62 73
@@ -26,6 +26,8
26 26 #include <pybind11/pybind11.h>
27 27 #include <pybind11/operators.h>
28 28 #include <pybind11/embed.h>
29 #include <pybind11/numpy.h>
30 #include <pybind11/chrono.h>
29 31
30 32 #include <SqpApplication.h>
31 33 #include <Variable/VariableController.h>
@@ -35,6 +37,7
35 37 #include <Common/DateUtils.h>
36 38 #include <Variable/Variable.h>
37 39 #include <Data/ScalarSeries.h>
40 #include <Data/VectorSeries.h>
38 41
39 42 #include <AmdaProvider.h>
40 43 #include <AmdaResultParser.h>
@@ -45,13 +48,14
45 48 #include <QString>
46 49 #include <QFile>
47 50
51 using namespace std::chrono;
48 52 namespace py = pybind11;
49 53
50 54 std::ostream &operator <<(std::ostream& os, const Unit& u)
51 55 {
52 56 os << "=========================" << std::endl
53 57 << "Unit:" << std::endl
54 << "Name:" << std::endl << u.m_Name.toStdString() << std::endl
58 << " Name: " << u.m_Name.toStdString() << std::endl
55 59 << "Is_TimeUnit: " << u.m_TimeUnit << std::endl;
56 60 return os;
57 61 }
@@ -67,6 +71,25 std::ostream &operator <<(std::ostream& os, const IDataSeries& ds)
67 71 return os;
68 72 }
69 73
74 std::ostream &operator <<(std::ostream& os, const SqpRange& range)
75 {
76 os << "=========================" << std::endl
77 << "SqpRange:" << std::endl
78 << " Start date: " << DateUtils::dateTime(range.m_TStart).toString().toStdString() << std::endl
79 << " Stop date: " << DateUtils::dateTime(range.m_TEnd).toString().toStdString() << std::endl;
80 return os;
81 }
82
83 std::ostream &operator <<(std::ostream& os, const Variable& variable)
84 {
85 os << "=========================" << std::endl
86 << "Variable:" << std::endl
87 << " Name: " << variable.name().toStdString() << std::endl
88 << " range: " << std::endl << variable.range() << std::endl
89 << " cache range: " << std::endl << variable.cacheRange() << std::endl;
90 return os;
91 }
92
70 93 template <typename T>
71 94 std::string __repr__(const T& obj)
72 95 {
@@ -76,9 +99,21 std::string __repr__(const T& obj)
76 99 }
77 100
78 101
102
103
79 104 PYBIND11_MODULE(pytestamda, m){
105 int argc = 0;
106 char ** argv=nullptr;
107 SqpApplication::setOrganizationName("LPP");
108 SqpApplication::setOrganizationDomain("lpp.fr");
109 SqpApplication::setApplicationName("SciQLop");
110 static SqpApplication app(argc, argv);
111
80 112 m.doc() = "hello";
81 113
114 auto amda_provider = std::make_shared<AmdaProvider>();
115 m.def("amda_provider",[amda_provider](){return amda_provider;}, py::return_value_policy::copy);
116
82 117 py::enum_<DataSeriesType>(m, "DataSeriesType")
83 118 .value("SCALAR", DataSeriesType::SCALAR)
84 119 .value("SPECTROGRAM", DataSeriesType::SPECTROGRAM)
@@ -92,25 +127,50 PYBIND11_MODULE(pytestamda, m){
92 127 .def(py::self != py::self)
93 128 .def("__repr__",__repr__<Unit>);
94 129
130 py::class_<DataSeriesIteratorValue>(m,"DataSeriesIteratorValue")
131 .def_property_readonly("x", &DataSeriesIteratorValue::x)
132 .def("value", py::overload_cast<>(&DataSeriesIteratorValue::value, py::const_))
133 .def("value", py::overload_cast<int>(&DataSeriesIteratorValue::value, py::const_));
134
95 135 py::class_<IDataSeries, std::shared_ptr<IDataSeries>>(m, "IDataSeries")
96 136 .def("nbPoints", &IDataSeries::nbPoints)
97 137 .def_property_readonly("xAxisUnit", &IDataSeries::xAxisUnit)
98 138 .def_property_readonly("yAxisUnit", &IDataSeries::yAxisUnit)
99 139 .def_property_readonly("valuesUnit", &IDataSeries::valuesUnit)
140 .def("__getitem__", [](IDataSeries& serie, int key) {
141 return *(serie.begin()+key);
142 }, py::is_operator())
143 .def("__len__", &IDataSeries::nbPoints)
144 .def("__iter__", [](IDataSeries& serie) {
145 return py::make_iterator(serie.begin(), serie.end());
146 }, py::keep_alive<0, 1>())
100 147 .def("__repr__",__repr__<IDataSeries>);
101 148
102
103
104 149 py::class_<ScalarSeries, std::shared_ptr<ScalarSeries>, IDataSeries>(m, "ScalarSeries")
105 150 .def("nbPoints", &ScalarSeries::nbPoints);
106 151
152 py::class_<VectorSeries, std::shared_ptr<VectorSeries>, IDataSeries>(m, "VectorSeries")
153 .def("nbPoints", &VectorSeries::nbPoints);
154
107 155 py::class_<QString>(m, "QString")
108 156 .def(py::init([](const std::string& value){return QString::fromStdString(value);}))
109 157 .def("__repr__", &QString::toStdString);
110 158
111 py::class_<VariableController>(m, "VariableController");
159 py::class_<VariableController>(m, "VariableController")
160 .def_static("createVariable",[](const QString &name,
161 std::shared_ptr<IDataProvider> provider){
162 return sqpApp->variableController().createVariable(name, {{"dataType", "vector"}, {"xml:id", "c1_b"}}, provider);
163 })
164 .def_static("hasPendingDownloads",
165 [](){return sqpApp->variableController().hasPendingDownloads();}
166 );
112 167
113 py::class_<AmdaProvider>(m, "AmdaProvider");
168 py::class_<TimeController>(m,"TimeController")
169 .def_static("setTime", [](SqpRange range){sqpApp->timeController().onTimeToUpdate(range);});
170
171 py::class_<IDataProvider, std::shared_ptr<IDataProvider>>(m, "IDataProvider");
172
173 py::class_<AmdaProvider, std::shared_ptr<AmdaProvider>, IDataProvider>(m, "AmdaProvider");
114 174
115 175 py::class_<AmdaResultParser>(m, "AmdaResultParser")
116 176 .def_static("readTxt", AmdaResultParser::readTxt)
@@ -118,25 +178,50 PYBIND11_MODULE(pytestamda, m){
118 178 return std::dynamic_pointer_cast<ScalarSeries>(AmdaResultParser::readTxt(path, DataSeriesType::SCALAR));
119 179 }, py::return_value_policy::copy);
120 180
121 py::class_<Variable>(m, "Variable")
181 py::class_<Variable,std::shared_ptr<Variable>>(m, "Variable")
122 182 .def(py::init<const QString&>())
123 183 .def_property("name", &Variable::name, &Variable::setName)
124 184 .def_property("range", &Variable::range, &Variable::setRange)
125 .def_property("cacheRange", &Variable::cacheRange, &Variable::setCacheRange);
185 .def_property("cacheRange", &Variable::cacheRange, &Variable::setCacheRange)
186 .def_property_readonly("nbPoints", &Variable::nbPoints)
187 .def_property_readonly("dataSeries", &Variable::dataSeries)
188 .def("__len__", [](Variable& variable) {
189 auto rng = variable.dataSeries()->xAxisRange(variable.range().m_TStart,variable.range().m_TEnd);
190 return std::distance(rng.first,rng.second);
191 })
192 .def("__iter__", [](Variable& variable) {
193 auto rng = variable.dataSeries()->xAxisRange(variable.range().m_TStart,variable.range().m_TEnd);
194 return py::make_iterator(rng.first, rng.second);
195 }, py::keep_alive<0, 1>())
196 .def("__getitem__", [](Variable& variable, int key) {
197 //insane and slow!
198 auto rng = variable.dataSeries()->xAxisRange(variable.range().m_TStart,variable.range().m_TEnd);
199 if(key<0)
200 return *(rng.second+key);
201 else
202 return *(rng.first+key);
203 })
204 .def("__repr__",__repr__<Variable>);
126 205
127 206 py::implicitly_convertible<std::string, QString>();
128 207
129 py::class_<TimeController>(m,"TimeController");
130 208
131 209 py::class_<SqpRange>(m,"SqpRange")
132 210 .def("fromDateTime", &SqpRange::fromDateTime, py::return_value_policy::move)
133 211 .def(py::init([](double start, double stop){return SqpRange{start, stop};}))
134 .def("__repr__", [](const SqpRange& range){
135 QString repr = QString("SqpRange:\n Start date: %1\n Stop date: %2")
136 .arg(DateUtils::dateTime(range.m_TStart).toString())
137 .arg(DateUtils::dateTime(range.m_TEnd).toString());
138 return repr.toStdString();
139 });
212 .def(py::init([](system_clock::time_point start, system_clock::time_point stop)
213 {
214 double start_ = 0.001 * duration_cast<milliseconds>(start.time_since_epoch()).count();
215 double stop_ = 0.001 * duration_cast<milliseconds>(stop.time_since_epoch()).count();
216 return SqpRange{start_, stop_};
217 }))
218 .def_property_readonly("start", [](const SqpRange& range){
219 return system_clock::from_time_t(range.m_TStart);
220 })
221 .def_property_readonly("stop", [](const SqpRange& range){
222 return system_clock::from_time_t(range.m_TEnd);
223 })
224 .def("__repr__", __repr__<SqpRange>);
140 225
141 226 py::class_<QUuid>(m,"QUuid");
142 227
@@ -149,14 +234,9 PYBIND11_MODULE(pytestamda, m){
149 234 }
150 235
151 236
152 int pytestamda_test(int argc, char** argv, const char* testScriptPath )
237 int pytestamda_test(const char* testScriptPath )
153 238 {
154 SqpApplication::setOrganizationName("LPP");
155 SqpApplication::setOrganizationDomain("lpp.fr");
156 SqpApplication::setApplicationName("SciQLop");
157 SqpApplication app(argc, argv);
158 239 py::scoped_interpreter guard{};
159
160 240 py::globals()["__file__"] = py::str(testScriptPath);
161 241 py::eval_file(testScriptPath);
162 242 return 0;
@@ -20,11 +20,11
20 20 -- Mail : alexis.jeandet@member.fsf.org
21 21 ----------------------------------------------------------------------------*/
22 22 #include <QString>
23 extern int pytestamda_test(int argc, char** argv, const char* testScriptPath );
23 extern int pytestamda_test(const char* testScriptPath );
24 24
25 25 int main(int argc, char** argv)
26 26 {
27 pytestamda_test(argc, argv, PYTESTAMDA_SCRIPT);
27 pytestamda_test(PYTESTAMDA_SCRIPT);
28 28 return 0;
29 29 }
30 30
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now