|
| 1 | +from typing import Any, Optional, List, Dict, Type, Tuple |
| 2 | +import logging |
| 3 | +import pandas as pd |
| 4 | +import numpy as np |
| 5 | +import inspect |
| 6 | + |
| 7 | +from allensdk.internal.api.behavior_data_lims_api import BehaviorDataLimsApi |
| 8 | +from allensdk.brain_observatory.behavior.internal import BehaviorBase |
| 9 | +from allensdk.brain_observatory.running_speed import RunningSpeed |
| 10 | + |
| 11 | +BehaviorDataApi = Type[BehaviorBase] |
| 12 | + |
| 13 | + |
| 14 | +class BehaviorDataSession(object): |
| 15 | + def __init__(self, api: Optional[BehaviorDataApi] = None): |
| 16 | + self.api = api |
| 17 | + |
| 18 | + @classmethod |
| 19 | + def from_lims(cls, behavior_session_id: int) -> "BehaviorDataSession": |
| 20 | + return cls(api=BehaviorDataLimsApi(behavior_session_id)) |
| 21 | + |
| 22 | + @classmethod |
| 23 | + def from_nwb_path( |
| 24 | + cls, nwb_path: str, **api_kwargs: Any) -> "BehaviorDataSession": |
| 25 | + return NotImplementedError |
| 26 | + |
| 27 | + @property |
| 28 | + def behavior_session_id(self) -> int: |
| 29 | + """Unique identifier for this experimental session. |
| 30 | + :rtype: int |
| 31 | + """ |
| 32 | + return self.api.behavior_session_id |
| 33 | + |
| 34 | + @property |
| 35 | + def ophys_session_id(self) -> Optional[int]: |
| 36 | + """The unique identifier for the ophys session associated |
| 37 | + with this behavior session (if one exists) |
| 38 | + :rtype: int |
| 39 | + """ |
| 40 | + return self.api.ophys_session_id |
| 41 | + |
| 42 | + @property |
| 43 | + def ophys_experiment_ids(self) -> Optional[List[int]]: |
| 44 | + """The unique identifiers for the ophys experiment(s) associated |
| 45 | + with this behavior session (if one exists) |
| 46 | + :rtype: int |
| 47 | + """ |
| 48 | + return self.api.ophys_experiment_ids |
| 49 | + |
| 50 | + @property |
| 51 | + def licks(self) -> pd.DataFrame: |
| 52 | + """Get lick data from pkl file. |
| 53 | +
|
| 54 | + Returns |
| 55 | + ------- |
| 56 | + np.ndarray |
| 57 | + A dataframe containing lick timestamps. |
| 58 | + """ |
| 59 | + return self.api.get_licks() |
| 60 | + |
| 61 | + @property |
| 62 | + def rewards(self) -> pd.DataFrame: |
| 63 | + """Get reward data from pkl file. |
| 64 | +
|
| 65 | + Returns |
| 66 | + ------- |
| 67 | + pd.DataFrame |
| 68 | + A dataframe containing timestamps of delivered rewards. |
| 69 | + """ |
| 70 | + return self.api.get_rewards() |
| 71 | + |
| 72 | + @property |
| 73 | + def running_data_df(self) -> pd.DataFrame: |
| 74 | + """Get running speed data. |
| 75 | +
|
| 76 | + Returns |
| 77 | + ------- |
| 78 | + pd.DataFrame |
| 79 | + Dataframe containing various signals used to compute running speed. |
| 80 | + """ |
| 81 | + return self.api.get_running_data_df() |
| 82 | + |
| 83 | + @property |
| 84 | + def running_speed(self) -> RunningSpeed: |
| 85 | + """Get running speed using timestamps from |
| 86 | + self.get_stimulus_timestamps. |
| 87 | +
|
| 88 | + NOTE: Do not correct for monitor delay. |
| 89 | +
|
| 90 | + Returns |
| 91 | + ------- |
| 92 | + RunningSpeed (NamedTuple with two fields) |
| 93 | + timestamps : np.ndarray |
| 94 | + Timestamps of running speed data samples |
| 95 | + values : np.ndarray |
| 96 | + Running speed of the experimental subject (in cm / s). |
| 97 | + """ |
| 98 | + return self.api.get_running_speed() |
| 99 | + |
| 100 | + @property |
| 101 | + def stimulus_presentations(self) -> pd.DataFrame: |
| 102 | + """Get stimulus presentation data. |
| 103 | +
|
| 104 | + NOTE: Uses timestamps that do not account for monitor delay. |
| 105 | +
|
| 106 | + Returns |
| 107 | + ------- |
| 108 | + pd.DataFrame |
| 109 | + Table whose rows are stimulus presentations |
| 110 | + (i.e. a given image, for a given duration, typically 250 ms) |
| 111 | + and whose columns are presentation characteristics. |
| 112 | + """ |
| 113 | + return self.api.get_stimulus_presentations() |
| 114 | + |
| 115 | + @property |
| 116 | + def stimulus_templates(self) -> Dict[str, np.ndarray]: |
| 117 | + """Get stimulus templates (movies, scenes) for behavior session. |
| 118 | +
|
| 119 | + Returns |
| 120 | + ------- |
| 121 | + Dict[str, np.ndarray] |
| 122 | + A dictionary containing the stimulus images presented during the |
| 123 | + session. Keys are data set names, and values are 3D numpy arrays. |
| 124 | + """ |
| 125 | + return self.api.get_stimulus_templates() |
| 126 | + |
| 127 | + @property |
| 128 | + def stimulus_timestamps(self) -> np.ndarray: |
| 129 | + """Get stimulus timestamps from pkl file. |
| 130 | +
|
| 131 | + NOTE: Located with behavior_session_id |
| 132 | +
|
| 133 | + Returns |
| 134 | + ------- |
| 135 | + np.ndarray |
| 136 | + Timestamps associated with stimulus presentations on the monitor |
| 137 | + that do no account for monitor delay. |
| 138 | + """ |
| 139 | + return self.api.get_stimulus_timestamps() |
| 140 | + |
| 141 | + @property |
| 142 | + def task_parameters(self) -> dict: |
| 143 | + """Get task parameters from pkl file. |
| 144 | +
|
| 145 | + Returns |
| 146 | + ------- |
| 147 | + dict |
| 148 | + A dictionary containing parameters used to define the task runtime |
| 149 | + behavior. |
| 150 | + """ |
| 151 | + return self.api.get_task_parameters() |
| 152 | + |
| 153 | + @property |
| 154 | + def trials(self) -> pd.DataFrame: |
| 155 | + """Get trials from pkl file |
| 156 | +
|
| 157 | + Returns |
| 158 | + ------- |
| 159 | + pd.DataFrame |
| 160 | + A dataframe containing behavioral trial start/stop times, |
| 161 | + and trial data |
| 162 | + """ |
| 163 | + return self.api.get_trials() |
| 164 | + |
| 165 | + @property |
| 166 | + def metadata(self) -> Dict[str, Any]: |
| 167 | + """Return metadata about the session. |
| 168 | + :rtype: dict |
| 169 | + """ |
| 170 | + return self.api.get_metadata() |
| 171 | + |
| 172 | + def cache_clear(self) -> None: |
| 173 | + """Convenience method to clear the api cache, if applicable.""" |
| 174 | + try: |
| 175 | + self.api.cache_clear() |
| 176 | + except AttributeError: |
| 177 | + logging.getLogger("BehaviorOphysSession").warning( |
| 178 | + "Attempted to clear API cache, but method `cache_clear`" |
| 179 | + f" does not exist on {self.api.__class__.__name__}") |
| 180 | + |
| 181 | + def list_api_methods(self) -> List[Tuple[str, str]]: |
| 182 | + """Convenience method to expose list of API `get` methods. These methods |
| 183 | + can be accessed by referencing the API used to initialize this |
| 184 | + BehaviorDataSession via its `api` instance attribute. |
| 185 | + :rtype: list of tuples, where the first value in the tuple is the |
| 186 | + method name, and the second value is the method docstring. |
| 187 | + """ |
| 188 | + methods = [m for m in inspect.getmembers(self.api, inspect.ismethod) |
| 189 | + if m[0].startswith("get_")] |
| 190 | + docs = [inspect.getdoc(m[1]) or "" for m in methods] |
| 191 | + method_names = [m[0] for m in methods] |
| 192 | + return list(zip(method_names, docs)) |
0 commit comments