-
Notifications
You must be signed in to change notification settings - Fork 0
/
generatechartfile.py
89 lines (67 loc) · 2.1 KB
/
generatechartfile.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
import parameters as para
datasetname = '334111'
sourceDir = 'ChartOutput_'+datasetname
outputFile = 'combinedChart_'+datasetname+'.txt'
nameLookupFile = 'dataNew/all/'+datasetname+'.csv'
#nameLookupFile = 'data_/all/alldata.csv'
dataFilesDir = 'data_'+datasetname+'/'
nameMap = None
def createNameMap():
global nameMap
nameMap = {}
f = open(nameLookupFile)
headers = f.readline().split(',')
keyIndex = headers.index('COMNAM')
valueIndex = headers.index('PERMNO')
for line in f:
args = line.split(',')
key = args[keyIndex].replace(' ','_')
value = args[valueIndex]
if key not in nameMap:
nameMap[key] = value
f.close()
def translateName(name):
if nameMap == None:
createNameMap()
return nameMap[name]
# wow, I have to get this from alldata.csv, and data from para.
def toFileName(name):
return dataFilesDir + name + '.csv'
def translateDate(fileName, dayIndex):
para.caching = True
data, headers = para.readFile(fileName)
return data['Date'][int(dayIndex)]
def parseRow(rawRow):
cols = rawRow.split(',')
fileName = toFileName(cols[0])
newcols = [
translateName(cols[0]), #Permno
cols[0], #Comnam
translateDate(fileName, cols[1]), #Date
cols[2], #algo
cols[3], #groupSize
cols[4], #predictSize
]+cols[5:]
return ','.join(map(str,newcols))
def getHeaders(groupSize):
headers = ['PERMNO', 'COMNAM', 'DATE', 'ALGO', 'GROUPSIZE', 'PREDICTSIZE']
headers += map(lambda n : 'T' + str(n), range(0,groupSize))
return ','.join(headers)
def generateChartFile():
import os
rowFiles = os.listdir(sourceDir)
outputF = open(outputFile, 'w+')
outputF.write(getHeaders(240))
outputF.write('\n')
for rowFile in rowFiles:
f = open(sourceDir + '/' + rowFile)
rawRow = f.read()
f.close()
row = parseRow(rawRow)
outputF.write(row)
outputF.write('\n')
outputF.close()
def main():
generateChartFile()
if __name__ == '__main__':
main()