Skip to content

Commit

Permalink
Typing edits
Browse files Browse the repository at this point in the history
  • Loading branch information
GraysonBellamy committed Jun 15, 2024
1 parent 29d2642 commit cb86225
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 278 deletions.
14 changes: 9 additions & 5 deletions pyalicat/comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async def _read(self, len: int) -> ByteString | None:
Returns:
ByteString: The serial communication.
"""
pass
raise NotImplementedError

@abstractmethod
async def _write(self, command: str) -> None:
Expand All @@ -43,12 +43,12 @@ async def _write(self, command: str) -> None:
Args:
command (str): The serial communication.
"""
pass
raise NotImplementedError

@abstractmethod
async def close(self):
"""Closes the serial communication."""
pass
raise NotImplementedError

@abstractmethod
async def _readline(self) -> str | None:
Expand All @@ -57,7 +57,7 @@ async def _readline(self) -> str | None:
Returns:
str: The serial communication.
"""
pass
raise NotImplementedError

@abstractmethod
async def _write_readline(self, command: str) -> str | None:
Expand All @@ -69,7 +69,7 @@ async def _write_readline(self, command: str) -> str | None:
Returns:
str: The serial communication.
"""
pass
raise NotImplementedError


class SerialDevice(CommDevice):
Expand Down Expand Up @@ -115,6 +115,10 @@ def __init__(
# "rtscts": rtscts,
}
self.isOpen = False
if SerialStream is None:
raise ImportError(
"SerialStream could not be loaded. OS possibly not supported."
)
self.ser_devc = SerialStream(**self.serial_setup)

async def _read(self, len: int | None = None) -> ByteString | None:
Expand Down
33 changes: 19 additions & 14 deletions pyalicat/daq.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,10 @@ async def logging(
for dev in self.df:
unique.update(self.df[dev])
await self.create_table(unique, conn)
start = time.time_ns()
start = time.perf_counter_ns()
prev = start
reps = 0
while (time.time_ns() - start) / 1e9 <= duration:
while (time.perf_counter_ns() - start) / 1e9 <= duration:
# Check if something in queue
if not self.qin.empty():
comm = self.qin.get()
Expand All @@ -362,32 +362,35 @@ async def logging(
df = await comm[0](*comm[1:])
self.qout.put(df)
# if not, continue logging
if time.time_ns() / 1e9 - (reps * 1 / rate + start / 1e9) >= 1 / rate:
if (
time.perf_counter_ns() / 1e9 - (reps * 1 / rate + start / 1e9)
>= 1 / rate
):
# Check if something is in the queue
print(
f"Difference between readings: {(time.time_ns() - prev) / 1e9} s"
f"Difference between readings: {(time.perf_counter_ns() - prev) / 1e9} s"
)
# if (
# abs(time.time_ns() / 1e9 - reps * 1 / rate - start / 1e9)
# abs(time.perf_counter_ns() / 1e9 - reps * 1 / rate - start / 1e9)
# > 1.003 / rate
# ):
# warnings.warn("Warning! Acquisition rate is too high!")
time1 = time.time_ns()
prev = time.time_ns()
time1 = time.perf_counter_ns()
prev = time.perf_counter_ns()
if write_async:
nurse_time = time.time_ns()
nurse_time = time.perf_counter_ns()
# open_nursery
async with create_task_group() as g:
# insert_data from the previous iteration
g.start_soon(self.insert_data, rows, conn)
# get
g.start_soon(self.update_dict_log, self.Daq, self.qualities)
else:
nurse_time = time.time_ns()
nurse_time = time.perf_counter_ns()
# Get the data
self.df = await self.Daq.get(self.qualities)
# Write the data from this iteration
time2 = time.time_ns()
time2 = time.perf_counter_ns()
rows = []
for dev in self.df:
rows.append(
Expand All @@ -407,23 +410,25 @@ async def logging(
}
)
print(f"Process took {(time2 - time1) / 1e6} ms")
time3 = time.time_ns()
time3 = time.perf_counter_ns()
if not write_async:
await self.insert_data(
rows, conn
) # This takes a little bit (~8 ms). I think we should run this in a nursery with the next .get() call. That means that we will have to wait until the next loop to submit the data from the previous iteration.
time4 = time.time_ns()
time4 = time.perf_counter_ns()
print(f"Insert took {(time4 - time3) / 1e6} ms")
print(
f"Time with nursery is {write_async}: {(time4 - nurse_time) / 1e6} ms"
)
reps += 1
while (time.time_ns() - start) / 1e9 / (
while (time.perf_counter_ns() - start) / 1e9 / (
1 / rate
) >= 1.00 * reps + 1:
reps += 1
warnings.warn("Warning! Process takes too long!")
print(f"Total time: {(time.time_ns() - start) / 1e9} s with {reps} reps")
print(
f"Total time: {(time.perf_counter_ns() - start) / 1e9} s with {reps} reps"
)

def start_logging(
self, write_async: bool = False, duration: float = "", rate: float = ""
Expand Down
Loading

0 comments on commit cb86225

Please sign in to comment.