<commit_before>from __future__ import print_function

from collections import defaultdict

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
from matplotlib.figure import Figure
from PyQt4.QtCore import Qt
from PyQt4.QtGui import (
    QBrush, QFrame, QLabel, QMainWindow, QPainter, QVBoxLayout, QWidget)
import numpy as np

from nfc.archive.archive import Archive
from nfc.ui.flow_layout import FlowLayout
from nfc.ui.multiselection import Multiselection
from nfc.ui.spectrogram_clip_figure import SpectrogramClipFigure as ClipFigure
from nfc.util.bunch import Bunch
from nfc.util.preferences import preferences as prefs
import nfc.util.classification_command_utils as command_utils


_SPACING_ASPECT_RATIO = 2
"""ratio of vertical clip spacing to minimum horizontal spacing."""


# TODO: Figure out where this belongs and put it there.
_CLIP_CLASS_NAME_COMPONENT_SEPARATOR = '.'


class ClipsWindow(QMainWindow):
    
    
    def __init__(
            self, parent, archive, station_name, detector_name, night,
            clip_class_name, command_set_name):
        
        super(ClipsWindow, self).__init__(parent)
        
        self.setAttribute(Qt.WA_DeleteOnClose)
        
        self._archive = archive
        
        self._init_classification_dict(command_set_name)
        
        self._create_ui()
        
        self.set_clips(station_name, detector_name, night, clip_class_name)
        
        date = str(self._date.day) + ' ' + self._date.strftime('%B %Y')
        title = '{:s} - {:s} - {:s} - {:s}'.format(
            self._archive.name, self._station_name, self._detector_name, date)
        self.setWindowTitle(title)
        
        
    def _init_classification_dict(self, command_set_name):
        
        if command_set_name is None:
            self._classification_dict = {}
            
        else:
            self._classification_dict = \
                self._create_classification_dict(command_set_name)
        
        
    def _create_classification_dict(self, command_set_name):
        
        command_sets = prefs.get('clipsWindow.commandSets')
        
        if command_sets is None:
            
            # TODO: Handle error.
            return {}
        
        command_set = command_sets.get(command_set_name)
        
        if command_set is None:
            
            # TODO: Handle error.
            return {}
            
        # Want to wind up with a mapping from commands to clip classes.
        # This will be the composition of the command set, which maps commands
        # to clip class name fragments, and a mapping from clip class name
        # fragments to clip classes. For each clip class name fragment
        # of a command set, there must be exactly one clip class whose name
        # ends with that fragment.
        
        classes = self._archive.clip_classes
        classes_dict = self._create_fragment_to_classes_dict(classes)
        classification_dict = {}
        
        for command, (fragment, all_) in command_set.iteritems():
            
            classes = classes_dict.get(fragment)
            
            if classes is None:
                # TODO: Handle error.
                pass
            
            elif len(classes) != 1:
                # TODO: Handle error.
                pass
            
            else:
                classification_dict[command] = (classes[0], all_)
            
#         for command, (clip_class, all_) in classification_dict.iteritems():
#             print(command, clip_class.name, all_)
            
        return classification_dict
        
        
    def _create_fragment_to_classes_dict(self, clip_classes):
        d = defaultdict(list)
        d[Archive.CLIP_CLASS_NAME_UNCLASSIFIED].append(None)
        for c in clip_classes:
            for f in self._get_clip_class_name_fragments(c.name):
                d[f].append(c)
        return d
    
    
    def _get_clip_class_name_fragments(self, name):
        sep = _CLIP_CLASS_NAME_COMPONENT_SEPARATOR
        parts = name.split(sep)
        n = len(parts)
        return [sep.join(parts[i:n]) for i in xrange(n)]
    
    
    def _create_ui(self):
        
        widget = QWidget(self)
#        self.setFrameStyle(QFrame.Shape.Panel | QFrame.Shadow.Sunken)
        <commit_msg>Addressed one to do item in clips_window.py.<commit_after>from __future__ import print_function

