forked from s4hri/SMIRED500
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmi2csv.py
60 lines (56 loc) · 2.26 KB
/
smi2csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Copyright (C) 2019 Davide De Tommaso
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>
import sys
import argparse
import logging
import pandas as pd
logging.basicConfig(level=logging.INFO)
parser = argparse.ArgumentParser()
parser.add_argument('smifile_input', metavar='smifile_input', type=str, nargs='+',
help='The SMI text file containing name and path,')
parser.add_argument('smifile_output', metavar='smifile_output', type=str, nargs='+',
help='The SMI CSV file containing name and path,')
args = parser.parse_args()
logging.info('Loading SMI file from %s' % args.smifile_input[0])
data = {}
with open(args.smifile_input[0], 'r') as f:
headers = f.readline().rstrip().split(',')
headers.append('Event')
logging.info('Parsing data headers ...')
for item in headers:
logging.info(item)
data[item] = []
logging.info('Parsing data rows ...')
for row in f:
fields = row.rstrip().split(',')
for i in range(0, len(headers) - 1):
if len(fields) == len(headers) - 1:
data[headers[i]].append(fields[i])
else:
data[headers[i]].append(None)
i+=1
if fields[1] == 'MSG':
data['Time'].pop()
data['Time'].append(fields[0])
data['Type'].pop()
data['Type'].append(fields[1])
data['Trial'].pop()
data['Trial'].append(fields[2])
data['Event'].append(fields[3][11:])
else:
data['Event'].append(None)
logging.info('Exporting data in CSV file %s', args.smifile_output[0])
df = pd.DataFrame.from_dict(data)
df.to_csv(args.smifile_output[0])