Given two sets of measurements for the same images, Spearman correlation asks whether the images appear in a similar order in both. Here, one set is architecture drift. Lower drift means the staged image better preserved the original walls, windows, and doors. The other set describes framing. The example uses room comparisons from StagingBench and calculates the relationship for zoom deviation and visible overlap.
The embedded string keeps all 129 records, including empty fields. DictReader turns each line into a dictionary. The filter requires alignment_ok to equal the literal string True and zoom to be present. Comparing the string explicitly matters because False is also a nonempty string. There are 91 records after filtering, and each has an overlap value. Using the same rows for both calculations keeps the comparison paired with the corresponding drift scores.
Parse the measurements with Decimal so equal values tie at the precision recorded in the text. Zoom deviation is the absolute distance from 1.0, so changes in either direction count equally. Decimal arithmetic also gives the threshold comparison its literal meaning. A deviation of exactly 0.02 belongs in the group for 0.02 or more, and overlap of exactly 97% belongs in the group for at least 97%.
For the ranks, sort the row indices by measurement and walk through the tied values in order. Each tied group receives the average of the rank positions it occupies. Store those ranks at the original indices so each framing rank stays paired with the drift rank for the same image. statistics.correlation then computes Pearson correlation on the two rank lists, which gives Spearman's rho. Run the code with Python 3.10 or later, when statistics.correlation is available.
import csv
from decimal import Decimal
from io import StringIO
from itertools import groupby
from statistics import correlation, median
DATA = """tool,architecture_drift,alignment_ok,zoom,visible_overlap_pct
T01,16.2,,1.0028,99.81
T01,32.9,False,,
T01,9.2,True,1.0043,99.64
T01,40.1,False,,
T01,13.7,True,1.007,99.29
T01,43.4,False,,
T01,41.0,True,0.9971,99.81
T01,11.6,True,1.0013,99.9
T01,13.1,False,,
T01,22.7,True,1.0022,99.9
T02,11.0,,1.0003,99.61
T02,24.1,True,1.0059,98.91
T02,21.7,True,1.0014,99.95
T02,56.5,False,,
T02,21.4,True,1.0016,99.84
T02,26.2,True,1.0053,98.99
T02,35.1,False,,
T02,17.4,True,0.9985,100.0
T02,9.2,False,,
T02,28.0,False,,
T03,28.2,,0.9898,98.92
T03,30.6,True,0.9914,99.67
T03,29.7,True,1.0031,99.58
T03,29.0,False,,
T03,15.5,True,0.9988,99.87
T03,21.1,True,0.9961,99.94
T03,18.0,True,0.9643,100.0
T03,12.6,True,0.8354,79.69
T03,14.9,False,,
T03,20.2,True,1.0008,99.82
T04,27.1,,0.9871,99.88
T04,26.6,True,0.9971,100.0
T04,33.5,True,0.9942,99.87
T04,28.8,False,,
T04,22.5,True,0.9996,99.87
T04,21.3,True,0.9969,99.69
T04,24.4,True,0.9982,99.78
T04,23.5,True,0.9902,76.3
T04,26.9,False,,
T04,26.6,True,0.9989,99.93
T05,29.4,,1.0002,99.67
T05,6.1,True,1.0,99.74
T05,23.6,True,1.0002,99.67
T05,61.5,False,,
T05,36.2,True,0.875,99.75
T05,57.3,False,,
T05,30.4,True,0.9991,99.71
T05,22.0,True,0.989,68.68
T05,11.4,False,,
T05,19.9,True,1.0001,99.74
T06,18.1,,1.0,89.06
T06,23.3,True,1.0067,88.63
T06,21.0,True,1.0068,88.58
T06,33.3,True,1.3916,71.97
T06,14.8,True,1.0065,86.05
T06,22.5,True,1.0067,83.31
T06,35.2,True,1.0064,98.43
T06,27.4,True,1.0061,88.56
T06,10.2,False,,
T06,21.2,True,1.0063,88.63
T07,25.4,True,1.0039,99.19
T07,33.6,True,1.0054,99.12
T07,22.9,True,1.0009,99.86
T07,50.4,False,,
T07,38.0,True,1.0012,99.78
T07,34.9,True,1.0048,93.51
T07,46.3,True,1.0054,95.96
T07,38.7,True,0.9808,91.54
T07,20.4,False,,
T07,28.2,True,0.9975,99.82
T08,11.4,,1.0,99.69
T08,31.3,True,1.0,99.85
T08,26.3,True,1.0011,99.69
T08,15.1,True,0.9997,99.8
T08,34.9,True,0.9983,100.0
T08,16.6,True,0.9969,99.88
T08,23.5,True,0.998,99.93
T08,9.7,True,0.9985,99.97
T08,5.7,False,,
T08,24.5,True,0.9991,99.85
T09,13.3,True,1.0009,100.0
T09,11.6,True,1.0015,99.95
T09,26.4,True,1.0024,99.55
T09,10.4,False,,
T09,19.8,True,0.9979,97.77
T09,26.0,True,0.9881,95.83
T09,18.9,True,0.9981,99.2
T09,7.8,True,1.001,99.95
T09,23.3,False,,
T09,21.0,True,1.0008,100.0
T10,10.6,True,1.0003,99.93
T10,27.8,True,0.9993,99.93
T10,7.5,True,1.0007,99.82
T10,6.4,True,0.9999,100.0
T10,7.8,True,0.9994,100.0
T10,13.7,True,1.0009,99.95
T10,9.7,True,0.9987,100.0
T10,8.9,True,0.9995,100.0
T10,3.1,False,,
T10,17.0,True,1.0009,99.87
T11,52.8,True,1.1809,63.28
T11,27.1,True,1.0008,97.27
T11,61.3,False,,
T11,54.5,False,,
T11,7.2,True,1.0059,99.45
T11,9.7,True,0.9681,98.96
T11,9.2,True,0.9997,98.99
T11,35.5,True,0.9623,79.61
T11,7.7,False,,
T11,26.6,True,0.9881,98.48
T12,18.9,,1.0056,99.17
T12,25.4,True,1.0003,100.0
T12,19.1,True,1.0005,100.0
T12,15.2,True,1.0005,100.0
T12,8.0,True,1.0005,100.0
T12,16.3,True,1.0206,96.23
T12,42.1,True,1.0003,100.0
T12,11.8,False,,
T12,15.7,True,1.002,99.84
T13,12.7,,1.0,99.74
T13,16.9,True,1.0002,99.67
T13,4.0,True,1.0004,99.61
T13,52.8,False,,
T13,18.8,True,0.9935,97.45
T13,12.6,True,0.9997,99.82
T13,7.3,True,0.9996,99.57
T13,41.5,True,0.9596,85.13
T13,2.4,False,,
T13,57.4,False,,
"""
def average_ranks(values):
ranks = [0.0] * len(values)
ordered = sorted(range(len(values)), key=values.__getitem__)
first_rank = 1
for _, group in groupby(ordered, key=values.__getitem__):
indices = list(group)
last_rank = first_rank + len(indices) - 1
average = (first_rank + last_rank) / 2
for index in indices:
ranks[index] = average
first_rank = last_rank + 1
return ranks
def summarize(label, measure, drift):
rho = correlation(average_ranks(measure), average_ranks(drift))
print(f"{label}: n={len(drift)}, rho={rho:.4f}")
def show_split(label, measure, drift, includes):
scores = [score for value, score in zip(measure, drift) if includes(value)]
print(f"{label}: n={len(scores)}, median drift={median(scores)}")
rows = [
row for row in csv.DictReader(StringIO(DATA))
if row["alignment_ok"] == "True" and row["zoom"]
]
drift = [Decimal(row["architecture_drift"]) for row in rows]
zoom = [abs(Decimal(row["zoom"]) - Decimal("1")) for row in rows]
overlap = [Decimal(row["visible_overlap_pct"]) for row in rows]
summarize("Zoom deviation", zoom, drift)
summarize("Visible overlap", overlap, drift)
zoom_cut = Decimal("0.02")
overlap_cut = Decimal("97")
show_split("Zoom deviation < 0.02", zoom, drift, lambda x: x < zoom_cut)
show_split("Zoom deviation >= 0.02", zoom, drift, lambda x: x >= zoom_cut)
show_split("Visible overlap >= 97%", overlap, drift, lambda x: x >= overlap_cut)
show_split("Visible overlap < 97%", overlap, drift, lambda x: x < overlap_cut)
show_split applies each cutoff to the original measurement and takes the median of the corresponding drift scores in their original units. Each pair of conditions accounts for all 91 qualifying records. Running the script produces the following output.
Zoom deviation: n=91, rho=0.3774
Visible overlap: n=91, rho=-0.3036
Zoom deviation < 0.02: n=82, median drift=21.35
Zoom deviation >= 0.02: n=9, median drift=33.3
Visible overlap >= 97%: n=73, median drift=20.2
Visible overlap < 97%: n=18, median drift=24.75
The full framing comparison also gave rho of 0.2017 for absolute rotation (n = 91) and 0.2731 for invented area (n = 91). The positive coefficients indicate that higher measurements tended to accompany higher drift. Overlap's negative coefficient indicates the reverse. The rank correlations use the full range of each measurement. The median comparisons describe the groups formed by the chosen thresholds.
These are exploratory comparisons from a small sample, and only 9 records fall into the larger zoom deviation group. The framing measures and architecture drift come from the same alignment, so they are not independent. The code measures how the scores move together. It cannot establish whether changing the framing caused the architecture to change.
Top comments (0)