Home > Enterprise >  merge two thresholds for two 3D arrays into a list
merge two thresholds for two 3D arrays into a list

Time:07-30

I have the first 3D array of size (50,250,250) that includes data points (1,2,3,4,5). I set up a threshold that is 3, where the data points above should equal to 1 and below it equal to 0. the only exception is when the data points are equal to 3, it has to test the second threshold (threshold1=50) that is based on the second 3D array of size (50,250,250). my equation is how to include the two thresholds in my code! In other words, the for loop will check every datapoint in array 1 and perform the first threshold testing, if the datapoint is equal to 3, the for loop should check the counterpart of that datapoint in the second array for the second threshold testing! I have tried the below code, but the results did not make sense

res1=[]
f1=numpy.ones((250, 250))
threshold=3
threshold1=30

for i in array1:
        i = i.data
        ii= f1*i
        ii[ii < threshold]  = 0
        ii[ii > threshold]  = 1
        res1.append(ii)
        if ii[ii == threshold]:
            for j in array2:
                j = j.data
                jj[jj < threshold1]  = 0
                jj[jj > threshold1]  = 1
                res1.append(jj)

Array1:
   array([[[0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        ...,
        [3., 3., 3., ..., 0., 0., 0.],
        [3., 3., 3., ..., 0., 0., 0.],
        [3., 3., 3., ..., 0., 0., 0.]],

       [[0., 0., 0., ..., 0., 0., 1.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        ...,
        [3., 3., 3., ..., 0., 0., 0.],
        [3., 3., 3., ..., 0., 0., 0.],
        [3., 3., 3., ..., 0., 0., 0.]],

    Array2:[[        nan,         nan,         nan, ...,         nan,
       0.9839769,   1.7042577],
    [        nan,         nan,         nan, ...,         nan,
             nan,         nan],
    [        nan,         nan,         nan, ...,   3.2351596,
       2.0924768,   1.7604152],
    ...,
    [        nan,         nan,         nan, ..., 158.48865  ,
     158.48865  , 125.888    ],
    [        nan,         nan,         nan, ..., 158.48865  ,
     158.48865  , 158.48865  ],
    [        nan,         nan,         nan, ..., 125.88556  ,
     158.48865  , 158.48865  ]],

the produced list (rest1)

`[array([[0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        ...,
        [1., 1., 1., ..., 0., 0., 0.],
        [1., 1., 1., ..., 0., 0., 0.],
        [1., 1., 1., ..., 0., 0., 0.]]),
 array([[0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        ...,
        [1., 1., 1., ..., 0., 0., 0.],
        [1., 1., 1., ..., 0., 0., 0.],
        [1., 1., 1., ..., 0., 0., 0.]]),
 array([[0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],`   

CodePudding user response:

IIUC, for your second if condition, you are trying to see whether there is at least a 3 value in that array1, and then you will choose that 2D array of the same position. In that case, you should use in operator.

for i in range(len(array1)):
    if threshold in array1[i]:
        array2[i][array2[i] < threshold1]  = 0
        array2[i][array2[i] > threshold1]  = 1
        res1.append(array2[i])
    else:    
        array1[i][array1[i] < threshold]  = 0
        array1[i][array1[i] > threshold]  = 1
        res1.append(array1[i])

The above method is a bit lengthy for numpy. There's a numpy way to do this, too.

array1[array1 < threshold] = 0
array1[array1 > threshold] = 1

array2_condition = np.unique(np.argwhere(array1 == 3)[:,0]) # return the index of array1 if 3 in array1
chosen_array2 = array2[array2_condition]
chosen_array2[chosen_array2 < threshold1] = 0
chosen_array2[chosen_array2 > threshold1] = 1

array2[array2_condition] = chosen_array2 # if you still want array2 values to be changed

res1 = array1
res1[array2_condition] = chosen_array2  # Final result

Update

As was mentioned by the OP, every 2D array has at least a 3 in it. So, the array2_condition is not applicable. Instead, we will modify the array2_condition and use a for loop to change the elements.

res1 = array1
res1[res1 < threshold] = 0
res1[res1 > threshold] = 1

array2_condition = np.argwhere(array1 == 3)

for data in array2_condition:
    if array2[tuple(data)] > threshold1:
        res1[tuple(data)] = 1
    elif array2[tuple(data)] < threshold1:
        res12[tuple(data)] = 0
  • Related