<s> import numpy as np <EOL> import matplotlib . pyplot as plt <EOL> import matplotlib . patheffects as PathEffects <EOL> import seaborn as sns <EOL> from functools import wraps <EOL> from sklearn . decomposition import PCA <EOL> import matplotlib . animation as animation <EOL> import matplotlib . patches as mpatches <EOL> def _get_legend ( palette , labels ) : <EOL> handles = [ ] <EOL> for c , l in zip ( palette , np . unique ( labels ) ) : <EOL> patch = mpatches . Patch ( color = c , label = l ) <EOL> handles . append ( patch ) <EOL> return handles <EOL> def embedding2d ( data , train_data = None , method = None ) : <EOL> '''<STR_LIT>''' <EOL> if method is None : <EOL> method = PCA ( n_components = <NUM_LIT:2> ) <EOL> if train_data is None : <EOL> ebd = method . fit_transform ( data ) <EOL> else : <EOL> method . fit ( train_data ) <EOL> ebd = method . transform ( data ) <EOL> return ebd , method <EOL> def embedding2dplot ( data , labels , show_median = True , show_legend = True ) : <EOL> '''<STR_LIT>''' <EOL> max_label = labels . max ( ) <EOL> palette = np . array ( sns . color_palette ( "<STR_LIT>" , max_label + <NUM_LIT:1> ) ) <EOL> fig = plt . figure ( figsize = ( <NUM_LIT:8> , <NUM_LIT:8> ) ) <EOL> ax = plt . subplot ( aspect = '<STR_LIT>' ) <EOL> sc = ax . scatter ( data [ : , <NUM_LIT:0> ] , data [ : , <NUM_LIT:1> ] , lw = <NUM_LIT:0> , s = <NUM_LIT> , <EOL> c = palette [ labels . astype ( np . int ) ] ) <EOL> plt . xlim ( - <NUM_LIT> , <NUM_LIT> ) <EOL> plt . ylim ( - <NUM_LIT> , <NUM_LIT> ) <EOL> ax . axis ( '<STR_LIT>' ) <EOL> ax . axis ( '<STR_LIT>' ) <EOL> if show_median : <EOL> txts = [ ] <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> xtext , ytext = np . median ( data [ labels == i , : ] , axis = <NUM_LIT:0> ) <EOL> txt = ax . text ( xtext , ytext , str ( i ) , fontsize = <NUM_LIT> ) <EOL> txt . set_path_effects ( [ <EOL> PathEffects . Stroke ( linewidth = <NUM_LIT:5> , foreground = "<STR_LIT:w>" ) , <EOL> PathEffects . Normal ( ) ] ) <EOL> txts . append ( txt ) <EOL> if show_legend : <EOL> handles = _get_legend ( palette , labels ) <EOL> ax . legend ( handles = handles ) <EOL> return fig , ax , sc <EOL> def timeseries2d ( data , train_data = None , method = None ) : <EOL> '''<STR_LIT>''' <EOL> t , b , d = data . shape <EOL> data = data . reshape ( ( t * b , d ) ) <EOL> ebd , mtd = embedding2d ( data , train_data , method ) <EOL> ebd = ebd . reshape ( ( t , b , - <NUM_LIT:1> ) ) <EOL> return ebd , mtd <EOL> def timeseries2dplot ( data , labels ) : <EOL> '''<STR_LIT>''' <EOL> t , b , d = data . shape <EOL> data = data . reshape ( ( t * b , d ) ) <EOL> labels = labels [ np . newaxis ] . repeat ( t , axis = <NUM_LIT:0> ) <EOL> labels = labels . flatten ( ) <EOL> ebd , mtd = embedding2d ( data ) <EOL> fig , ax , sc , txts = embedding2dplot ( ebd , labels ) <EOL> return fig , ax , sc , txts <EOL> def _prepare_fig_labels ( data , labels ) : <EOL> '''<STR_LIT>''' <EOL> max_label = labels . max ( ) <EOL> palette = np . array ( sns . color_palette ( "<STR_LIT>" , max_label + <NUM_LIT:1> ) ) <EOL> t , b , d = data . shape <EOL> data = data . transpose ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:2> ) . reshape ( ( t * b , d ) ) <EOL> labels = labels [ np . newaxis ] . repeat ( t , axis = <NUM_LIT:0> ) . transpose ( <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> labels = labels . flatten ( ) <EOL> fig = plt . figure ( figsize = ( <NUM_LIT:8> , <NUM_LIT:8> ) ) <EOL> return labels , palette , fig <EOL> def _prepare_axis ( subplot , data = None ) : <EOL> ax = plt . subplot ( subplot , aspect = '<STR_LIT>' ) <EOL> if data is not None : <EOL> xymin = data . min ( ) - data . std ( ) / <NUM_LIT:3> <EOL> xymax = data . max ( ) + data . std ( ) / <NUM_LIT:3> <EOL> plt . xlim ( xymin , xymax ) <EOL> plt . ylim ( xymin , xymax ) <EOL> ax . axis ( '<STR_LIT>' ) <EOL> return ax <EOL> def animate ( func ) : <EOL> @ wraps ( func ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> make_frame , fig , fargs , video_length , ani_path = func ( * args , ** kwargs ) <EOL> ani = animation . FuncAnimation ( fig , make_frame , frames = video_length , interval = <NUM_LIT:100> , <EOL> fargs = fargs ) <EOL> ani . save ( ani_path , writer = '<STR_LIT>' , fps = <NUM_LIT:5> ) <EOL> return ani <EOL> return wrapper <EOL> @ animate <EOL> def timeseries2dvideo ( data , labels , ani_path = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> labels , palette , fig = _prepare_fig_labels ( data , labels ) <EOL> ax = _prepare_axis ( <NUM_LIT> , data ) <EOL> t , b , d = data . shape <EOL> data = data . transpose ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:2> ) . reshape ( ( t * b , d ) ) <EOL> sc = ax . scatter ( [ ] , [ ] ) <EOL> def make_frame ( t , sc ) : <EOL> pts = data [ t ] <EOL> color = np . hstack ( [ palette [ labels [ t ] . astype ( np . int ) ] , <NUM_LIT:1.> ] ) <EOL> offsets = np . vstack ( [ sc . get_offsets ( ) , pts ] ) <EOL> sc . set_offsets ( offsets ) <EOL> colors = np . vstack ( [ sc . get_facecolors ( ) , color ] ) <EOL> sc . set_facecolors ( colors ) <EOL> return make_frame , fig , ( sc , ) , data . shape [ <NUM_LIT:0> ] , ani_path <EOL> @ animate <EOL> def video_embedding ( video , embedding , labels , ani_path = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> labels , palette , fig = _prepare_fig_labels ( embedding , labels ) <EOL> ax2 = _prepare_axis ( <NUM_LIT> , embedding ) <EOL> ax1 = _prepare_axis ( <NUM_LIT> ) <EOL> sc = ax2 . scatter ( [ ] , [ ] ) <EOL> t , b , d = embedding . shape <EOL> embedding = embedding . transpose ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:2> ) . reshape ( ( t * b , d ) ) <EOL> t , b , d = video . shape <EOL> video = video . transpose ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:2> ) . reshape ( ( t * b , d ) ) <EOL> dim = np . sqrt ( d ) . astype ( '<STR_LIT:int>' ) <EOL> init_frame = video [ <NUM_LIT:0> ] . reshape ( ( dim , dim ) ) <EOL> vid = ax1 . imshow ( init_frame , cmap = '<STR_LIT>' , vmin = video . min ( ) , vmax = video . max ( ) ) <EOL> def make_frame ( t , sc , vid ) : <EOL> pts = embedding [ t ] <EOL> frame = video [ t ] . reshape ( ( dim , dim ) ) <EOL> color = np . hstack ( [ palette [ labels [ t ] . astype ( np . int ) ] , <NUM_LIT:1.> ] ) <EOL> offsets = np . vstack ( [ sc . get_offsets ( ) , pts ] ) <EOL> sc . set_offsets ( offsets ) <EOL> colors = np . vstack ( [ sc . get_facecolors ( ) , color ] ) <EOL> sc . set_facecolors ( colors ) <EOL> vid . set_data ( frame ) <EOL> return sc , vid <EOL> return make_frame , fig , ( sc , vid ) , t * b , ani_path <EOL> """<STR_LIT>""" </s>
