Exemplo n.º 1
0
def test_get_behavior_stimulus_file(behavior_experiment_id, compare_val):

    if compare_val is None:
        expected_fail = False
        try:
            api = BehaviorLimsApi(behavior_experiment_id)
            api.extractor.get_behavior_stimulus_file()
        except OneResultExpectedError:
            expected_fail = True
        assert expected_fail is True
    else:
        api = BehaviorLimsApi(behavior_experiment_id)
        assert api.extractor.get_behavior_stimulus_file() == compare_val
Exemplo n.º 2
0
def test_get_ori_info_from_trial(
    behavior_experiment_id,
    ti,
    expected,
    exception,
):
    """was feeling worried that the values would be wrong,
    this helps reaffirm that maybe they are not...

    Notes
    -----
    - i may be rewriting code here but its more a sanity check really...
    """
    lims_api = BehaviorLimsApi(behavior_session_id=behavior_experiment_id)
    stim_output = pd.read_pickle(
        lims_api.extractor.get_behavior_stimulus_file())
    trial_log = stim_output['items']['behavior']['trial_log']

    if exception:
        with pytest.raises(exception):
            trials_processing.get_ori_info_from_trial(
                trial_log,
                ti,
            )
    else:
        assert trials_processing.get_ori_info_from_trial(
            trial_log,
            ti,
        ) == expected  # noqa: E501
Exemplo n.º 3
0
def write_behavior_nwb(session_data, nwb_filepath):

    nwb_filepath_inprogress = nwb_filepath+'.inprogress'
    nwb_filepath_error = nwb_filepath+'.error'

    # Clean out files from previous runs:
    for filename in [nwb_filepath_inprogress,
                     nwb_filepath_error,
                     nwb_filepath]:
        if os.path.exists(filename):
            os.remove(filename)

    try:
        json_session = BehaviorSession(api=BehaviorJsonApi(session_data))
        lims_api = BehaviorLimsApi(
            behavior_session_id=session_data['behavior_session_id'])
        lims_session = BehaviorSession(api=lims_api)

        logging.info("Comparing a BehaviorSession created from JSON "
                     "with a BehaviorSession created from LIMS")
        assert sessions_are_equal(json_session, lims_session, reraise=True)

        BehaviorNwbApi(nwb_filepath_inprogress).save(json_session)

        logging.info("Comparing a BehaviorSession created from JSON "
                     "with a BehaviorSession created from NWB")
        nwb_api = BehaviorNwbApi(nwb_filepath_inprogress)
        nwb_session = BehaviorSession(api=nwb_api)
        assert sessions_are_equal(json_session, nwb_session, reraise=True)

        os.rename(nwb_filepath_inprogress, nwb_filepath)
        return {'output_path': nwb_filepath}
    except Exception as e:
        os.rename(nwb_filepath_inprogress, nwb_filepath_error)
        raise e
Exemplo n.º 4
0
 def get_behavior_session(self,
                          behavior_session_id: int) -> BehaviorSession:
     """Returns a BehaviorSession object that contains methods to
     analyze a single behavior session.
     :param behavior_session_id: id that corresponds to a behavior session
     :type behavior_session_id: int
     :rtype: BehaviorSession
     """
     return BehaviorSession(BehaviorLimsApi(behavior_session_id))
Exemplo n.º 5
0
def test_get_extended_trials(behavior_experiment_id):
    api = BehaviorLimsApi(behavior_experiment_id)
    df = api.get_extended_trials()
    ets = ExtendedTrialSchema(partial=False, many=True)
    data_list_cs = df.to_dict('records')
    data_list_cs_sc = ets.dump(data_list_cs)
    ets.load(data_list_cs_sc)

    df_fail = df.drop(['behavior_session_uuid'], axis=1)
    ets = ExtendedTrialSchema(partial=False, many=True)
    data_list_cs = df_fail.to_dict('records')
    data_list_cs_sc = ets.dump(data_list_cs)
    try:
        ets.load(data_list_cs_sc)
        raise RuntimeError("This should have failed with "
                           "marshmallow.schema.ValidationError")
    except ValidationError:
        pass
