peachyontop

Neural Pipeline

Mar 20th, 2026
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.22 KB | None | 0 0
  1. import sys
  2. import socket
  3. import struct
  4. import numpy as np
  5. import pyqtgraph.opengl as gl
  6. from pyqtgraph.Qt import QtCore, QtWidgets
  7.  
  8. class NeuralTelemetryFeed(QtWidgets.QMainWindow):
  9.     def __init__(self, channels=5120):
  10.         super().__init__()
  11.         self.channels = channels
  12.        
  13.         self.view = gl.GLViewWidget()
  14.         self.setCentralWidget(self.view)
  15.         self.view.setCameraPosition(distance=45, elevation=20, azimuth=45)
  16.         self.setWindowTitle('1U Dock: Real-Time Telemetry (Awaiting Signal...)')
  17.  
  18.         self.pos = self._load_electrode_geometry()
  19.         self.colors = np.array([[0.1, 0.1, 0.3, 0.5]] * self.channels) # Default: Idle Blue
  20.         self.scatter = gl.GLScatterPlotItem(pos=self.pos, color=self.colors, size=1.5, pxMode=True)
  21.         self.view.addItem(self.scatter)
  22.  
  23.         self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  24.         self.socket.setblocking(False) # Non-blocking for UI fluidity
  25.        
  26.         self.timer = QtCore.QTimer()
  27.         self.timer.timeout.connect(self.poll_hardware_stream)
  28.         self.timer.start(10) # 100Hz Polling
  29.  
  30.     def _load_electrode_geometry(self):
  31.  
  32.         t = np.linspace(0, 2*np.pi, self.channels)
  33.         cos_t, sin_t = np.cos(t), np.sin(t)
  34.         return np.vstack([15*cos_t, 15*sin_t, 10*np.sin(t*5)]).T
  35.  
  36.     def poll_hardware_stream(self):
  37.         try:
  38.             # Format: 5120 floats (4 bytes each) representing variation/activity
  39.             data = self.socket.recv(self.channels * 4)
  40.             if data:
  41.                 activity = np.frombuffer(data, dtype=np.float32)
  42.                 self.update_visualization(activity)
  43.         except (BlockingIOError, ConnectionResetError):
  44.             pass # No data yet, keep the display static
  45.  
  46.     def update_visualization(self, activity):
  47.         norm_activity = np.clip(activity, 0, 1)
  48.         self.colors[:, 0] = norm_activity          # Red
  49.         self.colors[:, 1] = norm_activity * 0.5    # Green (Warmth)
  50.         self.colors[:, 2] = 1 - norm_activity      # Blue
  51.         self.scatter.setData(color=self.colors)
  52.  
  53. if __name__ == '__main__':
  54.     app = QtWidgets.QApplication(sys.argv)
  55.     feed = NeuralTelemetryFeed()
  56.     feed.show()
  57.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment