|
9 | 9 | import numpy as np |
10 | 10 | import pandas as pd |
11 | 11 | import joblib |
| 12 | +import sys |
12 | 13 |
|
13 | 14 | import actipy |
14 | 15 |
|
@@ -148,8 +149,37 @@ def main(): |
148 | 149 | # Output paths |
149 | 150 | basename = resolve_path(args.filepath)[1] |
150 | 151 | outdir = os.path.join(args.outdir, basename) |
| 152 | + outputSummaryFile = f"{outdir}/{basename}-outputSummary.json" |
151 | 153 | os.makedirs(outdir, exist_ok=True) |
152 | 154 |
|
| 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 | + |
153 | 183 | # Run classifier |
154 | 184 | if verbose: |
155 | 185 | print("Loading classifier...") |
@@ -193,7 +223,6 @@ def main(): |
193 | 223 | outputSummary = {**summary, **info} |
194 | 224 |
|
195 | 225 | # Save output summary |
196 | | - outputSummaryFile = f"{outdir}/{basename}-outputSummary.json" |
197 | 226 | with open(outputSummaryFile, "w") as f: |
198 | 227 | json.dump(outputSummary, f, indent=4, cls=NpEncoder) |
199 | 228 |
|
@@ -357,6 +386,8 @@ def default(self, obj): |
357 | 386 | return float(obj) |
358 | 387 | if isinstance(obj, np.ndarray): |
359 | 388 | return obj.tolist() |
| 389 | + if pd.isnull(obj): # handles pandas NAType |
| 390 | + return np.nan |
360 | 391 | return json.JSONEncoder.default(self, obj) |
361 | 392 |
|
362 | 393 |
|
|
0 commit comments