Esempio n. 1
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
def test_add_stimulus_presentations(nwbfile, stimulus_presentations_behavior,
                                    stimulus_timestamps, roundtrip,
                                    roundtripper,
                                    stimulus_templates: StimulusTemplate):
    nwb.add_stimulus_timestamps(nwbfile, stimulus_timestamps)
    nwb.add_stimulus_presentations(nwbfile, stimulus_presentations_behavior)
    nwb.add_stimulus_template(nwbfile=nwbfile,
                              stimulus_template=stimulus_templates)

    # Add index for this template to NWB in-memory object:
    nwb_template = nwbfile.stimulus_template[stimulus_templates.image_set_name]
    compare = (stimulus_presentations_behavior['image_set'] ==
               nwb_template.name)
    curr_stimulus_index = stimulus_presentations_behavior[compare]
    nwb.add_stimulus_index(nwbfile, curr_stimulus_index, nwb_template)

    if roundtrip:
        obt = roundtripper(nwbfile, BehaviorNwbApi)
    else:
        obt = BehaviorNwbApi.from_nwbfile(nwbfile)

    expected = stimulus_presentations_behavior.copy()
    expected['is_change'] = [False, True, True, True, True]

    obtained = obt.get_stimulus_presentations()

    pd.testing.assert_frame_equal(expected[sorted(expected.columns)],
                                  obtained[sorted(obtained.columns)],
                                  check_dtype=False)
def test_add_task_parameters_stim_nan(nwbfile, roundtrip,
                                      roundtripper,
                                      task_parameters_nan_stimulus_duration):
    """
    Same as test_add_task_parameters, but stimulus_duration_sec is NaN
    """
    task_params = task_parameters_nan_stimulus_duration
    nwb.add_task_parameters(nwbfile, task_params)

    if roundtrip:
        obt = roundtripper(nwbfile, BehaviorNwbApi)
    else:
        obt = BehaviorNwbApi.from_nwbfile(nwbfile)

    task_parameters_obt = obt.get_task_parameters()

    assert len(task_parameters_obt) == len(task_params)
    for key, val in task_params.items():
        if key in ('omitted_flash_fraction',
                   'stimulus_duration_sec'):
            if math.isnan(val):
                assert math.isnan(task_parameters_obt[key])
            if math.isnan(task_parameters_obt[key]):
                assert math.isnan(val)
        else:
            assert val == task_parameters_obt[key]
def test_add_licks(nwbfile, roundtrip, roundtripper, licks):

    nwb.add_licks(nwbfile, licks)

    if roundtrip:
        obt = roundtripper(nwbfile, BehaviorNwbApi)
    else:
        obt = BehaviorNwbApi.from_nwbfile(nwbfile)

    pd.testing.assert_frame_equal(licks, obt.get_licks(), check_dtype=False)
def test_add_stimulus_timestamps(nwbfile, stimulus_timestamps,
                                 roundtrip, roundtripper):

    nwb.add_stimulus_timestamps(nwbfile, stimulus_timestamps)

    if roundtrip:
        obt = roundtripper(nwbfile, BehaviorNwbApi)
    else:
        obt = BehaviorNwbApi.from_nwbfile(nwbfile)

    np.testing.assert_array_almost_equal(stimulus_timestamps,
                                         obt.get_stimulus_timestamps())
def test_add_stimulus_templates(nwbfile, behavior_stimuli_data_fixture,
                                roundtrip, roundtripper):
    stimulus_templates = get_stimulus_templates(behavior_stimuli_data_fixture,
                                                grating_images_dict={})

    nwb.add_stimulus_template(nwbfile, stimulus_templates)

    if roundtrip:
        obt = roundtripper(nwbfile, BehaviorNwbApi)
    else:
        obt = BehaviorNwbApi.from_nwbfile(nwbfile)

    stimulus_templates_obt = obt.get_stimulus_templates()

    assert stimulus_templates_obt == stimulus_templates
def test_add_running_acquisition_to_nwbfile(nwbfile, roundtrip, roundtripper,
                                            running_acquisition_df_fixture):
    nwbfile = nwb.add_running_acquisition_to_nwbfile(
        nwbfile, running_acquisition_df_fixture)

    if roundtrip:
        obt = roundtripper(nwbfile, BehaviorNwbApi)
    else:
        obt = BehaviorNwbApi.from_nwbfile(nwbfile)

    obt_running_acq_df = obt.get_running_acquisition_df()

    pd.testing.assert_frame_equal(running_acquisition_df_fixture,
                                  obt_running_acq_df,
                                  check_like=True)
def test_add_running_speed_to_nwbfile(nwbfile, running_speed, roundtrip,
                                      roundtripper):

    nwbfile = nwb.add_running_speed_to_nwbfile(nwbfile, running_speed)

    if roundtrip:
        obt = roundtripper(nwbfile, BehaviorNwbApi)
    else:
        obt = BehaviorNwbApi.from_nwbfile(nwbfile)

    obt_running_speed = obt.get_running_speed()

    assert np.allclose(running_speed.timestamps,
                       obt_running_speed['timestamps'])
    assert np.allclose(running_speed.values, obt_running_speed['speed'])
def test_add_behavior_only_metadata(roundtrip, roundtripper,
                                    behavior_only_metadata_fixture):

    metadata = behavior_only_metadata_fixture
    nwbfile = pynwb.NWBFile(session_description='asession',
                            identifier='afile',
                            session_start_time=metadata['date_of_acquisition'])
    nwb.add_metadata(nwbfile, metadata, behavior_only=True)

    if roundtrip:
        obt = roundtripper(nwbfile, BehaviorNwbApi)
    else:
        obt = BehaviorNwbApi.from_nwbfile(nwbfile)

    metadata_obt = obt.get_metadata()

    assert len(metadata_obt) == len(metadata)
    for key, val in metadata.items():
        assert val == metadata_obt[key]
Esempio n. 10
0
def test_add_task_parameters(nwbfile, roundtrip,
                             roundtripper, task_parameters):

    nwb.add_task_parameters(nwbfile, task_parameters)

    if roundtrip:
        obt = roundtripper(nwbfile, BehaviorNwbApi)
    else:
        obt = BehaviorNwbApi.from_nwbfile(nwbfile)

    task_parameters_obt = obt.get_task_parameters()

    assert len(task_parameters_obt) == len(task_parameters)
    for key, val in task_parameters.items():
        if key == 'omitted_flash_fraction':
            if math.isnan(val):
                assert math.isnan(task_parameters_obt[key])
            if math.isnan(task_parameters_obt[key]):
                assert math.isnan(val)
        else:
            assert val == task_parameters_obt[key]
Esempio n. 11
0
 def from_nwb_path(cls, nwb_path: str,
                   **api_kwargs: Any) -> "BehaviorSession":
     return cls(api=BehaviorNwbApi.from_path(path=nwb_path, **api_kwargs))