示例#1
0
def make_field_chooser(x, xs, y, ys):
    """Choose values for x and y.
    """
    xd = Dropdown(description='%s:' % x, values=xs)
    yd = Dropdown(description='%s:' % y, values=ys)
    container = VBox(children=[xd, yd])
    return container
示例#2
0
    def status_printer(_, total=None, desc=None, ncols=None, img=None):
        """
        Manage the printing of an IPython/Jupyter Notebook progress bar widget.
        """
        # Fallback to text bar if there's no total
        # DEPRECATED: replaced with an 'info' style bar
        # if not total:
        #    return super(mytqdm, mytqdm).status_printer(file)

        # fp = file

        # Prepare IPython progress bar
        try:
            if total:
                pbar = IntProgress(min=0, max=total)
            else:  # No total? Show info style bar with no progress tqdm status
                pbar = IntProgress(min=0, max=1)
                pbar.value = 1
                pbar.bar_style = 'info'
        except NameError:
            # #187 #451 #558
            raise ImportError(
                "IntProgress not found. Please update jupyter and ipywidgets."
                " See https://ipywidgets.readthedocs.io/en/stable"
                "/user_install.html")

        if desc:
            pbar.description = desc
            if IPYW >= 7:
                pbar.style.description_width = 'initial'
        # Prepare status text
        ptext = HTML()
        timg = HTML()
        if img:
            timg.value = "<br>%s<br>" % img
        # Only way to place text to the right of the bar is to use a container
        container = VBox([HBox(children=[pbar, ptext]), timg])
        # Prepare layout
        if ncols is not None:  # use default style of ipywidgets
            # ncols could be 100, "100px", "100%"
            ncols = str(ncols)  # ipywidgets only accepts string
            try:
                if int(ncols) > 0:  # isnumeric and positive
                    ncols += 'px'
            except ValueError:
                pass
            pbar.layout.flex = '2'
            container.layout.width = ncols
            container.layout.display = 'inline-flex'
            container.layout.flex_flow = 'row wrap'
        display(container)

        return container
示例#3
0
    def __init__(self, model=None, model_config=None, *args, **kwargs):
        # RUN HTML
        self.model = model or Spectrogram(
            **(model_config
               or {}))  #Need to access spectrogram if defaulting...
        # Create alert widget (refactor into its own function?)
        alert = Alert(description="Alert or something")
        link((self.model, "message"), (alert, "value"))

        # Create a GUI
        kwargs["orientation"] = 'horizontal'
        kwargs["children"] = [
            HBox([
                VBox([self.INOUT_panel(), self.model, alert]),
                VBox([
                    self.plot_panel(),
                    self.slicing_panel(),
                    self.unit_panel()
                ]),
            ])
        ]
        super(SpectraGui, self).__init__(*args, **kwargs)
        self._dom_classes += ("spectroscopy row", )
示例#4
0
def make_vgroup(children):
    container = VBox(children=children)
    return container
