From e718ad47d2bc7827c3019129b445d69687ae0d1f 2019-03-20 09:59:22 From: Alexis Jeandet Date: 2019-03-20 09:59:22 Subject: [PATCH] Spectrogram wrapper allow to set values from python Signed-off-by: Alexis Jeandet --- diff --git a/external/TimeSeries b/external/TimeSeries index 2146d47..e08e903 160000 --- a/external/TimeSeries +++ b/external/TimeSeries @@ -1 +1 @@ -Subproject commit 2146d47985f44d6914b224c3b99ebf9e16e87b3f +Subproject commit e08e903b65e9d884187e1809c75b22379783bfee diff --git a/include/Data/SpectrogramTimeSerie.h b/include/Data/SpectrogramTimeSerie.h index 930efd5..30988df 100644 --- a/include/Data/SpectrogramTimeSerie.h +++ b/include/Data/SpectrogramTimeSerie.h @@ -10,7 +10,13 @@ class SCIQLOP_CORE_EXPORT SpectrogramTimeSerie { public: using item_t = - decltype(TimeSeries::TimeSerie()[0]); + decltype(std::declval< + TimeSeries::TimeSerie>()[0]); + + using iterator_t = decltype( + std::declval>() + .begin()); + SpectrogramTimeSerie() {} ~SpectrogramTimeSerie() = default; using TimeSerie::TimeSerie; diff --git a/src/pybind11_wrappers/CoreWrappers.cpp b/src/pybind11_wrappers/CoreWrappers.cpp index de5b9d3..66ecb76 100644 --- a/src/pybind11_wrappers/CoreWrappers.cpp +++ b/src/pybind11_wrappers/CoreWrappers.cpp @@ -73,15 +73,12 @@ void copy_spectro(py::array_t& t, py::array_t& values, auto t_view = t.unchecked<1>(); auto values_view = values.unchecked<2>(); const auto width = values.shape(0); - std::cout << "WIDTH" << width << std::endl; for(std::size_t i = 0; i < t.size(); i++) { dest_t[i] = t_view[i]; for(int j = 0; j < width; j++) { dest_values[i * width + j] = values_view(j, i); - std::cout << "dest_values[" << i * width + j << "] = values_view(" << j - << ", " << i << ") = " << values_view(j, i) << std::endl; } } } @@ -278,9 +275,12 @@ PYBIND11_MODULE(pysciqlopcore, m) *(ts.begin() + key) = value; }); - py::class_(m, "SpectrogramTimeSerieItem") - .def("__getitem__", [](SpectrogramTimeSerie::item_t& self, - std::size_t key) { return self[key]; }); + py::class_(m, "SpectrogramTimeSerieItem") + .def("__getitem__", [](SpectrogramTimeSerie::iterator_t& self, + std::size_t key) { return (*self)[key]; }) + .def("__setitem__", + [](SpectrogramTimeSerie::iterator_t& self, std::size_t key, + double value) { (*self)[key] = value; }); py::class_( m, "SpectrogramTimeSerie") @@ -299,7 +299,10 @@ PYBIND11_MODULE(pysciqlopcore, m) return SpectrogramTimeSerie(_t, _values, shape); })) .def("__getitem__", - [](SpectrogramTimeSerie& ts, std::size_t key) { return ts[key]; }); + [](SpectrogramTimeSerie& ts, + std::size_t key) -> SpectrogramTimeSerie::iterator_t { + return ts.begin() + key; + }); py::class_>(m, "Variable2") .def(py::init()) diff --git a/tests/TestVariables.py b/tests/TestVariables.py index 15f3d43..7c18198 100755 --- a/tests/TestVariables.py +++ b/tests/TestVariables.py @@ -39,6 +39,24 @@ class TimeSeriesData(unittest.TestCase): ts[0]=123. self.assertEqual(ts[0],123.) + def test_set_VectorTimeSerie_values(self): + ts = pysciqlopcore.VectorTimeSerie(10) + ts.t[0]=111. + self.assertEqual(ts.t[0],111.) + ts[0].x=111. + ts[0].y=222. + ts[0].z=333. + self.assertEqual(ts[0].x,111.) + self.assertEqual(ts[0].y,222.) + self.assertEqual(ts[0].z,333.) + + def test_set_SpectrogramTimeSerie_values(self): + ts = pysciqlopcore.SpectrogramTimeSerie((10,100)) + ts.t[0]=111. + self.assertEqual(ts.t[0],111.) + ts[0][11]=123. + self.assertEqual(ts[0][11],123.) + def test_build_ScalarTimeSerie_from_np_arrays(self): ts = pysciqlopcore.ScalarTimeSerie(np.arange(10), np.arange(10)*10) for i in range(len(ts)): @@ -81,8 +99,20 @@ class TimeSeriesData(unittest.TestCase): ts = pysciqlopcore.SpectrogramTimeSerie(np.arange(10), v) for i in range(len(ts)): for j in range(4): - print(f"ts[{i}][{j}] = " + str(ts[i][j])) + self.assertEqual(ts[i][j], i*10**j) + +class VariableData(unittest.TestCase): + def test_default_state(self): + v=pysciqlopcore.Variable2("hello") + self.assertEqual(str(v.name), str("hello")) + self.assertEqual(type(v.data), type(None)) + self.assertEqual(len(v), 0) + def test_set_name(self): + v=pysciqlopcore.Variable2("hello") + self.assertEqual(str(v.name), str("hello")) + v.name="newName" + self.assertEqual(str(v.name), str("newName")) if __name__ == '__main__': unittest.main(exit=False)