# -*- coding: utf-8 -*- """The SWORD confidence grades (Appendix B) and the classes of the gauge - node pairs with different river names (5.3). Confidence: the match_confidence of the SWORD reaches and nodes of the release, and the not-found reaches that still have placed nodes. Names: the 368 pairs of Data22 whose river names differ, in three classes. These classes are not computed here; the class and the reason of each pair are in ../input/pairs_different_river_names_classified.csv. This script checks that the file holds exactly the pairs of Data22 and counts the classes. Not reproducible from the release: the share of nodes moved by the two rules that use SWORD's links (0.15%) and the link test without the links touching such nodes (98.8%) come from a diagnostic table of the program that made the release; that table is not released. Input: paths.SWORD_XW, ../Data/Data22_pairs_different_river_names.csv (run Data22 first) and ../input/pairs_different_river_names_classified.csv. Output: ../Data/Data23_sword_confidence_and_name_classes.json Usage: python Data23_sword_confidence_and_name_classes.py """ import os, sys, json import pandas as pd, pyarrow.parquet as pq sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import paths as PTH HERE = os.path.dirname(os.path.abspath(__file__)) PAPER = os.path.dirname(HERE) DATD = os.path.join(PAPER, 'Data') STEM = os.path.join(PTH.SWORD_XW, 'flownest_merit90m_crosswalk_sword_v17c_') GRADES = ['high', 'medium', 'low', 'not_found'] # ---- confidence of the reaches and the nodes R = pq.read_table(STEM + 'reach.parquet', columns=['match_confidence', 'nodes_placed']).to_pandas() N = pq.read_table(STEM + 'node.parquet', columns=['match_confidence']).to_pandas() assert set(R.match_confidence) == set(N.match_confidence) == set(GRADES) rc, nc = R.match_confidence.value_counts(), N.match_confidence.value_counts() not_found = R.match_confidence == 'not_found' assert ((R.nodes_placed > 0) | not_found).all(), 'every matched reach has placed nodes' O = dict(release=PTH.SWORD_XW_NAME, reaches=len(R), reaches_by_confidence={g: int(rc[g]) for g in GRADES}, reaches_high_pct=round(100 * rc['high'] / len(R), 1), not_found_with_placed_nodes=int((not_found & (R.nodes_placed > 0)).sum()), nodes=len(N), nodes_by_confidence={g: int(nc[g]) for g in GRADES}, nodes_high_pct=round(100 * nc['high'] / len(N), 1)) # ---- the pairs with different river names, by class KEY = ['station_seq_id', 'pair_sword_reach_id'] D = pd.read_csv(os.path.join(DATD, 'Data22_pairs_different_river_names.csv'), usecols=KEY) C = pd.read_csv(os.path.join(PAPER, 'input', 'pairs_different_river_names_classified.csv'), usecols=KEY + ['name_class']) assert len(C) == len(D) and not C.duplicated(KEY).any() assert C.merge(D, on=KEY).shape[0] == len(D), 'the classified file holds the pairs of Data22' cls = C.name_class.value_counts() assert set(cls.index) == {'same_river_other_name', 'different_river', 'cannot_tell_from_names'} O['pairs_different_names'] = len(C) O['pairs_different_names_by_class'] = {k: int(cls[k]) for k in ('same_river_other_name', 'different_river', 'cannot_tell_from_names')} json.dump(O, open(os.path.join(DATD, 'Data23_sword_confidence_and_name_classes.json'), 'w'), indent=1) print(json.dumps(O, indent=1))