示例#5
0
    def INOUT_panel(self):
        # create correlation controls. NOTE: should only be called once.
        incheck = Checkbox(description='Import')
        link((self.model, "inbox"), (incheck, "value"))
        outcheck = Checkbox(description='Export')
        link((self.model, "outbox"), (outcheck, "value"))

        #loaddata = Checkbox(description="Testdata")
        #link((self.model, "load_spec"), (loaddata, "value"))
        #testdataset = Text(description = "")
        #link((self.model, "testdataset"), (testdataset, "value"))

        filename = Text(description="")
        link((self.model, "file_name"), (filename, "value"))
        loadbutton = Button(color='black',
                            background_color='AliceBlue',
                            description="Load")
        loadbutton.on_click(lambda x: self.model.load_from_ns())
        boxi = HBox([filename, loadbutton])
        #link((self.model, "load_spec"), (specbox, "visible"))

        #        loadfile = Checkbox(description="NB Variable") #Test Data
        #        link((self.model, "load_file"), (loadfile, "value"))

        #                filebox = HBox([loadbutton, filename])
        #link((self.model, "load_file"), (filebox, "visible"))

        #boxi = VBox([
        #HBox([loaddata, loadfile]),
        #loaddata,
        #             specbox,
        #filebox,
        #            ])
        link((self.model, "inbox"), (boxi, "visible"))

        saveplot = Button(color='black',
                          background_color='AliceBlue',
                          description='Save Plot')
        saveplot.on_click(lambda x: self.model.save_plot())
        savespec = Button(color='black',
                          background_color='AliceBlue',
                          description='Export Dataset')
        savespec.on_click(lambda x: self.model.save_to_ns())
        savespecas = Text(description="")
        link((self.model, "save_spec_as"), (savespecas, "value"))

        boxo = VBox([
            savespecas,
            HBox([saveplot, savespec]),
        ])
        link((self.model, "outbox"), (boxo, "visible"))

        #reset = Button(color='white',background_color='violet',description='Reset Defaults')
        #reset.on_click(lambda x: self.model.)

        #redraw = Button(description="Redraw")
        #redraw.on_click(lambda x: self.model.draw())

        return ControlPanel(
            title="Import/Export Dataset",
            children=[HBox([VBox([incheck, outcheck]),
                            VBox([boxi, boxo])])])
示例#6
0
    def slicing_panel(self):
        """ create spectrum controls.  NOTE: should only be called once."""

        model = self.model  #For readability

        # ALL WIDGETS ARE CAPITALIZED.
        #AXIS = RadioButtons(values=[0,1],description="Axis")
        #link((model, "slice_axis"), (AXIS, "value"))

        SPECSTART = FloatSlider(
            description="Spec Start",
            min=model.specslice_position_start)  #Will not try to update

        link((model, "specslice_position_start"), (SPECSTART, "value"))
        link((model, "specstep"), (SPECSTART, "step"))
        link((model, "specslider_start"),
             (SPECSTART,
              "min"))  # Start end values (IE slider_min / slider_max)
        link((model, "specslider_end"), (SPECSTART, "max"))

        SPECEND = FloatSlider(
            description="Spec End",
            max=model.specslice_position_end)  # Will not try to update

        link((model, "specslice_position_end"), (SPECEND, "value"))
        link((model, "specstep"), (SPECEND, "step"))
        link((model, "specslider_start"), (SPECEND, "min"))
        link((model, "specslider_end"), (SPECEND, "max"))

        # SPACING WIDGET
        SPECSPACING = IntText(description="Spec Sample by", value=1)
        link((model, "specspacing"), (SPECSPACING, "value"))

        TIMESTART = FloatSlider(description="Var Start",
                                min=model.timeslice_position_start)

        link((model, "timeslice_position_start"), (TIMESTART, "value"))
        link((model, "timestep"), (TIMESTART, "step"))
        link((model, "timeslider_start"), (TIMESTART, "min"))
        link((model, "timeslider_end"), (TIMESTART, "max"))

        TIMEEND = FloatSlider(description="Var End",
                              max=model.timeslice_position_end)
        link((model, "timeslice_position_end"), (TIMEEND, "value"))
        link((model, "timestep"), (TIMEEND, "step"))
        link((model, "timeslider_start"), (TIMEEND, "min"))
        link((model, "timeslider_end"), (TIMEEND, "max"))

        TIMESPACING = IntText(description="Var Sample by", value=1)
        link((model, "timespacing"), (TIMESPACING, "value"))

        speccheck = Checkbox(description="Spectral Axis")
        link((model, "specbox"), (speccheck, "value"))
        SPECRANGED = VBox([SPECSTART, SPECEND, SPECSPACING])
        link((model, "specbox"), (SPECRANGED, "visible"))

        timecheck = Checkbox(description="Variation Axis")
        link((model, "timebox"), (timecheck, "value"))
        TIMERANGED = VBox([TIMESTART, TIMEEND, TIMESPACING])
        link((model, "timebox"), (TIMERANGED, "visible"))

        return ControlPanel(
            title="Slicing/Sampling",
            children=[HBox([speccheck, timecheck]), SPECRANGED, TIMERANGED])
