0

My NewCandGrid are data of a of (18,4,3). I use it to calculate lolo. However, after applying the function DeTrend my NewCandGrid has changed its values, so NewCandGrid before is unequal to NewCandGrid after.

from statsmodels.nonparametric.smoothers_lowess import lowess

def DeTrend(TheCandData,TheMDI):
    '''' Fit a lowess to the data and remove the curve '''
    ''' default smoothing looks ok on test '''
    ''' Works on grids as well as vectors '''

    sizee=np.shape(TheCandData)
    if (len(sizee) < 2): #for TS
        gots=np.where(TheCandData > TheMDI)[0]
        los=lowess(TheCandData[gots],range(len(TheCandData[gots])))[:,1]
        TheCandData[gots]=TheCandData[gots]-los
    else: # for Grid
        for ltt in range(len(TheCandData[0,:,0])):
            for lnn in range(len(TheCandData[0,0,:])):
                gots=np.where(TheCandData[:,ltt,lnn] > TheMDI)[0]
                if (len(gots) > 12):
                    los=lowess(TheCandData[gots,ltt,lnn],range(len(TheCandData[gots,ltt,lnn])))[:,1]
                    TheCandData[gots,ltt,lnn]=TheCandData[gots,ltt,lnn]-los
                else:
                    TheCandData[:,ltt,lnn]=TheMDI

    return TheCandData # DETREND

print('NewCandGrid before: ', NewCandGrid)
lolo=DeTrend(NewCandGrid,mdi)
print('NewCandGrid after: ', NewCandGrid)

I do not understand how applying a function can change the data I inserted. I'd really appreciate if someone could explain it to me, please. What do I have to change so that NewCandGrid stays the same before and after?

