<s> from __future__ import division <EOL> import numpy as np <EOL> from collections import defaultdict <EOL> import json <EOL> import itertools <EOL> from sklearn import cluster , preprocessing , manifold <EOL> from datetime import datetime <EOL> import sys <EOL> class KeplerMapper ( object ) : <EOL> def __init__ ( self , verbose = <NUM_LIT:2> ) : <EOL> self . verbose = verbose <EOL> self . chunk_dist = [ ] <EOL> self . overlap_dist = [ ] <EOL> self . d = [ ] <EOL> self . nr_cubes = <NUM_LIT:0> <EOL> self . overlap_perc = <NUM_LIT:0> <EOL> self . clusterer = False <EOL> def fit_transform ( self , X , projection = "<STR_LIT>" , scaler = preprocessing . MinMaxScaler ( ) ) : <EOL> self . scaler = scaler <EOL> self . projection = str ( projection ) <EOL> if str ( type ( projection ) ) [ <NUM_LIT:1> : <NUM_LIT:6> ] == "<STR_LIT:class>" : <EOL> reducer = projection <EOL> if self . verbose > <NUM_LIT:0> : <EOL> try : <EOL> projection . set_params ( ** { "<STR_LIT>" : self . verbose } ) <EOL> except : <EOL> pass <EOL> print ( "<STR_LIT>" % str ( projection ) ) <EOL> X = reducer . fit_transform ( X ) <EOL> if isinstance ( projection , str ) : <EOL> if self . verbose > <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" % ( projection ) ) <EOL> if projection == "<STR_LIT>" : <EOL> X = np . sum ( X , axis = <NUM_LIT:1> ) . reshape ( ( X . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) <EOL> if projection == "<STR_LIT>" : <EOL> X = np . mean ( X , axis = <NUM_LIT:1> ) . reshape ( ( X . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) <EOL> if projection == "<STR_LIT>" : <EOL> X = np . median ( X , axis = <NUM_LIT:1> ) . reshape ( ( X . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) <EOL> if projection == "<STR_LIT>" : <EOL> X = np . max ( X , axis = <NUM_LIT:1> ) . reshape ( ( X . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) <EOL> if projection == "<STR_LIT>" : <EOL> X = np . min ( X , axis = <NUM_LIT:1> ) . reshape ( ( X . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) <EOL> if projection == "<STR_LIT>" : <EOL> X = np . std ( X , axis = <NUM_LIT:1> ) . reshape ( ( X . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) <EOL> if projection == "<STR_LIT>" : <EOL> X_mean = np . mean ( X , axis = <NUM_LIT:0> ) <EOL> X = np . sum ( np . sqrt ( ( X - X_mean ) ** <NUM_LIT:2> ) , axis = <NUM_LIT:1> ) . reshape ( ( X . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) ) <EOL> if isinstance ( projection , list ) : <EOL> if self . verbose > <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" % ( str ( projection ) ) ) <EOL> X = X [ : , np . array ( projection ) ] <EOL> if scaler is not None : <EOL> if self . verbose > <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" % str ( scaler ) ) <EOL> X = scaler . fit_transform ( X ) <EOL> return X <EOL> def map ( self , projected_X , inverse_X = None , clusterer = cluster . DBSCAN ( eps = <NUM_LIT:0.5> , min_samples = <NUM_LIT:3> ) , nr_cubes = <NUM_LIT:10> , overlap_perc = <NUM_LIT:0.1> ) : <EOL> start = datetime . now ( ) <EOL> def cube_coordinates_all ( nr_cubes , nr_dimensions ) : <EOL> l = [ ] <EOL> for x in range ( nr_cubes ) : <EOL> l += [ x ] * nr_dimensions <EOL> return [ np . array ( list ( f ) ) for f in sorted ( set ( itertools . permutations ( l , nr_dimensions ) ) ) ] <EOL> nodes = defaultdict ( list ) <EOL> links = defaultdict ( list ) <EOL> complex = { } <EOL> self . nr_cubes = nr_cubes <EOL> self . clusterer = clusterer <EOL> self . overlap_perc = overlap_perc <EOL> if self . verbose > <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" % ( str ( projected_X . shape ) ) ) <EOL> if inverse_X is None : <EOL> inverse_X = projected_X <EOL> self . chunk_dist = ( np . max ( projected_X , axis = <NUM_LIT:0> ) - np . min ( projected_X , axis = <NUM_LIT:0> ) ) / nr_cubes <EOL> self . overlap_dist = self . overlap_perc * self . chunk_dist <EOL> self . d = np . min ( projected_X , axis = <NUM_LIT:0> ) <EOL> di = np . array ( [ x for x in range ( projected_X . shape [ <NUM_LIT:1> ] ) ] ) <EOL> ids = np . array ( [ x for x in range ( projected_X . shape [ <NUM_LIT:0> ] ) ] ) <EOL> projected_X = np . c_ [ ids , projected_X ] <EOL> inverse_X = np . c_ [ ids , inverse_X ] <EOL> if self . verbose > <NUM_LIT:0> : <EOL> total_cubes = len ( cube_coordinates_all ( nr_cubes , projected_X . shape [ <NUM_LIT:1> ] ) ) <EOL> print ( "<STR_LIT>" % total_cubes ) <EOL> for i , coor in enumerate ( cube_coordinates_all ( nr_cubes , di . shape [ <NUM_LIT:0> ] ) ) : <EOL> hypercube = projected_X [ np . invert ( np . any ( ( projected_X [ : , di + <NUM_LIT:1> ] >= self . d [ di ] + ( coor * self . chunk_dist [ di ] ) ) & <EOL> ( projected_X [ : , di + <NUM_LIT:1> ] < self . d [ di ] + ( coor * self . chunk_dist [ di ] ) + self . chunk_dist [ di ] + self . overlap_dist [ di ] ) == False , axis = <NUM_LIT:1> ) ) ] <EOL> if self . verbose > <NUM_LIT:1> : <EOL> print ( "<STR_LIT>" % <EOL> ( hypercube . shape [ <NUM_LIT:0> ] , i , total_cubes , self . d [ di ] + ( coor * self . chunk_dist [ di ] ) ) ) <EOL> if hypercube . shape [ <NUM_LIT:0> ] > <NUM_LIT:0> : <EOL> inverse_x = inverse_X [ [ int ( nn ) for nn in hypercube [ : , <NUM_LIT:0> ] ] ] <EOL> clusterer . fit ( inverse_x [ : , <NUM_LIT:1> : ] ) <EOL> if self . verbose > <NUM_LIT:1> : <EOL> print ( "<STR_LIT>" % ( np . unique ( clusterer . labels_ [ clusterer . labels_ > - <NUM_LIT:1> ] ) . shape [ <NUM_LIT:0> ] , i ) ) <EOL> for a in np . c_ [ hypercube [ : , <NUM_LIT:0> ] , clusterer . labels_ ] : <EOL> if a [ <NUM_LIT:1> ] != - <NUM_LIT:1> : <EOL> cluster_id = str ( coor [ <NUM_LIT:0> ] ) + "<STR_LIT:_>" + str ( i ) + "<STR_LIT:_>" + str ( a [ <NUM_LIT:1> ] ) + "<STR_LIT:_>" + str ( coor ) + "<STR_LIT:_>" + str ( self . d [ di ] + ( coor * self . chunk_dist [ di ] ) ) <EOL> nodes [ cluster_id ] . append ( int ( a [ <NUM_LIT:0> ] ) ) <EOL> else : <EOL> if self . verbose > <NUM_LIT:1> : <EOL> print ( "<STR_LIT>" % ( i ) ) <EOL> candidates = itertools . combinations ( nodes . keys ( ) , <NUM_LIT:2> ) <EOL> for candidate in candidates : <EOL> if len ( nodes [ candidate [ <NUM_LIT:0> ] ] + nodes [ candidate [ <NUM_LIT:1> ] ] ) != len ( set ( nodes [ candidate [ <NUM_LIT:0> ] ] + nodes [ candidate [ <NUM_LIT:1> ] ] ) ) : <EOL> links [ candidate [ <NUM_LIT:0> ] ] . append ( candidate [ <NUM_LIT:1> ] ) <EOL> if self . verbose > <NUM_LIT:0> : <EOL> nr_links = <NUM_LIT:0> <EOL> for k in links : <EOL> nr_links += len ( links [ k ] ) <EOL> print ( "<STR_LIT>" % ( nr_links , len ( nodes ) , str ( datetime . now ( ) - start ) ) ) <EOL> complex [ "<STR_LIT>" ] = nodes <EOL> complex [ "<STR_LIT>" ] = links <EOL> complex [ "<STR_LIT>" ] = self . projection <EOL> return complex <EOL> def visualize ( self , complex , color_function = "<STR_LIT>" , path_html = "<STR_LIT>" , title = "<STR_LIT>" , <EOL> graph_link_distance = <NUM_LIT:30> , graph_gravity = <NUM_LIT:0.1> , graph_charge = - <NUM_LIT> , custom_tooltips = None , width_html = <NUM_LIT:0> , <EOL> height_html = <NUM_LIT:0> , show_tooltips = True , show_title = True , show_meta = True ) : <EOL> json_s = { } <EOL> json_s [ "<STR_LIT>" ] = [ ] <EOL> json_s [ "<STR_LIT>" ] = [ ] <EOL> k2e = { } <EOL> for e , k in enumerate ( complex [ "<STR_LIT>" ] ) : <EOL> if custom_tooltips is not None : <EOL> tooltip_s = "<STR_LIT>" % k + "<STR_LIT:U+0020>" . join ( [ str ( f ) for f in custom_tooltips [ complex [ "<STR_LIT>" ] [ k ] ] ] ) <EOL> if color_function == "<STR_LIT>" : <EOL> tooltip_i = int ( ( ( sum ( [ f for f in custom_tooltips [ complex [ "<STR_LIT>" ] [ k ] ] ] ) / len ( custom_tooltips [ complex [ "<STR_LIT>" ] [ k ] ] ) ) * <NUM_LIT:30> ) ) <EOL> json_s [ "<STR_LIT>" ] . append ( { "<STR_LIT:name>" : str ( k ) , "<STR_LIT>" : tooltip_s , "<STR_LIT>" : <NUM_LIT:2> * int ( np . log ( len ( complex [ "<STR_LIT>" ] [ k ] ) ) ) , "<STR_LIT>" : str ( tooltip_i ) } ) <EOL> else : <EOL> json_s [ "<STR_LIT>" ] . append ( { "<STR_LIT:name>" : str ( k ) , "<STR_LIT>" : tooltip_s , "<STR_LIT>" : <NUM_LIT:2> * int ( np . log ( len ( complex [ "<STR_LIT>" ] [ k ] ) ) ) , "<STR_LIT>" : str ( k . split ( "<STR_LIT:_>" ) [ <NUM_LIT:0> ] ) } ) <EOL> else : <EOL> tooltip_s = "<STR_LIT>" % ( k , len ( complex [ "<STR_LIT>" ] [ k ] ) ) <EOL> json_s [ "<STR_LIT>" ] . append ( { "<STR_LIT:name>" : str ( k ) , "<STR_LIT>" : tooltip_s , "<STR_LIT>" : <NUM_LIT:2> * int ( np . log ( len ( complex [ "<STR_LIT>" ] [ k ] ) ) ) , "<STR_LIT>" : str ( k . split ( "<STR_LIT:_>" ) [ <NUM_LIT:0> ] ) } ) <EOL> k2e [ k ] = e <EOL> for k in complex [ "<STR_LIT>" ] : <EOL> for link in complex [ "<STR_LIT>" ] [ k ] : <EOL> json_s [ "<STR_LIT>" ] . append ( { "<STR_LIT:source>" : k2e [ k ] , "<STR_LIT:target>" : k2e [ link ] , "<STR_LIT:value>" : <NUM_LIT:1> } ) <EOL> if width_html == <NUM_LIT:0> : <EOL> width_css = "<STR_LIT>" <EOL> width_js = '<STR_LIT>' <EOL> else : <EOL> width_css = "<STR_LIT>" % width_html <EOL> width_js = "<STR_LIT:%s>" % width_html <EOL> if height_html == <NUM_LIT:0> : <EOL> height_css = "<STR_LIT>" <EOL> height_js = '<STR_LIT>' <EOL> else : <EOL> height_css = "<STR_LIT>" % height_html <EOL> height_js = "<STR_LIT:%s>" % height_html <EOL> if show_tooltips == False : <EOL> tooltips_display = "<STR_LIT>" <EOL> else : <EOL> tooltips_display = "<STR_LIT>" <EOL> if show_meta == False : <EOL> meta_display = "<STR_LIT>" <EOL> else : <EOL> meta_display = "<STR_LIT>" <EOL> if show_title == False : <EOL> title_display = "<STR_LIT>" <EOL> else : <EOL> title_display = "<STR_LIT>" <EOL> with open ( path_html , "<STR_LIT:wb>" ) as outfile : <EOL> html = """<STR_LIT>""" % ( title , width_css , height_css , title_display , meta_display , tooltips_display , title , complex [ "<STR_LIT>" ] , self . nr_cubes , self . overlap_perc * <NUM_LIT:100> , color_function , complex [ "<STR_LIT>" ] , str ( self . clusterer ) , str ( self . scaler ) , width_js , height_js , graph_charge , graph_link_distance , graph_gravity , json . dumps ( json_s ) ) <EOL> outfile . write ( html . encode ( "<STR_LIT:utf-8>" ) ) <EOL> if self . verbose > <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" % path_html ) </s>
