Skip to content

Commit 14a0933

Browse files
committed
1
0 parents  commit 14a0933

16 files changed

+16053
-0
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*[[:space:]].npy filter=lfs diff=lfs merge=lfs -text
2+
*.npy filter=lfs diff=lfs merge=lfs -text

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Landslide-Susceptibility
2+
3+
4+
5+
SlopeCardMake.py 输入tif格式的影响因子图层与以fid为灰度值的斜坡单元图层,输出fid.npy的斜坡单元卡片与1D因子。
6+
SlopeCardResize.py 将斜坡单元卡片rezise为统一大小并得到一个npy文件。
7+
data_read.py 数据读取与增强。
8+
nni_trial 借助nni实现模型的超参数优化,并进行滑坡易发性评价。
9+
10+
11+
requirement:keras/nni/pandas/numpy/matplotlib

SlopeCardMake.py

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Tue Oct 30 13:13:10 2018
4+
5+
@author: 75129
6+
"""
7+
8+
import scipy.io as sio
9+
from scipy import misc
10+
import numpy as np
11+
import matplotlib as plt
12+
from numpy.matlib import repmat
13+
from sklearn.decomposition import PCA
14+
from scipy import stats
15+
import pandas as pd
16+
17+
#import arcpy
18+
#totaldata读取
19+
yinzi_name=['altitude','aspect','fault','ndvi','plan','profile','rainfall','river','road','slope','spi','sti','twi','soil_envi','landuse_envi','lithology_envi']
20+
yinzi_type_ori=['float','float','float','float','float','float','float','float','float','float','float','float','float','str','str','str']
21+
#load_fn = 'ysxp_lable.mat'
22+
#load_data = sio.loadmat(load_fn)
23+
#lable = load_data['lable']
24+
#load_fn = 'img_yx.mat'
25+
#load_data = sio.loadmat(load_fn)
26+
#lable = load_data['lable'] #假设文件中存有字符变量是matrix,例如matlab中save(load_fn, 'matrix');当然可以保存多个save(load_fn, 'matrix_x', 'matrix_y', ...);
27+
lable = misc.imread('C:/Users/75129/Desktop/mypy/demo_all/SUyushan.tif')
28+
row=len(lable)
29+
col=len(lable[0])
30+
nums_factors=len(yinzi_name)
31+
img=np.zeros((row,col,nums_factors))
32+
i=0
33+
yinzi_type=[]
34+
for i in range(nums_factors):
35+
name=yinzi_name[i]
36+
PATH='C:/Users/75129/Desktop/mypy/demo_all/tif_yushan/'
37+
img[:,:,i]=misc.imread(PATH+name+'.tif')
38+
# if yinzi_type_ori[i]=='str':
39+
# plt.pyplot.figure()
40+
# plt.pyplot.imshow(img[:,:,i])
41+
ttt=img[:,:,i]
42+
ttt[ttt==-32768]=0
43+
# ttt[ttt==-3.402823e+038]=0
44+
img[:,:,i]=ttt
45+
# img_i=load_data[name]
46+
# ma=np.max(img_i)
47+
# mi=np.min(img_i)
48+
# img[:,:,i]=(img_i-mi)/(ma-mi)
49+
i=i+1
50+
#pca = PCA(n_components=3)
51+
#img1=np.reshape(img,(2636*2206,16))
52+
#img_pca=pca.fit_transform(img1)
53+
#img_pca=np.reshape(img_pca,(2636,2206,3))
54+
#img=img_pca
55+
#plt.pyplot.imshow(img_pca[:,:,0])
56+
xfanwei=[]
57+
yfanwei=[]
58+
print('开始裁剪 。')
59+
empty_index=[]
60+
max_id=np.max(lable[lable!=65536])
61+
yinzi_1d=np.zeros([max_id+1,nums_factors])
62+
for it in range(max_id,max_id+1):#4890
63+
fanwei=(lable==it)
64+
fanwei=fanwei+np.zeros((row,col))
65+
img_it=np.zeros((row,col,nums_factors))
66+
for i in range(nums_factors):
67+
img_it[:,:,i]=fanwei*img[:,:,i]
68+
69+
index_fanwei=np.argwhere(lable==it)
70+
if len(index_fanwei)!=0:
71+
x_min=min(index_fanwei[:,0])
72+
x_max=max(index_fanwei[:,0])
73+
y_min=min(index_fanwei[:,1])
74+
y_max=max(index_fanwei[:,1])
75+
img_it_resize=img_it[x_min:(x_max+1),y_min:(y_max+1),:]
76+
# for i in range(nums_factors):
77+
# plt.pyplot.figure()
78+
# plt.pyplot.imshow(img_it_resize[:,:,i])
79+
filename='C:/Users/75129/Desktop/mypy/demo_all/yushan_all_OriCard/'+str(it)
80+
np.save(filename,img_it_resize)
81+
xfanwei.append(x_max-x_min+1)
82+
yfanwei.append(y_max-y_min+1)
83+
84+
for f in range(nums_factors):
85+
x,y,z=img_it_resize.shape
86+
values=np.reshape(img_it_resize,[x*y,z])
87+
values_f=values[:,f]
88+
values_f=values_f[values_f!=0]
89+
if yinzi_type_ori[f]=='str':
90+
if values_f.size!=0:
91+
yinzi_1d[it,f]=stats.mode(values_f)[0][0]
92+
else:
93+
yinzi_1d[it,f]=np.mean(values_f)
94+
print(str(it)+' is done'+' x:'+str(x_max-x_min+1)+' y:'+str(y_max-y_min+1))
95+
print(yinzi_1d[it,15])
96+
else:
97+
empty_index.append(it)
98+
filename='C:/Users/75129/Desktop/mypy/demo_all/yushan_all_OriCard/'+str(it)
99+
np.save(filename,np.zeros([1,1,nums_factors]))
100+
print(str(it)+' is done,but it is empty')
101+
102+
df = pd.DataFrame(yinzi_1d,columns=yinzi_name)
103+
df.to_csv('C:/Users/75129/Desktop/mypy/demo_all/yushan_all_OriCard/yinzi1d.csv')
104+
105+
106+
# plt.pyplot.imshow(img_it_resize[:,:,0])
107+
print('裁剪结束')
108+
109+
#band=len(totaldata[0][0])
110+
#totaldata=np.reshape(totaldata,(row*col,band))
111+
112+
113+
#假如这是pro,显示最后的概率分布图
114+
#pro=totaldata[:,4]
115+
#pro_map=np.reshape(pro,(row,col))
116+
#plt.pyplot.imshow(pro_map,cmap='bone')
117+
118+
#如果要输入到arcgis当中,就把pro_map保存为tif

SlopeCardResize.py

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Fri Nov 2 16:03:36 2018
4+
5+
@author: 75129
6+
"""
7+
8+
import scipy.io as sio
9+
import numpy as np
10+
import matplotlib as plt
11+
import scipy.interpolate as siInter
12+
13+
def resample(img,x_size,y_size,method):
14+
# plt.pyplot.figure()
15+
# plt.pyplot.imshow(img)
16+
(xOldSize,yOldSize)=img.shape
17+
newImg=np.zeros((x_size,y_size))
18+
pointsNew=np.where(newImg>-999999)
19+
pointsNew=np.array(pointsNew,dtype=float)
20+
pointsNew=pointsNew.T
21+
valueImg=img.reshape((xOldSize*yOldSize,1))
22+
pointsOld=np.where(img>-999999)
23+
pointsOld=np.array(pointsOld,dtype=float)
24+
pointsOld=pointsOld.T
25+
pointsOld[:,0]=pointsOld[:,0]/(xOldSize-1)*(x_size-1)
26+
pointsOld[:,1]=pointsOld[:,1]/(yOldSize-1)*(y_size-1)
27+
valueNewImg=siInter.griddata(pointsOld,valueImg,pointsNew,method)
28+
NewImg=np.reshape(valueNewImg,(x_size,y_size))
29+
# plt.pyplot.figure()
30+
# plt.pyplot.imshow(NewImg)
31+
return NewImg
32+
33+
34+
35+
36+
37+
#methoddict={0:'cubic',1:'nearest',2:'cubic',3:'nearest',4:'nearest',5:'cubic',6:'cubic',7:'cubic',8:'cubic',9:'cubic',10:'cubic',11:'cubic',12:'nearest',13:'cubic',14:'cubic',15:'cubic'}
38+
#{'altitude','aspect','fault','landuse','lithology','ndvi','plan','profile','rainfall','river','road','slope','soil','spi','sti','twi'}
39+
size_x=40
40+
size_y=40
41+
num_factos=16
42+
num_samples=7598
43+
img_new=np.zeros((num_samples,size_x,size_y,num_factos))
44+
45+
for it in range(num_samples):
46+
filename='C:/Users/75129/Desktop/mypy/demo_all/yushan_all_OriCard/'+str(it)
47+
img_resize=np.zeros((size_x,size_y,num_factos))
48+
img=np.load(filename+'.npy')
49+
(row,col)=img[:,:,0].shape
50+
if row==0 :
51+
img_resize=np.zeros(size_x,size_y,num_factos)
52+
print('row col == 0')
53+
54+
elif row==1 or col==1:
55+
img_resize=np.ones([size_x,size_y,num_factos])
56+
for i in range(num_factos):
57+
img_resize[:,:,i]=img_resize[:,:,i]*np.double(img[0,0,i])
58+
print('row col == 1')
59+
else:
60+
for i in range(num_factos):
61+
img_resize[:,:,i]=resample(img[:,:,i],size_x,size_y,'nearest')
62+
63+
# newfilename='C:/Users/75129/Desktop/mypy/xp_kp_newsize8080/'+str(it)
64+
# np.save(newfilename,img_resize)
65+
# plt.pyplot.imshow(img_resize[:,:,0])
66+
67+
# newfilename='C:/Users/75129/Desktop/mypy/xp_kp_newsize8080/'+str(it)
68+
# img_it=np.load(newfilename+'.npy')
69+
img_new[it,:,:,:]=img_resize
70+
print(str(it)+' is done')
71+
# print(it)
72+
newfilename='C:/Users/75129/Desktop/mypy/demo_all/yushan_all_ResizeCard/all_yushan'
73+
np.save(newfilename,img_new)
74+
75+
76+
77+
78+
79+

__pycache__/data_read.cpython-36.pyc

5.98 KB
Binary file not shown.

0 commit comments

Comments
 (0)