def get_pattern_parts(self,E,phns,phn_times,s):
     """
     Parameters
     ----------
     E: array
         edgemap features
     phns: array
         strings representing the labels for the utterance
     phn_times: array
         times when the
     """
     feature_start, \
         feature_step, num_features =\
         esp._get_feature_label_times(s,
                                  self.num_window_samples,
                                  self.num_window_step_samples)
     feature_labels, \
         feature_label_transitions \
         = esp._get_labels(phn_times,
                       phns,
                       feature_start,
                       feature_step,
                       num_features,
                       self.sample_rate)
     pattern_part_times = []
     for pattern in self.patterns: pattern_part_times.append(
         esp.get_pattern_part_times(pattern,
                                    phns,
                                    feature_label_transitions))
     return [[E[:,phn_time[0]:phn_time[1]+1] for phn_time in part_time]\
                 for part_time in pattern_part_times]
 def get_patterns(self,E,phns,phn_times,s,
                  context=False,template_length=32):
     """
     Parameters
     ----------
     E: array
         edgemap features
     phns: array
         strings representing the labels for the utterance
     phn_times: array
         times when the
     """
     feature_start, \
         feature_step, num_features =\
         esp._get_feature_label_times(s,
                                  self.num_window_samples,
                                  self.num_window_step_samples)
     feature_labels, \
         feature_label_transitions \
         = esp._get_labels(phn_times,
                       phns,
                       feature_start,
                       feature_step,
                       num_features,
                       self.sample_rate)
     pattern_times = esp.get_pattern_times(self.patterns,
                                           phns,
                                           feature_label_transitions)
     if context:
         return [E[:,max(pattern_time[0]-template_length/3,0):\
                         min(pattern_time[0]+template_length+1+ (template_length)/3,E.shape[1])]\
                     for pattern_time in pattern_times]
     else:
         return [E[:,pattern_time[0]:pattern_time[1]+1]\
                     for pattern_time in pattern_times]
 def frame_to_phn_idx(self,frame_num,s,phn_times,phns):
     feature_start, \
         feature_step, num_features =\
         esp._get_feature_label_times(s,
                                  self.num_window_samples,
                                  self.num_window_step_samples)
     feature_labels, \
         feature_label_transitions \
         = esp._get_labels(phn_times,
                       phns,
                       feature_start,
                       feature_step,
                       num_features,
                       self.sample_rate)
     feature_label_transitions < frame_num
 def get_patterns_specs(self,S,phns,phn_times,s,
                        offset=3):
     first_window_s_avg, window_s_avg_step, num_windows = esp._get_spectrogram_label_times(s,
                                  self.num_window_samples,
                                  self.num_window_step_samples)
     spec_labels, \
         spec_label_transitions \
         = esp._get_labels(phn_times,
                       phns,
                       first_window_s_avg,
                       window_s_avg_step,
                       num_windows,
                       self.sample_rate)
     pattern_times = esp.get_pattern_times(self.patterns,
                                           phns,
                                           spec_label_transitions)
     return [S[:,pattern_time[0]-offset:pattern_time[1]+1+offset]\
                     for pattern_time in pattern_times]
 def get_pattern_times(self,phns,phn_times,s,
                  template_length=32):
     feature_start, \
         feature_step, num_features =\
         esp._get_feature_label_times(s,
                                  self.num_window_samples,
                                  self.num_window_step_samples)
     feature_labels, \
         feature_label_transitions \
         = esp._get_labels(phn_times,
                       phns,
                       feature_start,
                       feature_step,
                       num_features,
                       self.sample_rate)
     pattern_times = esp.get_pattern_times(self.patterns,
                                           phns,
                                           feature_label_transitions)
     return pattern_times
 def get_pattern_fronts_backs(self,E,phns,phn_times,s,bg_len,
                              context=False,part_radius=5):
     """
     Parameters
     ----------
     E: array
         edgemap features
     phns: array
         strings representing the labels for the utterance
     phn_times: array
         times when the
     """
     feature_start, \
         feature_step, num_features =\
         esp._get_feature_label_times(s,
                                  self.num_window_samples,
                                  self.num_window_step_samples)
     feature_labels, \
         feature_label_transitions \
         = esp._get_labels(phn_times,
                       phns,
                       feature_start,
                       feature_step,
                       num_features,
                       self.sample_rate)
     pattern_times = esp.get_pattern_times(self.patterns,
                                           phns,
                                           feature_label_transitions)
     if context:
         return [(E[:,max(pattern_time[0]-part_radius,0):\
                          min(pattern_time[0]+part_radius,E.shape[0])],\
                      E[:,max(pattern_time[1]-part_radius,0):\
                              min(pattern_time[1]+part_radius,E.shape[0])],\
                 pattern_time) for pattern_time in pattern_times]
     else:
         return [(E[:,max(pattern_time[0]-part_radius,0):\
                          min(pattern_time[0]+part_radius,E.shape[1])],\
                      E[:,max(pattern_time[1]-part_radius,0):\
                              min(pattern_time[1]+part_radius,E.shape[1])],\
                     pattern_time,part_radius) for pattern_time in pattern_times]
 def get_patterns_negative(self,E,phns,phn_times,s,length):
     """ Returns a negative example for the given pattern
     Parameters
     ----------
     E: array
         edgemap features
     phns: array
         strings representing the labels for the utterance
     phn_times: array
         times when the
     """
     feature_start, \
         feature_step, num_features =\
         esp._get_feature_label_times(s,
                                  self.num_window_samples,
                                  self.num_window_step_samples)
     feature_labels, \
         feature_label_transitions \
         = esp._get_labels(phn_times,
                       phns,
                       feature_start,
                       feature_step,
                       num_features,
                       self.sample_rate)
     pattern_times = esp.get_pattern_times(self.patterns,
                                           phns,
                                           feature_label_transitions)
     end_time = E.shape[1]-length-3
     start_time = length+3
     neg_pattern_time = np.random.randint(start_time,end_time)
     # make sure that its not close to the start time of a patter
     while self._within_pattern_tolerance(neg_pattern_time,
                                     pattern_times,
                                     length):
         neg_pattern_time = np.random.randint(start_time,end_time)
     return E[:,neg_pattern_time:neg_pattern_time+length].copy(), E[:,max(neg_pattern_time-length,0):neg_pattern_time].copy()
    def next(self,wait_for_positive_example=False,
             compute_S=True,
             compute_patterns_specs=False,
             compute_E=True,
             compute_patterns=False, compute_patterns_context=False,
             compute_bgds=False,
             compute_pattern_times=False,
             compute_pattern_parts=False,
             max_template_length = 40):
        """
        Processes the next speech utterance, or the next speech
        utterance that is a positive example.

        Sets the variables:
        self.E
        self.s
        self.phns
        self.phn_times
        self.patterns
        self.pattern_contexts

        Parameters:
        ------------
        wait_for_positive_example: bool
            cycle through the paths until one comes across a positive example

        compute_patterns: bool
            whether to get a list of pattern occurrences from the label set

        compute_patterns_context: bool
            whether to get the patterns with their surrounding context in a list
            for easier processing later

        compute_bgds: bool
            
        """
        if self.cur_data_pointer >= self.num_data-1:
            print "Reached end of data use reset method"
            return False
        else:
            self.cur_data_pointer +=1
        self.phns = self.get_phns(self.cur_data_pointer)
        if wait_for_positive_example:
            cur_data_pointer = self.cur_data_pointer
            no_positives = True
            for i in xrange(1,self.num_data-cur_data_pointer): 
                self.cur_data_pointer = cur_data_pointer + i
                self.phns = self.get_phns(self.cur_data_pointer)
                if self.has_pattern(self.phns):
                    no_positives = False
                    break
            if no_positives:
                print "Error: no positive examples left"
                return False
        self.s = self.get_s(self.cur_data_pointer)
        self.phn_times = self.get_phn_times(self.cur_data_pointer)
        if compute_S:
            self.S = self.get_processed_spec(self.s)
        if compute_patterns_specs:
            self.patterns_specs = self.get_patterns_specs(self.S,
                                                          self.phns,self.phn_times,self.s,
                                                          offset=self.offset)
        if compute_E:
            E,edge_feature_row_breaks,\
                edge_orientations= self.get_edgemap_no_threshold(self.s)
            self.E =E
            self.edge_feature_row_breaks = edge_feature_row_breaks
            self.edge_orientations = edge_orientations
        self.feature_start, \
            self.feature_step, self.num_features =\
            esp._get_feature_label_times(self.s,
                                         self.num_window_samples,
                                         self.num_window_step_samples)
        self.feature_labels, \
            self.feature_label_transitions \
            = esp._get_labels(self.phn_times,
                              self.phns,
                              self.feature_start,
                              self.feature_step,
                              self.num_features,
                              self.sample_rate)

        # select the object
        if compute_patterns:
            self.patterns = self.get_patterns(self.E,self.phns,self.phn_times,self.s)
        if compute_patterns_context:
            self.patterns_context = self.get_patterns(self.E,self.phns,self.phn_times,self.s,context=True,template_length=max_template_length)
        if compute_bgds:
            self.bgds = self.get_pattern_bgds(self.E,self.phns,self.phn_times,self.s,self.bg_len)
        if compute_pattern_times:
            self.pattern_times = self.get_pattern_times(
                                                        self.phns,
                                                        self.phn_times,
                                                        self.s,template_length=32)
        if compute_pattern_parts:
            self.pattern_parts = self.get_pattern_parts(self.E,self.phns,self.phn_times,self.s)
        return True