示例#7
0
    def plot_panel(self):
        # create draw mode controls.  NOTE: should only be called once.
        cbar = Checkbox(description="Colorbar")
        link((self.model, "colorbar"), (cbar, "value"))

        interact = Checkbox(description="Interactive")
        link((self.model, "interactive"), (interact, "value"))

        plug_select = Checkbox(description="Line Selection")
        link((self.model, "selectlines"), (plug_select, "value"))

        autoupdate = Checkbox(description="Auto Update")
        link((self.model, "autoupdate"), (autoupdate, "value"))

        plugin2 = Checkbox(description='Cursor')
        plugin3 = Checkbox(description='plugin3')
        fwidth = FloatText(description='Plot width')
        fheight = FloatText(description='Plot height')
        link((self.model, "figwidth"), (fwidth, "value"))
        link((self.model, "figheight"), (fheight, "value"))

        f = Text(description="Function:")
        link((self.model, "user_f"), (f, "value"))
        fapp = Button(color='black',
                      background_color='AliceBlue',
                      description="Apply")
        fapp.on_click(lambda x: self.model.apply_userf(name='apply clicked'))

        #plugins = HBox([plugin1,plugin2,plugin3])
        #more = Checkbox(description="More Options")### LINK IT
        #link((self, "moreopt"), (more, "value"))
        #popmore = Popup(children=[VBox([HBox([plug_select,plugin2,plugin3]),
        #                                HBox([f,fapp]),
        #                                VBox([fwidth, fheight])
        #                              ])],
        #                description='Advanced', button_text='Advanced')

        more = Checkbox(description="Advanced")
        link((self.model, "advancedbox"), (more, "value"))

        popmore = VBox([
            HBox([
                plug_select,
                plugin2,
                #		plugin3
            ]),
            HBox([f, fapp]),
            HBox([fwidth, fheight])
        ])
        link((self.model, "advancedbox"), (popmore, "visible"))

        cmapcheck = Checkbox(description="Colormap")
        link((self.model, "cmapbox"), (cmapcheck, "value"))

        cmap = Dropdown(description="Colormap", values=self.model.COLORMAPS)
        link((self.model, "colormap"), (cmap, "value"))
        link((self.model, "cmapbox"), (cmap, "visible"))

        colorcheck = Checkbox(description="Color")
        link((self.model, "colorbox"), (colorcheck, "value"))

        color = Dropdown(description="Color", values=self.model.COLORS)
        link((self.model, "color"), (color, "value"))
        link((self.model, "colorbox"), (color, "visible"))

        kind = Dropdown(description="Plot Type", values=PLOTPARSER.keys())
        link((self.model, "kind"), (kind, "value"))

        return ControlPanel(title="Plot Settings",
                            children=[
                                VBox([autoupdate, kind]),
                                HBox([cbar, interact]),
                                HBox([colorcheck, cmapcheck]),
                                HBox([more]), cmap, color, popmore
                            ])
