Example #1
0
    def __init__(self):

        # create the label and slider
        self.__label = Label('OFF')
        self.slider = slider = HorizontalSlider(0,
                                                5,
                                                step=1,
                                                StyleName="slider")
        slider.setDragable(True)
        slider.addControlValueListener(self)

        # put them in a hpanel
        self.hpanel = hpanel = HorizontalPanel(Spacing=10)
        hpanel.add(slider)
        hpanel.add(self.__label)

        # create the color panel and give it color
        self.colorpanel = CaptionPanel('Color:',
                                       SimplePanel(StyleName='colorpanel'))
        self.randomcolor()

        # we're initially off
        self.value = 0
        # create our timer
        self.timer = Timer(notify=self)
Example #2
0
    def __init__(self):

        # layed out in a grid with odd rows a different color for
        # visual separation
        grid = Grid(4, 3, CellPadding=50, CellSpacing=0)
        rf = grid.getRowFormatter()
        rf.setStyleName(1, 'oddrow')
        rf.setStyleName(3, 'oddrow')

        # the clock
        clock = Clock()
        grid.setWidget(
            0, 0, CaptionPanel('Using notify()',
                               clock.button,
                               StyleName='left'))
        grid.setWidget(0, 1, clock.datelabel)
        grid.setWidget(0, 2, HTML(Clock.__doc__, StyleName='desc'))

        # popup timer buttons
        ptb = PopupTimerButton(5)
        grid.setWidget(
            1, 0, CaptionPanel('Subclassing Timer()', ptb, StyleName='left'))
        grid.setWidget(1, 1, ptb.box)
        grid.setWidget(1, 2, HTML(PopupTimerButton.__doc__, StyleName='desc'))

        # the second instance
        ptb = PopupTimerButton(15)
        grid.setWidget(
            2, 0,
            CaptionPanel('Subclassing Timer()&nbsp;&nbsp;(<em>again</em>)',
                         ptb,
                         StyleName='left'))
        grid.setWidget(2, 1, ptb.box)
        grid.setWidget(
            2, 2,
            HTML('''This is the same as the previous
                                  example and is here to demonstrate
                                  creating multiple timers (each with
                                  their own state) which is difficult
                                  to do without sublcassing''',
                 StyleName='desc'))

        # random color
        randomcolor = RandomColor()
        grid.setWidget(
            3, 0,
            CaptionPanel('Using onTimer()',
                         randomcolor.hpanel,
                         StyleName='left'))
        grid.setWidget(3, 1, randomcolor.colorpanel)
        grid.setWidget(3, 2, HTML(RandomColor.__doc__, StyleName='desc'))

        # add it all to the root panel
        RootPanel().add(grid)

        # kickstart the slider handle (see above concerning a
        # potential bug)
        randomcolor.initialize()
Example #3
0
    def __init__(self):
        SimplePanel.__init__(self)

        contents = HTML("""<h3>The Zen of Python, by Tim Peters</h3>
<p>Beautiful is better than ugly.<br />
Explicit is better than implicit.<br />
Simple is better than complex.<br />
Complex is better than complicated.<br />
Flat is better than nested.<br />
Sparse is better than dense.<br />
Readability counts.<br />
Special cases aren't special enough to break the rules.<br />
Although practicality beats purity.<br />
Errors should never pass silently.<br />
Unless explicitly silenced.<br />
In the face of ambiguity, refuse the temptation to guess.<br />
There should be one-- and preferably only one --obvious way to do it.<br />
Although that way may not be obvious at first unless you're Dutch.<br />
Now is better than never.<br />
Although never is often better than *right* now.<br />
If the implementation is hard to explain, it's a bad idea.<br />
If the implementation is easy to explain, it may be a good idea.<br />
Namespaces are one honking great idea -- let's do more of those!</p>
""")

        panel = CaptionPanel("Caption-Panel", contents,
                             Width="300px")

        self.add(panel)
Example #4
0
    def onModuleLoad(self):

        v = VerticalPanel(Spacing=10)

        p = HorizontalPanel(Spacing=10,
                            VerticalAlignment=HasAlignment.ALIGN_BOTTOM)

        sc = SliderClass(False)
        p.add(CaptionPanel("clickable only", sc))
        sc = SliderClass(True)
        p.add(CaptionPanel("draggable", sc))
        sc = SliderClass(True)
        p.add(CaptionPanel("draggable", sc))

        v.add(CaptionPanel("Vertical Sliders with inputboxes", p))

        p = HorizontalPanel()
        p.setSpacing(10)
        p.setVerticalAlignment(HasAlignment.ALIGN_BOTTOM)

        sc = HSliderClass(False)
        p.add(CaptionPanel("clickable only", sc))
        sc = HSliderClass(True)
        p.add(CaptionPanel("draggable", sc))

        v.add(CaptionPanel("Horizontal Sliders with inputboxes", p))

        p = HorizontalPanel()
        p.setSpacing(10)
        p.setVerticalAlignment(HasAlignment.ALIGN_BOTTOM)

        sc = ASliderClass(False)
        p.add(CaptionPanel("clickable only", sc))
        sc = ASliderClass(True)
        p.add(CaptionPanel("draggable", sc))

        v.add(CaptionPanel("2D Controls: Inputboxes are draggable as well", p))

        RootPanel().add(v)
Example #5
0
    def __init__(self):

        # create the label and slider
        self.__label = Label('OFF')
        self.slider = slider = HorizontalSlider(0, 5, step=1,
                                                StyleName="slider")
        slider.setDragable(True)
        slider.addControlValueListener(self)

        # put them in a hpanel
        self.hpanel = hpanel = HorizontalPanel(Spacing=10)
        hpanel.add(slider)
        hpanel.add(self.__label)

        # create the color panel and give it color
        self.colorpanel = CaptionPanel('Color:',
                                       SimplePanel(StyleName='colorpanel'))
        self.randomcolor()

        # we're initially off
        self.value = 0
        # create our timer
        self.timer = Timer(notify=self)
