Microglia Counter — Study Notes
🐍 Python
What does a function definition look like in Python?
def function_name(param1, param2):
code here
return result
The keyword def starts it, parameters go in parentheses, and return sends a value back.
What is the difference between int(), float(), and str()?
int()converts to a whole number (drops decimals)float()converts to a decimal numberstr()converts to text
Example: int(3.9) → 3 (not 4!)
What does return False inside a function do? It immediately exits the function and sends the value False back to wherever the function was called. Any code after return is skipped.
What is a dictionary and how do you access a value? A dictionary stores key:value pairs.
params = {"min_size": 120}
params["min_size"] # → 120
What does min(255, max(0, value)) do? It clamps a value so it never goes below 0 or above 255. max(0, value) prevents negatives; min(255, ...) prevents going over 255. Used to keep pixel values in a valid range.
What is a for loop with two variables like for (x, y) in points? It unpacks each tuple in the list. If points = [(1,2),(3,4)], the loop runs twice: first with x=1, y=2, then with x=3, y=4.
What does try/except do? try runs code that might fail. If an error occurs, except catches it and runs alternative code instead of crashing the whole program.
try:
risky()
except Exception as e:
print(e)
What is the difference between a parameter and an argument?
A parameter is the placeholder name in the function definition.
An argument is the actual value passed when calling the function.
def add(a, b): # a and b are parameters
add(3, 4) # 3 and 4 are arguments
What does dict(params) do? It creates a shallow copy of the dictionary params. Changes to the copy do not affect the original — useful in the auto-tuning loop where we test many parameter combinations without overwriting the defaults.
What does append() do on a list? Adds an item to the end of the list.
valid = []
valid.append((cx, cy))
After this, valid contains one (x,y) tuple.
💡 Concepts
What is normalization and why do we do it? Normalization rescales pixel values so the minimum = 0 and maximum = 1. This removes differences in brightness between images so the threshold works consistently across different samples.
What is Otsu thresholding? An algorithm that automatically finds the best cutoff value to separate foreground (cells) from background. It looks at the histogram of pixel values and finds the threshold that minimizes variance within each group.
What is solidity and what does it tell us about a cell? Solidity = cell area ÷ bounding box area. A value near 1.0 = compact/round shape. A low value = irregular or branchy shape. Real microglia have moderate solidity — not perfectly round, not too spiky.
What is circularity and what does a value of 1.0 mean? Circularity = 4π × area ÷ perimeter². A value of 1.0 is a perfect circle. Microglia have processes (branches), so their circularity is well below 1.0. We reject near-perfect circles as unlikely microglia.
What is an F1 score and why use it instead of just counting? F1 = 2 × precision × recall ÷ (precision + recall). It balances two errors: detecting too many false cells (bad precision) and missing real cells (bad recall). A count alone cannot distinguish these — F1 penalises both equally.
What is a grid search? A brute-force parameter tuning method. You define a list of values for each parameter and test every combination. The program tries all 180 combinations (5×4×3×3) and keeps whichever set gets the best F1 score against your reference.
What is a binary mask? A black-and-white image where white pixels (value 255) mark the region of interest and black pixels (value 0) mark everything outside. Used here to restrict detection to inside the ROI the user drew in Fiji.
What is abstraction in programming? Breaking a complex task into smaller named functions, each doing one thing. Instead of one giant block, detect() detects, save_cell_counter_xml() saves, passes_morphology() filters. Each piece is easier to read, test, and fix.
Why do we use Thread.sleep(2000) before loading markers? Cell Counter takes ~2 seconds to fully open. Without the pause, the load command runs before the window is ready and fails. Thread.sleep(2000) tells Jython to wait 2000 milliseconds before continuing.
What is XML and why does Fiji use it? XML is a structured text format using opening and closing tags (like HTML). Fiji's Cell Counter stores marker positions in XML because it is human-readable, easy to parse, and widely supported across tools and programming languages.
🔬 Fiji / ImageJ
What is ImagePlus in Fiji? The main image object in ImageJ/Fiji. It holds pixel data, dimensions, calibration, and metadata. You pass it around between functions. imp.getProcessor() gives you access to the actual pixel values.
What is an ImageProcessor? The object inside an ImagePlus that holds and manipulates pixel data. You call methods like .subtract(), .multiply(), .threshold() on it to do math on every pixel in the image.
What does IJ.run(imp, "Gaussian Blur...", "sigma=3") do? Runs Fiji's built-in Gaussian Blur command on the image imp with a blur radius of 3 pixels. Blurring reduces noise so the threshold works more cleanly — small speckles get smoothed away.
What is ParticleAnalyzer and what does it find? A built-in Fiji tool that finds connected white regions (blobs) in a binary image and measures them. We use it to find all blobs within our min/max size range after thresholding the microglia channel.
What is RoiManager(False) and why use it instead of RoiManager()? RoiManager(False) creates a hidden ROI Manager that never appears in the Fiji UI. This prevents detected blobs from cluttering the user's ROI list — we only want the final Cell Counter markers to be visible.
What is GenericDialog used for? A Fiji class that creates a pop-up input window. You add fields with addFileField(), addNumericField(), addCheckbox(), then call showDialog() to display it. After the user clicks OK, you read values back with getNextString(), getNextNumber(), etc.
What is an Overlay in Fiji? A layer of annotations drawn on top of an image without modifying the actual pixels. We add PointRoi objects (red dots) to an Overlay to show detected cell locations. The user can toggle it off without affecting the image.
What is Jython? Python that runs inside the Java Virtual Machine. It lets you write Python code that directly calls Fiji's Java classes and methods. This is how our Python plugin uses Fiji tools like ImagePlus, ParticleAnalyzer, and RoiManager.
🧫 Biology
Why do we use both Iba1 and P2RY12 channels to detect microglia? Both are microglia markers, so combining them (averaging the two channels) increases signal-to-noise. A cell must be bright in both channels to stand out, which reduces false positives from cells that express only one marker weakly.
Why do we use DAPI to validate detected microglia blobs? DAPI marks ALL cell nuclei. A detected Iba1/P2RY12 blob that has no DAPI signal inside it is likely debris or an artifact, not a real cell. Requiring DAPI overlap confirms that each detected blob contains an actual nucleus.