Skip to content

Commit 53893c3

Browse files
committed
add analysis cli
1 parent 5c8eb7d commit 53893c3

File tree

4 files changed

+152
-4
lines changed

4 files changed

+152
-4
lines changed

.github/ISSUE_TEMPLATE/Bug_report.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@ What happened instead.
1616

1717
## Reproduction Steps
1818

19-
Steps to reproduce the bug
19+
```python
20+
import peeper
21+
22+
```
2023

2124
## System Information
2225

23-
OS type, model ...
26+
$ python -m peeper.help
27+
28+
```
29+
<paste here>
30+
```

peeper/analysis/cli.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""Command line interface"""
4+
5+
import argparse
6+
import os
7+
8+
from hal.files.models.files import Document
9+
from hal.streams.logger import log_message
10+
11+
from peeper.analysis.models import Plotter
12+
13+
14+
def create_args():
15+
"""
16+
:return: ArgumentParser
17+
Parser that handles cmd arguments.
18+
"""
19+
20+
parser = argparse.ArgumentParser(usage='-i <input file> '
21+
'-h for full usage')
22+
parser.add_argument('-i', dest='file',
23+
help='input file', required=True)
24+
return parser
25+
26+
27+
def parse_args(parser):
28+
"""
29+
:param parser: ArgumentParser
30+
Object that holds cmd arguments.
31+
:return: tuple
32+
Values of arguments.
33+
"""
34+
35+
args = parser.parse_args()
36+
file = str(args.file)
37+
38+
assert os.path.exists(file)
39+
assert Document(file).extension == ".csv"
40+
41+
return file
42+
43+
44+
def main():
45+
file = parse_args(create_args())
46+
log_message("Using file", file)
47+
48+
output_file = "plot.png"
49+
output_file = os.path.join(os.path.dirname(file), output_file)
50+
51+
if os.path.exists(output_file): # remove any previous outputs
52+
os.remove(output_file)
53+
54+
driver = Plotter(file)
55+
driver.save(output_file)
56+
57+
log_message("Plot saved to", output_file)
58+
59+
60+
if __name__ == '__main__':
61+
main()

peeper/analysis/models.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""Module containing analysis models"""
4+
5+
import pandas as pd
6+
from hal.files.models.files import Document
7+
from hal.files.models.system import ls_dir
8+
9+
10+
class Plotter:
11+
"""Plots data in file"""
12+
13+
def __init__(self, file):
14+
"""
15+
:param file: folder where there are the input files
16+
"""
17+
18+
self.path = file
19+
self.data = {
20+
file: pd.read_csv(file)
21+
for file in self._find_files()
22+
} # dictionary file name -> file data (as pandas data frame)
23+
24+
def _find_files(self):
25+
"""Finds files in folder
26+
27+
:return: list of input files in folder
28+
"""
29+
30+
files = ls_dir(self.path)
31+
files = [
32+
file
33+
for file in files
34+
if Document(file).extension == ".csv" # just csv files
35+
]
36+
return files
37+
38+
def _merge(self):
39+
"""Merges data frames into one big
40+
41+
:return: one big data frame with data from all input files
42+
"""
43+
44+
dfs = [] # list of data frames
45+
for file, data in self.data.items():
46+
file_name = Document(file).name
47+
data = data.drop(["Timestamp"], axis=1) # remove column
48+
data = data.set_index("Milliseconds") # set index
49+
50+
# rename columns
51+
new_columns = {
52+
col: file_name + " " + col
53+
for col in data.keys()
54+
}
55+
data = data.rename(index=str, columns=new_columns) # rename columns
56+
57+
dfs.append(data)
58+
59+
data = pd.concat(dfs, axis=1, sort=True) # merge
60+
61+
# rename rows (convert to float)
62+
new_rows = {
63+
row: float(row)
64+
for row in data.index
65+
}
66+
data = data.rename(index=new_rows)
67+
data = data.sort_index() # sort by index
68+
69+
return data
70+
71+
def save(self, output_file):
72+
"""Merges all inputs files into one
73+
74+
:param output_file: output file (where to write data)
75+
"""
76+
77+
data = self._merge()
78+
data = sample_by_frequency(data, 5)
79+
data.to_csv(output_file, index_label="Milliseconds")

peeper/preprocessing/cli.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
"""Command line interface for pre-processing models"""
3+
"""Command line interface"""
44

55
import argparse
66
import os
@@ -33,7 +33,8 @@ def parse_args(parser):
3333

3434
args = parser.parse_args()
3535
directory = str(args.dir)
36-
assert (os.path.exists(directory))
36+
37+
assert os.path.exists(directory)
3738

3839
return directory
3940

0 commit comments

Comments
 (0)