Skip to content

Commit

Permalink
refactor(reader): handle empty files gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
chanshing committed Jul 10, 2024
1 parent 6525958 commit e9f8cc6
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/actipy/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def read_device(input_file,

sample_rate = info['SampleRate']

if len(data) == 0:
print("File is empty. No data to process.")
return data, info

if lowpass_hz not in (None, False):
timer.start("Lowpass filter...")
data, info_lowpass = P.lowpass(data, sample_rate, lowpass_hz)
Expand Down Expand Up @@ -198,6 +202,16 @@ def _read_device(input_file, verbose=True):
data = {c: np.asarray(data_mmap[c]) for c in data_mmap.dtype.names}
data = pd.DataFrame(data, copy=False)

if len(data) == 0:
info['NumTicks'] = 0
info['StartTime'] = None
info['EndTime'] = None
info['WearTime(days)'] = 0
info['NumInterrupts'] = 0
data.set_index('time', inplace=True)
timer.stop()
return data, info

# Check for non-increasing timestamps. This is rare but can happen with
# buggy devices. TODO: Parser should do this.
errs = (data['time'].diff() <= pd.Timedelta(0)).sum()
Expand Down

0 comments on commit e9f8cc6

Please sign in to comment.