Skip to content

Commit

Permalink
add linting, reformat with black
Browse files Browse the repository at this point in the history
  • Loading branch information
sneakers-the-rat committed Jun 14, 2024
1 parent d1ada29 commit 9682506
Show file tree
Hide file tree
Showing 25 changed files with 1,094 additions and 871 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Lint

on:
push:

jobs:
lint:
runs-on: ubuntu-latest
continue-on-error: true
steps:
- uses: actions/checkout@v3
- uses: chartboost/ruff-action@v1
- uses: psf/black@stable
20 changes: 10 additions & 10 deletions miniscope_io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
from miniscope_io.io import SDCard

BASE_DIR = Path(__file__).parent.resolve()
DATA_DIR = BASE_DIR / 'data'
CONFIG_DIR = DATA_DIR / 'config'
DEVICE_DIR = BASE_DIR / 'devices'
DATA_DIR = BASE_DIR / "data"
CONFIG_DIR = DATA_DIR / "config"
DEVICE_DIR = BASE_DIR / "devices"

__all__ = [
'BASE_DIR',
'DATA_DIR',
'CONFIG_DIR',
'Config',
'SDCard',
'init_logger',
]
"BASE_DIR",
"DATA_DIR",
"CONFIG_DIR",
"Config",
"SDCard",
"init_logger",
]
33 changes: 23 additions & 10 deletions miniscope_io/data.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
"""
Classes for using in-memory data from a miniscope
"""

import numpy as np
from typing import List, Optional, overload, Literal, Union
from miniscope_io.models.sdcard import SDBufferHeader
from pydantic import BaseModel, field_validator

import pandas as pd


class Frame(BaseModel, arbitrary_types_allowed=True):
"""
An individual frame from a miniscope recording
Typically returned from :meth:`.SDCard.read`
"""

data: Optional[np.ndarray] = None
headers: Optional[List[SDBufferHeader]] = None

