Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug with w- io mode #1795

Merged
merged 6 commits into from
Dec 14, 2023

Conversation

h-mayorquin
Copy link
Contributor

@h-mayorquin h-mayorquin commented Dec 10, 2023

Motivation

@bendichter

The PR in #1748 introduced a bug where the io mode "w-" still tries to load the name space. See coments:
https://github.com/NeurodataWithoutBorders/pynwb/pull/1748/files#r1421748605
https://github.com/NeurodataWithoutBorders/pynwb/pull/1748/files#r1421748559

How to test the behavior?

Currently this fails in dev / main

from pathlib import Path
from pynwb import NWBHDF5IO
from pynwb.testing.mock.file import mock_NWBFile


nwbfile = mock_NWBFile()

file_path = Path('test.nwb')
if file_path.exists():
    file_path.unlink()

with NWBHDF5IO('test.nwb', 'w-') as io:
    io.write(nwbfile)


Traceback (most recent call last):
  File "/home/heberto/development/pynwb/build/error_test.py", line 13, in <module>
    with NWBHDF5IO('test.nwb', 'w-') as io:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/heberto/miniconda3/envs/pynwb/lib/python3.11/site-packages/hdmf/utils.py", line 644, in func_call
    return func(args[0], **pargs)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/heberto/development/pynwb/src/pynwb/__init__.py", line 269, in __init__
    super().load_namespaces(tm, path, file=file_obj, driver=driver)
  File "/home/heberto/miniconda3/envs/pynwb/lib/python3.11/site-packages/hdmf/utils.py", line 644, in func_call
    return func(args[0], **pargs)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/heberto/miniconda3/envs/pynwb/lib/python3.11/site-packages/hdmf/backends/hdf5/h5tools.py", line 167, in load_namespaces
    open_file_obj = cls.__resolve_file_obj(path, file_obj, driver)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/heberto/miniconda3/envs/pynwb/lib/python3.11/site-packages/hdmf/backends/hdf5/h5tools.py", line 142, in __resolve_file_obj
    file_obj = File(path, 'r', **file_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/heberto/miniconda3/envs/pynwb/lib/python3.11/site-packages/h5py/_hl/files.py", line 567, in __init__
    fid = make_fid(name, mode, userblock_size, fapl, fcpl, swmr=swmr)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/heberto/miniconda3/envs/pynwb/lib/python3.11/site-packages/h5py/_hl/files.py", line 231, in make_fid
    fid = h5f.open(name, flags, fapl=fapl)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py/h5f.pyx", line 106, in h5py.h5f.open
FileNotFoundError: [Errno 2] Unable to open file (unable to open file: name = 'test.nwb', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)

The current PR fixes this.

Checklist

  • Did you update CHANGELOG.md with your changes?
  • Have you checked our Contributing document?
  • Have you ensured the PR clearly describes the problem and the solution?
  • Is your contribution compliant with our coding style? This can be checked running flake8 from the source directory.
  • Have you checked to ensure that there aren't other open Pull Requests for the same change?
  • Have you included the relevant issue number using "Fix #XXX" notation where XXX is the issue number? By including "Fix #XXX" you allow GitHub to close issue #XXX when the PR is merged.

Copy link

codecov bot commented Dec 10, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (0e45cd9) 91.97% compared to head (c3dcf87) 91.97%.

Additional details and impacted files
@@           Coverage Diff           @@
##              dev    #1795   +/-   ##
=======================================
  Coverage   91.97%   91.97%           
=======================================
  Files          27       27           
  Lines        2617     2618    +1     
  Branches      683      683           
=======================================
+ Hits         2407     2408    +1     
  Misses        138      138           
  Partials       72       72           
Flag Coverage Δ
integration 71.23% <100.00%> (+0.01%) ⬆️
unit 83.65% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bendichter
Copy link
Contributor

Good catch

@bendichter bendichter self-requested a review December 10, 2023 22:20
bendichter
bendichter previously approved these changes Dec 10, 2023
Copy link
Contributor

@bendichter bendichter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. Could use a test if it's not too difficult to put one together

@h-mayorquin
Copy link
Contributor Author

@bendichter

We could add the following test:

import unittest
import tempfile
from pathlib import Path
from pynwb import NWBHDF5IO
from pynwb.testing.mock.file import mock_NWBFile


class TestNWBHDF5IO(unittest.TestCase):
    def test_file_creation_and_deletion(self):
        io_modes_that_create_file = ["w", "w-", "x"]

        with tempfile.TemporaryDirectory() as temp_dir:
            temp_dir = Path(temp_dir)
            for io_mode in io_modes_that_create_file:
                file_path = temp_dir / f"test_io_mode={io_mode}.nwb"

                # Test file creation
                nwbfile = mock_NWBFile()
                with NWBHDF5IO(str(file_path), io_mode) as io:
                    io.write(nwbfile)


if __name__ == "__main__":
    unittest.main()

Could this be a good test? if so, where should it be?

@rly
Copy link
Contributor

rly commented Dec 12, 2023

@h-mayorquin I think that test looks fine to cover this use case. Please add the test to this test class https://github.com/NeurodataWithoutBorders/pynwb/blob/dev/tests/integration/hdf5/test_io.py#L19

@h-mayorquin
Copy link
Contributor Author

@rly added the test.

@rly rly marked this pull request as ready for review December 14, 2023 06:31
@rly
Copy link
Contributor

rly commented Dec 14, 2023

Thanks for adding the test. I marked this PR as ready for review. @h-mayorquin I hope that's OK. @bendichter please re-review.

@bendichter bendichter self-requested a review December 14, 2023 17:02
@rly rly merged commit dd2e848 into NeurodataWithoutBorders:dev Dec 14, 2023
22 of 24 checks passed
@h-mayorquin h-mayorquin deleted the fix_creation_mode_list branch December 18, 2023 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants