forked from fengyingxin/geo1015_Assignment-4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_5_comp.py
142 lines (118 loc) · 4.58 KB
/
_5_comp.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
# The code is for Comparison
# Authors: Feng Yingxin(5692148), Gong Sicong(5711932), Rao Chengzhi(5841089)
import math
import json
import numpy as np
import rasterio
import matplotlib.pyplot as plt
num_tick = 5
def show_scatter(path, X, Y, Z):
plt.figure(figsize=(8, 6))
plt.scatter(x=X, y=Y, c=Z, s=0.1)
plt.title(path.strip(".txt"))
plt.colorbar()
plt.clim(-5, 40)
plt.axis("equal")
plt.savefig('figure/' + path + '.png')
# plt.show()
def show_image(path, result, x_positions, x_labels, y_positions, y_labels):
plt.figure(figsize=(8, 6))
plt.imshow(result)
plt.colorbar()
plt.title(path.strip('.npy'))
plt.clim(-5, 40)
plt.xticks(x_positions, x_labels)
plt.yticks(y_positions, y_labels)
plt.savefig('figure/' + path + '.png')
# plt.show()
def show_diff(path, diff, x_positions, x_labels, y_positions, y_labels):
plt.figure(figsize=(8, 6))
plt.imshow(diff, cmap='Spectral_r')
plt.colorbar()
plt.title(path.strip('.npy'))
plt.clim(-1, 1)
plt.xticks(x_positions, x_labels)
plt.yticks(y_positions, y_labels)
plt.savefig('figure/' + path + '.png')
# plt.show()
def get_diff(diff):
nonan_num = np.count_nonzero(~np.isnan(diff))
diff_squre = diff * diff
diff_squre[np.isnan(diff_squre)] = 0
mean = np.sum(np.sum(diff_squre)) / nonan_num
root = math.sqrt(mean)
return root
def comp():
# Open the parameter file
with open('file/parameter.json', 'r') as pfile:
parameter = json.load(pfile)
x_min, x_max = parameter['crop']['x_min'], parameter['crop']['x_max']
y_min, y_max = parameter['crop']['y_min'], parameter['crop']['y_max']
grid_size = parameter['thin']['grid_size']
thin_rate = parameter['thin']['thin_rate']
x_labels = np.linspace(x_min, x_max, num_tick)
y_labels = np.linspace(y_min, y_max, num_tick)
with rasterio.open('file\image.tif') as src:
nodata = src.nodata
pdok = src.read(1)
pdok[pdok == nodata] = np.nan
print(pdok.shape)
np.save("file/pdok", pdok)
pdok = np.load("file/pdok.npy")
# Get the input parameters
lst_input = []
for size in grid_size:
for rate in thin_rate:
lst_input.append({'size': size, 'rate': rate})
print('cloth-ground!')
for path in lst_input:
print(path)
path_cloth = 'cloth_size_{}_rate_{}'.format(path['size'], path['rate'])
path_ground = 'ground_size_{}_rate_{}'.format(path['size'], path['rate'])
cloth = np.load('file/' + path_cloth + '.npy')
ground = np.load('file/' + path_ground + '.npy')
diff = cloth - ground
x_positions = np.linspace(0, diff.shape[0], num_tick)
y_positions = np.linspace(0, diff.shape[1], num_tick)
show_diff('cloth-ground_size_{}_rate_{}'.format(path['size'], path['rate']), diff, x_positions, x_labels,
y_positions, y_labels)
print(get_diff(diff))
lst_path_cloth = ['cloth_size_{}_rate_{}'.format(input['size'], input['rate']) for input in lst_input]
lst_path_ground = ['ground_size_{}_rate_{}'.format(input['size'], input['rate']) for input in
lst_input]
lst_path = lst_path_cloth + lst_path_ground
print('pdok!')
for path in lst_path:
print(path)
ours = np.load('file/' + path + '.npy')
x_positions = np.linspace(0, ours.shape[0], num_tick)
y_positions = np.linspace(0, ours.shape[1], num_tick)
diff = ours - pdok
show_image('pdok_' + path, pdok, x_positions, x_labels, y_positions, y_labels)
show_image('ours_' + path, ours, x_positions, x_labels, y_positions, y_labels)
show_diff('diff_' + path, diff, x_positions, x_labels, y_positions, y_labels)
print(get_diff(diff))
print('thin!')
for path in lst_path:
with open("file/" + path + '.txt', "r") as ifile:
X, Y, Z = [], [], []
for line in ifile:
strip = line.strip('\n')
split = strip.split(',')
x, y, z = float(split[0]), float(split[1]), float(split[2])
X.append(x), Y.append(y), Z.append(z)
show_scatter(path, X, Y, Z)
def main():
comp()
if __name__ == "__main__":
main()
'''
with rasterio.open('file\M_58DZ1.tif') as src:
xmin, ymin, xmax, ymax = (190750, 351650, 191250, 352150)
window = src.window(xmin, ymin, xmax, ymax)
data = src.read(window=window)
meta = src.meta.copy()
meta.update({'height': data.shape[1], 'width': data.shape[2]})
with rasterio.open('file\image.tif', 'w', **meta) as dst:
dst.write(data)
'''