Exemplo n.º 6
0
    def get_session(self, behavior_session_uuid=None, behavior_session_id=None):
        assert not all(v is None for v in [
                       behavior_session_uuid, behavior_session_id]), 'must enter either a behavior_session_uuid or a behavior_session_id'

        if behavior_session_uuid is not None and behavior_session_id is not None:
            # if both a behavior session uuid and a lims id are entered, ensure that they match
            behavior_api = BehaviorLimsApi(behavior_session_id)
            assert behavior_session_uuid == behavior_api.get_behavior_session_uuid(), 'behavior_session {} does not match behavior_session_id {}'.format(behavior_session_uuid, behavior_session_id)
        if behavior_session_uuid is None and behavior_session_id is not None:
            # get a behavior session uuid if a lims ID was entered
            behavior_api = BehaviorLimsApi(behavior_session_id)
            behavior_session_uuid = behavior_api.get_behavior_session_uuid()

        filters = [{"name": "id", "op": "eq", "val": behavior_session_uuid}]
        behavior_df = self.get_df('behavior_sessions', filters=filters).rename(columns={'id': 'behavior_session_uuid'})
        state_df = self.get_df('states').rename(columns={'id': 'state_id'})
        regimen_df = self.get_df('regimens').rename(columns={'id': 'regimen_id', 'name': 'regimen_name'}).drop(['states', 'active'], axis=1)
        stage_df = self.get_df('stages').rename(columns={'id': 'stage_id'}).drop(['states'], axis=1)

        behavior_df = pd.merge(behavior_df, state_df, how='left', on='state_id')
        behavior_df = pd.merge(behavior_df, stage_df, how='left', on='stage_id')
        behavior_df = pd.merge(behavior_df, regimen_df, how='left', on='regimen_id')
        behavior_df.drop(['state_id', 'stage_id', 'regimen_id'], inplace=True, axis=1)
        if len(behavior_df) == 0:
            raise RuntimeError("Session not found %s:" % behavior_session_uuid)
        assert len(behavior_df) == 1
        session_dict = behavior_df.iloc[0].to_dict()

        filters = [{"name": "behavior_session_uuid", "op": "eq", "val": behavior_session_uuid}]
        trials_df = self.get_df('trials', filters=filters).sort_values('index').drop(['id', 'behavior_session'], axis=1).set_index('index', drop=False)
        trials_df['behavior_session_uuid'] = trials_df['behavior_session_uuid'].map(uuid.UUID)
        del trials_df.index.name
        session_dict['trials'] = trials_df[EDF_COLUMNS]

        return session_dict
Exemplo n.º 7
0
 def setup_class(cls):
     cls.bd = BehaviorLimsApi(976012750)
     cls.od = BehaviorOphysLimsApi(976255949)
Exemplo n.º 8
0
 def from_lims(cls, behavior_session_id: int) -> "BehaviorSession":
     return cls(api=BehaviorLimsApi(behavior_session_id))
Exemplo n.º 9
0
def test_behavior_session_id_to_foraging_id(behavior_session_id):
    session = BehaviorLimsApi(behavior_session_id=behavior_session_id)
    behavior_session_uuid = session.get_metadata().behavior_session_uuid
    expected = UUID('394a910e-94c7-4472-9838-5345aff59ed8')
    assert behavior_session_uuid == expected
Exemplo n.º 10
0
def test_behavior_session_id_to_foraging_id(behavior_session_id):
    session = BehaviorLimsApi(behavior_session_id=behavior_session_id)
    foraging_id = session.get_behavior_session_uuid()
    assert foraging_id == '394a910e-94c7-4472-9838-5345aff59ed8'
Exemplo n.º 11
0
def test_foraging_id_to_behavior_session_id(behavior_session_uuid):
    session = BehaviorLimsApi.from_foraging_id(behavior_session_uuid)
    assert session.behavior_session_id == 823847007