def get_metadata(self) -> BehaviorMetadata: """Return metadata about the session. :rtype: BehaviorMetadata """ metadata = BehaviorMetadata( extractor=self.extractor, stimulus_timestamps=self.get_stimulus_timestamps(), behavior_stimulus_file=self._behavior_stimulus_file()) return metadata
def test_get_behavior_session_uuid(MockBehaviorLimsApi, monkeypatch): with monkeypatch.context() as ctx: def dummy_init(self, extractor, behavior_stimulus_file): self._extractor = extractor self._behavior_stimulus_file = behavior_stimulus_file ctx.setattr(BehaviorMetadata, '__init__', dummy_init) stimulus_file = MockBehaviorLimsApi._behavior_stimulus_file() metadata = BehaviorMetadata(extractor=MockBehaviorLimsApi.extractor, behavior_stimulus_file=stimulus_file) expected = UUID('138531ab-fe59-4523-9154-07c8d97bbe03') assert expected == metadata.behavior_session_uuid
def get_metadata(self) -> dict: metadata_nwb_obj = self.nwbfile.lab_meta_data['metadata'] data = OphysBehaviorMetadataSchema( exclude=['date_of_acquisition']).dump(metadata_nwb_obj) self._behavior_session_id = data["behavior_session_id"] # Add pyNWB Subject metadata to behavior session metadata nwb_subject = self.nwbfile.subject data['mouse_id'] = int(nwb_subject.subject_id) data['sex'] = nwb_subject.sex data['age_in_days'] = BehaviorMetadata.parse_age_in_days( age=nwb_subject.age) data['full_genotype'] = nwb_subject.genotype data['reporter_line'] = nwb_subject.reporter_line data['driver_line'] = sorted(list(nwb_subject.driver_line)) data['cre_line'] = BehaviorMetadata.parse_cre_line( full_genotype=nwb_subject.genotype) # Add other metadata stored in nwb file to behavior session meta data['date_of_acquisition'] = self.nwbfile.session_start_time data['behavior_session_uuid'] = uuid.UUID( data['behavior_session_uuid']) return data
def test_cre_line(monkeypatch): """Tests that cre_line properly parsed from driver_line""" with monkeypatch.context() as ctx: def dummy_init(self): pass def full_genotype(self): return 'Sst-IRES-Cre/wt;Ai148(TIT2L-GC6f-ICL-tTA2)/wt' ctx.setattr(BehaviorMetadata, '__init__', dummy_init) ctx.setattr(BehaviorMetadata, 'full_genotype', property(full_genotype)) metadata = BehaviorMetadata() assert metadata.cre_line == 'Sst-IRES-Cre'
def test_get_date_of_acquisition(monkeypatch, tmp_path, test_params, expected_warn_msg): mock_session_id = test_params["behavior_session_id"] pkl_save_path = tmp_path / f"mock_pkl_{mock_session_id}.pkl" with open(pkl_save_path, 'wb') as handle: pickle.dump({"start_time": test_params['pkl_expt_date']}, handle) behavior_stimulus_file = pd.read_pickle(pkl_save_path) tz = pytz.timezone("America/Los_Angeles") extractor_expt_date = tz.localize( test_params['extractor_expt_date']).astimezone(pytz.utc) class MockExtractor(): def get_date_of_acquisition(self): return extractor_expt_date def get_behavior_session_id(self): return test_params['behavior_session_id'] def get_behavior_stimulus_file(self): return pkl_save_path extractor = MockExtractor() with monkeypatch.context() as ctx: def dummy_init(self, extractor, behavior_stimulus_file): self._extractor = extractor self._behavior_stimulus_file = behavior_stimulus_file ctx.setattr(BehaviorMetadata, '__init__', dummy_init) metadata = BehaviorMetadata( extractor=extractor, behavior_stimulus_file=behavior_stimulus_file) if expected_warn_msg: with pytest.warns(Warning, match=expected_warn_msg): obt_date = metadata.date_of_acquisition else: obt_date = metadata.date_of_acquisition assert obt_date == extractor_expt_date
def test_age_in_days_invalid_age(monkeypatch): """Test that age_in_days is None if age not prefixed with P""" class MockExtractor: def get_age(self): return 'Q123' extractor = MockExtractor() with monkeypatch.context() as ctx: def dummy_init(self): self._extractor = extractor ctx.setattr(BehaviorMetadata, '__init__', dummy_init) metadata = BehaviorMetadata() assert metadata.age_in_days is None
def test_reporter_line_no_reporter_line(monkeypatch): """Test that if no reporter line, returns None""" class MockExtractor: def get_reporter_line(self): return [] extractor = MockExtractor() with monkeypatch.context() as ctx: def dummy_init(self): self._extractor = extractor ctx.setattr(BehaviorMetadata, '__init__', dummy_init) metadata = BehaviorMetadata() assert metadata.reporter_line is None
def test_age_in_days(monkeypatch): """Test that age_in_days properly parsed from age""" class MockExtractor: def get_age(self): return 'P123' extractor = MockExtractor() with monkeypatch.context() as ctx: def dummy_init(self): self._extractor = extractor ctx.setattr(BehaviorMetadata, '__init__', dummy_init) metadata = BehaviorMetadata() assert metadata.age_in_days == 123
def test_age_in_days_unkown_age(monkeypatch): """Test age in days is None if age is unknown""" class MockExtractor: def get_age(self): return 'unkown' extractor = MockExtractor() with monkeypatch.context() as ctx: def dummy_init(self): self._extractor = extractor ctx.setattr(BehaviorMetadata, '__init__', dummy_init) metadata = BehaviorMetadata() assert metadata.age_in_days is None
def test_reporter_line_str(monkeypatch): """Test that reporter line returns itself if str""" class MockExtractor: def get_reporter_line(self): return 'foo' extractor = MockExtractor() with monkeypatch.context() as ctx: def dummy_init(self): self._extractor = extractor ctx.setattr(BehaviorMetadata, '__init__', dummy_init) metadata = BehaviorMetadata() assert metadata.reporter_line == 'foo'
def test_reporter_line_multiple(monkeypatch): """Test that if multiple reporter lines, the first is returned""" class MockExtractor: def get_reporter_line(self): return ['foo', 'bar'] extractor = MockExtractor() with monkeypatch.context() as ctx: def dummy_init(self): self._extractor = extractor ctx.setattr(BehaviorMetadata, '__init__', dummy_init) metadata = BehaviorMetadata() assert metadata.reporter_line == 'foo'
def test_reporter_line(monkeypatch): """Test that reporter line properly parsed from list""" class MockExtractor: def get_reporter_line(self): return ['foo'] extractor = MockExtractor() with monkeypatch.context() as ctx: def dummy_init(self): self._extractor = extractor ctx.setattr(BehaviorMetadata, '__init__', dummy_init) metadata = BehaviorMetadata() assert metadata.reporter_line == 'foo'
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'
def test_cre_line_bad_full_genotype(monkeypatch): """Test that cre_line is None and no error raised""" with monkeypatch.context() as ctx: def dummy_init(self): pass def full_genotype(self): return 'foo' ctx.setattr(BehaviorMetadata, '__init__', dummy_init) ctx.setattr(BehaviorMetadata, 'full_genotype', property(full_genotype)) metadata = BehaviorMetadata() assert metadata.cre_line is None
def test_cre_line_bad_full_genotype(monkeypatch): """Test that cre_line is None and no error raised""" with monkeypatch.context() as ctx: def dummy_init(self): pass def full_genotype(self): return 'foo' ctx.setattr(BehaviorMetadata, '__init__', dummy_init) ctx.setattr(BehaviorMetadata, 'full_genotype', property(full_genotype)) metadata = BehaviorMetadata() with pytest.warns(UserWarning) as record: cre_line = metadata.cre_line assert cre_line is None assert str(record[0].message) == 'Unable to parse cre_line from ' \ 'full_genotype'
def test_age_in_days_edge_cases(monkeypatch, input_age, warning_msg, expected): """Test age in days edge cases""" class MockExtractor: def get_age(self): return input_age 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: age_in_days = metadata.age_in_days assert age_in_days is None assert str(record[0].message) == warning_msg
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