Skip to content

Commit

Permalink
fall back on sync_messages.txt if timestamps.npy missing (fixes #27)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmeliza committed Jul 18, 2023
1 parent b29975d commit 52b4550
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions arfx/oephys.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import numpy as np
import logging
import arf
import re

from arfx import core

Expand Down Expand Up @@ -41,9 +42,30 @@ def __init__(self, base, structure):
)
super().__init__(base, structure)

timestamps = np.load(os.path.join(self.path, "timestamps.npy"), mmap_mode="r")
self.offset = timestamps[0] / self.sampling_rate
# the timestamps.npy file can get deleted during spike sorting, so we
# fall back on the sync_messages.txt file
try:
timestamps = np.load(
os.path.join(self.path, "timestamps.npy"), mmap_mode="r"
)
sample_offset = timestamps[0]
except FileNotFoundError:
log.warn(
"- warning: timestamps.npy file is missing for %s; falling back on sync_messages.txt",
self.name,
)
processor, idx, subidx = (
structure["source_processor_name"],
structure["source_processor_id"],
structure["source_processor_sub_idx"],
)
sample_offset = find_sync_time(base, processor, idx, subidx)
if sample_offset is None:
raise RuntimeError(
"unable to determine sync time for %s dataset" % self.name
)

self.offset = sample_offset / self.sampling_rate
self.dtype = np.dtype("int16")
datfile = os.path.join(self.path, "continuous.dat")
self.fp = open(datfile, "rb")
Expand All @@ -57,11 +79,12 @@ def __init__(self, base, structure):
)
self.nsamples = size // self.nchannels
log.debug(
"- %s: array (%d, %d) @ %.1f/s",
"- %s: array (%d, %d) @ %.1f/s; offset=%.3f s",
structure["folder_name"],
self.nsamples,
self.nchannels,
self.sampling_rate,
self.offset,
)

@property
Expand Down Expand Up @@ -167,6 +190,25 @@ def __init__(self, path):
self.attrs.update(structure)


def find_sync_time(path, processor, id, subid):
"""Look up the start time for a processor using the sync_messages.txt file. Returns None if there is no match"""

rx = re.compile(
r"Processor: (?P<processor>.+?) Id: (?P<id>\d+?) subProcessor: (?P<subid>\d+?) start time: (?P<start>\d+)"
)
with open(os.path.join(path, "sync_messages.txt"), "r") as fp:
for line in fp:
match = rx.match(line)
if match is None:
continue
if (
match.group("processor") == processor
and int(match.group("id")) == int(id)
and int(match.group("subid")) == int(subid)
):
return int(match.group("start"))


def script(argv=None):
import argparse
import glob
Expand Down

0 comments on commit 52b4550

Please sign in to comment.