Search code examples
pythonsignalsdata-analysisnumpy-ndarray

I need to resample a np array with signal resample


I have some .csv files with data of different lengths. I need to load some defined columns, sort them and resample to the same length (100%).

I have written some defined nested loops to sort the data, but the resampling function doesn't work in the end.

subjects   = ['S01_']
conditions = ['Shoe1_', 'Shoe2_']
trials     = ['Run_1', 'Run_2','Run_3']

results_GRF_AP = np.empty(shape=(100,6))*np.NaN
results_GRF_ML = np.empty(shape=(100,6))*np.NaN
results_GRF_VERT = np.empty(shape=(100,6))*np.NaN

results_AnkPower = np.empty(shape=(100,6))*np.NaN

ind = 0
for s, subject in enumerate(subjects):
    for c, condition in enumerate(conditions):
        for t, trial in enumerate(trials):
            ind += 1
            filename = path + subject + condition + trial + extension
            try:
                data2 = pd.read_csv(filename,delimiter = ';')
                data2=np.array(data2)
                    
            except Exception as err:
                print(filename, err)          
                continue
            else:
                print(filename, 'loaded')
                pass
            
            threshold = 30 ## defined threshold value in Newton from vertical GRF
            signal = np.array(data2[:,4])
            indices_bigger_than_threshold = np.where(signal > threshold)[0] # get item
            non_consecutive = np.where(np.diff(indices_bigger_than_threshold) != 1)[0]+1 # +1 for selecting the next
            first_bigger_than_threshold1 = np.zeros_like(signal, dtype=np.bool)
            first_bigger_than_threshold1[indices_bigger_than_threshold[0]] = True # retain the first
            first_bigger_than_threshold1[indices_bigger_than_threshold[non_consecutive]] = True
            indices_bigger_than_threshold = np.array(indices_bigger_than_threshold)

            GRF_AP = (data2[indices_bigger_than_threshold,2])
            GRF_ML = (data2[indices_bigger_than_threshold,3])
            GRF_VERT= (data2[indices_bigger_than_threshold,4])
            Ank_Power = (data2[indices_bigger_than_threshold,28])
            
            GRF_AP_Norm = signal.resample(GRF_AP,100)
            GRF_ML_Norm = signal.resample(GRF_ML,100)
            GRF_VERT_Norm= signal.resample(GRF_VERT,100)
            Ank_Power_Norm = signal.resample(Ank_Power,100)
            
            # taking one column from the loaded .csv file to store in the results parameter.  
            results_GRF_AP[:,ind-1] = GRF_AP_Norm
            results_GRF_ML[:,ind-1] = GRF_ML_Norm
            results_GRF_VERT[:,ind-1] = GRF_VERT_Norm
            
            results_AnkPower[:,ind-1] = Ank_Power_Norm

All the parts work individually, but in the loop I get an error

AttributeError: 'numpy.ndarry' object has no attribute 'resample'

If I run from scipy import signal and then uses the resample on the loaded subject, condition and trial it works.


Solution

  • I think your problem is that you are overwriting the signal function from scipy with the variable that you call signal:

    signal = np.array(data2[:,4])
    

    If you rename your variable it should be fixed.