In [1]:
%pylab inline

import pandas as pd

import csv

import os

import shapely.affinity as af
import shapely.geometry as sh

from equivalent_ellipse import *

from scaled_figures import *

from glob import glob

pylab.rcParams['savefig.dpi'] = 254


Populating the interactive namespace from numpy and matplotlib

In [2]:
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 [3]:
num_cutouts = len(x_list)

In [4]:
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 [5]:
cutout = [0,]*num_cutouts

for i in range(num_cutouts):
    cutout[i] = shapely_cutout(x_array[i],y_array[i])

In [6]:
box_bounds = zeros([num_cutouts])

for i in range(num_cutouts):
    box_bounds[i] = max(
        array(
            [cutout[i].bounds[2]-cutout[i].bounds[0],
             cutout[i].bounds[3]-cutout[i].bounds[1]]
            )
        )

In [7]:
cutout_choice = 35

if (box_bounds[cutout_choice]>10):
    scaleToFit = True
else:
    scaleToFit = False

In [8]:
if scaleToFit:
    cutout[cutout_choice] = af.scale(
        cutout[cutout_choice],
        xfact=10/box_bounds[cutout_choice],
        yfact=10/box_bounds[cutout_choice])

In [11]:
weights = array([1,3,5,10,50])

ellipse_results = [0,]*len(weights)

#for i in range(num_cutouts):
for i in range(len(weights)):
    print("Weight %.3f" % (weights[i]))
    ellipse_results[i] = eq_ellipse_calc(cutout[cutout_choice],weights[i],5)
    print(" ")


Weight 1.000
first local minima of 5.0511 at:
[a,b] = [0.8065, 0.2928], [width, length] = [5.9509, 9.6422], theta = 146.4020

agreeing local minima of 5.0511 at:
[a,b] = [0.8065, 0.2928], [width, length] = [5.9509, 9.6422], theta = 146.4020

agreeing local minima of 5.0511 at:
[a,b] = [0.8065, 0.2928], [width, length] = [5.9509, 9.6422], theta = 146.4020

agreeing local minima of 5.0511 at:
[a,b] = [0.8065, 0.2928], [width, length] = [5.9509, 9.6422], theta = 146.4020

agreeing local minima of 5.0511 at:
[a,b] = [0.8065, 0.2928], [width, length] = [5.9509, 9.6422], theta = 146.4020

 
Weight 3.000
first local minima of 8.8930 at:
[a,b] = [0.8944, 0.3417], [width, length] = [5.2211, 9.4577], theta = 147.3707

failed to find local minima at:
[a,b] = [-7.4699, -0.9862], [width, length] = [0.0000, 3.5888], theta = 159.6052

failed to find local minima at:
[a,b] = [4.1921, 5.4780], [width, length] = [0.0000, 1.4760], theta = 170.8461

agreeing local minima of 8.8930 at:
[a,b] = [0.8944, 0.3417], [width, length] = [5.2211, 9.4577], theta = 147.3707

agreeing local minima of 8.8930 at:
[a,b] = [0.8944, 0.3417], [width, length] = [5.2211, 9.4577], theta = 147.3707

failed to find local minima at:
[a,b] = [-14.5683, 3.2441], [width, length] = [0.0000, 0.2988], theta = 94.7223

agreeing local minima of 8.8930 at:
[a,b] = [0.8944, 0.3417], [width, length] = [5.2211, 9.4577], theta = 147.3707

failed to find local minima at:
[a,b] = [15.3493, 15.0760], [width, length] = [0.0000, 12.1204], theta = 51.6981

agreeing local minima of 8.8930 at:
[a,b] = [0.8944, 0.3417], [width, length] = [5.2211, 9.4577], theta = 147.3707

 
Weight 5.000
first local minima of 10.3398 at:
[a,b] = [0.8901, 0.3619], [width, length] = [4.9044, 9.4073], theta = 147.8234

failed to find local minima at:
[a,b] = [10.5532, 0.2416], [width, length] = [0.0000, 9.5400], theta = 136.2681

agreeing local minima of 10.3398 at:
[a,b] = [0.8901, 0.3619], [width, length] = [4.9044, 9.4073], theta = 147.8233

