In [8]:
%pylab inline
import pandas as pd
import csv
from threshold_functions import *
from equivalent_ellipse import *
from scipy.interpolate import SmoothBivariateSpline, bisplrep
In [9]:
x_list = list()
y_list = list()
with open('../data/cutout_x.csv', 'r') as x_csvfile:
with open('../data/cutout_y.csv', 'r') as y_csvfile:
x_reader = csv.reader(x_csvfile, delimiter=',', lineterminator='\n')
y_reader = csv.reader(y_csvfile, delimiter=',', lineterminator='\n')
for row in x_reader:
x_list += [row]
for row in y_reader:
y_list += [row]
In [10]:
num_cutouts = len(x_list)
In [11]:
x_array = [0,]*num_cutouts
y_array = [0,]*num_cutouts
for i in range(num_cutouts):
x_array[i] = array(x_list[i], dtype='float')
y_array[i] = array(y_list[i], dtype='float')
In [12]:
cutout = [0,]*num_cutouts
for i in range(num_cutouts):
cutout[i] = shapely_cutout(x_array[i],y_array[i])
In [13]:
cutout_dimensions = pd.DataFrame.from_csv('../data/cutout_dimensions.csv')
#cutout_dimensions
In [14]:
boundBox = cutout_dimensions['box_bounds'].values
#ref = boundBox < 11
width = cutout_dimensions['width'].values
length = cutout_dimensions['length'].values
cutoutRef = arange(num_cutouts)
ratio = width/length
In [15]:
# plot(append(width,width),append(log2(ratio),log2(1/ratio)),'.')
In [16]:
def give_calc(xGrid,yGrid,width,ratio,w):
w = w/max(w)
ref = (w == 0)
width = delete(width,find(ref))
ratio = delete(ratio,find(ref))
w = delete(w,find(ref))
return fit_give(xGrid,log2(yGrid),
append(width,width),
append(log2(ratio),log2(1/ratio)),
zeros(len(width)*2),append(w,w))
def gap_calc(xGrid,yGrid,width,ratio):
return angle_gap(xGrid,log2(yGrid),append(width,width),
append(log2(ratio),log2(1/ratio)),1,2)
In [17]:
w = ones(len(width))
valid = (give_calc(width,ratio,width,ratio,w) < 0.15) & (gap_calc(width,ratio,width,ratio) < 130)
curr_give = give_calc(width[valid],ratio[valid],width,ratio,w)
curr_gap = gap_calc(width[valid],ratio[valid],width,ratio)
curr_gap
Out[17]:
In [18]:
curr_give[2] = 0.2
sum((curr_give/0.15)**15)
Out[18]:
In [19]:
curr_gap[2] = 100
sum((curr_gap/130)**30)
Out[19]:
In [20]:
sum(w)
Out[20]:
In [137]:
global keeping_track
def toMinimise(w):
global keeping_track
if (sum(w) > 13):
curr_give = give_calc(width[valid],ratio[valid],width,ratio,w)
else:
curr_give = ones(len(w))
#curr_gap = gap_calc(width[valid],ratio[valid],width,ratio)
weights_bias = sum((2*exp(-((w-0.5)*2)**2) + 3*w - 1)/3)
too_few_bias = exp(-(sum(w)-15))
give_bias = sum(30/(1 + exp(-(curr_give-0.15)*100)))
output = weights_bias + give_bias + too_few_bias
if keeping_track > 20:
print("Output %.4f, weight %.4f, too few %.4f, give %.4f" % (output, weights_bias, too_few_bias, give_bias))
w_show = reshape(w,[4,29])
imshow(w_show, interpolation='nearest', cmap=cm.gray, vmin=0, vmax=1)
show()
keeping_track = 0
else:
keeping_track += 1
return output
In [22]:
t = linspace(0,20,1000)
y = exp(-(t-13))
plot(t,y)
ylim([0,1])
Out[22]:
In [136]:
t = linspace(0,1,1000)
y = (2*exp(-((t-0.5)*2)**2) + 3*t - 1)/3
plot(t,y)
Out[136]:
In [123]:
t = linspace(0,1,1000)
y = 30/(1 + exp(-(t-0.15)*100))
plot(t,y)
# ylim(0,10)
Out[123]:
In [23]:
bounds = ([0,1],)*len(width)
In [139]:
keeping_track = 0
minimizer_kwargs = {"tol":0.1,"method": 'SLSQP', "bounds": bounds }
x0 = ones(len(width))
output = basinhopping(toMinimise,x0,
niter=1,minimizer_kwargs=minimizer_kwargs)
output
In [76]:
w_show = reshape(w,[4,29])
imshow(w_show, interpolation='nearest', cmap=cm.gray, vmin=0, vmax=1)
colorbar()
Out[76]: