Skip to content

Commit

Permalink
Update hardware_validation.py
Browse files Browse the repository at this point in the history
  • Loading branch information
bimac committed Aug 20, 2024
1 parent 684d576 commit 6efe34f
Showing 1 changed file with 66 additions and 4 deletions.
70 changes: 66 additions & 4 deletions iblrig/hardware_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import sounddevice
import usb
from dateutil.relativedelta import relativedelta
from PyQt5.QtGui import QColorConstants
from PyQt5.QtWidgets import QApplication
from serial import Serial, SerialException
from serial.serialutil import SerialTimeoutException
from serial.tools import list_ports
Expand Down Expand Up @@ -97,7 +99,10 @@ def _get_bpod(self) -> Generator[Result, None, Bpod | None]:
yield Result(Status.INFO, f'Cannot complete validation of {self.name} without Bpod')
return None
try:
return Bpod(self.hardware_settings.device_bpod.COM_BPOD, skip_initialization=True)
disabled_ports = [x - 1 for x in self.hardware_settings['device_bpod']['DISABLE_BEHAVIOR_INPUT_PORTS']]
return Bpod(
self.hardware_settings.device_bpod.COM_BPOD, skip_initialization=True, disable_behavior_ports=disabled_ports
)
except Exception as e:
yield Result(Status.FAIL, f'Cannot complete validation of {self.name}: connection to Bpod failed', exception=e)
return None
Expand Down Expand Up @@ -385,7 +390,7 @@ def _run(self):
rate = np.mean(1 / np.diff(timestamps))
port = re.sub('(In)|(High)', '', event_name)
port = re.sub('Port', 'Behavior Port ', port)
port = re.sub('BNC', 'BNC In ', port)
port = re.sub('BNC', 'BNC Input ', port)
if event_name in ['Port1In']:
yield Result(Status.INFO, f"Expected input events on Bpod's '{port}' at ~{rate:0.0f} Hz")
else:
Expand Down Expand Up @@ -576,6 +581,63 @@ def _run(self):
if bpod is None:
return False

# prepare test of TTL output
from iblrig.gui.frame2ttl import Frame2TTLCalibrationTarget

app = QApplication.instance()
if app_created := app is None:
app = QApplication([])
calibration_target = Frame2TTLCalibrationTarget(color=QColorConstants.Black)
calibration_target.show()

# Define state-machine
def softcode_handler(softcode: int):
nonlocal calibration_target
calibration_target.color = QColorConstants.White if softcode == 1 else QColorConstants.Black

original_softcode_handler = bpod.softcode_handler_function
bpod.softcode_handler_function = softcode_handler
sma = StateMachine(bpod)
sma.add_state(
state_name='white',
state_timer=1,
state_change_conditions={'Tup': 'black', 'BNC1High': 'black'},
output_actions=[('SoftCode', 1)],
)
sma.add_state(
state_name='black',
state_timer=1,
state_change_conditions={'Tup': 'exit', 'BNC1Low': 'exit'},
output_actions=[('SoftCode', 2)],
)

# Run state-machine
try:
bpod.send_state_machine(sma)
bpod.run_state_machine(sma)
bpod_data = bpod.session.current_trial.export()
events: dict[str, list[float]] = bpod_data.get('Events timestamps', {})
except Exception as e:
yield Result(Status.FAIL, 'Error running state-machine', exception=e)
return False
finally:
bpod.softcode_handler_function = original_softcode_handler
calibration_target.close()
if app_created:
app.quit()

# Evaluate results
if (n_events := len(events.get('BNC1High', [])) + len(events.get('BNC1Low', []))) == 2:
yield Result(Status.PASS, "Detected the correct number of events on Bpod's 'TTL Input 1'")
return True
else:
yield Result(
Status.FAIL,
('No' if n_events == 0 else 'too few' if n_events < 2 else 'too many') + " events detected on Bpod's 'BNC Input 1'",

Check failure on line 636 in iblrig/hardware_validation.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

iblrig/hardware_validation.py:636:131: E501 Line too long (132 > 130)
solution='Check for proper installation and calibration of Frame2TTL module',
)
return False


class ValidatorGit(Validator):
_name = 'Git'
Expand Down Expand Up @@ -643,7 +705,7 @@ class ValidatorSound(ValidatorSerial):
_module_name: str | None = None

def __init__(self, *args, **kwargs):
output_type = kwargs['hardware_settings'].device_sound.OUTPUT
output_type = kwargs.get('hardware_settings', load_pydantic_yaml(HardwareSettings)).device_sound.OUTPUT
match output_type:
case 'harp':
self._name = 'HARP Sound Card'
Expand Down Expand Up @@ -727,7 +789,7 @@ def _run(self):
solution="Make sure to connect the sound-card to Bpod's TTL Input 2",
)
elif n_events == 1:
yield Result(Status.PASS, "Detected Event on Bpod's TTL Input 2")
yield Result(Status.PASS, "Detected Event on Bpod's 'TTL Input 2'")
else:
yield Result(
Status.FAIL,
Expand Down

0 comments on commit 6efe34f

Please sign in to comment.