<commit_before>from gen_mixins import SubjectMixin
import geometry
from moduleBase import moduleBase
from moduleMixins import introspectModuleMixin
import moduleUtils
import Measure2DFrame
reload(Measure2DFrame)

import vtk
import vtktud
import wx

class M2DMeasurementInfo:
    pass

class M2DWidget:
    """Class for encapsulating widget binding and all its metadata.
    """

    def __init__(self, widget, name, type_string):
        """
        @param type_string: ellipse
        """
        self.widget = widget
        self.name = name
        self.type_string = type_string
        self.measurement_string = ""
        # we'll use this to pack all widget-specific measurement
        # information
        self.measurement_info = M2DMeasurementInfo()


class M2DWidgetList:
    """List of M2DWidgets that can be queried by name or type.
    """

    def __init__(self):
        self._wdict = {}

    def close(self):
        pass

    def add(self, widget):
        """widget is an instance of M2DWidget.
        """

        if not widget.name:
            raise KeyError("Widget has to have a name.")

        if widget.name in self._wdict:
            raise KeyError("Widget with that name already in list.")

        self._wdict[widget.name] = widget


    def get_names(self):
        return self._wdict.keys()

    def get_widget(self, name):
        return self._wdict[name]

    def get_widgets_of_type(self, type_string):
        """Return a list of all widgets of type type_string.
        """

        wlist = []
        for wname, w in self._wdict.items():
            if w.type_string == type_string:
                wlist.append(w)

        return wlist

    def remove(self, name):
        """Remove widget with name from internal dict.
        Return binding to widget that was just removed (so that client
        can do widget specific finalisation.
        """

        w = self._wdict[name]
        del(self._wdict[name])
        
        
        return w
    
    def rename_widget(self, old_name, new_name):
        """After profuse error-checking, rename widget with old_name
        to new_name.
        """

        if not new_name:
            raise KeyError('Widget cannot have empty name.')

        if old_name not in self._wdict:
            raise KeyError('widget %s not in list.' % (old_name,))

        if new_name in self._wdict:
            raise KeyError('widget with name %s alread exists.' %
                    (new_name,))

        w = self.get_widget(old_name)
        self.remove(old_name)
        w.name = new_name
        self.add(w)
        

    def __contains__(self, name):
        """Returns true if there's a widget with that name already in
        the list.
        """

        return name in self._wdict

class Measure2D(introspectModuleMixin, moduleBase):
    def __init__(self, module_manager):
        moduleBase.__init__(self, module_manager)

        self._view_frame = None
        self._viewer = None
        self._input_image = None
        self._dummy_image_source = vtk.vtkImageMandelbrotSource()
        
        self._widgets = M2DWidgetList()

        # build frame
        self._view_frame = moduleUtils.instantiateModuleViewFrame(
            self, self._moduleManager, Measure2DFrame.Measure2DFrame)

        # now link up all event handlers
        self._bind_events()

        # then build VTK pipeline
        self._create_vtk_pipeline()

        # set us up with dummy input
        self._setup_new_image()

        # show everything
        self.view()

        
    def close(self):
        if self._view_frame is not None:
            self._view_frame.close()
            
    def view(self):
        self._view_frame.Show()
        self._view_frame.Raise()

        # GOTCHA!! (finally)
        # we need to do this to make sure that the Show() and Raise() above
        # are actually performed.  Not doing this is what resulted in the
        # "empty renderwindow" bug after module reloading, and also in the
        # fact that shortly after module creation dummy data rendered outside
        # the module frame.
        # YEAH.
        wx.SafeYield()

        self.render()

        # so if we bring up the view after having executed the network once,
        # re-executing will not do a set_input()!  (the scheduler doesn't
        # know that the module is now dirty)  Two solutions:
        # * make module dirty when view is activated
        # * activate view at instantiation. <--- we're doing this now.
        
    def get_input_descriptions(self):
        return ('Image data',)

    def get_output_descriptions(self):
        return ('self', 'widget_list')

    def get_output(self, idx):
        if idx == 0:
            return self
        else:
            return self._widgets

    def execute_module(self):
        pass

    def logic_to_config(self):
        pass

    def config_to_logic(self):
        pass
    
    def set_input(self, idx, input_stream):
        if self._input_image != input_stream:
            self._input_image = input_stream
            self._setup_new_image()
            
    def _bind_events(self):
        """Setup all event handling based on the view frame.
        """
                
        slice_slider = self._view_frame._image_control_panel.slider<commit_msg>Measure2D execute_module now calls render.<commit_after>from gen_mixins import SubjectMixin
