Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

daq.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import logging
  2. import os
  3. import sys
  4. import time
  5. import cerebus.cbpy as cb
  6. import numpy as np
  7. from aux import log
  8. class Daq:
  9. """
  10. Description: This class contains all methods and variables related to
  11. communication with the NSP.
  12. """
  13. def __init__(self):
  14. """
  15. Opens the connection to the NSP and returns error if NSP is offline.
  16. Writes the connection parameters to the params
  17. (see Data class for info on params).
  18. """
  19. log.info("Initializing Daq class")
  20. log.info("Opening cbpy connection to NSP...")
  21. # Try opening a connection
  22. res = cb.open()
  23. if res[0] < 0:
  24. log.critical("failed to open cbpy connection with NSP")
  25. time.sleep(1)
  26. # If success, return result and add connection parameters to params
  27. if res[0] >= 0:
  28. log.info("Success! NSP Initialized")
  29. # Save the connection parameters to params
  30. self.instance = res[0]
  31. self.connection = res[1]['connection']
  32. self.instrument = res[1]['instrument']
  33. elif res[0] < 0:
  34. log.warning("Could not open cbpy connection to NSP... retrying")
  35. self.close_connection()
  36. time.sleep(0.5)
  37. res = cb.open()
  38. if res[0] < 0:
  39. log.critical("NSP not online / misbehaving. Please check if reboot NSP.")
  40. sys.exit(1)
  41. return None
  42. def close_connection(self):
  43. """
  44. Close the connection to the NSP. Stop running BCI.
  45. """
  46. res_close = cb.close()
  47. if res_close >= 0:
  48. log.info("cbpy connection to NSP closed.")
  49. return None
  50. def test_channel_config(self, channels=range(1, 97), group=5):
  51. """Check if NSP is broadcasting the queried channels at the right srate"""
  52. res = cb.trial_config(reset=True, instance=self.instance)
  53. time.sleep(0.2)
  54. # data = cb.trial_continuous()
  55. # nChansBroadcast = len(data[1])
  56. # print(nChansBroadcast)
  57. extraChans = np.setdiff1d(list(range(1, 97)), list(channels))
  58. if len(extraChans) > 0:
  59. log.info("Checking unconfigured channels")
  60. for channel in extraChans:
  61. c = cb.get_channel_config(channel)
  62. if c[1]['smpgroup'] != 0:
  63. res = cb.set_channel_config(channel, chaninfo={'smpgroup':0})
  64. if res == 0:
  65. log.info("turned of channel {}".format(channel))
  66. time.sleep(0.05)
  67. count = self.validate_channels_srate(channels, group)
  68. if count > 0:
  69. # Configure the channels and start recording
  70. log.info("Assigning channels: {} to group: {}".format(list(channels), group))
  71. self.configure_channels(channels, group)
  72. # time.sleep(0.2)
  73. count = self.validate_channels_srate(channels, group)
  74. if count > 0:
  75. log.error("Failed to configure {} channels. Restart NSP.".format(count))
  76. res = self.close_connection()
  77. return None
  78. def validate_channels_srate(self, channels=range(1, 97), group=5):
  79. cb.trial_config()
  80. time.sleep(0.2)
  81. data = cb.trial_continuous(reset=True)
  82. """ Check if channels have been configured"""
  83. count = 0
  84. for channel in channels:
  85. time.sleep(0.2)
  86. # assert data[1][channel - 1][2] == 30000, 'channel no %r not configured correctly' % channel
  87. c = cb.get_channel_config(channel, instance=self.instance)
  88. if c[1]['smpgroup'] != group:
  89. count += 1
  90. log.warning("Channel {} is in {} instead of {}".format(count, c[1]['smpgroup'], group))
  91. if count == 0:
  92. log.info("{} channels are in group {}".format(len(list(channels)), group))
  93. else:
  94. log.info("{} channels are not in group ".format(count, group))
  95. # print("Please restart nPlayServer to reset channels")
  96. return count
  97. def configure_channels(self, channels=list(range(1, 97)), group=5):
  98. """
  99. Configures 96 channels at 30k
  100. inputs:
  101. channels: List of channels
  102. Default: [1:96]
  103. sampling_group: The sampling group to assign to the channel.
  104. They are 1 to 5 and defined as follows:
  105. 1 = 0.5Khz, 2 = 1Khz, 3 = 2Khz, 4 = 10Khz, 5 =
  106. Example:
  107. To set channels 5, 6 and 7 use at 30k use:
  108. >>> channels = [5, 6, 7]
  109. >>> daq.configure_channels(channels, 5)
  110. Currently, turning channels off is not supported.
  111. """
  112. for channel in channels:
  113. res = cb.set_channel_config(channel, chaninfo={'smpgroup': group}, instance=self.instance)
  114. time.sleep(0.2)
  115. if res != 0:
  116. log.warning("failed for channel {}".format(channel))
  117. elif res == 0:
  118. log.info("Assigned ch{} to group {}".format(channel, group))
  119. return None
  120. if __name__ == '__main__':
  121. daq = Daq()
  122. time.sleep(0.5)
  123. res = daq.test_channel_config(range(1, 11), group=5)