Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

population_template: Fix aggregation weights import #3001

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions bin/population_template
Original file line number Diff line number Diff line change
Expand Up @@ -558,19 +558,17 @@ def parse_input_files(in_files, mask_files, contrasts, f_agg_weight=None, whites
import csv # pylint: disable=import-outside-toplevel
try:
with open(f_agg_weight, 'r') as fweights:
agg_weights = dict((row[0].lstrip().rstrip(), row[1]) for row in csv.reader(fweights, delimiter=',', quotechar='#'))
agg_weights = dict((row[0].strip(), row[1].strip()) for row in csv.reader(fweights, delimiter=',', quotechar='#'))
except UnicodeDecodeError:
with open(f_agg_weight, 'r') as fweights:
reader = csv.reader(fweights.read().decode('utf-8', errors='replace'), delimiter=',', quotechar='#')
agg_weights = dict((row[0].lstrip().rstrip(), row[1]) for row in reader)
agg_weights = dict((row[0].strip(), row[1].strip()) for row in reader)
pref = '^' + re.escape(get_common_prefix(list(agg_weights.keys())))
suff = re.escape(get_common_postfix(list(agg_weights.keys()))) + '$'
for key in agg_weights.keys():
agg_weights[re.sub(suff, '', re.sub(pref, '', key))] = agg_weights.pop(key).strip()

agg_weights = {re.sub(suff, '', re.sub(pref, '', item[0])):item[1] for item in agg_weights.items()}
for inp in inputs:
if inp.uid not in agg_weights:
raise MRtrixError('aggregation weight not found for %s' % inp.uid)
raise MRtrixError('aggregation weight not found for input "%s"' % inp.uid)
inp.aggregation_weight = agg_weights[inp.uid]
app.console('Using aggregation weights ' + f_agg_weight)
weights = [float(inp.aggregation_weight) for inp in inputs if inp.aggregation_weight is not None]
Expand Down
Loading