fix: read attribute associations down their own column
Browse filesThe deviation matrix is asymmetric: cell (row=A, col=B) differs from
(row=B, col=A). The sparse map stored only the upper triangle and took the
cell whose row was the alphabetically later of the two names, so which of
the two values reached the graph depended on where the English label fell
in the alphabet.
On a 78-attribute client file this made 121 of 3003 pairs (4.0%) appear or
vanish by that accident, affecting 74 attributes, and mixed both readings
within a single attribute's edge list.
Store both directions keyed "source,target" and make getCorr directional;
the source attribute supplies the column. Both existing call sites already
passed the source first, so level 1 and level 2 needed no change.
Verified against the domain expert's expected top 5 for one attribute, in a
real browser with vis-network: exact match, no console errors.
- build_network.py +17 -17
- docs/deviation-direction.md +72 -0
|
@@ -46,22 +46,24 @@ def generate_network_html(network_data, threshold, max_l1_nodes,
|
|
| 46 |
else:
|
| 47 |
avg_corr = max_corr = min_corr = float(threshold)
|
| 48 |
|
| 49 |
-
# --- Build sparse
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
attr_list = sorted(driver_names, key=lambda x: x.lower())
|
| 51 |
-
attr_to_idx = {name: i for i, name in enumerate(attr_list)}
|
| 52 |
|
| 53 |
sparse_corr = {}
|
| 54 |
-
for i,
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if val >= threshold:
|
| 64 |
-
sparse_corr[f"{i},{j}"] = round(val, 1)
|
| 65 |
|
| 66 |
# Build attribute metadata array (parallel to attr_list)
|
| 67 |
attr_meta = []
|
|
@@ -311,12 +313,10 @@ var FADED = {{
|
|
| 311 |
|
| 312 |
function esc(s) {{ return String(s).replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"'); }}
|
| 313 |
|
| 314 |
-
// ---
|
| 315 |
function getCorr(i, j) {{
|
| 316 |
if (i === j) return 0;
|
| 317 |
-
|
| 318 |
-
var key = a + ',' + b;
|
| 319 |
-
return corrMatrix[key] || 0;
|
| 320 |
}}
|
| 321 |
|
| 322 |
// --- Styling helpers (ported from Python) ---
|
|
|
|
| 46 |
else:
|
| 47 |
avg_corr = max_corr = min_corr = float(threshold)
|
| 48 |
|
| 49 |
+
# --- Build sparse deviation matrix (only above-threshold pairs) ---
|
| 50 |
+
# Directed: the key is "source,target". The source attribute supplies the COLUMN
|
| 51 |
+
# and the target supplies the ROW, because the deviation matrix is asymmetric —
|
| 52 |
+
# an attribute's associations are read down its own column. Storing only one
|
| 53 |
+
# triangle would let the alphabetical sort above decide which of the two values
|
| 54 |
+
# is used. See docs/deviation-direction.md.
|
| 55 |
attr_list = sorted(driver_names, key=lambda x: x.lower())
|
|
|
|
| 56 |
|
| 57 |
sparse_corr = {}
|
| 58 |
+
for i, source in enumerate(attr_list):
|
| 59 |
+
if source not in corr_df.columns:
|
| 60 |
+
continue
|
| 61 |
+
for j, target in enumerate(attr_list):
|
| 62 |
+
if i == j or target not in corr_df.index:
|
| 63 |
+
continue
|
| 64 |
+
val = float(corr_df.loc[target, source])
|
| 65 |
+
if val >= threshold:
|
| 66 |
+
sparse_corr[f"{i},{j}"] = round(val, 1)
|
|
|
|
|
|
|
| 67 |
|
| 68 |
# Build attribute metadata array (parallel to attr_list)
|
| 69 |
attr_meta = []
|
|
|
|
| 313 |
|
| 314 |
function esc(s) {{ return String(s).replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"'); }}
|
| 315 |
|
| 316 |
+
// --- Deviation lookup. Directed: i is the source (column), j the target (row). ---
|
| 317 |
function getCorr(i, j) {{
|
| 318 |
if (i === j) return 0;
|
| 319 |
+
return corrMatrix[i + ',' + j] || 0;
|
|
|
|
|
|
|
| 320 |
}}
|
| 321 |
|
| 322 |
// --- Styling helpers (ported from Python) ---
|
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Reading direction of the deviation matrix
|
| 2 |
+
|
| 3 |
+
## The matrix is asymmetric
|
| 4 |
+
|
| 5 |
+
The block the app reads from `Results Correls` is a **deviation** matrix, not a
|
| 6 |
+
correlation matrix. Rows are attributes (column C), columns are the same attributes
|
| 7 |
+
repeated as headers in row 5. Every pair therefore appears twice — once on each side
|
| 8 |
+
of the diagonal — and **the two cells hold different numbers**.
|
| 9 |
+
|
| 10 |
+
Measured on a real client file (P&G FemCare, 78 attributes, block `AG92:DF169`):
|
| 11 |
+
|
| 12 |
+
- mean gap between `[A,B]` and `[B,A]`: **1.37**, maximum **13.02**
|
| 13 |
+
- all 78 blank cells fall exactly on the diagonal, zero blanks off it
|
| 14 |
+
|
| 15 |
+
That last point matters: the blanks are precisely the self-pairs, which proves the row
|
| 16 |
+
and column labels are correctly aligned. The asymmetry is real data, not a parsing
|
| 17 |
+
error.
|
| 18 |
+
|
| 19 |
+
## The rule
|
| 20 |
+
|
| 21 |
+
**An attribute's associations are read down its own column.** For attribute `X`,
|
| 22 |
+
locate its column via the row-5 header and read down the attribute rows. In code:
|
| 23 |
+
|
| 24 |
+
```python
|
| 25 |
+
value_for(source, target) == corr_df.loc[target, source]
|
| 26 |
+
```
|
| 27 |
+
|
| 28 |
+
Confirmed against the client's own expectation: for `newness` (column `BI`), the
|
| 29 |
+
expected top 5 matches the column reading exactly, and matches the row reading for
|
| 30 |
+
none of the five.
|
| 31 |
+
|
| 32 |
+
## Why the previous behaviour was wrong
|
| 33 |
+
|
| 34 |
+
`build_network.py` sorts attribute names alphabetically to assign index numbers, then
|
| 35 |
+
used to store only the upper triangle, taking the cell whose **row** was the
|
| 36 |
+
alphabetically later of the two names. So which of the two values reached the graph
|
| 37 |
+
depended on where the English label fell in the alphabet. On the file above, **121 of
|
| 38 |
+
3003 pairs (4.0%) appeared or vanished** by that accident, affecting 74 of 78
|
| 39 |
+
attributes, and a single attribute's edge list mixed both readings.
|
| 40 |
+
|
| 41 |
+
`newness` was the reported symptom: mid-alphabet, so 43 partners were read across its
|
| 42 |
+
row and 34 down its column. `contains safe materials` looked fine only because it sorts
|
| 43 |
+
early and none of its pairs happened to straddle the threshold.
|
| 44 |
+
|
| 45 |
+
## Consequence for the data structure
|
| 46 |
+
|
| 47 |
+
The sparse map is **directed**: keys are `"source,target"`, and both directions are
|
| 48 |
+
stored. The same pair legitimately shows different values depending on which node is
|
| 49 |
+
selected — selecting `newness` shows its column value for `makes life easier` (28.3),
|
| 50 |
+
selecting `makes life easier` shows its own column value for `newness` (32.9).
|
| 51 |
+
|
| 52 |
+
`getCorr(i, j)` in the generated JavaScript takes `i` as the source and `j` as the
|
| 53 |
+
target and looks the key up directly. Both call sites already pass the source first:
|
| 54 |
+
level 1 uses the selected node, level 2 uses the level-1 node.
|
| 55 |
+
|
| 56 |
+
## Open questions
|
| 57 |
+
|
| 58 |
+
Deliberately deferred, to revisit with the codeowner:
|
| 59 |
+
|
| 60 |
+
- **Level 2** currently follows the same rule (each level-1 node's own column). This is
|
| 61 |
+
consistent, but was not independently confirmed against an expected result.
|
| 62 |
+
- **Undirected framing.** The UI calls this an "association network", which implies one
|
| 63 |
+
value per pair. Averaging the two directions would restore that at the cost of
|
| 64 |
+
showing neither measured value. Directional was chosen because the underlying
|
| 65 |
+
statistic is directional and the UI is already ego-centric.
|
| 66 |
+
|
| 67 |
+
## Related known behaviour
|
| 68 |
+
|
| 69 |
+
Blank cells in column D (Category Index) become `0`, which renders as the smallest node
|
| 70 |
+
labelled `(0)` and sorts last. This is **expected**: some attributes are not asked in
|
| 71 |
+
the category modules the index is derived from, so no value exists. Left as-is by
|
| 72 |
+
decision, not oversight.
|