From ccbb9a41d2ae1f9406cf268c57ba1be359ea7f16 Mon Sep 17 00:00:00 2001 From: Robert Smith Date: Tue, 17 Sep 2024 13:01:31 +1000 Subject: [PATCH 1/2] population_template: Fix aggregation weights import Previous code attempted to write to the dictionary while looping over it; this yields an Exception. --- bin/population_template | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/bin/population_template b/bin/population_template index 6842c88e81..a79b31e169 100755 --- a/bin/population_template +++ b/bin/population_template @@ -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] From 1927d795cbf7b0b4215a3cf06e1b4b9c366d7b30 Mon Sep 17 00:00:00 2001 From: Robert Smith Date: Wed, 18 Sep 2024 11:24:43 +1000 Subject: [PATCH 2/2] population_template: pylint fix --- bin/population_template | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/population_template b/bin/population_template index a79b31e169..b44506ebf3 100755 --- a/bin/population_template +++ b/bin/population_template @@ -565,7 +565,7 @@ def parse_input_files(in_files, mask_files, contrasts, f_agg_weight=None, whites 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()))) + '$' - agg_weights = {re.sub(suff, '', re.sub(pref, '', key)):agg_weights[key] for key in agg_weights.keys()} + 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 input "%s"' % inp.uid)