<commit_before># Copyright (c) 2011, Enthought, Ltd.
# Author: <NAME> <<EMAIL>>
# License: Apache license

from traits.has_traits import HasTraits, on_trait_change
from traits.trait_types import Button, List, CFloat, Str, Range
from traitsui.api import View, Item, Group, VGroup, HGroup
from traitsui.editors.tabular_editor import TabularEditor
from traitsui.group import VGrid
from traitsui.include import Include
from traitsui.item import  Spring
from traitsui.menu import OKButton
from traits.api import Instance
import numpy as np
from pyanno.modelBt import ModelBt

from pyanno.ui.arrayview import Array2DAdapter
from pyanno.plots.hinton_plot import HintonDiagramPlot
from pyanno.plots.theta_plot import ThetaPlot
from pyanno.ui.model_view import PyannoModelView, NewModelDialog


MODEL_BT_NAME = 'Model B-with-theta'


class NewModelBtDialog(NewModelDialog):
    model_name = Str(MODEL_BT_NAME)
    nclasses = Range(low=3, high=50, value=5)

    parameters_group = VGroup(
        Item(name='nclasses',
             label='Number of annotation classes:')
    )


class GammaView(HasTraits):
    data = List

    def traits_view(self):
        return View(
            Group(Item('data',
                       editor=TabularEditor(
                           adapter=Array2DAdapter(ncolumns=len(self.data[0]),
                                                  format='%.4f',
                                                  show_index=False),
                           editable=False
                       ),
                       show_label=False)),
            title     = 'Model B-with-Theta, parameters gamma',
            width     = 500,
            height    = 200,
            resizable = True,
            buttons   = [OKButton]
            )

            )


class ModelBtView(PyannoModelView):
    """ Traits UI Model/View for 'ModelBt' objects.
    """

    model_name = Str(MODEL_BT_NAME)
    new_model_dialog_class = NewModelBtDialog

    @classmethod
    def _create_model_from_dialog(cls, dialog):
        return ModelBt.create_initial_state(dialog.nclasses)


    #### Model properties
    gamma = List(CFloat)


    @on_trait_change('model_updated')
    def update_from_model(self):
        """Update view parameters to the ones in the model."""
        self.gamma = self.model.gamma.tolist()
        self.theta_view.theta_samples_valid = False
        self.theta_view.redraw = True


    def _gamma_default(self):
        return self.model.gamma.tolist()


    @on_trait_change('gamma[]', post_init=True)
    def _update_gamma(self):
        self.model.gamma = np.asarray(self.gamma)
        self.gamma_hinton.data = self.gamma


    #### UI-related traits

    gamma_hinton = Instance(HintonDiagramPlot)

    theta_view = Instance(ThetaPlot)


    def _gamma_hinton_default(self):
        return HintonDiagramPlot(data = self.gamma,
                                 title='Gamma parameters, P(label=k)')


    def _theta_view_default(self):
        self.theta_view = ThetaPlot(model=self.model)
        self.theta_view._update_plot_data()
        return self.theta_view


    #### Actions

    view_gamma = Button(label='View...')

    view_theta = Button(label='View...')


    def _view_gamma_fired(self):
        """Create viewer for gamma parameters"""
        gamma_view = GammaView(data=[self.gamma])
        gamma_view.edit_traits()

    #### Traits UI view #########

    body = VGroup(
        Include('info_group'),
        VGrid(
            Item('handler.gamma_hinton',
                 style='custom',
                 resizable=False,
                 show_label=False,
                 width=300
            ),
            Item('handler.view_gamma', show_label=False),
            Item('handler.theta_view',
                 style='custom',
                 resizable=False,
                 show_label=False,
            ),
            Item('handler.view_theta',
                 show_label=False,
                 enabled_when='False'),
        )
    )

    traits_view = View(body, buttons=[OKButton], resizable=True)


#### Testing and debugging ####################################################
<commit_msg>NEW: theta viewer for model bt<commit_after># Copyright (c) 2011, Enthought, Ltd.
# Author: <NAME> <<EMAIL>>
# License: Apache license

from traits.has_traits import HasTraits, on_trait_change
from traits.trait_types import Button, List, CFloat, Str, Range
from traitsui.api import View, Item, Group, VGroup, HGroup
from traitsui.editors.tabular_editor import TabularEditor
from traitsui.group import VGrid
from traitsui.include import Include
from traitsui.item import  Spring
from traitsui.menu import OKButton
from traits.api import Instance
import numpy as np
from pyanno.modelBt import ModelBt

