Last active
January 13, 2023 07:28
-
-
Save mikelgg93/b8ef26366c6d3a43750d73b5aad57244 to your computer and use it in GitHub Desktop.
Python snippet example on how to compute blink rate per minute.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
import matplotlib.pyplot as plt | |
events_df = pd.read_csv("path/events.csv") | |
blinks_df = pd.read_csv("path/blinks.csv") | |
# filter blinks only occurring between event name A and B | |
event_name_A = "recording.begin" | |
event_name_B = "recording.end" | |
time_event_A = events_df[events_df["name"] == event_name_A]["timestamp [ns]"].values[0] | |
time_event_B = events_df[events_df["name"] == event_name_B]["timestamp [ns]"].values[0] | |
blinks_df_filter = blinks_df[ | |
(blinks_df["start timestamp [ns]"] > time_event_A) | |
& (blinks_df["end timestamp [ns]"] < time_event_B) | |
] | |
# Blink rate per minute | |
blinks_df_filter["minute"] = np.nan | |
blinks_per_minute = pd.DataFrame( | |
columns=["minute", "number of blinks", "avg_blink_duration"] | |
) | |
minutes_the_section_last = np.ceil((time_event_B - time_event_A) / 1e9 / 60) | |
for minute in range(0, int(minutes_the_section_last), 1): | |
time_min_start = time_event_A + minute * 60 * 1e9 | |
idx = blinks_df_filter.loc[ | |
(blinks_df_filter["start timestamp [ns]"] > time_min_start) | |
& (blinks_df_filter["start timestamp [ns]"] < time_min_start + 60 * 1e9) | |
].index | |
blinks_df_filter.loc[idx, "minute"] = minute | |
avg_blink_duration = blinks_df_filter.loc[idx, "duration [ms]"].mean() | |
blinks_per_minute = pd.concat( | |
[ | |
blinks_per_minute, | |
pd.DataFrame( | |
[[minute, len(idx.to_numpy()), avg_blink_duration]], | |
columns=["minute", "number of blinks", "avg_blink_duration"], | |
), | |
], | |
ignore_index=True, | |
) | |
blinks_per_minute.to_csv("blinks_per_minute.csv") | |
blinks_per_minute.plot(x="minute", y="number of blinks") | |
plt.show() | |
print(blinks_df_filter) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment