From 03ddb5615cada02f7c84987090c73d546dc8d9c0 Mon Sep 17 00:00:00 2001 From: MikeG Date: Wed, 11 May 2022 08:47:46 +0200 Subject: [PATCH] remove some of the pylint exceptions, fix the resulting style errors (#4) * no longer disabling `len-as-condition` and `no-else-return` --- .pylintrc | 2 +- voxcell/nexus/voxelbrain.py | 9 ++++++--- voxcell/region_map.py | 11 ++++++----- voxcell/traits.py | 4 ++-- voxcell/vector_fields.py | 19 +++++++++---------- voxcell/voxel_data.py | 21 +++++++++++---------- 6 files changed, 35 insertions(+), 31 deletions(-) diff --git a/.pylintrc b/.pylintrc index b4cd464..7289f2e 100644 --- a/.pylintrc +++ b/.pylintrc @@ -1,5 +1,5 @@ [MESSAGES CONTROL] -disable=bad-continuation,fixme,invalid-name,len-as-condition,no-else-return +disable=bad-continuation,fixme,invalid-name [FORMAT] # Regexp for a line that is allowed to be longer than the limit. diff --git a/voxcell/nexus/voxelbrain.py b/voxcell/nexus/voxelbrain.py index 1963c97..83f00d4 100644 --- a/voxcell/nexus/voxelbrain.py +++ b/voxcell/nexus/voxelbrain.py @@ -52,14 +52,17 @@ def open(url, cache_dir=None): parsed = urllib.parse.urlsplit(url) if parsed.scheme in ('', 'file'): return LocalAtlas(url) - elif parsed.scheme in ('http', 'https'): + + if parsed.scheme in ('http', 'https'): if not parsed.path.startswith('/api/analytics/atlas/releases/'): raise VoxcellError(f"Unexpected URL: '{url}'") + if cache_dir is None: raise VoxcellError("`cache_dir` should be specified") + return VoxelBrainAtlas(url, cache_dir) - else: - raise VoxcellError(f"Unexpected URL: '{url}'") + + raise VoxcellError(f"Unexpected URL: '{url}'") @abc.abstractmethod def fetch_data(self, data_type): diff --git a/voxcell/region_map.py b/voxcell/region_map.py index 1088d0e..7fb2c0d 100644 --- a/voxcell/region_map.py +++ b/voxcell/region_map.py @@ -27,10 +27,11 @@ def __call__(self, value): """Return True if the given value matches.""" if hasattr(self.value, 'match'): return bool(self.value.search(value)) - elif isinstance(value, str) and self.ignore_case: + + if isinstance(value, str) and self.ignore_case: return self.value.upper() == value.upper() - else: - return self.value == value + + return self.value == value class RegionMap: @@ -62,8 +63,8 @@ def get(self, _id, attr, with_ascendants=False): """ if with_ascendants: return [self._get(k, attr) for k in self._ascendants(_id)] - else: - return self._get(_id, attr) + + return self._get(_id, attr) def find(self, value, attr, ignore_case=False, with_descendants=False): """Find IDs of the regions matching a given attribute. diff --git a/voxcell/traits.py b/voxcell/traits.py index d32f616..fd01225 100644 --- a/voxcell/traits.py +++ b/voxcell/traits.py @@ -144,8 +144,8 @@ def collect(self, positions, preassigned=None, names=None): """ if preassigned is None: return self.collect_traits(self.assign(positions), names) - else: - return self.collect_traits(self.assign_conditional(positions, preassigned), names) + + return self.collect_traits(self.assign_conditional(positions, preassigned), names) def collect_traits(self, chosen, names=None): """Return the trait values corresponding to an array of indices. diff --git a/voxcell/vector_fields.py b/voxcell/vector_fields.py index 2c4fe2a..4d513df 100644 --- a/voxcell/vector_fields.py +++ b/voxcell/vector_fields.py @@ -58,20 +58,19 @@ def combine_vector_fields(fields): Returns: A 5D numpy array representing an orientation field """ - if fields: - shape = fields[0].shape + if not fields: + return np.empty((0,)) - # add a second-to-last dimension: the number of fields - result = np.zeros(shape=shape[:-1] + (len(fields),) + (shape[-1],), dtype=fields[0].dtype) + shape = fields[0].shape - # abusing numpy broadcasting here saves us having to do an explicit transpose afterwards - for i, f in enumerate(fields): - result[..., i] = f + # add a second-to-last dimension: the number of fields + result = np.zeros(shape=shape[:-1] + (len(fields),) + (shape[-1],), dtype=fields[0].dtype) - return result + # abusing numpy broadcasting here saves us having to do an explicit transpose afterwards + for i, f in enumerate(fields): + result[..., i] = f - else: - return np.empty((0,)) + return result def split_orientation_field(field): diff --git a/voxcell/voxel_data.py b/voxcell/voxel_data.py index 5379fc8..54ae9da 100644 --- a/voxcell/voxel_data.py +++ b/voxcell/voxel_data.py @@ -266,10 +266,10 @@ def clip(self, bbox, na_value=0, inplace=False): mask[indices] = True self.raw[np.logical_not(mask)] = na_value return None - else: - raw = np.full_like(self.raw, na_value) - raw[indices] = self.raw[indices] - return VoxelData(raw, self.voxel_dimensions, self.offset) + + raw = np.full_like(self.raw, na_value) + raw[indices] = self.raw[indices] + return VoxelData(raw, self.voxel_dimensions, self.offset) def filter(self, predicate, inplace=False): """Set values for voxel positions not satisfying `predicate` to zero. @@ -284,13 +284,14 @@ def filter(self, predicate, inplace=False): ijk = np.stack(np.mgrid[[slice(0, d) for d in self.shape]], axis=-1) xyz = self.indices_to_positions(0.5 + ijk) mask = predicate(xyz.reshape(-1, self.ndim)).reshape(self.shape) + if inplace: self.raw[np.invert(mask)] = 0 return None - else: - raw = np.zeros_like(self.raw) - raw[mask] = self.raw[mask] - return VoxelData(raw, self.voxel_dimensions, self.offset) + + raw = np.zeros_like(self.raw) + raw[mask] = self.raw[mask] + return VoxelData(raw, self.voxel_dimensions, self.offset) def compact(self, na_values=(0,), inplace=False): """Reduce size of raw data by clipping N/A values. @@ -314,8 +315,8 @@ def compact(self, na_values=(0,), inplace=False): self.raw = raw self.offset = offset return None - else: - return VoxelData(raw, self.voxel_dimensions, offset) + + return VoxelData(raw, self.voxel_dimensions, offset) def with_data(self, raw): """Return VoxelData of the same shape with different data."""