agreeing local minima of 10.3398 at:
[a,b] = [0.8901, 0.3619], [width, length] = [4.9044, 9.4073], theta = 147.8234

failed to find local minima at:
[a,b] = [-7.4327, -3.3386], [width, length] = [0.0000, 0.6304], theta = 88.4437

failed to find local minima at:
[a,b] = [-7.2712, -2.6960], [width, length] = [0.0000, 0.5275], theta = 11.9389

agreeing local minima of 10.3398 at:
[a,b] = [0.8901, 0.3619], [width, length] = [4.9044, 9.4073], theta = 147.8233

agreeing local minima of 10.3398 at:
[a,b] = [0.8901, 0.3619], [width, length] = [4.9044, 9.4073], theta = 147.8234

 
Weight 10.000
first local minima of 19.0679 at:
[a,b] = [-0.1463, -1.1066], [width, length] = [5.2642, 5.9825], theta = 110.2441

new local minima of 11.6769 at:
[a,b] = [0.9352, 0.3445], [width, length] = [4.5450, 9.3787], theta = 149.0949

failed to find local minima at:
[a,b] = [-4.9116, 2.6847], [width, length] = [0.0000, 10.7068], theta = 158.7240

agreeing local minima of 11.6769 at:
[a,b] = [0.9352, 0.3445], [width, length] = [4.5450, 9.3787], theta = 149.0949

agreeing local minima of 11.6769 at:
[a,b] = [0.9352, 0.3445], [width, length] = [4.5450, 9.3787], theta = 149.0949

agreeing local minima of 11.6769 at:
[a,b] = [0.9352, 0.3445], [width, length] = [4.5450, 9.3787], theta = 149.0949

failed to find local minima at:
[a,b] = [17.9957, -19.2983], [width, length] = [0.0000, 0.6723], theta = 134.9647

agreeing local minima of 11.6769 at:
[a,b] = [0.9352, 0.3445], [width, length] = [4.5450, 9.3787], theta = 149.0949

 
Weight 50.000
failed to find local minima at:
[a,b] = [5.5599, 6.4542], [width, length] = [0.0000, 3.0801], theta = 83.9601

first local minima of 12.9303 at:
[a,b] = [0.9886, 0.3194], [width, length] = [4.2132, 9.3614], theta = 150.3361

failed to find local minima at:
[a,b] = [-11.4768, 4.0273], [width, length] = [0.0000, 4.6195], theta = 125.8273

failed to find local minima at:
[a,b] = [0.9873, -0.5049], [width, length] = [0.0000, 9.0439], theta = 110.3220

failed to find local minima at:
[a,b] = [-18.8849, 27.7087], [width, length] = [0.0000, 12.5479], theta = 30.5780

failed to find local minima at:
[a,b] = [-4.9900, -9.9322], [width, length] = [0.0000, 6.0099], theta = 23.7593

failed to find local minima at:
[a,b] = [-3.7425, 8.4920], [width, length] = [0.0000, 5.1248], theta = 118.1742

failed to find local minima at:
[a,b] = [-23.1795, 18.2017], [width, length] = [0.0000, 10.2818], theta = 29.7758

new local minima of 12.9233 at:
[a,b] = [0.9809, 0.3096], [width, length] = [4.2145, 9.3595], theta = 148.2645

agreeing local minima of 12.9233 at:
[a,b] = [0.9809, 0.3096], [width, length] = [4.2145, 9.3595], theta = 148.2645

failed to find local minima at:
[a,b] = [-1.5951, 4.2235], [width, length] = [0.0000, 4.1527], theta = 104.5292

agreeing local minima of 12.9233 at:
[a,b] = [0.9809, 0.3096], [width, length] = [4.2145, 9.3595], theta = 148.2645

agreeing local minima of 12.9233 at:
[a,b] = [0.9809, 0.3096], [width, length] = [4.2145, 9.3595], theta = 148.2645

agreeing local minima of 12.9233 at:
[a,b] = [0.9809, 0.3096], [width, length] = [4.2145, 9.3595], theta = 148.2645

 

In [12]:
ellipse_def = zeros([len(weights),5])

