diff --git a/.flake8 b/.flake8 index 2f19a70c..e8334ae8 100644 --- a/.flake8 +++ b/.flake8 @@ -8,4 +8,5 @@ max-line-length = 99 extend-ignore = E203,E402,F401,D100,D101,D102,D103,D104,D105,D106,D107,D200 docstring-convention = numpy # Ignore missing docstrings within unit testing functions. -per-file-ignores = **/tests/:D100,D101,D102,D103,D104,D105,D106,D107 +# Ignore C420 Unnecessary dict comprehension +per-file-ignores = **/tests/:D100,D101,D102,D103,D104,D105,D106,D107,C420 diff --git a/docs/examples/SIMA example.ipynb b/docs/examples/SIMA example.ipynb index ec652d05..37bdd7c1 100644 --- a/docs/examples/SIMA example.ipynb +++ b/docs/examples/SIMA example.ipynb @@ -76,7 +76,7 @@ "sequences = [sima.Sequence.create(\"TIFF\", tiff) for tiff in tiffs[:1]]\n", "try:\n", " dataset = sima.ImagingDataset(sequences, \"example.sima\")\n", - "except BaseException:\n", + "except Exception:\n", " dataset = sima.ImagingDataset.load(\"example.sima\")" ] }, diff --git a/examples/SIMA example.ipynb b/examples/SIMA example.ipynb index 849685e8..7007e1a7 100644 --- a/examples/SIMA example.ipynb +++ b/examples/SIMA example.ipynb @@ -76,7 +76,7 @@ "sequences = [sima.Sequence.create(\"TIFF\", tiff) for tiff in tiffs[:1]]\n", "try:\n", " dataset = sima.ImagingDataset(sequences, \"example.sima\")\n", - "except BaseException:\n", + "except Exception:\n", " dataset = sima.ImagingDataset.load(\"example.sima\")" ] }, diff --git a/fissa/core.py b/fissa/core.py index 3bc5c146..c9dec19a 100644 --- a/fissa/core.py +++ b/fissa/core.py @@ -383,7 +383,9 @@ def separate_trials( message_extra = " for ROI {}".format(label) warnings.warn( "{}Found values below zero in raw signal{}. Offsetting so minimum is 0." - "".format(header, message_extra) + "".format(header, message_extra), + UserWarning, + stacklevel=2, ) X -= X.min() @@ -1177,7 +1179,7 @@ def separation_prep(self, redo=False): self.load(fname) if self.raw is not None: return - except BaseException as err: + except Exception as err: print("An error occurred while loading {}".format(fname)) print(err) print("Extraction will be redone and {} overwritten".format(fname)) @@ -1379,7 +1381,7 @@ def separate(self, redo_prep=False, redo_sep=False): self.load(fname) if self.result is not None: return - except BaseException as err: + except Exception as err: print("An error occurred while loading {}".format(fname)) print(err) print( @@ -1859,6 +1861,7 @@ def save_to_matlab(self, fname=None): "The experiment.save_to_matlab() method is deprecated." " Please use experiment.to_matfile() instead.", DeprecationWarning, + stacklevel=2, ) return self.to_matfile(fname=fname, legacy=True) diff --git a/fissa/extraction.py b/fissa/extraction.py index b9260a64..e9a110b6 100644 --- a/fissa/extraction.py +++ b/fissa/extraction.py @@ -191,7 +191,9 @@ def image2array(image): " with {} dimensions (page shaped {})." " All dimensions before the final two (height and" " width) will be treated as time-like and flattened." - "".format(image, n_pages, page.ndim, page.shape) + "".format(image, n_pages, page.ndim, page.shape), + UserWarning, + stacklevel=2, ) elif page.ndim > 3 and (np.array(page.shape[:-2]) > 1).sum() > 1: warnings.warn( @@ -199,7 +201,9 @@ def image2array(image): " (page shaped {})." " All dimensions before the final two (height and" " width) will be treated as time-like and flattened." - "".format(image, page.ndim, page.shape) + "".format(image, page.ndim, page.shape), + UserWarning, + stacklevel=2, ) shp = [-1] + list(page.shape[-2:]) frames.append(page.reshape(shp)) @@ -327,7 +331,9 @@ def getmean(data): " with {} dimensions (page shaped {})." " All dimensions before the final two (height and" " width) will be treated as time-like and flattened." - "".format("", n_pages, page.ndim, page.shape) + "".format("", n_pages, page.ndim, page.shape), + UserWarning, + stacklevel=2, ) elif page.ndim > 3 and (np.array(page.shape[:-2]) > 1).sum() > 1: warnings.warn( @@ -335,7 +341,9 @@ def getmean(data): " (page shaped {})." " All dimensions before the final two (height and" " width) will be treated as time-like and flattened." - "".format("", page.ndim, page.shape) + "".format("", page.ndim, page.shape), + UserWarning, + stacklevel=2, ) shp = [-1] + list(page.shape[-2:]) page = page.reshape(shp) diff --git a/fissa/readimagejrois.py b/fissa/readimagejrois.py index e8b36b5c..ff1f02f5 100644 --- a/fissa/readimagejrois.py +++ b/fissa/readimagejrois.py @@ -275,7 +275,7 @@ def _getcoords(z=0): coords = _getcoords(z) coords = coords.astype("float") return {"polygons": coords} - except BaseException: + except Exception: raise ValueError( "read_imagej_roi: ROI type {} not supported".format(roi_type) ) diff --git a/fissa/tests/base_test.py b/fissa/tests/base_test.py index f7b679a9..fb09564d 100644 --- a/fissa/tests/base_test.py +++ b/fissa/tests/base_test.py @@ -79,7 +79,7 @@ def assert_starts_with(actual, desired): """ try: assert len(actual) >= len(desired) - except BaseException: + except Exception: print( "Actual string too short ({} < {} characters)".format( len(actual), len(desired) @@ -89,7 +89,7 @@ def assert_starts_with(actual, desired): raise try: return assert_equal(str(actual)[: len(desired)], desired) - except BaseException as err: + except Exception as err: msg = "ACTUAL: {}".format(actual) if isinstance(getattr(err, "args", None), str): err.args += "\n" + msg diff --git a/fissa/tests/generate_downsampled_resources.py b/fissa/tests/generate_downsampled_resources.py index b5951f96..184da940 100755 --- a/fissa/tests/generate_downsampled_resources.py +++ b/fissa/tests/generate_downsampled_resources.py @@ -318,7 +318,7 @@ def _writecoords(points): _writecoords(coords) coords = coords.astype("float") # return {'polygons': coords} - except BaseException: + except Exception: raise ValueError( "read_imagej_roi: ROI type {} not supported".format(roi_type) ) diff --git a/fissa/tests/test_core.py b/fissa/tests/test_core.py index 072563e8..9b16cace 100644 --- a/fissa/tests/test_core.py +++ b/fissa/tests/test_core.py @@ -1251,7 +1251,7 @@ def test_load_none_force(self): # Make a save file which contains values set to `None` fname = os.path.join(self.output_dir, "dummy.npz") os.makedirs(self.output_dir) - np.savez_compressed(fname, **{field: None for field in fields}) + np.savez_compressed(fname, **{field: None for field in fields}) # noqa: C420 # Load the file and check the data appears as None, not np.array(None) exp.load(fname, force=True) for field in fields: @@ -1281,7 +1281,7 @@ def test_load_scalar_force(self): # Make a save file which contains values set to a scalar fname = os.path.join(self.output_dir, "dummy.npz") os.makedirs(self.output_dir) - np.savez_compressed(fname, **{field: 1337 for field in fields}) + np.savez_compressed(fname, **{field: 1337 for field in fields}) # noqa: C420 # Load the file and check the data appears correctly exp.load(fname, force=True) for field in fields: