Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/ethopy/core/behavior.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
interfaces with hardware components and maintains experiment state.
"""

import logging
from dataclasses import dataclass, fields
from dataclasses import field as datafield
from datetime import datetime, timedelta
Expand All @@ -14,6 +15,8 @@
import datajoint as dj
import numpy as np

log = logging.getLogger(__name__)

from ethopy.core.experiment import ExperimentClass
from ethopy.core.logger import ( # pylint: disable=W0611, # noqa: F401
behavior,
Expand Down Expand Up @@ -199,6 +202,11 @@ def log_activity(self, activity_key: dict) -> int:

"""
activity = BehActivity(**activity_key)
# GPIO callbacks may fire before setup() wires the logger; drop them.
if self.logger is None:
log.warning("log_activity called before logger is set; dropping %s",
activity_key)
return activity.time or 0
# if activity.time is not set, set it to the current time
if not activity.time:
activity.time = self.logger.logger_timer.elapsed_time()
Expand Down
6 changes: 6 additions & 0 deletions tests/test_behavior.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ def test_log_activity_lick(self, behavior):
assert behavior.last_lick.time == 1500
assert behavior.licked_port == 2

def test_log_activity_before_logger_setup(self, behavior):
"""Callbacks firing before setup() must not raise AttributeError."""
behavior.logger = None
result = behavior.log_activity({"type": "Lick", "port": 1, "time": 500})
assert result == 500

def test_is_licking_no_lick(self, behavior):
"""Test is_licking when no lick has occurred."""
from ethopy.core.behavior import BehActivity
Expand Down