Count a month of CT scans with code, chart them, and save your work where others can see it
Step 1 · The question
How many CT scans were done last month, and of which body regions?
A radiology department asks questions like this every month. The answers decide how scanner time is booked, how many technologists are on each shift, and whether a rise in head CTs from the emergency department needs a closer look. The answer sits in a table that the scheduling system can export: one row for each study, with the date, the body part scanned, the patient's age and the department that ordered it.
You could open that table in a spreadsheet and count by hand. In research you do it with code instead, because code can be rerun next month in seconds, gives the same answer every time, and shows a reader exactly what you did. In this lab you write that code in Python, the programming language most used in data science and AI research. You run it in Google Colab, a free website that runs Python on Google's computers, so there is nothing to install. Colab works with notebooks: documents that mix short explanations with cells of code you run one at a time, each showing its result underneath. You save the finished notebook to GitHub, a website that stores code along with every earlier version of it.
Your table is one month of CT studies, 300 to 500 rows, with the columns study_id, date, body_region, age and ordering_dept. It is made up, not real patients, and it is generated from your GitHub username, so your numbers differ from everyone else's. That is how step 5 can check that the answers are really yours.
By the end of this lab you can:
toolkit.py, the file of your own functions that later labs reuse.Step 2 · The idea
Almost every clinical dataset you meet in research is a table. Each row is one thing you counted, such as a study or a patient, and each column is one fact about it. In Python, tables are handled by pandas, a library: a bundle of ready-made code that you load with one line instead of writing it yourself. pandas holds a table in an object called a DataFrame, and gives you tools to filter, count and summarize it.
The first thing to do with almost any table is count: how many rows share each value of a column. Before letting code do it, count a small batch by hand, the way you would with a printout and a pencil.
Twenty-four CT studies from one morning (practice data, not your dataset). Click each study to add it to the bar for its body region. When you have the idea, let the code finish the job.
The output under the button is what one line of pandas, value_counts(), prints for this column. On your full month it gives the answer to the lab's question in less than a second, and it will give the same answer next month on next month's table. Turning those counts into the bar chart above takes one more line, using a plotting library called matplotlib. A chart is part of the answer, not decoration: a reader should understand it from its title and axis labels alone, which is why Part 3 asks you to label yours.
Step 3 · Warm-up
Two short reading checks. They are not graded and always show the explanation. They also work as a skip test: they cover what the notebook's first parts teach.
What does this code print?
regions = ["Head", "Chest", "Chest", "Spine"]
count = 0
for r in regions:
if r == "Chest":
count = count + 1
print(count)
A for loop visits each item of the list in turn and calls it r. The if line adds 1 to count only when r equals "Chest", which happens twice. Note the two kinds of equals sign: = stores a value in a name, while == asks whether two things are equal.
Drag the four lines into the order that loads the table, counts the body regions and draws the chart. On a keyboard, focus a line and use the arrow keys.
Python runs code from top to bottom, so each line can only use what earlier lines made. The library has to be loaded (import) before you use it, the table has to be read before you can count it, and the counts have to exist before you can plot them. pd is the usual short name for pandas, and df the usual name for a DataFrame.
toolkit.py.Step 4 · Notebook
The notebook Lab_1_Python_Colab_GitHub.ipynb starts from zero. Part 1 teaches the handful of Python ideas you need on a short list of CT studies: a variable (a name that holds a value), a list (several values in order), a loop (repeat a step for each item), an if statement and a function (a named, reusable piece of code). Part 2 loads your month of studies and answers the question; Part 3 draws the chart; Part 4 saves everything to GitHub and starts your toolkit.py. Every task ends with a test cell that prints "Check passed" when your code is right, so you always know where you stand.
Type the same name into the notebook's first cell:
GITHUB_USERNAME = "your-username"
Colab needs a Google account. It opens the course's copy of the notebook; follow "Save your work to GitHub" below to keep your own.
Tick each part off when you finish it. Your ticks show as progress on the Research Lab map.
Every task in the notebook is tagged with one mode. In Write tasks you write a few lines yourself from an empty cell, with a hint and a hidden solution. In Fill in tasks most of the code is there and you complete the blanks. Reuse tasks import a function you wrote in an earlier lab from your own toolkit.py, the way real projects grow. In Direct the AI tasks you ask an AI assistant (Colab's built-in one is suggested, but any is fine) to write the code, paste it in, run the test cell, and note in the AI-use log what you asked and what you had to change. Fix tasks give you code "written by an AI" with one realistic mistake to find. Read tasks ask you to put lines in order or predict what code will print before you run it.
Early labs lean on writing code yourself, because that is how the basics stick. Later labs lean on reusing your code, directing an AI and checking its work, because that is how research code is written now, and checking is the part a person must still do.
Colab runs the notebook, but it does not keep your work for you. You keep it in your own copy of the course repository on GitHub (a repository, or repo, is a project folder on GitHub with the full history of every saved change). The first time, you make that copy, called a fork; after that, you save into it whenever you finish a part.
github.com/your-username/CA2i-Research-Launchpad.your-username/CA2i-Research-Launchpad, branch main, and keep the file path lesson-notebooks/Lab_1_Python_Colab_GitHub.ipynb. Write a short note about what changed (for example "Part 2 done") and click OK.Step 5 · Report back
The notebook's last cell prints the numbers below, worked out from your own dataset. Enter them here. If one doesn't match, you get a hint first, then an explanation, and after a third try the answer, so you can find where your analysis went differently. Getting help is fine; the lab is then marked "finished with help".
Loading the answer checker...