3

I wrote the code below in Ipython notebook to generate a sigmoid function controlled by parameters a which defines the position of the sigmoid center, and b which defines its width:

%matplotlib inline
    import numpy as np
    import matplotlib.pyplot as plt

def sigmoid(x,a,b):
    #sigmoid function with parameters a = center; b = width
    s= 1/(1+np.exp(-(x-a)/b))
    return 100.0*(s-min(s))/(max(s)-min(s)) # normalize sigmoid to 0-100

x = np.linspace(0,10,256)
sigm = sigmoid(x, a=5, b=1)
fig = plt.figure(figsize=(24,6))
ax1 = fig.add_subplot(2, 1, 1)
ax1.set_xticks([])
ax1.set_xticks([])
plt.plot(x,sigm,lw=2,color='black')
plt.xlim(x.min(), x.max())

I wanted to add interactivity for parameters a and b so I re-wrote the function as below:

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from IPython.html.widgets import interactive
from IPython.display import display

def sigmoid_demo(a=5,b=1):
    x = np.linspace(0,10,256)
    s = 1/(1+np.exp(-(x-a)/(b+0.1))) # +0.1 to avoid dividing by 0
    sn = 100.0*(s-min(s))/(max(s)-min(s)) # normalize sigmoid to 0-100
    fig = plt.figure(figsize=(24,6))
    ax1 = fig.add_subplot(2, 1, 1)
    ax1.set_xticks([])
    ax1.set_yticks([])
    plt.plot(x,sn,lw=2,color='black')
    plt.xlim(x.min(), x.max())

w=widgets.interactive(sigmoid_demo,a=5,b=1)
display(w)

Is it possible to save the modified sigmoid function parameters a and b to variables dynamically as they are modified by the sliders, so they can be used in another subplot or separate plot, and do further calculations.?

I've gone through all examples of interactive widgets I could find but have not see anything pointing in that direction (these widgets are fairly new, so documentation for them does not seem as extensive as for other features). Any suggestions in the right direction is appreciated.

MyCarta
  • 808
  • 2
  • 12
  • 37

1 Answers1

4

You want to create the float widget manually:

from IPython.html.widgets import FloatSlider

# same as before

F1 = FloatSlider(value=5, min=0, max=10)
w=interactive(sigmoid_demo, a=F1, b=7)
display(w)

Then you can access F1.value any time after and it will be the current slider value.

You can also loop through w.widgets but that's painful.

Matt
  • 27,170
  • 6
  • 80
  • 74
  • Matt, what you suggest works (FloatSliderWidget, not FloatSlider) in that it allows to get the final parameter choice to use for further calculations. But what about the dynamically saving back to a variable? What I mean is - and I should clarify, I realize it was not that clear in the question - what if as I change the parameters with the slider in this subplot, I want another subplot that uses those same parameters to be interactively updated as well? – MyCarta Dec 16 '14 at 03:53
  • Never mind. I can do that by including the second subplot in sigmoid_demo – MyCarta Dec 16 '14 at 04:25
  • 1
    FloatSlider on IPython master :-) And you can do it only on current master cf doc: http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/Interactive%20Widgets/Widget%20Events.ipynb – Matt Dec 16 '14 at 08:08