Skip to content

Commit 2e75903

Browse files
author
“Aidan
committed
Time format for reading csv file
1 parent 5f8ab5a commit 2e75903

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

docs/source/usage.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,24 @@ A template can be downloaded as follows:
5656
5757
If the CSV file has a different header, use the option --txyz to specify the time and x-y-z columns, in that order.
5858
The --csvStartRow option can be used to specify the first row of data in the CSV file, starting from the header row.
59+
The --csvDateFormat option can be used to specify the date format of the time column.
60+
Further information on valid date format strings can be found in the `Python datetime documentation <https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes>`_.
5961

6062
For example:
6163
.. code-block:: console
6264
SAMPLE CSV FILE
6365

6466
HEADER_TIMESTAMP,temperature,X,Y,Z
65-
2013-10-21 10:00:08.000,26.3,-0.078923,0.396706,0.917759
66-
2013-10-21 10:00:08.010,26.2,-0.094370,0.381479,0.933580
67-
2013-10-21 10:00:08.020,26.2,-0.094370,0.366252,0.901938
68-
2013-10-21 10:00:08.030,26.2,-0.078923,0.411933,0.901938
67+
21/10/2013 10:00:08.000,26.3,-0.078923,0.396706,0.917759
68+
21/10/2013 10:00:08.010,26.2,-0.094370,0.381479,0.933580
69+
21/10/2013 10:00:08.020,26.2,-0.094370,0.366252,0.901938
70+
21/10/2013 10:00:08.030,26.2,-0.078923,0.411933,0.901938
6971
...
7072

7173
Then run the command as follows:
7274
.. code-block:: console
7375
74-
$ actinet data/sample.csv.gz --txyz HEADER_TIMESTAMP,X,Y,Z --csvStartRow 3
76+
$ actinet data/sample.csv.gz --txyz HEADER_TIMESTAMP,X,Y,Z --csvStartRow 3 --csvDateFormat "%d/%m/%Y %H:%M:%S.%f"
7577
7678
Other accelerometer file formats
7779
--------------------------------

src/actinet/actinet.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626

2727
def main():
28-
2928
parser = argparse.ArgumentParser(
3029
description="A tool to predict activities from accelerometer data using a self-supervised ResNet-18 model",
3130
add_help=True,
@@ -45,6 +44,11 @@ def main():
4544
help="Enter custom acitivty classifier file to use",
4645
default=None,
4746
)
47+
parser.add_argument(
48+
"--no-hmm",
49+
action="store_true",
50+
help="Disable HMM post-processing",
51+
)
4852
parser.add_argument(
4953
"--force-download",
5054
action="store_true",
@@ -80,7 +84,7 @@ def main():
8084
default=None)
8185
parser.add_argument(
8286
"--csvStartRow",
83-
help="Row number to start reading a CSV file. Default: 0",
87+
help="Row number to start reading a CSV file. Default: 1 (First row)",
8488
type=int,
8589
default=1,
8690
)
@@ -89,7 +93,15 @@ def main():
8993
"in the input file, in that order. Use a comma-separated string. "
9094
"Only needed for CSV files, can be ignored for other file types. "
9195
"Default: 'time,x,y,z'"),
92-
type=str, default="time,x,y,z")
96+
type=str, default="time,x,y,z"
97+
)
98+
parser.add_argument('--csvDateFormat',
99+
default="%Y-%m-%d %H:%M:%S.%f",
100+
type=str, help=("Date time format for csv file ",
101+
"when reading a csv file. Default: ,",
102+
"'%Y-%m-%d %H:%M:%S.%f'. "
103+
"See https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes for more possible codes.")
104+
)
93105
parser.add_argument(
94106
"--plot-activity",
95107
"-p",
@@ -143,6 +155,7 @@ def main():
143155
args.filepath,
144156
args.txyz,
145157
args.csvStartRow-1, # -1 to convert to zero-based index
158+
args.csvDateFormat,
146159
resample_hz=None,
147160
sample_rate=args.sample_rate,
148161
verbose=verbose,
@@ -212,7 +225,7 @@ def main():
212225

213226
if verbose:
214227
print("Running activity classifier...")
215-
Y = classifier.predict_from_frame(data, args.sample_rate)
228+
Y = classifier.predict_from_frame(data, args.sample_rate, not args.no_hmm)
216229

217230
# Save predicted activities
218231
timeSeriesFile = f"{outdir}/{basename}-timeSeries.csv.gz"
@@ -272,10 +285,9 @@ def main():
272285

273286

274287
def read(
275-
filepath, usecols, skipRows=0, resample_hz="uniform",
276-
sample_rate=None, lowpass_hz=None, verbose=True
288+
filepath, usecols, skipRows=0, dateFormat=None,
289+
resample_hz="uniform", sample_rate=None, lowpass_hz=None, verbose=True
277290
):
278-
279291
p = pathlib.Path(filepath)
280292
ftype = p.suffixes[0].lower()
281293
fsize = round(p.stat().st_size / (1024 * 1024), 1)
@@ -288,6 +300,7 @@ def read(
288300
filepath,
289301
usecols=[tcol, xcol, ycol, zcol],
290302
parse_dates=[tcol],
303+
date_format=dateFormat,
291304
index_col=tcol,
292305
dtype={xcol: "f4", ycol: "f4", zcol: "f4"},
293306
skiprows=skipRows,

0 commit comments

Comments
 (0)