<commit_before>'''
Created on Dec 18, 2014

@author: <NAME> <<EMAIL>>
'''

from __future__ import division

import datetime

import cv2
import yaml

from .data_handler import DataHandler


class PassBase(DataHandler):
    """ base class for the actual video analysis passes """
    
    pass_id_replacements = {'first': '1',
                            'second': '2',
                            'third': '3',
                            'fourth': '4'}
    
    
    def __init__(self, *args, **kwargs):
        super(PassBase, self).__init__(*args, **kwargs)
        self.initialize_pass()


    def initialize_pass(self):
        """ initialize values necessary for this run """
        self.params = self.data['parameters']
        self.result = self.data.create_child(self.pass_name)
        self.result['code_status'] = self.get_code_status()
        self.debug = {}
        self._cache = {}
        if self.params['debug/output'] is None:
            self.debug_output = []
        else:
            self.debug_output = self.params['debug/output']
            
    
    def blur_image(self, image):
        """ returns a blurred version of the image """
        blur_sigma = self.params['video/blur_radius']
        blur_sigma_color = self.params['video/blur_sigma_color']

        if blur_sigma_color == 0:
            image_blurred = cv2.GaussianBlur(image, ksize=(0, 0),
                                             sigmaX=blur_sigma,
                                             borderType=cv2.BORDER_REPLICATE)
        
        else:
            image_blurred = cv2.bilateralFilter(image, d=int(blur_sigma),
                                                sigmaColor=blur_sigma_color,
                                                sigmaSpace=blur_sigma)
            
        return image_blurred
            
    
    def log_event(self, description):
        """ stores and/or outputs the time and date of the event given by name """
        self.logger.info(description)
        
        # save the event in the result structure
        if 'event_log' not in self.data:
            self.data['event_log'] = []
        event = str(datetime.datetime.now()) + ': ' + description 
        self.data['event_log'].append(event)

    
    def update_status(self, status_update):
        """ update the status file """
        path = self.get_filename('status.yaml', 'logging')
        with open(path, 'r+') as fp:
            # read data
            try:
                status = yaml.load(fp)
            except IOError:
                status = {}
            
            # update data
            status.update(status_update)
            
            # write data
            fp.seek(0)
            fp.truncate()
            yaml.dump(status, fp)
        
    
    def set_pass_status(self, **kwargs):
        """ update the status for the current pass """
        # make sure that we know the name of this pass
        pass_name = kwargs.pop('pass_name', self.pass_name)
                
        data = kwargs
        data['timestamp'] = str(datetime.datetime.now())
                
        self.update_status({pass_name: data})
        
        <commit_msg>Fixed bug occuring when status file was not present<commit_after>'''
Created on Dec 18, 2014

@author: <NAME> <<EMAIL>>
'''

from __future__ import division

import datetime

import cv2
import yaml

from .data_handler import DataHandler


class PassBase(DataHandler):
    """ base class for the actual video analysis passes """
    
    pass_id_replacements = {'first': '1',
                            'second': '2',
                            'third': '3',
                            'fourth': '4'}
    
    
    def __init__(self, *args, **kwargs):
        super(PassBase, self).__init__(*args, **kwargs)
        self.initialize_pass()


    def initialize_pass(self):
        """ initialize values necessary for this run """
        self.params = self.data['parameters']
        self.result = self.data.create_child(self.pass_name)
        self.result['code_status'] = self.get_code_status()
        self.debug = {}
        self._cache = {}
        if self.params['debug/output'] is None:
            self.debug_output = []
        else:
            self.debug_output = self.params['debug/output']
            
    
    def blur_image(self, image):
        """ returns a blurred version of the image """
        blur_sigma = self.params['video/blur_radius']
        blur_sigma_color = self.params['video/blur_sigma_color']

        if blur_sigma_color == 0:
            image_blurred = cv2.GaussianBlur(image, ksize=(0, 0),
                                             sigmaX=blur_sigma,
                                             borderType=cv2.BORDER_REPLICATE)
        
        else:
            image_blurred = cv2.bilateralFilter(image, d=int(blur_sigma),
                                                sigmaColor=blur_sigma_color,
                                                sigmaSpace=blur_sigma)
            
        return image_blurred
            
    
    def log_event(self, description):
        """ stores and/or outputs the time and date of the event given by name """
        self.logger.info(description)
        
        # save the event in the result structure
        if 'event_log' not in self.data:
            self.data['event_log'] = []
        event = str(datetime.datetime.now()) + ': ' + description 
        self.data['event_log'].append(event)

    
    def update_status(self, status_update):
        """ update the status file """
        path = self.get_filename('status.yaml', 'logging')

        # read data
        try:
            with open(path, 'r') as fp:
                status = yaml.load(fp)
        except IOError:
            status = {}
            
        # update data
        status.update(status_update)
            
        # write data
        with open(path, 'w') as fp:
            yaml.dump(status, fp)
        
    
    def set_pass_status(self, **kwargs):
        """ update the status for the current pass """
        # make sure that we know the name of this pass
        pass_name = kwargs.pop('pass_name', self.pass_name)
                
        data = kwargs
        data['timestamp'] = str(datetime.datetime.now())
                
        self.update_status({pass_name: data})
        
        
{'fraction_non_alphanumeric': 0.10323089046493301, 'fraction_numerical': 0.005516154452324665, 'mean_word_length': 1.59975419909873, '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': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4106799', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1660, 'n_words': 411}