Pedro
Pedro

Reputation: 21

Strange variable behaviour in Python

My first post here.

So I'm loading data into a variable called f1_data, then passing it to pm.removeDC() function to do some signal processing, and keeping the result into the same variable. But then, I want to replace only the column 8, with the original f1_data that I called raw_data and I can't figure it out why it doesn't work. Here are the functions. Help anyone?

inside file pm.py

def removeDC(data):    
    # define the filter
    butter_order = 2
    hp_cutoff_Hz = 1.0
    b, a = signal.butter(butter_order, hp_cutoff_Hz/(fs_Hz / 2.0), 'highpass')

    for i in range(1,9):
        data[:,i] = signal.lfilter(b, a, data[:,i], 0)

    return (data)

def get_epoch1(data, t_sec, epoch, f_tup, col):
#f_tup = (f_wdir, f_name, f_columns, out_save, out_dir, out_number, fig_width)
    f_name = f_tup[1]
    fig_width = f_tup[6]

    epoch_boolvector = (t_sec >= epoch[0][0]) & (t_sec <= epoch[0][1])
    epoch_timescale = t_sec[epoch_boolvector]
    epoch_data = data[epoch_boolvector]

    plt.figure(figsize=(fig_width,8), dpi=96)
    plt.plot(epoch_timescale, epoch_data[:,col]);
    plt.xlim(epoch_timescale[0], epoch_timescale[-1])
    plt.show()
    return (epoch_boolvector, epoch_timescale, epoch_data)

inside main file

#load the whole data
(f1_data, f1_data_indices, f1_timescale) = pm.load_data(f1_wdir, f1_name)

raw_data = f1_data[:] #create copy of f1_data

(f1ep1_boolvector, f1ep1_timescale, f1ep1_data) = pm.get_epoch1(f1_data, f1_timescale, f1_epochs[1], f1_tup, 8)

#--- filter data to remove DC (1Hz)
f1_data = pm.removeDC(f1_data)


# replace only channel 8 with original data
f1_data[:,8] = raw_data[:,8]
(f1ep2_boolvector, f1ep2_timescale, f1ep2_data) = pm.get_epoch1(f1_data, f1_timescale, f1_epochs[1], f1_tup, 8)

Upvotes: 1

Views: 170

Answers (1)

Pedro
Pedro

Reputation: 21

The solution is import copy and use copy.deepcopy function. For further info check this link: docs.python.org/2/library/copy.html

When I have raw_data = f1_data[:] I get, after pm.removeDC():

 raw_data is f1_data: False
(raw_data == f1_data).all(): True  

But when I have raw_data = copy.deepcopy(f1_data) I get, after pm.removeDC():

 raw_data is f1_data: False  
(raw_data == f1_data).all(): False

Upvotes: 1

Related Questions