<s> """<STR_LIT>""" <EOL> import random <EOL> import numpy <EOL> from PIL import Image , ImageDraw <EOL> import scipy . spatial . distance as distance <EOL> from pyRMSD . condensedMatrix import CondensedMatrix <EOL> def create_matrices ( data , verbose = False ) : <EOL> """<STR_LIT>""" <EOL> condensed_matrices = { } <EOL> all_observations = { } <EOL> for dataset_name in data . all_datasets : <EOL> dataset = data . all_datasets [ dataset_name ] <EOL> observations = dataset_loading_2D ( dataset , data . scale_factor [ dataset_name ] ) <EOL> all_observations [ dataset_name ] = observations <EOL> condensed_matrix_data = distance . pdist ( observations ) <EOL> condensed_matrix = CondensedMatrix ( condensed_matrix_data ) <EOL> condensed_matrices [ dataset_name ] = condensed_matrix <EOL> if verbose : <EOL> print "<STR_LIT>" % dataset_name <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" , condensed_matrix . calculateMax ( ) <EOL> print "<STR_LIT>" , condensed_matrix . calculateMin ( ) <EOL> print "<STR_LIT>" , condensed_matrix . calculateMean ( ) <EOL> print "<STR_LIT>" , condensed_matrix . calculateVariance ( ) <EOL> print "<STR_LIT>" <EOL> return condensed_matrices , all_observations <EOL> def params_to_string ( params ) : <EOL> """<STR_LIT>""" <EOL> s = "<STR_LIT>" <EOL> for k in params : <EOL> try : <EOL> value = params [ k ] <EOL> if isinstance ( value , ( int , long ) ) : <EOL> s += "<STR_LIT>" % ( k , value ) <EOL> else : <EOL> s += "<STR_LIT>" % ( k , value ) <EOL> except TypeError : <EOL> s += "<STR_LIT>" % ( k , params [ k ] ) <EOL> return s [ : - <NUM_LIT:1> ] <EOL> def dataset_loading_2D ( dataset_string , scale_factor = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> observations = [ ] <EOL> lines = dataset_string . split ( "<STR_LIT:\n>" ) <EOL> for l in lines : <EOL> try : <EOL> nums = l . split ( ) <EOL> observations . append ( ( float ( nums [ <NUM_LIT:0> ] ) * scale_factor , float ( nums [ <NUM_LIT:1> ] ) * scale_factor ) ) <EOL> except IndexError : <EOL> pass <EOL> except : <EOL> print "<STR_LIT>" <EOL> exit ( ) <EOL> return observations <EOL> def get_2D_bounding_box ( observations ) : <EOL> """<STR_LIT>""" <EOL> x = [ ] <EOL> y = [ ] <EOL> for o in observations : <EOL> x . append ( o [ <NUM_LIT:0> ] ) <EOL> y . append ( o [ <NUM_LIT:1> ] ) <EOL> return ( min ( x ) , min ( y ) ) , ( max ( x ) , max ( y ) ) <EOL> def create_canvas ( dataset_observations , scale , margin ) : <EOL> """<STR_LIT>""" <EOL> boundaries = get_2D_bounding_box ( dataset_observations ) <EOL> width = boundaries [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] - boundaries [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> height = boundaries [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] - boundaries [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> image = Image . new ( '<STR_LIT>' , ( int ( width * scale + <NUM_LIT:2> * margin ) , int ( height * scale + <NUM_LIT:2> * margin ) ) ) <EOL> draw = ImageDraw . Draw ( image ) <EOL> return ( image , draw , boundaries ) <EOL> def draw_point_into_canvas ( dataset_observations , i , draw , scale , margin , color , boundaries , print_number , shape_function = None ) : <EOL> """<STR_LIT>""" <EOL> position = ( ( dataset_observations [ i ] [ <NUM_LIT:0> ] - boundaries [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) * scale + margin , ( dataset_observations [ i ] [ <NUM_LIT:1> ] - boundaries [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) * scale + margin ) <EOL> if print_number : <EOL> draw . text ( position , str ( i ) , fill = color ) <EOL> if shape_function is None : <EOL> draw . point ( position ) <EOL> else : <EOL> boundaries = ( position [ <NUM_LIT:0> ] - int ( ( <NUM_LIT:0.5> * scale ) ) , position [ <NUM_LIT:1> ] - int ( ( <NUM_LIT:0.5> * scale ) ) , <EOL> position [ <NUM_LIT:0> ] + int ( ( <NUM_LIT:0.5> * scale ) ) , position [ <NUM_LIT:1> ] + int ( ( <NUM_LIT:0.5> * scale ) ) ) <EOL> shape_function ( draw , boundaries , color ) <EOL> return position <EOL> def show_2D_dataset ( dataset_observations , scale , margin = <NUM_LIT:0> , cutoff = <NUM_LIT:0> , cutoffed_nodes = [ ] ) : <EOL> """<STR_LIT>""" <EOL> scaled_radi = cutoff * scale <EOL> ( image , draw , boundaries ) = create_canvas ( dataset_observations , scale , margin ) <EOL> for i in range ( len ( dataset_observations ) ) : <EOL> position = draw_point_into_canvas ( dataset_observations , i , draw , scale , margin , "<STR_LIT>" , boundaries ) <EOL> if i in cutoffed_nodes : <EOL> draw . ellipse ( ( position [ <NUM_LIT:0> ] - scaled_radi , position [ <NUM_LIT:1> ] - scaled_radi , position [ <NUM_LIT:0> ] + scaled_radi , position [ <NUM_LIT:1> ] + scaled_radi ) ) <EOL> return image <EOL> def generate_color_list ( number_of_colors ) : <EOL> """<STR_LIT>""" <EOL> color_list = [ ] <EOL> for i in range ( number_of_colors ) : <EOL> r = random . randint ( <NUM_LIT> , <NUM_LIT:255> ) <EOL> g = random . randint ( <NUM_LIT> , <NUM_LIT:255> ) <EOL> b = random . randint ( <NUM_LIT> , <NUM_LIT:255> ) <EOL> scolor = ( "<STR_LIT>" % ( r , g , b ) ) <EOL> color_list . append ( scolor ) <EOL> return color_list <EOL> def generate_faded_red ( alpha ) : <EOL> """<STR_LIT>""" <EOL> if alpha > <NUM_LIT:0> and alpha < <NUM_LIT> : <EOL> alpha = <NUM_LIT> <EOL> r = int ( alpha * <NUM_LIT> ) <EOL> g = <NUM_LIT:0.0> <EOL> b = <NUM_LIT:0.0> <EOL> return "<STR_LIT>" % ( r , g , b ) <EOL> def draw_cross ( draw , boundaries , color ) : <EOL> xsup = boundaries [ <NUM_LIT:0> ] <EOL> ysup = boundaries [ <NUM_LIT:1> ] <EOL> xinf = boundaries [ <NUM_LIT:2> ] <EOL> yinf = boundaries [ <NUM_LIT:3> ] <EOL> draw . line ( [ ( xsup , ysup ) , ( xinf , yinf ) ] , width = <NUM_LIT:2> , fill = color ) <EOL> draw . line ( [ ( xsup , yinf ) , ( xinf , ysup ) ] , width = <NUM_LIT:2> , fill = color ) <EOL> def draw_circle ( draw , boundaries , color ) : <EOL> draw . ellipse ( boundaries , fill = color ) <EOL> def draw_square ( draw , boundaries , color ) : <EOL> draw . rectangle ( boundaries , fill = color ) <EOL> def draw_rombo ( draw , boundaries , color ) : <EOL> xsup = boundaries [ <NUM_LIT:0> ] <EOL> ysup = boundaries [ <NUM_LIT:1> ] <EOL> xinf = boundaries [ <NUM_LIT:2> ] <EOL> yinf = boundaries [ <NUM_LIT:3> ] <EOL> draw . polygon ( [ ( xsup , ysup + <NUM_LIT:0.5> * ( yinf - ysup ) ) , <EOL> ( xsup + <NUM_LIT:0.5> * ( xinf - xsup ) , ysup ) , <EOL> ( xinf , ysup + <NUM_LIT:0.5> * ( yinf - ysup ) ) , <EOL> ( xsup + <NUM_LIT:0.5> * ( xinf - xsup ) , yinf ) ] , fill = color ) <EOL> def draw_triangle ( draw , boundaries , color ) : <EOL> xsup = boundaries [ <NUM_LIT:0> ] <EOL> ysup = boundaries [ <NUM_LIT:1> ] <EOL> xinf = boundaries [ <NUM_LIT:2> ] <EOL> yinf = boundaries [ <NUM_LIT:3> ] <EOL> draw . polygon ( [ ( xsup , yinf ) , ( xsup + <NUM_LIT:0.5> * ( xinf - xsup ) , ysup ) , ( xinf , yinf ) ] , fill = color ) <EOL> def show_2D_dataset_clusters ( dataset_observations , clusterization , scale , margin = <NUM_LIT:0> , print_numbers = False ) : <EOL> """<STR_LIT>""" <EOL> ( image , draw , boundaries ) = create_canvas ( dataset_observations , scale , margin ) <EOL> color_list = generate_color_list ( len ( clusterization . clusters ) ) <EOL> available_shape_functions = [ draw_cross , draw_circle , draw_square , draw_triangle , draw_rombo ] <EOL> for i , c in enumerate ( clusterization . clusters ) : <EOL> color = color_list . pop ( ) <EOL> for j in c . all_elements : <EOL> draw_point_into_canvas ( dataset_observations , j , draw , scale , margin , color , boundaries , print_numbers , available_shape_functions [ i % <NUM_LIT:5> ] ) <EOL> return image <EOL> def generate_similarity_network ( W , dataset_observations , scale , margin = <NUM_LIT:0> , print_numbers = False ) : <EOL> norm_W = W / numpy . max ( W ) <EOL> ( image , draw , boundaries ) = create_canvas ( dataset_observations , scale , margin ) <EOL> for i in range ( len ( dataset_observations ) - <NUM_LIT:1> ) : <EOL> i_position = ( ( dataset_observations [ i ] [ <NUM_LIT:0> ] - boundaries [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) * scale + margin , ( dataset_observations [ i ] [ <NUM_LIT:1> ] - boundaries [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) * scale + margin ) <EOL> for j in range ( i + <NUM_LIT:1> , len ( dataset_observations ) ) : <EOL> if norm_W [ i ] [ j ] > <NUM_LIT:0.0> : <EOL> j_position = ( ( dataset_observations [ j ] [ <NUM_LIT:0> ] - boundaries [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) * scale + margin , ( dataset_observations [ j ] [ <NUM_LIT:1> ] - boundaries [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) * scale + margin ) <EOL> draw . line ( [ i_position , j_position ] , width = <NUM_LIT:2> , fill = generate_faded_red ( norm_W [ i ] [ j ] ) ) <EOL> for i in range ( len ( dataset_observations ) ) : <EOL> position = ( ( dataset_observations [ i ] [ <NUM_LIT:0> ] - boundaries [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) * scale + margin , ( dataset_observations [ i ] [ <NUM_LIT:1> ] - boundaries [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) * scale + margin ) <EOL> draw . point ( position , fill = "<STR_LIT>" ) <EOL> if print_numbers : <EOL> draw . text ( position , str ( i ) , fill = "<STR_LIT>" ) <EOL> return image </s>
