test_numpy_data_accessor.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright 2021-2025 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import os
  12. from importlib import util
  13. import numpy
  14. import pandas
  15. from taipy.gui import Gui
  16. from taipy.gui.data.data_format import _DataFormat
  17. from taipy.gui.data.numpy_data_accessor import _NumpyDataAccessor
  18. a_numpy_array = numpy.array([1, 2, 3])
  19. def test_simple_data(gui: Gui, helpers):
  20. accessor = _NumpyDataAccessor(gui)
  21. ret_data = accessor.get_data("x", a_numpy_array, {"start": 0, "end": -1}, _DataFormat.JSON)
  22. assert ret_data
  23. value = ret_data["value"]
  24. assert value
  25. assert value["rowcount"] == 3
  26. data = value["data"]
  27. assert len(data) == 3
  28. def test_simple_data_with_arrow(gui: Gui, helpers):
  29. if util.find_spec("pyarrow"):
  30. accessor = _NumpyDataAccessor(gui)
  31. ret_data = accessor.get_data("x", a_numpy_array, {"start": 0, "end": -1}, _DataFormat.APACHE_ARROW)
  32. assert ret_data
  33. value = ret_data["value"]
  34. assert value
  35. assert value["rowcount"] == 3
  36. data = value["data"]
  37. assert isinstance(data, bytes)
  38. def test_slice(gui: Gui, helpers):
  39. accessor = _NumpyDataAccessor(gui)
  40. value = accessor.get_data("x", a_numpy_array, {"start": 0, "end": 1}, _DataFormat.JSON)["value"]
  41. assert value["rowcount"] == 3
  42. data = value["data"]
  43. assert len(data) == 2
  44. value = accessor.get_data("x", a_numpy_array, {"start": "0", "end": "1"}, _DataFormat.JSON)["value"]
  45. data = value["data"]
  46. assert len(data) == 2
  47. def test_csv(gui, small_dataframe):
  48. accessor = _NumpyDataAccessor(gui)
  49. pd = small_dataframe
  50. path = accessor.to_csv("", pd)
  51. assert path is not None
  52. assert os.path.getsize(path) > 0
  53. def test__from_pandas(gui):
  54. accessor = _NumpyDataAccessor(gui)
  55. ad = accessor._from_pandas(pandas.DataFrame(a_numpy_array), numpy.ndarray)
  56. assert isinstance(ad, numpy.ndarray)
  57. assert len(ad) == 3