def test_create_epochs(self): signal = [0] * 30 + [1] * 5 + [0] * 30 + [1] * 5 + [0] * 30 events = nk.find_events(np.array(signal)) data = nk.create_epochs(pd.DataFrame({"Signal": signal}), events_onsets=events["onsets"], duration=5) self.assertEqual(len(data), 2)
def test_create_epochs(df): df = pd.DataFrame({ "Trigger": pd.Series( scipy.signal.square(1 * np.pi * 5 * np.linspace(0, 1, 2000, endpoint=False))), "Signal": pd.Series(np.sin(20 * np.pi * np.linspace(0, 1, 2000, endpoint=False))) * np.random.normal(0, 1, 2000), "Signal2": pd.Series(np.sin(60 * np.pi * np.linspace(0, 1, 2000, endpoint=False))) * np.random.normal(0, 2, 2000) }) df.plot() events = nk.find_events(df["Trigger"], cut="lower") assert len(events) == 2 fig = nk.plot_events_in_signal(df, events["onsets"]) epochs = nk.create_epochs(df, events["onsets"], duration=0.1) assert len(epochs) == 2
def test_find_events(self): signal = [0] * 30 + [1] * 5 + [0] * 30 + [1] * 5 + [0] * 30 events = nk.find_events(np.array(signal)) self.assertEqual(len(events["onsets"]), 2)
# A large number of HRV indices can be found print("HRV indices: ") print(bio["ECG"]["HRV"]) # RSA (respiratory sinus arrithmia) algorithm -> P2T nk.z_score(bio["df"][["ECG_Filtered", "RSP_Filtered", "RSA"]])[1000:2500].plot() plt.title('RSA algorithm') plt.savefig('graphs/rsaalg') #define condition list condition_list = ["Negative", "Neutral", "Neutral", "Negative"] #dict containing onsets and durations of each event --> should be 1 events = nk.find_events(df["Photosensor"], cut="lower") print("event finding") print(events) #create_epoch --> epochs of data corresponding to each event (since is 1 event, is the epoch[0]) epochs = nk.create_epochs(bio["df"], events["onsets"], duration=700, onset=-100) nk.z_score(epochs[0][["ECG_Filtered", "EDA_Filtered", "Photosensor"]]).plot() plt.title('Epoch') plt.savefig('graphs/epoch') # itereate through the epochs and store the interesting results in a new dict that will be, at the end, converted to a dataframe data = {} # Initialize an empty dict for epoch_index in epochs:
for Detector in range(len(SCRs_df['onsets']) - 2): if ((SCRs_df['amplitudes'][Detector + 1] - SCRs_df['amplitudes'][Detector]) > 0.1): SCRs_df['Artefacts'][Detector + 1] = 1 else: SCRs_df['Artefacts'][Detector + 1] = 0 # Detection of artefacts related with time: onset and offset greather than duration threshold (500 ms) for Detector2 in range(len(SCRs_df['onsets']) - 2): if ((ts[SCRs_df['peaks'][Detector2 + 1]] - ts[SCRs_df['onsets'][Detector2]]) > 500): SCRs_df['Artefacts'][Detector2 + 1] = 1 else: SCRs_df['Artefacts'][Detector2 + 1] = 0 # Identification of onset and duration of the triggers events = nk.find_events(df["Digital input"], cut="higher") # Definition of the dataframe of the results Results = pd.DataFrame( np.zeros(((len(events['onsets'])), 5)), columns=['Trigger', 'peaks', 'amplitudes', 'latency', 'artefacts']) for trialnr in range(len(events['onsets'])): Temporal = SCRs_df[ (SCRs_df['onsets'] > (events['onsets'][trialnr] - 500)) & (SCRs_df['peaks'] < (events['onsets'][trialnr] + events['durations'][trialnr] + 500))] Results['Trigger'][trialnr] = trialnr Results['peaks'][trialnr] = len(Temporal['peaks']) Results['amplitudes'][trialnr] = Temporal['amplitudes'].max() #Results['amplitudes'][trialnr] = Temporal['amplitudes'].mean() Results['artefacts'][trialnr] = Temporal['Artefacts'].max()