import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22%2C%20app_title%3D%22Stretch%20%C2%B7%20A%20new%20edition%20and%20Share%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20import%20difflib%0A%20%20%20%20import%20json%0A%20%20%20%20import%20sqlite3%0A%20%20%20%20import%20unicodedata%0A%20%20%20%20from%20contextlib%20import%20closing%0A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20pandas%20as%20pd%0A%0A%20%20%20%20import%20course%0A%20%20%20%20import%20explain%0A%20%20%20%20from%20course%20import%20NOT_YET%2C%20PATHS%2C%20attempt%2C%20check_cases%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Stretch%20%C2%B7%20A%20new%20edition%20and%20Share%0A%0A%20%20%20%20For%20when%20you%20have%20finished%20%6031_share_round.py%60%20and%20%6032_datasette.py%60.%20Less%20help%2C%20more%20to%20think%20about.%20Do%20them%20in%20any%20order.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20S3.1%20%C2%B7%20Complementary%20suppression%0A%0A%20%20%20%20%60suppress%60%20hides%20small%20cells%2C%20but%20a%20province's%20total%20is%20published%20too%3A%20when%20only%20**one**%20of%20its%20cells%20is%20hidden%2C%20the%20total%20minus%20the%20visible%20cells%20gives%20it%20back%20(the%20explainer%20in%20%6031_share_round.py%60%2C%20section%203%2C%20shows%20it).%20Write%20%60suppress_complementary(table%2C%20threshold)%60%3A%0A%0A%20%20%20%20-%20hide%20every%20cell%20with%20fewer%20respondents%20than%20the%20threshold%2C%20and%20mark%20it%20%60%22small%20cell%22%60%20in%20a%20column%20%60suppressed%60%3B%0A%20%20%20%20-%20in%20each%20province%20where%20exactly%20**one**%20cell%20is%20hidden%20that%20way%2C%20also%20hide%20the%20province's%20other%20cell%20with%20the%20**fewest**%20respondents%2C%20marked%20%60%22complementary%22%60%20(the%20first%20one%2C%20if%20two%20tie)%3B%0A%20%20%20%20-%20%60suppressed%60%20is%20empty%20text%20for%20a%20published%20cell%3B%20%60weighted_persons%60%20is%20empty%20for%20both%20kinds%20of%20hidden%20cell%3B%20%60respondents%60%20is%20dropped.%0A%0A%20%20%20%20The%20input%20has%20the%20columns%20%60province%60%2C%20%60age_group%60%2C%20%60respondents%60%20and%20%60weighted_persons%60%2C%20like%20%60explain.SUPPRESSION_TABLE%60.%20A%20province%20where%20every%20cell%20is%20small%20has%20nothing%20left%20to%20hide.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20suppress_complementary(table%3A%20pd.DataFrame%2C%20threshold%3A%20int)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Small%20cells%20hidden%2C%20and%20one%20more%20cell%20in%20each%20province%20where%20a%20single%20hidden%20cell%20could%20be%20worked%20out.%22%22%22%0A%20%20%20%20raise%20NotImplementedError(%22Stretch%20S3.1%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_table%20%3D%20explain.SUPPRESSION_TABLE%0A%20%20%20%20check_cases(%22Stretch%20S3.1%22%2C%20suppress_complementary%2C%20%5B(f%22the%20six%20cells%2C%20threshold%20%7Bt%7D%22%2C%20(_table%2C%20t))%20for%20t%20in%20(10%2C%2030%2C%20100)%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20what%3D%22thresholds%22%2C%20summarize%3Dlambda%20t%3A%20t.astype(object).where(t.notna()%2C%20None).values.tolist()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hint%3D%22At%2010%2C%20one%20cell%20of%200600300000%20is%20small%2C%20so%20a%20second%20is%20hidden%3B%20at%2030%2C%20two%20are%20small%2C%20which%20is%20safe%20already.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%0A%20%20%20%20course.try_it(suppress_complementary%2C%20explain.SUPPRESSION_TABLE%2C%20100)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20S3.2%20%C2%B7%20Parts%20that%20add%20up%20to%20the%20whole%0A%0A%20%20%20%20Round%20each%20part%20of%20a%20distribution%20on%20its%20own%2C%20and%20the%20parts%20may%20add%20up%20to%20999%20or%201%2C001%20per%20thousand%3A%20a%20reader%20who%20adds%20them%20finds%20the%20release%20%22wrong%22.%20The%20**largest%20remainder**%20method%20fixes%20it.%20Write%20%60per_thousand(values)%60%3A%20for%20a%20Series%20of%20non-negative%20numbers%2C%20each%20value's%20share%20per%201%2C000%2C%20as%20whole%20numbers%20that%20add%20up%20to%20exactly%201%2C000%3A%0A%0A%20%20%20%201.%20the%20exact%20shares%3A%20%60values%20%2F%20values.sum()%20*%201000%60%3B%0A%20%20%20%202.%20round%20each%20**down**%20(%60numpy.floor%60%2C%20or%20%60%2F%2F%201%60)%3B%0A%20%20%20%203.%20hand%20out%20what%20is%20missing%20to%201%2C000%2C%20one%20each%2C%20to%20the%20values%20with%20the%20**largest%20remainders**%20(exact%20minus%20rounded%20down)%3B%20on%20a%20tie%2C%20the%20earlier%20value%20first%3B%0A%20%20%20%204.%20keep%20the%20Series'%20index%2C%20and%20give%20back%20whole%20numbers%20(%60.astype(int)%60).%0A%0A%20%20%20%20Tip%3A%20%60remainders.sort_values(ascending%3DFalse%2C%20kind%3D%22stable%22)%60%20sorts%20largest%20first%20and%20keeps%20ties%20in%20their%20order.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20per_thousand(values%3A%20pd.Series)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Each%20value's%20share%20per%201%2C000%2C%20as%20whole%20numbers%20adding%20up%20to%20exactly%201%2C000%20(largest%20remainder%20method).%22%22%22%0A%20%20%20%20raise%20NotImplementedError(%22Stretch%20S3.2%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%22Stretch%20S3.2%22%2C%20per_thousand%2C%20%5B%0A%20%20%20%20%20%20%20%20(%22three%20equal%20parts%22%2C%20(pd.Series(%5B1.0%2C%201.0%2C%201.0%5D%2C%20index%3D%5B%22a%22%2C%20%22b%22%2C%20%22c%22%5D)%2C))%2C%0A%20%20%20%20%20%20%20%20(%22weighted%20persons%20by%20age%20group%22%2C%20(pd.Series(%5B45684.0%2C%2032504.0%2C%2063332.0%2C%2053591.0%2C%2018237.0%5D%2C%20index%3D%5B%220-14%22%2C%20%2215-24%22%2C%20%2225-44%22%2C%20%2245-64%22%2C%20%2265%2B%22%5D)%2C))%2C%0A%20%20%20%20%20%20%20%20(%22one%20part%20only%22%2C%20(pd.Series(%5B7.0%5D%2C%20index%3D%5B%22all%22%5D)%2C))%2C%0A%20%20%20%20%5D%2C%20what%3D%22distributions%22%2C%20summarize%3Dlambda%20s%3A%20s.to_dict()%2C%20hint%3D%22Three%20equal%20parts%20give%20334%2C%20333%20and%20333%3A%20the%20first%20one%20gets%20the%20spare%20unit.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20naive%20way%20first%2C%20then%20yours.%0A%20%20%20%20_parts%20%3D%20pd.Series(%5B45684.0%2C%2032504.0%2C%2063332.0%2C%2053591.0%2C%2018237.0%5D%2C%20index%3D%5B%220-14%22%2C%20%2215-24%22%2C%20%2225-44%22%2C%20%2245-64%22%2C%20%2265%2B%22%5D)%0A%20%20%20%20mo.vstack(%5Bmo.md(f%22Rounded%20one%20by%20one%2C%20the%20parts%20add%20up%20to%20**%7Bint((_parts%20%2F%20_parts.sum()%20*%201000).round().sum())%7D**.%22)%2C%20course.try_it(per_thousand%2C%20_parts)%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20S3.3%20%C2%B7%20The%20crosswalk%20the%20other%20way%2C%20by%20shares%0A%0A%20%20%20%20In%20%6030_new_edition.py%60%20you%20mapped%20Round%203%20back%20to%20the%20old%20geography%2C%20because%20new%20%E2%86%92%20old%20never%20copies%20anyone.%20Sometimes%20the%20question%20goes%20the%20other%20way%3A%20Round%201's%20totals%20in%20the%20**new**%20geography.%20A%20municipality%20that%20split%20has%20two%20successors%2C%20and%20the%20old%20data%20cannot%20say%20who%20lives%20in%20which%3B%20the%20usual%20answer%20is%20to%20**share%20out**%20its%20total%20by%20each%20successor's%20share%20in%20a%20round%20that%20has%20both%20(Round%203).%0A%0A%20%20%20%20Write%20%60allocate_to_2024q2(totals%2C%20crosswalk%2C%20shares)%60%3A%0A%0A%20%20%20%20-%20%60totals%60%3A%20%60area_code_2023q4%60%20and%20%60weighted_persons%60%2C%20one%20row%20per%20old%20code%3B%0A%20%20%20%20-%20%60crosswalk%60%3A%20%60code_2024q2%60%20and%20%60code_2023q4%60%2C%20as%20from%20your%20%60load_crosswalk%60%3B%0A%20%20%20%20-%20%60shares%60%3A%20%60code_2024q2%60%20and%20%60share%60%2C%20only%20for%20successors%20of%20a%20split%3A%20the%20shares%20of%20one%20old%20code's%20successors%20add%20up%20to%201%3B%0A%20%20%20%20-%20result%3A%20%60code_2024q2%60%20and%20%60weighted_persons%60%2C%20one%20row%20per%20new%20code%2C%20sorted%20by%20code.%20A%20code%20with%20no%20successor%20in%20the%20crosswalk%20keeps%20its%20own%20code%3B%20a%20successor%20with%20no%20share%20gets%20the%20whole%20total%20(share%201).%0A%0A%20%20%20%20Join%20the%20totals%20to%20the%20crosswalk%20(old%20%E2%86%92%20new%3A%20%60validate%3D%22one_to_many%22%60%2C%20since%20one%20old%20code%20may%20have%20two%20successors)%2C%20then%20to%20the%20shares%20(%60validate%3D%22many_to_one%22%60)%2C%20multiply%2C%20and%20add%20up%20by%20new%20code.%20**The%20grand%20total%20must%20not%20change**%3A%20check%20it%20yourself.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20allocate_to_2024q2(totals%3A%20pd.DataFrame%2C%20crosswalk%3A%20pd.DataFrame%2C%20shares%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Totals%20in%202023Q4%20geography%20shared%20out%20to%202024Q2%20codes.%22%22%22%0A%20%20%20%20raise%20NotImplementedError(%22Stretch%20S3.3%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_crosswalk%20%3D%20explain.CROSSWALK%5B%5B%22code_2024q2%22%2C%20%22code_2023q4%22%5D%5D%0A%20%20%20%20_totals%20%3D%20pd.DataFrame(%7B%22area_code_2023q4%22%3A%20%5B%220100102500%22%2C%20%220100304000%22%2C%20%220100105000%22%2C%20%220600303000%22%5D%2C%20%22weighted_persons%22%3A%20%5B1000.0%2C%202000.0%2C%20500.0%2C%20300.0%5D%7D)%0A%20%20%20%20_shares%20%3D%20pd.DataFrame(%7B%22code_2024q2%22%3A%20%5B%220100304000%22%2C%20%220100306000%22%5D%2C%20%22share%22%3A%20%5B0.4%2C%200.6%5D%7D)%0A%20%20%20%20check_cases(%22Stretch%20S3.3%22%2C%20allocate_to_2024q2%2C%20%5B(%22a%20changed%20code%2C%20a%20split%2C%20an%20unchanged%20code%2C%20and%20one%20not%20in%20the%20crosswalk%22%2C%20(_totals%2C%20_crosswalk%2C%20_shares))%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dlambda%20t%3A%20t%5B%5B%22code_2024q2%22%2C%20%22weighted_persons%22%5D%5D.values.tolist()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hint%3D%22The%20split's%202%2C000%20becomes%20800%20and%201%2C200%3B%200600303000%20keeps%20its%20own%20code%3B%20the%20grand%20total%20stays%203%2C800.%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20split_shares(crosswalk%3A%20pd.DataFrame%2C%20round_id%3A%20str%20%3D%20%222024Q2%22)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Each%20split%20successor's%20share%20of%20its%20old%20code's%20weighted%20persons%2C%20measured%20in%20a%20stored%20round%20with%20the%20new%20codes.%22%22%22%0A%20%20%20%20split%20%3D%20crosswalk%5Bcrosswalk.duplicated(%22code_2023q4%22%2C%20keep%3DFalse)%5D%0A%20%20%20%20with%20course.database(read_only%3DTrue)%20as%20con%3A%0A%20%20%20%20%20%20%20%20stored%20%3D%20con.execute(%22SELECT%20area_code%2C%20sum(weight)%20AS%20weighted_persons%20FROM%20persons%20WHERE%20round%20%3D%20%3F%20GROUP%20BY%20area_code%22%2C%20%5Bround_id%5D).df()%0A%20%20%20%20sizes%20%3D%20split.merge(stored%2C%20left_on%3D%22code_2024q2%22%2C%20right_on%3D%22area_code%22%2C%20how%3D%22left%22).fillna(%7B%22weighted_persons%22%3A%200.0%7D)%0A%20%20%20%20sizes%5B%22share%22%5D%20%3D%20sizes%5B%22weighted_persons%22%5D%20%2F%20sizes.groupby(%22code_2023q4%22)%5B%22weighted_persons%22%5D.transform(%22sum%22)%0A%20%20%20%20return%20sizes%5B%5B%22code_2024q2%22%2C%20%22share%22%5D%5D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20allocate_button%20%3D%20mo.ui.run_button(label%3D%22Round%201's%20totals%20in%20the%202024Q2%20geography%22)%0A%20%20%20%20allocate_button%0A%20%20%20%20return%20(allocate_button%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(allocate_button)%3A%0A%20%20%20%20mo.stop(not%20allocate_button.value)%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20import%20importlib%0A%20%20%20%20%20%20%20%20_crosswalk%20%3D%20importlib.import_module(%2230_new_edition%22).load_crosswalk(PATHS.reference%20%2F%20%22psgc_2024Q2.xlsx%22)%0A%20%20%20%20%20%20%20%20_shares%20%3D%20split_shares(_crosswalk)%0A%20%20%20%20%20%20%20%20with%20course.database(read_only%3DTrue)%20as%20_con%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_totals%20%3D%20_con.execute(%22SELECT%20area_code%20AS%20area_code_2023q4%2C%20sum(weight)%20AS%20weighted_persons%20FROM%20persons%20WHERE%20round%20%3D%20'2023Q4'%20AND%20area_code%20IS%20NOT%20NULL%20GROUP%20BY%20area_code%22).df()%0A%20%20%20%20%20%20%20%20_result%20%3D%20attempt(allocate_to_2024q2%2C%20_totals%2C%20_crosswalk%2C%20_shares)%0A%20%20%20%20%20%20%20%20_out%20%3D%20course.waiting_for(%22Stretch%20S3.3%22)%20if%20_result%20is%20NOT_YET%20else%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22The%20shares%2C%20from%20Round%203%3A%22)%2C%20_shares%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22Grand%20total%20before%3A%20**%7B_totals%5B'weighted_persons'%5D.sum()%3A%2C.0f%7D**%3B%20after%3A%20**%7B_result%5B'weighted_persons'%5D.sum()%3A%2C.0f%7D**.%20The%20successors%20of%20the%20split%3A%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20_result%5B_result%5B%22code_2024q2%22%5D.isin(_shares%5B%22code_2024q2%22%5D)%5D%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%20%20%20%20except%20NotImplementedError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20course.waiting_for(str(_error))%0A%20%20%20%20except%20Exception%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(f%22Needs%20Rounds%201%20and%203%20stored%2C%20and%20%60load_crosswalk%60%20from%20%6030_new_edition.py%60%3A%20%7Btype(_error).__name__%7D%3A%20%7B_error%7D%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20_out%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20S3.4%20%C2%B7%20A%20canned%20query%20with%20two%20parameters%0A%0A%20%20%20%20Write%20%60region_sex_sql()%60%3A%20the%20release's%20cells%20for%20one%20region%20**and**%20one%20sex%2C%20with%20the%20columns%20%60province%60%2C%20%60age_group%60%2C%20%60weighted_persons%60%20and%20%60suppressed%60%2C%20sorted%20by%20province%20then%20age%20group.%20Two%20parameters%2C%20%60%3Aregion%60%20and%20%60%3Asex%60%3A%20Datasette%20shows%20a%20box%20for%20each.%0A%0A%20%20%20%20Then%20add%20it%20to%20your%20portal%3A%20the%20button%20below%20adds%20it%20to%20%60datasette.json%60%20as%20a%20second%20canned%20query%2C%20beside%20%60region_summary%60%2C%20and%20adds%20%60suppressed%60%20to%20the%20table's%20facets.%20Restart%20Datasette%20to%20see%20it.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20region_sex_sql()%20-%3E%20str%3A%0A%20%20%20%20%22%22%22The%20release's%20cells%20for%20one%20region%20and%20one%20sex.%22%22%22%0A%20%20%20%20raise%20NotImplementedError(%22Stretch%20S3.4%22)%0A%0A%0A%40app.function%0Adef%20two_parameter_story(region%3A%20str%2C%20sex%3A%20str)%20-%3E%20list%3A%0A%20%20%20%20%22%22%22Run%20region_sex_sql%20on%20a%20small%20release.%20Used%20by%20the%20check.%22%22%22%0A%20%20%20%20data%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22province%22%3A%20%5B%220100100000%22%2C%20%220100100000%22%2C%20%220100100000%22%2C%20%220100200000%22%2C%20%220600300000%22%2C%20%220600300000%22%5D%2C%0A%20%20%20%20%20%20%20%20%22sex%22%3A%20%5B%22Female%22%2C%20%22Male%22%2C%20%22Female%22%2C%20%22Female%22%2C%20%22Female%22%2C%20%22Male%22%5D%2C%0A%20%20%20%20%20%20%20%20%22age_group%22%3A%20%5B%2265%2B%22%2C%20%220-14%22%2C%20%220-14%22%2C%20%220-14%22%2C%20%220-14%22%2C%20%2265%2B%22%5D%2C%0A%20%20%20%20%20%20%20%20%22weighted_persons%22%3A%20%5B300.0%2C%201150.0%2C%201200.0%2C%20800.0%2C%20None%2C%20410.0%5D%2C%0A%20%20%20%20%20%20%20%20%22suppressed%22%3A%20%5B0%2C%200%2C%200%2C%200%2C%201%2C%200%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20with%20closing(sqlite3.connect(%22%3Amemory%3A%22))%20as%20con%3A%0A%20%20%20%20%20%20%20%20data.to_sql(%22release_table%22%2C%20con%2C%20index%3DFalse)%0A%20%20%20%20%20%20%20%20cursor%20%3D%20con.execute(region_sex_sql()%2C%20%7B%22region%22%3A%20region%2C%20%22sex%22%3A%20sex%7D)%0A%20%20%20%20%20%20%20%20return%20%5B%5Bd%5B0%5D%20for%20d%20in%20cursor.description%5D%5D%20%2B%20%5Blist(row)%20for%20row%20in%20cursor.fetchall()%5D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%22Stretch%20S3.4%22%2C%20two_parameter_story%2C%20%5B(%22region%2001%2C%20Female%22%2C%20(%2201%22%2C%20%22Female%22))%2C%20(%22region%2006%2C%20Male%22%2C%20(%2206%22%2C%20%22Male%22))%2C%20(%22region%2006%2C%20Female%22%2C%20(%2206%22%2C%20%22Female%22))%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20what%3D%22questions%22%2C%20hint%3D%22Region%2001%2C%20Female%3A%20three%20cells%2C%20sorted%20by%20province%20then%20age%20group.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20portal_round%20%3D%20mo.ui.dropdown(options%3Dsorted(p.parent.name%20for%20p%20in%20PATHS.release.glob(%22*%2Fdatasette.json%22))%20or%20%5B%22none%20yet%22%5D%2C%20label%3D%22Release%20with%20portal%20files%3A%22)%0A%20%20%20%20add_button%20%3D%20mo.ui.run_button(label%3D%22Add%20the%20query%20to%20datasette.json%22)%0A%20%20%20%20mo.hstack(%5Bportal_round%2C%20add_button%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%20add_button%2C%20portal_round%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(add_button%2C%20portal_round)%3A%0A%20%20%20%20mo.stop(not%20add_button.value)%0A%20%20%20%20_path%20%3D%20PATHS.release%20%2F%20portal_round.value%20%2F%20%22datasette.json%22%0A%20%20%20%20_sql%20%3D%20attempt(region_sex_sql)%0A%20%20%20%20if%20_sql%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20course.waiting_for(%22Stretch%20S3.4%22)%0A%20%20%20%20elif%20not%20_path.exists()%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(%22Write%20the%20portal%20files%20first%3A%20%6032_datasette.py%60%2C%20section%204.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20_config%20%3D%20json.loads(_path.read_text(encoding%3D%22utf-8%22))%0A%20%20%20%20%20%20%20%20_db%20%3D%20_config%5B%22databases%22%5D%5B%22release%22%5D%0A%20%20%20%20%20%20%20%20_db%5B%22queries%22%5D%5B%22region_sex%22%5D%20%3D%20%7B%22title%22%3A%20%22Cells%20for%20one%20region%20and%20one%20sex%22%2C%20%22description%22%3A%20%22Enter%20a%20two-digit%20region%20code%2C%20like%2006%2C%20and%20Female%20or%20Male.%22%2C%20%22sql%22%3A%20_sql.strip()%7D%0A%20%20%20%20%20%20%20%20_facets%20%3D%20_db%5B%22tables%22%5D%5B%22release_table%22%5D%5B%22facets%22%5D%0A%20%20%20%20%20%20%20%20_facets%20%2B%3D%20%5B%5D%20if%20%22suppressed%22%20in%20_facets%20else%20%5B%22suppressed%22%5D%0A%20%20%20%20%20%20%20%20_path.write_text(json.dumps(_config%2C%20indent%3D2%2C%20ensure_ascii%3DFalse)%20%2B%20%22%5Cn%22%2C%20encoding%3D%22utf-8%22)%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(f%22Added%20to%20%60%7B_path.relative_to(PATHS.root).as_posix()%7D%60.%20Stop%20Datasette%20(Ctrl%20%2B%20C)%20and%20start%20it%20again.%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20_out%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20S3.5%20%C2%B7%20Matching%20names%20to%20codes%0A%0A%20%20%20%20Another%20agency%20sends%20a%20list%20of%20local%20governments%20by%20**name**%3A%20%60reference%2Flgu_names_other_agency.csv%60%2C%20with%20the%20province's%20name%20and%20a%20count%20of%20facilities.%20To%20join%20it%20to%20anything%2C%20each%20name%20needs%20its%20code.%20Names%20are%20written%20differently%20(%60Penablanca%60%2C%20%60Sta.%20Cruz%60%2C%20%60TALAVERA%60)%2C%20one%20has%20a%20typo%2C%20two%20are%20not%20municipalities%20at%20all%2C%20and%20the%20same%20municipality%20name%20exists%20in%20several%20provinces%3A%20the%20province%20is%20part%20of%20the%20match.%0A%0A%20%20%20%20Write%20%60match_names(lgus%2C%20places)%60%3A%0A%0A%20%20%20%20-%20%60lgus%60%3A%20%60lgu_name%60%20and%20%60province%60%2C%20as%20in%20the%20file%3B%0A%20%20%20%20-%20%60places%60%3A%20%60code%60%2C%20%60name%60%20and%20%60province%60%20for%20every%20city%20and%20municipality%20(from%20%60places()%60%20below)%3B%0A%20%20%20%20-%20result%3A%20the%20rows%20of%20%60lgus%60%20with%20three%20more%20columns%3A%20%60code%60%2C%20%60matched_name%60%20(the%20code%20list's%20name)%2C%20and%20%60match%60%3A%20%60%22exact%22%60%20when%20the%20two%20names%20are%20the%20same%20once%20**normalised**%2C%20%60%22close%22%60%20when%20%60difflib.get_close_matches(name%2C%20candidates%2C%20n%3D1%2C%20cutoff%3D0.8)%60%20finds%20one%20among%20the%20same%20province's%20normalised%20names%2C%20or%20%60%22none%22%60%20(and%20empty%20code%20and%20name).%0A%0A%20%20%20%20**Normalise**%20a%20name%3A%20lower%20case%3B%20accents%20removed%20(%60unicodedata.normalize(%22NFKD%22%2C%20text)%60%2C%20then%20drop%20the%20characters%20for%20which%20%60unicodedata.combining(c)%60%20is%20true)%3B%20%60sta.%60%20written%20%60santa%60%2C%20a%20leading%20%60s.%60%20written%20%60san%60%3B%20spaces%20collapsed.%0A%0A%20%20%20%20A%20close%20match%20is%20a%20**guess**%3A%20a%20report%20should%20show%20it%20for%20a%20person%20to%20confirm%2C%20never%20join%20on%20it%20silently.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20places(path%3DNone)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Every%20city%20and%20municipality%20of%20a%20code%20list%20edition%2C%20with%20its%20province's%20name.%22%22%22%0A%20%20%20%20table%20%3D%20pd.read_excel(path%20or%20PATHS.reference%20%2F%20%22psgc_2024Q2.xlsx%22%2C%20header%3D3%2C%20dtype%3Dstr)%0A%20%20%20%20table%20%3D%20table%5Btable%5B%22Code%22%5D.str.fullmatch(r%22%5Cd%2B%22).fillna(False)%5D.assign(code%3Dlambda%20t%3A%20t%5B%22Code%22%5D.str.zfill(10)%2C%20parent%3Dlambda%20t%3A%20t%5B%22Parent%20Code%22%5D.str.zfill(10))%0A%20%20%20%20province%20%3D%20table.loc%5Btable%5B%22Geographic%20Level%22%5D%20%3D%3D%20%22Province%22%5D.set_index(%22code%22)%5B%22Name%22%5D%0A%20%20%20%20lgus%20%3D%20table%5Btable%5B%22Geographic%20Level%22%5D.isin(%5B%22City%22%2C%20%22Municipality%22%5D)%5D%0A%20%20%20%20return%20pd.DataFrame(%7B%22code%22%3A%20lgus%5B%22code%22%5D%2C%20%22name%22%3A%20lgus%5B%22Name%22%5D%2C%20%22province%22%3A%20lgus%5B%22parent%22%5D.map(province)%7D).reset_index(drop%3DTrue)%0A%0A%0A%40app.function%0Adef%20match_names(lgus%3A%20pd.DataFrame%2C%20places%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Each%20name%20matched%20to%20a%20code%20within%20its%20province%3A%20exact%20once%20normalised%2C%20close%2C%20or%20none.%22%22%22%0A%20%20%20%20raise%20NotImplementedError(%22Stretch%20S3.5%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_places%20%3D%20pd.DataFrame(%7B%22code%22%3A%20%5B%220100102000%22%2C%20%220100103000%22%2C%20%220100105000%22%2C%20%220600103000%22%2C%20%220100201000%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%5B%22Santa%20Cruz%22%2C%20%22San%20Jose%22%2C%20%22Pe%C3%B1ablanca%22%2C%20%22San%20Jose%22%2C%20%22Cabiao%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22province%22%3A%20%5B%22Bulusan%22%2C%20%22Bulusan%22%2C%20%22Bulusan%22%2C%20%22Tagbak%22%2C%20%22Malinao%22%5D%7D)%0A%20%20%20%20_lgus%20%3D%20pd.DataFrame(%7B%22lgu_name%22%3A%20%5B%22Sta.%20Cruz%22%2C%20%22SAN%20JOSE%22%2C%20%22Penablanca%22%2C%20%22San%20%20Jose%22%2C%20%22Cabiaa%22%2C%20%22Barangay%20Poblacion%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22province%22%3A%20%5B%22Bulusan%22%2C%20%22Tagbak%22%2C%20%22Bulusan%22%2C%20%22Bulusan%22%2C%20%22Malinao%22%2C%20%22Bulusan%22%5D%7D)%0A%20%20%20%20check_cases(%22Stretch%20S3.5%22%2C%20match_names%2C%20%5B(%22six%20names%2C%20one%20province%20each%22%2C%20(_lgus%2C%20_places))%5D%2C%20what%3D%22lists%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20summarize%3Dlambda%20t%3A%20t%5B%5B%22lgu_name%22%2C%20%22province%22%2C%20%22code%22%2C%20%22matched_name%22%2C%20%22match%22%5D%5D.astype(object).where(t%5B%5B%22lgu_name%22%2C%20%22province%22%2C%20%22code%22%2C%20%22matched_name%22%2C%20%22match%22%5D%5D.notna()%2C%20None).values.tolist()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hint%3D%22The%20two%20San%20Jose%20rows%20get%20two%20different%20codes%3A%20one%20per%20province.%20Cabiaa%20is%20close%20to%20Cabiao.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20other%20agency's%20file%2C%20against%20the%202024Q2%20code%20list.%0A%20%20%20%20course.try_it(match_names%2C%20pd.read_csv(PATHS.reference%20%2F%20%22lgu_names_other_agency.csv%22)%2C%20places())%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
ea4b3b12a5f1e0c07c470ddebbeeb223