Skip to content

Commit

Permalink
Merge pull request #3001 from MRtrix3/population_template_aggweights
Browse files Browse the repository at this point in the history
population_template: Fix aggregation weights import
  • Loading branch information
Lestropie authored Nov 20, 2024
2 parents d258268 + 1927d79 commit 50a5e04
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, '', 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

0 comments on commit 50a5e04

Please sign in to comment.