"""Keep documented author-version and selection discrepancies visible."""
from pathlib import Path
import collections
import json
import re
from inspect_cluster_samples import read_benchmarks

rows = []
for line in Path('inputs/curtis2019-source/Table1.tex').read_text().splitlines():
    fields = [value.strip() for value in line.split('&')]
    if len(fields) == 12 and fields[0].isdigit():
        rows.append(fields)
new = read_benchmarks(Path('inputs/curtis2020-source/Benchmark_Clusters.txt'))
ngc = [row for row in new if row['cluster'] == 'NGC 6811']
identifiers = {row['source_id'].split()[-1] for row in ngc}
old = {row[2] for row in rows}
codes = collections.Counter(re.sub(r'\\.*', '', row[-1]).strip() for row in rows)
report = {'status': 'VERSION/SELECTION DIFFERENCES RETAINED; no claim that all author sources are identical',
          'curtis2019_v3_tex_rows': len(rows), 'curtis2019_abstract_claimed_measured_members': 171, 'curtis2019_tex_codes': dict(codes),
          'curtis2020_machine_ngc6811_rows': len(ngc), 'common_ids': len(identifiers & old), 'only_2019': len(old - identifiers), 'only_2020': len(identifiers - old),
          'curtis2020_text_describes_yy_selection': 135,
          'qualification': 'The2020 source machine table has161 NGC6811 rows, not the135YY described for its polynomial fit. The2019 TeX source has172 rows with codeY/R/S rather than two membership flags. Use an explicitly stated temperature-limited machine subset for the independent exercise; do not label it the published135YY calibration or silently fabricate missing flags.',
          'proposed_scope': 'Fit the actual frozen author2020 table over4200–6000K and retain temperature-only selection counts, robust/nonrobust comparisons and flagged Ruprecht counterexamples. Published polynomial coefficients remain separately labeled references.'}
assert len(new) == 923 and len(ngc) == 161
Path('results/sample-version-differences.json').write_text(json.dumps(report, indent=2) + '\n')
print(json.dumps(report, indent=2))