Example #6
0
class RandomColor:

    __doc__ = '''This last example demonstrates what most pyjamas
    programmers currently do with timers: create a Timer instance
    specifying <code>notify</code> with an object that has an
    <code>onTimer</code> attribute that is callable.  The slider on
    the left will adjust how often the middle panel changes color; it
    is either OFF or a value of seconds from 1 to 5.  Changing the
    slider immediately cancels the current timer and starts a new
    timer to change the color in the newly specified time.  Like the
    previous example, this timer reschedules itself (if it wasn't
    turned off) at the end of the call to <code>onTimer()</code>.
    '''

    def __init__(self):

        # create the label and slider
        self.__label = Label('OFF')
        self.slider = slider = HorizontalSlider(0, 5, step=1,
                                                StyleName="slider")
        slider.setDragable(True)
        slider.addControlValueListener(self)

        # put them in a hpanel
        self.hpanel = hpanel = HorizontalPanel(Spacing=10)
        hpanel.add(slider)
        hpanel.add(self.__label)

        # create the color panel and give it color
        self.colorpanel = CaptionPanel('Color:',
                                       SimplePanel(StyleName='colorpanel'))
        self.randomcolor()

        # we're initially off
        self.value = 0
        # create our timer
        self.timer = Timer(notify=self)

    def initialize(self):

        # this method solves an apparent bug with the slider: the
        # slider doesn't draw its handle if the position is set before
        # showing, so instead of doing this in __init__ (where I
        # originally had it), this method gets called after it is
        # shown on the root panel.  See below when it gets called.
        self.slider.setValue(self.value)
        self.slider.setControlPos(self.value)

    def onTimer(self, timer):

        # when the timer fires we randomize the color and (maybe)
        # reschedule ourselves.
        self.randomcolor()
        v = self.value * 1000
        if v:
            self.timer.schedule(v)

    def onControlValueChanged(self, sender, old, new):

        # event handler for when the slider is moved.
        if new == self.value:
            return
        self.value = new

        # is it being turned off?
        if new == 0:
            self.__label.setText('OFF')
            self.timer.cancel()
        else:
            # no it's being reset
            self.__label.setText(str(new) + ' sec')
            self.onTimer(self.timer)
            
    def randomcolor(self):

        # randomize the color and set the panel accordingly
        r = random()*256
        g = random()*256
        b = random()*256
        e = self.colorpanel.getWidget().getElement()
        color = '#%02x%02x%02x' % (r, g, b)
        self.colorpanel.setCaption('Color: %s' % color)
        DOM.setStyleAttribute(e, "background", color)
Example #7
0
class RandomColor:

    __doc__ = '''This last example demonstrates what most pyjamas
    programmers currently do with timers: create a Timer instance
    specifying <code>notify</code> with an object that has an
    <code>onTimer</code> attribute that is callable.  The slider on
    the left will adjust how often the middle panel changes color; it
    is either OFF or a value of seconds from 1 to 5.  Changing the
    slider immediately cancels the current timer and starts a new
    timer to change the color in the newly specified time.  Like the
    previous example, this timer reschedules itself (if it wasn't
    turned off) at the end of the call to <code>onTimer()</code>.
    '''

    def __init__(self):

        # create the label and slider
        self.__label = Label('OFF')
        self.slider = slider = HorizontalSlider(0,
                                                5,
                                                step=1,
                                                StyleName="slider")
        slider.setDragable(True)
        slider.addControlValueListener(self)

        # put them in a hpanel
        self.hpanel = hpanel = HorizontalPanel(Spacing=10)
        hpanel.add(slider)
        hpanel.add(self.__label)

        # create the color panel and give it color
        self.colorpanel = CaptionPanel('Color:',
                                       SimplePanel(StyleName='colorpanel'))
        self.randomcolor()

        # we're initially off
        self.value = 0
        # create our timer
        self.timer = Timer(notify=self)

    def initialize(self):

        # this method solves an apparent bug with the slider: the
        # slider doesn't draw its handle if the position is set before
        # showing, so instead of doing this in __init__ (where I
        # originally had it), this method gets called after it is
        # shown on the root panel.  See below when it gets called.
        self.slider.setValue(self.value)
        self.slider.setControlPos(self.value)

    def onTimer(self, timer):

        # when the timer fires we randomize the color and (maybe)
        # reschedule ourselves.
        self.randomcolor()
        v = self.value * 1000
        if v:
            self.timer.schedule(v)

    def onControlValueChanged(self, sender, old, new):

        # event handler for when the slider is moved.
        if new == self.value:
            return
        self.value = new

        # is it being turned off?
        if new == 0:
            self.__label.setText('OFF')
            self.timer.cancel()
        else:
            # no it's being reset
            self.__label.setText(str(new) + ' sec')
            self.onTimer(self.timer)

    def randomcolor(self):

        # randomize the color and set the panel accordingly
        r = random() * 256
        g = random() * 256
        b = random() * 256
        e = self.colorpanel.getWidget().getElement()
        color = '#%02x%02x%02x' % (r, g, b)
        self.colorpanel.setCaption('Color: %s' % color)
        DOM.setStyleAttribute(e, "background", color)
