Skip to content

Commit

Permalink
Merge pull request #2179 from AllenInstitute/rc/2.11.3
Browse files Browse the repository at this point in the history
rc/2.11.3
  • Loading branch information
djkapner authored Aug 4, 2021
2 parents b29058f + f898b16 commit 6cf59d5
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 93 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Change Log
All notable changes to this project will be documented in this file.

## [2.11.3] = 2021-08-04
- Bugfixes related to NWB creation for BehaviorSessions

## [2.11.2] = 2021-05-21
- Fixed mkdir error for non-existing ecephys upload directory
Expand Down
2 changes: 1 addition & 1 deletion allensdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#
import logging

__version__ = '2.11.2'
__version__ = '2.11.3'


try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,6 @@ def reporter_line(self) -> Optional[str]:
reporter_line = self._extractor.get_reporter_line()
return self.parse_reporter_line(reporter_line=reporter_line, warn=True)

@property
def indicator(self) -> Optional[str]:
"""Parses indicator from reporter"""
reporter_line = self.reporter_line
return self.parse_indicator(reporter_line=reporter_line, warn=True)

@property
def cre_line(self) -> Optional[str]:
"""Parses cre_line from full_genotype"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def __init__(self, extractor: BehaviorOphysDataExtractorBase,
# since it's only exposed internally
self._exclude_from_equals = {'project_code'}

@property
def indicator(self) -> Optional[str]:
"""Parses indicator from reporter"""
reporter_line = self.reporter_line
return self.parse_indicator(reporter_line=reporter_line, warn=True)

@property
def emission_lambda(self) -> float:
return 520.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import abc
import pandas as pd
from datetime import datetime
from typing import List

from allensdk.api.warehouse_cache.cache import memoize


class BehaviorDataExtractorBase(abc.ABC):
"""Abstract base class implementing required methods for extracting
Expand Down Expand Up @@ -34,11 +37,25 @@ def get_age(self) -> str:
"""Get the age code of the subject (ie P123)"""
raise NotImplementedError()

@abc.abstractmethod
@memoize
def get_stimulus_name(self) -> str:
"""Get the name of the stimulus presented for a behavior or
behavior + ophys experiment"""
raise NotImplementedError()
"""Get the stimulus set used from the behavior session pkl file
:rtype: str
"""
behavior_stimulus_path = self.get_behavior_stimulus_file()
pkl = pd.read_pickle(behavior_stimulus_path)

try:
stimulus_name = pkl["items"]["behavior"]["cl_params"]["stage"]
except KeyError:
raise RuntimeError(
f"Could not obtain stimulus_name/stage information from "
f"the *.pkl file ({behavior_stimulus_path}) "
f"for the behavior session to save as NWB! The "
f"following series of nested keys did not work: "
f"['items']['behavior']['cl_params']['stage']"
)
return stimulus_name

@abc.abstractmethod
def get_reporter_line(self) -> List[str]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ def get_age(self) -> str:
"""Get the age code of the subject (ie P123)"""
return self.data['age']

def get_stimulus_name(self) -> str:
"""Get the name of the stimulus presented for the experiment"""
return self.data['stimulus_name']

def get_reporter_line(self) -> str:
"""Get the (gene) reporter line for the subject associated with an
experiment"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from typing import Dict, List, Optional, Union
import pytz

import pandas as pd

from allensdk.api.warehouse_cache.cache import memoize
from allensdk.brain_observatory.behavior.session_apis.abcs.\
data_extractor_base.behavior_data_extractor_base import \
Expand Down Expand Up @@ -254,26 +252,6 @@ def get_equipment_name(self) -> str:
"""
return self.lims_db.fetchone(query, strict=True)

@memoize
def get_stimulus_name(self) -> str:
"""Get the stimulus set used from the behavior session pkl file
:rtype: str
"""
behavior_stimulus_path = self.get_behavior_stimulus_file()
pkl = pd.read_pickle(behavior_stimulus_path)

try:
stimulus_name = pkl["items"]["behavior"]["cl_params"]["stage"]
except KeyError:
raise RuntimeError(
f"Could not obtain stimulus_name/stage information from "
f"the *.pkl file ({behavior_stimulus_path}) "
f"for the behavior session to save as NWB! The "
f"following series of nested keys did not work: "
f"['items']['behavior']['cl_params']['stage']"
)
return stimulus_name

