Skip to content

Commit

Permalink
lint!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
sneakers-the-rat committed Jun 21, 2024
1 parent 87a74c5 commit beec7d0
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 158 deletions.
20 changes: 5 additions & 15 deletions miniscope_io/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,14 @@ class Frame(BaseModel, arbitrary_types_allowed=True):

@field_validator("headers")
@classmethod
def frame_nums_must_be_equal(
cls, v: List[SDBufferHeader]
) -> Optional[List[SDBufferHeader]]:
def frame_nums_must_be_equal(cls, v: List[SDBufferHeader]) -> Optional[List[SDBufferHeader]]:
"""
Each frame_number field in each header must be the same
(they come from the same frame!)
"""

if v is not None and not all(
[header.frame_num != v[0].frame_num for header in v]
):
raise ValueError(
f"All frame numbers should be equal! Got f{[h.frame_num for h in v]}"
)
if v is not None and not all([header.frame_num != v[0].frame_num for header in v]):
raise ValueError(f"All frame numbers should be equal! Got f{[h.frame_num for h in v]}")
return v

@property
Expand All @@ -55,16 +49,12 @@ class Frames(BaseModel):
frames: List[Frame]

@overload
def flatten_headers(
self, as_dict: Literal[False] = False
) -> List[SDBufferHeader]: ...
def flatten_headers(self, as_dict: Literal[False] = False) -> List[SDBufferHeader]: ...

@overload
def flatten_headers(self, as_dict: Literal[True] = True) -> List[dict]: ...

def flatten_headers(
self, as_dict: bool = False
) -> Union[List[dict], List[SDBufferHeader]]:
def flatten_headers(self, as_dict: bool = False) -> Union[List[dict], List[SDBufferHeader]]:
"""
Return flat list of headers, not grouped by frame
Expand Down
4 changes: 1 addition & 3 deletions miniscope_io/device_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ def updateDevice() -> None:

# set up serial port
try:
serial_port = serial.Serial(
port=comport, baudrate=baudrate, timeout=5, stopbits=1
)
serial_port = serial.Serial(port=comport, baudrate=baudrate, timeout=5, stopbits=1)
except Exception as e:
logger.exception(e)
raise e
Expand Down
8 changes: 2 additions & 6 deletions miniscope_io/devices/opalkelly.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,9 @@ def uploadBit(self, bit_file: str) -> None:
if ret == self.NoError:
self.logger.debug(f"Succesfully uploaded {bit_file}")
else:
raise DeviceConfigurationError(
f"Configuration of {self.info.productName} failed"
)
raise DeviceConfigurationError(f"Configuration of {self.info.productName} failed")
self.logger.debug(
"FrontPanel {} supported".format(
"is" if self.IsFrontPanelEnabled() else "not"
)
"FrontPanel {} supported".format("is" if self.IsFrontPanelEnabled() else "not")
)
ret = self.ResetFPGA()

Expand Down
40 changes: 12 additions & 28 deletions miniscope_io/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ def config(self) -> SDConfig:
if self._config is None:
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
)
configSectorData = np.frombuffer(sd.read(self.layout.sectors.size), dtype=np.uint32)

self._config = SDConfig(
**{
Expand Down Expand Up @@ -159,8 +157,7 @@ def frame_count(self) -> int:

self._frame_count = int(
np.ceil(
(self.config.n_buffers_recorded + self.config.n_buffers_dropped)
/ len(headers)
(self.config.n_buffers_recorded + self.config.n_buffers_dropped) / len(headers)
)
)

Expand All @@ -187,9 +184,7 @@ def __enter__(self) -> "SDCard":

# init private attrs
# create an empty frame to hold our data!
self._array = np.zeros(
(self.config.width * self.config.height, 1), dtype=np.uint8
)
self._array = np.zeros((self.config.width * self.config.height, 1), dtype=np.uint8)
self._pixel_count = 0
self._last_buffer_n = 0
self._frame = 0
Expand All @@ -202,7 +197,7 @@ def __enter__(self) -> "SDCard":

return self

def __exit__(self, exc_type, exc_value, traceback):
def __exit__(self, exc_type, exc_val, exc_tb): # noqa: ANN001
self._f.close()
self._f = None
self._frame = 0
Expand All @@ -224,9 +219,7 @@ def _read_data_header(self, sd: BinaryIO) -> SDBufferHeader:
dataHeader = np.append(
dataHeader,
np.frombuffer(
sd.read(
(dataHeader[self.layout.buffer.length] - 1) * self.layout.word_size
),
sd.read((dataHeader[self.layout.buffer.length] - 1) * self.layout.word_size),
dtype=np.uint32,
),
)
Expand Down Expand Up @@ -265,9 +258,7 @@ def _read_size(self, header: SDBufferHeader) -> int:
them separate in case they are separable actions for now
"""
n_blocks = self._n_frame_blocks(header)
read_size = (n_blocks * self.layout.sectors.size) - (
header.length * self.layout.word_size
)
read_size = (n_blocks * self.layout.sectors.size) - (header.length * self.layout.word_size)
return read_size

