Skip to content

Commit

Permalink
Use explicit exception chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
akihironitta committed Oct 4, 2020
1 parent 72e8be3 commit ad3b83d
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions pl_bolts/callbacks/vision/confused_logit.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ def on_train_batch_end(self, trainer, pl_module, batch, batch_idx, dataloader_id
x, y = batch
try:
logits = pl_module.last_logits
except AttributeError as e:
except AttributeError as err:
m = """please track the last_logits in the training_step like so:
def training_step(...):
self.last_logits = your_logits
"""
raise AttributeError(m)
raise AttributeError(m) from err

# only check when it has opinions (ie: the logit > 5)
if logits.max() > self.min_logit_value:
Expand Down
4 changes: 2 additions & 2 deletions pl_bolts/datamodules/base_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ def _download_from_url(self, base_url: str, data_folder: str, file_name: str):
fpath = os.path.join(data_folder, file_name)
try:
urllib.request.urlretrieve(url, fpath)
except HTTPError:
raise RuntimeError(f'Failed download from {url}')
except HTTPError as err:
raise RuntimeError(f'Failed download from {url}') from err
4 changes: 2 additions & 2 deletions pl_bolts/datamodules/imagenet_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
try:
from torchvision.datasets import ImageNet
from torchvision.datasets.imagenet import load_meta_file
except ImportError:
except ImportError as err:
raise ImportError('You want to use `torchvision` which is not installed yet,' # pragma: no-cover
' install it with `pip install torchvision`.')
' install it with `pip install torchvision`.') from err


class UnlabeledImagenet(ImageNet):
Expand Down
4 changes: 2 additions & 2 deletions pl_bolts/datamodules/mnist_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
try:
from torchvision import transforms as transform_lib
from torchvision.datasets import MNIST
except ImportError:
except ImportError as err:
raise ImportError('You want to use `torchvision` which is not installed yet,' # pragma: no-cover
' install it with `pip install torchvision`.')
' install it with `pip install torchvision`.') from err

try:
from PIL import Image
Expand Down
4 changes: 2 additions & 2 deletions pl_bolts/models/regression/linear_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ def cli_main():
# create dataset
try:
from sklearn.datasets import load_boston
except ImportError:
except ImportError as err:
raise ImportError('You want to use `sklearn` which is not installed yet,' # pragma: no-cover
' install it with `pip install sklearn`.')
' install it with `pip install sklearn`.') from err

X, y = load_boston(return_X_y=True) # these are numpy arrays
loaders = SklearnDataModule(X, y)
Expand Down
4 changes: 2 additions & 2 deletions pl_bolts/models/regression/logistic_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ def cli_main():
# Example: Iris dataset in Sklearn (4 features, 3 class labels)
try:
from sklearn.datasets import load_iris
except ImportError:
except ImportError as err:
raise ImportError('You want to use `sklearn` which is not installed yet,' # pragma: no-cover
' install it with `pip install sklearn`.')
' install it with `pip install sklearn`.') from err

X, y = load_iris(return_X_y=True)
loaders = SklearnDataModule(X, y)
Expand Down
4 changes: 2 additions & 2 deletions pl_bolts/models/self_supervised/cpc/cpc_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ def validation_step(self, batch, batch_nb):
def shared_step(self, batch):
try:
from pl_bolts.datamodules.stl10_datamodule import STL10DataModule
except ImportError:
except ImportError as err:
raise ImportError('You want to use `torchvision` which is not installed yet,' # pragma: no-cover
' install it with `pip install torchvision`.')
' install it with `pip install torchvision`.') from err

if isinstance(self.datamodule, STL10DataModule):
unlabeled_batch = batch[0]
Expand Down

0 comments on commit ad3b83d

Please sign in to comment.