Skip to content

Commit

Permalink
auto fixes from pre-commit.com hooks (Project-MONAI#1198)
Browse files Browse the repository at this point in the history
### Checks
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Avoid including large-size files in the PR.
- [x] Clean up long text outputs from code cells in the notebook.
- [x] For security purposes, please check the contents and remove any
sensitive info such as user names and private key.
- [x] Ensure (1) hyperlinks and markdown anchors are working (2) use
relative paths for tutorial repo files (3) put figure and graphs in the
`./figure` folder
- [ ] Notebook runs automatically `./runner.sh -t <path to .ipynb file>`

---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
monai-bot authored Jan 30, 2023
1 parent 97cc083 commit 25131a5
Show file tree
Hide file tree
Showing 171 changed files with 3,512 additions and 4,299 deletions.
4 changes: 2 additions & 2 deletions .github/contributing_templates/notebook/example_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
from PIL import Image


class ExampleImageGenerator():
class ExampleImageGenerator:
def __init__(self, num_image=40, image_size=(128, 128)):
self.num_image = num_image
self.image_size = image_size

def generate(self, tempdir):
for i in range(self.num_image):
im, seg = create_test_image_2d(
self.image_size[0], self.image_size[1], num_seg_classes=1,random_state=np.random.RandomState(42)
self.image_size[0], self.image_size[1], num_seg_classes=1, random_state=np.random.RandomState(42)
)
Image.fromarray((im * 255).astype("uint8")).save(os.path.join(tempdir, f"img{i:d}.png"))
Image.fromarray((seg * 255).astype("uint8")).save(os.path.join(tempdir, f"seg{i:d}.png"))
Expand Down
29 changes: 5 additions & 24 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,11 @@ repos:
args: ['--maxkb=1024']
- id: detect-private-key

#- repo: https://github.com/asottile/pyupgrade
# rev: v2.23.2
# hooks:
# - id: pyupgrade
# args: [--py36-plus]
# name: Upgrade code

#- repo: https://github.com/asottile/yesqa
# rev: v1.2.3
# hooks:
# - id: yesqa
# name: Unused noqa

#- repo: https://github.com/PyCQA/isort
# rev: 5.9.3
# hooks:
# - id: isort
# name: Format imports

# - repo: https://github.com/psf/black
# rev: 21.7b0
# hooks:
# - id: black
# name: Format code
- repo: https://github.com/psf/black
rev: "22.12.0"
hooks:
- id: black
- id: black-jupyter

#- repo: https://github.com/executablebooks/mdformat
# rev: 0.7.8
Expand Down
44 changes: 13 additions & 31 deletions 2d_classification/mednist_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,10 @@
}
],
"source": [
"class_names = sorted(x for x in os.listdir(data_dir)\n",
" if os.path.isdir(os.path.join(data_dir, x)))\n",
"class_names = sorted(x for x in os.listdir(data_dir) if os.path.isdir(os.path.join(data_dir, x)))\n",
"num_class = len(class_names)\n",
"image_files = [\n",
" [\n",
" os.path.join(data_dir, class_names[i], x)\n",
" for x in os.listdir(os.path.join(data_dir, class_names[i]))\n",
" ]\n",
" [os.path.join(data_dir, class_names[i], x) for x in os.listdir(os.path.join(data_dir, class_names[i]))]\n",
" for i in range(num_class)\n",
"]\n",
"num_each = [len(image_files[i]) for i in range(num_class)]\n",
Expand Down Expand Up @@ -341,9 +337,7 @@
"test_x = [image_files_list[i] for i in test_indices]\n",
"test_y = [image_class[i] for i in test_indices]\n",
"\n",
"print(\n",
" f\"Training count: {len(train_x)}, Validation count: \"\n",
" f\"{len(val_x)}, Test count: {len(test_x)}\")"
"print(f\"Training count: {len(train_x)}, Validation count: \" f\"{len(val_x)}, Test count: {len(test_x)}\")"
]
},
{
Expand All @@ -370,8 +364,7 @@
" ]\n",
")\n",
"\n",
"val_transforms = Compose(\n",
" [LoadImage(image_only=True), EnsureChannelFirst(), ScaleIntensity()])\n",
"val_transforms = Compose([LoadImage(image_only=True), EnsureChannelFirst(), ScaleIntensity()])\n",
"\n",
"y_pred_trans = Compose([Activations(softmax=True)])\n",
"y_trans = Compose([AsDiscrete(to_onehot=num_class)])"
Expand All @@ -397,16 +390,13 @@
"\n",
"\n",
"train_ds = MedNISTDataset(train_x, train_y, train_transforms)\n",
"train_loader = DataLoader(\n",
" train_ds, batch_size=300, shuffle=True, num_workers=10)\n",
"train_loader = DataLoader(train_ds, batch_size=300, shuffle=True, num_workers=10)\n",
"\n",
"val_ds = MedNISTDataset(val_x, val_y, val_transforms)\n",
"val_loader = DataLoader(\n",
" val_ds, batch_size=300, num_workers=10)\n",
"val_loader = DataLoader(val_ds, batch_size=300, num_workers=10)\n",
"\n",
"test_ds = MedNISTDataset(test_x, test_y, val_transforms)\n",
"test_loader = DataLoader(\n",
" test_ds, batch_size=300, num_workers=10)"
"test_loader = DataLoader(test_ds, batch_size=300, num_workers=10)"
]
},
{
Expand All @@ -430,8 +420,7 @@
"outputs": [],
"source": [
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
"model = DenseNet121(spatial_dims=2, in_channels=1,\n",
" out_channels=num_class).to(device)\n",
"model = DenseNet121(spatial_dims=2, in_channels=1, out_channels=num_class).to(device)\n",
"loss_function = torch.nn.CrossEntropyLoss()\n",
"optimizer = torch.optim.Adam(model.parameters(), 1e-5)\n",
"max_epochs = 4\n",
Expand Down Expand Up @@ -477,9 +466,7 @@
" loss.backward()\n",
" optimizer.step()\n",
" epoch_loss += loss.item()\n",
" print(\n",
" f\"{step}/{len(train_ds) // train_loader.batch_size}, \"\n",
" f\"train_loss: {loss.item():.4f}\")\n",
" print(f\"{step}/{len(train_ds) // train_loader.batch_size}, \" f\"train_loss: {loss.item():.4f}\")\n",
" epoch_len = len(train_ds) // train_loader.batch_size\n",
" epoch_loss /= step\n",
" epoch_loss_values.append(epoch_loss)\n",
Expand Down Expand Up @@ -509,8 +496,7 @@
" if result > best_metric:\n",
" best_metric = result\n",
" best_metric_epoch = epoch + 1\n",
" torch.save(model.state_dict(), os.path.join(\n",
" root_dir, \"best_metric_model.pth\"))\n",
" torch.save(model.state_dict(), os.path.join(root_dir, \"best_metric_model.pth\"))\n",
" print(\"saved new best metric model\")\n",
" print(\n",
" f\"current epoch: {epoch + 1} current AUC: {result:.4f}\"\n",
Expand All @@ -519,9 +505,7 @@
" f\" at epoch: {best_metric_epoch}\"\n",
" )\n",
"\n",
"print(\n",
" f\"train completed, best_metric: {best_metric:.4f} \"\n",
" f\"at epoch: {best_metric_epoch}\")"
"print(f\"train completed, best_metric: {best_metric:.4f} \" f\"at epoch: {best_metric_epoch}\")"
]
},
{
Expand Down Expand Up @@ -581,8 +565,7 @@
"metadata": {},
"outputs": [],
"source": [
"model.load_state_dict(torch.load(\n",
" os.path.join(root_dir, \"best_metric_model.pth\")))\n",
"model.load_state_dict(torch.load(os.path.join(root_dir, \"best_metric_model.pth\")))\n",
"model.eval()\n",
"y_true = []\n",
"y_pred = []\n",
Expand Down Expand Up @@ -626,8 +609,7 @@
}
],
"source": [
"print(classification_report(\n",
" y_true, y_pred, target_names=class_names, digits=4))"
"print(classification_report(y_true, y_pred, target_names=class_names, digits=4))"
]
},
{
Expand Down
36 changes: 19 additions & 17 deletions 2d_registration/registration_mednist.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@
"train_data = MedNISTDataset(root_dir=root_dir, section=\"training\", download=True, transform=None)\n",
"training_datadict = [\n",
" {\"fixed_hand\": item[\"image\"], \"moving_hand\": item[\"image\"]}\n",
" for item in train_data.data if item[\"label\"] == 4 # label 4 is for xray hands\n",
" for item in train_data.data\n",
" if item[\"label\"] == 4 # label 4 is for xray hands\n",
"]\n",
"print(\"\\n first training items: \", training_datadict[:3])"
]
Expand All @@ -247,9 +248,15 @@
" [\n",
" LoadImageD(keys=[\"fixed_hand\", \"moving_hand\"]),\n",
" EnsureChannelFirstD(keys=[\"fixed_hand\", \"moving_hand\"]),\n",
" ScaleIntensityRanged(keys=[\"fixed_hand\", \"moving_hand\"],\n",
" a_min=0., a_max=255., b_min=0.0, b_max=1.0, clip=True,),\n",
" RandRotateD(keys=[\"moving_hand\"], range_x=np.pi/4, prob=1.0, keep_size=True, mode=\"bicubic\"),\n",
" ScaleIntensityRanged(\n",
" keys=[\"fixed_hand\", \"moving_hand\"],\n",
" a_min=0.0,\n",
" a_max=255.0,\n",
" b_min=0.0,\n",
" b_max=1.0,\n",
" clip=True,\n",
" ),\n",
" RandRotateD(keys=[\"moving_hand\"], range_x=np.pi / 4, prob=1.0, keep_size=True, mode=\"bicubic\"),\n",
" RandZoomD(keys=[\"moving_hand\"], min_zoom=0.9, max_zoom=1.1, prob=1.0, mode=\"bicubic\", align_corners=False),\n",
" ]\n",
")"
Expand Down Expand Up @@ -347,8 +354,7 @@
}
],
"source": [
"train_ds = CacheDataset(data=training_datadict[:1000], transform=train_transforms,\n",
" cache_rate=1.0, num_workers=4)\n",
"train_ds = CacheDataset(data=training_datadict[:1000], transform=train_transforms, cache_rate=1.0, num_workers=4)\n",
"train_loader = DataLoader(train_ds, batch_size=16, shuffle=True, num_workers=2)"
]
},
Expand All @@ -370,11 +376,8 @@
"source": [
"device = torch.device(\"cuda:0\")\n",
"model = GlobalNet(\n",
" image_size=(64, 64),\n",
" spatial_dims=2,\n",
" in_channels=2, # moving and fixed\n",
" num_channel_initial=16,\n",
" depth=3).to(device)\n",
" image_size=(64, 64), spatial_dims=2, in_channels=2, num_channel_initial=16, depth=3 # moving and fixed\n",
").to(device)\n",
"image_loss = MSELoss()\n",
"if USE_COMPILED:\n",
" warp_layer = Warp(3, \"border\").to(device)\n",
Expand Down Expand Up @@ -498,8 +501,7 @@
}
],
"source": [
"val_ds = CacheDataset(data=training_datadict[2000:2500], transform=train_transforms,\n",
" cache_rate=1.0, num_workers=0)\n",
"val_ds = CacheDataset(data=training_datadict[2000:2500], transform=train_transforms, cache_rate=1.0, num_workers=0)\n",
"val_loader = DataLoader(val_ds, batch_size=16, num_workers=0)\n",
"for batch_data in val_loader:\n",
" moving = batch_data[\"moving_hand\"].to(device)\n",
Expand Down Expand Up @@ -543,20 +545,20 @@
"for b in range(batch_size):\n",
" # moving image\n",
" plt.subplot(batch_size, 3, b * 3 + 1)\n",
" plt.axis('off')\n",
" plt.axis(\"off\")\n",
" plt.title(\"moving image\")\n",
" plt.imshow(moving_image[b], cmap=\"gray\")\n",
" # fixed image\n",
" plt.subplot(batch_size, 3, b * 3 + 2)\n",
" plt.axis('off')\n",
" plt.axis(\"off\")\n",
" plt.title(\"fixed image\")\n",
" plt.imshow(fixed_image[b], cmap=\"gray\")\n",
" # warped moving\n",
" plt.subplot(batch_size, 3, b * 3 + 3)\n",
" plt.axis('off')\n",
" plt.axis(\"off\")\n",
" plt.title(\"predicted image\")\n",
" plt.imshow(pred_image[b], cmap=\"gray\")\n",
"plt.axis('off')\n",
"plt.axis(\"off\")\n",
"plt.show()"
]
}
Expand Down
10 changes: 9 additions & 1 deletion 2d_segmentation/torch/unet_evaluation_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@
from monai.inferers import sliding_window_inference
from monai.metrics import DiceMetric
from monai.networks.nets import UNet
from monai.transforms import Activations, EnsureChannelFirstd, AsDiscrete, Compose, LoadImaged, SaveImage, ScaleIntensityd
from monai.transforms import (
Activations,
EnsureChannelFirstd,
AsDiscrete,
Compose,
LoadImaged,
SaveImage,
ScaleIntensityd,
)


