Skip to content

Commit

Permalink
Merge pull request #386 from effigies/fix/split_info
Browse files Browse the repository at this point in the history
FIX: Pass new variable args as kwargs in split()
  • Loading branch information
effigies authored Feb 11, 2019
2 parents 13c857f + 687407a commit 45881ff
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion bids/analysis/tests/test_transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def sparse_run_variable_with_missing_values():
'amplitude': [1, 1, np.nan, 1]
})
run_info = [RunInfo({'subject': '01'}, 20, 2, 'dummy.nii.gz')]
var = SparseRunVariable('var', data, run_info, 'events')
var = SparseRunVariable(name='var', data=data, run_info=run_info, source='events')
return BIDSRunVariableCollection([var])


Expand Down
5 changes: 2 additions & 3 deletions bids/analysis/transformations/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ def _transform(self, var, model='spm', derivative=False, dispersion=False,
convolved = hrf.compute_regressor(vals, model, onsets,
fir_delays=fir_delays, min_onset=0)

return DenseRunVariable(var.name, convolved[0],
var.run_info, var.source,
var.sampling_rate)
return DenseRunVariable(name=var.name, values=convolved[0], run_info=var.run_info,
source=var.source, sampling_rate=var.sampling_rate)


class Demean(Transformation):
Expand Down
14 changes: 8 additions & 6 deletions bids/variables/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ def _load_time_variables(layout, dataset=None, columns=None, scan_length=None,
if df.empty:
continue

var = SparseRunVariable(col, df, run_info, 'events')
var = SparseRunVariable(name=col, data=df, run_info=run_info,
source='events')
run.add_variable(var)

# Process confound files
Expand All @@ -225,8 +226,9 @@ def _load_time_variables(layout, dataset=None, columns=None, scan_length=None,
_data = _data.loc[:, conf_cols]
for col in _data.columns:
sr = 1. / run.repetition_time
var = DenseRunVariable(col, _data[[col]], run_info,
'regressors', sr)
var = DenseRunVariable(name=col, values=_data[[col]],
run_info=run_info, source='regressors',
sampling_rate=sr)
run.add_variable(var)

# Process recordinging files
Expand Down Expand Up @@ -279,8 +281,8 @@ def _load_time_variables(layout, dataset=None, columns=None, scan_length=None,
df = pd.DataFrame(values, columns=rf_cols)
source = 'physio' if '_physio.tsv' in rf else 'stim'
for col in df.columns:
var = DenseRunVariable(col, df[[col]], run_info, source,
freq)
var = DenseRunVariable(name=col, values=df[[col]], run_info=run_info,
source=source, sampling_rate=freq)
run.add_variable(var)
return dataset

Expand Down Expand Up @@ -400,6 +402,6 @@ def make_patt(x, regex_search=False):
if prepend_type:
col_name = '%s.%s' % (suffix, col_name)

node.add_variable(SimpleVariable(col_name, df, suffix))
node.add_variable(SimpleVariable(name=col_name, data=df, source=suffix))

return dataset
2 changes: 1 addition & 1 deletion bids/variables/kollekshuns.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def from_df(cls, data, entities=None, source='contrast'):
_data = pd.DataFrame(data[col].values, columns=['amplitude'])
if entities is not None:
_data = pd.concat([_data, entities], axis=1, sort=True)
variables.append(SimpleVariable(col, _data, source))
variables.append(SimpleVariable(name=col, data=_data, source=source))
return BIDSVariableCollection(variables)

def clone(self):
Expand Down
3 changes: 2 additions & 1 deletion bids/variables/tests/test_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def generate_DEV(name='test', sr=20, duration=480):
entities = {e: uuid.uuid4().hex for e in ent_names}
image = uuid.uuid4().hex + '.nii.gz'
run_info = RunInfo(entities, duration, 2, image)
return DenseRunVariable('test', values, run_info, 'dummy', sr)
return DenseRunVariable(name='test', values=values, run_info=run_info,
source='dummy', sampling_rate=sr)


@pytest.fixture
Expand Down
27 changes: 19 additions & 8 deletions bids/variables/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ def split(self, grouper):
subsets = []
for i, (name, g) in enumerate(data.groupby(grouper)):
name = '%s.%s' % (self.name, name)
args = [name, g, getattr(self, 'run_info', None), self.source]
col = self.__class__(*args)
col = self.__class__(name=name, data=g, source=self.source,
run_info=getattr(self, 'run_info', None))
subsets.append(col)
return subsets

Expand Down Expand Up @@ -340,8 +340,12 @@ def to_dense(self, sampling_rate):
last_ind = onsets[i]

run_info = list(self.run_info)
return DenseRunVariable(self.name, ts, run_info, self.source,
sampling_rate)
return DenseRunVariable(
name=self.name,
values=ts,
run_info=run_info,
source=self.source,
sampling_rate=sampling_rate)

@classmethod
def _merge(cls, variables, name, **kwargs):
Expand Down Expand Up @@ -398,9 +402,11 @@ def split(self, grouper):
'''
values = grouper.values * self.values.values
df = pd.DataFrame(values, columns=grouper.columns)
return [DenseRunVariable('%s.%s' % (self.name, name), df[name].values,
self.run_info, self.source,
self.sampling_rate)
return [DenseRunVariable(name='%s.%s' % (self.name, name),
values=df[name].values,
run_info=self.run_info,
source=self.source,
sampling_rate=self.sampling_rate)
for i, name in enumerate(df.columns)]

def _build_entity_index(self, run_info, sampling_rate):
Expand Down Expand Up @@ -506,7 +512,12 @@ def _merge(cls, variables, name, sampling_rate=None, **kwargs):
values = pd.concat([v.values for v in variables], axis=0, sort=True)
run_info = list(chain(*[v.run_info for v in variables]))
source = variables[0].source
return DenseRunVariable(name, values, run_info, source, sampling_rate)
return DenseRunVariable(
name=name,
values=values,
run_info=run_info,
source=source,
sampling_rate=sampling_rate)


def merge_variables(variables, name=None, **kwargs):
Expand Down

0 comments on commit 45881ff

Please sign in to comment.