You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

109 lines
3.6 KiB

  1. # -*- coding: utf-8 -*-
  2. import nilmdb.server
  3. import nilmdb.client
  4. import nilmdb.client.numpyclient
  5. from nilmdb.utils.printf import *
  6. from nilmdb.utils import timestamper
  7. from nilmdb.client import ClientError, ServerError
  8. from nilmdb.utils import datetime_tz
  9. from nose.plugins.skip import SkipTest
  10. from nose.tools import *
  11. from nose.tools import assert_raises
  12. import itertools
  13. import distutils.version
  14. from testutil.helpers import *
  15. import numpy as np
  16. testdb = "tests/numpyclient-testdb"
  17. testurl = "http://localhost:32180/"
  18. def setup_module():
  19. global test_server, test_db
  20. # Clear out DB
  21. recursive_unlink(testdb)
  22. # Start web app on a custom port
  23. test_db = nilmdb.utils.serializer_proxy(nilmdb.server.NilmDB)(testdb)
  24. test_server = nilmdb.server.Server(test_db, host = "127.0.0.1",
  25. port = 32180, stoppable = False,
  26. fast_shutdown = True,
  27. force_traceback = True)
  28. test_server.start(blocking = False)
  29. def teardown_module():
  30. global test_server, test_db
  31. # Close web app
  32. test_server.stop()
  33. test_db.close()
  34. class TestNumpyClient(object):
  35. def test_numpyclient_01_basic(self):
  36. # Test basic connection
  37. client = nilmdb.client.numpyclient.NumpyClient(url = testurl)
  38. version = client.version()
  39. eq_(distutils.version.LooseVersion(version),
  40. distutils.version.LooseVersion(test_server.version))
  41. # Verify subclassing
  42. assert(isinstance(client, nilmdb.client.Client))
  43. # Layouts
  44. for layout in "int8_t", "something_8", "integer_1":
  45. with assert_raises(ValueError):
  46. for x in client.stream_extract_numpy("/foo", layout=layout):
  47. pass
  48. for layout in "int8_1", "uint8_30", "int16_20", "float64_100":
  49. with assert_raises(ClientError) as e:
  50. for x in client.stream_extract_numpy("/foo", layout=layout):
  51. pass
  52. in_("No such stream", str(e.exception))
  53. with assert_raises(ClientError) as e:
  54. for x in client.stream_extract_numpy("/foo"):
  55. pass
  56. in_("can't get layout for path", str(e.exception))
  57. client.close()
  58. def test_numpyclient_02_extract(self):
  59. client = nilmdb.client.numpyclient.NumpyClient(url = testurl)
  60. # Insert some data as text
  61. client.stream_create("/newton/prep", "float32_8")
  62. testfile = "tests/data/prep-20120323T1000"
  63. start = nilmdb.utils.time.parse_time("20120323T1000")
  64. rate = 120
  65. data = timestamper.TimestamperRate(testfile, start, rate)
  66. result = client.stream_insert("/newton/prep", data,
  67. start, start + 119999777)
  68. # Extract Numpy arrays
  69. array = None
  70. pieces = 0
  71. for chunk in client.stream_extract_numpy("/newton/prep", maxrows=1000):
  72. pieces += 1
  73. if array is not None:
  74. array = np.vstack((array, chunk))
  75. else:
  76. array = chunk
  77. eq_(array.shape, (14400, 9))
  78. eq_(pieces, 15)
  79. # Try structured
  80. s = list(client.stream_extract_numpy("/newton/prep", structured = True))
  81. assert(np.array_equal(np.c_[s[0]['timestamp'], s[0]['data']], array))
  82. # Compare. Will be close but not exact because the conversion
  83. # to and from ASCII was lossy.
  84. data = timestamper.TimestamperRate(testfile, start, rate)
  85. actual = np.fromstring(" ".join(data), sep=' ').reshape(14400, 9)
  86. assert(np.allclose(array, actual))
  87. client.close()