Example #8
0
    def onModuleLoad(self):

        self.current_request = None

        Window.setTitle("Lattice Builder Web Interface")
        self.setStyleSheet("./LatBuilderWeb.css")
        self.includeMathJax('TeX-AMS-MML_HTMLorMML')

        self.TEXT_WAITING = "Lattice Builder is working..."
        self.TEXT_ERROR = "Server Error"
        self.FIGURES_OF_MERIT = [
            # (key, name)
            ('{cs}P2', 'P2'),
            ('{cs}P4', 'P4'),
            ('{cs}P6', 'P6'),
            ('{cs}R{alpha}', 'R_alpha'),
            ('spectral', 'spectral'),
        ]
        self.CONSTRUCTION_METHODS = [
            ('explicit:{genvec}', "Explicit (Evaluation)",
             "Evaluates the figure of merit for a given generating vector.<br/>"
             "<strong>Please specify the generating vector in the Lattice "
             "Properties panel above.</strong>"),
            ('exhaustive', "Exhaustive",
             "Examines all generating vectors and retains the best one."),
            ('random:{samples}', "Random",
             "Examines a number of randomly selected generating vectors and "
             "retains the best one."),
            ('Korobov', "Korobov",
             "Examines all generating vectors of the form (1, a, a^2 mod n, "
             "..., a^s mod n) and retains the best one."),
            ('random-Korobov:{samples}', "Random Korobov",
             "Examines a number of randomly selected generating vectors of "
             "the form (1, a, a^2 mod n, ..., a^s mod n) and retains the "
             "best one."),
            ('CBC', "Component-by-Component",
             "Examines all possible values of the components of the "
             "generating vector and selects the best ones, one coordinate "
             "at a time."),
            ('random-CBC:{samples}', "Random Component-by-Component",
             "Examines a number of randomly selected values of the "
             "components of the generating vector and selects the best "
             "ones, one coordinate at a time."),
            ('fast-CBC', "Fast Component-by-Component",
             "Examines all possible values of the components of the "
             "generating vector and selects the best ones, one coordinate "
             "at a time.  Computation is accelerated by using fast "
             "Fourier transforms."),
        ]
        self.COMBINER_TYPES = [
            ('level:max', 'highest level'),
            ('sum', 'weighted sum'),
            ('max', 'maximum weighted value'),
        ]
        self.NORMALIZATION_TYPES = [
            ('norm:P{alpha}-SL10', 'SL10 P-alpha'),
            ('norm:P{alpha}-DPW08', 'DPW08 P-alpha'),
        ]

        captionstyle = {
            'Width': '10em',
            'HorizontalAlignment': 'right',
        }

        self.remote = LatBuilderService()
        WeightValuesArray.REMOTE = self.remote

        main_panel = VerticalPanel(Spacing=30)

        # information

        info = """<h2>Lattice Builder Web Interface</h2>
        <p>This Web interface allows
        <a href="https://github.com/mungerd/latbuilder#readme">Lattice Builder</a>
        users to call the executable program without having to construct the
        command line explicitly.
        </p>
        <p>Enter the construction parameters below, and press the <em>Search for
        Good Lattices</em> button.  The results will show at the bottom.
        </p>"""

        main_panel.add(HTML(info))

        self.version_label = HTML()
        main_panel.add(self.version_label)
        self.remote.backend_version(self)

        params_panel = VerticalPanel(Spacing=15)
        main_panel.add(params_panel)

        # lattice type and size and dimension

        lat_panel = VerticalPanel()
        params_panel.add(CaptionPanel("Lattice Properties", lat_panel))
        lat_panel.add(
            HTML(
                r'\[ P_n = \left\{ (i \boldsymbol a \bmod n) / n \::\: i = 0, \dots, n \right\} \qquad (\boldsymbol a \in \mathbb Z^s) \]',
                StyleName='DisplayMath'))

        self.size = TextBox(Text="2^10")
        self.size.addChangeListener(self)

        self.embedded = CheckBox("embedded")
        self.embedded.addClickListener(self)

        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML(r"Size (\(n\)): ", StyleName="CaptionLabel"))
        panel.add(self.size)
        panel.add(self.embedded)
        lat_panel.add(panel)

        self.dimension = TextBox(Text="3")
        self.dimension.addChangeListener(self)

        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML(r"Dimension (\(s\)): ", StyleName="CaptionLabel"))
        panel.add(self.dimension)
        lat_panel.add(panel)

        self.generating_vector = GeneratingVector(self.size)
        self.generating_vector.panel.setVisible(False)
        lat_panel.add(self.generating_vector.panel)

        # figure of merit

        merit_panel = VerticalPanel()
        params_panel.add(CaptionPanel("Figure of Merit", merit_panel))
        merit_panel.add(
            HTML(
                r"\[ \left[ \mathcal D_q(P_n) \right]^q = "
                r"\sum_{\emptyset \neq u \subseteq \{1,\dots,s\}}"
                r"\gamma_u^q \, \left[\mathcal D_u(P_n)\right]^q"
                r"\qquad (q > 0) \]",
                StyleName='DisplayMath'))

        self.norm_type = TextBox(Text="2")
        self.norm_type.addChangeListener(self)

        panel = HorizontalPanel(Spacing=8)
        panel.add(
            HTML(r"Norm type (\(q\) or <b>inf</b>): ",
                 StyleName="CaptionLabel"))
        panel.add(self.norm_type)
        merit_panel.add(panel)

        self.merit = ListBox()
        self.merit.addChangeListener(self)
        for key, name in self.FIGURES_OF_MERIT:
            self.merit.addItem(name)

        self.merit_cs = CheckBox("Use coordinate-symmetric implementation",
                                 Checked=True)

        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML("Figure of merit: ", StyleName="CaptionLabel"))
        panel.add(self.merit)
        panel.add(self.merit_cs)
        merit_panel.add(panel)

        self.merit_alpha_panel = HorizontalPanel(Spacing=8)
        self.merit_alpha = TextBox(Text="2")
        self.merit_alpha_panel.add(
            HTML("Value of alpha: ", StyleName="CaptionLabel"))
        self.merit_alpha_panel.add(self.merit_alpha)
        merit_panel.add(self.merit_alpha_panel)

        # filters and combiner

        multilevel_panel = VerticalPanel(Spacing=8)
        self.multilevel_panel = CaptionPanel("Multilevel Filters and Combiner",
                                             multilevel_panel,
                                             Visible=False)
        params_panel.add(self.multilevel_panel)

        self.ml_normalization_enable = CheckBox("Normalization")
        self.ml_normalization_enable.addClickListener(self)
        multilevel_panel.add(self.ml_normalization_enable)

        self.ml_normalization_panel = VerticalPanel(Spacing=4,
                                                    Visible=False,
                                                    StyleName='SubPanel')
        multilevel_panel.add(self.ml_normalization_panel)

        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML("Normalization type: ", StyleName="CaptionLabel"))
        self.ml_normalization_type = ListBox()
        for key, name in self.NORMALIZATION_TYPES:
            self.ml_normalization_type.addItem(name, value=key)
        panel.add(self.ml_normalization_type)
        self.ml_normalization_panel.add(panel)

        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML("Minimum level: ", StyleName="CaptionLabel"))
        self.ml_min_level = TextBox(Text="1")
        panel.add(self.ml_min_level)
        self.ml_normalization_panel.add(panel)

        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML("Maximum level: ", StyleName="CaptionLabel"))
        self.ml_max_level = TextBox(Text="1")
        panel.add(self.ml_max_level)
        self.ml_normalization_panel.add(panel)

        self.ml_lowpass_enable = CheckBox("Low-pass filter")
        self.ml_lowpass_enable.addClickListener(self)
        multilevel_panel.add(self.ml_lowpass_enable)

        self.ml_lowpass_panel = VerticalPanel(Spacing=4,
                                              Visible=False,
                                              StyleName='SubPanel')
        multilevel_panel.add(self.ml_lowpass_panel)

        self.ml_lowpass = TextBox(Text="1.0")
        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML("Low-pass threshold: ", StyleName="CaptionLabel"))
        panel.add(self.ml_lowpass)
        self.ml_lowpass_panel.add(panel)

        self.combiner_type = ListBox()
        for key, name in self.COMBINER_TYPES:
            self.combiner_type.addItem(name, value=key)
        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML("Combiner: ", StyleName="CaptionLabel"))
        panel.add(self.combiner_type)
        multilevel_panel.add(panel)

        # weights

        self.weights = CompoundWeights()
        weights_panel = VerticalPanel()
        params_panel.add(CaptionPanel("Weights", weights_panel))
        weights_panel.add(
            HTML(r"\[ \gamma_u^p \qquad (u \subseteq \{1, \dots, s\}) \]",
                 StyleName='DisplayMath'))

        self.weights_power = TextBox(Text="2")
        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML(r"Weights power (\(p\)): ", StyleName="CaptionLabel"))
        panel.add(self.weights_power)

        weights_panel.add(panel)
        weights_panel.add(self.weights.panel)
        self.weights.add_weights(ProductWeights)

        # construction method

        cons_panel = VerticalPanel()
        params_panel.add(CaptionPanel("Construction Method", cons_panel))

        self.construction = ListBox()
        self.construction.addChangeListener(self)
        for key, name, desc in self.CONSTRUCTION_METHODS:
            self.construction.addItem(name, value=key)
        self.construction_desc = HTML()

        panel = HorizontalPanel(Spacing=8)
        panel.add(self.construction)
        panel.add(self.construction_desc)
        cons_panel.add(panel)

        self.construction_samples_panel = HorizontalPanel(Spacing=8)
        self.construction_samples = TextBox(Text="30")
        self.construction_samples_panel.add(
            HTML("Random samples: ", StyleName="CaptionLabel"))
        self.construction_samples_panel.add(self.construction_samples)
        cons_panel.add(self.construction_samples_panel)

        # execute button

        panel = VerticalPanel(Spacing=8,
                              Width="100%",
                              HorizontalAlignment='center')
        main_panel.add(panel)

        button_panel = HorizontalPanel()
        panel.add(button_panel)
        self.button_search = Button("Search", self)
        button_panel.add(self.button_search)
        self.button_abort = Button("Abort", self, Visible=False)
        button_panel.add(self.button_abort)

        self.status = Label()
        panel.add(self.status)

        # results

        results_panel = VerticalPanel()
        self.results_panel = CaptionPanel("Results",
                                          results_panel,
                                          Visible=False)
        main_panel.add(self.results_panel)

        self.results_size = Label()
        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML("Lattice size: ", StyleName="ResultsCaptionLabel"))
        panel.add(self.results_size)
        results_panel.add(panel)

        self.results_gen = Label()
        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML("Generating vector: ", StyleName="ResultsCaptionLabel"))
        panel.add(self.results_gen)
        results_panel.add(panel)

        self.results_merit = Label()
        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML("Merit value: ", StyleName="ResultsCaptionLabel"))
        panel.add(self.results_merit)
        results_panel.add(panel)

        self.results_cpu_time = Label()
        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML("CPU time: ", StyleName="ResultsCaptionLabel"))
        panel.add(self.results_cpu_time)
        results_panel.add(panel)

        self.results_cmd = Label(StyleName='Command', Visible=False)
        panel = HorizontalPanel(Spacing=8)

        self.results_cmd_link = Hyperlink("Command line: ",
                                          StyleName="ResultsCaptionLabel")
        self.results_cmd_link.addClickListener(self)
        panel.add(self.results_cmd_link)
        panel.add(self.results_cmd)
        results_panel.add(panel)

        # update selections

        self.construction.selectValue('CBC')

        self.onChange(self.size)
        self.onChange(self.construction)
        self.onChange(self.merit)
        self.onChange(self.dimension)
        self.onClick(self.embedded)
        self.onChange(self.ml_normalization_enable)
        self.onChange(self.ml_lowpass_enable)

        RootPanel().add(main_panel)
