<commit_before>"""View for model and data pair."""
from traits.has_traits import HasTraits, on_trait_change
from traits.trait_numeric import Array
from traits.trait_types import Any, File, Instance, Button, Enum, Str, Range, Bool, Float, Event, List
from traitsui.editors.file_editor import FileEditor
from traitsui.editors.instance_editor import InstanceEditor
from traitsui.editors.tabular_editor import TabularEditor
from traitsui.group import HGroup, VGroup
from traitsui.item import Item, Spring
from traitsui.menu import OKCancelButtons
from traitsui.view import View
from pyanno.modelBt import ModelBt
from pyanno.ui.arrayview import Array2DAdapter
from pyanno.ui.model_bt_view import ModelBtView

import numpy as np

ANNOTATIONS_INFO_STR = """Annotations file {}
Number of annotations: {}
Number of annotators: {}
Labels: {}"""


# TODO fix size, scroll bar on second line
# TODO remember last setting of parameters
class NewModelDialog(HasTraits):
    model_name = Str
    nclasses = Range(low=3, high=50)

    def traits_view(self):
        traits_view = View(
            VGroup(
                Item(name='nclasses',
                     label='Number of annotation classes:',
                     id='test')
            ),
            buttons=OKCancelButtons,
            title='Create new ' + self.model_name,
            kind='modal'
        )
        return traits_view


class DataView(HasTraits):
    data = Array

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


class ModelDataView(HasTraits):

    model = Any
    model_view = Instance(ModelBtView)
    model_updated = Event
    model_update_suspended = Bool(False)

    annotations = Array(dtype=int, shape=(None, None))
    annotations_file = File
    annotations_updated = Event
    annotations_are_defined = Bool(False)

    annotations_info_str = Str


    @on_trait_change('annotations_updated,model_updated')
    def _update_info_str(self):
        if not self.annotations_are_defined:
            self.info_string = ('Please define an annotations list.')
        else:
            self.info_string = ('Model and annotations are defined.')

    @on_trait_change('annotations_updated')
    @on_trait_change('model,model:theta,model:gamma')
    def _fire_model_updated(self):
        if not self.model_update_suspended:
            self.model_updated = True


    ### Actions ##############################################################

    ml_estimate = Button(label='ML estimate...')
    map_estimate = Button(label='MAP estimate...')
    sample_theta_posterior = Button(label='Sample theta...')
    estimate_labels = Button(label='Estimate labels...')

    def _ml_estimate_fired(self):
        """Request data file and run ML estimation of parameters."""
        print 'Estimate...'
        model.mle(self.annotations, estimate_gamma=True)<commit_msg>NEW: load annotations action<commit_after>"""View for model and data pair."""
from traits.has_traits import HasTraits, on_trait_change
from traits.trait_numeric import Array
from traits.trait_types import Any, File, Instance, Button, Enum, Str, Range, Bool, Float, Event, List
from traitsui.editors.file_editor import FileEditor
from traitsui.editors.instance_editor import InstanceEditor
from traitsui.editors.tabular_editor import TabularEditor
from traitsui.group import HGroup, VGroup
from traitsui.item import Item, Spring
from traitsui.menu import OKCancelButtons
from traitsui.view import View
from pyanno.modelBt import ModelBt
from pyanno.ui.arrayview import Array2DAdapter
from pyanno.ui.model_bt_view import ModelBtView

import numpy as np

ANNOTATIONS_INFO_STR = """Annotations file {}
Number of annotations: {}
Number of annotators: {}
Labels: {}"""


# TODO fix size, scroll bar on second line
# TODO remember last setting of parameters
class NewModelDialog(HasTraits):
    model_name = Str
    nclasses = Range(low=3, high=50)

    def traits_view(self):
        traits_view = View(
            VGroup(
                Item(name='nclasses',
                     label='Number of annotation classes:',
                     id='test')
            ),
            buttons=OKCancelButtons,
            title='Create new ' + self.model_name,
            kind='modal'
        )
        return traits_view


class DataView(HasTraits):
    data = Array

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


class ModelDataView(HasTraits):

    model = Any
    model_view = Instance(ModelBtView)
    model_updated = Event
    model_update_suspended = Bool(False)

    annotations = Array(dtype=int, shape=(None, None))
    annotations_file = File
    annotations_updated = Event
    annotations_are_defined = Bool(False)

    annotations_info_str = Str


    @on_trait_change('annotations_file')
    def _update_annotations_file(self):
        print 'loading file', self.annotations_file
        # load file
        # I don't use np.loadtxt to allow for format 1,2,3,\n
        # (i.e., last number is followed by comma)
        # TODO function in util to load arbitrary formats
        # TODO generalize to load all possible data values (including string annotations)
        # TODO allow different values for missing annotations
        annotations = []
        with open(self.annotations_file) as fh:
            for n, line in enumerate(fh):
                line = line.strip().replace(',', '')
                annotations.append(np.fromstring(line, dtype=int, sep=' '))

        self.annotations = np.asarray(annotations, dtype=int)
        self.annotations[self.annotations>-1] -= 1
        self.annotations_are_defined = True
        self.annotations_updated = True

    @on_trait_change('annotations_updated,model_updated')

    @on_trait_change('annotations_updated,model_updated')
    def _update_info_str(self):
        if not self.annotations_are_defined:
            self.info_string = ('Please define an annotations list.')
        else:
            self.info_string = ('Model and annotations are defined.')

    @on_trait_change('annotations_updated')
    @on_trait_change('model,model:theta,model:gamma')
    def _fire_model_updated(self):
        if not self.model_update_suspended:
            self.model_updated = True


    ### Actions ##############################################################

    ml_estimate = Button(label='ML estimate...')
    map_estimate = Button(label='MAP estimate...')
    sample_theta_posterior = Button(label='Sample theta...')
    estimate_labels = Button(label='Estimate labels...')

    def _ml_estimate_fired(self):
        """Request data file and run ML estimation of parameters."""
        print 'Estimate...'
        model.mle(self.annotations, estimate_gamma=True)
{'fraction_non_alphanumeric': 0.10308347310024513, 'fraction_numerical': 0.0037414527157786093, 'mean_word_length': 2.5756457564575648, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2824763', 'n_tokens_mistral': 2110, 'n_tokens_neox': 2030, 'n_words': 526}