-
Notifications
You must be signed in to change notification settings - Fork 0
/
joes_dbinner.py
executable file
·118 lines (105 loc) · 3.99 KB
/
joes_dbinner.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
from pylab import *
import csv
from jmoo_problems import *
from jmoo_algorithms import *
from jmoo_properties import *
import random, utility
from time import *
import os
class histobin:
def __init__(self, numBins, decision):
self.numBins = numBins
self.bins = []
self.decision = decision
spread = decision.up - decision.low
step = spread/float(numBins)
self.bins = [bin(i, decision.low + step*i, decision.low + step*(i+1)) for i in range(numBins)]
def getTotal(self):
total = 0
for bin in self.bins:
total += bin.count
return total
def __repr__(self):
s = self.decision.name
total = self.getTotal()
if total == 0: return str([0 for bin in self.bins])
for bin in self.bins:
s += str("%3.0f" % (100.0*bin.count/float(total))) + ","
s += str("%5.2f" % self.decision.low) + "," + str("%5.2f" % ((self.decision.up-self.decision.low)/float(self.numBins)))
#s += "]"
return s
def populate(self, data):
for item in data:
for b,bin in enumerate(self.bins):
if b == 0:
if item >= bin.low and item < bin.up:
self.bins[b].add(item)
break
elif b == (len(self.bins)-1):
if item > bin.low and item <= bin.up:
self.bins[b].add(item)
break
else:
if item > bin.low and item < bin.up:
self.bins[b].add(item)
break
def sort(self, bywhat=None):
if bywhat == "count": bywhat = lambda bin: bin.count
else: bywhat = lambda bin: bin.index
self.bins = sorted(self.bins, key=bywhat)
class bin:
def __init__(self, ind, low, up):
self.index = ind
self.low = low
self.up = up
self.count = 0
self.items = []
def add(self, item):
self.count += 1
self.items.append(item)
decision_data = []
objective_data = []
for p,problem in enumerate(problems):
decision_data.append([])
objective_data.append([])
for a,algorithm in enumerate(algorithms):
print "" + algorithm.name + "" + problem.name
finput = open(DATA_PREFIX + DECISION_BIN_TABLE + "_" + problem.name + "_" + algorithm.name + DATA_SUFFIX, 'rb')
reader = csv.reader(finput, delimiter=',')
decision_data[p].append([])
objective_data[p].append([])
for i,row in enumerate(reader):
if i == 0:
objective_data[p][a] = [[] for obj in problem.objectives]
decision_data[p][a] = [[] for dec in problem.decisions]
elements = row
for d,dec in enumerate(problem.decisions):
decision_data[p][a][d].append(float(elements[d]))
d = len(problem.decisions)
for o,obj in enumerate(problem.objectives):
if not elements[d+o].strip() == "?": objective_data[p][a][o].append(float(elements[d+o]))
# histogram
recommendations = []
lows = []
ups = []
ranges = []
numBins = 10
columns = decision_data[p][a]
histobins = [histobin(numBins, problem.decisions[i]) for i in range(len(problem.decisions))]
for c,hbin in enumerate(histobins):
hbin.populate(columns[c])
#hbin.sort("count")
#print hbin
hbin.sort()
print hbin
"""
for c,col in enumerate(decision_data[p][a]):
histo,bin = numpy.histogram(col, bins=10, range=[problem.decisions[c].low, problem.decisions[c].up])
bins = []
total = float(sum(histo))
s = ""
for h in histo:
bins.append( ((h/total)*100) )
s += str( ("%4.0f" % bins[-1] ) ) + ","
print s
"""