@@ -16,13 +16,14 @@ def add_data_to_posterior(df,
1616 conditions = None , # eg ('stim_on', 'stim_stop')
1717 b_name = 'b_stim_per_condition' , # for posterior
1818 group_name = 'Condition code' , # for posterior
19- do_make_change = True ,
19+ do_make_change = 'subtract' ,
2020 do_mean_over_trials = True ,
2121 ):
2222 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
2525 assert len (conditions ) == 2 , f'{ condition_name } ={ conditions } . Should be only two instead!'
26+ assert do_make_change in [False , 'subtract' , 'divide' ]
2627 if not (condition_name in index_cols ):
2728 index_cols .append (condition_name )
2829 if do_mean_over_trials :
@@ -36,6 +37,7 @@ def add_data_to_posterior(df,
3637 index_cols = index_cols ,
3738 condition_name = condition_name ,
3839 conditions = conditions ,
40+ fold_change_method = do_make_change ,
3941 do_take_mean = False )
4042 # Condition is removed from both index columns and dfbayes
4143 index_cols .remove (condition_name )
@@ -98,29 +100,33 @@ def fill_row(rows):
98100
99101
100102def 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' ):
104104 for condition in conditions :
105105 assert condition in df [condition_name ].unique (), f'{ condition } not in { df [condition_name ].unique ()} '
106106 if y not in df .columns :
107107 raise ValueError (f'{ y } is not a column in this dataset: { df .columns } ' )
108+
109+ # Take mean of trials:
108110 if do_take_mean :
109- # Take mean of trials:
110111 df = df .groupby (list (index_cols )).mean ().reset_index ()
112+
111113 # Make multiindex
112114 mdf = df .set_index (list (set (index_cols ) - {'i_spike' })).copy ()
113- # mdf.xs(0, level='stim') - mdf.xs(1, level='stim')
114115 if (mdf .xs (conditions [1 ], level = condition_name ).size !=
115116 mdf .xs (conditions [0 ], level = condition_name ).size ):
116117 raise IndexError (f'Uneven number of entries in conditions! Try setting do_take_mean=True'
117118 f'{ mdf .xs (conditions [0 ], level = condition_name ).size , mdf .xs (conditions [1 ], level = condition_name ).size } ' )
118119
119120 # Subtract/divide
120121 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 ()
124130 except Exception as e :
125131 print (f'Try recasting { condition_name } as integer and try again. Alternatively, use bayes_window.workflow.'
126132 f' We do that automatically there ' )
0 commit comments