for i in range(len(weights)):
    ellipse_def[i,:] = array(ellipse_results[i].x)

In [13]:
a = ellipse_def[:,0]
b = ellipse_def[:,1]

width = zeros([len(weights)])
length = zeros([len(weights)])
theta = zeros([len(weights)])

w = abs(ellipse_def[:,2])
l = abs(ellipse_def[:,3])

ref = l > w

width[ref] = w[ref]
length[ref] = l[ref]
theta[ref] = mod(ellipse_def[:,4],180)[ref]

width[~ref] = l[~ref]
length[~ref] = w[~ref]
theta[~ref] = mod(ellipse_def[:,4]+90,180)[~ref]

In [14]:
if (~os.path.isdir('../figures/weighting/individual/'+str(cutout_choice)+'/'))!=-2:
    os.mkdir('../figures/weighting/individual/'+str(cutout_choice))

if (~os.path.isdir('../figures/weighting/concat/'+str(cutout_choice)+'/'))!=-2:
    os.mkdir('../figures/weighting/concat/'+str(cutout_choice))

In [15]:
toRemove = glob('../figures/weighting/individual/'+str(cutout_choice)+'/*')
for path in toRemove:
    os.remove(path)

toRemove = glob('../figures/weighting/concat/'+str(cutout_choice)+'/*')
for path in toRemove:
    os.remove(path)

In [16]:
for i in range(len(weights)):

    ellipse = create_ellipse(ellipse_def[i,:])


    scaled_fig_start(12,12)

    plot(cutout[cutout_choice].exterior.xy[0],cutout[cutout_choice].exterior.xy[1])
    plot(ellipse.exterior.xy[0],ellipse.exterior.xy[1],'r--')

    plot(ellipse.centroid.xy[0],ellipse.centroid.xy[1],'go')
    
    text(-5.5,5,('Weight: '+str(weights[i])),fontsize=11)
    text(-5.5,-5,('Width: %.2f' % (width[i])),fontsize=11)
    text(-5.5,-5.5,('Length: %.2f' % (length[i])),fontsize=11)
    text(3.5,5,('Cutout: '+str(cutout_choice)),fontsize=11)
    
    scaled_fig_end(12,12)
    
    savefig('../figures/weighting/individual/'+str(cutout_choice)+'/weighting_'+str(i)+'_'+str(weights[i])+'.png')



In [17]:
images = sort(glob('../figures/weighting/individual/'+str(cutout_choice)+'/weighting*.png'))

In [18]:
for i in range(len(images)//2):
    
    a = imread(images[2*i])
    b = imread(images[2*i + 1])

    c = concatenate((a,b),axis=0)

    imsave(('../figures/weighting/concat/'+
           str(cutout_choice)+'/concat_'+
           str(cutout_choice)+'_'+
           str(i)+'.png'),c, dpi=254)
    
if mod(len(images),2) == 1:
    
    i += 1
    c = imread(images[2*i])
    
    imsave(('../figures/weighting/concat/'+
           str(cutout_choice)+'/concat_'+
           str(cutout_choice)+'_'+
           str(i)+'.png'),c, dpi=254)

In [19]:
width.ptp()


Out[19]:
1.7364115964280655

In [20]:
# images = sort(glob('../figures/weighting/final/*.png'))

In [21]:
# for i in range(len(images)//2):
    
#     a = imread(images[2*i])
#     b = imread(images[2*i + 1])

#     c = concatenate((a,b),axis=0)

#     imsave(('../figures/weighting/final/'+
#            'concat_'+
#            str(cutout_choice)+'_'+
#            str(i)+'.png'),c, dpi=254)
    
# if mod(len(images),2) == 1:
    
#     i += 1
#     c = imread(images[2*i])
    
#     imsave(('../figures/weighting/final/'+
#            'concat_'+
#            str(cutout_choice)+'_'+
#            str(i)+'.png'),c, dpi=254)

In [22]:
# See if this can be automated

# OPEN IMAGES IN GIMP "AS LAYER"
# SAVE IMAGES TO GIF "AS ANIMATION"

# Run imagemagick on resulting file:
# convert -units PixelsPerInch input.gif -density 254 output.pdf