Add dummy coded columns separately
@@ -587,10 +587,16 @@ class BIDSStatsModelsNodeOutput:
var_names = list(self.node.model['x'])
- # If a single incoming contrast, set to intercept
+ # If dummy coded condition columns needed, generate and concat
+ if inputs:
+ dummy_df = pd.get_dummies(df['contrast'])
+ dummies_needed = set(var_names).intersection(dummy_df)
+ if dummies_needed:
+ df = pd.concat([df, dummy_df[dummies_needed]], axis=1)
+
+ # If a single incoming contrast, keep track of name
if 'contrast' in df.columns and df['contrast'].nunique() == 1:
unique_in_contrast = df['contrast'].unique()[0]
- df.rename(columns={unique_in_contrast: 'intercept'}, inplace=True)
else:
unique_in_contrast = None
@@ -668,15 +674,9 @@ class BIDSStatsModelsNodeOutput:
def _inputs_to_df(self, inputs):
# Convert the inputs to a DataFrame and add to list. Each row is
- # an input; each column is either an entity or a contrast name from
- # the previous level.
- if inputs:
+ # an input; each column is an entity
rows = [{**con.entities, 'contrast': con.name} for con in inputs]
input_df = pd.DataFrame.from_records(rows)
- for i, con in enumerate(inputs):
- if con.name not in input_df.columns:
- input_df.loc[:, con.name] = 0
- input_df.loc[input_df.index[i], con.name] = 1
return input_df
def _build_contrasts(self, unique_in_contrast=None):