import geometry
from moduleBase import moduleBase
from moduleMixins import introspectModuleMixin
import moduleUtils
import Measure2DFrame
reload(Measure2DFrame)

import vtk
import vtktud
import wx

class M2DMeasurementInfo:
    pass

class M2DWidget:
    """Class for encapsulating widget binding and all its metadata.
    """

    def __init__(self, widget, name, type_string):
        """
        @param type_string: ellipse
        """
        self.widget = widget
        self.name = name
        self.type_string = type_string
        self.measurement_string = ""
        # we'll use this to pack all widget-specific measurement
        # information
        self.measurement_info = M2DMeasurementInfo()


class M2DWidgetList:
    """List of M2DWidgets that can be queried by name or type.
    """

    def __init__(self):
        self._wdict = {}

    def close(self):
        pass

    def add(self, widget):
        """widget is an instance of M2DWidget.
        """

        if not widget.name:
            raise KeyError("Widget has to have a name.")

        if widget.name in self._wdict:
            raise KeyError("Widget with that name already in list.")

        self._wdict[widget.name] = widget


    def get_names(self):
        return self._wdict.keys()

    def get_widget(self, name):
        return self._wdict[name]

    def get_widgets_of_type(self, type_string):
        """Return a list of all widgets of type type_string.
        """

        wlist = []
        for wname, w in self._wdict.items():
            if w.type_string == type_string:
                wlist.append(w)

        return wlist

    def remove(self, name):
        """Remove widget with name from internal dict.
        Return binding to widget that was just removed (so that client
        can do widget specific finalisation.
        """

        w = self._wdict[name]
        del(self._wdict[name])
        
        
        return w
    
    def rename_widget(self, old_name, new_name):
        """After profuse error-checking, rename widget with old_name
        to new_name.
        """

        if not new_name:
            raise KeyError('Widget cannot have empty name.')

        if old_name not in self._wdict:
            raise KeyError('widget %s not in list.' % (old_name,))

        if new_name in self._wdict:
            raise KeyError('widget with name %s alread exists.' %
                    (new_name,))

        w = self.get_widget(old_name)
        self.remove(old_name)
        w.name = new_name
        self.add(w)
        

    def __contains__(self, name):
        """Returns true if there's a widget with that name already in
        the list.
        """

        return name in self._wdict

class Measure2D(introspectModuleMixin, moduleBase):
    def __init__(self, module_manager):
        moduleBase.__init__(self, module_manager)

        self._view_frame = None
        self._viewer = None
        self._input_image = None
        self._dummy_image_source = vtk.vtkImageMandelbrotSource()
        
        self._widgets = M2DWidgetList()

        # build frame
        self._view_frame = moduleUtils.instantiateModuleViewFrame(
            self, self._moduleManager, Measure2DFrame.Measure2DFrame)

        # now link up all event handlers
        self._bind_events()

        # then build VTK pipeline
        self._create_vtk_pipeline()

        # set us up with dummy input
        self._setup_new_image()

        # show everything
        self.view()

        
    def close(self):
        if self._view_frame is not None:
            self._view_frame.close()
            
    def view(self):
        self._view_frame.Show()
        self._view_frame.Raise()

        # GOTCHA!! (finally)
        # we need to do this to make sure that the Show() and Raise() above
        # are actually performed.  Not doing this is what resulted in the
        # "empty renderwindow" bug after module reloading, and also in the
        # fact that shortly after module creation dummy data rendered outside
        # the module frame.
        # YEAH.
        wx.SafeYield()

        self.render()

        # so if we bring up the view after having executed the network once,
        # re-executing will not do a set_input()!  (the scheduler doesn't
        # know that the module is now dirty)  Two solutions:
        # * make module dirty when view is activated
        # * activate view at instantiation. <--- we're doing this now.
        
    def get_input_descriptions(self):
        return ('Image data',)

    def get_output_descriptions(self):
        return ('self', 'widget_list')

    def get_output(self, idx):
        if idx == 0:
            return self
        else:
            return self._widgets

    def execute_module(self):
        self.render()

    def logic_to_config(self):
        pass

    def config_to_logic(self):
        pass
    
    def set_input(self, idx, input_stream):
        if self._input_image != input_stream:
            self._input_image = input_stream
            self._setup_new_image()
            
    def _bind_events(self):
        """Setup all event handling based on the view frame.
        """
                
        slice_slider = self._view_frame._image_control_panel.slider
{'fraction_non_alphanumeric': 0.07812206572769953, 'fraction_numerical': 0.0025352112676056337, 'mean_word_length': 2.031881582692855, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3193687', 'n_tokens_mistral': 3036, 'n_tokens_neox': 2913, 'n_words': 933}