NewCandGrid before: [[[-1.00000000e+30 -1.00000000e+30 -1.00000000e+30]
  [ 8.05583477e+00  3.98919654e+00 -1.00000000e+30]
  [ 2.96531844e+00  2.95409465e+00 -1.00000000e+30]
  [ 5.94939768e-01  1.34735203e+00  2.74114871e+00]]

 [[-1.00000000e+30 -1.00000000e+30 -1.00000000e+30]
  [ 6.38335228e-01 -2.79925972e-01 -1.00000000e+30]
  [ 3.11135244e+00 -8.68405342e-01 -1.00000000e+30]
  [ 1.50577343e+00  1.99039400e+00  3.82448101e+00]]

 [[-1.00000000e+30 -1.00000000e+30 -1.00000000e+30]
  [-7.40832031e-01  2.35169721e+00 -1.00000000e+30]
  [ 3.72192693e+00  3.37117839e+00 -1.00000000e+30]
  [ 1.43938363e+00  9.47059929e-01 -7.47185409e-01]]

 [[-1.00000000e+30 -1.00000000e+30 -1.00000000e+30]
  [-6.65916634e+00 -8.34969997e-01 -1.00000000e+30]
  [-1.00000000e+30  3.00343990e-01 -1.00000000e+30]
  [-1.10061586e+00 -1.80168962e+00 -1.61468470e+00]]

 [[-1.00000000e+30 -1.00000000e+30 -1.00000000e+30]
  [ 1.48833525e+00 -7.09414244e-01 -1.00000000e+30]
  [-1.00000000e+30  2.53207356e-01 -1.00000000e+30]
  [-1.06449015e-01  2.74019885e+00  1.01698208e+00]]

 [[-1.00000000e+30 -1.00000000e+30 -1.00000000e+30]
  [ 6.27502441e-01 -3.02469525e-02 -1.00000000e+30]
  [-1.00000000e+30  1.86177090e-01 -1.00000000e+30]
  [-5.10615826e-01 -9.71273124e-01 -1.50385153e+00]]

 [[-1.00000000e+30 -1.00000000e+30 -1.00000000e+30]
  [ 2.53332764e-01  4.38641578e-01 -1.00000000e+30]
  [ 9.76544976e-01 -1.01739563e-01 -1.00000000e+30]
  [-7.15060413e-01 -1.71919012e+00 -2.05718517e+00]]

 [[-1.00000000e+30 -1.00000000e+30 -1.00000000e+30]
  [ 4.80834961e-01  9.47252095e-01 -1.00000000e+30]
  [-1.73437643e+00 -2.08376384e+00 -1.00000000e+30]
  [-2.05644894e+00 -4.26168966e+00 -1.48135090e+00]]

 [[-1.00000000e+30 -1.00000000e+30 -1.00000000e+30]
  [-3.88332367e-01 -8.86359036e-01 -1.00000000e+30]
  [-2.65767217e+00  6.66365802e-01 -1.00000000e+30]
  [-2.10367155e+00 -6.35856450e-01 -2.05518410e-01]]

 [[-1.00000000e+30 -1.00000000e+30 -1.00000000e+30]
  [-1.65333271e+00 -4.31015015e-01 -1.00000000e+30]
  [-1.02017176e+00 -4.83268690e+00 -1.00000000e+30]
  [-1.23228252e+00 -3.22472882e+00 -4.78968477e+00]]

 [[-1.00000000e+30 -1.00000000e+30 -1.00000000e+30]
  [-1.57916641e+00 -2.07885790e+00 -1.00000000e+30]
  [ 3.11494827e-01 -2.63840532e+00 -1.00000000e+30]
  [-1.63950443e+00 -2.62919021e+00 -2.20051837e+00]]

 [[-1.00000000e+30 -1.00000000e+30 -1.00000000e+30]
  [-1.70833147e+00 -1.99385834e+00 -1.00000000e+30]
  [-1.66433752e+00  6.52844250e-01 -1.00000000e+30]
  [-1.53394890e+00 -3.59085655e+00 -4.40551805e+00]]

 [[-1.00000000e+30 -1.00000000e+30 -1.00000000e+30]
  [-1.32583201e+00 -1.23302543e+00 -1.00000000e+30]
  [-2.08767223e+00 -2.20382166e+00 -1.00000000e+30]
  [-9.12281930e-01 -3.09627342e+00 -1.97551858e+00]]

 [[-1.00000000e+30 -1.00000000e+30 -1.00000000e+30]
  [-2.07497910e-01 -3.49696092e-02 -1.00000000e+30]
  [-2.81710410e+00 -1.39548922e+00 -1.00000000e+30]
  [-6.63115680e-01  1.98318577e+00 -1.85135114e+00]]

 [[-1.00000000e+30 -1.00000000e+30 -1.00000000e+30]
  [ 5.18334687e-01  2.83918917e-01 -1.00000000e+30]
  [-1.07410395e+00 -2.31738880e-01 -1.00000000e+30]
  [ 1.25549519e+00  5.08310080e-01  6.29483223e-01]]

 [[-1.00000000e+30 -1.00000000e+30 -1.00000000e+30]
  [-8.50831985e-01 -1.30191422e+00 -1.00000000e+30]
  [-1.86600399e+00 -1.39257193e+00 -1.00000000e+30]
  [-1.22783792e+00 -1.81543982e+00 -5.14685333e-01]]

 [[-1.00000000e+30 -1.00000000e+30 -1.00000000e+30]
  [-1.59499872e+00 -4.87747282e-01 -1.00000000e+30]
  [-3.82017159e+00  9.49510872e-01 -1.00000000e+30]
  [ 4.74384636e-01  5.52580976e+00  4.51198196e+00]]

 [[-1.00000000e+30 -1.00000000e+30 -1.00000000e+30]
  [-1.39833128e+00 -6.67834654e-02 -1.00000000e+30]
  [-8.66819918e-01 -1.20905556e-01 -1.00000000e+30]
  [ 1.17132926e+00 -1.93356991e-01 -7.68851280e-01]]]
KMFWeb
  • 55
  • 1
  • 8
  • 2
    use a copy of `NewCandGrid` when passing it to `DeTrend` like so:`lolo=DeTrend(NewCandGrid.copy(),mdi)` – Anwarvic May 15 '20 at 12:41
  • 2
    Lines like `TheCandData[gots]=TheCandData[gots]-los` change the values in the array passed to `DeTrend`. – mkrieger1 May 15 '20 at 12:42

0 Answers0