示例#8
0
    def __init__(self,
                 model,
                 opt,
                 maxiters,
                 verbose=False,
                 current_iteration=0,
                 ipython_notebook=True,
                 clear_after_finish=False):
        self.verbose = verbose
        if self.verbose:
            self.model = model
            self.iteration = current_iteration
            self.p_iter = self.iteration
            self.maxiters = maxiters
            self.len_maxiters = len(str(maxiters))
            self.opt_name = opt.opt_name
            self.model.add_observer(self, self.print_status)
            self.status = 'running'
            self.clear = clear_after_finish

            self.update()

            try:
                from IPython.display import display
                from IPython.html.widgets import IntProgress, HTML, Box, VBox, HBox, FlexBox
                self.text = HTML(width='100%')
                self.progress = IntProgress(min=0, max=maxiters)
                #self.progresstext = Text(width='100%', disabled=True, value='0/{}'.format(maxiters))
                self.model_show = HTML()
                self.ipython_notebook = ipython_notebook
            except:
                # Not in Ipython notebook
                self.ipython_notebook = False

            if self.ipython_notebook:
                left_col = VBox(children=[self.progress, self.text],
                                padding=2,
                                width='40%')
                right_col = Box(children=[self.model_show],
                                padding=2,
                                width='60%')
                self.hor_align = FlexBox(children=[left_col, right_col],
                                         width='100%',
                                         orientation='horizontal')

                display(self.hor_align)

                try:
                    self.text.set_css('width', '100%')
                    left_col.set_css({
                        'padding': '2px',
                        'width': "100%",
                    })

                    right_col.set_css({
                        'padding': '2px',
                    })

                    self.hor_align.set_css({
                        'width': "100%",
                    })

                    self.hor_align.remove_class('vbox')
                    self.hor_align.add_class('hbox')

                    left_col.add_class("box-flex1")
                    right_col.add_class('box-flex0')

                except:
                    pass

                #self.text.add_class('box-flex2')
                #self.progress.add_class('box-flex1')
            else:
                self.exps = exponents(self.fnow, self.current_gradient)
                print('Running {} Code:'.format(self.opt_name))
                print('  {3:7s}   {0:{mi}s}   {1:11s}    {2:11s}'.format(
                    "i", "f", "|g|", "runtime", mi=self.len_maxiters))
示例#9
0
    def __init__(self, model, opt, maxiters, verbose=False, current_iteration=0, ipython_notebook=True, clear_after_finish=False):
        self.verbose = verbose
        if self.verbose:
            self.model = model
            self.iteration = current_iteration
            self.p_iter = self.iteration
            self.maxiters = maxiters
            self.len_maxiters = len(str(maxiters))
            self.opt_name = opt.opt_name
            self.model.add_observer(self, self.print_status)
            self.status = 'running'
            self.clear = clear_after_finish

            self.update()

            try:
                from IPython.display import display
                from IPython.html.widgets import IntProgress, HTML, Box, VBox, HBox, FlexBox
                self.text = HTML(width='100%')
                self.progress = IntProgress(min=0, max=maxiters)
                #self.progresstext = Text(width='100%', disabled=True, value='0/{}'.format(maxiters))
                self.model_show = HTML()
                self.ipython_notebook = ipython_notebook
            except:
                # Not in Ipython notebook
                self.ipython_notebook = False

            if self.ipython_notebook:
                left_col = VBox(children=[self.progress, self.text], padding=2, width='40%')
                right_col = Box(children=[self.model_show], padding=2, width='60%')
                self.hor_align = FlexBox(children = [left_col, right_col], width='100%', orientation='horizontal')

                display(self.hor_align)

                try:
                    self.text.set_css('width', '100%')
                    left_col.set_css({
                             'padding': '2px',
                             'width': "100%",
                             })

                    right_col.set_css({
                             'padding': '2px',
                             })

                    self.hor_align.set_css({
                             'width': "100%",
                             })

                    self.hor_align.remove_class('vbox')
                    self.hor_align.add_class('hbox')

                    left_col.add_class("box-flex1")
                    right_col.add_class('box-flex0')

                except:
                    pass

                #self.text.add_class('box-flex2')
                #self.progress.add_class('box-flex1')
            else:
                self.exps = exponents(self.fnow, self.current_gradient)
                print('Running {} Code:'.format(self.opt_name))
                print('  {3:7s}   {0:{mi}s}   {1:11s}    {2:11s}'.format("i", "f", "|g|", "runtime", mi=self.len_maxiters))
