-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData Extractor.py
More file actions
162 lines (130 loc) · 5.67 KB
/
Copy pathData Extractor.py
File metadata and controls
162 lines (130 loc) · 5.67 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
__author__ = 'Joshua Zosky'
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
PyQt4 code borrowed from:
ZetCode PyQt4 tutorial
In this example, we select a file with a
QtGui.QFileDialog and display its contents
in a QtGui.QTextEdit.
author: Jan Bodnar
website: zetcode.com
last edited: October 2011
"""
import csv
import os
import sys
from PyQt4 import QtGui
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.dict_header = ["segment #",
"_dbrain_18cluster_LORB",
"_dbrain_18cluster_RORB",
"_dbrain_18cluster_LP",
"_dbrain_18cluster_RP",
"_dbrain_18cluster_LIF",
"_dbrain_18cluster_RIF",
"_dbrain_18cluster_LPF",
"_dbrain_18cluster_RPF",
"_dbrain_18cluster_LT",
"_dbrain_18cluster_RT",
"_dbrain_18cluster_LIT",
"_dbrain_18cluster_RIT",
"_dbrain_18cluster_LTP",
"_dbrain_18cluster_RTP",
"_dbrain_18cluster_LO",
"_dbrain_18cluster_RO",
"_dbrain_18cluster_LIO",
"_dbrain_18cluster_RIO",
"delete me"]
self.dict_save_header = ["subject #",
"segment #",
"_dbrain_18cluster_LORB",
"_dbrain_18cluster_RORB",
"_dbrain_18cluster_LP",
"_dbrain_18cluster_RP",
"_dbrain_18cluster_LIF",
"_dbrain_18cluster_RIF",
"_dbrain_18cluster_LPF",
"_dbrain_18cluster_RPF",
"_dbrain_18cluster_LT",
"_dbrain_18cluster_RT",
"_dbrain_18cluster_LIT",
"_dbrain_18cluster_RIT",
"_dbrain_18cluster_LTP",
"_dbrain_18cluster_RTP",
"_dbrain_18cluster_LO",
"_dbrain_18cluster_RO",
"_dbrain_18cluster_LIO",
"_dbrain_18cluster_RIO"]
self.overall_data = []
self.initUI()
def initUI(self):
self.textEdit = QtGui.QTextEdit()
self.setCentralWidget(self.textEdit)
self.statusBar()
openFile = QtGui.QAction(QtGui.QIcon('open.png'), 'Open', self)
openFile.setShortcut('Ctrl+O')
openFile.setStatusTip('Open Analysis File(s)')
openFile.triggered.connect(self.showDialogOpen)
saveFile = QtGui.QAction(QtGui.QIcon('save.png'), 'Save', self)
saveFile.setShortcut('Ctrl+O')
saveFile.setStatusTip('Save Analysis File(s)')
saveFile.triggered.connect(self.showDialogSave)
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(openFile)
fileMenu.addAction(saveFile)
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('File dialog')
self.show()
def showDialogOpen(self):
fnames = QtGui.QFileDialog.getOpenFileNames(self, 'Open file(s)', '/home')
if fnames is None:
return
fileListing = ""
self.overall_data = []
for names in fnames:
names = '%s' % names
fileListing += names + "\n"
subject_number = self.get_subject_number((os.path.basename(names))[4:9])
self.overall_data.extend(self.extract_data(names, subject_number))
fileListing += "%s rows of data ready to save." % (len(self.overall_data))
self.textEdit.setText(fileListing)
def showDialogSave(self):
fname = QtGui.QFileDialog.getSaveFileName(self, 'Save file', filter='*.csv', selectedFilter='*.csv' '/home')
if fname is None:
return
with open(fname, 'w') as csvfile:
fieldnames = self.dict_save_header
dictwriter = csv.DictWriter(csvfile, fieldnames, lineterminator='\n')
dictwriter.writeheader()
dictwriter.writerows(self.overall_data)
def extract_data(self, file_names, subject_number):
return_data = []
with open(file_names, 'rb') as csvfile:
dictreader = csv.DictReader(csvfile, fieldnames=self.dict_header, delimiter='\t')
for rows in dictreader:
if rows['segment #'][0:7] == 'Segment':
rows = {key: value for key, value in rows.items() if key != 'delete me'}
rows['subject #'] = subject_number
return_data.append(rows)
return return_data
def get_subject_number(self, fname):
message = "Are you sure this is the correct subject number: %s?" % fname
reply = QtGui.QMessageBox.question(self,
'Message',
message,
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
return fname
else:
return '-9999'
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()