@@ -16,13 +16,14 @@ def add_data_to_posterior(df,
16
16
conditions = None , # eg ('stim_on', 'stim_stop')
17
17
b_name = 'b_stim_per_condition' , # for posterior
18
18
group_name = 'Condition code' , # for posterior
19
- do_make_change = True ,
19
+ do_make_change = 'subtract' ,
20
20
do_mean_over_trials = True ,
21
21
):
22
22
index_cols = list (index_cols )
23
- if conditions is None :
24
- conditions = df [condition_name ].drop_duplicates ().sort_values ().values
23
+
24
+ conditions = conditions or df [condition_name ].drop_duplicates ().sort_values ().values
25
25
assert len (conditions ) == 2 , f'{ condition_name } ={ conditions } . Should be only two instead!'
26
+ assert do_make_change in [False , 'subtract' , 'divide' ]
26
27
if not (condition_name in index_cols ):
27
28
index_cols .append (condition_name )
28
29
if do_mean_over_trials :
@@ -36,6 +37,7 @@ def add_data_to_posterior(df,
36
37
index_cols = index_cols ,
37
38
condition_name = condition_name ,
38
39
conditions = conditions ,
40
+ fold_change_method = do_make_change ,
39
41
do_take_mean = False )
40
42
# Condition is removed from both index columns and dfbayes
41
43
index_cols .remove (condition_name )
@@ -98,29 +100,33 @@ def fill_row(rows):
98
100
99
101
100
102
def make_fold_change (df , y = 'log_firing_rate' , index_cols = ('Brain region' , 'Stim phase' ),
101
- condition_name = 'stim' , conditions = (0 , 1 ), do_take_mean = False ):
102
- # for index_col in index_cols:
103
- # assert type(df[index_col].iloc[0]) != str, f'Make sure {index_col} contains not strings!'
103
+ condition_name = 'stim' , conditions = (0 , 1 ), do_take_mean = False , fold_change_method = 'divide' ):
104
104
for condition in conditions :
105
105
assert condition in df [condition_name ].unique (), f'{ condition } not in { df [condition_name ].unique ()} '
106
106
if y not in df .columns :
107
107
raise ValueError (f'{ y } is not a column in this dataset: { df .columns } ' )
108
+
109
+ # Take mean of trials:
108
110
if do_take_mean :
109
- # Take mean of trials:
110
111
df = df .groupby (list (index_cols )).mean ().reset_index ()
112
+
111
113
# Make multiindex
112
114
mdf = df .set_index (list (set (index_cols ) - {'i_spike' })).copy ()
113
- # mdf.xs(0, level='stim') - mdf.xs(1, level='stim')
114
115
if (mdf .xs (conditions [1 ], level = condition_name ).size !=
115
116
mdf .xs (conditions [0 ], level = condition_name ).size ):
116
117
raise IndexError (f'Uneven number of entries in conditions! Try setting do_take_mean=True'
117
118
f'{ mdf .xs (conditions [0 ], level = condition_name ).size , mdf .xs (conditions [1 ], level = condition_name ).size } ' )
118
119
119
120
# Subtract/divide
120
121
try :
121
- data = (mdf .xs (conditions [1 ], level = condition_name ) -
122
- mdf .xs (conditions [0 ], level = condition_name )
123
- ).reset_index ()
122
+ if fold_change_method == 'subtract' :
123
+ data = (mdf .xs (conditions [1 ], level = condition_name ) -
124
+ mdf .xs (conditions [0 ], level = condition_name )
125
+ ).reset_index ()
126
+ else :
127
+ data = (mdf .xs (conditions [1 ], level = condition_name ) /
128
+ mdf .xs (conditions [0 ], level = condition_name )
129
+ ).reset_index ()
124
130
except Exception as e :
125
131
print (f'Try recasting { condition_name } as integer and try again. Alternatively, use bayes_window.workflow.'
126
132
f' We do that automatically there ' )
0 commit comments