#!/usr/bin/env python3 """Recompute four month/area strata from the existing published saved CSV. No network, files written or fresh transaction collection. """ import csv,json,hashlib,sys from decimal import Decimal from collections import defaultdict from pathlib import Path def summarize(path): path=Path(path);groups=defaultdict(list);saved=defaultdict(set);rows=0 with path.open(encoding='utf-8-sig',newline='') as stream: for row in csv.DictReader(stream): if row['집계포함']!='포함':continue amount=Decimal(row['금액(만원)'].replace(',',''));area=Decimal(row['전용면적(㎡)']) if not amount.is_finite() or not area.is_finite() or amount<=0 or area<=0:raise ValueError('Finite positive amount and area required') key=(row['계약월'],'le85' if area<=85 else 'gt85');groups[key].append(amount);saved[key].add(row['자료저장시각(KST)']);rows+=1 result=[] for (month,area),values in sorted(groups.items()): values.sort();n=len(values);indices=[(n+1)//2] if n%2 else [n//2,n//2+1];middle=[values[i-1] for i in indices];median=sum(middle)/len(middle) result.append({'month':month,'area_group':area,'count':n,'middle_positions_one_based':indices,'middle_values_manwon':[str(v) for v in middle],'median_manwon':str(median),'median_is_in_selected_input':median in values,'saved_at_kst':sorted(saved[(month,area)])}) return {'analysis_date':'2026-11-01','source_sha256':hashlib.sha256(path.read_bytes()).hexdigest(),'included_rows':rows,'groups':result,'method':'Month then existing <=85/>85 square-metre filter. Sort amounts and average middle one/two values. Show all non-empty groups.','limits':'Existing saved CSV only;not fresh collection,current market value or matched-property price change. Area bands do not hold building/floor/day constant. One-record group is not a representative price index.'} if __name__=='__main__':print(json.dumps(summarize(sys.argv[1]),ensure_ascii=False,indent=2))