SubCAM37插值後分別對xy做歸一化
來自專欄 《人工智慧》
import numpy as npimport operatorimport osimport copyfrom matplotlib.font_manager import FontPropertiesfrom scipy.interpolate import lagrangeimport randomimport matplotlib.pyplot as pltimport math np.set_printoptions(suppress=True)# 把opt文件內的逗號變為空格#數據在我的百度雲資料庫txt文件,及opt文件np.set_printoptions(threshold=np.inf) #輸出全部矩陣不帶省略號random.seed(10)# with open(opt\SubCAM.opt,r) as f:# file = f.read()# file = file.strip().split(
)# for data in file:# # print(data)# data = data.replace(,, )#replace不能用於列表# data = data.replace(-1,0)# with open(txt\SubCAM1.txt,a) as f:# f.write(data+
)##########################################data = np.loadtxt(txt//SubCAM.txt)# data = data.astype(np.int64)#變為整數,易於觀看數字x1 = data[:,5]#x起點坐標x2 = data[:,9]#x終點坐標y1 = data[:,6]#y起y2 = data[:,10]#y起z1 = data[:,4]#IDpartz2 = data[:,8]#IDpartdiam = data[:,12]# print(y1.tolist())# print(y2.tolist())# print(z1.tolist())# print(z2.tolist())s1 = [a1 for a1 in range(1,len(x1)-1) if z1[a1]==z2[a1-1]!=-1 or z1[a1]!= z2[a1-1]]#id相同不等於0,或id不同# print(s1)lx = []#x1,x2相同的部分組成的列表lxqi = []lxzg = []for i1 in range(len(s1)-1): b1 = x1[s1[i1]:s1[i1+1]] b1 = b1.tolist() b2 = x2[s1[i1+1]-1]#s1[i1]相當於a1# b1 = b1 + [b2]#把與x2最後相連的一個數和x1拼接起來 b5 = z1[s1[i1]]#x,y起點id b1qi_id = [b5]+b1 +[b2] b6 = z2[s1[i1+1]-1]#x,y終點id b1zg_id = [b6] + b1+[b2] lx.append(b1) lxqi.append(b1qi_id) lxzg.append(b1zg_id)###################################################ly = []#y坐標以及管徑大小for i3 in range(len(s1)-1): b3 = y1[s1[i3]:s1[i3+1]] b3 = b3.tolist() b4 = y2[s1[i3+1]-1]#y最後一個不相等的數 b3 = b3 + [b4] dm = diam[s1[i3+1]-1] b3 = b3 + [dm]#加上管徑 ly.append(b3)###################################################### lxwei =copy.deepcopy(lx)#深拷貝,不改變原來的值# lywei = copy.deepcopy(ly)# print(lx)# print(ly)#帶有起點id的x坐標與y坐標合併for q1 in range(len(lxqi)): for q2 in range(len(ly[q1])): lxqi[q1].append(ly[q1][q2])#帶有終點id的x坐標與y坐標合併for p1 in range(len(lxzg)): for p2 in range(len(ly[p1])): lxzg[p1].append(ly[p1][p2])lxqi.sort(key=operator.itemgetter(0))#排序,只按照第一個索引大小排序tou = lxqilxzg.sort(key=operator.itemgetter(0)) wei = lxzg # ########################################## final = [] # for i in range(len(tou)-1):# for dwei in wei:# if dwei[0]==tou[i][0] and dwei[0]==tou[i+1][0]:# zuhe = [dwei,tou[i],tou[i+1]]# final.append(zuhe)# print(final)toudeng = []weideng = []for dwei in wei: for i in range(len(tou)-1): if dwei[0] ==tou[i][0] and dwei[0]==tou[i+1][0]: toud = [dwei,tou[i],tou[i+1]] toudeng.append(toud)for dtou in tou: for i in range(len(wei)-1): if dtou[0] == wei[i][0] and dtou[0]==wei[i+1][0]: weid = [wei[i],wei[i+1],dtou] weideng.append(weid)# ###################################################datatoudeng = []dataweideng = []#去掉起點idfor i in range(len(toudeng)): a = toudeng[i][0][1::] b = toudeng[i][1][1::] c = toudeng[i][2][1::] d = [a]+[b]+[c] datatoudeng.append(d)for i in range(len(weideng)): a1 = weideng[i][0][1::] b1 = weideng[i][1][1::] c1 = weideng[i][2][1::] d1 = [a1]+[b1]+[c1] dataweideng.append(d1)# print(dataweideng)#####################################################################判斷管徑信息是否加進列表,若未加進則只為x,y坐標,為偶數for i in range(len(dataweideng)): a = dataweideng[i] assert len(a[0])%2==1 assert len(a[1])%2==1 assert len(a[2])%2==1for i in range(len(datatoudeng)): a = datatoudeng[i] assert len(a[0])%2==1 assert len(a[1])%2==1 assert len(a[2])%2==1finaldata = datatoudeng +dataweideng#未插值final = datatoudengdata7 = []for i in range(len(final)): a = np.array(final[i]) if a.shape==(3,7): b = a.tolist() data7.append(b)final = np.array(data7)x = final[:,:,0:3]y = final[:,:,3:6]############################旋轉def rotate(angle,valuex,valuey): rotatex = math.cos(angle)*valuex -math.sin(angle)*valuey rotatey = math.cos(angle)*valuey + math.sin(angle)* valuex return rotatex,rotateyrotatedata = []for i in range(0,360,3):#3,5 x1,y1 = rotate(i,x,y) final[:,:,0:3] = x1 final[:,:,3:6] = y1 rotatedata.append(final)finaldata = []for file in rotatedata: for data in file: finaldata.append(data)finaldata = np.array(finaldata)###################################### 插值data = finaldatafinaldata = []for i in range(len(data)): file = data[i] dataxy = [] for j in range(len(file)): lenlist = len(file[j]) halflen = lenlist//2 insert1 = []# insert2 = insert1[1] for k in range(halflen-1): sourcex = [file[j][k],file[j][k+1]] sourcey = [file[j][k+halflen],file[j][k+halflen+1]] insertx = np.linspace(file[j][k],file[j][k+1],4) f = lagrange(sourcex,sourcey) inserty = f(insertx) insertx = insertx.tolist() inserty = inserty.tolist() xy = [insertx,inserty] insert1.append(xy) ax1 =insert1[0][0]#第一個0當k=0時,第二個0是x坐標 ax2 = insert1[1][0]#當k=1時,x坐標 ax12 = insert1[0][0]+insert1[1][0][1::] ay1 = insert1[0][1]#當k=0時,y坐標 ay2 = insert1[1][1]#當k=1時,y坐標 ay12 = insert1[0][1]+insert1[1][1][1::]# xy = ax12 + ay12 +[file[j][-1]]#在此處加入了管徑信息 xy = ax12 + ay12#未加入管徑信息 dataxy.append(xy) finaldata.append(dataxy)finaldata = np.array(finaldata)print(finaldata.shape)print(np.max(finaldata))print(np.min(finaldata))#########################################去除管徑信息finalSubCAM = []final = finaldatafor i in range(len(final)): finalx = final[i][:,0:7] finaly = final[i][:,7:14] Xmax = np.max(finalx) Xmin = np.min(finalx) Ymax = np.max(finaly) Ymin = np.min(finaly) normx = (finalx-Xmin)/(Xmax-Xmin) normy = (finaly-Ymin)/(Ymax-Ymin) normxy = np.concatenate((normx,normy),axis=1) finalSubCAM.append(normxy)finaldata = np.array(finalSubCAM)print(np.min(finaldata))print(np.max(finaldata))print(finaldata.shape)#####################################np.save(C:\eclipse\eclipse\workspace\回憶真實血管網路\各種GAN變體\SubCAM對xy分別重新插值歸一化.npy,finaldata)######################################## 可視化旋轉效果(未加管徑可視化)# final = finaldata# for file in final:# lenth0 = len(file[0])# lenth1 = len(file[1])# lenth2 = len(file[2])# ban0 = lenth0 // 2# ban1 = lenth1 // 2# ban2 = lenth2 // 2# plt.plot(file[0][0:ban0],file[0][ban0:lenth0])# plt.plot(file[1][0:ban1],file[1][ban1:lenth1])# plt.plot(file[2][0:ban2],file[2][ban2:lenth2])# plt.show()# ########################################### 可視化旋轉效果(加入管徑可視化)# final = finaldata# for file in final:# lenth0 = len(file[0])-1# lenth1 = len(file[1])-1# lenth2 = len(file[2])-1# ban0 = lenth0 // 2# ban1 = lenth1 // 2# ban2 = lenth2 // 2# plt.plot(file[0][0:ban0],file[0][ban0:lenth0])# plt.plot(file[1][0:ban1],file[1][ban1:lenth1])# plt.plot(file[2][0:ban2],file[2][ban2:lenth2])# plt.show()
推薦閱讀:
※經典的分類例子:
※利用大數據促進可持續發展
※通俗易懂說數據挖掘十大經典演算法
※大數據講座一:大數據中的數據流向
※數據分析師該這樣霸氣回應「0.00008的轉化也很好」的謬論