Example #9
0
class LatBuilderWeb:
    def setStyleSheet(self, sheet):
        e = DOM.createElement('link')
        e.setAttribute('rel', 'stylesheet')
        e.setAttribute('type', 'text/css')
        e.setAttribute('href', sheet)
        html = Window.getDocumentRoot().parentElement
        head = html.getElementsByTagName('head').item(0)
        head.appendChild(e)

    def includeMathJax(self, config):
        html = Window.getDocumentRoot().parentElement
        head = html.getElementsByTagName('head').item(0)

        e = DOM.createElement('script')
        e.setAttribute('type', 'text/javascript')
        e.setAttribute(
            'src', 'http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=' +
            config)
        head.appendChild(e)

        e = DOM.createElement('script')
        e.setAttribute('type', 'text/javascript')
        e.textContent = 'function Typeset() { MathJax.Hub.Queue(["Typeset",MathJax.Hub]); }'
        head.appendChild(e)

    def onModuleLoad(self):

        self.current_request = None

        Window.setTitle("Lattice Builder Web Interface")
        self.setStyleSheet("./LatBuilderWeb.css")
        self.includeMathJax('TeX-AMS-MML_HTMLorMML')

        self.TEXT_WAITING = "Lattice Builder is working..."
        self.TEXT_ERROR = "Server Error"
        self.FIGURES_OF_MERIT = [
            # (key, name)
            ('{cs}P2', 'P2'),
            ('{cs}P4', 'P4'),
            ('{cs}P6', 'P6'),
            ('{cs}R{alpha}', 'R_alpha'),
            ('spectral', 'spectral'),
        ]
        self.CONSTRUCTION_METHODS = [
            ('explicit:{genvec}', "Explicit (Evaluation)",
             "Evaluates the figure of merit for a given generating vector.<br/>"
             "<strong>Please specify the generating vector in the Lattice "
             "Properties panel above.</strong>"),
            ('exhaustive', "Exhaustive",
             "Examines all generating vectors and retains the best one."),
            ('random:{samples}', "Random",
             "Examines a number of randomly selected generating vectors and "
             "retains the best one."),
            ('Korobov', "Korobov",
             "Examines all generating vectors of the form (1, a, a^2 mod n, "
             "..., a^s mod n) and retains the best one."),
            ('random-Korobov:{samples}', "Random Korobov",
             "Examines a number of randomly selected generating vectors of "
             "the form (1, a, a^2 mod n, ..., a^s mod n) and retains the "
             "best one."),
            ('CBC', "Component-by-Component",
             "Examines all possible values of the components of the "
             "generating vector and selects the best ones, one coordinate "
             "at a time."),
            ('random-CBC:{samples}', "Random Component-by-Component",
             "Examines a number of randomly selected values of the "
             "components of the generating vector and selects the best "
             "ones, one coordinate at a time."),
            ('fast-CBC', "Fast Component-by-Component",
             "Examines all possible values of the components of the "
             "generating vector and selects the best ones, one coordinate "
             "at a time.  Computation is accelerated by using fast "
             "Fourier transforms."),
        ]
        self.COMBINER_TYPES = [
            ('level:max', 'highest level'),
            ('sum', 'weighted sum'),
            ('max', 'maximum weighted value'),
        ]
        self.NORMALIZATION_TYPES = [
            ('norm:P{alpha}-SL10', 'SL10 P-alpha'),
            ('norm:P{alpha}-DPW08', 'DPW08 P-alpha'),
        ]

        captionstyle = {
            'Width': '10em',
            'HorizontalAlignment': 'right',
        }

        self.remote = LatBuilderService()
        WeightValuesArray.REMOTE = self.remote

        main_panel = VerticalPanel(Spacing=30)

        # information

        info = """<h2>Lattice Builder Web Interface</h2>
        <p>This Web interface allows
        <a href="https://github.com/mungerd/latbuilder#readme">Lattice Builder</a>
        users to call the executable program without having to construct the
        command line explicitly.
        </p>
        <p>Enter the construction parameters below, and press the <em>Search for
        Good Lattices</em> button.  The results will show at the bottom.
        </p>"""

        main_panel.add(HTML(info))

        self.version_label = HTML()
        main_panel.add(self.version_label)
        self.remote.backend_version(self)

        params_panel = VerticalPanel(Spacing=15)
        main_panel.add(params_panel)

        # lattice type and size and dimension

        lat_panel = VerticalPanel()
        params_panel.add(CaptionPanel("Lattice Properties", lat_panel))
        lat_panel.add(
            HTML(
                r'\[ P_n = \left\{ (i \boldsymbol a \bmod n) / n \::\: i = 0, \dots, n \right\} \qquad (\boldsymbol a \in \mathbb Z^s) \]',
                StyleName='DisplayMath'))

        self.size = TextBox(Text="2^10")
        self.size.addChangeListener(self)

        self.embedded = CheckBox("embedded")
        self.embedded.addClickListener(self)

        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML(r"Size (\(n\)): ", StyleName="CaptionLabel"))
        panel.add(self.size)
        panel.add(self.embedded)
        lat_panel.add(panel)

        self.dimension = TextBox(Text="3")
        self.dimension.addChangeListener(self)

        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML(r"Dimension (\(s\)): ", StyleName="CaptionLabel"))
        panel.add(self.dimension)
        lat_panel.add(panel)

        self.generating_vector = GeneratingVector(self.size)
        self.generating_vector.panel.setVisible(False)
        lat_panel.add(self.generating_vector.panel)

        # figure of merit

        merit_panel = VerticalPanel()
        params_panel.add(CaptionPanel("Figure of Merit", merit_panel))
        merit_panel.add(
            HTML(
                r"\[ \left[ \mathcal D_q(P_n) \right]^q = "
                r"\sum_{\emptyset \neq u \subseteq \{1,\dots,s\}}"
                r"\gamma_u^q \, \left[\mathcal D_u(P_n)\right]^q"
                r"\qquad (q > 0) \]",
                StyleName='DisplayMath'))

        self.norm_type = TextBox(Text="2")
        self.norm_type.addChangeListener(self)

        panel = HorizontalPanel(Spacing=8)
        panel.add(
            HTML(r"Norm type (\(q\) or <b>inf</b>): ",
                 StyleName="CaptionLabel"))
        panel.add(self.norm_type)
        merit_panel.add(panel)

        self.merit = ListBox()
        self.merit.addChangeListener(self)
        for key, name in self.FIGURES_OF_MERIT:
            self.merit.addItem(name)

        self.merit_cs = CheckBox("Use coordinate-symmetric implementation",
                                 Checked=True)

        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML("Figure of merit: ", StyleName="CaptionLabel"))
        panel.add(self.merit)
        panel.add(self.merit_cs)
        merit_panel.add(panel)

        self.merit_alpha_panel = HorizontalPanel(Spacing=8)
        self.merit_alpha = TextBox(Text="2")
        self.merit_alpha_panel.add(
            HTML("Value of alpha: ", StyleName="CaptionLabel"))
        self.merit_alpha_panel.add(self.merit_alpha)
        merit_panel.add(self.merit_alpha_panel)

        # filters and combiner

        multilevel_panel = VerticalPanel(Spacing=8)
        self.multilevel_panel = CaptionPanel("Multilevel Filters and Combiner",
                                             multilevel_panel,
                                             Visible=False)
        params_panel.add(self.multilevel_panel)

        self.ml_normalization_enable = CheckBox("Normalization")
        self.ml_normalization_enable.addClickListener(self)
        multilevel_panel.add(self.ml_normalization_enable)

        self.ml_normalization_panel = VerticalPanel(Spacing=4,
                                                    Visible=False,
                                                    StyleName='SubPanel')
        multilevel_panel.add(self.ml_normalization_panel)

        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML("Normalization type: ", StyleName="CaptionLabel"))
        self.ml_normalization_type = ListBox()
        for key, name in self.NORMALIZATION_TYPES:
            self.ml_normalization_type.addItem(name, value=key)
        panel.add(self.ml_normalization_type)
        self.ml_normalization_panel.add(panel)

        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML("Minimum level: ", StyleName="CaptionLabel"))
        self.ml_min_level = TextBox(Text="1")
        panel.add(self.ml_min_level)
        self.ml_normalization_panel.add(panel)

        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML("Maximum level: ", StyleName="CaptionLabel"))
        self.ml_max_level = TextBox(Text="1")
        panel.add(self.ml_max_level)
        self.ml_normalization_panel.add(panel)

        self.ml_lowpass_enable = CheckBox("Low-pass filter")
        self.ml_lowpass_enable.addClickListener(self)
        multilevel_panel.add(self.ml_lowpass_enable)

        self.ml_lowpass_panel = VerticalPanel(Spacing=4,
                                              Visible=False,
                                              StyleName='SubPanel')
        multilevel_panel.add(self.ml_lowpass_panel)

        self.ml_lowpass = TextBox(Text="1.0")
        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML("Low-pass threshold: ", StyleName="CaptionLabel"))
        panel.add(self.ml_lowpass)
        self.ml_lowpass_panel.add(panel)

        self.combiner_type = ListBox()
        for key, name in self.COMBINER_TYPES:
            self.combiner_type.addItem(name, value=key)
        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML("Combiner: ", StyleName="CaptionLabel"))
        panel.add(self.combiner_type)
        multilevel_panel.add(panel)

        # weights

        self.weights = CompoundWeights()
        weights_panel = VerticalPanel()
        params_panel.add(CaptionPanel("Weights", weights_panel))
        weights_panel.add(
            HTML(r"\[ \gamma_u^p \qquad (u \subseteq \{1, \dots, s\}) \]",
                 StyleName='DisplayMath'))

        self.weights_power = TextBox(Text="2")
        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML(r"Weights power (\(p\)): ", StyleName="CaptionLabel"))
        panel.add(self.weights_power)

        weights_panel.add(panel)
        weights_panel.add(self.weights.panel)
        self.weights.add_weights(ProductWeights)

        # construction method

        cons_panel = VerticalPanel()
        params_panel.add(CaptionPanel("Construction Method", cons_panel))

        self.construction = ListBox()
        self.construction.addChangeListener(self)
        for key, name, desc in self.CONSTRUCTION_METHODS:
            self.construction.addItem(name, value=key)
        self.construction_desc = HTML()

        panel = HorizontalPanel(Spacing=8)
        panel.add(self.construction)
        panel.add(self.construction_desc)
        cons_panel.add(panel)

        self.construction_samples_panel = HorizontalPanel(Spacing=8)
        self.construction_samples = TextBox(Text="30")
        self.construction_samples_panel.add(
            HTML("Random samples: ", StyleName="CaptionLabel"))
        self.construction_samples_panel.add(self.construction_samples)
        cons_panel.add(self.construction_samples_panel)

        # execute button

        panel = VerticalPanel(Spacing=8,
                              Width="100%",
                              HorizontalAlignment='center')
        main_panel.add(panel)

        button_panel = HorizontalPanel()
        panel.add(button_panel)
        self.button_search = Button("Search", self)
        button_panel.add(self.button_search)
        self.button_abort = Button("Abort", self, Visible=False)
        button_panel.add(self.button_abort)

        self.status = Label()
        panel.add(self.status)

        # results

        results_panel = VerticalPanel()
        self.results_panel = CaptionPanel("Results",
                                          results_panel,
                                          Visible=False)
        main_panel.add(self.results_panel)

        self.results_size = Label()
        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML("Lattice size: ", StyleName="ResultsCaptionLabel"))
        panel.add(self.results_size)
        results_panel.add(panel)

        self.results_gen = Label()
        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML("Generating vector: ", StyleName="ResultsCaptionLabel"))
        panel.add(self.results_gen)
        results_panel.add(panel)

        self.results_merit = Label()
        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML("Merit value: ", StyleName="ResultsCaptionLabel"))
        panel.add(self.results_merit)
        results_panel.add(panel)

        self.results_cpu_time = Label()
        panel = HorizontalPanel(Spacing=8)
        panel.add(HTML("CPU time: ", StyleName="ResultsCaptionLabel"))
        panel.add(self.results_cpu_time)
        results_panel.add(panel)

        self.results_cmd = Label(StyleName='Command', Visible=False)
        panel = HorizontalPanel(Spacing=8)

        self.results_cmd_link = Hyperlink("Command line: ",
                                          StyleName="ResultsCaptionLabel")
        self.results_cmd_link.addClickListener(self)
        panel.add(self.results_cmd_link)
        panel.add(self.results_cmd)
        results_panel.add(panel)

        # update selections

        self.construction.selectValue('CBC')

        self.onChange(self.size)
        self.onChange(self.construction)
        self.onChange(self.merit)
        self.onChange(self.dimension)
        self.onClick(self.embedded)
        self.onChange(self.ml_normalization_enable)
        self.onChange(self.ml_lowpass_enable)

        RootPanel().add(main_panel)

    def onChange(self, sender):

        if sender == self.construction:
            key, name, desc = \
                    self.CONSTRUCTION_METHODS[self.construction.getSelectedIndex()]
            self.construction_desc.setHTML(desc)
            self.construction_samples_panel.setVisible('{samples}' in key)
            if key.startswith('explicit'):
                self.generating_vector.panel.setVisible(True)
                self.button_search.setText("Evaluate Figure of Merit")
            else:
                self.generating_vector.panel.setVisible(False)
                self.button_search.setText("Search for Good Lattices")

        elif sender == self.merit:
            key, name = \
                    self.FIGURES_OF_MERIT[self.merit.getSelectedIndex()]
            self.merit_alpha_panel.setVisible('{alpha}' in key)
            self.merit_cs.setVisible('{cs}' in key)

        elif sender == self.size:
            max_level = LatSize(self.size.getText()).max_level
            if int(self.ml_min_level.getText()) > max_level:
                self.ml_min_level.setText(max_level)
            self.ml_max_level.setText(max_level)

        elif sender == self.dimension:
            # resize weights
            dimension = int(self.dimension.getText())
            self.generating_vector.dimension = dimension
            self.weights.dimension = dimension

        elif sender == self.norm_type:
            q = self.norm_type.getText().strip()
            self.merit_cs.setVisible(q == '2')
            if q == 'inf':
                self.weights_power.setText('1')
            else:
                self.weights_power.setText(q)

    def onClick(self, sender):
        if sender == self.embedded:
            self.multilevel_panel.setVisible(self.embedded.getChecked())

        elif sender == self.ml_normalization_enable:
            self.ml_normalization_panel.setVisible(
                self.ml_normalization_enable.getChecked())

        elif sender == self.ml_lowpass_enable:
            self.ml_lowpass_panel.setVisible(
                self.ml_lowpass_enable.getChecked())

        elif sender == self.results_cmd_link:
            self.results_cmd.setVisible(not self.results_cmd.getVisible())

        elif sender == self.button_search:

            self.results_panel.setVisible(False)
            self.button_search.setVisible(False)
            self.button_abort.setVisible(True)

            lattype = self.embedded.getChecked() and 'embedded' or 'ordinary'
            size = self.size.getText()
            dimension = self.dimension.getText()

            norm_type = self.norm_type.getText()
            merit, merit_name = \
                    self.FIGURES_OF_MERIT[self.merit.getSelectedIndex()]
            alpha = self.merit_alpha.getText()
            cs = norm_type == 2 and self.merit_cs.getChecked() and 'CS:' or ''

            weights_power = self.weights_power.getText()
            weights = [w.as_arg() for w in self.weights.weights]

            construction, construction_name, desc = \
                    self.CONSTRUCTION_METHODS[self.construction.getSelectedIndex()]
            samples = self.construction_samples.getText()
            genvec = ','.join(self.generating_vector.values)

            mlfilters = []
            combiner_type = None

            if self.embedded.getChecked():
                if self.ml_normalization_enable.getChecked():
                    ml_normalization_type, ml_normalization_name = \
                            self.NORMALIZATION_TYPES[self.ml_normalization_type.getSelectedIndex()]
                    ml_normalization_type += ':even:{},{}'.format(
                        self.ml_min_level.getText(),
                        self.ml_max_level.getText())
                    mlfilters.append(ml_normalization_type.format(alpha=alpha))
                if self.ml_lowpass_enable.getChecked():
                    mlfilters.append('low-pass:{}'.format(
                        self.ml_lowpass.getText()))

                combiner_type, combiner_name = \
                        self.COMBINER_TYPES[self.combiner_type.getSelectedIndex()]

            self.status.setText(self.TEXT_WAITING)

            self.current_request = self.remote.latbuilder_exec(
                lattype, size, dimension, norm_type,
                merit.format(alpha=alpha, cs=cs),
                construction.format(samples=samples, genvec=genvec), weights,
                weights_power, None, mlfilters, combiner_type, self)

        elif sender == self.button_abort:
            # Need to patch JSONService.sendRequest():
            #
            # return HTTPRequest().asyncPost(self.url, msg_data,
            #                                JSONResponseTextHandler(request_info)
            #                                False, self.content_type,
            #                                self.headers)
            if self.current_request:
                self.current_request.abort()
                self.current_request = None
            self.button_abort.setVisible(False)
            self.button_search.setVisible(True)

        elif sender == self.product_weights_expr_link:
            self.showDialog(self._product_weights_expr_dialog)

        elif sender == self.order_weights_expr_link:
            self.showDialog(self._order_weights_expr_dialog)

    def onRemoteResponse(self, response, request_info):
        try:
            if request_info.method == 'latbuilder_exec':
                self.button_search.setVisible(True)
                self.button_abort.setVisible(False)
                cmd, points, gen, merit, seconds = eval(response)
                self.results_size.setText(points)
                self.results_gen.setText(', '.join(gen))
                self.results_merit.setText(merit)
                self.results_cpu_time.setText(format_time(seconds=seconds))
                self.results_cmd.setText(cmd)
                self.results_panel.setVisible(True)
                self.status.setText("")
            elif request_info.method == 'backend_version':
                version = response
                self.version_label.setHTML(
                    "<b>Backend:</b> {}".format(version))
        except:
            self.status.setText(response.replace('\n', '  |  '))

    def onRemoteError(self, code, errobj, request_info):
        if request_info.method == 'latbuilder_exec':
            self.button_search.setVisible(True)
            self.button_abort.setVisible(False)
        message = errobj['message']
        if code != 0:
            self.status.setText("HTTP error %d: %s" % (code, message['name']))
        else:
            code = errobj['code']
            if code == -32603:
                self.status.setText("Aborted.")
            else:
                self.status.setText("JSONRPC Error %s: %s" % (code, message))
