|
1 | 1 | import pandas as pd |
2 | 2 | import numpy as np |
3 | 3 | import ast |
4 | | -from typing import Tuple |
| 4 | +from typing import Tuple, Union |
5 | 5 | import lecilab_behavior_analysis.utils as utils |
6 | 6 |
|
7 | 7 | def fill_missing_data(df: pd.DataFrame) -> pd.DataFrame: |
@@ -421,27 +421,32 @@ def get_triangle_polar_plot_df(df: pd.DataFrame) -> pd.DataFrame: |
421 | 421 | return df_bias |
422 | 422 |
|
423 | 423 |
|
424 | | -def get_bias_evolution_df(df: pd.DataFrame, groupby: str) -> pd.DataFrame: |
| 424 | +def get_bias_evolution_df(df: pd.DataFrame, groupby: Union[str, list[str]]) -> pd.DataFrame: |
425 | 425 | """ |
426 | 426 | Gets how the bias of the animals (alternating, right bias, or left bias) |
427 | 427 | evolves over time. |
428 | 428 |
|
429 | 429 | Arguments: |
430 | 430 | df: DataFrame with the data |
431 | | - groupby: str, the column to group by. Can be 'session' or 'trial_group' |
| 431 | + groupby: str or list, the column(s) to group by. Can be 'session' or 'trial_group' |
432 | 432 |
|
433 | 433 | Returns: |
434 | 434 | df: DataFrame with the bias evolution |
435 | 435 | """ |
436 | | - utils.column_checker(df, required_columns={"roa_choice_numeric", "subject", groupby}) |
| 436 | + groupby_items = ["subject"] |
| 437 | + if isinstance(groupby, str): |
| 438 | + groupby_items.append(groupby) |
| 439 | + elif isinstance(groupby, list): |
| 440 | + groupby_items.extend(groupby) |
| 441 | + utils.column_checker(df, required_columns=set(groupby_items + ["roa_choice_numeric"])) |
437 | 442 | df_anchev = df.copy() |
438 | | - df_anchev = df_anchev.groupby(['subject', groupby])['roa_choice_numeric'].value_counts().reset_index(name='count') |
| 443 | + df_anchev = df_anchev.groupby(groupby_items)['roa_choice_numeric'].value_counts().reset_index(name='count') |
439 | 444 | # transform counts into percentages |
440 | | - df_anchev['percentage'] = df_anchev['count'] / df_anchev.groupby(['subject', groupby])['count'].transform('sum') |
| 445 | + df_anchev['percentage'] = df_anchev['count'] / df_anchev.groupby(groupby_items)['count'].transform('sum') |
441 | 446 |
|
442 | 447 | # pivot or melt the dataframe so that each subject and session has a y value, that will be the percentage when the bias |
443 | 448 | # is 0, and the x value will be the differences between the percentages when the bias is 1 and -1 |
444 | | - df_bias_pivot = df_anchev.pivot(index=['subject', groupby], columns='roa_choice_numeric', values='percentage') |
| 449 | + df_bias_pivot = df_anchev.pivot(index=groupby_items, columns='roa_choice_numeric', values='percentage') |
445 | 450 |
|
446 | 451 | # fill the NaN values with 0 |
447 | 452 | df_bias_pivot = df_bias_pivot.fillna(0) |
@@ -486,6 +491,16 @@ def create_transition_matrix(events: list) -> pd.DataFrame: |
486 | 491 | # Return the transition matrix as a pandas DataFrame for better readability |
487 | 492 | return pd.DataFrame(transition_matrix, index=items, columns=items) |
488 | 493 |
|
| 494 | + |
| 495 | +def add_visual_stimulus_difference(df_in: pd.DataFrame) -> pd.DataFrame: |
| 496 | + df = df_in.copy() # Create a copy to avoid modifying the original DataFrame |
| 497 | + utils.column_checker(df_in, required_columns={"visual_stimulus"}) |
| 498 | + df['visual_stimulus'] = df['visual_stimulus'].apply(ast.literal_eval) |
| 499 | + df["visual_stim_difference"] = df["visual_stimulus"].apply(lambda x: x[0] - x[1]) |
| 500 | + # bin the data every 0.1 |
| 501 | + df["vis_stim_dif_bin"] = np.round((df["visual_stim_difference"] // 0.1) * 0.1, 1) |
| 502 | + return df |
| 503 | + |
489 | 504 | # if __name__ == "__main__": |
490 | 505 | # from lecilab_behavior_analysis.utils import load_example_data |
491 | 506 | # df = load_example_data("mouse1") |
|
0 commit comments