如何解决... ValueError:无法将float NaN转换为整数

时间:2016-05-10 16:33:44

标签: python floating-point scipy nan ndimage

我运行了相当复杂的代码,所以我不会因为我之前有过这样的工作而烦恼,但现在却出现了这个错误。

粒子是一个填充0或255的3D元组,我使用scipy质心函数,然后尝试将值转换为最接近的整数(因为我处理数组)。在最后一行找到错误......任何人都可以解释为什么这可能是??

第二行填充粒子 第3行删除具有不同标签的任何周围粒子(这是所有标签的for循环)

Particle = []
Particle = big_labelled_stack[x_start+20:x_stop+20,y_start+20:y_stop+20,z_start+20:z_stop+20]
Particle = np.where(Particle == i ,255,0)

CoM = scipy.ndimage.measurements.center_of_mass(Particle)
CoM = [ (int(round(x)) for x in CoM ] 

提前致谢。如果你需要更多的代码,请问,但我认为它不会对你有所帮助,而且非常混乱。

##################更多代码
 border = 30

[labelled_stack,no_of_label] = label(labelled,structure_array,output_type)
# RE-LABEL particles now no. of seeds has been reduced! LAST LABELLING

#Increase size of stack by increasing borders and equal them to 0; to allow us to cut out particles into cube shape which else might lye outside the border
h,w,l = labelled.shape
big_labelled_stack = np.zeros(shape=(h+60,w+60,l+60),dtype=np.uint32)

 # Creates an empty border around labelled_stack full of zeros of size border

if (no_of_label > 0):       #Small sample may return no particles.. so this stage not neccesary

    info = np.zeros(shape=(no_of_label,19))          #Creates array to store coordinates of particles

    for i in np.arange(1,no_of_label,1):  


        coordinates = find_objects(labelled_stack == i)[0] #Find coordinates of label i.
        x_start = int(coordinates[0].start)
        x_stop = int(coordinates[0].stop)
        y_start = int(coordinates[1].start)
        y_stop = int(coordinates[1].stop)
        z_start = int(coordinates[2].start)
        z_stop = int(coordinates[2].stop)

        dx = (x_stop - x_start)  
        dy = (y_stop - y_start)
        dz = (z_stop - z_start)

        Particle = np.zeros(shape=(dy,dx,dz),dtype = np.uint16)                
        Particle = big_labelled_stack[x_start+30:x_start+dx+30,y_start+30:y_start+dy+30,z_start+30:z_start+dz+30]
        Particle = np.where(Particle == i ,255,0)

        big_labelled_stack[border:h+border,border:w+border,border:l+border] = labelled_stack   
        big_labelled_stack = np.where(big_labelled_stack == i , 255,0)
        CoM_big_stack = scipy.ndimage.measurements.center_of_mass(big_labelled_stack)
        C = np.asarray(CoM_big_stack) - border



        if dx > dy:
            b = dx
        else:                           #Finds the largest of delta_x,y,z and saves as b, so that we create 'Cubic_Particle' of size 2bx2bx2b (cubic box)
            b = dy
        if dz > b:
            b = dz

        CoM = scipy.ndimage.measurements.center_of_mass(Particle)
        CoM = [ (int(round(x))) for x in CoM ] 


        Cubic_Particle = np.zeros(shape=(2*b,2*b,2*b))
        Cubic_Particle[(b-CoM[0]):(b+dx-CoM[0]),(b-CoM[1]):(b+dy-CoM[1]),(b-CoM[2]):(b+dz-CoM[2])] = Particle

        volume = Cubic_Particle.size    # Gives volume of the box in voxels



        info[i-1,:] = [C[0],C[1],C[2],i,C[0]-b,C[1]-b,C[2]-b,C[0]+b,C[1]+b,C[2]+b,volume,0,0,0,0,0,0,0,0] # Fills an array with label.No., size of box, and co-ords            


else:
    print('No particles found, try increasing the sample size')
    info = []

好吧,所以我有一堆标记的粒子,我想要做的事情有两件事,首先找到每个粒子的质量中心,而不是标签,这是CoM_big_labelled_stack(和C)所做的。并将co-ords存储在名为info的列表(元组)中。我也试图在粒子周围创建一个立方体盒子,以质心为中心(与CoM变量有关),所以首先我用scipy中的find objects函数找到一个粒子,然后我用它们坐标在粒子周围创建一个非立方体框,并找到它的质心。然后找到盒子的最长尺寸并称之为b,创建一个大小为2b的立方体盒子,并在正确的位置填充粒子。

对不起,这段代码很乱,我是Python的新手

0 个答案:

没有答案