示例#10
0
    def optimize(self, method='HS', maxiter=500, ftol=1e-6, gtol=1e-6, step_length=1., callback=None, verbose=True):
        """
        Optimize the model.

        The strategy is to run conjugate natural gradients on the variational
        parameters, interleaved with gradient based optimization of any
        non-variational parameters. self.hyperparam_interval dictates how
        often this happens.

        Arguments
        ---------
        :method: ['FR', 'PR','HS','steepest'] -- conjugate gradient method
        :maxiter: int
        :ftol: float
        :gtol: float
        :step_length: float

        """

        assert method in ['FR', 'PR','HS','steepest'], 'invalid conjugate gradient method specified.'

        ## For GPy style notebook verbosity

        if verbose:
            try:
                from IPython.display import display
                from IPython.html.widgets import IntProgress, HTML, Box, VBox, HBox, FlexBox
                self.text = HTML(width='100%')
                self.progress = IntProgress(min=0, max=maxiter)
                self.progress.bar_style = 'info'
                self.status = 'Running'

                html_begin = """<style type="text/css">
                .tg-opt  {font-family:"Courier New", Courier, monospace !important;padding:2px 3px;word-break:normal;border-collapse:collapse;border-spacing:0;border-color:#DCDCDC;margin:0px auto;width:100%;}
                .tg-opt td{font-family:"Courier New", Courier, monospace !important;font-weight:bold;color:#444;background-color:#F7FDFA;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#DCDCDC;}
                .tg-opt th{font-family:"Courier New", Courier, monospace !important;font-weight:normal;color:#fff;background-color:#26ADE4;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#DCDCDC;}
                .tg-opt .tg-left{font-family:"Courier New", Courier, monospace !important;font-weight:normal;text-align:left;}
                .tg-opt .tg-right{font-family:"Courier New", Courier, monospace !important;font-weight:normal;text-align:right;}
                </style>
                <table class="tg-opt">"""

                html_end = "</table>"

                self.ipython_notebook = True
            except:
                # Not in Ipython notebook
                self.ipython_notebook = False
        else:
            self.ipython_notebook = False

        if self.ipython_notebook:
            left_col = VBox(children=[self.progress, self.text], padding=2, width='100%')
            self.hor_align = FlexBox(children = [left_col], width='100%', orientation='horizontal')

            display(self.hor_align)

            try:
                self.text.set_css('width', '100%')
                left_col.set_css({
                         'padding': '2px',
                         'width': "100%",
                         })

                self.hor_align.set_css({
                         'width': "100%",
                         })

                self.hor_align.remove_class('vbox')
                self.hor_align.add_class('hbox')

                left_col.add_class("box-flex1")

            except:
                pass

        self.start = time.time()
        self._time = self.start

        ## ---

        iteration = 0
        bound_old = self.bound()
        searchDir_old = 0.
        iteration_failed = False
        while True:

            if callback is not None:
                callback()

            grad,natgrad = self.vb_grad_natgrad()
            grad,natgrad = -grad,-natgrad
            squareNorm = np.dot(natgrad,grad) # used to monitor convergence

            #find search direction
            if (method=='steepest') or not iteration:
                beta = 0
            elif (method=='PR'):
                beta = np.dot((natgrad-natgrad_old),grad)/squareNorm_old
            elif (method=='FR'):
                beta = squareNorm/squareNorm_old
            elif (method=='HS'):
                beta = np.dot((natgrad-natgrad_old),grad)/np.dot((natgrad-natgrad_old),grad_old)
            if np.isnan(beta) or (beta < 0.):
                beta = 0.
            searchDir = -natgrad + beta*searchDir_old

            # Try a conjugate step
            phi_old = self.get_vb_param().copy()
            try:
                self.set_vb_param(phi_old + step_length*searchDir)
                bound = self.bound()
            except LinAlgError:
                self.set_vb_param(phi_old)
                bound = bound_old-1

            iteration += 1

            # Make sure there's an increase in the bound, else revert to steepest, which is guaranteed to increase the bound.
            # (It's the same as VBEM.)
            if bound < bound_old:
                searchDir = -natgrad
                try:
                    self.set_vb_param(phi_old + step_length*searchDir)
                    bound = self.bound()
                except LinAlgError:
                    import warnings
                    warnings.warn("Caught LinalgError in setting variational parameters, trying to continue with old parameter settings", LinAlgWarning)
                    self.set_vb_param(phi_old)
                    bound = self.bound()
                    iteration_failed = False
                iteration += 1


            if verbose:
                if self.ipython_notebook:

                    t = time.time()
                    seconds = t-self.start

                    self.status = 'Running'
                    self.progress.bar_style = 'info'

                    names_vals = [['conjugate gradient method', "{:s}".format(method)],
                                  ['runtime', "{:.1f}s".format(seconds)],
                                  ['evaluation', "{}".format(iteration)],
                                  ['objective', "{:12.5f}".format(-bound)],
                                  ['||gradient||', "{:12.5f}".format(float(squareNorm))],
                                  ['beta', "{:12.5f}".format(beta)],
                                  ['status', "{:s}".format(self.status)],
                                ]

                    html_body = ""
                    for name, val in names_vals:
                        html_body += "<tr>"
                        html_body += "<td class='tg-left'>{}</td>".format(name)
                        html_body += "<td class='tg-right'>{}</td>".format(val)
                        html_body += "</tr>"

                    self.progress.value = iteration
                    self.text.value = html_begin + html_body + html_end

                else:
                    print('\riteration '+str(iteration)+' bound='+str(bound) + ' grad='+str(squareNorm) + ', beta='+str(beta))
                    sys.stdout.flush()

            # Converged yet? try the parameters if so
            if np.abs(bound-bound_old) <= ftol:
                if verbose:
                    if self.ipython_notebook:
                        self.status = 'vb converged (ftol)'
                        names_vals[-1] = ['status', "{:s}".format(self.status)]

                        html_body = ""
                        for name, val in names_vals:
                            html_body += "<tr>"
                            html_body += "<td class='tg-left'>{}</td>".format(name)
                            html_body += "<td class='tg-right'>{}</td>".format(val)
                            html_body += "</tr>"

                        self.text.value = html_begin + html_body + html_end
                        self.progress.bar_style = 'success'

                    else:
                        print('vb converged (ftol)')

                if self.optimize_parameters() < 1e-1:
                    break

            if squareNorm <= gtol:
                if verbose:
                    if self.ipython_notebook:
                        self.status = 'vb converged (gtol)'
                        names_vals[-1] = ['status', "{:s}".format(self.status)]

                        html_body = ""
                        for name, val in names_vals:
                            html_body += "<tr>"
                            html_body += "<td class='tg-left'>{}</td>".format(name)
                            html_body += "<td class='tg-right'>{}</td>".format(val)
                            html_body += "</tr>"

                        self.text.value = html_begin + html_body + html_end
                        self.progress.bar_style = 'success'

                    else:
                        print('vb converged (gtol)')

                if self.optimize_parameters() < 1e-1:
                    break

            if iteration >= maxiter:
                if verbose:
                    if self.ipython_notebook:
                        self.status = 'maxiter exceeded'
                        names_vals[-1] = ['status', "{:s}".format(self.status)]

                        html_body = ""
                        for name, val in names_vals:
                            html_body += "<tr>"
                            html_body += "<td class='tg-left'>{}</td>".format(name)
                            html_body += "<td class='tg-right'>{}</td>".format(val)
                            html_body += "</tr>"

                        self.text.value = html_begin + html_body + html_end
                        self.progress.bar_style = 'warning'

                    else:
                        print('maxiter exceeded')
                break

            #store essentials of previous iteration
            natgrad_old = natgrad.copy() # copy: better safe than sorry.
            grad_old = grad.copy()
            searchDir_old = searchDir.copy()
            squareNorm_old = squareNorm

            # hyper param_optimisation
            if ((iteration >1) and not (iteration%self.hyperparam_interval)) or iteration_failed:
                self.optimize_parameters()

            bound_old = bound

        # Clean up temporary fields after optimization
        if self.ipython_notebook:
            del self.text
            del self.progress
            del self.hor_align
