Back to: Python for Medical Imaging
Difficulty: Beginner
Reading Time: 20–25 minutes
Prerequisites:
- Python installed
- Visual Studio Code installed (recommended)
- Anaconda installed (optional but recommended)
Learning Objectives
After completing this tutorial, you will be able to:
- Understand what Jupyter Notebook is.
- Create and manage notebooks.
- Run Python code interactively.
- Combine code, text, equations, and images in one document.
- Visualize data and images.
- Save and share notebooks.
- Prepare for future Medical Imaging and AI tutorials.
Introduction
However, this section introduces Jupyter and its core concepts.
Medical imaging research involves much more than writing Python scripts. Researchers often need to combine code, figures, explanations, equations, and results into a single document.
Jupyter Notebook makes this possible by providing an interactive environment where code and documentation coexist.
Jupyter for stepwise execution
Moreover, Jupyter lets you execute code one section at a time and see results immediately.
This interactive workflow makes Jupyter an indispensable tool for data science, artificial intelligence, and medical imaging research.
Why Use Jupyter Notebook?
Jupyter advantages
Additionally, Jupyter Notebook offers several advantages:
- Interactive coding
- Immediate visualization of results
- Integrated documentation using Markdown
- Support for mathematical equations (LaTeX)
- Easy sharing of research
- Reproducible scientific workflows
- Excellent support for Python libraries
It is widely used by universities, research institutions, and healthcare organizations.
Jupyter in the Medical Imaging Workflow
CT / MRI / PET Image
↓
Read DICOM Images
↓
(pydicom / SimpleIT
↓
Jupyter Notebook
Data Analysis Image Visualization AI Models
Clinical Interpretation
Why this matters: A single notebook can contain the code to load images, visualize them, perform measurements, and explain the results all in one place.
What is Jupyter Notebook?
A Jupyter Notebook is an interactive document composed of cells.
There are two main types:
Code Cells
Used to write and execute Python code.
print ("Welcome to PyMedLab!")
Markdown Cells
Used to write formatted text.
Example:
# Medical Imaging
This notebook demonstrates how to load a CT image.
Starting Jupyter Notebook
Using Anaconda
Open:
Anaconda Navigator
↓
Launch Jupyter Notebook
Using the Command Line
Navigate to your project folder and type:
jupyter notebook
Your browser will open automatically.
Creating Your First Notebook
Click
New
↓
Python 3
Rename the notebook
Introduction_to_Python.ipynb
Running Code
Type
print("Hello PyMedLab!")
Press
Shift + Enter
Output
Hello PyMedLab!
Variables
patient = "John Doe"
age = 54
print(patient)
print(age)
Output
John Doe
54
Performing Calculations
slice_thickness = 1.25
number_of_slices = 320
scan_length = slice_thickness * number_of_slices
print(scan_length)
Output
400.0
Using NumPy
import numpy as np
image = np. random. randint (-1000,1000, (512,512))
print (image. shape)
Output
(512,512)
This creates a synthetic CT image represented as a NumPy array.
Displaying Images
import matplotlib. pyplot as plt
plt. imshow(image, cmap="gray")
plt.title("Synthetic CT Image")
plt. axis("off")
plt.show()
You should see a grayscale image.
Combining Text and Code
Markdown allows you to explain your work directly in the notebook.
Example:
# CT Image Analysis
The following code displays a simulated CT image.
Next, we will calculate image statistics.
Mathematical Equations
Jupyter supports LaTeX.
Example:
HU = Pixel Value × Rescale Slope + Rescale Intercept
This is useful when documenting imaging physics and quantitative analysis.
Saving Your Notebook
Click
File
↓
Save Notebook
or press
Ctrl + S
The notebook is saved with the extension:
.ipynb
Organizing Your Projects
A suggested project structure:
MedicalImagingProjects/
├ notebooks/
├ CT_Analysis.ipynb
├ MRI_Visualization.ipynb
├ data/
├ CT/
├ MRI/
images/
├ scripts/
└── README.md
Keeping notebooks, data, and scripts organized makes projects easier to maintain.
Common Beginner Mistakes
Forgetting to Run Previous Cells
Variables defined in earlier cells won’t exist unless those cells have been executed.
Running Cells Out of Order
Always execute cells from top to bottom to ensure the notebook’s state is consistent.
Not Saving Frequently
Remember to save your notebook regularly.
Mixing Too Many Topics
Keep each notebook focused on a single task or experiment.
Best Practices
✔ Give notebooks descriptive names.
✔ Add Markdown headings.
✔ Explain your code.
✔ Keep notebooks organized.
✔ Save frequently.
✔ Store datasets in separate folders.
Exercises
Exercise 1
Create a notebook named:
Introduction.ipynb
Exercise 2
Print your name and profession.
Exercise 3
Create variables representing:
- Patient age
- Slice thickness
- Number of CT slices
Print the values.
Exercise 4
Generate a random 512 × 512 image using NumPy and display it with Matplotlib.
Exercise 5
Add a Markdown section titled:
# CT Image Analysis
Write two or three sentences explaining what the notebook does.
Summary
In this tutorial, you learned:
- What Jupyter Notebook is.
- How to create and run notebooks.
- How to use code and Markdown cells.
- How to visualize data.
- How to organize notebooks for medical imaging projects.
Jupyter Notebook is one of the most widely used tools in scientific computing because it combines programming, documentation, and visualization in a single interactive environment.
What’s Next?
In the next lesson, Creating Virtual Environments, you’ll learn how to isolate project dependencies using Python’s venv module. This ensures that each project has its own set of libraries, making your code more reproducible and easier to share with colleagues.
PyMedLab Tip
Think of a Jupyter Notebook as a digital laboratory notebook. Instead of recording experiments on paper, you document your research with code, figures, equations, and observations in one place. This makes your analyses reproducible, easier to review, and simpler to share with collaborators.

Leave a Reply