def _read_buffer(self, sd: BinaryIO, header: SDBufferHeader) -> np.ndarray:
Expand Down Expand Up @@ -340,18 +331,14 @@ def read(self, return_header: bool = False) -> Union[np.ndarray, Frame]:
# blank, and thus have a value of 0 for the header size, and we
# can't read 0 from the card.
self._f.seek(last_position, 0)
raise EndOfRecordingException(
"Reached the end of the video!"
) from None
raise EndOfRecordingException("Reached the end of the video!") from None
else:
raise e
except IndexError as e:
if "index 0 is out of bounds for axis 0 with size 0" in str(e):
# end of file if we are reading from a disk image without any
# additional space on disk
raise EndOfRecordingException(
"Reached the end of the video!"
) from None
raise EndOfRecordingException("Reached the end of the video!") from None
else:
raise e

Expand Down Expand Up @@ -387,7 +374,7 @@ def to_video(
isColor: bool = False,
force: bool = False,
progress: bool = True,
):
) -> None:
"""
Save contents of SD card to video with opencv
Expand All @@ -411,8 +398,7 @@ def to_video(
path = Path(path)
if path.exists() and not force:
raise FileExistsError(
f"{str(path)} already exists, not overwriting. "
"Use force=True to overwrite."
f"{str(path)} already exists, not overwriting. " "Use force=True to overwrite."
)

if progress:
Expand Down Expand Up @@ -454,7 +440,7 @@ def to_video(
# General Methods
# --------------------------------------------------

def skip(self):
def skip(self) -> None:
"""
Skip a frame
Expand Down Expand Up @@ -502,9 +488,7 @@ def check_valid(self) -> bool:
"""
with open(self.drive, "rb") as sd:
sd.seek(self.layout.sectors.header_pos, 0)
headerSectorData = np.frombuffer(
sd.read(self.layout.sectors.size), dtype=np.uint32
)
headerSectorData = np.frombuffer(sd.read(self.layout.sectors.size), dtype=np.uint32)

valid = False
if (
Expand Down
14 changes: 7 additions & 7 deletions miniscope_io/logging.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
Logging factory and handlers
"""

import logging
from logging.handlers import RotatingFileHandler
from pathlib import Path
Expand All @@ -15,7 +19,7 @@ def init_logger(
file_level: Optional[LOG_LEVELS] = None,
log_file_n: Optional[int] = None,
log_file_size: Optional[int] = None,
):
) -> logging.Logger:
"""
Make a logger.
Expand Down Expand Up @@ -60,9 +64,7 @@ def init_logger(

# Add handlers for stdout and file
if log_dir is not False:
logger.addHandler(
_file_handler(name, file_level, log_dir, log_file_n, log_file_size)
)
logger.addHandler(_file_handler(name, file_level, log_dir, log_file_n, log_file_size))

logger.addHandler(_rich_handler())

Expand All @@ -82,9 +84,7 @@ def _file_handler(
file_handler = RotatingFileHandler(
str(filename), mode="a", maxBytes=log_file_size, backupCount=log_file_n
)
file_formatter = logging.Formatter(
"[%(asctime)s] %(levelname)s [%(name)s]: %(message)s"
)
file_formatter = logging.Formatter("[%(asctime)s] %(levelname)s [%(name)s]: %(message)s")
file_handler.setLevel(file_level)
file_handler.setFormatter(file_formatter)
return file_handler
Expand Down
4 changes: 4 additions & 0 deletions miniscope_io/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
Data models :)
"""

