Skip to content

Commit 0f40c6d

Browse files
committed
Handle empty data files
1 parent 2ad5b87 commit 0f40c6d

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/actinet/actinet.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import numpy as np
1010
import pandas as pd
1111
import joblib
12+
import sys
1213

1314
import actipy
1415

@@ -148,8 +149,37 @@ def main():
148149
# Output paths
149150
basename = resolve_path(args.filepath)[1]
150151
outdir = os.path.join(args.outdir, basename)
152+
outputSummaryFile = f"{outdir}/{basename}-outputSummary.json"
151153
os.makedirs(outdir, exist_ok=True)
152154

155+
# If no data, save info and exit
156+
if len(data) == 0 or data.isna().any(axis=1).all(): # TODO: check na only on x,y,z cols?
157+
# Save info as outputSummary.json
158+
with open(outputSummaryFile, "w") as f:
159+
json.dump(info, f, indent=4, cls=NpEncoder)
160+
161+
# Print
162+
if verbose:
163+
print("\nSummary Stats\n---------------------")
164+
print(
165+
json.dumps(
166+
{
167+
key: info[key]
168+
for key in [
169+
"Filename",
170+
"Filesize(MB)",
171+
"WearTime(days)",
172+
"NonwearTime(days)",
173+
"ReadOK"
174+
]
175+
},
176+
indent=4,
177+
cls=NpEncoder,
178+
)
179+
)
180+
print("No data to process. Exiting early...")
181+
sys.exit(0)
182+
153183
# Run classifier
154184
if verbose:
155185
print("Loading classifier...")
@@ -193,7 +223,6 @@ def main():
193223
outputSummary = {**summary, **info}
194224

195225
# Save output summary
196-
outputSummaryFile = f"{outdir}/{basename}-outputSummary.json"
197226
with open(outputSummaryFile, "w") as f:
198227
json.dump(outputSummary, f, indent=4, cls=NpEncoder)
199228

@@ -357,6 +386,8 @@ def default(self, obj):
357386
return float(obj)
358387
if isinstance(obj, np.ndarray):
359388
return obj.tolist()
389+
if pd.isnull(obj): # handles pandas NAType
390+
return np.nan
360391
return json.JSONEncoder.default(self, obj)
361392

362393

0 commit comments

Comments
 (0)