コード例 #1
0
def test_nwb_end_to_end(tmpdir_factory):
    oeid = 789359614
    nwb_filepath = os.path.join(str(tmpdir_factory.mktemp('test_nwb_end_to_end')), 'nwbfile.nwb')

    d1 = BehaviorOphysSession.from_lims(oeid)
    BehaviorOphysNwbApi(nwb_filepath).save(d1)

    d2 = BehaviorOphysSession(api=BehaviorOphysNwbApi(nwb_filepath))
    equals(d1, d2, reraise=True)
コード例 #2
0
def test_get_corrected_fluorescence_traces(nwbfile, roundtrip,
                                           filter_invalid_rois, valid_roi_ids,
                                           roundtripper, dff_traces,
                                           corrected_fluorescence_traces,
                                           cell_specimen_table, metadata,
                                           ophys_timestamps):

    nwb.add_metadata(nwbfile, metadata)
    nwb.add_cell_specimen_table(nwbfile, cell_specimen_table)
    nwb.add_dff_traces(nwbfile, dff_traces, ophys_timestamps)
    nwb.add_corrected_fluorescence_traces(nwbfile,
                                          corrected_fluorescence_traces)

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

    if filter_invalid_rois:
        corrected_fluorescence_traces = corrected_fluorescence_traces[
            corrected_fluorescence_traces["cell_roi_id"].isin(valid_roi_ids)]

    pd.testing.assert_frame_equal(corrected_fluorescence_traces,
                                  obt.get_corrected_fluorescence_traces(),
                                  check_dtype=False)
コード例 #3
0
def test_add_stimulus_timestamps(nwbfile, stimulus_timestamps, roundtrip, roundtripper):

    nwb.add_stimulus_timestamps(nwbfile, stimulus_timestamps)

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

    np.testing.assert_array_almost_equal(stimulus_timestamps, obt.get_stimulus_timestamps())
コード例 #4
0
def test_add_average_image(nwbfile, roundtrip, roundtripper, average_image, image_api):

    nwb.add_average_image(nwbfile, average_image)

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

    assert image_api.deserialize(average_image) == image_api.deserialize(obt.get_average_projection())
コード例 #5
0
def test_segmentation_mask_image(nwbfile, roundtrip, roundtripper, segmentation_mask_image, image_api):

    nwb.add_segmentation_mask_image(nwbfile, segmentation_mask_image)

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

    assert image_api.deserialize(segmentation_mask_image) == image_api.deserialize(obt.get_segmentation_mask_image())
コード例 #6
0
def test_add_max_projection(nwbfile, roundtrip, roundtripper, max_projection, image_api):

    nwb.add_max_projection(nwbfile, max_projection)

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

    assert image_api.deserialize(max_projection) == image_api.deserialize(obt.get_max_projection())
コード例 #7
0
def test_add_trials(nwbfile, roundtrip, roundtripper, trials):

    nwb.add_trials(nwbfile, trials, {})

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

    pd.testing.assert_frame_equal(trials, obt.get_trials(), check_dtype=False)
コード例 #8
0
def test_add_running_data_df_to_nwbfile(nwbfile, running_data_df, roundtrip, roundtripper):

    unit_dict = {'v_sig': 'V', 'v_in': 'V', 'speed': 'cm/s', 'timestamps': 's', 'dx': 'cm'}
    nwbfile = nwb.add_running_data_df_to_nwbfile(nwbfile, running_data_df, unit_dict)

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

    pd.testing.assert_frame_equal(running_data_df, obt.get_running_data_df())
コード例 #9
0
def test_add_stimulus_templates(nwbfile, stimulus_templates, roundtrip, roundtripper):
    for key, val in stimulus_templates.items():
        nwb.add_stimulus_template(nwbfile, val, key)

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

    stimulus_templates_obt = obt.get_stimulus_templates()
    for key in set(stimulus_templates.keys()).union(set(stimulus_templates_obt.keys())):
        np.testing.assert_array_almost_equal(stimulus_templates[key], stimulus_templates_obt[key])
コード例 #10
0
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, BehaviorOphysNwbApi)
    else:
        obt = BehaviorOphysNwbApi.from_nwbfile(nwbfile)

    running_speed_obt = obt.get_running_speed()
    assert np.allclose(running_speed.timestamps, running_speed_obt.timestamps)
    assert np.allclose(running_speed.values, running_speed_obt.values)
コード例 #11
0
def test_get_motion_correction(nwbfile, roundtrip, roundtripper, motion_correction, ophys_timestamps, metadata, cell_specimen_table, dff_traces):

    nwb.add_metadata(nwbfile, metadata)
    nwb.add_cell_specimen_table(nwbfile, cell_specimen_table)
    nwb.add_dff_traces(nwbfile, dff_traces, ophys_timestamps)
    nwb.add_motion_correction(nwbfile, motion_correction)

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

    pd.testing.assert_frame_equal(motion_correction, obt.get_motion_correction(), check_dtype=False)
