def test_list_tuple_3_int(): with nt.assert_raises(ValueError): c = interactive(f, tup=(1, 2, 0)) with nt.assert_raises(ValueError): c = interactive(f, tup=(1, 2, -1)) for min, max, step in [(0, 2, 1), (1, 10, 2), (1, 100, 2), (-5, 5, 4), (-100, -20, 4)]: c = interactive(f, tup=(min, max, step), lis=[min, max, step]) nt.assert_equal(len(c.children), 2) d = dict(cls=widgets.IntSlider, min=min, max=max, step=step, readout=True) check_widgets(c, tup=d, lis=d)
def test_list_tuple_2_float(): with nt.assert_raises(ValueError): c = interactive(f, tup=(1.0, 1.0)) with nt.assert_raises(ValueError): c = interactive(f, tup=(0.5, -0.5)) for min, max in [(0.5, 1.5), (1.1, 10.2), (1, 2.2), (-5.0, 5), (-20, -19.0)]: c = interactive(f, tup=(min, max), lis=[min, max]) nt.assert_equal(len(c.children), 2) d = dict(cls=widgets.FloatSlider, min=min, max=max, step=0.1, readout=True) check_widgets(c, tup=d, lis=d)
def interactfem3loop(): S = 4. ht = 1. xmin = -10. xmax = 10. zmax = 10. # xmin = lambda dx: -40.*dx # xmax = lambda dx: 40.*dx fem3loopwrap = lambda L,R,yc,xc,zc,dincl,ddecl,f,dx,showDataPts: fem3loop(L,R,-yc,xc,zc,dincl,ddecl,S,ht,f,xmin,xmax,dx,showDataPts) Q = interactive(fem3loopwrap, L = FloatSlider(min=0.00,max=0.20,step=0.01,value=0.10), R = FloatSlider(min=0.0,max=20000.,step=1000.,value=2000.), xc = FloatSlider(min=-10.,max=10.,step=1.,value=0.0), yc = FloatSlider(min=-10.,max=10.,step=1.,value=0.0), zc = FloatSlider(min=0.,max=zmax,step=0.5,value=1.), dincl = FloatSlider(min=-90.,max=90.,step=1.,value=0.), ddecl = FloatSlider(min=0.,max=180.,step=1.,value=90.), f = FloatSlider(min=10.,max=19990.,step=10.,value=10000.), dx = FloatSlider(min=0.25,max=5.,step=0.25,value=0.25), showDataPts = Checkbox(value=False) ) return Q
def test_default_out_of_bounds(): @annotate(f=(0, 10.), h={'a': 1}, j=['hi', 'there']) def f(f='hi', h=5, j='other'): pass c = interactive(f) check_widgets( c, f=dict( cls=widgets.FloatSlider, value=5., ), h=dict( cls=widgets.Dropdown, values={'a': 1}, value=1, ), j=dict( cls=widgets.Dropdown, values={ 'hi': 'hi', 'there': 'there' }, value='hi', ), )
def test_default_out_of_bounds(): @annotate(f=(0, 10.), h={'a': 1}, j=['hi', 'there']) def f(f='hi', h=5, j='other'): pass c = interactive(f) check_widgets( c, f=dict( cls=widgets.FloatSliderWidget, value=5., ), h=dict( cls=widgets.DropdownWidget, values={'a': 1}, value=1, ), j=dict( cls=widgets.DropdownWidget, values={ 'hi': 'hi', 'there': 'there' }, value='hi', ), )
def test_list_tuple_3_int(): with nt.assert_raises(ValueError): c = interactive(f, tup=(1,2,0)) with nt.assert_raises(ValueError): c = interactive(f, tup=(1,2,-1)) for min, max, step in [ (0,2,1), (1,10,2), (1,100,2), (-5,5,4), (-100,-20,4) ]: c = interactive(f, tup=(min, max, step), lis=[min, max, step]) nt.assert_equal(len(c.children), 2) d = dict( cls=widgets.IntSlider, min=min, max=max, step=step, readout=True, ) check_widgets(c, tup=d, lis=d)
def display_columns(df): """ Display the columns of df in a SelectMultiple widget and print the code representing the list of selected columns. """ dt = list(df.dtypes.items()) column_display = [f"{c} ({str(t)})" for c, t in dt] column = [c for c, t in dt] w = widgets.SelectMultiple(options=list(zip(column_display, column))) h = widgets.BoundedIntText(value=5, min=1, max=len(df)) def print_columns(columns=None, head=5): if columns is None: return else: inner = map(lambda t: '"' + str(t) + '"', columns) print(f"cols_selected = [{', '.join(inner)}]") print() if len(columns) == 0: return try: display(df[list(columns)].head(head).style) except AttributeError: pass return interactive(print_columns, columns=w, head=h)
def test_interact_manual_nocall(): callcount = 0 def calltest(testarg): callcount += 1 c = interactive(calltest, testarg=5, __manual=True) c.children[0].value = 10 nt.assert_equal(callcount, 0)
def pick_soft_threshold(self): w = interactive(self._soft_threshold, method=('spearman', 'pearson', 'kendall'), power=widgets.IntSliderWidget(min=0, max=100, step=2, value=10), signed=True) display(w)
def pick_hard_threshold(self): w = interactive(self._soft_threshold, method=('spearman', 'pearson', 'kendall'), power=widgets.FloatSliderWidget(min=0, max=.99, step=.01, value=0.5), signed=True) display(w)
def InteractLogs(d2=50, d3=100, rho1=2300, rho2=2300, rho3=2300, v1=500, v2=1000, v3=1500): logs = interactive(plotLogsInteract, d2=FloatSlider(min=0., max=100., step=5, value=d2), d3=FloatSlider(min=100., max=200., step=5, value=d3), rho1=FloatSlider(min=2000., max=5000., step=50., value=rho1), rho2=FloatSlider(min=2000., max=5000., step=50., value=rho2), rho3=FloatSlider(min=2000., max=5000., step=50., value=rho3), v1=FloatSlider(min=300., max=4000., step=50., value=v1), v2=FloatSlider(min=300., max=4000., step=50., value=v2), v3=FloatSlider(min=300., max=4000., step=50., value=v3)) return logs
def test_priority(): @annotate(annotate="annotate", kwarg="annotate") def f(kwarg="default", annotate="default", default="default"): pass c = interactive(f, kwarg="kwarg") check_widgets(c, kwarg=dict(cls=widgets.Text, value="kwarg"), annotate=dict(cls=widgets.Text, value="annotate"))
def test_list_tuple_2_int(): with nt.assert_raises(ValueError): c = interactive(f, tup=(1, 1)) with nt.assert_raises(ValueError): c = interactive(f, tup=(1, -1)) for min, max in [(0, 1), (1, 10), (1, 2), (-5, 5), (-20, -19)]: c = interactive(f, tup=(min, max), lis=[min, max]) nt.assert_equal(len(c.children), 3) d = dict( cls=widgets.IntSlider, min=min, max=max, step=1, readout=True, ) check_widgets(c, tup=d, lis=d)
def TrajectorySliderView(traj, frame=0, **kwargs): """IPython notebook widget for viewing trajectories in the browser with an interactiver slider. Parameters ---------- traj : Trajectory Trajectory for which you want the viewer. frame : int, default=0 Frame to set the slider to by default kwargs : string See TrajectoryView for all available options. See Also -------- TrajectoryView: IPython notebook widget for displaying trajectories in the browser with WebGL. """ widget = TrajectoryView(traj, frame=frame, **kwargs) def slide(frame): widget.frame = frame s = IntSlider(min=0, max=traj.n_frames - 1, value=frame) slider = interactive(slide, frame=s) container = Box() container.children = [widget, slider] return container
def test_list_tuple_2_float(): with nt.assert_raises(ValueError): c = interactive(f, tup=(1.0, 1.0)) with nt.assert_raises(ValueError): c = interactive(f, tup=(0.5, -0.5)) for min, max in [(0.5, 1.5), (1.1, 10.2), (1, 2.2), (-5., 5), (-20, -19.)]: c = interactive(f, tup=(min, max), lis=[min, max]) nt.assert_equal(len(c.children), 3) d = dict( cls=widgets.FloatSlider, min=min, max=max, step=.1, readout=True, ) check_widgets(c, tup=d, lis=d)
def test_list_tuple_str(): values = ['hello', 'there', 'guy'] first = values[0] c = interactive(f, tup=tuple(values), lis=list(values)) nt.assert_equal(len(c.children), 3) d = dict(cls=widgets.Dropdown, value=first, values=values) check_widgets(c, tup=d, lis=d)
def test_default_values(): @annotate(n=10, f=(0, 10.), g=5, h={'a': 1, 'b': 2}, j=['hi', 'there']) def f(n, f=4.5, g=1, h=2, j='there'): pass c = interactive(f) check_widgets( c, n=dict( cls=widgets.IntSlider, value=10, ), f=dict( cls=widgets.FloatSlider, value=4.5, ), g=dict( cls=widgets.IntSlider, value=5, ), h=dict(cls=widgets.Dropdown, values={ 'a': 1, 'b': 2 }, value=2), j=dict(cls=widgets.Dropdown, values=['hi', 'there'], value='there'), )
def test_list_tuple_str(): values = ["hello", "there", "guy"] first = values[0] c = interactive(f, tup=tuple(values), lis=list(values)) nt.assert_equal(len(c.children), 2) d = dict(cls=widgets.Dropdown, value=first, values=values) check_widgets(c, tup=d, lis=d)
def interactive_responseFct(): app = interactive( plot_ResponseFct, h_boom=FloatSlider(min=h_boom, max=h_boom_max, step=0.1, value=h_boom, continuous_update=False), h_1=FloatSlider(min=0., max=zmax, value=0.1, step=0.1, continuous_update=False), sigma_1=FloatSlider(min=sigmin, max=sigmax, value=sigmin, step=sigmin, continuous_update=False), sigma_2=FloatSlider(min=sigmin, max=sigmax, value=sigmin, step=sigmin, continuous_update=False), orientation=ToggleButtons(options=['vertical', 'horizontal'])) return app
def makeinteract3diagrams(v, z): Q = interactive(lambda x0, dx: view3diagrams( x0, dx, v1=v[0], v2=v[1], v3=v[2], z1=z[0], z2=z[1]), x0=IntSlider(min=0, max=10, step=1, value=4), dx=IntSlider(min=0, max=20, step=1, value=4)) return Q
def test_annotations(): @annotate(n=10, f=widgets.FloatText()) def f(n, f): pass c = interactive(f) check_widgets(c, n=dict(cls=widgets.IntSlider, value=10), f=dict(cls=widgets.FloatText))
def makeinteractTXdiagram(): Q = interactive(lambda x0, dx: viewTXdiagram( x0, dx, v1=v1, v2=v2, v3=v3, z1=z1, z2=z2), x0=IntSlider(min=1, max=10, step=1, value=4), dx=IntSlider(min=1, max=10, step=1, value=4)) return Q
def test_list_tuple_2_int(): with nt.assert_raises(ValueError): c = interactive(f, tup=(1,1)) with nt.assert_raises(ValueError): c = interactive(f, tup=(1,-1)) for min, max in [ (0,1), (1,10), (1,2), (-5,5), (-20,-19) ]: c = interactive(f, tup=(min, max), lis=[min, max]) nt.assert_equal(len(c.children), 2) d = dict( cls=widgets.IntSlider, min=min, max=max, step=1, readout=True, ) check_widgets(c, tup=d, lis=d)
def test_default_values(): @annotate(n=10, f=(0, 10.), g=5, h={'a': 1, 'b': 2}, j=['hi', 'there']) def f(n, f=4.5, g=1, h=2, j='there'): pass c = interactive(f) check_widgets(c, n=dict( cls=widgets.IntSlider, value=10, ), f=dict( cls=widgets.FloatSlider, value=4.5, ), g=dict( cls=widgets.IntSlider, value=5, ), h=dict( cls=widgets.Dropdown, values={'a': 1, 'b': 2}, value=2 ), j=dict( cls=widgets.Dropdown, values={'hi':'hi', 'there':'there'}, value='there' ), )
def draw_2D_slice_notebook(self, p_vals, x_variable, y_variable, range_x, range_y, slider_ranges, image_container=None, **kwargs): plot_widget = interactive(make_2D_slice, ds=fixed(self), p_vals=fixed(p_vals), x_variable=fixed(x_variable), y_variable=fixed(y_variable), range_x=fixed(range_x), range_y=fixed(range_y), intersections={'Single':[1], 'Single and Triple':[1,3], 'Triple':[3], 'All':range(1, 100)}, highlight=Text(value=''), image_widget=fixed(image_container), **{i:FloatSlider(min=log10(slider_ranges[i][0]), max=log10(slider_ranges[i][1]), step=log10(slider_ranges[i][1]/slider_ranges[i][0])/20, value=log10(p_vals[i])) for i in slider_ranges } ) make_2D_slice(ds=self, p_vals=p_vals, x_variable=x_variable, y_variable=y_variable, range_x=range_x, range_y=range_y, intersections=range(1, 100), image_widget=image_container, highlight='') return plot_widget
def interact_femPipe(): Q = interactive(fempipeWidget, alpha=FloatSlider(min=0.1, max=2., step=0.1, value=1.), pipedepth=FloatSlider(min=0.5, max=4.0, step=0.1, value=1.0)) return Q
def test_default_description(): c = interactive(f, b='text') w = c.children[0] check_widget(w, cls=widgets.Text, value='text', description='b', )
def test_custom_description(): c = interactive(f, b=widgets.Text(value='text', description='foo')) w = c.children[0] check_widget(w, cls=widgets.Text, value='text', description='foo', )
def test_list_tuple_str(): values = ['hello', 'there', 'guy'] first = values[0] dvalues = OrderedDict((v, v) for v in values) c = interactive(f, tup=tuple(values), lis=list(values)) nt.assert_equal(len(c.children), 2) d = dict(cls=widgets.DropdownWidget, value=first, values=dvalues) check_widgets(c, tup=d, lis=d)
def run_selector(self): ''' Driver of interactive plotting of SMB util-service-point-id :return: Container widget with child indicating which util-service-point to plot ''' smb_selection = SelectWidget(values=OrderedDict((str(id), id) for id in self.service_points)) i = interactive(self.interactive_selector, SMB=smb_selection) return i
def test_list_tuple_3_float(): with nt.assert_raises(ValueError): c = interactive(f, tup=(1,2,0.0)) with nt.assert_raises(ValueError): c = interactive(f, tup=(-1,-2,1.)) with nt.assert_raises(ValueError): c = interactive(f, tup=(1,2.,-1.)) for min, max, step in [ (0.,2,1), (1,10.,2), (1,100,2.), (-5.,5.,4), (-100,-20.,4.) ]: c = interactive(f, tup=(min, max, step), lis=[min, max, step]) nt.assert_equal(len(c.children), 2) d = dict( cls=widgets.FloatSlider, min=min, max=max, step=step, readout=True, ) check_widgets(c, tup=d, lis=d)
def test_fixed(): c = interactive(f, a=widgets.fixed(5), b='text') nt.assert_equal(len(c.children), 1) w = c.children[0] check_widget(w, cls=widgets.Text, value='text', description='b', )
def test_single_value_bool(): for a in (True, False): c = interactive(f, a=a) w = c.children[0] check_widget(w, cls=widgets.Checkbox, description='a', value=a, )
def test_single_value_string(): a = u'hello' c = interactive(f, a=a) w = c.children[0] check_widget(w, cls=widgets.Text, description='a', value=a, )
def makeinteractTXdiagram(): v1 = 600. v2 = 1200. v3 = 1700. z1, z2 = 5., 10. Q = interactive(lambda x0, dx: viewTXdiagram(x0, dx, v1=v1, v2=v2, v3=v3, z1=z1, z2=z2), x0=IntSlider(min=1, max=10, step=1, value=4), dx=IntSlider(min=1, max=10, step=1,value=4)) return Q
def test_list_tuple_invalid(): for bad in [ (), (5, 'hi'), ('hi', 5), ({},), (None,), ]: with nt.assert_raises(ValueError): print(bad) # because there is no custom message in assert_raises c = interactive(f, tup=bad)
def makeinteractTXwigglediagram(): v1 = 600. v2 = 1200. v3 = 1700. z1, z2 = 5., 10. Q = interactive(lambda x0, dx, tI, v, Fit=False: viewWiggleTX(x0, dx, tI, v, Fit=Fit, v1=v1, v2=v2, v3=v3, z1=z1, z2=z2), x0=IntSlider(min=1, max=10, step=1, value=4), dx=IntSlider(min=1, max=10, step=1,value=4), tI=FloatSlider(min=0., max=0.25, step=0.0025,value=0.05), v=FloatSlider(min=400, max=2000, step=50,value=1000.)) return Q
def test_list_tuple_invalid(): for bad in [ (), (5, 'hi'), ('hi', 5), ({}, ), (None, ), ]: with nt.assert_raises(ValueError): print(bad) # because there is no custom message in assert_raises c = interactive(f, tup=bad)
def setup_conditional_sampler(x, y, yerr, kernel): # Pre-compute a bunch of things for speed. xs = np.linspace(-6, 6, 300) rxx = x[:, None] - x[None, :] rss = xs[:, None] - xs[None, :] rxs = x[None, :] - xs[:, None] ye2 = yerr**2 # Initialize at the least squares position. mu, _ = least_squares(x, y, yerr) # This function samples from the conditional distribution and # plots those samples. def sample_conditional(amp, ell, m=mu[0], b=mu[1]): np.random.seed(123) # Compute the covariance matrices. Kxx = kernel([amp, ell], rxx) Kxx[np.diag_indices_from(Kxx)] += ye2 Kss = kernel([amp, ell], rss) Kxs = kernel([amp, ell], rxs) # Compute the residuals. resid = y - (m * x + b) model = m * xs + b a = np.linalg.solve(Kxx, resid) # Compute the likelihood. s, ld = np.linalg.slogdet(Kxx) ll = -0.5 * (np.dot(resid, a) + ld + len(x) * np.log(2 * np.pi)) # Compute the predictive mean. mu = np.dot(Kxs, a) + model # Compute the predictive covariance. cov = Kss - np.dot(Kxs, np.linalg.solve(Kxx, Kxs.T)) # Sample and display the results. y0 = np.random.multivariate_normal(mu, cov, 6) pl.errorbar(x, y, yerr=yerr, fmt=".k", capsize=0) pl.plot(xs, y0.T, "k", alpha=0.5) pl.plot(xs, m * xs + b, ":r") pl.ylim(-3, 3) pl.xlim(-6, 6) pl.title("lnlike = {0}".format(ll)) w = interactive(sample_conditional, amp=(1.0e-4, 2.0), ell=(0.01, 3.0), m=(-1.0, 5.0), b=(-3.0, 3.0)) display(w) return w
def InteractLogs(d2=50, d3=100, rho1=2300, rho2=2300, rho3=2300, v1=500, v2=1000, v3=1500): logs = interactive(plotLogsInteract, d2 =FloatSlider(min = 0. ,max = 100. ,step = 5 , value = d2 ), d3 =FloatSlider(min = 100. ,max = 200. ,step = 5 , value = d3 ), rho1=FloatSlider(min = 2000.,max = 5000.,step = 50., value = rho1), rho2=FloatSlider(min = 2000.,max = 5000.,step = 50., value = rho2), rho3=FloatSlider(min = 2000.,max = 5000.,step = 50., value = rho3), v1 =FloatSlider(min = 300. ,max = 4000.,step = 50., value = v1 ), v2 =FloatSlider(min = 300. ,max = 4000.,step = 50., value = v2 ), v3 =FloatSlider(min = 300. ,max = 4000.,step = 50., value = v3 )) return logs
def interact(self): def _interact(count, field): self.view(count, field) display( interactive( _interact, count=IntSliderWidget(min=5,max=100,step=5,value=35), field=('cumtime', 'tottime', 'ncalls'), ) )
def test_list_tuple_str(): values = ['hello', 'there', 'guy'] first = values[0] c = interactive(f, tup=tuple(values), lis=list(values)) nt.assert_equal(len(c.children), 3) d = dict( cls=widgets.Dropdown, value=first, options=values ) check_widgets(c, tup=d, lis=d)
def test_custom_description(): d = {} def record_kwargs(**kwargs): d.clear() d.update(kwargs) c = interactive(record_kwargs, b=widgets.Text(value="text", description="foo")) w = c.children[0] check_widget(w, cls=widgets.Text, value="text", description="foo") w.value = "different text" nt.assert_equal(d, {"b": "different text"})
def nb_set_fig_size(): from IPython.html import widgets from IPython.display import display width, height = matplotlib.rcParams["figure.figsize"] width_slider = widgets.IntSlider(min=1, max=40, value=width) height_slider = widgets.IntSlider(min=1, max=40, value=height) def set_figsize(width, height): matplotlib.rcParams["figure.figsize"] = width, height matplotlib.pyplot.plot(numpy.sin(numpy.arange(100))) w = widgets.interactive(set_figsize,width=width_slider, height=height_slider) display(w)
def makeinteractTXwigglediagram(): v1 = 600. v2 = 1200. v3 = 1700. z1, z2 = 5., 10. Q = interactive(lambda x0, dx, tI, v, Fit=False: viewWiggleTX( x0, dx, tI, v, Fit=Fit, v1=v1, v2=v2, v3=v3, z1=z1, z2=z2), x0=IntSlider(min=1, max=10, step=1, value=4), dx=IntSlider(min=1, max=10, step=1, value=4), tI=FloatSlider(min=0., max=0.25, step=0.0025, value=0.05), v=FloatSlider(min=400, max=2000, step=50, value=1000.)) return Q
def InteractNosiy(noisyDataFile, noisyTimeFile): noisy = interactive(NoisyNMOWidget, t0=FloatSlider(min=0.1, max=0.6, step=0.01, continuous_update=False), v=FloatSlider(min=800., max=2500., step=100., continuous_update=False), syndat=fixed(noisyDataFile), timdat=fixed(noisyTimeFile)) return noisy
def InteractClean(cleanDataFile, cleanTimeFile): clean = interactive(CleanNMOWidget, t0=FloatSlider(min=0.2, max=0.8, step=0.01, continuous_update=False), v=FloatSlider(min=1000., max=5000., step=100., continuous_update=False), syndat=fixed(cleanDataFile), timdat=fixed(cleanTimeFile)) return clean
def test_single_value_dict(): for d in [ dict(a=5), dict(a=5, b='b', c=dict), ]: c = interactive(f, d=d) w = c.children[0] check_widget(w, cls=widgets.Dropdown, description='d', options=d, value=next(iter(d.values())), )
def test_single_value_float(): for a in (2.25, 1.0, -3.5): c = interactive(f, a=a) w = c.children[0] check_widget(w, cls=widgets.FloatSlider, description='a', value=a, min= -a if a > 0 else 3*a, max= 3*a if a > 0 else -a, step=0.1, readout=True, )
def test_annotations(): @annotate(n=10, f=widgets.FloatText()) def f(n, f): pass c = interactive(f) check_widgets( c, n=dict( cls=widgets.IntSlider, value=10, ), f=dict(cls=widgets.FloatText, ), )