Skip to content

Commit

Permalink
Fix notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeschamps committed Jul 6, 2024
1 parent 906e7bb commit ac39361
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
29 changes: 16 additions & 13 deletions applications/care/denoising/2D/U2OS_CARE.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import tifffile\n",
"from PIL import Image\n",
"\n",
"from careamics import CAREamist\n",
"from careamics.config import create_care_configuration\n",
"from careamics.utils.metrics import scale_invariant_psnr\n",
"from careamics_portfolio import PortfolioManager"
"from careamics_portfolio import PortfolioManager\n",
"from PIL import Image"
]
},
{
Expand Down Expand Up @@ -204,7 +203,7 @@
],
"source": [
"config = create_care_configuration(\n",
" experiment_name=\"care_n2n\",\n",
" experiment_name=\"care_U20S\",\n",
" data_type=\"tiff\",\n",
" axes=\"YX\",\n",
" patch_size=(128, 128),\n",
Expand Down Expand Up @@ -417,7 +416,7 @@
"\n",
"for i in range(3):\n",
" pred_image = prediction[images[i]].squeeze()\n",
" \n",
"\n",
" psnr_noisy = scale_invariant_psnr(test_GT[images[i]], test_low[images[i]])\n",
" psnr_result = scale_invariant_psnr(test_GT[images[i]], pred_image)\n",
"\n",
Expand All @@ -428,7 +427,7 @@
" ax[i, 1].title.set_text(f\"Prediction\\nPSNR: {psnr_result:.2f}\")\n",
"\n",
" ax[i, 2].imshow(test_GT[images[i]], cmap=\"gray\")\n",
" ax[i, 2].title.set_text(\"Ground-truth\")\n"
" ax[i, 2].title.set_text(\"Ground-truth\")"
]
},
{
Expand Down Expand Up @@ -490,24 +489,28 @@
"cv_image_pred = prediction[im_idx].squeeze()\n",
"\n",
"# create image\n",
"cover = np.zeros_like(cv_image_noisy, dtype=np.float32) \n",
"cover = np.zeros_like(cv_image_noisy, dtype=np.float32)\n",
"width = cover.shape[1]\n",
"\n",
"# # normalize train and prediction\n",
"norm_noise = (cv_image_noisy - cv_image_noisy.min()) / (cv_image_noisy.max() - cv_image_noisy.min())\n",
"norm_pred = (cv_image_pred - cv_image_pred.min()) / (cv_image_pred.max() - cv_image_pred.min())\n",
"norm_noise = (cv_image_noisy - cv_image_noisy.min()) / (\n",
" cv_image_noisy.max() - cv_image_noisy.min()\n",
")\n",
"norm_pred = (cv_image_pred - cv_image_pred.min()) / (\n",
" cv_image_pred.max() - cv_image_pred.min()\n",
")\n",
"\n",
"# fill in halves\n",
"cover[:, :width // 2] = norm_noise[:, :width // 2]\n",
"cover[:, width // 2:] = norm_pred[:, width // 2:]\n",
"cover[:, : width // 2] = norm_noise[:, : width // 2]\n",
"cover[:, width // 2 :] = norm_pred[:, width // 2 :]\n",
"\n",
"# plot the single image\n",
"plt.imshow(cover)\n",
"\n",
"# save the image\n",
"im = Image.fromarray(cover * 255)\n",
"im = im.convert('L')\n",
"im.save(\"U2OS_CARE.jpeg\")\n"
"im = im.convert(\"L\")\n",
"im.save(\"U2OS_CARE.jpeg\")"
]
},
{
Expand Down
24 changes: 14 additions & 10 deletions applications/n2v/2D/SEM_N2V2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
"from pathlib import Path\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import tifffile\n",
"import numpy as np\n",
"from PIL import Image\n",
"import tifffile\n",
"from careamics import CAREamist\n",
"from careamics.config import create_n2v_configuration\n",
"from careamics_portfolio import PortfolioManager"
"from careamics_portfolio import PortfolioManager\n",
"from PIL import Image"
]
},
{
Expand Down Expand Up @@ -709,9 +709,9 @@
"\n",
"fig, ax = plt.subplots(2, 2, figsize=(10, 10))\n",
"ax[0, 0].imshow(train_image, cmap=\"gray\")\n",
"ax[0, 1].imshow(prediction.squeeze(), cmap=\"gray\")\n",
"ax[0, 1].imshow(prediction[0].squeeze(), cmap=\"gray\")\n",
"ax[1, 0].imshow(train_image[y_start:y_end, x_start:x_end], cmap=\"gray\")\n",
"ax[1, 1].imshow(prediction.squeeze()[y_start:y_end, x_start:x_end], cmap=\"gray\")"
"ax[1, 1].imshow(prediction[0].squeeze()[y_start:y_end, x_start:x_end], cmap=\"gray\")"
]
},
{
Expand All @@ -736,7 +736,7 @@
"y_start, height = 1400, 512\n",
"\n",
"# create image\n",
"cover = np.zeros((height, width)) \n",
"cover = np.zeros((height, width))\n",
"\n",
"# normalize train and prediction\n",
"norm_train = (train_image - train_image.min()) / (train_image.max() - train_image.min())\n",
Expand All @@ -745,16 +745,20 @@
"norm_pred = (pred - pred.min()) / (pred.max() - pred.min())\n",
"\n",
"# fill in halves\n",
"cover[:, :width // 2] = norm_train[y_start:y_start + height, x_start:x_start + width // 2]\n",
"cover[:, width // 2:] = norm_pred[y_start:y_start + height, x_start + width // 2:x_start + width]\n",
"cover[:, : width // 2] = norm_train[\n",
" y_start : y_start + height, x_start : x_start + width // 2\n",
"]\n",
"cover[:, width // 2 :] = norm_pred[\n",
" y_start : y_start + height, x_start + width // 2 : x_start + width\n",
"]\n",
"\n",
"# plot the single image\n",
"plt.imshow(cover, cmap=\"gray\")\n",
"\n",
"# save the image\n",
"im = Image.fromarray(cover * 255)\n",
"im = im.convert('L')\n",
"im.save(\"SEM_N2V2.jpeg\")\n"
"im = im.convert(\"L\")\n",
"im.save(\"SEM_N2V2.jpeg\")"
]
},
{
Expand Down
1 change: 0 additions & 1 deletion scripts/run_all_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
notebooks.extend(list(Path(folder).rglob("*.ipynb")))

# run each notebook
last_failed = False
for nb in notebooks:
print(f"Running {nb}")
subprocess.run(["python", "scripts/run_notebook.py", "-n", str(nb)])
Expand Down
2 changes: 1 addition & 1 deletion scripts/run_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
print(f"{datetime.now()} Done running {nb}")

# save notebook
if not last_failed and n_cells == len(nb_out.cells):
if not last_failed and len(nb_out.cells) > 0:
# make sure that the notebook has the same number of cells
# this can avoid overwriting the notebook with an empty one
print(f"{datetime.now()} Writing {nb}")
Expand Down

0 comments on commit ac39361

Please sign in to comment.