def collection(): """Create a collection at dataset level.""" df = pd.DataFrame({ 'subject': np.repeat(np.arange(10), 4), 'run': np.tile(np.arange(4), 10), 'age': np.repeat(np.random.normal(40, 10, 10).astype(int), 4) }) entities = df[['subject', 'run']] return BIDSVariableCollection.from_df(df, entities=entities)
def _concatenate_input_nodes(self, nodes): data, entities = [], [] for n in nodes: contrasts = [c.name for c in n.contrasts] row = pd.Series(np.ones(len(contrasts)), index=contrasts) data.append(row) entities.append(pd.Series(n.entities)) data = pd.concat(data, axis=1, sort=True).T entities = pd.concat(entities, axis=1, sort=True).T return BIDSVariableCollection.from_df(data, entities, self.level)
def _merge_contrast_inputs(self, inputs): """ Merges a list of ContrastInfo tuples and constructs a dict mapping from units of the current level to BIDSVariableCollections. Parameters ---------- inputs: [[ContrastInfo]] List of list of ContrastInfo tuples passed from the previous Step. Each element in the outer list maps to the output of a unit at the previous level; each element in the inner list is a ContrastInfo tuple. E.g., if contrast information is being passed from run-level to subject-level, each outer element is a run. Returns ------- A dictionary, where the keys are the values of the entities at the current level (e.g., '01', '02'...) and the values are BIDSVariableCollection containing contrast information. Notes ----- Each output BIDSVariableCollection contains information for a single unit at the present level. The variables in the collection reflect the union of all contrasts found in one or more of the inputs. A value of 1 indicates that the contrast is present for a given row in the input; 0 indicates that the contrast was missing. """ groups = self._group_objects_by_entities(inputs) ent_cols = list(list(groups.values())[0][0].entities.keys()) collections = {} for name, contrasts in groups.items(): # Create a DF with contrasts in rows and contrast names in columns data = [{**c.entities, **{c.name: 1}} for c in contrasts] data = pd.DataFrame.from_records(data) # Group by all entities and sum, collapsing over rows belonging # to the current unit data = data.groupby(ent_cols).sum() # Split the DF up into separate data and entities DFs entities = data.index.to_frame(index=False) data = data.reset_index(drop=True) # Construct the collection coll = BIDSVariableCollection.from_df(data, entities, self.level) collections[name] = coll return collections