Skip to content

Commit

Permalink
Merge pull request #9 from Aharoni-Lab/bugfix-relative-paths
Browse files Browse the repository at this point in the history
bugfix: relative and absolute paths
  • Loading branch information
t-sasatani authored Sep 4, 2023
2 parents 508e76c + 6b929b1 commit 530820e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
34 changes: 13 additions & 21 deletions miniscope_io/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
29 changes: 29 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 530820e

Please sign in to comment.