-
Notifications
You must be signed in to change notification settings - Fork 0
/
2023-03-31_testScript_InfoFlow_shorter.py
executable file
·166 lines (126 loc) · 5.56 KB
/
2023-03-31_testScript_InfoFlow_shorter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# standard library imports
import numpy as np
import scipy.io
import scipy.io as spio
import skimage.io as skio
from scipy.ndimage import gaussian_filter
import pylab as plt # this is from matplotlib library
import skimage.transform as sktform
from skimage import color
# from tqdm import tqdm # this optional import is for monitoring progress
import skimage.transform as sktform
import skimage.util as skutil
import scipy.ndimage as ndimage
"""
Imports of various flow functions.
"""
from InfoFlow.gc_flow import GC_full_reduced_separate_regress_individual
from InfoFlow.DDC_flow import DDC_cause
from InfoFlow.pdc_dtf_flow import PDC_central_flow
from InfoFlow.pcca_flow import pcca_cause_block
from InfoFlow.correlation_flow import nd_xcorr_lag
from InfoFlow.optical_flow import extract_optflow # this depends on opencv installation. please install with pip install opencv-contrib-python from https://pypi.org/project/opencv-contrib-python/
from InfoFlow.LK_flow import Linear_LK_cause
from InfoFlow.flow_vis import flow_to_color
from InfoFlow.utils import read_video_cv2
from InfoFlow.dynamic_image import _compute_dynamic_image
import InfoFlow.infoflow as infoflow_scripts
"""
1. read in the video file.
"""
myVid = read_video_cv2(r'/archive/bioinformatics/Danuser_lab/shared/ComputerVisionDatasets/CrowdFlow_SaadAli/Data_Mov_Format/3687-18_70.mov')
myVid = color.rgb2gray(myVid) # convert to grayscale image
# a) debug visualization, computing the dynamic image.
dyn_image_Vid = _compute_dynamic_image(myVid[...,None])
plt.figure(figsize=(5,5))
plt.title('Dynamic Image')
plt.imshow(dyn_image_Vid, cmap='coolwarm')
plt.show()
plt.figure(figsize=(5,5))
plt.title('Variance Image')
plt.imshow(np.var(myVid, axis=0), cmap='coolwarm')
plt.show()
"""
downscale the input image for speed and smooth at gaussian sigma 1.
"""
myVid8 = infoflow_scripts.gaussian_video_pyramid(myVid, scales=[4], sigma=1)[0] # set to no smooth!.
# =============================================================================
# 2. run through all causal measures and extract the output
# =============================================================================
causal_flow_outputs = [infoflow_scripts.causal_flow_scores(myVid8, GC_full_reduced_separate_regress_individual, winsize=3, lag=1, alpha=1), # this is weird!.
infoflow_scripts.causal_flow_scores(myVid8, DDC_cause, eps=1e-12, alpha=1e-2),
infoflow_scripts.causal_flow_scores(myVid8, Linear_LK_cause, eps=1e-12),
infoflow_scripts.causal_flow_scores(myVid8, PDC_central_flow, lag=1, alpha=1e-2),
infoflow_scripts.causal_flow_scores(myVid8, nd_xcorr_lag, lag=1)]
causal_flow_vectors = [infoflow_scripts.causal_block_flow_scores_gradient(flo) for flo in causal_flow_outputs]
#
# testing the PCCA block flow.
pcca_flow = infoflow_scripts.causal_block_flow(myVid8, pcca_cause_block,
block_size=3,
k=1,
m=1,
eta_xt=5e-4,
eta_yt=5e-4,
eta_xtkm=5e-4)
# # =============================================================================
# # 3. Visualization of output.
# # =============================================================================
xy_coords = np.indices(myVid8.shape[1:]); xy_coords=xy_coords.transpose(1,2,0)
xy_coords = xy_coords[...,::-1]
sampling = 1
flow_methods = ['cGC', 'DDC', 'LK', 'PDC', 'Corr']
for ii in np.arange(len(causal_flow_vectors)):
plt.figure(figsize=(15,15))
plt.title(flow_methods[ii])
plt.imshow(myVid8[0])
plt.quiver(xy_coords[::sampling,::sampling,0],
xy_coords[::sampling,::sampling,1],
causal_flow_vectors[ii][::sampling,::sampling,1], # x
-causal_flow_vectors[ii][::sampling,::sampling,0]) # y
plt.show()
mean_flow_color = flow_to_color(causal_flow_vectors[ii][...,::-1])
plt.figure(figsize=(15,15))
plt.title(flow_methods[ii]+'_flow-color')
plt.imshow(myVid8[0], cmap='gray')
plt.imshow(mean_flow_color, alpha=0.7)
plt.show()
"""
Visualization for pcca flow vector
"""
plt.figure(figsize=(15,15))
plt.title('PCCA')
plt.imshow(myVid8[0])
plt.quiver(xy_coords[::sampling,::sampling,0],
xy_coords[::sampling,::sampling,1],
pcca_flow[::sampling,::sampling,1], # x
-pcca_flow[::sampling,::sampling,0]) # y
plt.show()
mean_flow_color = flow_to_color(pcca_flow[...,::-1])
plt.figure(figsize=(15,15))
plt.title('PCCA_flow-color')
plt.imshow(myVid8[0], cmap='gray')
plt.imshow(mean_flow_color, alpha=0.7)
plt.show()
"""
Compare to optical flow - uses the opencv library.
"""
optical_flow_params = dict(pyr_scale=0.5, levels=1, winsize=3, iterations=5, poly_n=3, poly_sigma=1.2, flags=0)
vid_flow = extract_optflow(255*myVid8[:],
optical_flow_params,
rescale_intensity=False,
intensity_range=[2,98])
optflow_vector = vid_flow.mean(axis=0)
mean_opt_flow_color = flow_to_color(vid_flow.mean(axis=0))
plt.figure(figsize=(15,15))
plt.title('optical flow baseline')
plt.imshow(myVid8[0])
plt.quiver(xy_coords[::sampling,::sampling,0],
xy_coords[::sampling,::sampling,1],
optflow_vector[::sampling,::sampling,0], # x
-optflow_vector[::sampling,::sampling,1]) # y
plt.show()
plt.figure(figsize=(15,15))
plt.title('optical flow baseline_flow-color')
plt.imshow(myVid8[0], cmap='gray')
plt.imshow(mean_opt_flow_color)
plt.show()