示例#11
0
    def optimize(self,
                 method='HS',
                 maxiter=500,
                 ftol=1e-6,
                 gtol=1e-6,
                 step_length=1.,
                 callback=None,
                 verbose=True):
        """
        Optimize the model.

        The strategy is to run conjugate natural gradients on the variational
        parameters, interleaved with gradient based optimization of any
        non-variational parameters. self.hyperparam_interval dictates how
        often this happens.

        Arguments
        ---------
        :method: ['FR', 'PR','HS','steepest'] -- conjugate gradient method
        :maxiter: int
        :ftol: float
        :gtol: float
        :step_length: float

        """

        assert method in ['FR', 'PR', 'HS', 'steepest'
                          ], 'invalid conjugate gradient method specified.'

        ## For GPy style notebook verbosity

        if verbose:
            try:
                from IPython.display import display
                from IPython.html.widgets import IntProgress, HTML, Box, VBox, HBox, FlexBox
                self.text = HTML(width='100%')
                self.progress = IntProgress(min=0, max=maxiter)
                self.progress.bar_style = 'info'
                self.status = 'Running'

                html_begin = """<style type="text/css">
                .tg-opt  {font-family:"Courier New", Courier, monospace !important;padding:2px 3px;word-break:normal;border-collapse:collapse;border-spacing:0;border-color:#DCDCDC;margin:0px auto;width:100%;}
                .tg-opt td{font-family:"Courier New", Courier, monospace !important;font-weight:bold;color:#444;background-color:#F7FDFA;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#DCDCDC;}
                .tg-opt th{font-family:"Courier New", Courier, monospace !important;font-weight:normal;color:#fff;background-color:#26ADE4;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#DCDCDC;}
                .tg-opt .tg-left{font-family:"Courier New", Courier, monospace !important;font-weight:normal;text-align:left;}
                .tg-opt .tg-right{font-family:"Courier New", Courier, monospace !important;font-weight:normal;text-align:right;}
                </style>
                <table class="tg-opt">"""

                html_end = "</table>"

                self.ipython_notebook = True
            except:
                # Not in Ipython notebook
                self.ipython_notebook = False
        else:
            self.ipython_notebook = False

        if self.ipython_notebook:
            left_col = VBox(children=[self.progress, self.text],
                            padding=2,
                            width='100%')
            self.hor_align = FlexBox(children=[left_col],
                                     width='100%',
                                     orientation='horizontal')

            display(self.hor_align)

            try:
                self.text.set_css('width', '100%')
                left_col.set_css({
                    'padding': '2px',
                    'width': "100%",
                })

                self.hor_align.set_css({
                    'width': "100%",
                })

                self.hor_align.remove_class('vbox')
                self.hor_align.add_class('hbox')

                left_col.add_class("box-flex1")

            except:
                pass

        self.start = time.time()
        self._time = self.start

        ## ---

        iteration = 0
        bound_old = self.bound()
        searchDir_old = 0.
        iteration_failed = False
        while True:

            if callback is not None:
                callback()

            grad, natgrad = self.vb_grad_natgrad()
            grad, natgrad = -grad, -natgrad
            squareNorm = np.dot(natgrad, grad)  # used to monitor convergence

            #find search direction
            if (method == 'steepest') or not iteration:
                beta = 0
            elif (method == 'PR'):
                beta = np.dot((natgrad - natgrad_old), grad) / squareNorm_old
            elif (method == 'FR'):
                beta = squareNorm / squareNorm_old
            elif (method == 'HS'):
                beta = np.dot((natgrad - natgrad_old), grad) / np.dot(
                    (natgrad - natgrad_old), grad_old)
            if np.isnan(beta) or (beta < 0.):
                beta = 0.
            searchDir = -natgrad + beta * searchDir_old

            # Try a conjugate step
            phi_old = self.get_vb_param().copy()
            try:
                self.set_vb_param(phi_old + step_length * searchDir)
                bound = self.bound()
            except LinAlgError:
                self.set_vb_param(phi_old)
                bound = bound_old - 1

            iteration += 1

            # Make sure there's an increase in the bound, else revert to steepest, which is guaranteed to increase the bound.
            # (It's the same as VBEM.)
            if bound < bound_old:
                searchDir = -natgrad
                try:
                    self.set_vb_param(phi_old + step_length * searchDir)
                    bound = self.bound()
                except LinAlgError:
                    import warnings
                    warnings.warn(
                        "Caught LinalgError in setting variational parameters, trying to continue with old parameter settings",
                        LinAlgWarning)
                    self.set_vb_param(phi_old)
                    bound = self.bound()
                    iteration_failed = False
                iteration += 1

            if verbose:
                if self.ipython_notebook:

                    t = time.time()
                    seconds = t - self.start

                    self.status = 'Running'
                    self.progress.bar_style = 'info'

                    names_vals = [
                        ['conjugate gradient method', "{:s}".format(method)],
                        ['runtime', "{:.1f}s".format(seconds)],
                        ['evaluation', "{}".format(iteration)],
                        ['objective', "{:12.5f}".format(-bound)],
                        ['||gradient||', "{:12.5f}".format(float(squareNorm))],
                        ['beta', "{:12.5f}".format(beta)],
                        ['status', "{:s}".format(self.status)],
                    ]

                    html_body = ""
                    for name, val in names_vals:
                        html_body += "<tr>"
                        html_body += "<td class='tg-left'>{}</td>".format(name)
                        html_body += "<td class='tg-right'>{}</td>".format(val)
                        html_body += "</tr>"

                    self.progress.value = iteration
                    self.text.value = html_begin + html_body + html_end

                else:
                    print('\riteration ' + str(iteration) + ' bound=' +
                          str(bound) + ' grad=' + str(squareNorm) + ', beta=' +
                          str(beta))
                    sys.stdout.flush()

            # Converged yet? try the parameters if so
            if np.abs(bound - bound_old) <= ftol:
                if verbose:
                    if self.ipython_notebook:
                        self.status = 'vb converged (ftol)'
                        names_vals[-1] = ['status', "{:s}".format(self.status)]

                        html_body = ""
                        for name, val in names_vals:
                            html_body += "<tr>"
                            html_body += "<td class='tg-left'>{}</td>".format(
                                name)
                            html_body += "<td class='tg-right'>{}</td>".format(
                                val)
                            html_body += "</tr>"

                        self.text.value = html_begin + html_body + html_end
                        self.progress.bar_style = 'success'

                    else:
                        print('vb converged (ftol)')

                if self.optimize_parameters() < 1e-1:
                    break

            if squareNorm <= gtol:
                if verbose:
                    if self.ipython_notebook:
                        self.status = 'vb converged (gtol)'
                        names_vals[-1] = ['status', "{:s}".format(self.status)]

                        html_body = ""
                        for name, val in names_vals:
                            html_body += "<tr>"
                            html_body += "<td class='tg-left'>{}</td>".format(
                                name)
                            html_body += "<td class='tg-right'>{}</td>".format(
                                val)
                            html_body += "</tr>"

                        self.text.value = html_begin + html_body + html_end
                        self.progress.bar_style = 'success'

                    else:
                        print('vb converged (gtol)')

                if self.optimize_parameters() < 1e-1:
                    break

            if iteration >= maxiter:
                if verbose:
                    if self.ipython_notebook:
                        self.status = 'maxiter exceeded'
                        names_vals[-1] = ['status', "{:s}".format(self.status)]

                        html_body = ""
                        for name, val in names_vals:
                            html_body += "<tr>"
                            html_body += "<td class='tg-left'>{}</td>".format(
                                name)
                            html_body += "<td class='tg-right'>{}</td>".format(
                                val)
                            html_body += "</tr>"

                        self.text.value = html_begin + html_body + html_end
                        self.progress.bar_style = 'warning'

                    else:
                        print('maxiter exceeded')
                break

            #store essentials of previous iteration
            natgrad_old = natgrad.copy()  # copy: better safe than sorry.
            grad_old = grad.copy()
            searchDir_old = searchDir.copy()
            squareNorm_old = squareNorm

            # hyper param_optimisation
            if ((iteration > 1) and not (iteration % self.hyperparam_interval)
                ) or iteration_failed:
                self.optimize_parameters()

            bound_old = bound

        # Clean up temporary fields after optimization
        if self.ipython_notebook:
            del self.text
            del self.progress
            del self.hor_align