diff --git a/miniscope_io/io.py b/miniscope_io/io.py index 6eb6254..928dea2 100644 --- a/miniscope_io/io.py +++ b/miniscope_io/io.py @@ -29,15 +29,10 @@ class SDCard: def __init__( self, drive: Union[str, Path], - layout: SDLayout, - directpath: bool = False + layout: SDLayout ): - if directpath == False: - self.drive = Path(drive).resolve() - else: - self.drive = drive - # Logs the error appropriately. + self.drive = drive self.layout = layout # Private attributes used when the file reading context is entered @@ -72,20 +67,17 @@ def __init__( @property def config(self) -> SDConfig: if self._config is None: - try: - with open(self.drive, 'rb') as sd: - sd.seek(self.layout.sectors.config_pos, 0) - configSectorData = np.frombuffer(sd.read(self.layout.sectors.size), dtype=np.uint32) - - self._config = SDConfig( - **{ - k: configSectorData[v] - for k, v in self.layout.config.dict().items() - if v is not None - } - ) - except Exception as error: - print("An exception occurred:", error) + with open(self.drive, 'rb') as sd: + sd.seek(self.layout.sectors.config_pos, 0) + configSectorData = np.frombuffer(sd.read(self.layout.sectors.size), dtype=np.uint32) + + self._config = SDConfig( + **{ + k: configSectorData[v] + for k, v in self.layout.config.dict().items() + if v is not None + } + ) return self._config diff --git a/tests/test_io.py b/tests/test_io.py index dc173e0..b7edf53 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -1,6 +1,10 @@ import pytest +from pathlib import Path +import os from miniscope_io.sdcard import DataHeader +from miniscope_io.formats import WireFreeSDLayout +from miniscope_io.io import SDCard from miniscope_io.exceptions import EndOfRecordingException from miniscope_io.data import Frame @@ -77,6 +81,31 @@ def test_frame_count(wirefree): with pytest.raises(EndOfRecordingException): frame = wirefree.read() +def test_relative_path(): + """ + Test that we can use both relative and absolute paths in the SD card model + """ + # get absolute path of working directory, then get relative path to data from there + abs_cwd = Path(os.getcwd()).resolve() + abs_child = Path(__file__).parent.parent / 'data' / 'wirefree_example.img' + rel_path = abs_child.relative_to(abs_cwd) + + assert not rel_path.is_absolute() + sdcard = SDCard(drive = rel_path, layout = WireFreeSDLayout) + + # check we can do something basic like read config + assert sdcard.config is not None + + # check it remains relative after init + assert not sdcard.drive.is_absolute() + + # now try with an absolute path + abs_path = rel_path.resolve() + assert abs_path.is_absolute() + sdcard_abs = SDCard(drive= abs_path, layout= WireFreeSDLayout) + assert sdcard_abs.config is not None + assert sdcard_abs.drive.is_absolute() + # # @pytest.mark.parametrize(