# Digests and comparison

## Digest

```python
Digest(data: bytes, kind: DigestKind | int)
```

A multi-byte hash: the bytes and the kind that says how they may be compared.

BMH and mHash give bit vectors, Radial quantized coefficients, ColorHash a histogram, ColorMoments a vector of signed 16-bit features. Each comparison method accepts only the kinds it is meaningful for and raises :class:`DigestKindError` for the others; :meth:`similarity` picks the comparison that belongs to the kind.

Digests are immutable, hashable, comparable for equality and picklable. `hex()` and `from_hex()` give a text form that keeps the kind.

### from\_uint64

```python
from_uint64(value: int) -> Digest
```

Wrap a 64-bit hash as an 8-byte `BITS` digest.

The most significant byte comes first, as in :func:`hash_to_hex`.

### from\_hex

```python
from_hex(text: str) -> Digest
```

Parse the text form written by :meth:`hex` (`"<kind>:<hex>"`).

### hex

```python
hex() -> str
```

Return the text form: the kind, a colon, the bytes in lowercase hex.

For example `"bits:3fa0..."`; :meth:`from_hex` parses it back.

### hamming\_distance

```python
hamming_distance(other: Digest) -> int
```

Count the differing bits between two `BITS` digests (BMH, mHash).

### l2\_distance

```python
l2_distance(other: Digest) -> float
```

Euclidean distance between two feature vectors (ColorMoments).

### histogram\_intersection

```python
histogram_intersection(other: Digest) -> float
```

Share of color two `HISTOGRAM` digests (ColorHash) have in common.

The result is 0.0 to 1.0.

### radial\_similarity

```python
radial_similarity(other: Digest) -> float
```

Peak cross-correlation of two Radial digests, -1.0 to 1.0.

Raises :class:`NoStructureError` when either digest is flat (all bytes equal), as the Radial hash of an image without angular structure is: any score would be invented. :data:`RADIAL_PCC_THRESHOLD` is the source's cut for "same image".

### similarity

```python
similarity(other: Digest) -> float
```

Compare with the similarity measure that belongs to the digests' kind.

`BITS`: share of equal bits, 0.0 to 1.0. `HISTOGRAM`: histogram intersection, 0.0 to 1.0. `COEFFICIENTS`: radial peak correlation, -1.0 to 1.0. `VECTOR16` (ColorMoments) has no normalized similarity: use :meth:`l2_distance`.

## DigestKind

Bases: `IntEnum`

What a digest's bytes mean, and so which comparisons it supports.

The values are those of `ph_digest_kind_t`.

## hamming\_distance

```python
hamming_distance(a: int, b: int) -> int
```

Count the differing bits between two 64-bit hashes, 0 to 64.

## similarity

```python
similarity(a: int, b: int) -> float
```

Share of equal bits between two 64-bit hashes.

1.0 means identical; unrelated images score near 0.5.

## hash\_to\_hex

```python
hash_to_hex(value: int) -> str
```

Format a 64-bit hash as 16 lowercase hex digits, most significant first.

## hash\_from\_hex

```python
hash_from_hex(text: str) -> int
```

Parse the 16 hex digits written by :func:`hash_to_hex`.

## RADIAL\_PCC\_THRESHOLD

```python
RADIAL_PCC_THRESHOLD: float = float(lib.PH_RADIAL_PCC_THRESHOLD)
```

## PH\_DIGEST\_MAX\_BYTES

```python
PH_DIGEST_MAX_BYTES: int = int(lib.PH_DIGEST_MAX_BYTES)
```
