<s> import scipy . spatial as ss <EOL> from scipy . special import digamma , gamma <EOL> from math import log , pi <EOL> import numpy . random as nr <EOL> import crosscat . utils . data_utils as du <EOL> import crosscat . cython_code . State as State <EOL> import random <EOL> import numpy <EOL> def mi ( x , y , k = <NUM_LIT:3> , base = <NUM_LIT:2> ) : <EOL> """<STR_LIT>""" <EOL> x = [ [ entry ] for entry in x ] <EOL> y = [ [ entry ] for entry in y ] <EOL> assert len ( x ) == len ( y ) , "<STR_LIT>" <EOL> assert k <= len ( x ) - <NUM_LIT:1> , "<STR_LIT>" <EOL> intens = <NUM_LIT> <EOL> x = [ list ( p + intens * nr . rand ( len ( x [ <NUM_LIT:0> ] ) ) ) for p in x ] <EOL> y = [ list ( p + intens * nr . rand ( len ( y [ <NUM_LIT:0> ] ) ) ) for p in y ] <EOL> points = zip2 ( x , y ) <EOL> tree = ss . cKDTree ( points ) <EOL> dvec = [ tree . query ( point , k + <NUM_LIT:1> , p = float ( '<STR_LIT>' ) ) [ <NUM_LIT:0> ] [ k ] for point in points ] <EOL> a , b , c , d = avgdigamma ( x , dvec ) , avgdigamma ( y , dvec ) , digamma ( k ) , digamma ( len ( x ) ) <EOL> return ( - a - b + c + d ) / log ( base ) <EOL> def avgdigamma ( points , dvec ) : <EOL> N = len ( points ) <EOL> tree = ss . cKDTree ( points ) <EOL> avg = <NUM_LIT:0.> <EOL> for i in range ( N ) : <EOL> dist = dvec [ i ] <EOL> num_points = len ( tree . query_ball_point ( points [ i ] , dist - <NUM_LIT> , p = float ( '<STR_LIT>' ) ) ) <EOL> avg += digamma ( num_points ) / N <EOL> return avg <EOL> def zip2 ( * args ) : <EOL> return [ sum ( sublist , [ ] ) for sublist in zip ( * args ) ] <EOL> def generate_correlated_data ( num_rows , num_cols , means , corr , seed = <NUM_LIT:0> ) : <EOL> assert ( corr <= <NUM_LIT:1> and corr >= <NUM_LIT:0> ) <EOL> assert ( num_cols == len ( means ) ) <EOL> numpy . random . seed ( seed = seed ) <EOL> mu = numpy . array ( means ) <EOL> sigma = numpy . ones ( ( num_cols , num_cols ) , dtype = float ) * corr <EOL> for i in range ( num_cols ) : <EOL> sigma [ i , i ] = <NUM_LIT:1> <EOL> X = numpy . random . multivariate_normal ( mu , sigma , num_rows ) <EOL> return X <EOL> def generate_correlated_state ( num_rows , num_cols , num_views , num_clusters , mean_range , corr , seed = <NUM_LIT:0> ) : <EOL> assert ( num_clusters <= num_rows ) <EOL> assert ( num_views <= num_cols ) <EOL> T = numpy . zeros ( ( num_rows , num_cols ) ) <EOL> random . seed ( seed ) <EOL> numpy . random . seed ( seed = seed ) <EOL> get_next_seed = lambda : random . randrange ( <NUM_LIT> ) <EOL> cols_to_views = range ( num_views ) <EOL> view_counts = numpy . ones ( num_views , dtype = int ) <EOL> for i in range ( num_views , num_cols ) : <EOL> r = random . randrange ( num_views ) <EOL> cols_to_views . append ( r ) <EOL> view_counts [ r ] += <NUM_LIT:1> <EOL> random . shuffle ( cols_to_views ) <EOL> assert ( len ( cols_to_views ) == num_cols ) <EOL> assert ( max ( cols_to_views ) == num_views - <NUM_LIT:1> ) <EOL> row_to_clusters = [ ] <EOL> cluster_counts = [ ] <EOL> for view in range ( num_views ) : <EOL> row_to_cluster = range ( num_clusters ) <EOL> cluster_counts_i = numpy . ones ( num_clusters , dtype = int ) <EOL> for i in range ( num_clusters , num_rows ) : <EOL> r = random . randrange ( num_clusters ) <EOL> row_to_cluster . append ( r ) <EOL> cluster_counts_i [ r ] += <NUM_LIT:1> <EOL> random . shuffle ( row_to_cluster ) <EOL> assert ( len ( row_to_cluster ) == num_rows ) <EOL> assert ( max ( row_to_cluster ) == num_clusters - <NUM_LIT:1> ) <EOL> row_to_clusters . append ( row_to_cluster ) <EOL> cluster_counts . append ( cluster_counts_i ) <EOL> assert ( len ( row_to_clusters ) == num_views ) <EOL> for view in range ( num_views ) : <EOL> for cluster in range ( num_clusters ) : <EOL> cell_cols = view_counts [ view ] <EOL> cell_rows = cluster_counts [ view ] [ cluster ] <EOL> means = numpy . random . uniform ( - mean_range / <NUM_LIT> , mean_range / <NUM_LIT> , cell_cols ) <EOL> X = generate_correlated_data ( cell_rows , cell_cols , means , corr , seed = get_next_seed ( ) ) <EOL> col_indices = numpy . nonzero ( numpy . array ( cols_to_views ) == view ) [ <NUM_LIT:0> ] <EOL> row_indices = numpy . nonzero ( numpy . array ( row_to_clusters [ view ] ) == cluster ) [ <NUM_LIT:0> ] <EOL> for col in range ( cell_cols ) : <EOL> for row in range ( cell_rows ) : <EOL> r = row_indices [ row ] <EOL> c = col_indices [ col ] <EOL> T [ r , c ] = X [ row , col ] <EOL> M_c = du . gen_M_c_from_T ( T ) <EOL> M_r = du . gen_M_r_from_T ( T ) <EOL> X_L , X_D = generate_X_L_and_X_D ( T , M_c , cols_to_views , row_to_clusters , seed = get_next_seed ( ) ) <EOL> return T , M_c , M_r , X_L , X_D , cols_to_views <EOL> def generate_X_L_and_X_D ( T , M_c , cols_to_views , row_to_clusters , seed = <NUM_LIT:0> ) : <EOL> state = State . p_State ( M_c , T , SEED = seed ) <EOL> X_L = state . get_X_L ( ) <EOL> X_L [ '<STR_LIT>' ] [ '<STR_LIT>' ] = cols_to_views <EOL> state = State . p_State ( M_c , T , X_L = X_L , X_D = row_to_clusters , SEED = seed ) <EOL> X_L = state . get_X_L ( ) <EOL> X_D = state . get_X_D ( ) <EOL> return X_L , X_D </s>
