3

I'm running quite a complex code so I won't bother with details as I've had it working before but now im getting this error.

Particle is a 3D tuple filled with 0 or 255, and I am using the scipy centre of mass function and then trying to turn the value into its closest integer (as I'm dealing with arrays). The error is found with on the last line... can anyone explain why this might be??

2nd line fills Particle 3rd line deletes any surrounding particles with a different label (This is in a for loop for all labels)

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 ] 

Thanks in advance. If you need more code just ask but I dont think it will help you and its very messy.

################## MORE CODE
 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 = []

Ok, so I have a stack full of labelled particles, there are two things I am trying to do, first find the centre of masses of each particle with respect ot the labelled_stack which is what CoM_big_labelled_stack (and C) does. and stores the co-ords in a list (tuple) called info. I am also trying to create a cubic box around the particle, with its centre of mass as the centre (which is relating to the CoM variable), so first I use the find objects function in scipy to find a particle, i then use these coordinates to create a non-cubic box around the particle, and find its centre of mass.I then find the longest dimension of the box and call it b, creating a cubic box of size 2b and filling it with particle in the right position.

Sorry this code is a mess, I am very new to Python

Scott Alistair
  • 161
  • 4
  • 12
  • 1
    Is your "particle" empty? If so, you may be trying to compute the centre of mass of an empty set, which looks like 0/0 and would give you NaNs. – Gareth McCaughan May 10 '16 at 16:37
  • I think you are right, this must be the problem, Thank you! Can't find out why its empty though – Scott Alistair May 10 '16 at 17:29
  • Well, it'll happen if nothing within your 40x40x40 region has value `i`, whatever `i` happens to be. E.g., if you've got a block full of 0s and `i>0`, or if all your values are at most 200 and `i==255`. Do you want to provide a bit more of the code? – Gareth McCaughan May 10 '16 at 21:07
  • OK i have done, I'm scared its a bit incomprehensible though – Scott Alistair May 11 '16 at 09:36
  • I think maybe i need to refill big_labelled_stack each time in the loop!?! – Scott Alistair May 11 '16 at 09:38
  • Related directly to the error itself: there is no equivalent for NaN for integers, which is why the conversion fails. That would be the actual answer to your question, so you may want to rephrase your question. –  May 11 '16 at 09:51
  • Probably not -- I don't see that anything is modifying it in the loop. The following is certainly not your problem, but almost certainly a bug: your `i` loop is iterating from 1 *inclusive* to `no_of_label` *exclusive*, and I bet you actually want to go 1 further than that. – Gareth McCaughan May 11 '16 at 10:45
  • I take it your `find_objects` function is meant to return something like a bounding box for each maximal connected set of cells containing a given label. Are the "start" and "stop" coordinates it returns inclusive or exclusive? In particular, consider the case where you have a 1-cell object at, say, position (10,10,10); will you get start=10 and stop=10, or start=10 and stop=11? If it's the former, then your `Particle` is going to be missing its "higher" faces, and if any dimension is of size 1 it's going to be empty. – Gareth McCaughan May 11 '16 at 10:51
  • Ohhhh, wait, you *are* modifying `big_labelled_stack` in the loop. Yeah, don't do that, that'll break everything. – Gareth McCaughan May 11 '16 at 10:59
  • 1
    Don't "refill" it, though. Use a different name for the thing you get out of `np.where` -- it isn't a labelled stack any more. – Gareth McCaughan May 11 '16 at 11:01

0 Answers0