from pyanno.ui.arrayview import Array2DAdapter
from pyanno.plots.hinton_plot import HintonDiagramPlot
from pyanno.plots.theta_plot import ThetaPlot
from pyanno.ui.model_view import PyannoModelView, NewModelDialog


MODEL_BT_NAME = 'Model B-with-theta'


class NewModelBtDialog(NewModelDialog):
    model_name = Str(MODEL_BT_NAME)
    nclasses = Range(low=3, high=50, value=5)

    parameters_group = VGroup(
        Item(name='nclasses',
             label='Number of annotation classes:')
    )


class GammaView(HasTraits):
    data = List

    def traits_view(self):
        return View(
            Group(Item('data',
                       editor=TabularEditor(
                           adapter=Array2DAdapter(ncolumns=len(self.data[0]),
                                                  format='%.4f',
                                                  show_index=False),
                           editable=False
                       ),
                       show_label=False)),
            title     = 'Model B-with-Theta, parameters gamma',
            width     = 500,
            height    = 200,
            resizable = True,
            buttons   = [OKButton]
            )


class BtThetaView(HasTraits):
    data = List

    def traits_view(self):
        return View(
            Group(Item('data',
                       editor=TabularEditor(
                           adapter=Array2DAdapter(ncolumns=len(self.data[0]),
                                                  format='%.4f',
                                                  show_index=False),
                           editable=False
                       ),
                       show_label=False)),
            title     = 'Model B-with-Theta, parameters gamma',
            width     = 500,
            height    = 200,
            resizable = True,
            buttons   = [OKButton]
            )


class ModelBtView(PyannoModelView):
    """ Traits UI Model/View for 'ModelBt' objects.
    """

    model_name = Str(MODEL_BT_NAME)
    new_model_dialog_class = NewModelBtDialog

    @classmethod
    def _create_model_from_dialog(cls, dialog):
        return ModelBt.create_initial_state(dialog.nclasses)


    #### Model properties
    gamma = List(CFloat)


    @on_trait_change('model_updated')
    def update_from_model(self):
        """Update view parameters to the ones in the model."""
        self.gamma = self.model.gamma.tolist()
        self.theta_view.theta_samples_valid = False
        self.theta_view.redraw = True


    def _gamma_default(self):
        return self.model.gamma.tolist()


    @on_trait_change('gamma[]', post_init=True)
    def _update_gamma(self):
        self.model.gamma = np.asarray(self.gamma)
        self.gamma_hinton.data = self.gamma


    #### UI-related traits

    gamma_hinton = Instance(HintonDiagramPlot)

    theta_view = Instance(ThetaPlot)


    def _gamma_hinton_default(self):
        return HintonDiagramPlot(data = self.gamma,
                                 title='Gamma parameters, P(label=k)')


    def _theta_view_default(self):
        self.theta_view = ThetaPlot(model=self.model)
        self.theta_view._update_plot_data()
        return self.theta_view


    #### Actions

    view_gamma = Button(label='View...')

    view_theta = Button(label='View...')


    def _view_gamma_fired(self):
        """Create viewer for gamma parameters."""
        gamma_view = GammaView(data=[self.gamma])
        gamma_view.edit_traits()


    def _view_theta_fired(self):
        """Create viewer for theta parameters."""
        theta_view = BtThetaView(data=[self.model.theta.tolist()])
        theta_view.edit_traits()


    #### Traits UI view #########

    body = VGroup(
        Include('info_group'),
        VGrid(
            Item('handler.gamma_hinton',
                 style='custom',
                 resizable=False,
                 show_label=False,
                 width=300
            ),
            Item('handler.view_gamma', show_label=False),
            Item('handler.theta_view',
                 style='custom',
                 resizable=False,
                 show_label=False,
            ),
            Item('handler.view_theta', show_label=False),
        )
    )

    traits_view = View(body, buttons=[OKButton], resizable=True)


#### Testing and debugging ####################################################

{'fraction_non_alphanumeric': 0.10688524590163935, 'fraction_numerical': 0.005573770491803279, 'mean_word_length': 2.0832210242587603, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2236502', 'n_tokens_mistral': 2655, 'n_tokens_neox': 2488, 'n_words': 530}