DEV Community

StagingBench Team
StagingBench Team

Posted on AI-assisted

Count Aspect Ratio Changes and Calculate Drift Medians in Python

A staged photo has several properties worth recording separately. The aspect ratio flag tells you whether its frame kept the original width to height proportion. The alignment flag records whether the comparison succeeded. Architecture drift summarizes structural change, with lower scores indicating closer preservation of the room. For a listing, a different ratio prompts an inspection of the edges for cropping and of fixed features for stretching. The fields below come from the public StagingBench data, and you can summarize them with collections.Counter and statistics.median from the Python standard library.

The embedded sample contains one record per tool, for 13 records in total. For each tool with an observed ratio change, the sample uses one changed record. For each remaining tool, it uses one kept record. This selection makes both branches of the counting logic visible and is intended for demonstrating the calculation. Each tuple contains the tool, room, aspect ratio flag, alignment flag, and drift score. Room identifiers stay as strings so that "01" retains its leading zero. A blank alignment entry becomes None, distinct from the False value used for a failed alignment.

The loop increments a total for every record. The expression int(not aspect_kept) adds one when the ratio changed and zero when it was kept. Printing changes over totals for each tool gives the count alongside the number of records examined. Iterating over the keys in totals keeps all observed tools in the output, including those with zero changes. In a larger input, the same logic accumulates multiple records for each tool.

Next, the script appends each drift score to the list keyed by its aspect ratio flag. It also builds a second set of lists using aligned is True, which admits only confirmed alignment. That matters because a failed alignment produces a fallback drift score that cannot be compared with one from an aligned room. The median of the full group describes the recorded values, including fallback scores. The aligned median describes the comparable subset. Printing the number of scores next to each median makes the size of that subset visible. An empty group prints "unavailable".

In the full data, 101 of 129 records kept the ratio and 28 changed it. The full group medians were 21.7 for kept and 21.85 for changed. With confirmed alignment, the medians were 21.35 for 72 kept records and 22.5 for 19 changed records. The script below prints the results for the embedded sample, which has only two aligned records in each ratio group. In that sample, the changed group's aligned median of 35.80 is the midpoint of 52.8 and 18.8. Keeping the sample sizes on the same lines as the medians helps prevent a small demonstration from being read as a benchmark result.

from collections import Counter
from statistics import median

records = [
    ("T01", "01", True, None, 16.2),
    ("T02", "01", True, None, 11.0),
    ("T03", "01", True, None, 28.2),
    ("T04", "01", True, None, 27.1),
    ("T05", "04", False, False, 61.5),
    ("T06", "01", False, None, 18.1),
    ("T07", "01", True, True, 25.4),
    ("T08", "01", True, None, 11.4),
    ("T09", "04", False, False, 10.4),
    ("T10", "01", True, True, 10.6),
    ("T11", "01", False, True, 52.8),
    ("T12", "01", True, None, 18.9),
    ("T13", "05", False, True, 18.8),
]

totals = Counter()
changes = Counter()
all_scores = {True: [], False: []}
aligned_scores = {True: [], False: []}

for tool, room, aspect_kept, aligned, drift in records:
    totals[tool] += 1
    changes[tool] += int(not aspect_kept)
    all_scores[aspect_kept].append(drift)
    if aligned is True:
        aligned_scores[aspect_kept].append(drift)

print("Changed aspect ratios by tool")
for tool in sorted(totals):
    print(f"{tool} {changes[tool]}/{totals[tool]}")

for label, groups in (
    ("All sample records", all_scores),
    ("Aligned sample records", aligned_scores),
):
    print(label)
    for aspect_kept, group_label in ((True, "Kept"), (False, "Changed")):
        scores = groups[aspect_kept]
        value = f"{median(scores):.2f}" if scores else "unavailable"
        print(f"{group_label} median drift {value} (n={len(scores)})")
Enter fullscreen mode Exit fullscreen mode

Output:

Changed aspect ratios by tool
T01 0/1
T02 0/1
T03 0/1
T04 0/1
T05 1/1
T06 1/1
T07 0/1
T08 0/1
T09 1/1
T10 0/1
T11 1/1
T12 0/1
T13 1/1
All sample records
Kept median drift 17.55 (n=8)
Changed median drift 18.80 (n=5)
Aligned sample records
Kept median drift 18.00 (n=2)
Changed median drift 35.80 (n=2)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)