-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_pairs.py
225 lines (197 loc) · 9.63 KB
/
generate_pairs.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# -*- coding: utf-8 -*-
#
# @author: franlopezguzman
import numpy as np
import matplotlib.pyplot as plt
from os import mkdir
from os.path import exists
from scipy.spatial import ConvexHull
from math import *
#---------------------------------------------------------------------
def main():
# Define parameters
global numerositiesLeft, numerositiesRight, outData, outDir, imageFormat
global spacingLarger, spacingSmaller, sizeLarger, sizeSmaller
numerositiesLeft = [5,6,7,8,9,10,12,14]
numerositiesRight = [5,6,7,8,9,10,12,14]
spacingLarger = 1. / max(max(numerositiesLeft),max(numerositiesRight))
spacingSmaller = spacingLarger / 4
sizeLarger = .1
sizeSmaller = sizeLarger / 2
repetitions = 4
imageFormat = 'png'
#Create data array and directory
outDir, outData = new_data(numerositiesLeft, numerositiesRight, repetitions)
#Create figures for all sets
generate_pairs(numerositiesLeft,numerositiesRight, totalRepetitions=repetitions)
#Save data
save_data(outDir, outData)
#---------------------------------------------------------------------
def generate_pairs(numerositiesLeft,numerositiesRight,totalRepetitions=2, colorLeft='#FDFF00', colorRight='#0004FA'):
pair = -1
for numLeft in numerositiesLeft:
for numRight in numerositiesRight:
if (numLeft != numRight):
for repetition in range(totalRepetitions):
pair += 1
numMax = max(numLeft,numRight)
isCongruent = (repetition % 2 == 0) #alternating congruence
congruenceOK = False
congruenceIterations = 0
while not congruenceOK:
plt.close()
(fig, [axLeft,axRight]) = new_figure()
#Generate left image
occupiedAreaLeft = occupied_area(numLeft, numMax, isCongruent)
itemSizeMaxLeft, itemSizeMinLeft = item_size(numLeft, numMax, isCongruent, occupiedAreaLeft)
paintedAreaLeft, convexHullLeft = generate_set(
numLeft,occupiedAreaLeft,itemSizeMaxLeft,itemSizeMinLeft,colorLeft,axLeft)
#Generate right image
occupiedAreaRight = occupied_area(numRight, numMax, isCongruent)
itemSizeMaxRight, itemSizeMinRight = item_size(numRight, numMax, isCongruent, occupiedAreaRight)
paintedAreaRight, convexHullRight = generate_set(
numRight,occupiedAreaRight,itemSizeMaxRight,itemSizeMinRight,colorRight, axRight)
#Check if congruence is respected
congruenceOK = check_congruence(numLeft, paintedAreaLeft, convexHullLeft,
numRight, paintedAreaRight, convexHullRight, isCongruent)
congruenceIterations += 1
if (congruenceIterations == 100):
print('Error: congruence not respected for pair %02d %02d repetition %1d ' % (numLeft, numRight, repetition//2))
congruenceOK = True
#Save figure
fig.set_size_inches(18.5, 10.5)
plt.savefig(outDir + '%02d' % numLeft + '%02d' % numRight +
'_Cong' + str(int(isCongruent)) + '_Rep' + str(repetition//2) +
'.' + imageFormat, format=imageFormat)
plt.close()
#Save data
outData[pair,0] = numLeft
outData[pair,1] = numRight
outData[pair,2] = max(numLeft,numRight) / min(numLeft,numRight) #Ratio
outData[pair,3] = isCongruent
outData[pair,4] = paintedAreaLeft
outData[pair,5] = paintedAreaRight
outData[pair,6] = convexHullLeft
outData[pair,7] = convexHullRight
#---------------------------------------------------------------------
def generate_set(numerosity,occupiedArea=1,itemSizeMax=.1,itemSizeMin=.025,color='#0004FA', ax='None'):
#Create a vector with the radii of all the dots
radii = itemSizeMin + (itemSizeMax-itemSizeMin) * np.random.rand(numerosity,1)
radii = -np.sort(-radii, axis=0)
#Create an array for the coordinates
coords = np.zeros([numerosity,2])
#Create a figure
if (ax == 'None'):
fig, ax = new_figure()
#Define the coordinates for the dots so that they fall inside the occupied are and don't supperpose
for i in range(numerosity):
available = False
plotError = False
setIterations = 0
while (not available):
coords[i,0] = np.random.rand(1)
coords[i,1] = np.random.rand(1)
available = is_available(i,radii,coords,occupiedArea)
setIterations += 1
if setIterations == 1000:
plotError = True
break
if (not plotError):
circle = plt.Circle((coords[i,0],coords[i,1]), radii[i],color=color)
ax.add_artist(circle)
else:
circle = plt.Circle((coords[i,0],coords[i,1]), radii[i],color='white')
ax.add_artist(circle)
break
#Calculate convex hull:
convexHull = ConvexHull(coords)
#Calculate painted area:
paintedArea = pi * np.sum(radii**2)
return (paintedArea, convexHull.volume)
#---------------------------------------------------------------------
def occupied_area(num, numMax, isCongruent=1):
if isCongruent:
if (num == numMax):
occupiedArea = sqrt(spacingLarger * num)
else:
occupiedArea = sqrt(spacingSmaller * num)
else:
if (num == numMax):
occupiedArea = sqrt(spacingSmaller * num)
else:
occupiedArea = sqrt(spacingLarger * num)
return occupiedArea
#---------------------------------------------------------------------
def item_size(num, numMax, isCongruent=1, occupiedArea=1):
if isCongruent:
if (num == numMax):
itemSizeMax = sqrt(sizeLarger/num)
else:
itemSizeMax = sqrt(sizeSmaller/num)
else:
if (num == numMax):
itemSizeMax = sqrt(sizeSmaller/num)
else:
itemSizeMax = sqrt(sizeLarger/num)
itemSizeMin = itemSizeMax / 2
return (itemSizeMax, itemSizeMin)
#---------------------------------------------------------------------
def is_available(i,radii,coords,occupiedArea=1):
posX = ((coords[i,0])-radii[i]>.05) and (coords[i,0]+radii[i]<.95)
posY = ((coords[i,1])-radii[i]>.05) and (coords[i,1]+radii[i]<.95)
available = posX and posY #Avoid dots on borders
if (not available):
return available
for j in range(i):
distDots = sqrt((coords[i,0]-coords[j,0])**2 + (coords[i,1]-coords[j,1])**2)
position = distDots > (radii[i]+radii[j])*1.1 #Avoid superposition
area = distDots < sqrt(occupiedArea) #Dots fall inside occupied area
available = available and position and area
if (not available):
break
return available
#---------------------------------------------------------------------
def check_congruence(numLeft, paintedAreaLeft, convexHullLeft,
numRight, paintedAreaRight, convexHullRight, isCongruent):
numMax = max(numLeft, numRight)
if isCongruent:
if (numLeft == numMax):
return (paintedAreaLeft > paintedAreaRight) and (convexHullLeft > convexHullRight)
else:
return (paintedAreaRight > paintedAreaLeft) and (convexHullRight > convexHullLeft)
else:
if (numLeft == numMax):
return (paintedAreaRight > paintedAreaLeft) and (convexHullRight > convexHullLeft)
else:
return (paintedAreaLeft > paintedAreaRight) and (convexHullLeft > convexHullRight)
#---------------------------------------------------------------------
def new_figure():
(fig, [ax1,ax2]) = plt.subplots(1,2)
ax1.set_xlim([0,1]); ax1.set_ylim([0,1])
ax1.set_aspect('equal'); ax1.set_facecolor('#BEBDC2')
ax1.set_xticks([]); ax1.set_yticks([])
ax1.spines['top'].set_linewidth(-10);
ax1.spines['right'].set_linewidth(-10)
ax1.spines['bottom'].set_linewidth(-10)
ax1.spines['left'].set_linewidth(-10)
ax2.set_xlim([0,1]); ax2.set_ylim([0,1])
ax2.set_aspect('equal')
ax2.set_facecolor('#BEBDC2')
ax2.set_xticks([]); ax2.set_yticks([])
ax2.spines['top'].set_linewidth(-10)
ax2.spines['right'].set_linewidth(-10)
ax2.spines['bottom'].set_linewidth(-10)
ax2.spines['left'].set_linewidth(-10)
return (fig, [ax1,ax2])
#---------------------------------------------------------------------
def new_data(numerositiesLeft, numerositiesRight, repetitions, outDir='Figuras/'):
outData = np.zeros([len(numerositiesLeft)*len(numerositiesRight)*repetitions, 8])
if not exists(outDir):
mkdir(outDir)
return outDir, outData
#---------------------------------------------------------------------
def save_data(outDir, outData):
header = 'numLeft,numRight,ratio,isCongruent,paintedAreaLeft,paintedAreaRight,convexHullLeft,convexHullRight'
np.savetxt(outDir+'data.csv', outData, header=header, fmt='%.3f', delimiter=',')
#---------------------------------------------------------------------
if __name__=='__main__': main()