<commit_before>from base import Video
import numpy as np
import cv2
import ipdb


class Reduced_chi(Video):


    def _get_initial_master(self, start, samples, alpha=0.02):
        '''
        Samples previous frames from the video to get an inital master frame
        
        start = frame number to start at
        samples = how many frames to sample
        alpha = regulates the weighting of the images
        
        '''
        # Get empty frame
        self.cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, start)
        sucess, initial_frame = self.cap.read()
        while not sucess:
            sucess, initial_frame = self.cap.read()
        initial_frame = np.float32(initial_frame)
        # Generate value within start and start+samples
        sam_array = np.arange(start, start+samples)
        count = 0.
        for frame_no in sam_array:
            # get frame
            sucess, frame = self.cap.read()
            if not sucess:
               continue
            cv2.accumulateWeighted(np.float32(frame), initial_frame, alpha)
            count += 1

        return initial_frame

    def parallel_moving_ave(self, frame_array, n_sample=120, alpha=0.02):
        '''Does same as moving_ave_chi but in parrallel'''
        # Check for master image
        if not 'moving_ave_frame' in vars(self):
            # Check if at zero
            if np.min(frame_array) - n_sample < 0:
                start = 0
            else:
                start = np.min(frame_array) - n_sample
            self.moving_ave_frame = self._get_initial_master(start, n_sample)
            self.frame_no = frame_array

        self.frame_chi = []
        self.frame_time = []
        # start chi square
        x,y,z = self.moving_ave_frame.shape
        ddof = float(x * y)
        for frame_no in frame_array:
            self.cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frame_no)
            success, frame = self.cap.read()
            if not success:
                # remove frame_no
                self.frame_no.pop(self.frame_no.index(frame_no))
                continue
            self.frame_time.append(self.cap.get(cv2.cv.CV_CAP_PROP_POS_MSEC))
            # Cal chi
            frame = np.float32(frame)
            # add 1 so chi square can't be nan or inf
            self.frame_chi.append(chisquare(frame+1,
                                            self.moving_ave_frame+1).sum()
                                                                     / ddof)              
            # Calculate new average frame
            cv2.accumulateWeighted(frame, self.moving_ave_frame, alpha)

        return self.frame_no, self.frame_time, self.frame_chi
        
    
    def moving_ave_chi(self, max_frames=-1, fps=1, alpha=0.02, plot=False):
        '''
        Uses moving average for master frame
        Calculates fps (frames per second)
        '''
        if max_frames < 1:
            max_frames = self.frames<commit_msg>fixed bug with missmached arrays<commit_after>from base import Video
import numpy as np
import cv2
import ipdb


class Reduced_chi(Video):


    def _get_initial_master(self, start, samples, alpha=0.02):
        '''
        Samples previous frames from the video to get an inital master frame
        
        start = frame number to start at
        samples = how many frames to sample
        alpha = regulates the weighting of the images
        
        '''
        # Get empty frame
        self.cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, start)
        sucess, initial_frame = self.cap.read()
        while not sucess:
            sucess, initial_frame = self.cap.read()
        initial_frame = np.float32(initial_frame)
        # Generate value within start and start+samples
        sam_array = np.arange(start, start+samples)
        count = 0.
        for frame_no in sam_array:
            # get frame
            sucess, frame = self.cap.read()
            if not sucess:
               continue
            cv2.accumulateWeighted(np.float32(frame), initial_frame, alpha)
            count += 1

        return initial_frame

    def parallel_moving_ave(self, frame_array, n_sample=120, alpha=0.02):
        '''Does same as moving_ave_chi but in parrallel'''
        # Check for master image
        if not 'moving_ave_frame' in vars(self):
            # Check if at zero
            if np.min(frame_array) - n_sample < 0:
                start = 0
            else:
                start = np.min(frame_array) - n_sample
            self.moving_ave_frame = self._get_initial_master(start, n_sample)
            
        self.frame_no = []
        self.frame_chi = []
        self.frame_time = []
        # start chi square
        x,y,z = self.moving_ave_frame.shape
        ddof = float(x * y)
        for frame_no in frame_array:
            self.cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frame_no)
            success, frame = self.cap.read()
            if not success:
                # remove frame_no
                #self.frame_no.pop(self.frame_no.index(frame_no))
                continue
            self.frame_time.append(self.cap.get(cv2.cv.CV_CAP_PROP_POS_MSEC))
            # Cal chi
            frame = np.float32(frame)
            self.frame_no.append(frame_no)
            # add 1 so chi square can't be nan or inf
            self.frame_chi.append(chisquare(frame+1,
                                            self.moving_ave_frame+1).sum()
                                                                     / ddof)              
            # Calculate new average frame
            cv2.accumulateWeighted(frame, self.moving_ave_frame, alpha)

        return self.frame_no, self.frame_time, self.frame_chi
        
    
    def moving_ave_chi(self, max_frames=-1, fps=1, alpha=0.02, plot=False):
        '''
        Uses moving average for master frame
        Calculates fps (frames per second)
        '''
        if max_frames < 1:
            max_frames = self.frames
{'fraction_non_alphanumeric': 0.07861952861952862, 'fraction_numerical': 0.011447811447811448, 'mean_word_length': 1.6665170556552962, 'pattern_counts': {'":': 0, '<': 7, '<?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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2828459', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1669, 'n_words': 431}