#!/usr/bin/env ruby # frozen_string_literal: true # Compare perimeters in a kgrid CSV against a reference CSV. # # compare.rb OURS.csv REF.csv [TOL] # # The reference may be another kgrid CSV (column "perimeter") or the old # solver's extracted_data.csv (column "Perimeter"). Prints the number of # common k, mismatches beyond TOL (default 1e-9 relative), and the largest # difference. require 'csv' def load(path) table = CSV.read(path, headers: true) col = (table.headers & %w[perimeter Perimeter]).first abort("#{path}: no perimeter column") unless col table.each_with_object({}) { |row, h| h[Integer(row['k'])] = Float(row[col]) } end ours_path, ref_path, tol_arg = ARGV abort('usage: compare.rb OURS.csv REF.csv [TOL]') unless ours_path && ref_path tol = tol_arg ? Float(tol_arg) : 1e-9 ours = load(ours_path) ref = load(ref_path) common = (ours.keys & ref.keys).sort diffs = common.map { |k| [k, (ours[k] - ref[k]).abs / (1.0 + ref[k].abs)] } bad = diffs.select { |_, d| d > tol } worst = diffs.max_by { |_, d| d } puts "common k: #{common.size} (#{common.first}..#{common.last})" puts "mismatches (> #{tol} relative): #{bad.size}" puts "max relative difference: #{worst ? format('%.3e at k=%d', worst[1], worst[0]) : 'n/a'}" bad.first(20).each { |k, _| puts " k=#{k}: ours=#{ours[k]} ref=#{ref[k]}" } exit(bad.empty? ? 0 : 1)