Skip to content

Commit

Permalink
Use explicit exception chaining (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
akihironitta authored Oct 14, 2020
1 parent f49958d commit 40fd35b
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,10 +20,10 @@
try:
from torchvision.datasets import ImageNet
from torchvision.datasets.imagenet import load_meta_file
except ModuleNotFoundError:
except ModuleNotFoundError as err:
raise ModuleNotFoundError( # pragma: no-cover
'You want to use `torchvision` which is not installed yet, 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,10 +3,10 @@
try:
from torchvision import transforms as transform_lib
from torchvision.datasets import MNIST
except ModuleNotFoundError:
except ModuleNotFoundError as err:
raise ModuleNotFoundError( # pragma: no-cover
'You want to use `torchvision` which is not installed yet, 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,10 +126,10 @@ def cli_main():
# create dataset
try:
from sklearn.datasets import load_boston
except ModuleNotFoundError:
except ModuleNotFoundError as err:
raise ModuleNotFoundError( # pragma: no-cover
'You want to use `sklearn` which is not installed yet, 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,10 +132,10 @@ def cli_main():
# Example: Iris dataset in Sklearn (4 features, 3 class labels)
try:
from sklearn.datasets import load_iris
except ModuleNotFoundError:
except ModuleNotFoundError as err:
raise ModuleNotFoundError( # pragma: no-cover
'You want to use `sklearn` which is not installed yet, 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 @@ -168,10 +168,10 @@ def validation_step(self, batch, batch_nb):
def shared_step(self, batch):
try:
from pl_bolts.datamodules.stl10_datamodule import STL10DataModule
except ModuleNotFoundError:
except ModuleNotFoundError as err:
raise ModuleNotFoundError( # pragma: no-cover
'You want to use `torchvision` which is not installed yet, install it with `pip install torchvision`.'
)
) from err

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

0 comments on commit 40fd35b

Please sign in to comment.