0

我正在尝试根据从输入文件中读取的 3 个不同列表创建一个矩阵。我需要一个包含 3 列的矩阵:Col_2 首先代表 x 坐标,然后 Col_3 代表 y,Col_4 代表第三个位置的 z。

目标是将此矩阵输入到 ConvexHulll 函数中并获得一个数字,然后我将其附加到另一个数组中。

现在我正在使用这段代码,但没有创建输出文件,我认为这是因为矩阵有问题。(此代码在 ACT 扩展的脚本中调用)

在此先感谢您的帮助! 我没有设法导入我的输入文件,但它看起来像这样(33563 行)

import sys
import os

def ScipyRW(UserFiles,inputfile,outputfile,SF):
#Sample code for testing Scipy library from IronPython(ACT)
import scipy as sp 
import numpy as np

scaleFactor=float(SF) 

print(UserFiles)
print(inputfile)
print(outputfile)
print(scaleFactor)

fn=open(inputfile,'r')
line=fn.readline()


Col_1=[]
Col_2=[]
Col_3=[]
Col_4=[]

while line:
    v=line.split(",")
    Col_1.append(float((v[0]))) #reading column 1
    Col_2.append(float((v[1]))) #reading column 2
    Col_3.append(float((v[2]))) #reading column 3
    Col_4.append(float((v[3]))) #reading column 4
    line=fn.readline()
fn.close()

print("csv file read")

from scipy.spatial import ConvexHull

NodeNo=np.append(Col_1)
Matrix=np.array([Col_2],[Col_3],[Col_4])

myhull = ConvexHull(Matrix)
vol= hull.volume
ar = hull.area

myICV = vol-(ar*scaleFactor)/1000
print(myICV)

NodeNo=np.array(Col_1)
ICV=np.full_like(NodeNo,myICV)

np.savetxt(outputfile,(NodeNo,ICV),delimiter=',',fmt='%f') #save output into txt file

print ("Input csv file processed by numpy and written out")

ScipyRW(sys.argv[1],sys.argv[2],sys.argv[3],sys.argv[4])
4

1 回答 1

0

我稍微修改了你的脚本:

import scipy as sp 
import numpy as np
inputfile = "path/to/coordinate/file"

Col_1=[]
Col_2=[]
Col_3=[]
Col_4=[]
# replaced while with for-loop
for line in open(inputfile):
    v = line.split(',')
    Col_1.append(float((v[0]))) #reading column 1
    Col_2.append(float((v[1]))) #reading column 2
    Col_3.append(float((v[2]))) #reading column 3
    Col_4.append(float((v[3]))) #reading column 4
    
from scipy.spatial import ConvexHull

# Replaced append with direct assignment
NodeNo=Col_1
# Added a matrix transpose and fixed brackets
Matrix=np.array([Col_2,Col_3,Col_4]).T

myhull = ConvexHull(Matrix)
vol= myhull.volume
ar = myhull.area
scaleFactor = 1

myICV = vol-(ar*scaleFactor)/1000
print(myICV)

NodeNo=np.array(Col_1)
ICV=np.full_like(NodeNo,myICV)
于 2021-05-31T05:41:21.623 回答