DEV Community

StagingBench Team
StagingBench Team

Posted on AI-assisted

Cross-tabulate two three-state fields with collections.Counter

A CSV reader gives you strings, including an empty string when a cell is blank. That distinction matters in the StagingBench room results. A framing value of False records a measured failure. An empty framing value means alignment failed and the framing result is unknown. Both cases belong in the total. They need different keys in the count.

The unit to count is one room record. Take aspect_ratio_kept first and framing_pass second, convert each value, then use the resulting tuple as a key. The order matters because (True, False) describes an output that kept its aspect ratio and failed framing. Reversing the two values describes an output that changed aspect ratio and passed framing. Those combinations occur different numbers of times.

The script takes the CSV path as its argument, a flat export of the room records from results.json with the columns tool, room, aspect_ratio_kept and framing_pass. Save it as cross_tab.py and run python3 -I cross_tab.py aspect_framing.csv. The three sample rows are an in-memory test that runs before the script opens the results.

import csv
import sys
from collections import Counter
from io import StringIO

states = {"False": False, "True": True, "": None}
order = {False: 0, True: 1, None: 2}

def tally(stream):
    pairs = Counter()
    for row in csv.DictReader(stream):
        aspect = states[row["aspect_ratio_kept"]]
        framing = states[row["framing_pass"]]
        pairs[aspect, framing] += 1
    return pairs

sample = StringIO("aspect_ratio_kept,framing_pass\nTrue,\nTrue,False\nTrue,\n")
assert tally(sample) == {(True, None): 2, (True, False): 1}

with open(sys.argv[1], newline="", encoding="utf-8") as stream:
    counts = tally(stream)

for pair in sorted(counts, key=lambda p: (order[p[0]], order[p[1]])):
    print(pair, counts[pair])
print("Total", sum(counts.values()))
Enter fullscreen mode Exit fullscreen mode

The run printed:

(False, False) 14
(False, True) 4
(False, None) 8
(True, False) 10
(True, True) 65
(True, None) 18
Total 119
Enter fullscreen mode Exit fullscreen mode

The states mapping does the work that a truth test would get wrong. In Python, bool("False") is True because the string contains characters. At the other end, bool("") is False, which would turn every unknown framing result into a failure. Looking up the literal CSV value preserves the meaning of all three states. It also makes an unexpected spelling visible. A value outside the mapping raises a KeyError at the conversion step.

The small test exercises that distinction through the same CSV reader as the full run. The first and third sample rows end with a comma, leaving the framing field empty. They contribute two observations to (True, None). The middle row spells out False and contributes one to (True, False). Checking the whole counter catches a mistake that checking its total would miss. A parser that merged both keys would still report three observations.

A nested dictionary would also work. The outer key could select the aspect state and the inner key could select the framing state. That layout is convenient when you want all the framing counts for a particular aspect result. Here the output needs one line per observed pair, so a tuple key keeps the lookup and increment together. An if and else chain would require separate branches for the combinations, even though every branch would perform the same operation of adding one observation.

Sorting needs a small decision of its own. Python can compare two Boolean values, but it cannot order None against a Boolean. The order mapping supplies a display position for each state. The sort uses the aspect position first and the framing position second. It leaves the counter keys intact, so a reader sees None wherever the original cell was empty. Only pairs present in the input get a line.

Finally, read down the two lines ending in None. Their counts are eight and 18, giving 26 records with unknown framing. The two lines ending in False contribute 14 and 10, giving 24 measured failures. Folding blanks into False would print 50 framing failures and still produce a total of 119. The total checks that every room record contributed once. The three-row test checks that those contributions went to the intended places.

Top comments (0)