@memoize
def get_reporter_line(self) -> List[str]:
"""Returns the genotype name(s) of the reporter line(s).
Expand Down
1 change: 1 addition & 0 deletions allensdk/brain_observatory/session_api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from allensdk.core.lazy_property import LazyProperty

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


def is_equal(a: Any, b: Any) -> bool:
Expand Down
58 changes: 2 additions & 56 deletions allensdk/test/brain_observatory/behavior/test_behavior_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def test_get_task_parameters_task_id_exception():
}

with pytest.raises(RuntimeError) as error:
_ = get_task_parameters(input_data)
get_task_parameters(input_data)
assert "does not know how to parse 'task_id'" in error.value.args[0]


Expand Down Expand Up @@ -285,7 +285,7 @@ def test_get_task_parameters_flash_duration_exception():
}

with pytest.raises(RuntimeError) as error:
_ = get_task_parameters(input_data)
get_task_parameters(input_data)
shld_be = "'images' and/or 'grating' not a valid key"
assert shld_be in error.value.args[0]

Expand Down Expand Up @@ -585,57 +585,3 @@ def dummy_init(self, extractor, behavior_stimulus_file):
obt_date = metadata.date_of_acquisition

assert obt_date == extractor_expt_date


def test_indicator(monkeypatch):
"""Test that indicator is parsed from full_genotype"""

class MockExtractor:
def get_reporter_line(self):
return 'Ai148(TIT2L-GC6f-ICL-tTA2)'

extractor = MockExtractor()

with monkeypatch.context() as ctx:
def dummy_init(self):
self._extractor = extractor

ctx.setattr(BehaviorMetadata,
'__init__',
dummy_init)

metadata = BehaviorMetadata()

assert metadata.indicator == 'GCaMP6f'


@pytest.mark.parametrize("input_reporter_line, warning_msg, expected", (
(None, 'Error parsing reporter line. It is null.', None),
('foo', 'Could not parse indicator from reporter because none'
'of the expected substrings were found in the reporter', None)
)
)
def test_indicator_edge_cases(monkeypatch, input_reporter_line, warning_msg,
expected):
"""Test indicator parsing edge cases"""

class MockExtractor:
def get_reporter_line(self):
return input_reporter_line

extractor = MockExtractor()

with monkeypatch.context() as ctx:
def dummy_init(self):
self._extractor = extractor

ctx.setattr(BehaviorMetadata,
'__init__',
dummy_init)

metadata = BehaviorMetadata()

with pytest.warns(UserWarning) as record:
indicator = metadata.indicator
assert indicator is expected
assert str(record[0].message) == warning_msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pytest

from allensdk.brain_observatory.behavior.metadata.behavior_ophys_metadata \
import BehaviorOphysMetadata


def test_indicator(monkeypatch):
"""Test that indicator is parsed from full_genotype"""

class MockExtractor:
def get_reporter_line(self):
return 'Ai148(TIT2L-GC6f-ICL-tTA2)'

extractor = MockExtractor()

with monkeypatch.context() as ctx:
def dummy_init(self):
self._extractor = extractor

ctx.setattr(BehaviorOphysMetadata,
'__init__',
dummy_init)

metadata = BehaviorOphysMetadata()

assert metadata.indicator == 'GCaMP6f'


@pytest.mark.parametrize("input_reporter_line, warning_msg, expected", (
(None, 'Error parsing reporter line. It is null.', None),
('foo', 'Could not parse indicator from reporter because none'
'of the expected substrings were found in the reporter', None)
)
)
def test_indicator_edge_cases(monkeypatch, input_reporter_line, warning_msg,
expected):
"""Test indicator parsing edge cases"""

class MockExtractor:
def get_reporter_line(self):
return input_reporter_line

extractor = MockExtractor()

with monkeypatch.context() as ctx:
def dummy_init(self):
self._extractor = extractor

ctx.setattr(BehaviorOphysMetadata,
'__init__',
dummy_init)

metadata = BehaviorOphysMetadata()

with pytest.warns(UserWarning) as record:
indicator = metadata.indicator
assert indicator is expected
assert str(record[0].message) == warning_msg
5 changes: 5 additions & 0 deletions doc_template/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ The Allen SDK provides Python code for accessing experimental metadata along wit
See the `mouse connectivity section <connectivity.html>`_ for more details.


What's New - 2.11.3
-----------------------------------------------------------------------
- Bugfixes related to NWB creation for BehaviorSessions


What's New - 2.11.2
-----------------------------------------------------------------------
- Fixed mkdir error for non-existing ecephys upload directory
Expand Down

0 comments on commit 6cf59d5

Please sign in to comment.