In [ ]:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

from sklearn.utils import check_random_state
# dirty local imports:
import sys
sys.path.append("./../../../")
from share_code.utils import my_saving_display, huber_ori, huber_conco, quadratic, pinball, hinge, moreau_pinball, berhu


from matplotlib import rc
from matplotlib import animation

%matplotlib notebook
In [ ]:
sns.set_context('poster')
sns.set_style("ticks")
sns.set_palette("colorblind", 8)
dirname = "../prebuiltimages/"
imageformat = '.pdf'
saving = True

pgf_with_latex = {
    "text.usetex": True,            # use LaTeX to write all text
    "pgf.preamble": [               # Set-up LaTeX
        r'\usepackage{amsmath}',
        r'\usepackage{mathtools}',
    ]
}

plt.rcParams.update(pgf_with_latex)
In [ ]:
import seaborn as sns
sns.set_palette("colorblind")
sns.set_style("ticks")
color_blind_list = sns.color_palette("GnBu_d", 8)
c1 = color_blind_list[0]
c2 = color_blind_list[1]
c3 = color_blind_list[2]
In [ ]:
x = np.linspace(-4, 4, 500)
In [ ]:
# huber_ori case
fig0 = plt.figure(figsize=(6, 6))
ax1 = plt.subplot(111)
ax1.plot(x, np.abs(x), 'k', lw=2, label='$f=|\cdot|$')
plt.xlim([-4, 4])
plt.ylim([-1, 5])
plt.legend(loc='upper center')
plt.axes().set_aspect('equal')
plt.show()
my_saving_display(fig0, dirname, "abs", imageformat, saving)


thresholds = [2.5, 1, 0.2]

for threshold in thresholds:
    ax1.plot(x, huber_ori(x, threshold), c=c3, lw=1.7,
             label='$^\gamma f, \gamma= {:.1f}$'.format(threshold))
    plt.legend(loc='upper center')
    figname = "Huber_ori{:.1f}".format(threshold).replace(".", "pt")

    my_saving_display(fig0, dirname, figname, imageformat, saving)
In [ ]:
# huber_conco case

fig0 = plt.figure(figsize=(6, 6))
ax1 = plt.subplot(111)
ax1.plot(x, np.abs(x), 'k', lw=2, label='$f=|\cdot|$')
plt.xlim([-4, 4])
plt.ylim([-1, 5])
plt.legend(loc='upper center')
plt.axes().set_aspect('equal')
plt.show()
# my_saving_display(fig0, dirname, "abs", imageformat, saving)


thresholds = [2.5, 1, 0.2]

for threshold in thresholds:
    ax1.plot(x, huber_conco(x, threshold), c=c3, lw=1.7,
             label=r'$_\omega^\gamma f, \gamma= {:.1f}$'.format(threshold))
    plt.legend(loc='upper center')
    figname = "Huber_conco{:.1f}".format(threshold).replace(".", "pt")

    my_saving_display(fig0, dirname, figname, imageformat, saving)
In [ ]:
nb_s = 90
values = np.linspace(- 10.0, 10.0, num=nb_s)

gamma = 1.5

f1 = np.abs
f2 = quadratic

fig0 = plt.figure(figsize=(6, 6))



ax1 = plt.subplot(121)
ax2 = plt.subplot(122)
ax1.set_ylim([-1, 20])
ax2.set_ylim([-1, 20])

ax1.set_xlim([-5, 5])
ax2.set_xlim([-5, 5])

fig0.tight_layout() 

for value in values:
    ax1.plot(x, f1(value) + f2(x - value) / gamma, c=c3, lw=1.7)
    ax2.plot(x, f2(value) / gamma + f1(x - value), c=c3, lw=1.7)

    figname = "Huber_conco{:.1f}".format(threshold).replace(".", "pt")
In [ ]:
# Moreauization on the absolute value function
In [ ]:
gamma = 3
f1 = np.abs
f2 = quadratic

nb_s = 90
values = np.linspace(- 10.0, 10.0, num=nb_s)



# def movie_making(f1, f2, gamma, nb_s, values, fig_movie):
#     Create movie
def init():
    line1.set_data([], [])
    line2.set_data([], [])

    return line1,line2

# animation function.  This is called sequentially
def animate(i, f1=f1, f2=f2, gamma=gamma):
    y = f1(values[i]) + f2(values - values[i]) / gamma
    z = f2(values[i]) / gamma + f1(x - values[i])

    line1.set_data(values, y)
    line2.set_data(values, z)

    return line1, line2,

nb_s = 100

Nframes = nb_s
values = np.linspace(- 10.0, 10.0, num=nb_s)
x = np.linspace(- 10.0, 10.0, num=nb_s)

fig_movie = plt.figure()

title = "Moreauization"
plt.title( title + r'$|\cdot|$')

ax1 = plt.axes(xlim=(-10, 10), ylim=([-1, 20]))
ax1 = plt.subplot(121)
ax2 = plt.subplot(122)
ax1.set_ylim([-1, 20])
ax2.set_ylim([-1, 20])

ax1.set_xlim([-10, 10])
ax2.set_xlim([-10, 10])

ax1.plot(values, huber_ori(values, threshold=gamma))
ax2.plot(values, huber_ori(values, threshold=gamma))
ax1.plot(values, f1(values), 'k--')
ax2.plot(values, f2(values) / gamma, 'k--')

line1, = ax1.plot([], [], lw=1, c='k')
line2, = ax2.plot([], [], lw=1, c='k')
# line, = ax.fill_between([], [], [], facecolor='blue', alpha=0.5)

