In [8]:
%pylab inline

import pandas as pd

import csv

from threshold_functions import *

from equivalent_ellipse import *

from scipy.interpolate import SmoothBivariateSpline, bisplrep


Populating the interactive namespace from numpy and matplotlib

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]:
array([  22.05796393,   12.97150852,   15.4480922 ,  114.41900685,
         12.86442621,   45.05604711,   50.89193108,   71.02505439,
         14.86260378,   13.73522834,   28.53415934,   14.52084192,
         12.97150852,   37.60018701,   90.45003316,   41.86943705,
         61.16413327,   38.94884243,   25.62485427,   59.84479106,
         45.05604711,   74.8128632 ,   15.69237109,   37.7737172 ,
         27.61627322,   24.57974805,   23.50689082,   14.32039921,
         74.81286289,   36.24210316,   33.0620745 ,   36.40251564,
         26.76336576,   99.75940399,   38.94884122,   97.73670756,
         23.50689059,   12.97151283,   49.15760775,   37.72170788,
         13.65532892,   26.3431725 ,   68.01266376,   25.74202726,
         29.5555355 ,   45.05605536,   23.50689193,   62.17904765,
         13.65533836,   42.73353425,   68.01268089,   57.81567984,
         12.72280297,   10.54700253,   17.49751532,   42.7726882 ,
         45.05605614,   13.65534948,   42.73353534,   91.67712739,
         17.13722801,   31.28382127,   12.72280323,   58.2988146 ,
         23.8047465 ,   21.36408199,   21.76698801,   45.0560484 ,
         35.09802512,   40.72582797,   15.36845615,   58.29881463,
         23.80475957,   50.84048514,   13.1216308 ,   72.43305875,
         34.44201164,   36.27642908,   51.90132721,   25.23974732,
         31.49707471,   96.83700661,   58.298817  ,   91.90442605,
         22.05796286,   15.64115911,   13.12162159,   30.83619322,
         36.27642908,   22.00066572,   58.2988139 ,   64.37534178,
         22.05796403,  102.9921899 ,   27.98233018,   82.16611229,
         30.83619352,   22.00069262,   31.86814726,   71.02505451,
         18.67888957,   35.97740025,   70.94418244])

In [18]:
curr_give[2] = 0.2
sum((curr_give/0.15)**15)


Out[18]:
75.407633101701038

In [19]:
curr_gap[2] = 100
sum((curr_gap/130)**30)


Out[19]:
0.023786428268844292

In [20]:
sum(w)


Out[20]:
116.0

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]:
(0, 1)

In [136]:
t = linspace(0,1,1000)
y = (2*exp(-((t-0.5)*2)**2) + 3*t - 1)/3
plot(t,y)


Out[136]:
[<matplotlib.lines.Line2D at 0x7f7e5f4f5be0>]

In [123]:
t = linspace(0,1,1000)
y = 30/(1 + exp(-(t-0.15)*100))
plot(t,y)
# ylim(0,10)


Out[123]:
[<matplotlib.lines.Line2D at 0x7f7e5f6dceb8>]

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


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-139-66886a57a2e3> in <module>()
      5 
      6 output = basinhopping(toMinimise,x0,
----> 7                       niter=1,minimizer_kwargs=minimizer_kwargs)
      8 
      9 output

/usr/local/lib/python3.4/dist-packages/scipy/optimize/_basinhopping.py in basinhopping(func, x0, niter, T, stepsize, minimizer_kwargs, take_step, accept_test, callback, interval, disp, niter_success)
    603 
    604     bh = BasinHoppingRunner(x0, wrapped_minimizer, take_step_wrapped,
--> 605                             accept_tests, disp=disp)
    606 
    607     # start main iteration loop

/usr/local/lib/python3.4/dist-packages/scipy/optimize/_basinhopping.py in __init__(self, x0, minimizer, step_taking, accept_tests, disp)
     70 
     71         # do initial minimization
---> 72         minres = minimizer(self.x)
     73         if not minres.success:
     74             self.res.minimization_failures += 1

/usr/local/lib/python3.4/dist-packages/scipy/optimize/_basinhopping.py in __call__(self, x0)
    277             return self.minimizer(x0, **self.kwargs)
    278         else:
--> 279             return self.minimizer(self.func, x0, **self.kwargs)
    280 
    281 

TypeError: minimize() got an unexpected keyword argument 'epsilon'

In [76]:
w_show = reshape(w,[4,29])
imshow(w_show, interpolation='nearest', cmap=cm.gray, vmin=0, vmax=1)
colorbar()


Out[76]:
<matplotlib.colorbar.Colorbar at 0x7f7e60054e10>