import numpy as np
import matplotlib.pyplot as plt
import os
# 读取文件夹
def file_name(file_dir):
L=[]
for root, dirs, files in os.walk(file_dir):
for file in files:
if os.path.splitext(file)[1] == '.npy':
L.append(os.path.join(root, file))
return L
# 将不同的树和在一起,形成整个树
totalTree = []
GAN_type = 'Men_389_Bifurcation'
dirList = file_name('C:\\Users\\Administrator\\Desktop\\finalTrees\\%s' % GAN_type)
for i in range(len(dirList)):
data = np.load(dirList[i])
for j in range(len(data)):
totalTree.append(data[j])
totalTree = np.array(totalTree)
print(GAN_type)
print(totalTree.shape)
######################################################################################
# data = np.load('C:\\Users\\Administrator\\Desktop\\NewDCGANTree\\Men_546_Convergence_DCGAN_gen_Trees\\Men_546_Convergence_DCGAN_gen4.npy')
# print(data.shape)
#######################################################################################
# 提取segment,data为一个树,返回一个数组,存储一段的坐标和管径[[x],[y],[diam]]
def extractSegment(data):
# 将数组变为列表
dataList = []
for i in range(len(data)):
fencha = []
for j in range(len(data[i])):
fencha.append(data[i][j].tolist())
dataList.append(fencha)
segments = []
for i in range(len(dataList)):
if [dataList[i][0],dataList[i][3],dataList[i][6]] in segments:
segments.append([dataList[i][0],dataList[i][3],dataList[i][6]])
if [dataList[i][1],dataList[i][4],dataList[i][7]] not in segments:
segments.append([dataList[i][1],dataList[i][4],dataList[i][7]])
if [dataList[i][2],dataList[i][5],dataList[i][8]] not in segments:
segments.append([dataList[i][2],dataList[i][5],dataList[i][8]])
# 在将提取的segment变为数组
segments_ = []
for i in range(len(segments)):
segment = [np.array(segments[i][0]),np.array(segments[i][1]),np.array(segments[i][2])]
segments_.append(segment)
segments = np.array(segments_)
print("总共血管段数: ", len(segments))
return segments
# 得到分支x,y坐标,segment为单个分支
def get_axis_segment(segment):
x = segment[0]
y = segment[1]
diam = segment[2]
return x, y, diam
# 距离计算公式
def get_len(x1,x2,y1,y2):
diff_x = (x1-x2)**2
diff_y = (y1-y2)**2
length = np.sqrt(diff_x+diff_y)
return length
#计算分支总长度,segment为单个分支
def get_total_len_diam(segment):
x, y, diam = get_axis_segment(segment)
# 相邻两点距离列表
lin_list = []
for i in range(1,len(x)):
lin_len = get_len(x[i-1],x[i],y[i-1],y[i])
lin_list.append(lin_len)
# 一段总长度
total_len = 0
for file in lin_list:
total_len += file
# 返回这一段的长度和管径大小
return total_len,diam[0]
# data为一个树,返回异质性CV值,和平均值
def getAvgAndCV(data):
segments = extractSegment(data)
# 计算一个树中所有段的长度和管径,加入列表
totalLenList = []
diamList = []
for i in range(len(segments)):
total_len,diam = get_total_len_diam(segments[i])
totalLenList.append(total_len)
diamList.append(diam)
# 平均值
lenMean = np.mean(totalLenList)
diamMean = np.mean(diamList)
# 标准差
lenSD = np.std(totalLenList,ddof=1)
diamSD = np.std(diamList,ddof=1)
# CV值
lenCV = lenSD/lenMean
diamCV = diamSD/diamMean
return lenMean, diamMean, lenCV, diamCV
lenMean, diamMean, lenCV, diamCV = getAvgAndCV(totalTree)
print("Length Avg: %f, Length CV: %f " % (lenMean, lenCV))
print("Diameter Avg: %f, Diameter CV: %f" % (diamMean, diamCV))