File size: 1,865 Bytes
90d0886
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""

K6 — Encompassment per-cell (R8 fix).



R7 bug: K6 = 0.6138 uniform across all cells

Cause: encompassment_engine_v2 uses a global pattern index.



R8 fix: K6[cell] = fraction of layer-distinct fingerprints

       that this cell has seen (via fp_history).

"""
import numpy as np


def _hamming_sim(a, b):
    """Similarity = 1 - normalized hamming distance on hex strings."""
    if not a or not b or len(a) != len(b):
        return 0.0
    try:
        x = int(a, 16) ^ int(b, 16)
    except ValueError:
        return 0.0
    n_bits = len(a) * 4
    dist = bin(x).count("1") / n_bits
    return 1.0 - dist


def build_layer_fingerprints(cells, history_window=100):
    """Group distinct fingerprints by layer."""
    out = {}
    for c in cells:
        h = getattr(c, "fp_history", [])
        if not h:
            continue
        out.setdefault(c.layer_idx, set()).update(h[-history_window:])
    return out


def compute_k6_percell(cells, layer_fps, sim_threshold=0.7,

                       history_window=100):
    """

    For each cell:

      K6 = fraction of layer's distinct fingerprints

           that the cell has 'seen' (similarity >= sim_threshold).



    Returns {cell_id: K6 in [0, 1]}.

    """
    out = {}
    for c in cells:
        lfps = layer_fps.get(c.layer_idx, set())
        if not lfps:
            out[c.cell_id] = 0.0
            continue
        history = getattr(c, "fp_history", [])
        if not history:
            out[c.cell_id] = 0.0
            continue

        recent = history[-history_window:]
        matched = 0
        for fp in lfps:
            for h in recent:
                if _hamming_sim(fp, h) >= sim_threshold:
                    matched += 1
                    break

        out[c.cell_id] = matched / max(1, len(lfps))
    return out