Compare - Docs

Skip to content

# Compare

Provides functions to measure the similarity between devices.

## `dice_coefficient(device_a, device_b)`

Calculates the Dice coefficient between two binary devices.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`device_a` | `Device` | The first device. | required
`device_b` | `Device` | The second device. | required

Returns:

Type | Description
--- | ---
`float` | The Dice coefficient between two devices. A value closer to 1 indicates more similarity.

Source code in `prefab/compare.py`

    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    def dice_coefficient(device_a: Device, device_b: Device) -> float:
        """
        Calculates the Dice coefficient between two binary devices.

        Parameters
        ----------
        device_a : Device
            The first device.
        device_b : Device
            The second device.

        Returns
        -------
        float
            The Dice coefficient between two devices. A value closer to 1 indicates more
            similarity.
        """
        intersection = 2.0 * np.sum(
            np.logical_and(device_a.device_array, device_b.device_array)
        )
        size_a = np.sum(device_a.device_array)
        size_b = np.sum(device_b.device_array)
        return intersection / (size_a + size_b)

## `hamming_distance(device_a, device_b)`

Calculates the Hamming distance between two binary devices.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`device_a` | `Device` | The first device. | required
`device_b` | `Device` | The second device. | required

Returns:

Type | Description
--- | ---
`int` | The Hamming distance between two devices. A lower value indicates more similarity.

Source code in `prefab/compare.py`

    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    def hamming_distance(device_a: Device, device_b: Device) -> int:
        """
        Calculates the Hamming distance between two binary devices.

        Parameters
        ----------
        device_a : Device
            The first device.
        device_b : Device
            The second device.

        Returns
        -------
        int
            The Hamming distance between two devices. A lower value indicates more
            similarity.
        """
        return np.sum(device_a.device_array != device_b.device_array)

## `intersection_over_union(device_a, device_b)`

Calculates the Intersection over Union (IoU) between two binary devices.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`device_a` | `Device` | The first device. | required
`device_b` | `Device` | The second device. | required

Returns:

Type | Description
--- | ---
`float` | The Intersection over Union between two devices. A value closer to 1 indicates more similarity (more overlap).

Source code in `prefab/compare.py`

    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    def intersection_over_union(device_a: Device, device_b: Device) -> float:
        """
        Calculates the Intersection over Union (IoU) between two binary devices.

        Parameters
        ----------
        device_a : Device
            The first device.
        device_b : Device
            The second device.

        Returns
        -------
        float
            The Intersection over Union between two devices. A value closer to 1 indicates
            more similarity (more overlap).
        """
        return np.sum(
            np.logical_and(device_a.device_array, device_b.device_array)
        ) / np.sum(np.logical_or(device_a.device_array, device_b.device_array))

## `mean_squared_error(device_a, device_b)`

Calculate the mean squared error (MSE) between two non-binarized devices.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`device_a` | `Device` | The first device. | required
`device_b` | `Device` | The second device. | required

Returns:

Type | Description
--- | ---
`float` | The mean squared error between two devices. A lower value indicates more similarity.

Source code in `prefab/compare.py`

     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    def mean_squared_error(device_a: Device, device_b: Device) -> float:
        """
        Calculate the mean squared error (MSE) between two non-binarized devices.

        Parameters
        ----------
        device_a : Device
            The first device.
        device_b : Device
            The second device.

        Returns
        -------
        float
            The mean squared error between two devices. A lower value indicates more
            similarity.
        """
        return np.mean((device_a.device_array - device_b.device_array) ** 2)