From 5f437bb20bdd05f570c677d88d3371512e5ad62e Mon Sep 17 00:00:00 2001 From: Joris Vincent Date: Mon, 13 Mar 2023 12:10:30 +0100 Subject: [PATCH] More explicit handling of TooManyUnknowns (vs. other resolution errors) Fixes some flake8 warnings --- stimupy/components/__init__.py | 2 +- stimupy/components/circulars.py | 5 ++--- stimupy/components/frames.py | 2 +- stimupy/components/gratings.py | 6 +++--- stimupy/illusions/dungeons.py | 2 +- stimupy/illusions/sbcs.py | 4 ++-- stimupy/utils/resolution.py | 17 ++++++++++++----- tests/test_validate_resolution_components.py | 6 +++--- 8 files changed, 25 insertions(+), 19 deletions(-) diff --git a/stimupy/components/__init__.py b/stimupy/components/__init__.py index 4bee84de..0d95f6a1 100644 --- a/stimupy/components/__init__.py +++ b/stimupy/components/__init__.py @@ -224,7 +224,7 @@ def resolve_grating_params( length, visual_angle, ppd = resolution.resolve_1D( length=length, visual_angle=visual_angle, ppd=ppd ) - except ValueError: + except resolution.TooManyUnknownsError: ppd = ppd length = length visual_angle = visual_angle diff --git a/stimupy/components/circulars.py b/stimupy/components/circulars.py index 59187e1c..352d6da5 100644 --- a/stimupy/components/circulars.py +++ b/stimupy/components/circulars.py @@ -1,6 +1,5 @@ import copy import itertools -import warnings import numpy as np import scipy.special as sp @@ -196,7 +195,7 @@ def disc_and_rings( # Try to resolve resolution; try: shape, visual_size, ppd = resolution.resolve(shape=shape, visual_size=visual_size, ppd=ppd) - except ValueError: + except resolution.TooManyUnknownsError: # Check visual_size visual_size = resolution.validate_visual_size(visual_size) @@ -413,7 +412,7 @@ def grating( # Resolve sizes try: shape, visual_size, ppd = resolution.resolve(shape=shape, visual_size=visual_size, ppd=ppd) - except: + except resolution.TooManyUnknownsError: pass # Resolve grating diff --git a/stimupy/components/frames.py b/stimupy/components/frames.py index 5755d4fb..42c04690 100644 --- a/stimupy/components/frames.py +++ b/stimupy/components/frames.py @@ -170,7 +170,7 @@ def grating( # Try to resolve resolution try: shape, visual_size, ppd = resolution.resolve(shape=shape, visual_size=visual_size, ppd=ppd) - except ValueError: + except resolution.TooManyUnknownsError: ppd = resolution.validate_ppd(ppd) if ppd is not None else None shape = resolution.validate_shape(shape) if shape is not None else None visual_size = ( diff --git a/stimupy/components/gratings.py b/stimupy/components/gratings.py index 7f0f959f..8a0f3230 100644 --- a/stimupy/components/gratings.py +++ b/stimupy/components/gratings.py @@ -195,7 +195,7 @@ def square_wave( # Try to resolve resolution try: shape, visual_size, ppd = resolution.resolve(shape=shape, visual_size=visual_size, ppd=ppd) - except ValueError: + except resolution.TooManyUnknownsError: ppd = resolution.validate_ppd(ppd) shape = resolution.validate_shape(shape) visual_size = resolution.validate_visual_size(visual_size) @@ -346,7 +346,7 @@ def sine_wave( # Try to resolve resolution try: shape, visual_size, ppd = resolution.resolve(shape=shape, visual_size=visual_size, ppd=ppd) - except ValueError: + except resolution.TooManyUnknownsError: ppd = resolution.validate_ppd(ppd) shape = resolution.validate_shape(shape) visual_size = resolution.validate_visual_size(visual_size) @@ -578,7 +578,7 @@ def staircase( # Try to resolve resolution try: shape, visual_size, ppd = resolution.resolve(shape=shape, visual_size=visual_size, ppd=ppd) - except ValueError: + except resolution.TooManyUnknownsError: ppd = resolution.validate_ppd(ppd) shape = resolution.validate_shape(shape) visual_size = resolution.validate_visual_size(visual_size) diff --git a/stimupy/illusions/dungeons.py b/stimupy/illusions/dungeons.py index 89449f2a..f0baafcd 100644 --- a/stimupy/illusions/dungeons.py +++ b/stimupy/illusions/dungeons.py @@ -125,7 +125,7 @@ def resolve_dungeon_params( # Try to resolve resolution try: shape, visual_size, ppd = resolution.resolve(shape=shape, visual_size=visual_size, ppd=ppd) - except ValueError: + except resolution.TooManyUnknownsError: ppd = resolution.validate_ppd(ppd) shape = resolution.validate_shape(shape) visual_size = resolution.validate_visual_size(visual_size) diff --git a/stimupy/illusions/sbcs.py b/stimupy/illusions/sbcs.py index d37151b6..fef67458 100644 --- a/stimupy/illusions/sbcs.py +++ b/stimupy/illusions/sbcs.py @@ -527,7 +527,7 @@ def two_sided_with_dots( try: shape, visual_size, ppd = resolution.resolve(shape=shape, visual_size=visual_size, ppd=ppd) visual_size_ = (visual_size[0], visual_size[1] / 2) - except: + except resolution.TooManyUnknownsError: visual_size_ = None stim1 = with_dots( @@ -618,7 +618,7 @@ def two_sided_dotted( try: shape, visual_size, ppd = resolution.resolve(shape=shape, visual_size=visual_size, ppd=ppd) visual_size_ = (visual_size[0], visual_size[1] / 2) - except: + except resolution.TooManyUnknownsError: visual_size_ = None stim1 = dotted( diff --git a/stimupy/utils/resolution.py b/stimupy/utils/resolution.py index e79648bb..aebaa8f4 100644 --- a/stimupy/utils/resolution.py +++ b/stimupy/utils/resolution.py @@ -1,6 +1,7 @@ +import copy import warnings from collections import namedtuple -import copy + import numpy as np Visual_size = namedtuple("Visual_size", "height width") @@ -12,6 +13,10 @@ class ResolutionError(ValueError): pass +class TooManyUnknownsError(ValueError): + pass + + def resolve(shape=None, visual_size=None, ppd=None): """Resolves the full resolution, for 2 givens and 1 unknown @@ -105,7 +110,9 @@ def resolve_1D(length=None, visual_angle=None, ppd=None, round=True): # Triage based on number of unknowns if n_unknowns > 1: # More than 1 unknown we cannot resolve - raise ValueError(f"Too many unkowns to resolve resolution; {length},{visual_angle},{ppd}") + raise TooManyUnknownsError( + f"Too many unkowns to resolve resolution; {length},{visual_angle},{ppd}" + ) else: # 1 unknown, so need to resolve # Which unknown? if length is None: @@ -364,7 +371,7 @@ def validate_shape(shape): try: if len(shape) < 1: # Empty sequence - raise ValueError(f"shape must be of at least length 1: {shape}") + raise TypeError(f"shape must be of at least length 1: {shape}") except TypeError: # not a sequence; make it one shape = (shape, shape) @@ -431,7 +438,7 @@ def validate_ppd(ppd): try: if len(ppd) < 1: # Empty sequence - raise ValueError(f"ppd must be of at least length 1: {ppd}") + raise TypeError(f"ppd must be of at least length 1: {ppd}") except TypeError: # not a sequence; make it one ppd = (ppd, ppd) @@ -496,7 +503,7 @@ def validate_visual_size(visual_size): try: if len(visual_size) < 1: # Empty sequence - raise ValueError(f"visual_size must be of at least length 1: {visual_size}") + raise TypeError(f"visual_size must be of at least length 1: {visual_size}") except TypeError: # not a sequence; make it one visual_size = (visual_size, visual_size) diff --git a/tests/test_validate_resolution_components.py b/tests/test_validate_resolution_components.py index 4712f5b5..2d3a8931 100644 --- a/tests/test_validate_resolution_components.py +++ b/tests/test_validate_resolution_components.py @@ -54,7 +54,7 @@ def test_valid_visual_size(visual_size, expected): @pytest.mark.parametrize( "visual_size, exception", [ - ((), ValueError), + ((), TypeError), ("bla", ValueError), ((32, 32, 32), TypeError), ({32, 32}, TypeError), @@ -94,7 +94,7 @@ def test_valid_shape(shape, expected): @pytest.mark.parametrize( "shape, exception", [ - ((), ValueError), + ((), TypeError), ("bla", ValueError), ((32, 32, 32), TypeError), ({32, 32}, TypeError), @@ -134,7 +134,7 @@ def test_valid_ppd(ppd, expected): @pytest.mark.parametrize( "ppd, exception", [ - ((), ValueError), + ((), TypeError), ("bla", ValueError), ((32, 32, 32), TypeError), ({32, 32}, TypeError),