-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportprc.py
168 lines (157 loc) · 4.82 KB
/
importprc.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
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
163
164
165
166
167
168
##################################
#
# importprc.py
# Copyright (C) Louisiana State University, Health Sciences Center
# Written by 2011-2013 Ruben Tikidji-Hamburyan <[email protected]>
#
#
# 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 2 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, GOOD TITLE or
# NON INFRINGEMENT. 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, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###################################
import sys
import os
import csv
import string as str
import re
import glprc
from xml.sax import handler, make_parser
class prcparcerpxm(handler.ContentHandler):
def __init__(self,mode = "export", name = None):
self.prclst=[]
self.mode = mode
self.prc = None
self.prcfl = False
self.name = name
def startElement(self, name, attr):
if name =="prc":
if self.prcfl: return
if self.mode == "list" and attr.get("name", 0):
self.prclst.append(attr["name"])
return
if attr.get("name", 0) == 0 or self.name == None or attr["name"] != self.name:
return
self.prcfl = True
self.prc = glprc.odprc()
self.prc.name = attr["name"]
self.prc.gsyn = attr["gsyn"]
return
if name == "item" and self.prcfl:
if not attr.get("ph",0): return
if not attr.get("prc1",0): return
if attr.get("prc2",0):
self.prc.data.append([attr["ph"], str.split(attr["prc1"],','), str.split(attr["prc2"],',') ] )
self.prc.f1 = True
else:
self.prc.data.append([attr["ph"], str.split(attr["prc1"],','), [] ] )
self.prc.f2 = False
def endElement(self, name):
if name =="prc":
self.prcfl = False
class importprc:
def __init__(self):
self.name="..."
self.gsyn=None
self.data={}
def readxml(self,filename):
pparser = prcparcerpxm(name=self.name)
parser=make_parser()
parser.setContentHandler(pparser)
parser.parse(filename)
self.gsyn = pparser.prc.gsyn
for data in pparser.prc.data:
if not self.data.get(data[0], 0):
self.data[ data[0] ]=[ data[1],data[2] ]
def readfile(self,filename,format="excel-tab",fx="1"):
if format == "pxm" : return self.readxml(filename)
fd = open(filename, "r")
data = csv.reader(fd,dialect=format)
for row in data:
if len(row) == 0: continue
elif len(row) > 1:
nrow = row
else:
nrow = row[0].split()
# print row, " -> ", nrow
if not self.data.get(nrow[0], 0):
self.data[ nrow[0] ]=[ [],[] ]
if fx == '1':
self.data[ nrow[0] ][0].append(nrow[1])
else:
self.data[ nrow[0] ][1].append(nrow[1])
fd.close()
return
def close(self):
list=[]
for item in self.data.items():
if len(item[1][0]) != len(item[1][1]) and len(item[1][1]) != 0:
sys.stderr.write("Two different prc1 and prc2 at ph=%s\nAbbort\n\n"%item[0])
sys.exit(1)
for item in self.data.items():
list.append(item)
list.sort()
self.data=list
def write(self):
result=["<prc name=\"%s\" gsyn=\"%s\">"%(self.name, self.gsyn)]
for item in self.data:
if len(item[1][1]) == 0:
result.append("\t<item ph=\"%s\" prc1=\"%s\" />"%(
item[0], str.join(item[1][0]).replace(" ",",") )
)
else:
result.append("\t<item ph=\"%s\" prc1=\"%s\" prc2=\"%s\"/>"%(
item[0], str.join(item[1][0]).replace(" ",","),
str.join(item[1][1]).replace(" ",",") )
)
result.append("</prc>")
return result
def getodprc(self):
result = glprc.odprc()
result.name, result.gsyn = self.name, self.gsyn
for item in self.data.items():
result.data.append( [float(item[1][0]),[],[] ] )
returlt.data[-1][1] = map( float, item[1][1])
returlt.data[-1][2] = map( float, item[1][2])
result.data.sort()
return result
def getglprc(self):
result = glprc.odprc()
result.name, result.gsyn = self.name, self.gsyn
result.data = []
for item in self.data.items():
result.data.append( [item[0],item[1][0],item[1][1]] )
result.data.sort()
#print result.data
if result.data[0][2] != []:
result.f2 = True
return result
def clear(self):
del self.data
self.data = {}
if __name__ == '__main__':
if len(sys.argv) < 4:
print "USAGE: %s prc-name gsyn f1-datafile [f2-datafila]" % sys.argv[0]
sys.exit(1)
impprc = importprc()
impprc.name = sys.argv[1]
impprc.gsyn = sys.argv[2]
for fname in str.split(sys.argv[3]):
impprc.readfile(fname)
if len(sys.argv) > 4:
for fname in str.split(sys.argv[4]):
impprc.readfile(fname,fx='2')
impprc.close()
for i in impprc.write():
print "\t%s"%i