Skip to content

Commit

Permalink
population_template: Fix aggregation weights import
Browse files Browse the repository at this point in the history
Previous code attempted to write to the dictionary while looping over it; this yields an Exception.
  • Loading branch information
Lestropie committed Sep 17, 2024
1 parent fb66ec3 commit ccbb9a4
Showing 1 changed file with 4 additions and 6 deletions.
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, '', key)):agg_weights[key] for key in agg_weights.keys()}
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

0 comments on commit ccbb9a4

Please sign in to comment.