# How do I view measurements?


```{admonition} What will I learn?
- How to view a table of measurements in QuPath
- How to visualize the distribution of a measurement across cells
- How to visualize measurements on an image
- How to generate smoothed versions of measurements
```


In this section, we'll continue using `OS-2.ndpi`.
To recap, this shows a tissue sample with different cell types, stained for Ki67.
The analysis question we want to answer is:

**What percentage of tumour cells are positive for Ki67?**

However, images contain a lot of information.
There might be some other questions we want to answer as well -- perhaps at the level of individual cells.

As it happens, QuPath already generates measurements during cell detection.
These include morphological measurements (those relating to cell shape) as well as intensity-based measurements (e.g., the intensity of a nuclear stain).

In this chapter, we'll examine those measurements.
Later, we'll see how useful they can be to answer our original analysis question.


## Define a specific annotation

```{figure} ../../images/measure_cells/0-specify-annotation.png
:figclass: margin 

Specify annotation
```

```{admonition} What to do
1. Open `OS-2.ndpi`
2. Run {menuselection}`Objects --> Delete --> Delete all objects`
3. Run {menuselection}`Objects --> Annotations --> Specify annotation...` and set the parameters
   * {guilabel}`Type:` `Rectangle`
   * {guilabel}`X origin:` `48000 px`
   * {guilabel}`Y origin:` `26000 px`
   * {guilabel}`Width:` `20000 px`
   * {guilabel}`Height:` `20000 px`
4. Press {guilabel}`Add annotation`
```

In this chapter and the next we'll want to create a large annotation containing a mixture of cell types.
{menuselection}`Objects --> Annotations --> Specify annotation...` enables us to standardize things so that we should all have *exactly* the same annotation.

Then, **before** detecting cells, take some time to calibrate your estimations and discover how good you are at assessing Ki67 by eye.

```{admonition} Question
How many nuclei do you think are visible inside this annotation?
```

```{admonition} Question
What percentage of all nuclei inside the annotation do you think are Ki67 positive (i.e. brown)?
```


## Detecting cells

```{margin}
{{C}}{{M}}
```

```{admonition} What to do
1. Run {menuselection}`Analyze --> Cell detection --> Positive cell detection`.
2. Select {guilabel}`Optical density sum` in the dropdown box for {guilabel}`Detection image`.
3. Set {guilabel}`Requested pixel size` to `1.0` (to speed things up)
3. Click {guilabel}`Run`.
```

This follows the process we used in ["How do I count positive cells?"](how_do_i_count_positive_cells.md) -- but without being too fussy about annotating only tumour cells.


```{figure} ../../images/measure_cells/1-positive-cell-detection-result.jpg
An annotation containing positive and negative cells of different types
```


## QuPath's measurements


The easiest way to view measurements for cells in QuPath is to open the detection measurement table.
This enables you to get an overview of the different measurements made on detections, to sort detections based on measurements, and to visualize the distribution of measurements across all cells.


```{margin}
{{M}}
```

```{admonition} What to do
1. Run {menuselection}`Measure --> Show detection measurements`.
2. Click {guilabel}`Show plots`.
```

The plot shown by default is a histogram.
This shows a 1-dimensional overview of a single measurement across all cells in the image.


```{figure} ../../images/measure_cells/2-cell-detection-measurements-hist.png
QuPath's detection measurements and a histogram
```

As well as histograms, we can investigate the distribution of measurements using scatter plots.

```{admonition} What to do
3. Click {guilabel}`Scatter plot`.
4. Plot {guilabel}`Nucleus: DAB OD mean` against {guilabel}`Nucleus/Cell area ratio`
```

```{figure} ../../images/measure_cells/3-cell-detection-measurements-scatter.png
QuPath's detection measurements and a scatter plot
```


%## Challenge: finding lost friends
%
%Now that you've seen some ways to view measurements in QuPath, can you help me?
%My friend has gone missing and I need you to find them.
%
%```{admonition} Puzzle
%1. Run {menuselection}`Objects --> Delete --> Delete all objects`.
%2. Open the [`dino.geojson`](https://github.com/user-attachments/files/20590432/dino.zip) file by dragging & dropping it onto %QuPath or by navigating to {menuselection}`File --> Import objects from file...`.
%3. Inspect the cells that have appeared on the image (they will be in the upper left of the image).
%4. Try to find the missing character hiding somewhere in the cells.
%``` 


## Measurement maps

```{margin}
{{M}}
```


```{admonition} What to do
1. Run {menuselection}`Measure --> Show measurement maps`.
2. Select {guilabel}`Nucleus/Cell area ratio`.
3. Choose a colour scheme and enable fill detections ({kbd}`F`).
```

The measurement table allows you to view measurements in isolation.
However, it's also good to view measurements in the context of the image.
This can help to reveal spatial patterns among the cells that can be key to understanding the tissue.

```{figure} ../../images/measure_cells/4-measurement-map.jpg
A measurement map of nucleus/cell area ratio
```

## Smoothed measurements

```{margin}
{{M}}
```

```{figure} ../../images/measure_cells/5-measurement-map-select-smooth.png
:figclass: margin

Smoothed features window
```


```{admonition} What to do
1. Run {menuselection}`Analyze --> Calculate features --> Add smoothed features...`
2. Enter a radius of `50` and click {guilabel}`Run`.
3. Run {menuselection}`Measure --> Show measurement maps`, or click {guilabel}`Update map` if you left the window open.
4. Select the smoothed version of {guilabel}`Nucleus/Cell area ratio` you just created.
5. Choose a color scheme and enable fill detections ({kbd}`F`).
```

While measurement maps are useful, they are often very granular, and it can be hard to identify more high-level patterns in the image.
QuPath can smooth the features among neighbouring cells, which can help pick out where cells in certain regions share similar characteristics.


```{figure} ../../images/measure_cells/6-cell-detection-measurement-map-smoothed.jpg
A smoothed nucleus/cell area ratio heatmap
```



## Challenge: the little cells

You've learned a lot about measurements and classifications in QuPath.
But how good are you at crypto-microbiology?


```{admonition} Puzzle
1. Run {menuselection}`File --> Save` (you'll need your cells later!)
2. Open the image `noise.tif`.
3. Import new cells from the `little-cells.geojson` file by dragging & dropping it onto QuPath, or by navigating to {menuselection}`File --> Import objects from file...`


You should see a dense collection of cells.  

There is a subpopulation of cells with nucleus properties that are different from the others, and this subpopulation has a chilling message for you.

**Use what you've learned in this chapter to find this cell subpopulation.**
```
