Varlor
Varlor

Reputation: 1471

Python: Doing Calculations on array elements in a list

I have a list of arrays, in which each array represents a cell and the array elements are the coordinates x,y and z, the time point and the cell id. Here a sector of it:

cells=[   ..., 
   [ 264.847,  121.056,   30.868,   42.   ,  375.   ],
   [ 259.24 ,  116.875,   29.973,   43.   ,  375.   ],
   [ 260.757,  118.574,   32.772,   44.   ,  375.   ]]), array([[ 263.967,  154.089,   55.5  ,   38.   ,  376.   ],
   [ 260.744,  152.924,   55.5  ,   39.   ,  376.   ],
   [ 258.456,  151.373,   55.5  ,   40.   ,  376.   ],
   ..., 
   [ 259.086,  159.564,   48.521,   53.   ,  376.   ],
   [ 258.933,  159.796,   48.425,   54.   ,  376.   ],
   [ 259.621,  158.719,   51.606,   55.   ,  376.   ]]), array([[ 291.647,   57.582,   28.178,   38.   ,  377.   ],
   [ 284.625,   59.221,   30.028,   39.   ,  377.   ],
   [ 282.915,   59.37 ,   30.402,   40.   ,  377.   ],
   ..., 
   [ 271.224,   58.534,   23.166,   42.   ,  377.   ],
   [ 270.048,   58.738,   21.749,   43.   ,  377.   ],
   [ 268.38 ,   58.138,   20.606,   44.   ,  377.   ]]), array([[  87.83 ,  222.144,   26.258,   39.   ,  378.   ],
   [  99.779,  223.631,   24.98 ,   40.   ,  378.   ],
   [ 104.107,  224.177,   23.728,   41.   ,  378.   ],
   ..., 
   [ 127.778,  222.205,   23.123,   63.   ,  378.   ],
   [ 126.815,  222.347,   23.934,   64.   ,  378.   ],
   [ 127.824,  221.048,   25.508,   65.   ,  378.   ]]),...]

minimumCellCoors = cells
maximumCellCoors = cells
centoEdge = radius+fcr_size

Now i want to change the coordinates x, y and z, so the 0.,1. and 2. element of the arrays in the list to get them in a specific grid. The user gives the spacing for x,y and z and then the operation could look like:

 x_Coo=round(x_element/x)*x
 y_Coo=round(y_element/y)*y         
 z_Coo=round(z_element/z)*z

So the real question here is, how could i do a operation on all of the elements in the array ( or in this case the first three elements in the array in the list)?

EDIT

If i use list comprehension to the list like:

[np.round((cellID[:,0]-(centoEdge+1))/x)*x  for cellID in minimumCellCoors]
[np.round((cellID[:,1]-(centoEdge+1))/y)*y  for cellID in minimumCellCoors]
[np.round((cellID[:,2]-(centoEdge+1))/z)*z  for cellID in minimumCellCoors]

[np.round((cellID[:,0]+(centoEdge+1))/x)*x  for cellID in maximumCellCoors]
[np.round((cellID[:,1]+(centoEdge+1))/x)*y  for cellID in maximumCellCoors]
[np.round((cellID[:,2]+(centoEdge+1))/x)*z  for cellID in maximumCellCoors]

How could i fusion the single lists of arrays to one array again?

Best regards!

Upvotes: 1

Views: 677

Answers (1)

Kasravnd
Kasravnd

Reputation: 107287

First off you need to convert your list to a numpy array. It's more proper to create a numpy array instead of a list at first place. Then you can take advantage of numpy's vectorized operation support:

Here is an example:

In [45]: arr = np.arange(100).reshape(4, 5, 5)

In [46]: arr
Out[46]: 
array([[[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9],
        [10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19],
        [20, 21, 22, 23, 24]],

       [[25, 26, 27, 28, 29],
        [30, 31, 32, 33, 34],
        [35, 36, 37, 38, 39],
        [40, 41, 42, 43, 44],
        [45, 46, 47, 48, 49]],

       [[50, 51, 52, 53, 54],
        [55, 56, 57, 58, 59],
        [60, 61, 62, 63, 64],
        [65, 66, 67, 68, 69],
        [70, 71, 72, 73, 74]],

       [[75, 76, 77, 78, 79],
        [80, 81, 82, 83, 84],
        [85, 86, 87, 88, 89],
        [90, 91, 92, 93, 94],
        [95, 96, 97, 98, 99]]])


In [51]: arr[:,:,:3] = np.round(arr[:,:,:3]/5)*5 

In [52]: arr
Out[52]: 
array([[[ 0,  0,  0,  3,  4],
        [ 5,  5,  5,  8,  9],
        [10, 10, 10, 13, 14],
        [15, 15, 15, 18, 19],
        [20, 20, 20, 23, 24]],

       [[25, 25, 25, 28, 29],
        [30, 30, 30, 33, 34],
        [35, 35, 35, 38, 39],
        [40, 40, 40, 43, 44],
        [45, 45, 45, 48, 49]],

       [[50, 50, 50, 53, 54],
        [55, 55, 55, 58, 59],
        [60, 60, 60, 63, 64],
        [65, 65, 65, 68, 69],
        [70, 70, 70, 73, 74]],

       [[75, 75, 75, 78, 79],
        [80, 80, 80, 83, 84],
        [85, 85, 85, 88, 89],
        [90, 90, 90, 93, 94],
        [95, 95, 95, 98, 99]]])

Note that you can also perform the operations with same length arrays as well as scalars:

For instance you could also do the following:

In [53]: arr[:,:,:3] = np.round(arr[:,:,:3]/5)*[4, 5, 6]

Upvotes: 1

Related Questions