Skip to content

Commit

Permalink
reworked sleep_readers.py so pandas is not used, reworked test case t…
Browse files Browse the repository at this point in the history
…o work without panda
  • Loading branch information
simon-p-2000 committed Dec 16, 2024
1 parent d7093b6 commit 9364512
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
45 changes: 30 additions & 15 deletions src/sleepecg/io/sleep_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
from xml.etree import ElementTree

import numpy as np
import pandas as pd
from pandas import read_csv

from sleepecg.config import get_config_value
from sleepecg.heartbeats import detect_heartbeats
Expand Down Expand Up @@ -346,7 +344,13 @@ def read_mesa(
)

if activity_source == "actigraphy":
overlap_data = read_csv(overlap_filepath)
overlap_data = []

with open(overlap_filepath) as csv_file:
reader = csv.reader(csv_file, delimiter=",")
header = next(reader)
for row in reader:
overlap_data.append(dict(zip(header, row)))

for record_id in requested_records:
heartbeats_file = heartbeats_dir / f"{record_id}.npy"
Expand Down Expand Up @@ -424,7 +428,13 @@ def read_mesa(
checksums[activity_filename],
)

activity_data = pd.read_csv(activity_filepath)
activity_data = []

with open(activity_filepath) as csv_file:
reader = csv.reader(csv_file, delimiter=",")
header = next(reader)
for row in reader:
activity_data.append(dict(zip(header, row)))

recording_start_time = parsed_xml.recording_start_time
recording_duration = parsed_xml.recording_duration
Expand All @@ -444,19 +454,24 @@ def read_mesa(
)
recording_end_time = recording_end_time.strftime("%H:%M:%S").lstrip("0")

mesa_id = activity_data["mesaid"].iloc[0]
mesa_id = activity_data[0].get("mesaid")

start_line = overlap_data.loc[
overlap_data["mesaid"] == mesa_id, "line"
].iloc[0]
end_line = activity_data.loc[
activity_data["linetime"] == recording_end_time, "line"
].iloc[0]
start_line = int(
next(
row["line"] for row in overlap_data if row.get("mesaid") == mesa_id
)
)
end_line = int(
next(
row["line"]
for row in activity_data
if row.get("linetime") == recording_end_time
)
)

activity_counts = activity_data[
(activity_data["line"] >= start_line)
& (activity_data["line"] <= end_line)
]["activity"].to_list()
activity_counts = [
row["activity"] for row in activity_data[start_line - 1 : end_line]
]

activity_counts = np.array(activity_counts)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_sleep_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def _dummy_nsrr_overlap(filename: str, mesa_ids: list[int]):
with open(filename, "w") as csv:
csv.write("mesaid,line,linetime,starttime_psg\n")
for i in range(len(mesa_ids)):
csv.write(f"{mesa_ids[i][-1]},563,20:30:00,20:29:59\n")
csv.write(f"{mesa_ids[i][-1]},1,20:30:00,20:29:59\n")


def _dummy_nsrr_actigraphy(filename: str, mesa_id: str):
Expand All @@ -35,7 +35,7 @@ def _dummy_nsrr_actigraphy(filename: str, mesa_id: str):
with open(filename, "w") as csv:
csv.write("mesaid,line,linetime,activity\n")
for i in range(10):
csv.write(f"{mesa_id[-1]},{563 + i},{linetimes[i]},10\n")
csv.write(f"{mesa_id[-1]},{1 + i},{linetimes[i]},10\n")


def _dummy_nsrr_edf(filename: str, hours: float, ecg_channel: str):
Expand Down

0 comments on commit 9364512

Please sign in to comment.