コード例 #12
0
def test_add_metadata(nwbfile, roundtrip, roundtripper, metadata):

    nwb.add_metadata(nwbfile, metadata)

    if roundtrip:
        obt = roundtripper(nwbfile, BehaviorOphysNwbApi)
    else:
        obt = BehaviorOphysNwbApi.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]
コード例 #13
0
def test_get_cell_specimen_table(nwbfile, roundtrip, filter_invalid_rois, valid_roi_ids, roundtripper, cell_specimen_table, metadata, ophys_timestamps):

    nwb.add_metadata(nwbfile, metadata)
    nwb.add_cell_specimen_table(nwbfile, cell_specimen_table)

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

    if filter_invalid_rois:
        cell_specimen_table = cell_specimen_table[cell_specimen_table["cell_roi_id"].isin(valid_roi_ids)]

    pd.testing.assert_frame_equal(cell_specimen_table, obt.get_cell_specimen_table(), check_dtype=False)
コード例 #14
0
def write_behavior_ophys_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:
        session = BehaviorOphysSession(api=BehaviorOphysJsonApi(session_data))
        BehaviorOphysNwbApi(nwb_filepath_inprogress).save(session)
        assert equals(
            session,
            BehaviorOphysSession(
                api=BehaviorOphysNwbApi(nwb_filepath_inprogress)))
        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
コード例 #15
0
def test_add_stimulus_presentations(nwbfile, stimulus_presentations_behavior, stimulus_timestamps, roundtrip, roundtripper, stimulus_templates):
    nwb.add_stimulus_timestamps(nwbfile, stimulus_timestamps)
    nwb.add_stimulus_presentations(nwbfile, stimulus_presentations_behavior)
    for key, val in stimulus_templates.items():
        nwb.add_stimulus_template(nwbfile, val, key)

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

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

    pd.testing.assert_frame_equal(stimulus_presentations_behavior, obt.get_stimulus_presentations(), check_dtype=False)
コード例 #16
0
def test_add_task_parameters(nwbfile, roundtrip, roundtripper, task_parameters):

    nwb.add_task_parameters(nwbfile, task_parameters)

    if roundtrip:
        obt = roundtripper(nwbfile, BehaviorOphysNwbApi)
    else:
        obt = BehaviorOphysNwbApi.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]
コード例 #17
0
        experiment_id = sys.argv[1]

        # Define the cache
        cache_json = {
            'manifest_path':
            '/allen/programs/braintv/workgroups/nc-ophys/visual_behavior/SWDB_2019/visual_behavior_data_manifest.csv',
            'nwb_base_dir':
            '/allen/programs/braintv/workgroups/nc-ophys/visual_behavior/SWDB_2019/nwb_files',
            'analysis_files_base_dir':
            '/allen/programs/braintv/workgroups/nc-ophys/visual_behavior/SWDB_2019/extra_files'
        }

        # load the session
        cache = bpc.BehaviorProjectCache(cache_json)
        nwb_path = cache.get_nwb_filepath(experiment_id)
        api = BehaviorOphysNwbApi(nwb_path, filter_invalid_rois=True)
        session = BehaviorOphysSession(api)

        # Where to save the results
        output_path = '/allen/programs/braintv/workgroups/nc-ophys/visual_behavior/SWDB_2019/flash_response_500msec_response'

        # Define parameters for dff_trace, and response_window
        response_analysis_params = {
            'window_around_timepoint_seconds': [-.5, .75],  # -500ms, 750ms
            'response_window_duration_seconds': 0.5,
            'baseline_window_duration_seconds': 0.5
        }

        # compute the base flash_response_df
        flash_response_df = get_flash_response_df(session,
                                                  response_analysis_params)
コード例 #18
0
 def from_nwb_path(cls, nwb_path: str,
                   **api_kwargs: Any) -> "BehaviorOphysSession":
     api_kwargs["filter_invalid_rois"] = api_kwargs.get(
         "filter_invalid_rois", True)
     return cls(
         api=BehaviorOphysNwbApi.from_path(path=nwb_path, **api_kwargs))
コード例 #19
0
import os

from allensdk.brain_observatory.behavior.behavior_ophys_api.behavior_ophys_nwb_api import BehaviorOphysNwbApi
from allensdk.brain_observatory.behavior.behavior_ophys_session import BehaviorOphysSession

basedir = '/allen/aibs/technology/nicholasc/behavior_ophys'

rel_filepath_list = [
    'behavior_ophys_session_805784331.nwb',
    'behavior_ophys_session_789359614.nwb',
    'behavior_ophys_session_803736273.nwb',
    'behavior_ophys_session_808621958.nwb',
    'behavior_ophys_session_795948257.nwb'
]

for rel_filepath in rel_filepath_list:

    full_filepath = os.path.join(basedir, rel_filepath)
    session = BehaviorOphysSession(api=BehaviorOphysNwbApi(full_filepath))
    print(session.metadata['ophys_experiment_id'])