GitLab Repo

amachine.am_visualization.am_distance_plots

 1import numpy as np
 2from scipy.spatial.distance import pdist, squareform
 3
 4def animated_distance_plot( 
 5    vectors, 
 6    max_res=1440, 
 7    stride=7, 
 8    apply_threshold=False, 
 9    threshold=0.01 ) :
10
11    use_cuda = False # cpx is not None
12
13    n_vectors = len( vectors )
14    max_res = min( n_vectors, max_res )
15
16    if use_cuda :
17        state_vectors = cp.asarray( vectors[ 0:max_states, : ] )
18    else :
19        state_vectors = vectors
20
21    imgs = []
22    for start in range( 0, n_vectors, stride ) :
23        
24        if start % (stride*10) == 0 :
25            print( f"offset: {start}" )
26
27        end = min( start+max_res, n_vectors )
28        
29        if use_cuda :
30            distance_plot = squareform( 
31                cpx.scipy.spatial.distance.pdist( state_vectors[ start:end, : ], metric='jensenshannon' ).get()
32            )
33        else :
34            distance_plot = squareform( 
35                pdist( state_vectors[ start:end, : ], metric='jensenshannon' )
36            )
37
38        if len( distance_plot ) < max_res :
39            distance_plot = np.pad( 
40                distance_plot, 
41                ( ( 0, max_res-len( distance_plot ) ), (0, max_res-len( distance_plot )) ), 
42                mode='constant' )
43
44        if apply_threshold : 
45            distance_plot = np.where( distance_plot > threshold, 1, 0 ).astype( np.uint8 )
46            imgs.append( distance_plot )
47        else :
48            imgs.append( distance_plot.astype( np.float16 ) )
49
50    print( f"Rendering {len(imgs)} frames." )
51    to_video( imgs, fps=60 )
def animated_distance_plot( vectors, max_res=1440, stride=7, apply_threshold=False, threshold=0.01):
 5def animated_distance_plot( 
 6    vectors, 
 7    max_res=1440, 
 8    stride=7, 
 9    apply_threshold=False, 
10    threshold=0.01 ) :
11
12    use_cuda = False # cpx is not None
13
14    n_vectors = len( vectors )
15    max_res = min( n_vectors, max_res )
16
17    if use_cuda :
18        state_vectors = cp.asarray( vectors[ 0:max_states, : ] )
19    else :
20        state_vectors = vectors
21
22    imgs = []
23    for start in range( 0, n_vectors, stride ) :
24        
25        if start % (stride*10) == 0 :
26            print( f"offset: {start}" )
27
28        end = min( start+max_res, n_vectors )
29        
30        if use_cuda :
31            distance_plot = squareform( 
32                cpx.scipy.spatial.distance.pdist( state_vectors[ start:end, : ], metric='jensenshannon' ).get()
33            )
34        else :
35            distance_plot = squareform( 
36                pdist( state_vectors[ start:end, : ], metric='jensenshannon' )
37            )
38
39        if len( distance_plot ) < max_res :
40            distance_plot = np.pad( 
41                distance_plot, 
42                ( ( 0, max_res-len( distance_plot ) ), (0, max_res-len( distance_plot )) ), 
43                mode='constant' )
44
45        if apply_threshold : 
46            distance_plot = np.where( distance_plot > threshold, 1, 0 ).astype( np.uint8 )
47            imgs.append( distance_plot )
48        else :
49            imgs.append( distance_plot.astype( np.float16 ) )
50
51    print( f"Rendering {len(imgs)} frames." )
52    to_video( imgs, fps=60 )