2

Requirement:

I would like to make a function in python which is given 2 arrays, 1 bigger than the second one.
I would like the bigger one to take the same size that the smaller one.
BUT not only removing the end or beginning of the array I would like to keep a similar standard deviation of the chosen index of the bigger array.

For example :

For an array of size 10 and an array of size 95 the array of size 95 should become size 10 with for example the selected index [8, 17, 27, 36, 46, 55, 65, 74, 84, 93]
in pseudo-code an example of expected result would be :

len_bigger_array=95 # example where len(bigger_array)=95
len_smaller_array=10 # example where len(smaller_array)=10
result_array=[8, 17, 27, 36, 46, 55, 65, 74, 84, 93] #index of bigger_array with similar distance between each other
len(result_array)=10 #like len_small_array

1st Try :

total = len_bigger_array #len_bigger_array is len(bigger_array)
wanted = len_smaller_array #len_smaller_array is len(smaller_array)
div = 0
mod = 0
result_array = []
divider = int(total/wanted)
rest= total/wanted - divider
for x in range (0, total):
    div=div+1
    if (div >= divider):
        if (mod >= 0.95):
            div=div-1
            mod=0
        else:
            result_array.append(x)
            mod=mod+rest
            div=0
print(result_array, len(result_array))

And the result of this try :

  • for len_bigger_array=95 and len_smaller_array=10 it works : result_array=[8, 17, 27, 36, 46, 55, 65, 74, 84, 93] and len(result_array)=10
  • for len_bigger_array=1999 and len_smaller_array=100 it works also : len(result_array)=100 and contain index of bigger_array with a similar distance between each
  • for len_bigger_array=182 and len_smaller_array=69 it doesn't work: result_array contain index of bigger_array with a similar distance between each but len(result_array)!=len_smaller_array
  • for len_bigger_array=95 and len_smaller_array=33 it doesn't work: result_array contain index of bigger_array with a similar distance between each but len(result_array)!=len_smaller_array

2nd Try :

import math
total = len_bigger_array
wanted = len_smaller_array
result_array = []
divider = math.ceil(total/wanted) 
for x in range (0, total):
    if(x % divider == 0):
        result_array.append(x)
missingSlice = wanted - len(result_array)
print(missingSlice)
if (missingSlice>0):
    newWanted=int(total/missingSlice)
    print(newWanted)
    for x in range (0, total):
        if (x % newWanted ==0 and x !=0):
            if x not in l:
                result_array.append(x)
            else:
                result_array.append(x-1)
print(result_array,len(result_array))

And the result of this try :

  • for len_bigger_array=95 and len_smaller_array=10 it works : result_array=[0, 10, 20, 30, 40, 50, 60, 70, 80, 90] and len(result_array)=10
  • for len_bigger_array=1999 and len_smaller_array=100 it works also : len(result_array)=100 and contain index of bigger_array with a similar distance between each
  • for len_bigger_array=182 and len_smaller_array=69 it works: also : len(result_array)=69 and contain index of bigger_array with a similar distance between each
  • for len_bigger_array=95 and len_smaller_array=33 it doesn't work: result_array contain index of bigger_array with a similar distance between each but len(result_array)=32 instead of 33

Context information :

smaller_array and bigger_array contain slice of CT image so they can vary.
the result array should contain index of bigger_array, those index should have a similar standard deviation between each other in order to compare similar area between smaller_array and bigger_array (that have the same beginning and ending point but different number of slice).

SylwekFr
  • 308
  • 3
  • 21
  • you may have arrays that contain elements from which you can't find some subarray with similar stdev as you want, in which case you should expect to find the minimum difference between expected stdev and stdev of all the subarrays of the wanted size – kederrac Mar 01 '20 at 10:39
  • If you have the indices for the wanted subarray, where is the problem? – kederrac Mar 01 '20 at 11:01
  • how large can SMALL and BIG arrays be? If they are relatively small then brute force solution might suffice. – aiven Mar 01 '20 at 11:32
  • @kederrac the problem is the the result result array should be exactly the same size than the small array, And for example for len_bigger_array=182 and len_smaller_array=69 : len(result_array)=73 and should be 69, I have the index from the big array which are with similar distance from each other but too much of them – SylwekFr Mar 02 '20 at 10:45
  • @aiven the condition is mostly that small array < big array, let assume than small array will always be > 30 and big < 10000 but after they can be relatively close or no, here is more or less the difficulty – SylwekFr Mar 02 '20 at 10:48
  • if you already have the selected subarray(that has similar stdev) what is the purpose of your code? or what you mean by `selected index` – kederrac Mar 02 '20 at 10:53
  • @kederrac, the purpose of the code is that you have 2 arrays containing slice of images, they have same beginning and same end but different number of slice (so different length len_bigger_array and len_smaller_array), len(result_array) should be equal len(smaller_array) - the variable len_smaller_array. result_array should contain index of bigger_array with a similar standard deviation, so for example if it was simple and smaller_array=10 and bigger_array=30, result_array should take each 3 of bigger array (then len(result_array)=10 and the index have the same distance between each other) – SylwekFr Mar 02 '20 at 11:04

0 Answers0