Skip to content

Commit bc39e04

Browse files
authored
Allow files as substrings of other files in Nwbfile (#1382)
* Allow files as substrings of other files in Nwbfile * Update changelog * Fix failing tests * Remove helper * Remove deprecated func
1 parent dcff212 commit bc39e04

File tree

5 files changed

+13
-29
lines changed

5 files changed

+13
-29
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import all foreign key references.
3333
- Add methods for calling moseq visualization functions #1374
3434
- Common
3535
- Add tables for storing optogenetic experiment information #1312
36+
- Remove wildcard matching in `Nwbfile().get_abs_path` #1382
3637
- Decoding
3738
- Ensure results directory is created if it doesn't exist #1362
3839
- Position

src/spyglass/common/common_behav.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def insert_from_nwbfile(cls, nwb_file_name, skip_duplicates=False) -> None:
7878
"""
7979
nwbf = get_nwb_file(nwb_file_name)
8080
all_pos = get_all_spatial_series(nwbf, verbose=True)
81-
sess_key = Nwbfile.get_file_key(nwb_file_name)
81+
sess_key = {"nwb_file_name": nwb_file_name}
8282
src_key = dict(**sess_key, source="imported", import_file_name="")
8383

8484
if all_pos is None:

src/spyglass/common/common_nwbfile.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,6 @@ def fetch_nwb(self):
8484
for file in self.fetch("nwb_file_name")
8585
]
8686

87-
@classmethod
88-
def _get_file_name(cls, nwb_file_name: str) -> str:
89-
"""Get valid nwb file name given substring."""
90-
query = cls & f'nwb_file_name LIKE "%{nwb_file_name}%"'
91-
92-
if len(query) == 1:
93-
return query.fetch1("nwb_file_name")
94-
95-
raise ValueError(
96-
f"Found {len(query)} matches for {nwb_file_name} in Nwbfile table:"
97-
+ f" \n{query}"
98-
)
99-
100-
@classmethod
101-
def get_file_key(cls, nwb_file_name: str) -> dict:
102-
"""Return primary key using nwb_file_name substring."""
103-
return {"nwb_file_name": cls._get_file_name(nwb_file_name)}
104-
10587
@classmethod
10688
def get_abs_path(
10789
cls, nwb_file_name: str, new_file: bool = False, **kwargs
@@ -124,10 +106,17 @@ def get_abs_path(
124106
nwb_file_abspath : str
125107
The absolute path for the given file name.
126108
"""
109+
file_path = raw_dir + "/" + nwb_file_name
127110
if new_file:
128-
return raw_dir + "/" + nwb_file_name
111+
return file_path
112+
113+
query = cls & {"nwb_file_name": nwb_file_name}
114+
if len(query) != 1:
115+
raise ValueError(
116+
f"Could not find 1 entry for {nwb_file_name}:\n{query}"
117+
)
129118

130-
return raw_dir + "/" + cls._get_file_name(nwb_file_name)
119+
return file_path
131120

132121
@staticmethod
133122
def add_to_lock(nwb_file_name: str) -> None:

tests/common/test_ephys.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
from ..conftest import TEARDOWN
55

66

7-
def test_create_from_config(mini_insert, common_ephys, mini_path):
7+
def test_create_from_config(mini_insert, common_ephys, mini_copy_name):
88
before = common_ephys.Electrode().fetch()
9-
common_ephys.Electrode.create_from_config(mini_path.stem)
9+
common_ephys.Electrode.create_from_config(mini_copy_name)
1010
after = common_ephys.Electrode().fetch()
1111
# Because already inserted, expect no change
1212
assert array_equal(

tests/common/test_nwbfile.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ def lockfile(base_dir, teardown):
1919
os.remove(lockfile)
2020

2121

22-
def test_get_file_name_error(common_nwbfile):
23-
"""Test that an error is raised when trying non-existent file."""
24-
with pytest.raises(ValueError):
25-
common_nwbfile.Nwbfile._get_file_name("non-existent-file.nwb")
26-
27-
2822
def test_add_to_lock(common_nwbfile, lockfile, mini_copy_name):
2923
common_nwbfile.Nwbfile.add_to_lock(mini_copy_name)
3024
with lockfile.open("r") as f:

0 commit comments

Comments
 (0)