Example #10
0
 def add_weights(self, wclass):
     self._weights.append(wclass(getattr(self, 'remove_weights')))
     self._list_panel.add(CaptionPanel(wclass.NAME,
                                       self._weights[-1].panel))
     self._weights[-1].dimension = self.dimension
Example #11
0
    def onRemoteResponse(self, response, request_info):
        mname = request_info.method
        if mname == "customize_message":
            showCustomizationResult(self, response, request_info)
            return

        if mname == "get_messagesdata_for_cust":
            locations_data = response["locations"]
            selectionbox = VerticalPanel(Padding=3)
            locations = ListBox()
            for (loc_name, loc_id) in locations_data:
                locations.addItem(loc_id, loc_name)
            messages = ListBox()
            messages.setName("locations")
            messages.addItem(location_select_label)
            for (name, d) in response["messages"].items():
                messages.addItem(d['label'], name)

            locations.addChangeListener(self)
            messages.addChangeListener(self)
            self.locations = locations
            self.messages = messages

            locationbox = HorizontalPanel()
            locationbox.add(Label("Location: ", StyleName="text", Width=80))
            locationbox.add(locations)

            msgnamebox = HorizontalPanel()
            msgnamebox.add(Label("Message: ", StyleName="text", Width=80))
            msgnamebox.add(messages)

            selectionbox.add(locationbox)
            selectionbox.add(msgnamebox)

            mainpanel = VerticalPanel(StyleName="dataBoxContent")
            mainpanel.add(selectionbox)
            self.mainpanel = mainpanel
            root = RootPanel()
            root.add(mainpanel)

        if mname == "get_messagecustdata":
            self.messages_data = response
            buttonspanel = FlowPanel(Spacing=1, Padding=1, Width=600)
            #buttonspanel.add(Label("Macros:", StyleName="text"))
            for macro_d in self.messages_data['macros']:
                macrobutton = Button(macro_d['label'], self, StyleName="buttonlikelink")#"nicebutton small")
                macrobutton.name = macro_d['name']
                buttonspanel.add(macrobutton)

            msgpanel = VerticalPanel(Padding=1, Spacing=1)
            messagebox = TextArea()
            messagebox.setCharacterWidth(70)
            height = len(self.messages_data["text"].split('\n')) + 1
            messagebox.setVisibleLines(height)
            messagebox.setText(self.messages_data["text"])
            messagebox.setName("textBoxFormElement")
            self.messagebox = messagebox
            msgpanel.add(messagebox)
            self.statusbar = Label(StyleName="errorMessage")
            msgpanel.add(self.statusbar)
            actionbuttons = HorizontalPanel(Spacing=2)
            updatebutton = Button("Update", self, StyleName="nicebutton small yellow")
            updatebutton.name = "update"
            actionbuttons.add(updatebutton)
            #actionbuttons.add(Button("Send me a preview mail"))
            msgpanel.add(actionbuttons)

            editorbox = VerticalPanel(Padding=1)
            editorbox.add(buttonspanel)
            editorbox.add(msgpanel)
            editorpanel = CaptionPanel("Message editor", editorbox, Padding=1, StyleName="text")
            editorpanel.name = "editorpanel"
            self.editorpanel = editorpanel

            self.mainpanel.add(editorpanel)