@field_validator('headers')
@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 @@ -40,15 +49,20 @@ class Frames(BaseModel):
"""
A collection of frames from a miniscope recording
"""

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: 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 All @@ -65,7 +79,7 @@ def flatten_headers(self, as_dict:bool = False) -> Union[List[dict],List[SDBuffe
h.extend(headers)
return h

def to_df(self, what:Literal['headers']='headers') -> pd.DataFrame:
def to_df(self, what: Literal["headers"] = "headers") -> pd.DataFrame:
"""
Convert frames to pandas dataframe
Expand All @@ -77,5 +91,4 @@ def to_df(self, what:Literal['headers']='headers') -> pd.DataFrame:
if what == "headers":
return pd.DataFrame(self.flatten_headers(as_dict=True))
else:
raise ValueError('Return mode not implemented!')

raise ValueError("Return mode not implemented!")
14 changes: 6 additions & 8 deletions miniscope_io/device_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
updateDeviceParser.add_argument("module", help="module to update")
updateDeviceParser.add_argument("value", help="LED value")


def updateDevice():
'''
"""
Script to update hardware settings over a generic UART-USB converter.
This script currently supports updating the excitation LED brightness and electrical wetting lens driver gain.
Not tested after separating from stream_daq.py.
Expand All @@ -25,8 +26,8 @@ def updateDevice():
..todo::
Test to see if changing package structure broke anything.
'''
logger = init_logger('streamDaq')
"""
logger = init_logger("streamDaq")

args = updateDeviceParser.parse_args()
moduleList = ["LED", "EWL"]
Expand Down Expand Up @@ -73,7 +74,7 @@ def updateDevice():
except AssertionError as msg:
err_str = "Available modules:\n"
for module in moduleList:
err_str += "\t" + module + '\n'
err_str += "\t" + module + "\n"
logger.exception(err_str)
raise msg

Expand Down Expand Up @@ -103,9 +104,7 @@ def updateDevice():
command = [0, 0]

command[0] = int(
Preamble[0] * 2**preamblePos
+ deviceTag
+ np.floor(value / (2**uartPayload))
Preamble[0] * 2**preamblePos + deviceTag + np.floor(value / (2**uartPayload))
).to_bytes(1, "big")
command[1] = int(
Preamble[1] * 2**preamblePos + deviceTag + value % (2**uartPayload)
Expand All @@ -131,4 +130,3 @@ def updateDevice():
logger.info("\t" + module + ": " + str(value))
logger.info("Close serial port")
sys.exit(1)

1 change: 1 addition & 0 deletions miniscope_io/devices/opalkelly.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class okDev(ok.okCFrontPanel):
and how they're generated/where they're located.
"""

def __init__(self, serial_id: str = ""):
super().__init__()
ret = self.OpenBySerial("")
Expand Down
4 changes: 2 additions & 2 deletions miniscope_io/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

class InvalidSDException(Exception):
"""
Raised when :class:`.io.SDCard` is used with a drive that doesn't have the appropriate WRITE KEYS in its header
"""


class EndOfRecordingException(StopIteration):
"""
Raised when :class:`.io.SDCard` is at the end of the available recording!
"""
"""
4 changes: 1 addition & 3 deletions miniscope_io/formats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@

from miniscope_io.formats.sdcard import WireFreeSDLayout

__all__ = [
"WireFreeSDLayout"
]
__all__ = ["WireFreeSDLayout"]
128 changes: 55 additions & 73 deletions miniscope_io/formats/sdcard.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,43 @@
SD Card data layout formats for different miniscopes!
"""

from miniscope_io.models.sdcard import SectorConfig, ConfigPositions, SDHeaderPositions, \
BufferHeaderPositions, SDLayout
from miniscope_io.models.sdcard import (
SectorConfig,
ConfigPositions,
SDHeaderPositions,
BufferHeaderPositions,
SDLayout,
)

WireFreeSDLayout = SDLayout(
version="0.1.1",
sectors=SectorConfig(
header = 1022,
config = 1023,
data = 1024,
size = 512
sectors=SectorConfig(header=1022, config=1023, data=1024, size=512),
write_key0=0x0D7CBA17,
write_key1=0x0D7CBA17,
write_key2=0x0D7CBA17,
write_key3=0x0D7CBA17,
header=SDHeaderPositions(
gain=4, led=5, ewl=6, record_length=7, fs=8, delay_start=9, battery_cutoff=10
),
write_key0 = 0x0D7CBA17,
write_key1 = 0x0D7CBA17,
write_key2 = 0x0D7CBA17,
write_key3 = 0x0D7CBA17,
header = SDHeaderPositions(
gain = 4,
led = 5,
ewl = 6,
record_length = 7,
fs = 8,
delay_start = 9,
battery_cutoff = 10
config=ConfigPositions(
width=0,
height=1,
fs=2,
buffer_size=3,
n_buffers_recorded=4,
n_buffers_dropped=5,
),
config = ConfigPositions(
width = 0,
height = 1,
fs = 2,
buffer_size = 3,
n_buffers_recorded = 4,
n_buffers_dropped = 5
buffer=BufferHeaderPositions(
length=0,
linked_list=1,
frame_num=2,
buffer_count=3,
frame_buffer_count=4,
write_buffer_count=5,
dropped_buffer_count=6,
timestamp=7,
data_length=8,
),
buffer = BufferHeaderPositions(
length = 0,
linked_list = 1,
frame_num = 2,
buffer_count = 3,
frame_buffer_count = 4,
write_buffer_count = 5,
dropped_buffer_count = 6,
timestamp = 7,
data_length = 8
)
)

WireFreeSDLayout_Battery = SDLayout(**WireFreeSDLayout.model_dump())
Expand All @@ -57,42 +51,30 @@
WireFreeSDLayout_Battery.buffer.battery_voltage = 10



WireFreeSDLayout_Old = SDLayout(
sectors=SectorConfig(
header = 1023,
config = 1024,
data = 1025,
size = 512
sectors=SectorConfig(header=1023, config=1024, data=1025, size=512),
write_key0=0x0D7CBA17,
write_key1=0x0D7CBA17,
write_key2=0x0D7CBA17,
write_key3=0x0D7CBA17,
header=SDHeaderPositions(gain=4, led=5, ewl=6, record_length=7, fs=8),
config=ConfigPositions(
width=0,
height=1,
fs=2,
buffer_size=3,
n_buffers_recorded=4,
n_buffers_dropped=5,
),
write_key0 = 0x0D7CBA17,
write_key1 = 0x0D7CBA17,
write_key2 = 0x0D7CBA17,
write_key3 = 0x0D7CBA17,
header = SDHeaderPositions(
gain = 4,
led = 5,
ewl = 6,
record_length = 7,
fs = 8
buffer=BufferHeaderPositions(
length=0,
linked_list=1,
frame_num=2,
buffer_count=3,
frame_buffer_count=4,
write_buffer_count=5,
dropped_buffer_count=6,
timestamp=7,
data_length=8,
),
config = ConfigPositions(
width = 0,
height = 1,
fs = 2,
buffer_size = 3,
n_buffers_recorded = 4,
n_buffers_dropped = 5
),
buffer = BufferHeaderPositions(
length = 0,
linked_list = 1,
frame_num = 2,
buffer_count = 3,
frame_buffer_count = 4,
write_buffer_count = 5,
dropped_buffer_count = 6,
timestamp = 7,
data_length = 8
)
)
)
12 changes: 6 additions & 6 deletions miniscope_io/formats/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from miniscope_io.models.stream import StreamBufferHeaderFormat

StreamBufferHeader = StreamBufferHeaderFormat(
linked_list = (0, 32),
frame_num = (32, 64),
buffer_count = (64, 96),
frame_buffer_count = (96, 128),
timestamp = (192, 224),
pixel_count = (224, 256)
linked_list=(0, 32),
frame_num=(32, 64),
buffer_count=(64, 96),
frame_buffer_count=(96, 128),
timestamp=(192, 224),
pixel_count=(224, 256),
)
Loading

0 comments on commit 9682506

Please sign in to comment.