ENH: Permit intercept (1) as name and condition in contrats
@@ -541,6 +541,7 @@ class BIDSStatsModelsNodeOutput:
# the intercept column is given the same name as the input contrast.
# It may already exist, in which case we do nothing.
# * Otherwise, we name the column 'intercept'.
+ int_name = None
if 1 in var_names:
if ('contrast' not in df.columns or df['contrast'].nunique() > 1):
int_name = 'intercept'
@@ -564,11 +565,9 @@ class BIDSStatsModelsNodeOutput:
self.data = df.loc[:, var_names]
self.metadata = df.loc[:, df.columns.difference(var_names)]
-
-
# Create ModelSpec and build contrasts
self.model_spec = create_model_spec(self.data, node.model, self.metadata)
- self.contrasts = self._build_contrasts()
+ self.contrasts = self._build_contrasts(int_name)
def _collections_to_dfs(self, collections):
"""Merges collections and converts them to a pandas DataFrame."""
@@ -628,12 +627,18 @@ class BIDSStatsModelsNodeOutput:
input_df.loc[input_df.index[i], con.name] = 1
return input_df
- def _build_contrasts(self):
+ def _build_contrasts(self, int_name):
"""Contrast list of ContrastInfo objects based on current state."""
contrasts = {}
col_names = set(self.X.columns)
for con in self.node.contrasts:
- missing_vars = set(con['condition_list']) - col_names
+ name = con["name"]
+ condition_list = list(con["condition_list"])
+ if 1 in condition_list and int_name is not None:
+ condition_list[condition_list.index(1)] = int_name
+ if name == 1 and int_name is not None:
+ name = int_name
+ missing_vars = set(condition_list) - col_names
if missing_vars:
if self.invalid_contrasts == 'error':
msg = ("Variable(s) '{}' specified in condition list for "
@@ -645,7 +650,7 @@ class BIDSStatsModelsNodeOutput:
weights = np.atleast_2d(con['weights'])
# Add contrast name to entities; can be used in grouping downstream
entities = {**self.entities, 'contrast': con['name']}
- ci = ContrastInfo(con['name'], con['condition_list'],
+ ci = ContrastInfo(con['name'], condition_list,
con['weights'], con.get("test"), entities)
contrasts[con['name']] = ci
@@ -653,8 +658,12 @@ class BIDSStatsModelsNodeOutput:
if dummies:
conditions = col_names
if 'conditions' in dummies:
- conditions &= set(dummies['conditions'])
- conditions -= set([c.name for c in contrasts])
+ conds = set(dummies['conditions'])
+ if 1 in conds and int_name is not None:
+ conds.discard(1)
+ conds.add(int_name)
+ conditions &= conds
+ conditions -= set(c.name for c in contrasts)
for col_name in conditions:
if col_name in contrasts:
