|
1 | | -"""example function to analyze reaction times |
2 | | -- given a data frame with RT and accuracy, |
3 | | -compute mean RT for correct trials and mean accuracy |
| 1 | +"""Example class to analyze reaction times. |
| 2 | +
|
| 3 | +Given a data frame with RT and accuracy, compute mean RT for correct trials and |
| 4 | +mean accuracy. |
4 | 5 | """ |
5 | | -# %% |
6 | 6 | import pandas as pd |
7 | 7 |
|
8 | 8 |
|
9 | | -# %% |
10 | 9 | class RTAnalysis: |
11 | | - """[summary]""" |
| 10 | + """Response time (RT) analysis.""" |
12 | 11 |
|
13 | 12 | def __init__(self, outlier_cutoff_sd=None): |
14 | | - """ |
15 | | - RT analysis |
| 13 | + """Initialize a new RTAnalysis instance. |
16 | 14 |
|
17 | | - Parameters: |
18 | | - ----------- |
19 | | - outlier_cutoff_sd: standard deviation cutoff for long RT outliers (default: no cutoff) |
| 15 | + Parameters |
| 16 | + ---------- |
| 17 | + outlier_cutoff_sd : float, optional |
| 18 | + Standard deviation cutoff for long RT outliers, by default None |
20 | 19 | """ |
21 | 20 | self.outlier_cutoff_sd = outlier_cutoff_sd |
22 | | - self.meanrt_ = None |
23 | | - self.meanacc_ = None |
| 21 | + self.mean_rt_ = None |
| 22 | + self.mean_accuracy_ = None |
24 | 23 |
|
25 | 24 | def fit(self, rt, accuracy, verbose=True): |
26 | | - """[summary] |
27 | | -
|
28 | | - Args: |
29 | | - rt (Series of floats): response times for each trial |
30 | | - accuracy (Series of booleans): accuracy for each trial |
| 25 | + """Fit response time to accuracy. |
| 26 | +
|
| 27 | + Parameters |
| 28 | + ---------- |
| 29 | + rt : pd.Series |
| 30 | + Response time per trial |
| 31 | + accuracy : pd.Series |
| 32 | + Accuracy per trial |
| 33 | + verbose : bool, optional |
| 34 | + Whether to print verbose output or not, by default True |
| 35 | +
|
| 36 | + Raises |
| 37 | + ------ |
| 38 | + ValueError |
| 39 | + RT/accuracy length mismatch |
| 40 | + ValueError |
| 41 | + Accuracy is 0 |
31 | 42 | """ |
32 | | - |
33 | 43 | rt = self._ensure_series_type(rt) |
34 | 44 | accuracy = self._ensure_series_type(accuracy) |
35 | 45 |
|
36 | | - try: |
37 | | - assert rt.shape[0] == accuracy.shape[0] |
38 | | - except AssertionError as e: |
39 | | - raise ValueError("rt and accuracy must be the same length!") from e |
| 46 | + self._validate_length(rt, accuracy) |
40 | 47 |
|
41 | | - # ensure that accuracy values are boolean |
42 | | - assert not set(accuracy.unique()).difference([True, False]) |
| 48 | + # Ensure that accuracy values are boolean. |
| 49 | + assert accuracy.dtype == bool |
43 | 50 |
|
44 | | - if self.outlier_cutoff_sd is not None: |
45 | | - cutoff = rt.std() * self.outlier_cutoff_sd |
46 | | - if verbose: |
47 | | - print(f"outlier rejection excluded {(rt > cutoff).sum()} trials") |
48 | | - rt = rt.mask(rt > cutoff) |
| 51 | + rt = self.reject_outlier_rt(rt, verbose=verbose) |
49 | 52 |
|
50 | | - self.meanacc_ = accuracy.mean() |
| 53 | + self.mean_accuracy_ = accuracy.mean() |
51 | 54 | try: |
52 | | - assert self.meanacc_ > 0 |
| 55 | + assert self.mean_accuracy_ > 0 |
53 | 56 | except AssertionError as e: |
54 | | - raise ValueError("accuracy is zero") from e |
| 57 | + raise ValueError("Accuracy is zero!") from e |
55 | 58 |
|
56 | 59 | rt = rt.mask(~accuracy) |
57 | | - self.meanrt_ = rt.mean() |
| 60 | + self.mean_rt_ = rt.mean() |
58 | 61 |
|
59 | 62 | if verbose: |
60 | | - print(f"mean RT: {self.meanrt_}") |
61 | | - print(f"mean accuracy: {self.meanacc_}") |
| 63 | + print(f"mean RT: {self.mean_rt_}") |
| 64 | + print(f"mean accuracy: {self.mean_accuracy_}") |
| 65 | + |
| 66 | + @staticmethod |
| 67 | + def _validate_length(rt, accuracy): |
| 68 | + """Validate response time and accuracy series lengths. |
| 69 | +
|
| 70 | + Parameters |
| 71 | + ---------- |
| 72 | + rt : pd.Series |
| 73 | + Response time values |
| 74 | + accuracy : _type_ |
| 75 | + Accuracy values |
| 76 | +
|
| 77 | + Raises |
| 78 | + ------ |
| 79 | + ValueError |
| 80 | + Length mismatch |
| 81 | + """ |
| 82 | + same_length = rt.shape[0] == accuracy.shape[0] |
| 83 | + try: |
| 84 | + assert same_length |
| 85 | + except AssertionError as e: |
| 86 | + raise ValueError("RT and accuracy must be the same length!") from e |
| 87 | + |
62 | 88 |
|
63 | 89 | @staticmethod |
64 | 90 | def _ensure_series_type(var): |
65 | | - """return variable as a pandas Series or raise exception if |
66 | | - not possible |
| 91 | + """Return variable as a pandas Series. |
67 | 92 |
|
68 | | - Args: |
69 | | - var (array-like): variable to convert |
| 93 | + Parameters |
| 94 | + ---------- |
| 95 | + var : Iterable |
| 96 | + Variable to be converted |
70 | 97 |
|
71 | | - Returns: |
72 | | - series (pandas Series): converted variable |
| 98 | + Returns |
| 99 | + ------- |
| 100 | + pd.Series |
| 101 | + Variable values as a pandas Series |
73 | 102 | """ |
74 | | - |
75 | | - if type(var) is not pd.core.series.Series: |
| 103 | + if not isinstance(var, pd.Series): |
76 | 104 | var = pd.Series(var) |
77 | 105 | return var |
| 106 | + |
| 107 | + def reject_outlier_rt(self, rt, verbose=True): |
| 108 | + if self.outlier_cutoff_sd is None: |
| 109 | + return rt |
| 110 | + cutoff = rt.std() * self.outlier_cutoff_sd |
| 111 | + if verbose: |
| 112 | + n_excluded = (rt > cutoff).sum() |
| 113 | + print(f"Outlier rejection excluded {n_excluded} trials.") |
| 114 | + return rt.mask(rt > cutoff) |
0 commit comments