def server(): ''' Starts a server and sends several messages and terminates ''' device_coordinator = DeviceCoordinator() socket_device = SocketNetworkDevice("0.0.0.0", 5002) device_coordinator.add_devices([socket_device]) time.sleep(2) input("Press enter to start sending marker") message = start_message("test", "00") device_coordinator.dispatch(message) time.sleep(2) message = stop_message("test", "00") device_coordinator.dispatch(message) time.sleep(2) message = start_message("test", "01") device_coordinator.dispatch(message) time.sleep(2) message = stop_message("test", "01") device_coordinator.dispatch(message) time.sleep(3) device_coordinator.terminate()
def add_sensors(): # Creating an instance of sensor my_camera = CameraStreaming(camera_no=0, name="camera", output_path="./output") # Creating an instance of device coordinator device_coordinator = DeviceCoordinator() # Adding sensor to device coordinator device_coordinator.add_devices([my_camera]) experiment_id = "p01" stimuli_id = "S00" input("Press a button to start data recording") # Starts data recording device_coordinator.dispatch(start_message(experiment_id, stimuli_id)) time.sleep(5) # Stops deta recording device_coordinator.dispatch(stop_message(experiment_id, stimuli_id)) time.sleep(0.5) # Terminate, This step is necessary to close the connection with added devices device_coordinator.terminate()
def simple_scenario(stimuli_path): # Reading image stimuli and assigning an ID to them based on their alphabetical order stimuli_list = os.listdir(stimuli_path) stimuli_list.sort() stimuli = {} i = 0 for item in stimuli_list: stimuli[i] = item i += 1 print("initializing") # Creating an instance of simmer3 my_shimmer = Shimmer3Streaming(name="Shimmer3_sensor", output_path="./output") # Creating an instance of camera. by uncommenting this line and adding it to the dive_coordinator # you can record video data as well # my_camera = CameraStreaming(0, name="camera", output_path="./output", ) # Creating an instance of device coordinator device_coordinator = DeviceCoordinator() # Adding sensor to device coordinator device_coordinator.add_devices([my_shimmer]) experiment_id = "p01" # A delay to be sure initialing devices have finished time.sleep(3) input("\nPress a key to run the scenario") for stimuli_id, stmulus_name in stimuli.items(): # Starts data recording by displaying the image device_coordinator.dispatch(start_message(experiment_id, stimuli_id)) # Displaying image may start with some miliseconds delay after data recording # because of GTK initialization in show_image_standalone. If this delay is important to you, # use other tools for displaying image stimuli # Since image is displaying in another thread we have to manually create the same delay in current # thread to record data for 10 seconds timeout = 5 stimulus = ImageStimulus(stimuli_id, os.path.join(stimuli_path, stmulus_name), 5) stimulus.show_standalone() time.sleep(timeout) # IF the stimuli is a video we are displaying stimuli as follows #stimulus = VideoStimulus(stimuli_id, os.path.join(stimuli_path, stmulus_name)) #stimulus.show() # Stops data recording by closing image device_coordinator.dispatch(stop_message(experiment_id, stimuli_id)) input("\nPress a key to continue") # Terminate, This step is necessary to close the connection with added devices device_coordinator.terminate()
def _show_fixation_cross(self, *args): ''' Showing fixation cross before each stimuli ''' logging.info("Fixation cross {0}".format(datetime.datetime.now())) fixation_cross = \ ImageWindow("examples/images/fixation_cross.jpg", FIXATION_CROSS_SHOW_TIME) stimulus = self._stimuli_list[self._stimuli_index] self._device_coordinator.dispatch( start_message(self._experiment_id, stimulus.id)) fixation_cross.show_window() fixation_cross.connect("destroy", self._show_stimuli)
def show(self): ''' Shows the background window (A gray image) ''' logging.info("Start time {0}".format(datetime.datetime.now())) message = start_message(self._experiment_id, 0) # self._monitoring_device_coordinator.dispatch(message) self.connect("destroy", Gtk.main_quit) self.fullscreen() self.show_all() GLib.timeout_add_seconds(5, self._show_fixation_cross) Gtk.main()
def _show_fixation_cross(self, *args): ''' Showing fixation cross before each stimuli We start data recording here to later consider these three seconds as baseline data ''' logging.info("images/Fixation cross {0}".format( datetime.datetime.now())) fixation_cross = \ ImageWindow("images/fixation_cross.jpg", FIXATION_CROSS_SHOW_TIME) stimulus = self._stimuli_list[self._stimuli_index] self._device_coordinator.dispatch( start_message(self._experiment_id, stimulus.id)) fixation_cross.show_window() fixation_cross.connect("destroy", self._show_stimuli)
def __server(): ''' Starts a server and send several messages and terminates ''' device_coordinator = DeviceCoordinator() socket_device = SocketNetworkDevice("localhost", 5002) device_coordinator.add_devices([socket_device]) time.sleep(5) message = start_message("test", "00") device_coordinator.dispatch(message) time.sleep(2) message = stop_message("test", "00") device_coordinator.dispatch(message) time.sleep(2) message = start_message("test", "01") device_coordinator.dispatch(message) time.sleep(2) message = stop_message("test", "01") device_coordinator.dispatch(message) time.sleep(3) device_coordinator.terminate()
def test_http_network_device_happy_path(): coordinator = DeviceCoordinator() device = HttpNetworkDevice(["http://localhost:5003/"], name="test-http-network-device", timeout=15) coordinator.add_device(device) server = http.server.ThreadingHTTPServer(("localhost", 5003), Handler) threading.Thread(target=server.serve_forever, daemon=True).start() # To ensure device and server are started time.sleep(1) coordinator.dispatch(start_message("exp1", "stim1")) coordinator.dispatch(stop_message("exp1", "stim2")) # Should send TERMINATE message coordinator.terminate()
def test_system_health(mocked): output_dir = tempfile.mkdtemp(prefix="octopus-sensing-test") experiment_id = 'test-exp-2' stimuli_id = 'sti-2' params = board_shim.BrainFlowInputParams() params.serial_port = "/dev/ttyUSB0" device = \ brainflow_streaming.BrainFlowStreaming(2, 125, brain_flow_input_params=params, name="cyton_daisy", output_path=output_dir) msg_queue = queue.Queue() device.set_queue(msg_queue) monitoring_queue_in = queue.Queue() monitoring_queue_out = queue.Queue() device.set_monitoring_queues(monitoring_queue_in, monitoring_queue_out) device.start() time.sleep(0.2) msg_queue.put(start_message(experiment_id, stimuli_id)) # Allowing data collection for one second time.sleep(1) msg_queue.put(stop_message(experiment_id, stimuli_id)) time.sleep(0.2) # Sending terminate and waiting for the device process to exit. msg_queue.put(terminate_message()) device.join() # It should save the file after receiving a TERMINATE. brain_output = os.path.join(output_dir, "cyton_daisy") filename = "cyton_daisy-{}.csv".format(experiment_id) assert os.path.exists(brain_output) assert len(os.listdir(brain_output)) == 1 assert os.listdir(brain_output)[0] == filename filecontent = open(os.path.join(brain_output, filename), 'r').read() assert len(filecontent) >= 375
def test_happy_path(mocked): output_dir = tempfile.mkdtemp(prefix="octopus-sensing-test") device_name = 'test-video-device' experiment_id = 'test-exp-1' stimuli_id = 'sti-1' device = camera_streaming.CameraStreaming(camera_no=0, output_path=output_dir, name=device_name) msg_queue = queue.Queue() device.set_queue(msg_queue) device.start() time.sleep(0.2) msg_queue.put(start_message(experiment_id, stimuli_id)) time.sleep(1) msg_queue.put(stop_message(experiment_id, stimuli_id)) time.sleep(0.2) # It should save the file after receiving a STOP. device_output = os.path.join(output_dir, device_name) assert os.path.exists(device_output) recorded_file = os.path.join( device_output, '{}-{}-{}.avi'.format(device_name, experiment_id, stimuli_id)) assert os.path.exists(recorded_file) assert os.path.getsize( recorded_file) > 25600 # check the file size to assure it is not empty # Sending terminate and waiting for the device process to exit. msg_queue.put(terminate_message()) device.join()
def test_happy_path(mocked): output_dir = tempfile.mkdtemp(prefix="octopus-sensing-test") device_name = 'test-audio-device' experiment_id = 'test-exp-1' stimuli_id = 'sti-1' device = audio_streaming.AudioStreaming(1, name=device_name, output_path=output_dir) # Since there's no device coordinator running, we set the queue ourselves, # and will start the process. msg_queue = queue.Queue() device.set_queue(msg_queue) device.start() # To ensure the process is started. time.sleep(0.2) msg_queue.put(start_message(experiment_id, stimuli_id)) time.sleep(1) msg_queue.put(stop_message(experiment_id, stimuli_id)) time.sleep(0.2) # It should save the file after receiving a STOP. device_output = os.path.join(output_dir, device_name) assert os.path.exists(device_output) recorded_file = os.path.join( device_output, '{}-{}-{}.wav'.format(device_name, experiment_id, stimuli_id)) assert os.path.exists(recorded_file) # Sending terminate and waiting for the device process to exit. msg_queue.put(terminate_message()) device.join()
def test_system_health(mocked): '''Runs the whole system to roughly check everything is working together.''' # To ensure mocks are applied, we import these modules here. import octopus_sensing.devices.openbci_streaming as openbci_streaming import octopus_sensing.devices.shimmer3_streaming as shimmer3_streaming from octopus_sensing.device_coordinator import DeviceCoordinator from octopus_sensing.common.message_creators import start_message, stop_message, terminate_message from octopus_sensing.monitoring_endpoint import MonitoringEndpoint output_dir = tempfile.mkdtemp(prefix="octopus-sensing-test") coordinator = DeviceCoordinator() openbci = openbci_streaming.OpenBCIStreaming(name="eeg", output_path=output_dir) coordinator.add_device(openbci) shimmer = shimmer3_streaming.Shimmer3Streaming(name="shimmer", output_path=output_dir) coordinator.add_device(shimmer) monitoring_endpoint = MonitoringEndpoint(coordinator) monitoring_endpoint.start() try: coordinator.dispatch(start_message("int_test", "stimulus_1")) # Allowing data collection for five seconds time.sleep(5) coordinator.dispatch(stop_message("int_test", "stimulus_1")) http_client = http.client.HTTPConnection("127.0.0.1:9330") http_client.request("GET", "/") response = http_client.getresponse() assert response.status == 200 monitoring_data = pickle.loads(response.read()) assert isinstance(monitoring_data, dict) assert isinstance(monitoring_data["eeg"], list) # three seconds * data rate assert len(monitoring_data["eeg"]) == 3 * 128 assert len(monitoring_data["eeg"][0]) in (34, 35) assert len(monitoring_data["eeg"][-1]) in (34, 35) assert isinstance(monitoring_data["shimmer"], list) assert len(monitoring_data["shimmer"]) == 3 * 128 assert len(monitoring_data["shimmer"][0]) in (8, 9) assert len(monitoring_data["shimmer"][-1]) in (8, 9) finally: coordinator.dispatch(terminate_message()) monitoring_endpoint.stop() # To ensure termination is happened. time.sleep(0.5) eeg_output = os.path.join(output_dir, "eeg") assert os.path.exists(eeg_output) assert len(os.listdir(eeg_output)) == 1 assert os.listdir(eeg_output)[0] == "eeg-int_test.csv" shimmer_output = os.path.join(output_dir, "shimmer") assert os.path.exists(shimmer_output) assert len(os.listdir(shimmer_output)) == 1 assert os.listdir(shimmer_output)[0] == "shimmer-int_test.csv"