fig_movie.tight_layout()

def animate_as_gif(nframe):
    """Perform: Patrol-loop (otherwise just plug-in update_data)."""
    if nframe < Nframes:
        nframe = nframe
    elif nframe >= Nframes:
        nframe = 2 * Nframes - nframe - 1
    return animate(nframe)

simulation = animation.FuncAnimation(fig_movie, animate_as_gif, blit=False,
                                     frames=100,
                                     interval=50, repeat=True)
# to save the animation
if saving:
#     simulation.save(title, dpi=400)
    simulation.save(title + "abs" + '.gif', writer='imagemagick')
plt.show()
In [ ]:
 
In [ ]:
gamma = 3
f1 = pinball
f2 = quadratic

nb_s = 91
values = np.linspace(- 10.0, 10.0, num=nb_s)



# def movie_making(f1, f2, gamma, nb_s, values, fig_movie):
#     Create movie
def init():
    line1.set_data([], [])
    line2.set_data([], [])

    return line1,line2

# animation function.  This is called sequentially
def animate(i, f1=f1, f2=f2, gamma=gamma):
    y = f1(values[i]) + f2(values - values[i]) / gamma
    z = f2(values[i]) / gamma + f1(x - values[i])

    line1.set_data(values, y)
    line2.set_data(values, z)

    return line1, line2,

Nframes = nb_s
values = np.linspace(- 10.0, 10.0, num=nb_s)
x = np.linspace(- 10.0, 10.0, num=nb_s)

fig_movie = plt.figure()


title = "Moreauization"
plt.title( title + '(Pinball)')

ax1 = plt.axes(xlim=(-10, 10), ylim=([-1, 20]))
ax1 = plt.subplot(121)
ax2 = plt.subplot(122)
ax1.set_ylim([-1, 20])
ax2.set_ylim([-1, 20])

ax1.set_xlim([-10, 10])
ax2.set_xlim([-10, 10])

ax1.plot(values, moreau_pinball(values, gamma))
ax2.plot(values, moreau_pinball(values, gamma))
ax1.plot(values, f1(values), 'k--')
ax2.plot(values, f2(values) / gamma, 'k--')

line1, = ax1.plot([], [], lw=1, c='k')
line2, = ax2.plot([], [], lw=1, c='k')
# line, = ax.fill_between([], [], [], facecolor='blue', alpha=0.5)

fig_movie.tight_layout()

def animate_as_gif(nframe):
    """Perform: Patrol-loop (otherwise just plug-in update_data)."""
    if nframe < Nframes:
        nframe = nframe
    elif nframe >= Nframes:
        nframe = 2 * Nframes - nframe - 1
    return animate(nframe)

simulation = animation.FuncAnimation(fig_movie, animate_as_gif, blit=False,
                                     frames=100,
                                     interval=50, repeat=True)
# to save the animation
if saving:
#     simulation.save(title, dpi=400)
    simulation.save(title + "Pinball" + '.gif', writer='imagemagick')
plt.show()
In [ ]:
# Moreauization on the Behru function
In [ ]:
gamma = 2
f1 = berhu
f2 = quadratic

nb_s = 91
values = np.linspace(- 10.0, 10.0, num=nb_s)



# def movie_making(f1, f2, gamma, nb_s, values, fig_movie):
#     Create movie
def init():
    line1.set_data([], [])
    line2.set_data([], [])

    return line1,line2

# animation function.  This is called sequentially
def animate(i, f1=f1, f2=f2, gamma=gamma):
    y = f1(values[i]) + f2(values - values[i]) / gamma
    z = f2(values[i]) / gamma + f1(x - values[i])

    line1.set_data(values, y)
    line2.set_data(values, z)

    return line1, line2,

Nframes = nb_s
values = np.linspace(- 10.0, 10.0, num=nb_s)
x = np.linspace(- 10.0, 10.0, num=nb_s)

fig_movie = plt.figure()


title = "Moreauization"
plt.title( title + '(Pinball)')

ax1 = plt.axes(xlim=(-10, 10), ylim=([-1, 20]))
ax1 = plt.subplot(121)
ax2 = plt.subplot(122)
ax1.set_ylim([-1, 40])
ax2.set_ylim([-1, 40])

ax1.set_xlim([-10, 10])
ax2.set_xlim([-10, 10])


# XXX : TODO create the Moreau envelop of the Berhu function
# ax1.plot(values, moreau_pinball(values, gamma))
# ax2.plot(values, moreau_pinball(values, gamma))
ax1.plot(values, f1(values), 'k--')
ax2.plot(values, f2(values) / gamma, 'k--')

line1, = ax1.plot([], [], lw=1, c='k')
line2, = ax2.plot([], [], lw=1, c='k')
# line, = ax.fill_between([], [], [], facecolor='blue', alpha=0.5)

fig_movie.tight_layout()

def animate_as_gif(nframe):
    """Perform: Patrol-loop (otherwise just plug-in update_data)."""
    if nframe < Nframes:
        nframe = nframe
    elif nframe >= Nframes:
        nframe = 2 * Nframes - nframe - 1
    return animate(nframe)

simulation = animation.FuncAnimation(fig_movie, animate_as_gif, blit=False,
                                     frames=100,
                                     interval=50, repeat=True)
# to save the animation
if saving:
#     simulation.save(title, dpi=400)
    simulation.save(title + "Berhu" + '.gif', writer='imagemagick')
plt.show()
In [ ]: