-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathomnibus.py
160 lines (127 loc) · 4.97 KB
/
omnibus.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
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt
import matplotlib.colors
from sar_data import *
class Omnibus(object):
"""
Implements the Omnibus test statistic
"""
def __init__(self, sar_list, ENL):
"""
Create a new Omnibus test
sar_list should be a list of SARData objects
ENL is the (common) equivalent number of looks of the images
"""
self.sar_list = sar_list
self.ENL = ENL
p = 3
k = len(sar_list)
n = ENL
self.f = (k-1)*p**2
self.rho = 1- (2*p**2 - 1)/(6*p*(k-1)) * (k/n - 1/(n*k))
sum_term = sum([np.log(Xi.determinant()) for Xi in sar_list])
X = sar_sum(sar_list)
# Omnibus test
self.f = (k-1)*p**2
self.rho = 1 - (2*p**2 - 1)/(6*(k-1)*p) * (k/n - 1/(n*k))
self.w2 = p**2*(p**2-1)/(24*self.rho**2) * (k/n**2 - 1/((n*k)**2)) - (p**2*(k-1))/4 * (1 - 1/self.rho)**2
self.lnq = n*(p*k*np.log(k) + sum_term - k*np.log(X.determinant()))
def pvalue(self):
"Average probability over the tested region"
chi2 = scipy.stats.chi2.cdf
z = -2*self.rho*self.lnq
return 1 - np.mean(chi2(z, df=self.f) + self.w2 * (chi2(z, df=self.f+4) - chi2(z, df=self.f)))
def histogram(self):
"""
Histogram of no change region
and pdf with only chi2 term
"""
fig = plt.figure(figsize=(8, 4))
ax = fig.add_subplot(111)
ax.hist(-2*self.lnq.flatten(), bins=100, normed=True, color="#3F5D7D")
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
# Overlay pdf
p = 3
k = len(self.sar_list)
f = (k-1)*p**2
chi2 = scipy.stats.chi2(f)
x = np.linspace(0, 100, 1000)
y = chi2.pdf(x)
ax.plot(x, y, color="black", linewidth=2)
ax.set_xlim([0, 100])
return fig, ax
def image_binary(self, percent):
# Select threshold from chi2 percentile (ignore w2 term)
p = 3
k = len(self.sar_list)
f = (k-1)*p**2
chi2 = scipy.stats.chi2(f)
threshold = chi2.ppf(1.0 - percent)
im = np.zeros_like(self.lnq)
im[-2*self.lnq > threshold] = 1
return im.reshape(self.sar_list[0].shape)
def image_autothresholds(self):
pass
def image_linear(self, p1, p2):
pass
def masked_region(self, mask):
"""
Extract a subset of the image and test result defined by a mask
This is similar to using SARData.masked_region() and then RjTest,
but more efficient because the test statistic is not recomputed
"""
assert(mask.size) == sar_list[0].size
r = Omnibus.__new__(Omnibus)
r.sar_list = [X.masked_region(mask) for X in self.sar_list]
r.ENL = self.ENL
r.f = self.f
r.rho = self.rho
r.w2 = self.w2
r.lnq = self.lnq[mask]
return r
if __name__ == "__main__":
print("Omnibus test...")
# Omnibus test of the entire image
omnibus = Omnibus(sar_list, 13)
# Omnibus test over the no change region
omnibus_no_change = Omnibus(sar_list_nochange, 13)
# Histogram over the no change region
fig, ax = omnibus_no_change.histogram()
hist_filename = "fig/omnibus/hist.nochange.pdf"
fig.savefig(hist_filename, bbox_inches='tight')
# Histogram, entire region
fig, ax = omnibus.histogram()
hist_filename = "fig/omnibus/hist.total.pdf"
fig.savefig(hist_filename, bbox_inches='tight')
# Binary images
def omnibus_binary(percent):
im = omnibus.image_binary(percent)
plt.imsave("fig/omnibus/omnibus.{}.jpg".format(percent), im, cmap="gray")
omnibus_binary(0.00001)
omnibus_binary(0.0001)
omnibus_binary(0.001)
omnibus_binary(0.01)
omnibus_binary(0.05)
omnibus_binary(0.10)
# Pairwise test
def average_test_for_masked_region(mask):
print("March = April : {0:.4f}".format(Omnibus([march.masked_region(mask), april.masked_region(mask)], 13).pvalue()))
print("April = May : {0:.4f}".format(Omnibus([april.masked_region(mask), may.masked_region(mask)], 13).pvalue()))
print("May = June : {0:.4f}".format(Omnibus([may.masked_region(mask), june.masked_region(mask)], 13).pvalue()))
print("June = July : {0:.4f}".format(Omnibus([june.masked_region(mask), july.masked_region(mask)], 13).pvalue()))
print("July = August: {0:.4f}".format(Omnibus([july.masked_region(mask), august.masked_region(mask)], 13).pvalue()))
print("Omnibus : {0:.4f}".format(Omnibus([X.masked_region(mask) for X in sar_list], 13).pvalue()))
# Omnibus test in notable regions
print("")
print("Forest:")
average_test_for_masked_region(mask_forest)
print("")
print("Rye:")
average_test_for_masked_region(mask_rye)
print("")
print("Grass:")
average_test_for_masked_region(mask_grass)