Exporting a DataFrame from a list of sequences#
Sequence
objects can be quickly converted to a pandas.DataFrame
using the thebeat.utils.get_ioi_df()
function. Below we illustrate how it works.
The resulting pandas.DataFrame
is in the tidy data format, i.e. ‘long format’, where each individual IOI has its own row.
Preliminaries#
First we import some necessary functions and create a NumPy generator object with a seed, so you will get the same output as we.
[1]:
import thebeat
import numpy as np
import pandas as pd
rng = np.random.default_rng(seed=123)
And we create some random Sequence
objects:
[2]:
# Create empty list
sequences = []
# Create 10 random Sequence objects and add to the list
for _ in range(10):
sequence = thebeat.Sequence.generate_random_normal(n_events=10,
mu=500,
sigma=25,
rng=rng)
sequences.append(sequence)
See what they look like:
[3]:
print(sequences)
[Sequence(iois=[475.27 490.81 532.2 ... 484.09 513.55 492.09]), Sequence(iois=[491.94 502.43 461.85 ... 503.41 538.3 483.5 ]), Sequence(iois=[492.21 508.44 444.81 ... 518.87 496.35 532.05]), Sequence(iois=[526.85 509.82 500.13 ... 445.7 490.75 504.11]), Sequence(iois=[521.5 544.04 524.83 ... 535.75 496.09 483.16]), Sequence(iois=[484.02 498.47 490.18 ... 500.7 500.71 501.38]), Sequence(iois=[487.96 485.41 478.45 ... 486.42 486.03 492.09]), Sequence(iois=[488.48 464.09 534.13 ... 489.04 494.71 509.1 ]), Sequence(iois=[523.82 537.99 542.6 ... 503.21 481.64 484.49]), Sequence(iois=[520.33 541.05 494.34 ... 493.18 510.56 497.97])]
Creating a simple DataFrame of IOIs#
[4]:
df = thebeat.utils.get_ioi_df(sequences)
df
[4]:
sequence_i | ioi_i | ioi | |
---|---|---|---|
0 | 0 | 0 | 475.271966 |
1 | 0 | 1 | 490.805334 |
2 | 0 | 2 | 532.198132 |
3 | 0 | 3 | 504.849360 |
4 | 0 | 4 | 523.005772 |
... | ... | ... | ... |
85 | 9 | 4 | 492.915720 |
86 | 9 | 5 | 475.121716 |
87 | 9 | 6 | 493.178206 |
88 | 9 | 7 | 510.561104 |
89 | 9 | 8 | 497.966426 |
90 rows × 3 columns
Creating a DataFrame with additional calculations#
We can pass get_ioi_df()
additional functions to its additional_functions
parameter. They will be applied to each provided sequence, and the resulting values will be added to the pandas.DataFrame
. Note that we need to provide the actual functions, not simply their names. Also note that additional_functions
expects a list, so even if providing only one function, that function needs to be within a list.
[5]:
df = thebeat.utils.get_ioi_df(sequences=sequences,
additional_functions=[np.mean, np.std, np.min, np.max])
df
[5]:
sequence_i | mean | std | amin | amax | ioi_i | ioi | |
---|---|---|---|---|---|---|---|
0 | 0 | 503.364499 | 17.923263 | 475.271966 | 532.198132 | 0 | 475.271966 |
1 | 0 | 503.364499 | 17.923263 | 475.271966 | 532.198132 | 1 | 490.805334 |
2 | 0 | 503.364499 | 17.923263 | 475.271966 | 532.198132 | 2 | 532.198132 |
3 | 0 | 503.364499 | 17.923263 | 475.271966 | 532.198132 | 3 | 504.849360 |
4 | 0 | 503.364499 | 17.923263 | 475.271966 | 532.198132 | 4 | 523.005772 |
... | ... | ... | ... | ... | ... | ... | ... |
85 | 9 | 501.028710 | 18.898427 | 475.121716 | 541.045025 | 4 | 492.915720 |
86 | 9 | 501.028710 | 18.898427 | 475.121716 | 541.045025 | 5 | 475.121716 |
87 | 9 | 501.028710 | 18.898427 | 475.121716 | 541.045025 | 6 | 493.178206 |
88 | 9 | 501.028710 | 18.898427 | 475.121716 | 541.045025 | 7 | 510.561104 |
89 | 9 | 501.028710 | 18.898427 | 475.121716 | 541.045025 | 8 | 497.966426 |
90 rows × 7 columns
Saving the DataFrame#
To save the pandas.DataFrame
, we can simply use its pandas.DataFrame.to_csv()
method:
[6]:
df.to_csv('random_sequences.csv')