In [1]:
import yaml
import numpy as np

import descartes as des
import matplotlib.pyplot as plt
%matplotlib inline

from electronfactors import (
    equivalent_ellipse, shapely_cutout,
    display_shapely, display_equivalent_ellipse,
    make_ellipse
)

In [2]:
from electronfactors.visuals.utilities import create_green_cm
green_cm = create_green_cm()

In [3]:
def reg_m(y, x):
    ones = np.ones(len(x[0]))
    X = sm.add_constant(np.column_stack((x[0], ones)))
    for ele in x[1:]:
        X = sm.add_constant(np.column_stack((ele, X)))
    results = sm.OLS(y, X).fit()
    return results

In [4]:
from matplotlib import rc
rc('font',**{'family':'serif',
             'size':'16'})
# rc('text', usetex=True)
rc('legend', fontsize=16, scatterpoints=1, fancybox=True)

In [5]:
with open("model_cache/12MeV_10app_100ssd.yml", 'r') as file:
    cutout_data = yaml.load(file)

In [6]:
XCoords = np.array(cutout_data['P22']['XCoords'])
YCoords = np.array(cutout_data['P22']['YCoords'])
width = np.array(cutout_data['P22']['width'])
length = np.array(cutout_data['P22']['length'])
poi = np.array(cutout_data['P22']['poi'])

In [7]:
poi


Out[7]:
array([ 0.05, -0.53])

In [8]:
length


Out[8]:
array(10.25)

In [9]:
[poi[0]-width/2, poi[0]+width/2]


Out[9]:
[-3.4000000000000004, 3.5]

In [10]:
fig = plt.figure(figsize=(6, 6))
ax = fig.add_subplot(111)

cutout = shapely_cutout(XCoords, YCoords)
ellipse = make_ellipse(ax=ax, poi=poi, width=width, length=length)


cutout_patch = des.PolygonPatch(
    cutout, fc=green_cm(0.75), 
    lw=1)
ax.add_patch(cutout_patch)

ellipse_patch = des.PolygonPatch(
    ellipse, fc=[ 0,  0,  0, 0 ],
    lw=2)
ax.add_patch(ellipse_patch)


plt.plot(
    [poi[0]-width/2, poi[0]+width/2],
    [poi[1]]*2, '--',
    lw=2, color='black', marker='|', mew=3, ms=60)

plt.plot(
    [poi[0]]*2,
    [poi[1]-length/2, poi[1]+length/2], '--',
    lw=2, color='black', marker='_', mew=3, ms=60)

plt.text(0.5, -0.35, "width (cm)")
plt.text(-0.6, -1.5, "length (cm)", rotation='vertical')

plt.grid(True)

plt.xlim([-5, 6])
plt.ylim([-6, 5])

plt.xlabel(r'x (cm at 100 SSD)')
plt.ylabel(r'y (cm at 100 SSD)')

# plt.savefig('figures/example_ellipse.png', bbox_inches='tight', dpi=600)
# fig.savefig('figures/example_ellipse.eps', bbox_inches='tight')


Out[10]:
<matplotlib.text.Text at 0x7fc8446f6748>

In [11]:
custom_data = dict()

key = 'Concave'
custom_data[key] = dict()
custom_data[key]['XCoords'] = np.array([-3.5, -3.5, 0, 3.5, 3.5, 0.5, 0.5, 3.5, 3.5, 0, -3.5]) / 0.95
custom_data[key]['YCoords'] = np.array([0, 4, 5, 4.5, 2, 1.5, -1.5, -2, -4.5, -5, -4]) / 0.95

labels = [key for key in custom_data]
ellipse_raw = [equivalent_ellipse(**custom_data[key]) for key in labels]

In [12]:
concave_cutout = shapely_cutout(custom_data[key]['XCoords'], custom_data[key]['YCoords'])
concave_ellipse = make_ellipse(**ellipse_raw[0])

xlims = np.array([-4, 4])
ylims = np.array([-9.5, 9.5])
scale = 0.5

fig = plt.figure(figsize=(scale * xlims.ptp(), scale * ylims.ptp()))
ax = fig.add_subplot(111)

cutout_patch = des.PolygonPatch(concave_cutout, fc=np.random.uniform(size=3), alpha=0.5, lw=2)
ax.add_patch(cutout_patch)

ellipse_patch = des.PolygonPatch(concave_ellipse, fc=np.random.uniform(size=3), alpha=0.5, lw=2)
ax.add_patch(ellipse_patch)

plt.grid(True)

plt.xlim(xlims)
plt.ylim(ylims)

plt.title(r'Convex test shape')
plt.xlabel(r'x (cm)')
plt.ylabel(r'y (cm)')


Out[12]:
<matplotlib.text.Text at 0x7fc8446966a0>