forked from WeGlove/CoCoRo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDistributor.py
153 lines (135 loc) · 5.18 KB
/
Distributor.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
import random
class Distributor:
PIMGRight = 0.5
PIMGWRONG = 0.5
PCATRIGHT = (2/3)
PCATWRONG = (1/3)
PARMRIGHT = 0.75
PARMWRONG = 0.25
def __init__(self):
self.array1 = list(range(3))
self.array2 = list(range(3,6))
self.array3 = list(range(6,9))
self.array4= list(range(9,12))
def blanceImages(self, arr, choice):
amtChoice = 0
amtNonChoice= 0
for item in arr:
if item == choice:
amtChoice += 1
else:
amtNonChoice += 1
ratioCh= amtChoice/len(arr)
ratioNC = amtNonChoice / len(arr)
while (ratioCh != self.PIMGRight or ratioNC != self.PIMGWRONG):
amtChoice = 0
amtNonChoice = 0
self.add1toImg(arr, choice,ratioCh)
for item in arr:
if item == choice:
amtChoice += 1
else:
amtNonChoice += 1
ratioCh = amtChoice / len(arr)
ratioNC = amtNonChoice / len(arr)
def add1toImg(self, arr, choice, raC):
if raC <= self.PIMGRight:
arr.append(choice)
else:
if choice < 3:
sel = list(range(3))
sel.remove(choice)
arr.append(random.choice(sel))
elif choice < 6:
sel = list(range(3, 6))
sel.remove(choice)
arr.append(random.choice(sel))
elif choice < 9:
sel = list(range(6, 9))
sel.remove(choice)
arr.append(random.choice(sel))
else:
sel = list(range(9, 12))
sel.remove(choice)
arr.append(random.choice(sel))
def balanceCategories (self, chosen, alt, choice):
raCA = len(chosen)/(len(chosen)+len(alt))
raNA = len(alt)/(len(chosen)+len(alt))
while (raCA != self.PCATRIGHT or raNA != self.PCATWRONG):
self.add1toCat(chosen,choice, alt,raCA )
raCA = len(chosen) / (len(chosen) + len(alt))
raNA = len(alt) / (len(chosen) + len(alt))
def add1toCat (self,chosen,choice, alt, raCA):
if raCA <= self.PCATRIGHT:
amtChoice = 0
for item in chosen:
if item == choice:
amtChoice += 1
ratioCh = amtChoice / len(chosen)
self.add1toImg(chosen, choice, ratioCh)
else:
add = random.choice(alt)
alt.append(add)
def balanceArms(self, chosen, choice, chosenalt, alt1, alt2):
chosenside = []
chosenside.append(chosen)
chosenside.append(chosenalt)
counterside = []
counterside.append(alt1)
counterside.append(alt2)
raAA = len(chosenside)/(len(chosenside)+len(counterside))
raNA = len(counterside)/(len(chosenside)+len(counterside))
while (raAA != self.PARMRIGHT or raNA != self.PARMWRONG):
self.add1toArm(chosen, choice, chosenalt, alt1, alt2, raAA)
chosenside = []
chosenside.extend(chosen)
chosenside.extend(chosenalt)
counterside = []
counterside.extend(alt1)
counterside.extend(alt2)
raAA = len(chosenside) / (len(chosenside) + len(counterside))
raNA = len(counterside) / (len(chosenside) + len(counterside))
def add1toArm(self, chosen, choice, chosenalt, alt1, alt2, raAA):
if (raAA <= self.PARMRIGHT):
raCA = len(chosen) / (len(chosen) + len(chosenalt))
self.add1toCat(chosen,choice, chosenalt, raCA)
else:
res = random.choice(list(range(2)))
if (res == 0):
add = random.choice(alt1)
alt1.append(add)
else:
add = random.choice(alt2)
alt2.append(add)
"""
distr = Distributor()
distr.blanceImages(distr.array1,1)
distr.balanceCategories(distr.array1, distr.array2,1)
distr.balanceArms(distr.array1, 1, distr.array2,distr.array3, distr.array4)
print ("Array 1: " + str(distr.array1) + "Array 2: " + str(distr.array2) + "Array 3: " + str(distr.array3) +"Array 4: " + str(distr.array4))
amtChoice = 0
for item in distr.array1:
if item == 1:
amtChoice += 1
ratioCh = amtChoice / len(distr.array1)
print("Ratio inside Images for chosen: " + str (ratioCh) + " Ratio inside Images against chosen " + str(1-ratioCh))
raCA = len(distr.array1) / (len(distr.array1) + len(distr.array2))
print("Ratio inside Categories for chosen: " + str(raCA)+ " Ratio inside Categories against chosen: " + str(1-raCA))
chosenside = []
chosenside.extend(distr.array1)
chosenside.extend(distr.array2)
counterside = []
counterside.extend(distr.array3)
counterside.extend(distr.array4)
raAA = len(chosenside) / (len(chosenside) + len(counterside))
print("Ratio in Arms for Chosen: " + str(raAA) + " Ratio in Arms against Chosen: " + str(1-raAA))
print ("Total Accuracy for Truth: "+ str(raAA*raCA*ratioCh))
chosenside.extend(counterside)
acc = [0] * 12
print(chosenside)
for val in chosenside:
acc[val] +=1
acc = [val / len(chosenside) for val in acc]
for i in range(len(acc)):
print(f"{i}: Accumulation: {acc[i]:.3f}")
"""