Exemple #1
0
def test_roundtrip(tmpdir, ref_data_dir):
    """A simple check that file written can be read back in.

    Currently this does not do anything besides ensure there are no failures.
    Reading could fail, for example, if the extension spec is not included
    in the NWB file by accident.
    """
    # Write one of the subsampled experiments to NWB
    expt = 'sample_pointing_fred_170317_10_11_01'
    nwb_path = os.path.join(str(tmpdir), expt + '.nwb')
    labview_path = os.path.join(DATA_PATH, expt)
    with NwbFile(nwb_path, mode='w') as nwb:
        nwb.import_labview_folder(labview_path)

    # You would think the below would be a good check, but actually not.
    # The extensions are already loaded (from writing the file) and I can't
    # find a way to remove them.
    # with pynwb.NWBHDF5IO(nwb_path, 'r', load_namespaces=True) as io:
    #     new_file = io.read()
    # Right now, just check that the file has been read without errors
    # assert isinstance(new_file, pynwb.NWBFile)

    # Since the above isn't a meaningful test, roughly check that we have
    # included the extension spec and custom medata in the file.
    with h5py.File(nwb_path, 'r') as new_file:
        assert 'silverlab_extended_schema' in new_file['specifications']
        assert 'silverlab_metadata' in new_file['general']
        assert 'silverlab_optophysiology' in new_file['general']
def test_ambiguous_user_fails(tmpdir, ref_data_dir):
    fname = "test_metadata_import.nwb"
    nwb_path = os.path.join(str(tmpdir), fname)
    meta_path = os.path.join(ref_data_dir, 'meta_two_users.yaml')
    with pytest.raises(ValueError, match="Multiple users found in file."):
        with NwbFile(nwb_path, 'w') as nwb:
            nwb.create_from_metadata(meta_path)
def test_metadata_only(tmpdir, capfd, ref_data_dir):
    fname = "test_metadata.nwb"
    with NwbFile(os.path.join(str(tmpdir), fname), mode='w') as nwb:
        speed_data, start_time = nwb.create_nwb_file(ref_data_dir,
                                                     'test_metadata')
        nwb.add_core_metadata()
    compare_hdf5(str(tmpdir.join(fname)),
                 os.path.join(ref_data_dir, 'expected_meta_only.yaml'))
def test_no_start_time_fails(tmpdir, ref_data_dir):
    fname = "test_metadata_import.nwb"
    nwb_path = os.path.join(str(tmpdir), fname)
    meta_path = os.path.join(ref_data_dir, 'meta_two_users.yaml')
    with pytest.raises(ValueError) as exc_info:
        with NwbFile(nwb_path, 'w') as nwb:
            nwb.create_from_metadata(meta_path, user="******")
    assert "Start time for session not found!" == str(exc_info.value)
def test_user_not_found_fails(tmpdir, ref_data_dir):
    fname = "test_metadata_import.nwb"
    nwb_path = os.path.join(str(tmpdir), fname)
    meta_path = os.path.join(ref_data_dir, 'meta_two_users.yaml')
    with pytest.raises(ValueError) as exc_info:
        with NwbFile(nwb_path, 'w') as nwb:
            nwb.create_from_metadata(meta_path, user="******")
    assert "No session information found for user" in str(exc_info.value)
Exemple #6
0
def compare_hdf5(nwb_path, expected_yaml_path):
    """Test utility method comparing a generated NWB file against expected contents.

    As a side-effect, checks that we can open files for reading with our API.
    """
    with open(expected_yaml_path, 'r') as f:
        expected = yaml.load(f)
    with NwbFile(nwb_path, mode='r') as nwb:
        compare_group(nwb.hdf_file, expected, '')
Exemple #7
0
    def do_import_test(expt, add_suffix=False):
        """Helper method for tests below."""
        nwb_path = os.path.join(str(tmpdir), expt + '.nwb')
        labview_path = os.path.join(DATA_PATH, expt + ' FunctAcq' if add_suffix else expt)
        sig_path = os.path.join(ref_data_dir, expt + '.sig2')

        with NwbFile(nwb_path, mode='w') as nwb:
            nwb.import_labview_folder(labview_path)
        assert sig_gen.compare_to_sig(nwb_path, sig_path)
