<s> import unittest <EOL> import pylab as pl <EOL> import matplotlib as mpl <EOL> import itertools <EOL> import sys <EOL> import math <EOL> import timeit <EOL> import copy <EOL> import time <EOL> import struct <EOL> import scipy . stats . mstats as stats <EOL> import ConfigParser <EOL> import os . path <EOL> import getopt <EOL> import h5py <EOL> from gmm_specializer . gmm import * <EOL> MINVALUEFORMINUSLOG = - <NUM_LIT> <EOL> class Diarizer ( object ) : <EOL> def __init__ ( self , f_file_name , sp_file_name ) : <EOL> f = open ( f_file_name , "<STR_LIT:rb>" ) <EOL> print "<STR_LIT>" <EOL> try : <EOL> nSamples = struct . unpack ( '<STR_LIT>' , f . read ( <NUM_LIT:4> ) ) [ <NUM_LIT:0> ] <EOL> sampPeriod = struct . unpack ( '<STR_LIT>' , f . read ( <NUM_LIT:4> ) ) [ <NUM_LIT:0> ] <EOL> sampSize = struct . unpack ( '<STR_LIT>' , f . read ( <NUM_LIT:2> ) ) [ <NUM_LIT:0> ] <EOL> sampKind = struct . unpack ( '<STR_LIT>' , f . read ( <NUM_LIT:2> ) ) [ <NUM_LIT:0> ] <EOL> print "<STR_LIT>" , nSamples <EOL> self . total_num_frames = nSamples <EOL> D = sampSize / <NUM_LIT:4> <EOL> l = [ ] <EOL> count = <NUM_LIT:0> <EOL> while count < ( nSamples * D ) : <EOL> bFloat = f . read ( <NUM_LIT:4> ) <EOL> fl = struct . unpack ( '<STR_LIT>' , bFloat ) [ <NUM_LIT:0> ] <EOL> l . append ( fl ) <EOL> count = count + <NUM_LIT:1> <EOL> finally : <EOL> f . close ( ) <EOL> print "<STR_LIT>" <EOL> pruned_list = [ ] <EOL> num_speech_frames = nSamples <EOL> if sp_file_name : <EOL> sp = open ( sp_file_name , "<STR_LIT:r>" ) <EOL> l_start = [ ] <EOL> l_end = [ ] <EOL> num_speech_frames = <NUM_LIT:0> <EOL> for line in sp : <EOL> s = line . split ( '<STR_LIT:U+0020>' ) <EOL> st = math . floor ( <NUM_LIT:100> * float ( s [ <NUM_LIT:2> ] ) + <NUM_LIT:0.5> ) <EOL> en = math . floor ( <NUM_LIT:100> * float ( s [ <NUM_LIT:3> ] . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) ) + <NUM_LIT:0.5> ) <EOL> st1 = int ( st ) <EOL> en1 = int ( en ) <EOL> l_start . append ( st1 * <NUM_LIT> ) <EOL> l_end . append ( en1 * <NUM_LIT> ) <EOL> num_speech_frames = num_speech_frames + ( en1 - st1 + <NUM_LIT:1> ) <EOL> print "<STR_LIT>" , num_speech_frames <EOL> total = <NUM_LIT:0> <EOL> for start in l_start : <EOL> end = l_end [ l_start . index ( start ) ] <EOL> total += ( end / <NUM_LIT> - start / <NUM_LIT> + <NUM_LIT:1> ) <EOL> x = <NUM_LIT:0> <EOL> index = start <EOL> while x < ( end - start + <NUM_LIT> ) : <EOL> pruned_list . append ( l [ index ] ) <EOL> index += <NUM_LIT:1> <EOL> x += <NUM_LIT:1> <EOL> else : <EOL> pruned_list = l <EOL> floatArray = np . array ( pruned_list , dtype = np . float32 ) <EOL> self . X = floatArray . reshape ( num_speech_frames , D ) <EOL> self . N = self . X . shape [ <NUM_LIT:0> ] <EOL> self . D = self . X . shape [ <NUM_LIT:1> ] <EOL> def write_to_RTTM ( self , rttm_file_name , sp_file_name , meeting_name , most_likely , num_gmms , seg_length ) : <EOL> print "<STR_LIT>" <EOL> duration = seg_length <EOL> chunk = <NUM_LIT:0> <EOL> end_chunk = duration <EOL> max_gmm_list = [ ] <EOL> smoothed_most_likely = np . array ( [ ] , dtype = np . float32 ) <EOL> while end_chunk < len ( most_likely ) : <EOL> chunk_arr = most_likely [ range ( chunk , end_chunk ) ] <EOL> max_gmm = stats . mode ( chunk_arr ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> max_gmm_list . append ( max_gmm ) <EOL> smoothed_most_likely = np . append ( smoothed_most_likely , max_gmm * np . ones ( <NUM_LIT> ) ) <EOL> chunk += duration <EOL> end_chunk += duration <EOL> end_chunk -= duration <EOL> if end_chunk < len ( most_likely ) : <EOL> chunk_arr = most_likely [ range ( end_chunk , len ( most_likely ) ) ] <EOL> max_gmm = stats . mode ( chunk_arr ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> max_gmm_list . append ( max_gmm ) <EOL> smoothed_most_likely = np . append ( smoothed_most_likely , max_gmm * np . ones ( len ( most_likely ) - end_chunk ) ) <EOL> most_likely = smoothed_most_likely <EOL> out_file = open ( rttm_file_name , '<STR_LIT:w>' ) <EOL> with_non_speech = - <NUM_LIT:1> * np . ones ( self . total_num_frames ) <EOL> if sp_file_name : <EOL> speech_seg = np . loadtxt ( sp_file_name , delimiter = '<STR_LIT:U+0020>' , usecols = ( <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> speech_seg_i = np . round ( speech_seg * <NUM_LIT:100> ) . astype ( '<STR_LIT>' ) <EOL> sizes = np . diff ( speech_seg_i ) <EOL> sizes = sizes . reshape ( sizes . size ) <EOL> offsets = np . cumsum ( sizes ) <EOL> offsets = np . hstack ( ( <NUM_LIT:0> , offsets [ <NUM_LIT:0> : - <NUM_LIT:1> ] ) ) <EOL> offsets += np . array ( range ( len ( offsets ) ) ) <EOL> speech_index = <NUM_LIT:0> <EOL> counter = <NUM_LIT:0> <EOL> for pair in speech_seg_i : <EOL> st = pair [ <NUM_LIT:0> ] <EOL> en = pair [ <NUM_LIT:1> ] <EOL> speech_index = offsets [ counter ] <EOL> counter += <NUM_LIT:1> <EOL> idx = <NUM_LIT:0> <EOL> for x in range ( st + <NUM_LIT:1> , en + <NUM_LIT:1> ) : <EOL> with_non_speech [ x ] = most_likely [ speech_index + idx ] <EOL> idx += <NUM_LIT:1> <EOL> else : <EOL> with_non_speech = most_likely <EOL> cnum = with_non_speech [ <NUM_LIT:0> ] <EOL> cst = <NUM_LIT:0> <EOL> cen = <NUM_LIT:0> <EOL> for i in range ( <NUM_LIT:1> , self . total_num_frames ) : <EOL> if with_non_speech [ i ] != cnum : <EOL> if ( cnum >= <NUM_LIT:0> ) : <EOL> start_secs = ( ( cst ) * <NUM_LIT> ) <EOL> dur_secs = ( cen - cst + <NUM_LIT:2> ) * <NUM_LIT> <EOL> out_file . write ( "<STR_LIT>" + meeting_name + "<STR_LIT>" + str ( start_secs ) + "<STR_LIT:U+0020>" + str ( dur_secs ) + "<STR_LIT>" + "<STR_LIT>" + str ( cnum ) + "<STR_LIT>" ) <EOL> cst = i <EOL> cen = i <EOL> cnum = with_non_speech [ i ] <EOL> else : <EOL> cen += <NUM_LIT:1> <EOL> if cst < cen : <EOL> cnum = with_non_speech [ self . total_num_frames - <NUM_LIT:1> ] <EOL> if ( cnum >= <NUM_LIT:0> ) : <EOL> start_secs = ( ( cst + <NUM_LIT:1> ) * <NUM_LIT> ) <EOL> dur_secs = ( cen - cst + <NUM_LIT:1> ) * <NUM_LIT> <EOL> out_file . write ( "<STR_LIT>" + meeting_name + "<STR_LIT>" + str ( start_secs ) + "<STR_LIT:U+0020>" + str ( dur_secs ) + "<STR_LIT>" + "<STR_LIT>" + str ( cnum ) + "<STR_LIT>" ) <EOL> print "<STR_LIT>" <EOL> def write_to_GMM ( self , gmmfile ) : <EOL> gmm_f = open ( gmmfile , '<STR_LIT:w>' ) <EOL> gmm_f . write ( "<STR_LIT>" + str ( len ( self . gmm_list ) ) + "<STR_LIT:\n>" ) <EOL> cluster_count = <NUM_LIT:0> <EOL> for gmm in self . gmm_list : <EOL> gmm_f . write ( "<STR_LIT>" + str ( cluster_count ) + "<STR_LIT:\n>" ) <EOL> means = gmm . components . means <EOL> covars = gmm . components . covars <EOL> weights = gmm . components . weights <EOL> gmm_f . write ( "<STR_LIT>" + str ( gmm . M ) + "<STR_LIT:\n>" ) <EOL> gmm_count = <NUM_LIT:0> <EOL> for g in range ( <NUM_LIT:0> , gmm . M ) : <EOL> g_means = means [ gmm_count ] <EOL> g_covar_full = covars [ gmm_count ] <EOL> g_covar = np . diag ( g_covar_full ) <EOL> g_weight = weights [ gmm_count ] <EOL> gmm_f . write ( "<STR_LIT>" + str ( gmm_count ) + "<STR_LIT:\n>" ) <EOL> gmm_f . write ( "<STR_LIT>" + str ( g_weight ) + "<STR_LIT:\n>" ) <EOL> for f in range ( <NUM_LIT:0> , gmm . D ) : <EOL> gmm_f . write ( "<STR_LIT>" + str ( f ) + "<STR_LIT>" + str ( g_means [ f ] ) + "<STR_LIT>" + str ( g_covar [ f ] ) + "<STR_LIT:\n>" ) <EOL> gmm_count += <NUM_LIT:1> <EOL> cluster_count += <NUM_LIT:1> <EOL> print "<STR_LIT>" <EOL> def new_gmm ( self , M , cvtype ) : <EOL> self . M = M <EOL> self . gmm = GMM ( self . M , self . D , cvtype = cvtype ) <EOL> def new_gmm_list ( self , M , K , cvtype ) : <EOL> self . M = M <EOL> self . init_num_clusters = K <EOL> self . gmm_list = [ GMM ( self . M , self . D , cvtype = cvtype ) for i in range ( K ) ] <EOL> def segment_majority_vote ( self , interval_size , em_iters ) : <EOL> num_clusters = len ( self . gmm_list ) <EOL> likelihoods = self . gmm_list [ <NUM_LIT:0> ] . score ( self . X ) <EOL> for g in self . gmm_list [ <NUM_LIT:1> : ] : <EOL> likelihoods = np . column_stack ( ( likelihoods , g . score ( self . X ) ) ) <EOL> if num_clusters == <NUM_LIT:1> : <EOL> most_likely = np . zeros ( len ( self . X ) ) <EOL> else : <EOL> most_likely = likelihoods . argmax ( axis = <NUM_LIT:1> ) <EOL> iter_training = { } <EOL> for i in range ( interval_size , self . N , interval_size ) : <EOL> arr = np . array ( most_likely [ ( range ( i - interval_size , i ) ) ] ) <EOL> max_gmm = int ( stats . mode ( arr ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> iter_training . setdefault ( ( self . gmm_list [ max_gmm ] , max_gmm ) , [ ] ) . append ( self . X [ i - interval_size : i , : ] ) <EOL> arr = np . array ( most_likely [ ( range ( ( self . N / interval_size ) * interval_size , self . N ) ) ] ) <EOL> max_gmm = int ( stats . mode ( arr ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> iter_training . setdefault ( ( self . gmm_list [ max_gmm ] , max_gmm ) , [ ] ) . append ( self . X [ ( self . N / interval_size ) * interval_size : self . N , : ] ) <EOL> iter_bic_dict = { } <EOL> iter_bic_list = [ ] <EOL> for gp , data_list in iter_training . iteritems ( ) : <EOL> g = gp [ <NUM_LIT:0> ] <EOL> p = gp [ <NUM_LIT:1> ] <EOL> cluster_data = data_list [ <NUM_LIT:0> ] <EOL> for d in data_list [ <NUM_LIT:1> : ] : <EOL> cluster_data = np . concatenate ( ( cluster_data , d ) ) <EOL> g . train ( cluster_data , max_em_iters = em_iters ) <EOL> iter_bic_list . append ( ( g , cluster_data ) ) <EOL> iter_bic_dict [ p ] = cluster_data <EOL> return iter_bic_dict , iter_bic_list , most_likely <EOL> def cluster ( self , em_iters , KL_ntop , NUM_SEG_LOOPS_INIT , NUM_SEG_LOOPS , seg_length ) : <EOL> print "<STR_LIT>" <EOL> main_start = time . time ( ) <EOL> per_cluster = self . N / self . init_num_clusters <EOL> init_training = zip ( self . gmm_list , np . vsplit ( self . X , range ( per_cluster , self . N , per_cluster ) ) ) <EOL> for g , x in init_training : <EOL> g . train ( x , max_em_iters = em_iters ) <EOL> for segment_iter in range ( <NUM_LIT:0> , NUM_SEG_LOOPS_INIT ) : <EOL> iter_bic_dict , iter_bic_list , most_likely = self . segment_majority_vote ( seg_length , em_iters ) <EOL> best_BIC_score = <NUM_LIT:1.0> <EOL> total_events = <NUM_LIT:0> <EOL> total_loops = <NUM_LIT:0> <EOL> while ( best_BIC_score > <NUM_LIT:0> and len ( self . gmm_list ) > <NUM_LIT:1> ) : <EOL> total_loops += <NUM_LIT:1> <EOL> for segment_iter in range ( <NUM_LIT:0> , NUM_SEG_LOOPS ) : <EOL> iter_bic_dict , iter_bic_list , most_likely = self . segment_majority_vote ( seg_length , em_iters ) <EOL> best_merged_gmm = None <EOL> best_BIC_score = <NUM_LIT:0.0> <EOL> merged_tuple = None <EOL> merged_tuple_indices = None <EOL> if KL_ntop > <NUM_LIT:0> : <EOL> top_K_gmm_pairs = self . gmm_list [ <NUM_LIT:0> ] . find_top_KL_pairs ( KL_ntop , self . gmm_list ) <EOL> for pair in top_K_gmm_pairs : <EOL> score = <NUM_LIT:0.0> <EOL> gmm1idx = pair [ <NUM_LIT:0> ] <EOL> gmm2idx = pair [ <NUM_LIT:1> ] <EOL> g1 = self . gmm_list [ gmm1idx ] <EOL> g2 = self . gmm_list [ gmm2idx ] <EOL> if gmm1idx in iter_bic_dict and gmm2idx in iter_bic_dict : <EOL> d1 = iter_bic_dict [ gmm1idx ] <EOL> d2 = iter_bic_dict [ gmm2idx ] <EOL> data = np . concatenate ( ( d1 , d2 ) ) <EOL> elif gmm1idx in iter_bic_dict : <EOL> data = iter_bic_dict [ gmm1idx ] <EOL> elif gmm2idx in iter_bic_dict : <EOL> data = iter_bic_dict [ gmm2idx ] <EOL> else : <EOL> continue <EOL> new_gmm , score = compute_distance_BIC ( g1 , g2 , data , em_iters ) <EOL> if score > best_BIC_score : <EOL> best_merged_gmm = new_gmm <EOL> merged_tuple = ( g1 , g2 ) <EOL> merged_tuple_indices = ( gmm1idx , gmm2idx ) <EOL> best_BIC_score = score <EOL> else : <EOL> l = len ( iter_bic_list ) <EOL> for gmm1idx in range ( l ) : <EOL> for gmm2idx in range ( gmm1idx + <NUM_LIT:1> , l ) : <EOL> score = <NUM_LIT:0.0> <EOL> g1 , d1 = iter_bic_list [ gmm1idx ] <EOL> g2 , d2 = iter_bic_list [ gmm2idx ] <EOL> data = np . concatenate ( ( d1 , d2 ) ) <EOL> new_gmm , score = compute_distance_BIC ( g1 , g2 , data , em_iters ) <EOL> if score > best_BIC_score : <EOL> best_merged_gmm = new_gmm <EOL> merged_tuple = ( g1 , g2 ) <EOL> merged_tuple_indices = ( gmm1idx , gmm2idx ) <EOL> best_BIC_score = score <EOL> if best_BIC_score > <NUM_LIT:0.0> : <EOL> gmms_with_events = [ ] <EOL> for gp in iter_bic_list : <EOL> gmms_with_events . append ( gp [ <NUM_LIT:0> ] ) <EOL> for g in self . gmm_list : <EOL> if g not in gmms_with_events and g != merged_tuple [ <NUM_LIT:0> ] and g != merged_tuple [ <NUM_LIT:1> ] : <EOL> self . gmm_list . remove ( g ) <EOL> self . gmm_list . remove ( merged_tuple [ <NUM_LIT:0> ] ) <EOL> self . gmm_list . remove ( merged_tuple [ <NUM_LIT:1> ] ) <EOL> self . gmm_list . append ( best_merged_gmm ) <EOL> print "<STR_LIT>" , [ g . M for g in self . gmm_list ] <EOL> print "<STR_LIT>" , time . time ( ) - main_start <EOL> print "<STR_LIT>" , [ g . M for g in self . gmm_list ] <EOL> return most_likely <EOL> def print_usage ( ) : <EOL> print """<STR_LIT>""" <EOL> def print_no_config ( ) : <EOL> print "<STR_LIT>" <EOL> return <EOL> def get_config_params ( config ) : <EOL> try : <EOL> meeting_name = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> except : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> try : <EOL> f = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> except : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> try : <EOL> sp = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> except : <EOL> print "<STR_LIT>" <EOL> sp = False <EOL> try : <EOL> outfile = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> except : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> try : <EOL> gmmfile = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> except : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> try : <EOL> num_gmms = int ( config . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> except : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> try : <EOL> num_comps = int ( config . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> except : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> try : <EOL> kl_ntop = int ( config . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> except : <EOL> kl_ntop = <NUM_LIT:0> <EOL> try : <EOL> num_seg_iters_init = int ( config . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> except : <EOL> num_seg_iters_init = <NUM_LIT:2> <EOL> try : <EOL> num_seg_iters = int ( config . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> except : <EOL> num_seg_iters = <NUM_LIT:3> <EOL> try : <EOL> num_em_iters = int ( config . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> except : <EOL> num_em_iters = <NUM_LIT:3> <EOL> try : <EOL> seg_length = int ( config . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> except : <EOL> seg_length = <NUM_LIT> <EOL> return meeting_name , f , sp , outfile , gmmfile , num_gmms , num_comps , num_em_iters , kl_ntop , num_seg_iters_init , num_seg_iters , seg_length <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> device_id = <NUM_LIT:0> <EOL> try : <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , "<STR_LIT>" , [ "<STR_LIT>" ] ) <EOL> except getopt . GetoptError , err : <EOL> print_no_config ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> config_file = '<STR_LIT>' <EOL> config_specified = False <EOL> for o , a in opts : <EOL> if o == '<STR_LIT:-c>' : <EOL> config_file = a <EOL> config_specified = True <EOL> if o == '<STR_LIT>' : <EOL> print_usage ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> if not config_specified : <EOL> print "<STR_LIT>" <EOL> else : <EOL> print "<STR_LIT>" , config_file , "<STR_LIT:'>" <EOL> try : <EOL> open ( config_file ) <EOL> except IOError , err : <EOL> print "<STR_LIT>" , config_file , "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> config = ConfigParser . ConfigParser ( ) <EOL> config . read ( config_file ) <EOL> meeting_name , f , sp , outfile , gmmfile , num_gmms , num_comps , num_em_iters , kl_ntop , num_seg_iters_init , num_seg_iters , seg_length = get_config_params ( config ) <EOL> diarizer = Diarizer ( f , sp ) <EOL> diarizer . new_gmm_list ( num_comps , num_gmms , '<STR_LIT>' ) <EOL> most_likely = diarizer . cluster ( num_em_iters , kl_ntop , num_seg_iters_init , num_seg_iters , seg_length ) <EOL> diarizer . write_to_RTTM ( outfile , sp , meeting_name , most_likely , num_gmms , seg_length ) <EOL> diarizer . write_to_GMM ( gmmfile ) </s>
