<commit_before>from __future__ import division

from PyQt4 import QtCore, QtGui
import numpy as np
from .contrast_ctrl_template import Ui_Form

class ContrastCtrl(QtGui.QWidget):
    """Widget for controlling contrast with rapidly updating image content.

    Provides:
    * image histogram
    * contrast control
    * color lookup table
    * automatic gain control
    * center weighted gain control
    * zoom-to-image button
    """
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)

        self.imageItem = None
        self.lastMinMax = None  ## Records most recently measured maximum/minimum image values
        self.autoGainLevels = [0.0, 1.0]
        self.ignoreLevelChange = False
        self.alpha = 1.0
        self.lastAGCMax = None

        ## Connect DisplayGain dock
        self.ui.histogram.sigLookupTableChanged.connect(self.levelsChanged)
        self.ui.histogram.sigLevelsChanged.connect(self.levelsChanged)
        self.ui.btnAutoGain.toggled.connect(self.toggleAutoGain)
        self.ui.btnAutoGain.setChecked(True)
        self.ui.zoomLiveBtn.clicked.connect(self.zoomToImage)
        self.ui.alphaSlider.valueChanged.connect(self.alphaChanged)
        self.ui.hideShowBtn.clicked.connect(self.hideShowImage)

    def setImageItem(self, item):
        """Sets the ImageItem that will be affected by the contrast / color controls
        """
        self.imageItem = item
        self.ui.histogram.setImageItem(item)
        self.ui.histogram.fillHistogram(False)  ## for speed

    def zoomToImage(self):
        """Zoom the image's view such that the image fills most of the view.
        """
        self.imageItem.getViewBox().autoRange(items=[self.imageItem])

    def levelsChanged(self):
        if self.ui.btnAutoGain.isChecked() and not self.ignoreLevelChange:
            if self.lastMinMax is None:
                return
            bl, wl = self.getLevels()
            mn, mx = self.lastMinMax
            rng = float(mx-mn)
            if rng == 0:
                return
            newLevels = [(bl-mn) / rng, (wl-mn) / rng]
            self.autoGainLevels = newLevels
        
    def alphaChanged(self, val):
        self.alpha = val / self.ui.alphaSlider.maximum() ## slider only works in integers and we need a 0 to 1 value
        self.imageItem.setOpacity(self.alpha)
    
    def hideShowImage(self):
        if self.alpha>0.5:
            self.alpha=0.
        if self.alpha<0.5:
            self.alpha=1.
        self.ui.alphaSlider.setTickPosition(self.alpha*self.ui.alphaSlider.maximum())
        self.imageItem.setOpacity(self.alpha)
    
    def getLevels(self):
        return self.ui.histogram.getLevels()

    def toggleAutoGain(self, b):
        if b:
            self.lastAGCMax = None
            self.ui.histogram.vb.setMouseEnabled(x=False, y=False)
        else:
            self.ui.histogram.vb.setMouseEnabled(x=False, y=True)

    def resetAutoGain(self):
        """Causes the AGC to immediately scale to the next frame that arrives. This is called
        when a sudden change in the image values is expected.
        """
        self.lastMinMax = None

    def processImage(self, data):
        # Update auto gain for new image
        # Note that histogram is linked to image item; this is what determines
        # the final appearance of the image.<commit_msg>bug fixes<commit_after>from __future__ import division

from PyQt4 import QtCore, QtGui
import numpy as np
from .contrast_ctrl_template import Ui_Form

class ContrastCtrl(QtGui.QWidget):
    """Widget for controlling contrast with rapidly updating image content.

    Provides:
    * image histogram
    * contrast control
    * color lookup table
    * automatic gain control
    * center weighted gain control
    * zoom-to-image button
    """
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)

        self.imageItem = None
        self.lastMinMax = None  ## Records most recently measured maximum/minimum image values
        self.autoGainLevels = [0.0, 1.0]
        self.ignoreLevelChange = False
        self.alpha = 1.0
        self.lastAGCMax = None

        ## Connect DisplayGain dock
        self.ui.histogram.sigLookupTableChanged.connect(self.levelsChanged)
        self.ui.histogram.sigLevelsChanged.connect(self.levelsChanged)
        self.ui.btnAutoGain.toggled.connect(self.toggleAutoGain)
        self.ui.btnAutoGain.setChecked(True)
        self.ui.zoomLiveBtn.clicked.connect(self.zoomToImage)
        self.ui.alphaSlider.valueChanged.connect(self.alphaChanged)
        self.ui.hideShowBtn.clicked.connect(self.hideShowImage)

    def setImageItem(self, item):
        """Sets the ImageItem that will be affected by the contrast / color controls
        """
        self.imageItem = item
        self.ui.histogram.setImageItem(item)
        self.ui.histogram.fillHistogram(False)  ## for speed

    def zoomToImage(self):
        """Zoom the image's view such that the image fills most of the view.
        """
        self.imageItem.getViewBox().autoRange(items=[self.imageItem])

    def levelsChanged(self):
        if self.ui.btnAutoGain.isChecked() and not self.ignoreLevelChange:
            if self.lastMinMax is None:
                return
            bl, wl = self.getLevels()
            mn, mx = self.lastMinMax
            rng = float(mx-mn)
            if rng == 0:
                return
            newLevels = [(bl-mn) / rng, (wl-mn) / rng]
            self.autoGainLevels = newLevels
        
    def alphaChanged(self, val):
        self.alpha = val / self.ui.alphaSlider.maximum() ## slider only works in integers and we need a 0 to 1 value
        self.imageItem.setOpacity(self.alpha)
    
    def hideShowImage(self):
        if self.alpha>0.5:
            self.alpha=0.
        elif self.alpha<0.5:
            self.alpha=1.
        self.ui.alphaSlider.setValue(int(self.alpha*self.ui.alphaSlider.maximum()))
        self.imageItem.setOpacity(self.alpha)
    
    def getLevels(self):
        return self.ui.histogram.getLevels()

    def toggleAutoGain(self, b):
        if b:
            self.lastAGCMax = None
            self.ui.histogram.vb.setMouseEnabled(x=False, y=False)
        else:
            self.ui.histogram.vb.setMouseEnabled(x=False, y=True)

    def resetAutoGain(self):
        """Causes the AGC to immediately scale to the next frame that arrives. This is called
        when a sudden change in the image values is expected.
        """
        self.lastMinMax = None

    def processImage(self, data):
        # Update auto gain for new image
        # Note that histogram is linked to image item; this is what determines
        # the final appearance of the image.
{'fraction_non_alphanumeric': 0.0908029197080292, 'fraction_numerical': 0.004671532846715329, 'mean_word_length': 2.319282945736434, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2117185', 'n_tokens_mistral': 2068, 'n_tokens_neox': 1997, 'n_words': 506}