Skip to content

Commit e991b72

Browse files
authored
Merge pull request #1734 from AllenInstitute/GH-1491/behavior-session-id-in-BehaviorOphysSession
GH #1491: Add behavior_session_id to BehaviorOphysSession
2 parents 8f231f3 + 106c283 commit e991b72

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

allensdk/brain_observatory/behavior/behavior_ophys_session.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22
import pandas as pd
33
import xarray as xr
4-
from typing import Any
4+
from typing import Any, Optional
55
import logging
66

77

@@ -98,6 +98,13 @@ def ophys_experiment_id(self) -> int:
9898
"""
9999
return self.api.get_ophys_experiment_id()
100100

101+
@property
102+
def behavior_session_id(self) -> Optional[int]:
103+
"""Returns the unique identifier for the behavior session
104+
associated with this experiment, if applicable.
105+
"""
106+
return self.api.get_behavior_session_id()
107+
101108
@property
102109
def max_projection(self) -> Image:
103110
"""2D max projection image.

allensdk/brain_observatory/behavior/internal/behavior_base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ class BehaviorBase(abc.ABC):
1414
Child classes should be instantiated with a fetch API that implements these
1515
methods.
1616
"""
17+
@abc.abstractmethod
18+
def get_behavior_session_id(self) -> int:
19+
"""Returns the behavior_session_id associated with this experiment,
20+
if applicable.
21+
"""
22+
raise NotImplementedError()
23+
1724
@abc.abstractmethod
1825
def get_licks(self) -> pd.DataFrame:
1926
"""Get lick data from pkl file.

allensdk/brain_observatory/behavior/internal/behavior_ophys_base.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import abc
2+
from typing import Optional
23

34
import numpy as np
45
import pandas as pd
@@ -12,8 +13,16 @@ class BehaviorOphysBase(BehaviorBase):
1213
behavior+ophys session data.
1314
1415
Child classes should be instantiated with a fetch API that implements these
15-
methods. Both fetch API and session object should inherit from this base.
16+
methods.
1617
"""
18+
19+
@abc.abstractmethod
20+
def get_ophys_session_id(self) -> Optional[int]:
21+
"""Returns the ophys_session_id associated with this experiment,
22+
if applicable.
23+
"""
24+
raise NotImplementedError()
25+
1726
@abc.abstractmethod
1827
def get_average_projection(self) -> Image:
1928
"""Get an image whose values are the average obtained values at

allensdk/internal/api/ophys_lims_api.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,26 @@ def get_imaging_plane_group(self) -> Optional[int]:
8383
else:
8484
return None
8585

86+
@memoize
87+
def get_behavior_session_id(self) -> Optional[int]:
88+
"""Returns the behavior_session_id associated with this experiment,
89+
if applicable.
90+
"""
91+
query = f"""
92+
SELECT bs.id
93+
FROM ophys_experiments oe
94+
-- every ophys_experiment should have an ophys_session
95+
JOIN ophys_sessions os ON oe.ophys_session_id = os.id
96+
-- but not every ophys_session has a behavior_session
97+
LEFT JOIN behavior_sessions bs ON os.id = bs.ophys_session_id
98+
WHERE oe.id = {self.get_ophys_experiment_id()}
99+
"""
100+
response = self.lims_db.fetchall(query) # Can be null
101+
if not len(response):
102+
return None
103+
else:
104+
return response[0]
105+
86106
@memoize
87107
def get_ophys_experiment_dir(self):
88108
query = '''

0 commit comments

Comments
 (0)