def test_epochs(tmpdir, capfd, ref_data_dir):
    fname = "test_epochs.nwb"
    nwb_path = os.path.join(str(tmpdir), fname)
    with NwbFile(nwb_path, mode='w') as nwb:
        speed_data, start_time = nwb.create_nwb_file(ref_data_dir,
                                                     'test_epochs')
        nwb.add_core_metadata()
        nwb.add_speed_data(speed_data, start_time)
        nwb.determine_trial_times()
    compare_hdf5(nwb_path, os.path.join(ref_data_dir, 'expected_epochs.yaml'))
def do_import_test(tmpdir, expt, add_suffix=False):
    """Helper method for tests below."""
    nwb_path = os.path.join(str(tmpdir), expt + '.nwb')
    labview_path = os.path.join(DATA_PATH,
                                expt + ' FunctAcq' if add_suffix else expt)
    sig_path = os.path.join(REF_PATH, expt + '.sig')

    with NwbFile(nwb_path, mode='w') as nwb:
        nwb.import_labview_folder(labview_path)
    assert compare_to_signature(nwb_path, sig_path, ignore_external_file=True)
Exemple #10
0
def test_epochs(tmpdir, capfd):
    data_path = os.path.join(os.path.dirname(__file__), 'data')
    import silverlabnwb
    silverlabnwb.metadata.set_conf_dir(data_path)
    fname = "test_epochs.nwb"
    with NwbFile(os.path.join(str(tmpdir), fname), mode='w') as nwb:
        speed_data, start_time = nwb.create_nwb_file(data_path, 'test_epochs')
        nwb.add_core_metadata()
        nwb.add_speed_data(speed_data, start_time)
        nwb.determine_trial_times()
    compare_hdf5(str(tmpdir.join(fname)), os.path.join(data_path, 'expected.yaml'))
def test_metadata_import_correct(tmpdir, ref_data_dir):
    fname = "metadata_only_B.nwb"
    nwb_path = os.path.join(str(tmpdir), fname)
    meta_path = os.path.join(ref_data_dir, 'meta_two_users.yaml')
    sig_path = os.path.join(ref_data_dir, 'metadata_only_B.sig2')
    with NwbFile(nwb_path, 'w') as nwb:
        nwb.create_from_metadata(meta_path, user="******")
    sig_gen = SignatureGenerator()
    if os.environ.get('SILVERLAB_GEN_REF', '0') != '0':
        sig_gen.save_sig(nwb_path, sig_path)
    assert sig_gen.compare_to_sig(nwb_path, sig_path)
def compare_hdf5(nwb_path, expected_yaml_path):
    """Test utility method comparing a generated NWB file against expected contents.

    As a side-effect, checks that we can open files for reading with our API.
    """
    global current_file
    with open(expected_yaml_path, 'r') as f:
        yaml_instance = YAML(typ='safe')
        expected = yaml_instance.load(f)
    with NwbFile(nwb_path, mode='r') as nwb:
        current_file = nwb.hdf_file
        compare_group(nwb.hdf_file, expected, '')
        current_file = None
Exemple #13
0
def test_generate_signatures(ref_data_dir, sig_gen, nwb_name, monkeypatch):
    """A 'test' to generate reference data for the tests below."""
    sig_path = os.path.join(ref_data_dir, nwb_name + '.sig2')
    if os.environ.get('SILVERLAB_REGEN_NWB', '0') != '0':
        if nwb_name == 'sample_rois_200206_16_30_32':
            # Signature for sample_rois_200206_16_30_32 requires disabling _write_roi_data, as it contains no image data
            def do_nothing(*args):
                pass
            monkeypatch.setattr(NwbFile, "_write_roi_data", do_nothing)
        nwb_path = os.path.join(DATA_PATH, 'nwb2', nwb_name + '.nwb')
        labview_path = os.path.join(DATA_PATH,
                                    nwb_name + ' FunctAcq' if nwb_name[0] == '1' else nwb_name)
        with NwbFile(nwb_path, mode='w') as nwb:
            nwb.import_labview_folder(labview_path)
    else:
        nwb_path = os.path.join(DATA_PATH, nwb_name + '.nwb')
    sig_gen.save_sig(nwb_path, sig_path)
def test_determine_labview_version(tmpdir, ref_data_dir):
    fname = "test_labview_version.nwb"
    with NwbFile(os.path.join(str(tmpdir), fname), mode='w') as nwb:
        speed_data, start_time = nwb.create_nwb_file(ref_data_dir,
                                                     'test_labview_version')
    assert nwb.labview_version is LabViewVersions.pre2018