from collections import defaultdict

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
from matplotlib.figure import Figure
from PyQt4.QtCore import Qt
from PyQt4.QtGui import (
    QBrush, QFrame, QLabel, QMainWindow, QPainter, QVBoxLayout, QWidget)
import numpy as np

from nfc.archive.archive import Archive
from nfc.ui.flow_layout import FlowLayout
from nfc.ui.multiselection import Multiselection
from nfc.ui.spectrogram_clip_figure import SpectrogramClipFigure as ClipFigure
from nfc.util.bunch import Bunch
from nfc.util.preferences import preferences as prefs
import nfc.util.classification_command_utils as command_utils


_SPACING_ASPECT_RATIO = 2
"""ratio of vertical clip spacing to minimum horizontal spacing."""


class ClipsWindow(QMainWindow):
    
    
    def __init__(
            self, parent, archive, station_name, detector_name, night,
            clip_class_name, command_set_name):
        
        super(ClipsWindow, self).__init__(parent)
        
        self.setAttribute(Qt.WA_DeleteOnClose)
        
        self._archive = archive
        
        self._init_classification_dict(command_set_name)
        
        self._create_ui()
        
        self.set_clips(station_name, detector_name, night, clip_class_name)
        
        date = str(self._date.day) + ' ' + self._date.strftime('%B %Y')
        title = '{:s} - {:s} - {:s} - {:s}'.format(
            self._archive.name, self._station_name, self._detector_name, date)
        self.setWindowTitle(title)
        
        
    def _init_classification_dict(self, command_set_name):
        
        if command_set_name is None:
            self._classification_dict = {}
            
        else:
            self._classification_dict = \
                self._create_classification_dict(command_set_name)
        
        
    def _create_classification_dict(self, command_set_name):
        
        command_sets = prefs.get('clipsWindow.commandSets')
        
        if command_sets is None:
            
            # TODO: Handle error.
            return {}
        
        command_set = command_sets.get(command_set_name)
        
        if command_set is None:
            
            # TODO: Handle error.
            return {}
            
        # Want to wind up with a mapping from commands to clip classes.
        # This will be the composition of the command set, which maps commands
        # to clip class name fragments, and a mapping from clip class name
        # fragments to clip classes. For each clip class name fragment
        # of a command set, there must be exactly one clip class whose name
        # ends with that fragment.
        
        classes = self._archive.clip_classes
        classes_dict = self._create_fragment_to_classes_dict(classes)
        classification_dict = {}
        
        for command, (fragment, all_) in command_set.iteritems():
            
            classes = classes_dict.get(fragment)
            
            if classes is None:
                # TODO: Handle error.
                pass
            
            elif len(classes) != 1:
                # TODO: Handle error.
                pass
            
            else:
                classification_dict[command] = (classes[0], all_)
            
#         for command, (clip_class, all_) in classification_dict.iteritems():
#             print(command, clip_class.name, all_)
            
        return classification_dict
        
        
    def _create_fragment_to_classes_dict(self, clip_classes):
        d = defaultdict(list)
        d[Archive.CLIP_CLASS_NAME_UNCLASSIFIED].append(None)
        for c in clip_classes:
            for f in self._get_clip_class_name_fragments(c.name):
                d[f].append(c)
        return d
    
    
    def _get_clip_class_name_fragments(self, name):
        sep = Archive.CLIP_CLASS_NAME_COMPONENT_SEPARATOR
        parts = name.split(sep)
        n = len(parts)
        return [sep.join(parts[i:n]) for i in xrange(n)]
    
    
    def _create_ui(self):
        
        widget = QWidget(self)
#        self.setFrameStyle(QFrame.Shape.Panel | QFrame.Shadow.Sunken)
        
{'fraction_non_alphanumeric': 0.0703225055303295, 'fraction_numerical': 0.0013971358714634998, 'mean_word_length': 2.059116809116809, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7456864', 'n_tokens_mistral': 2440, 'n_tokens_neox': 2380, 'n_words': 601}