Skip to content

is_slide_correctly_stained

Decides if a slide is stained with an expected staining protocol.

This function decides if the per-tile computed color differences are close enough for a slide to be considered stained correctly (according to a given threshold). All of the differences are aggreagated using median.

Parameters:

Name Type Description Default
stain1_diffs list[float] | NDArray[float64]

List of differences for the first stain.

required
stain2_diffs list[float] | NDArray[float64]

List of differences for the second stain.

required
global_threshold float

Threshold used to determine if the color differences are small enough for a slide to be considered correctly stained. Currently recommended thresold values are 28 for H&E stained slides and 22 for H&DAB stained slides (assuming the differeces were computed by the ciede_2000 color difference method). Other similar values might also provide reasonable results.

required

Returns:

Type Description
CorrectStaining

Dictionary with answer if the slide is stained correctly.

Note

The returned dictionary contains the following values:

Key Description
correct_staining True if the differences are considered to be small enough for a slide to be considered as correctly stained.
stain1_diff_median Median of differences for the first stain.
stain2_diff_median Median of difference for the second stain.
Source code in rationai/qc/staining/is_slide_correctly_stained.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def is_slide_correctly_stained(
    stain1_diffs: list[float] | NDArray[np.float64],
    stain2_diffs: list[float] | NDArray[np.float64],
    global_threshold: float,
) -> CorrectStaining:
    """Decides if a slide is stained with an expected staining protocol.

    This function decides if the per-tile computed color differences are close
    enough for a slide to be considered stained correctly
    (according to a given threshold). All of the differences
    are aggreagated using median.

    Args:
        stain1_diffs: List of differences for the first stain.
        stain2_diffs: List of differences for the second stain.
        global_threshold: Threshold used to determine if the color differences
            are small enough for a slide to be considered correctly stained.
            Currently recommended thresold values are `28` for H&E stained slides
            and `22` for H&DAB stained slides (assuming the differeces were computed
            by the `ciede_2000` color difference method). Other similar values
            might also provide reasonable results.

    Returns:
        Dictionary with answer if the slide is stained correctly.

    Note:
        The returned dictionary contains the following values:

        | Key                   | Description                                                                                                   |
        |-----------------------|---------------------------------------------------------------------------------------------------------------|
        | `correct_staining`    | True if the differences are considered to be small enough for a slide to be considered as correctly stained.  |
        | `stain1_diff_median`  | Median of differences for the first stain.                                                                    |
        | `stain2_diff_median`  | Median of difference for the second stain.                                                                    |
    """
    median1 = np.nanmedian(stain1_diffs)
    median2 = np.nanmedian(stain2_diffs)

    result: CorrectStaining = {
        "stain1_diff_median": float(median1),
        "stain2_diff_median": float(median2),
        "correct_staining": bool((median1 + median2) < global_threshold),
    }

    return result