From 7c72afde42d9180021142cddf8a06fcee7d085ca Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauldmccarthy@gmail.com> Date: Tue, 3 Sep 2019 14:22:42 +0100 Subject: [PATCH] BF: Handle present but empty elements --- fsl/data/atlases.py | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/fsl/data/atlases.py b/fsl/data/atlases.py index fae1605d9..28ca1825a 100644 --- a/fsl/data/atlases.py +++ b/fsl/data/atlases.py @@ -521,28 +521,19 @@ class AtlasDescription(object): if self.atlasType == 'statistic': - statistic = header.find('statistic') - units = header.find('units') - lower = header.find('lower') - upper = header.find('upper') - precision = header.find('precision') - - if statistic is None: statistic = '' - else: statistic = statistic.text.strip() - if units is None: units = '' - else: units = units.text.strip() - if lower is None: lower = 0 - else: lower = float(lower.text.strip()) - if upper is None: upper = 100 - else: upper = float(upper.text.strip()) - if precision is None: precision = 2 - else: precision = float(precision.text.strip()) - - self.statistic = statistic - self.units = units - self.lower = lower - self.upper = upper - self.precision = precision + fields = ['statistic', 'units', 'lower', 'upper', 'precision'] + values = {} + + for field in fields: + elem = header.find(field) + if elem is not None and elem.text is not None: + values[field] = elem.text.strip() + + self.statistic = values.get('statistic', '') + self.units = values.get('units', '') + self.lower = float(values.get('lower', 0)) + self.upper = float(values.get('upper', 100)) + self.precision = int( values.get('precision', 2)) elif self.atlasType == 'probabilistic': self.statistic = '' -- GitLab