def main(tempdir):
Expand Down
4 changes: 2 additions & 2 deletions 3d_classification/densenet_training_array.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"metadata": {},
"outputs": [],
"source": [
"!python -c \"import monai\" || pip install -q \"monai-weekly[nibabel, tqdm]\"\n"
"!python -c \"import monai\" || pip install -q \"monai-weekly[nibabel, tqdm]\""
]
},
{
Expand Down Expand Up @@ -463,7 +463,7 @@
"# For the other dimensions (channel, width, height), use\n",
"# -1 to use 0 and img.shape[x]-1 for min and max, respectively\n",
"depth_slice = img.shape[2] // 2\n",
"occ_sens_b_box = [depth_slice-1, depth_slice, -1, -1, -1, -1]\n",
"occ_sens_b_box = [depth_slice - 1, depth_slice, -1, -1, -1, -1]\n",
"\n",
"occ_result, _ = occ_sens(x=img, b_box=occ_sens_b_box)\n",
"occ_result = occ_result[0, label.argmax().item()][None]\n",
Expand Down
12 changes: 11 additions & 1 deletion 3d_classification/ignite/densenet_training_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,17 @@ def prepare_batch(batch, device=None, non_blocking=False):
post_pred = Compose([Activations(softmax=True)])
# Ignite evaluator expects batch=(img, label) and returns output=(y_pred, y) at every iteration,
# user can add output_transform to return other values
evaluator = create_supervised_evaluator(net, val_metrics, device, True, prepare_batch=prepare_batch, output_transform=lambda x, y, y_pred: ([post_pred(i) for i in decollate_batch(y_pred)], [post_label(i) for i in decollate_batch(y, detach=False)]))
evaluator = create_supervised_evaluator(
net,
val_metrics,
device,
True,
prepare_batch=prepare_batch,
output_transform=lambda x, y, y_pred: (
[post_pred(i) for i in decollate_batch(y_pred)],
[post_label(i) for i in decollate_batch(y, detach=False)],
),
)

# add stats event handler to print validation stats via evaluator
val_stats_handler = StatsHandler(
Expand Down
Loading

0 comments on commit 25131a5

Please sign in to comment.