from miniscope_io.models.models import Container, MiniscopeConfig, MiniscopeIOModel

__all__ = [
Expand Down
14 changes: 12 additions & 2 deletions miniscope_io/models/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
Module-global configuration models
"""

from pathlib import Path
from typing import Literal, Optional

Expand Down Expand Up @@ -38,7 +42,10 @@ class LogConfig(MiniscopeIOModel):

@field_validator("level", "level_file", "level_stdout", mode="before")
@classmethod
def uppercase_levels(cls, value: Optional[str] = None):
def uppercase_levels(cls, value: Optional[str] = None) -> Optional[str]:
"""
Ensure log level strings are uppercased
"""
if value is not None:
value = value.upper()
return value
Expand Down Expand Up @@ -74,14 +81,16 @@ class Config(BaseSettings):

base_dir: Path = Field(
_default_basedir,
description="Base directory to store configuration and other temporary files, other paths are relative to this by default",
description="Base directory to store configuration and other temporary files, "
"other paths are relative to this by default",
)
log_dir: Path = Field(Path("logs"), description="Location to store logs")
logs: LogConfig = Field(LogConfig(), description="Additional settings for logs")

@field_validator("base_dir", mode="before")
@classmethod
def folder_exists(cls, v: Path) -> Path:
"""Ensure base_dir exists, make it otherwise"""
v = Path(v)
v.mkdir(exist_ok=True, parents=True)

Expand All @@ -90,6 +99,7 @@ def folder_exists(cls, v: Path) -> Path:

@model_validator(mode="after")
def paths_relative_to_basedir(self) -> "Config":
"""If relative paths are given, make them absolute relative to ``base_dir``"""
paths = ("log_dir",)
for path_name in paths:
path = getattr(self, path_name) # type: Path
Expand Down
1 change: 1 addition & 0 deletions miniscope_io/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class YAMLMixin:

@classmethod
def from_yaml(cls: Type[T], file_path: Union[str, Path]) -> T:
"""Instantiate this class by passing the contents of a yaml file as kwargs"""
with open(file_path) as file:
config_data = yaml.safe_load(file)
return cls(**config_data)
11 changes: 7 additions & 4 deletions miniscope_io/models/sdcard.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,14 @@ class SDLayout(MiniscopeConfig):
write_key2: int = 0x0D7CBA17
write_key3: int = 0x0D7CBA17
"""
These don't seem to actually be used in the existing reading/writing code, but we will leave them here for continuity's sake :)
These don't seem to actually be used in the existing reading/writing code,
but we will leave them here for continuity's sake :)
"""
word_size: int = 4
"""
I'm actually not sure what this is, but 4 is hardcoded a few times in the existing notebook and it
appears to be used as a word size when reading from the SD card.
I'm actually not sure what this is, but 4 is hardcoded a few times in the
existing notebook and it appears to be used as a word size when
reading from the SD card.
"""

header: SDHeaderPositions = SDHeaderPositions()
Expand All @@ -140,7 +142,8 @@ class SDConfig(MiniscopeConfig):
"""
The configuration of a recording taken on this SD card.
Read from the locations given in :class:`.ConfigPositions` for an SD card with a given :class:`.SDLayout`
Read from the locations given in :class:`.ConfigPositions`
for an SD card with a given :class:`.SDLayout`
"""

width: int
Expand Down
Loading

0 comments on commit beec7d0

Please sign in to comment.