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%22Module%207%3A%20Publish%20the%20tables%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20the%20settings%20every%20other%20cell%20uses.%0A%20%20%20%20import%20ast%0A%20%20%20%20import%20datetime%0A%20%20%20%20import%20subprocess%0A%20%20%20%20import%20sys%0A%20%20%20%20import%20textwrap%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20duckdb%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20openpyxl%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20polars%20as%20pl%0A%20%20%20%20import%20svy%0A%20%20%20%20from%20openpyxl.styles%20import%20Font%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20file%20locations%2C%20the%20survey's%20tables%20and%20the%20exercise%20checks%0A%20%20%20%20from%20course%20import%20NOT_YET%2C%20PATHS%2C%20attempt%2C%20check_cases%2C%20try_it%2C%20waiting_for%0A%0A%20%20%20%20EXPECTED_REGIONS%20%3D%20%5B%2201%22%2C%20%2204%22%2C%20%2207%22%5D%20%20%23%20the%20survey's%20regions%0A%20%20%20%20LEVEL_SHEETS%20%3D%20%7B%22national%22%3A%20%22National%22%2C%20%22regional%22%3A%20%22Regional%22%2C%20%22provincial%22%3A%20%22Provincial%22%7D%20%20%23%20each%20level%20and%20its%20sheet%20title%20in%20section%201's%20workbook%0A%20%20%20%20LEVEL_CODES%20%3D%20%7B%22national%22%3A%20%5B%5D%2C%20%22regional%22%3A%20%5B%22region_code%22%5D%2C%20%22provincial%22%3A%20%5B%22region_code%22%2C%20%22province_code%22%5D%7D%20%20%23%20each%20level's%20code%20columns%0A%20%20%20%20%23%20What%20the%20tables%20estimate%3A%20the%20name%20a%20reader%20sees%2C%20then%20(the%20frame's%20column%2C%20the%20statistic%2C%20its%20Excel%20number%20format)%0A%20%20%20%20INDICATORS%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22fishing%20households%22%3A%20(%22hh_count%22%2C%20%22total%22%2C%20%22%23%2C%23%230%22)%2C%0A%20%20%20%20%20%20%20%20%22household%20members%22%3A%20(%22hh_size%22%2C%20%22total%22%2C%20%22%23%2C%23%230%22)%2C%0A%20%20%20%20%20%20%20%20%22mean%20household%20size%22%3A%20(%22hh_size%22%2C%20%22mean%22%2C%20%220.00%22)%2C%0A%20%20%20%20%20%20%20%20%22households%20running%20a%20large-scale%20unit%22%3A%20(%22runs_large_unit%22%2C%20%22total%22%2C%20%22%23%2C%23%230%22)%2C%0A%20%20%20%20%7D%0A%20%20%20%20NUMBER_FORMATS%20%3D%20%7B%22cv%22%3A%20%220.0%25%22%2C%20%22n%22%3A%20%22%23%2C%23%230%22%7D%20%20%23%20the%20other%20numeric%20columns'%20Excel%20number%20formats%0A%20%20%20%20FIGURES%20%3D%20%5B%22estimate%22%2C%20%22se%22%2C%20%22cv%22%2C%20%22lci%22%2C%20%22uci%22%2C%20%22unweighted%22%2C%20%22n%22%5D%20%20%23%20what%20estimate()%20gives%20for%20each%20domain%2C%20in%20order%0A%20%20%20%20SCRATCH%20%3D%20PATHS.root%20%2F%20%22scratch%22%20%2F%20%22publish%22%20%20%23%20everything%20this%20module%20writes%20goes%20here%2C%20never%20into%20data%2F%0A%20%20%20%20%23%20The%20tabulation%20script%20(section%202)%3A%20its%20tables%2C%20each%20with%20its%20sheet's%20name%2C%20the%20indicator%20it%20estimates%20and%20the%0A%20%20%20%20%23%20variables%20it%20is%20cut%20by%20besides%20the%20level's%20codes%3B%20and%20where%20it%20reads%20the%20flat%20table%20and%20writes%20the%20workbook%2C%20from%0A%20%20%20%20%23%20the%20course%20folder%0A%20%20%20%20TABLES%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22fishing_households%22%3A%20(%22fishing%20households%22%2C%20%5B%5D)%2C%0A%20%20%20%20%20%20%20%20%22household_members%22%3A%20(%22household%20members%22%2C%20%5B%5D)%2C%0A%20%20%20%20%20%20%20%20%22mean_household_size%22%3A%20(%22mean%20household%20size%22%2C%20%5B%5D)%2C%0A%20%20%20%20%20%20%20%20%22households_by_scale%22%3A%20(%22fishing%20households%22%2C%20%5B%22scale%22%5D)%2C%0A%20%20%20%20%7D%0A%20%20%20%20FLAT_TABLE%20%3D%20%22scratch%2Fpublish%2Fhouseholds.parquet%22%0A%20%20%20%20TABLES_FILE%20%3D%20%22scratch%2Fpublish%2Ftables.xlsx%22%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%20Module%207%3A%20Publish%20the%20tables%0A%0A%20%20%20%20**Where%20this%20is%20in%20processing%20a%20survey.**%20After%20the%20estimates%20come%20the%20outputs%3A%20draft%20tables%20are%20prepared%20from%20the%20estimates%20and%20checked%2C%20and%20then%20produced%20in%20the%20form%20the%20survey%20publishes%20them.%20This%20module%20does%20both%20for%20the%20Survey%20of%20Fishing%20Households%202025%3A%20it%20turns%20module%206's%20estimates%20into%20a%20workbook%20a%20reader%20can%20open%20in%20Excel%2C%20and%20then%20into%20a%20script%20that%20makes%20the%20same%20tables%20again%20from%20the%20flat%20table%2C%20whoever%20runs%20it.%0A%0A%20%20%20%20Module%206%20estimated%20totals%20and%20means%20under%20the%20survey's%20design%2C%20with%20a%20standard%20error%20(SE)%20and%20a%20coefficient%20of%20variation%20(CV)%20for%20each.%20Those%20numbers%20now%20sit%20in%20a%20notebook.%20A%20survey%20publishes%20them%20as%20workbooks%2C%20made%20by%20a%20program%20that%20anyone%20on%20the%20team%20can%20run%20again%20on%20the%20next%20batch%20of%20data.%0A%0A%20%20%20%20%7C%20section%20%7C%20what%20you%20do%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%20%C2%B7%20Tables%20to%20a%20workbook%20%7C%20national%2C%20regional%20and%20provincial%20tables%20in%20one%20workbook%3A%20a%20sheet%20per%20level%2C%20number%20formats%20and%20a%20notes%20sheet%20%7C%0A%20%20%20%20%7C%202%20%C2%B7%20The%20tabulation%20as%20one%20script%20%7C%20from%20the%20flat%20table%20to%20one%20workbook%2C%20a%20sheet%20per%20table%20with%20its%20three%20levels%20stacked%20and%20the%20sample's%20own%20figures%20beside%20each%20estimate%2C%20in%20one%20script%20that%20runs%20again%20on%20the%20next%20batch%20%7C%0A%0A%20%20%20%20A%20script%20with%20a%20contract%20(a%20refusal%20of%20a%20missing%20region%2C%20exit%20codes%2C%20a%20manifest%20of%20its%20files)%20is%20in%20the%20stretch%20S2%2C%20%5Btabulation%5D(%2F%3Ffile%3D61_stretch_tabulation.py).%0A%0A%20%20%20%20**The%20data.**%20Every%20cell%20here%20starts%20from%20the%20survey%20as%20modules%202%20to%206%20leave%20it%3A%20one%20row%20per%20household%2C%20removed%20households%20and%20voided%20rows%20left%20out%2C%20codes%20as%20zero-padded%20text%2C%20and%20the%20design%20columns%20svy%20needs.%20The%20function%20%60read_survey%60%20below%20reads%20it%20straight%20from%20the%20regional%20databases%2C%20so%20that%20a%20script%20can%20do%20the%20same%20without%20this%20notebook.%0A%0A%20%20%20%20Exercises%20work%20as%20in%20the%20earlier%20modules%3A%20write%20the%20function%2C%20delete%20the%20%60raise%60%20line%2C%20use%20the%20**Try%20it**%20cell%2C%20read%20the%20check.%20If%20something%20goes%20red%2C%20read%20the%20error's%20**last%20line**%20first%3A%20it%20names%20what%20went%20wrong.%0A%0A%20%20%20%20**Where%20this%20module%20writes.**%20Every%20file%20it%20makes%20goes%20into%20%60scratch%2Fpublish%2F%60%20in%20the%20course%20folder.%20Nothing%20is%20written%20into%20%60data%2F%60%2C%20which%20holds%20the%20survey%20as%20it%20arrived.%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%20course.quizzes_at(%227.0%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%201%20%C2%B7%20Tables%20to%20a%20workbook%3A%20sheets%2C%20number%20formats%2C%20a%20notes%20sheet%0A%0A%20%20%20%20**The%20problem.**%20Module%206's%20estimates%20are%20floating-point%20numbers%20in%20a%20DataFrame%3A%20a%20count%20of%20households%20with%20four%20decimals%2C%20a%20CV%20written%20as%20a%20fraction.%20A%20reader%20of%20a%20published%20table%20expects%20%603%2C812%60%20and%20%604.0%25%60%2C%20one%20sheet%20per%20geographic%20level%2C%20and%20a%20note%20that%20says%20where%20the%20numbers%20come%20from%20and%20when%20they%20were%20made.%20A%20plain%20export%20gives%20none%20of%20that.%20In%20R%2C%20writexl's%20%60write_xlsx(list(National%20%3D%20national%2C%20Regional%20%3D%20regional)%2C%20%22tables.xlsx%22)%60%20writes%20one%20sheet%20per%20data%20frame%20of%20the%20list%2C%20with%20a%20bold%20header%20and%20no%20other%20cell%20format%20unless%20each%20column%20is%20wrapped%20in%20one%20of%20its%20formatting%20helpers%20(writexl%202.0.1%2C%20its%20reference%20manual%20on%20CRAN%2C%20checked%202026-11-20)%3B%20formatting%20done%20by%20hand%20in%20Excel%20afterwards%20is%20lost%20the%20next%20time%20the%20tables%20are%20made.%0A%0A%20%20%20%20**The%20tool.**%20openpyxl%20writes%20a%20workbook%20cell%20by%20cell%2C%20and%20each%20cell%20can%20carry%20an%20Excel%20**number%20format**%3A%20the%20pattern%20Excel%20uses%20to%20show%20the%20value%2C%20without%20changing%20the%20value%20itself.%20The%20cell%20keeps%20%603812.4467%60%3B%20the%20format%20%60%23%2C%23%230%60%20shows%20it%20as%20%603%2C812%60%2C%20and%20a%20reader%20who%20uses%20the%20cell%20in%20a%20formula%20still%20gets%20the%20full%20value.%0A%0A%20%20%20%20First%2C%20the%20data.%20The%20survey%20is%20the%20three%20regional%20databases%2C%20read%20with%20DuckDB%20as%20module%202%20did%2C%20cleaned%20as%20module%202%20did%2C%20and%20given%20its%20strata%20and%20sampling%20units%20as%20module%206%20did.%20Three%20more%20steps%20decide%20what%20the%20rows%20hold.%20A%20household's%20members%20are%20counted%20from%20its%20rows%20in%20%60member%60%2C%20not%20read%20from%20the%20cover's%20count%20%60hh_roster_members%60%3A%20module%204's%20rule%20H1%20compares%20the%20two%2C%20and%20they%20do%20not%20always%20agree.%20Then%20module%204's%20two%20design%20findings%20are%20applied%2C%20as%20module%206%20applied%20them.%20Three%20households%20turned%20out%20to%20be%20copies%20of%20another%20household's%20interview%2C%20each%20filed%20as%20a%20household%20of%20its%20own%20in%20its%20province's%20stratum%20F%2C%20where%20every%20unit%20is%20taken%3B%20each%20counts%20one%20household%20twice%2C%20so%20they%20are%20left%20out.%20And%20three%20households%20were%20each%20filed%20under%20another%20stratum%20than%20their%20area's%20other%20households%2C%20so%20each%20goes%20back%20to%20its%20area's%20stratum.%20Those%20decisions%20arrive%20here%20as%20a%20file%2C%20%60design_decisions.csv%60%3A%20a%20household%2C%20a%20decision%20(%60leave%20out%60%20or%20%60area's%20stratum%60)%20and%20a%20reason.%20This%20notebook%20writes%20the%20file%20from%20the%20course's%20list%20of%20planted%20defects%2C%20so%20this%20module%20does%20not%20depend%20on%20your%20work%20in%20an%20earlier%20one.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20read_survey(data%3A%20Path%2C%20regions%3A%20list%5Bstr%5D%2C%20decisions%3A%20pd.DataFrame%20%7C%20None%20%3D%20None)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20household%20of%20these%20regions%2C%20ready%20for%20svy%3A%20the%20households%20removed%20in%20the%20capture%20system%2C%20in%20editing%0A%20%20%20%20or%20by%20the%20sampling%20side%20left%20out%3B%20the%20design%20decisions%20applied%20(%60decisions%60%3A%20region_code%2C%20hh_id%20and%20decision%2C%20which%0A%20%20%20%20is%20%22leave%20out%22%20or%20%22area's%20stratum%22%3A%20the%20household%20goes%20back%20to%20the%20stratum%20of%20its%20area's%20other%20households)%3B%20area%0A%20%20%20%20codes%20as%20zero-padded%20text%3B%20%60stratum_id%60%2C%20%60psu_id%60%20and%20the%20stratum's%20count%20of%20sampling%20units%20%60psus_in_stratum%60%3B%20and%0A%20%20%20%20the%20columns%20the%20tables%20estimate%20from.%22%22%22%0A%20%20%20%20con%20%3D%20duckdb.connect()%0A%20%20%20%20queries%20%3D%20%5B%5D%0A%20%20%20%20for%20region%20in%20regions%3A%0A%20%20%20%20%20%20%20%20db%20%3D%20f%22sfh2025_r%7Bregion%7D%22%0A%20%20%20%20%20%20%20%20con.execute(f%22ATTACH%20'%7B(Path(data)%20%2F%20f'%7Bdb%7D.duckdb').as_posix()%7D'%20AS%20%7Bdb%7D%20(READ_ONLY)%22)%0A%20%20%20%20%20%20%20%20%23%20One%20household%20per%20row%3A%20its%20codes%20as%20text%2C%20its%20stratum%2C%20area%20and%20final%20weight%2C%20its%20members%20counted%20from%20its%0A%20%20%20%20%20%20%20%20%23%20rows%20in%20member%20(not%20the%20cover's%20count)%2C%20and%20whether%20it%20runs%20a%20large-scale%20unit%20(unit_scale%20%3D%202)%3B%20households%0A%20%20%20%20%20%20%20%20%23%20and%20rows%20removed%20in%20the%20capture%20system%2C%20in%20editing%20or%20by%20the%20sampling%20side%20left%20out%0A%20%20%20%20%20%20%20%20queries.append(f%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20SELECT%20lpad(CAST(h.region_code%20AS%20VARCHAR)%2C%202%2C%20'0')%20AS%20region_code%2C%20h.hh_id%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20lpad(CAST(h.province_code%20AS%20VARCHAR)%2C%203%2C%20'0')%20AS%20province_code%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20s.sel_stratum%2C%20s.sel_ea%2C%20s.sel_final_weight%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(SELECT%20count(*)%20FROM%20%7Bdb%7D.member%20AS%20m%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20WHERE%20m.hh_id%20%3D%20h.hh_id%20AND%20m.row_void%20IS%20DISTINCT%20FROM%201)%20AS%20hh_size%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20EXISTS%20(SELECT%201%20FROM%20%7Bdb%7D.fishing_unit%20AS%20u%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%20WHERE%20u.hh_id%20%3D%20h.hh_id%20AND%20u.unit_scale%20%3D%202%20AND%20u.row_void%20IS%20DISTINCT%20FROM%201)%20AS%20runs_large_unit%0A%20%20%20%20%20%20%20%20%20%20%20%20FROM%20%7Bdb%7D.household%20AS%20h%0A%20%20%20%20%20%20%20%20%20%20%20%20JOIN%20%7Bdb%7D.selection%20AS%20s%20ON%20s.hh_id%20%3D%20h.hh_id%0A%20%20%20%20%20%20%20%20%20%20%20%20WHERE%20h.hh_deleted_on%20IS%20NULL%20AND%20h.row_void%20IS%20DISTINCT%20FROM%201%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20AND%20s.sel_excluded%20IS%20DISTINCT%20FROM%201%20AND%20s.row_void%20IS%20DISTINCT%20FROM%201%22%22%22)%0A%20%20%20%20households%20%3D%20con.execute(%22%20UNION%20ALL%20%22.join(queries)%20%2B%20%22%20ORDER%20BY%20region_code%2C%20hh_id%22).df()%0A%20%20%20%20con.close()%0A%20%20%20%20if%20decisions%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%23%20The%20households%20left%20out%3A%20an%20anti-join%20on%20the%20household's%20key%0A%20%20%20%20%20%20%20%20left_out%20%3D%20decisions.loc%5Bdecisions%5B%22decision%22%5D%20%3D%3D%20%22leave%20out%22%2C%20%5B%22region_code%22%2C%20%22hh_id%22%5D%5D%0A%20%20%20%20%20%20%20%20marked%20%3D%20households.merge(left_out%2C%20on%3D%5B%22region_code%22%2C%20%22hh_id%22%5D%2C%20how%3D%22left%22%2C%20indicator%3DTrue)%0A%20%20%20%20%20%20%20%20households%20%3D%20marked%5Bmarked%5B%22_merge%22%5D%20%3D%3D%20%22left_only%22%5D.drop(columns%3D%22_merge%22).reset_index(drop%3DTrue)%0A%20%20%20%20%20%20%20%20%23%20The%20households%20filed%20under%20another%20stratum%20than%20their%20area's%3A%20they%20take%20their%20area's%20stratum%20back%0A%20%20%20%20%20%20%20%20moved%20%3D%20households.set_index(%5B%22region_code%22%2C%20%22hh_id%22%5D).index.isin(%0A%20%20%20%20%20%20%20%20%20%20%20%20decisions.loc%5Bdecisions%5B%22decision%22%5D%20%3D%3D%20%22area's%20stratum%22%5D.set_index(%5B%22region_code%22%2C%20%22hh_id%22%5D).index)%0A%20%20%20%20%20%20%20%20area%20%3D%20%5B%22region_code%22%2C%20%22province_code%22%2C%20%22sel_ea%22%5D%0A%20%20%20%20%20%20%20%20others%20%3D%20households.loc%5B~moved%2C%20%5B*area%2C%20%22sel_stratum%22%5D%5D.dropna(subset%3D%5B%22sel_ea%22%5D).drop_duplicates(area)%0A%20%20%20%20%20%20%20%20households.loc%5Bmoved%2C%20%22sel_stratum%22%5D%20%3D%20households.loc%5Bmoved%2C%20area%5D.merge(others%2C%20on%3Darea%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%5B%22sel_stratum%22%5D.to_numpy()%0A%20%20%20%20counts%20%3D%20pd.read_csv(Path(data)%20%2F%20%22stratum_counts.csv%22%2C%20dtype%3D%7B%22region_code%22%3A%20str%2C%20%22province_code%22%3A%20str%7D)%0A%20%20%20%20counts%5B%22region_code%22%5D%20%3D%20counts%5B%22region_code%22%5D.str.zfill(2)%0A%20%20%20%20counts%5B%22province_code%22%5D%20%3D%20counts%5B%22province_code%22%5D.str.zfill(3)%0A%20%20%20%20counts%20%3D%20counts.rename(columns%3D%7B%22eas_in_frame%22%3A%20%22psus_in_stratum%22%7D)%0A%20%20%20%20households%20%3D%20households.merge(counts%5B%5B%22region_code%22%2C%20%22province_code%22%2C%20%22sel_stratum%22%2C%20%22psus_in_stratum%22%5D%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%20%20%20%20%20%20on%3D%5B%22region_code%22%2C%20%22province_code%22%2C%20%22sel_stratum%22%5D%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20households%5B%22stratum_id%22%5D%20%3D%20households%5B%22region_code%22%5D%20%2B%20%22-%22%20%2B%20households%5B%22province_code%22%5D%20%2B%20%22-%22%20%2B%20households%5B%22sel_stratum%22%5D%0A%20%20%20%20%23%20The%20sampling%20unit%20is%20the%20enumeration%20area%3B%20in%20stratum%20F%2C%20where%20every%20registered%20large%20unit%20is%20taken%2C%20each%0A%20%20%20%20%23%20household%20is%20its%20own%20sampling%20unit%0A%20%20%20%20area%20%3D%20households%5B%22stratum_id%22%5D%20%2B%20%22-%22%20%2B%20households%5B%22sel_ea%22%5D.astype(%22string%22)%0A%20%20%20%20households%5B%22psu_id%22%5D%20%3D%20area.where(households%5B%22sel_stratum%22%5D%20!%3D%20%22F%22%2C%20households%5B%22region_code%22%5D%20%2B%20%22-hh-%22%20%2B%20households%5B%22hh_id%22%5D.astype(str))%0A%20%20%20%20households%5B%22hh_count%22%5D%20%3D%201%20%20%23%20a%20total%20of%20this%20column%20is%20a%20count%20of%20households%0A%20%20%20%20households%5B%22runs_large_unit%22%5D%20%3D%20households%5B%22runs_large_unit%22%5D.astype(int)%0A%20%20%20%20return%20households%0A%0A%0A%40app.function%0Adef%20read_decisions(path%3A%20Path)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20design%20decisions%2C%20from%20a%20CSV%20with%20the%20columns%20region_code%2C%20hh_id%20and%20decision%20(a%20reason%20column%20may%20follow).%22%22%22%0A%20%20%20%20decisions%20%3D%20pd.read_csv(path%2C%20dtype%3D%7B%22region_code%22%3A%20str%7D)%0A%20%20%20%20decisions%5B%22region_code%22%5D%20%3D%20decisions%5B%22region_code%22%5D.str.zfill(2)%0A%20%20%20%20return%20decisions%0A%0A%0A%40app.function%0Adef%20design_decisions(planted%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Module%204's%20two%20design%20findings%2C%20as%20module%206%20applies%20them%2C%20written%20as%20decisions%20from%20the%20course's%20list%20of%20planted%0A%20%20%20%20defects%3A%20each%20copied%20household%20is%20left%20out%2C%20and%20each%20household%20filed%20under%20another%20stratum%20than%20its%20area's%20goes%20back%0A%20%20%20%20to%20its%20area's%20stratum.%22%22%22%0A%20%20%20%20kinds%20%3D%20%7B%22household_copied%22%3A%20(%22leave%20out%22%2C%20%22a%20copy%20of%20another%20household's%20interview%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%22area_in_two_strata%22%3A%20(%22area's%20stratum%22%2C%20%22filed%20under%20another%20stratum%20than%20its%20area's%22)%7D%0A%20%20%20%20found%20%3D%20planted%5Bplanted%5B%22kind%22%5D.isin(list(kinds))%5D%0A%20%20%20%20return%20found.assign(decision%3Dfound%5B%22kind%22%5D.map(lambda%20kind%3A%20kinds%5Bkind%5D%5B0%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%20reason%3Dfound%5B%22kind%22%5D.map(lambda%20kind%3A%20kinds%5Bkind%5D%5B1%5D))%5B%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22decision%22%2C%20%22reason%22%5D%5D.reset_index(drop%3DTrue)%0A%0A%0A%40app.function%0Adef%20read_geography(path%3A%20Path)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20PSGC%20code%20list%20(one%20row%20per%20city%20or%20municipality)%2C%20with%20its%20region%20and%20province%20codes%20as%20zero-padded%20text.%22%22%22%0A%20%20%20%20geography%20%3D%20pd.read_csv(path%2C%20dtype%3Dstr)%0A%20%20%20%20geography%5B%22region_code%22%5D%20%3D%20geography%5B%22region_code%22%5D.str.zfill(2)%0A%20%20%20%20geography%5B%22province_code%22%5D%20%3D%20geography%5B%22province_code%22%5D.str.zfill(3)%0A%20%20%20%20return%20geography%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Module%204's%20design%20findings%20as%20decisions%2C%20in%20a%20file%20a%20script%20can%20read%20too%0A%20%20%20%20SCRATCH.mkdir(parents%3DTrue%2C%20exist_ok%3DTrue)%0A%20%20%20%20design_decisions(course.planted_defects()).to_csv(SCRATCH%20%2F%20%22design_decisions.csv%22%2C%20index%3DFalse)%0A%20%20%20%20decisions%20%3D%20read_decisions(SCRATCH%20%2F%20%22design_decisions.csv%22)%0A%20%20%20%20frame%20%3D%20read_survey(PATHS.data%2C%20EXPECTED_REGIONS%2C%20decisions)%0A%20%20%20%20geography%20%3D%20read_geography(PATHS.codes%20%2F%20%22geography.csv%22)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(frame)%3A%2C%7D%20households**%20in%20%7Blen(EXPECTED_REGIONS)%7D%20regions%2C%20after%20the%20%7Blen(decisions)%7D%20decisions%20in%20%60scratch%2Fpublish%2Fdesign_decisions.csv%60%3A%22)%2C%0A%20%20%20%20%20%20%20%20decisions%2C%0A%20%20%20%20%20%20%20%20frame.head()%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20decisions%2C%20frame%2C%20geography%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%23%20The%20estimates%2C%20by%20level%0A%0A%20%20%20%20Module%206%20taught%20svy%20step%20by%20step.%20Here%20two%20small%20functions%20wrap%20it.%20%60design_sample%60%20declares%20the%20design%3A%20the%20strata%2C%20the%20sampling%20units%2C%20the%20final%20weight%20%60sel_final_weight%60%2C%20and%20each%20stratum's%20count%20of%20sampling%20units%20for%20the%20finite%20population%20correction.%20%60estimate%60%20gives%20one%20indicator's%20estimates%20as%20a%20pandas%20table%20with%20the%20columns%20every%20table%20in%20this%20module%20uses%3A%20the%20domain's%20codes%2C%20then%20%60estimate%60%2C%20%60se%60%2C%20%60cv%60%2C%20the%2095%25%20confidence%20interval%20%60lci%60%20and%20%60uci%60%2C%20%60unweighted%60%20and%20%60n%60.%20svy%20gives%20the%20CV%20as%20a%20fraction%3A%20%600.0396%60%20is%203.96%25.%20Its%20settings%20are%20fixed%3B%20section%202%20says%20why.%0A%0A%20%20%20%20The%20last%20two%20columns%20describe%20the%20sample%20rather%20than%20the%20population%2C%20as%20module%206's%20section%203%20set%20them%20side%20by%20side.%20%60n%60%20is%20the%20number%20of%20households%20interviewed%20in%20the%20domain.%20%60unweighted%60%20is%20the%20sample's%20own%20figure%20for%20the%20indicator%2C%20computed%20with%20pandas%20from%20the%20same%20rows%3A%20for%20a%20total%2C%20the%20sum%20over%20the%20households%20interviewed%3B%20for%20a%20mean%2C%20their%20plain%20mean.%20A%20reader%20judges%20an%20estimate%20by%20the%20interviews%20behind%20it.%0A%0A%20%20%20%20The%20tables%20publish%20four%20indicators%20at%20three%20levels.%20The%20setting%20%60INDICATORS%60%20in%20the%20setup%20cell%20lists%20them%2C%20each%20with%20the%20column%20it%20is%20estimated%20from%2C%20the%20statistic%20and%20the%20Excel%20format%20it%20is%20shown%20in%3A%0A%0A%20%20%20%20%7C%20indicator%20%7C%20estimated%20from%20%7C%20statistic%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20fishing%20households%20%7C%20%60hh_count%60%2C%201%20on%20every%20row%20%7C%20total%3A%20the%20number%20of%20households%20the%20sample%20stands%20for%20%7C%0A%20%20%20%20%7C%20household%20members%20%7C%20%60hh_size%60%2C%20the%20household's%20rows%20in%20%60member%60%20%7C%20total%20%7C%0A%20%20%20%20%7C%20mean%20household%20size%20%7C%20%60hh_size%60%20%7C%20mean%20%7C%0A%20%20%20%20%7C%20households%20running%20a%20large-scale%20unit%20%7C%20%60runs_large_unit%60%2C%201%20if%20one%20of%20the%20household's%20units%20has%20a%20boat%20of%205.0%20gross%20tons%20or%20more%20(%60unit_scale%60%20%3D%202)%20%7C%20total%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20design_sample(frame%3A%20pd.DataFrame)%20-%3E%20svy.Sample%3A%0A%20%20%20%20%22%22%22The%20survey's%20sample%20as%20svy%20reads%20it%3A%20stratified%2C%20sampling%20units%20within%20strata%2C%20the%20final%20weight%2C%20and%20each%0A%20%20%20%20stratum's%20count%20of%20sampling%20units%20for%20the%20finite%20population%20correction%20(svy's%20pop_size%20wants%20counts%2C%20not%20fractions).%22%22%22%0A%20%20%20%20design%20%3D%20svy.Design(stratum%3D%22stratum_id%22%2C%20psu%3D%22psu_id%22%2C%20wgt%3D%22sel_final_weight%22%2C%20pop_size%3D%22psus_in_stratum%22%2C%20singleton%3D%22skip%22)%0A%20%20%20%20return%20svy.Sample(data%3Dpl.from_pandas(frame)%2C%20design%3Ddesign)%0A%0A%0A%40app.function%0Adef%20estimate(sample%3A%20svy.Sample%2C%20column%3A%20str%2C%20statistic%3A%20str%2C%20by%3A%20list%5Bstr%5D%20%7C%20None%20%3D%20None)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20indicator's%20estimates%2C%20as%20pandas%2C%20with%20the%20same%20settings%20every%20time%3A%20the%20domain%20columns%20(none%20for%20the%20whole%0A%20%20%20%20survey)%2C%20then%20estimate%2C%20se%2C%20cv%20(a%20fraction)%2C%20the%2095%25%20confidence%20interval%20lci%20and%20uci%2C%20unweighted%20(the%20sample's%20own%0A%20%20%20%20figure%3A%20the%20column's%20sum%20over%20the%20domain's%20households%2C%20or%20for%20a%20mean%20their%20plain%20mean%2C%20computed%20with%20pandas)%20and%20n%0A%20%20%20%20(households%20in%20the%20sample).%20No%20design%20effect.%22%22%22%0A%20%20%20%20by%20%3D%20by%20or%20%5B%5D%0A%20%20%20%20method%20%3D%20%7B%22total%22%3A%20sample.estimation.total%2C%20%22mean%22%3A%20sample.estimation.mean%7D%5Bstatistic%5D%0A%20%20%20%20result%20%3D%20method(y%3Dcolumn%2C%20by%3Dby%20or%20None).to_polars().to_pandas().rename(columns%3D%7B%22est%22%3A%20%22estimate%22%7D)%0A%20%20%20%20rows%20%3D%20sample.data.to_pandas()%20%20%23%20the%20households%20svy%20estimated%20from%2C%20one%20row%20each%0A%20%20%20%20own%20%3D%20%22sum%22%20if%20statistic%20%3D%3D%20%22total%22%20else%20%22mean%22%0A%20%20%20%20if%20by%3A%0A%20%20%20%20%20%20%20%20counted%20%3D%20rows.groupby(by%2C%20as_index%3DFalse)%5Bcolumn%5D.agg(own).rename(columns%3D%7Bcolumn%3A%20%22unweighted%22%7D)%0A%20%20%20%20%20%20%20%20result%20%3D%20result.merge(counted%2C%20on%3Dby%2C%20how%3D%22left%22%2C%20validate%3D%22one_to_one%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20result%5B%22unweighted%22%5D%20%3D%20rows%5Bcolumn%5D.agg(own)%0A%20%20%20%20return%20result%5B%5B*by%2C%20*FIGURES%5D%5D%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20The%20number%20of%20fishing%20households%20at%20each%20level%3A%20the%20whole%20survey%2C%20each%20region%2C%20each%20province%0A%20%20%20%20sample%20%3D%20design_sample(frame)%0A%20%20%20%20_national%20%3D%20estimate(sample%2C%20%22hh_count%22%2C%20%22total%22)%0A%20%20%20%20_regional%20%3D%20estimate(sample%2C%20%22hh_count%22%2C%20%22total%22%2C%20%5B%22region_code%22%5D)%0A%20%20%20%20_provincial%20%3D%20estimate(sample%2C%20%22hh_count%22%2C%20%22total%22%2C%20%5B%22region_code%22%2C%20%22province_code%22%5D)%0A%20%20%20%20mo.vstack(%5B_national%2C%20_regional%2C%20_provincial%5D)%0A%20%20%20%20return%20(sample%2C)%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%20Three%20tables%2C%20three%20shapes%3A%20the%20national%20one%20has%20no%20domain%20column%2C%20the%20regional%20one%20has%20%60region_code%60%2C%20the%20provincial%20one%20%60region_code%60%20and%20%60province_code%60.%20A%20reader%20wants%20names%2C%20not%20codes%2C%20and%20the%20program%20that%20writes%20the%20workbook%20wants%20one%20table%20it%20can%20split%20by%20level.%20The%20names%20are%20in%20the%20PSGC%20code%20list%2C%20%60geography%60%2C%20which%20has%20**one%20row%20per%20city%20or%20municipality**.%20La%20Union's%20name%20sits%20on%20every%20one%20of%20its%20cities'%20and%20municipalities'%20rows%2C%20so%20a%20join%20on%20the%20province%20code%20alone%20matches%20each%20province%20several%20times%20and%20multiplies%20its%20rows%3A%20the%20trap%20of%20module%203%2C%20and%20the%20reason%20%60validate%3D%60%20exists.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(geography)%3A%0A%20%20%20%20%23%20The%20code%20list%20has%20a%20row%20per%20city%20or%20municipality%3A%20a%20province's%20name%20repeats%20on%20each%20of%20its%20rows%0A%20%20%20%20geography.groupby(%5B%22region_code%22%2C%20%22province_code%22%2C%20%22province%22%5D).size().rename(%22rows%20in%20the%20code%20list%22).reset_index()%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%23%20Exercise%207.1.%20Every%20level%20in%20one%20long%20table%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20workbook%2C%20the%20CSV%20files%20and%20the%20report%20all%20start%20from%20one%20table%20that%20holds%20every%20level.%20If%20a%20province's%20row%20is%20doubled%20by%20the%20join%20that%20names%20it%2C%20every%20one%20of%20its%20estimates%20appears%20twice%2C%20and%20a%20reader%20who%20adds%20up%20the%20provinces%20gets%20a%20total%20twice%20too%20large.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20three%20tables%20%60estimate%60%20gives%20for%20one%20indicator%20(national%2C%20regional%2C%20provincial)%20and%20the%20code%20list%20%60geography%60%20%E2%86%92%20one%20table%20with%20the%20national%20row%20first%2C%20then%20the%20regions%2C%20then%20the%20provinces%2C%20and%20these%20columns.%20The%20example%20is%20the%20check's%20small%20made-up%20table%3A%0A%0A%20%20%20%20%7C%20level%20%7C%20region_code%20%7C%20province_code%20%7C%20domain%20%7C%20estimate%20%7C%20se%20%7C%20cv%20%7C%20n%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20national%20%7C%20*missing*%20%7C%20*missing*%20%7C%20All%20regions%20%7C%20300.0%20%7C%2030.0%20%7C%200.100%20%7C%2040%20%7C%0A%20%20%20%20%7C%20regional%20%7C%2001%20%7C%20*missing*%20%7C%20Region%20I%20(Ilocos%20Region)%20%7C%20200.0%20%7C%2025.0%20%7C%200.125%20%7C%2025%20%7C%0A%20%20%20%20%7C%20provincial%20%7C%2001%20%7C%20033%20%7C%20La%20Union%20%7C%20120.0%20%7C%2018.0%20%7C%200.150%20%7C%2015%20%7C%0A%0A%20%20%20%20-%20%60level%60%3A%20%60%22national%22%60%2C%20%60%22regional%22%60%20or%20%60%22provincial%22%60%3B%0A%20%20%20%20-%20%60domain%60%3A%20the%20name%20a%20reader%20sees%3A%20%60%22All%20regions%22%60%20for%20the%20national%20row%2C%20the%20region's%20name%20(%60geography%60's%20%60region%60)%20for%20a%20region%2C%20the%20province's%20name%20(%60province%60)%20for%20a%20province%3B%0A%20%20%20%20-%20%60region_code%60%2C%20%60province_code%60%3A%20the%20codes%2C%20missing%20where%20the%20level%20has%20none%3B%0A%20%20%20%20-%20one%20row%20per%20row%20of%20the%20three%20inputs%2C%20no%20more.%0A%0A%20%20%20%20**Tools%3A**%20%60drop_duplicates%60%20on%20the%20code%20list's%20name%20columns%2C%20%60merge(...%2C%20validate%3D%22many_to_one%22)%60%2C%20%60assign%60%2C%20and%20%60pd.concat%60%2C%20all%20from%20Arrays%20%26%20DataFrames%20and%20module%203.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20stack_levels(national%3A%20pd.DataFrame%2C%20regional%3A%20pd.DataFrame%2C%20provincial%3A%20pd.DataFrame%2C%20geography%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20three%20levels'%20estimates%20in%20one%20table%3A%20level%2C%20region_code%2C%20province_code%2C%20domain%20(the%20name%20a%20reader%20sees)%2C%0A%20%20%20%20estimate%2C%20se%2C%20cv%20and%20n%3B%20the%20national%20row%20first%2C%20then%20the%20regions%2C%20then%20the%20provinces.%22%22%22%0A%20%20%20%20%23%20Exercise%207.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20regions'%20names%3A%20one%20row%20per%20region%2C%20from%20geography's%20region_code%20and%20region%20(drop_duplicates)%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20with%20region%20renamed%20to%20domain%3B%20the%20provinces'%20names%20likewise%2C%20from%20region_code%2C%20province_code%20and%20province%0A%20%20%20%20%23%20Step%202%3A%20name%20the%20regional%20and%20provincial%20tables%20(merge%2C%20validate%3D%22many_to_one%22)%2C%20and%20give%20each%20table%20its%20level%3B%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20the%20national%20table%20gets%20domain%20%22All%20regions%22%0A%20%20%20%20%23%20Step%203%3A%20pd.concat%20the%20three%2C%20national%20first%2C%20and%20return%20the%20columns%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20level%2C%20region_code%2C%20province_code%2C%20domain%2C%20estimate%2C%20se%2C%20cv%2C%20n%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%207.1%22)%0A%0A%0A%40app.function%0Adef%20small_estimates()%20-%3E%20tuple%5Bpd.DataFrame%2C%20pd.DataFrame%2C%20pd.DataFrame%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22One%20indicator's%20estimates%20for%20two%20regions%20and%20three%20provinces%2C%20made%20up%2C%20and%20a%20code%20list%20with%20two%20cities%20or%0A%20%20%20%20municipalities%20per%20province%20(so%20a%20join%20on%20the%20province%20code%20alone%20doubles%20each%20province)%2C%20used%20by%20the%20checks.%22%22%22%0A%20%20%20%20national%20%3D%20pd.DataFrame(%7B%22estimate%22%3A%20%5B300.0%5D%2C%20%22se%22%3A%20%5B30.0%5D%2C%20%22cv%22%3A%20%5B0.1%5D%2C%20%22n%22%3A%20%5B40%5D%7D)%0A%20%20%20%20regional%20%3D%20pd.DataFrame(%7B%22region_code%22%3A%20%5B%2201%22%2C%20%2207%22%5D%2C%20%22estimate%22%3A%20%5B200.0%2C%20100.0%5D%2C%20%22se%22%3A%20%5B25.0%2C%2012.0%5D%2C%20%22cv%22%3A%20%5B0.125%2C%200.12%5D%2C%20%22n%22%3A%20%5B25%2C%2015%5D%7D)%0A%20%20%20%20provincial%20%3D%20pd.DataFrame(%7B%22region_code%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2207%22%5D%2C%20%22province_code%22%3A%20%5B%22033%22%2C%20%22055%22%2C%20%22012%22%5D%2C%20%22estimate%22%3A%20%5B120.0%2C%2080.0%2C%20100.0%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%20%20%20%22se%22%3A%20%5B18.0%2C%2016.0%2C%2012.0%5D%2C%20%22cv%22%3A%20%5B0.15%2C%200.2%2C%200.12%5D%2C%20%22n%22%3A%20%5B15%2C%2010%2C%2015%5D%7D)%0A%20%20%20%20geography%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2207%22%2C%20%2207%22%5D%2C%0A%20%20%20%20%20%20%20%20%22province_code%22%3A%20%5B%22033%22%2C%20%22033%22%2C%20%22055%22%2C%20%22055%22%2C%20%22012%22%2C%20%22012%22%5D%2C%0A%20%20%20%20%20%20%20%20%22citymun_code%22%3A%20%5B%2201%22%2C%20%2202%22%2C%20%2201%22%2C%20%2202%22%2C%20%2201%22%2C%20%2202%22%5D%2C%0A%20%20%20%20%20%20%20%20%22province%22%3A%20%5B%22La%20Union%22%2C%20%22La%20Union%22%2C%20%22Pangasinan%22%2C%20%22Pangasinan%22%2C%20%22Bohol%22%2C%20%22Bohol%22%5D%2C%0A%20%20%20%20%20%20%20%20%22region%22%3A%20%5B%22Region%20I%20(Ilocos%20Region)%22%5D%20*%204%20%2B%20%5B%22Region%20VII%20(Central%20Visayas)%22%5D%20*%202%2C%0A%20%20%20%20%7D)%0A%20%20%20%20return%20national%2C%20regional%2C%20provincial%2C%20geography%0A%0A%0A%40app.function%0Adef%20level_rows(table%3A%20pd.DataFrame%2C%20digits%3A%20int%20%3D%204)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20long%20table%20in%20one%20order%20for%20the%20checks%3A%20national%2C%20regional%2C%20provincial%2C%20then%20by%20code%3B%20columns%20in%20name%20order%3B%0A%20%20%20%20numbers%20rounded.%20So%20a%20right%20answer%20passes%20whatever%20order%20its%20rows%20and%20columns%20came%20in.%22%22%22%0A%20%20%20%20order%20%3D%20table%5B%22level%22%5D.map(%7B%22national%22%3A%200%2C%20%22regional%22%3A%201%2C%20%22provincial%22%3A%202%7D)%0A%20%20%20%20out%20%3D%20table.assign(_order%3Dorder).sort_values(%5B%22_order%22%2C%20%22region_code%22%2C%20%22province_code%22%5D%2C%20na_position%3D%22first%22).drop(columns%3D%22_order%22)%0A%20%20%20%20out%20%3D%20out.reset_index(drop%3DTrue)%5Bsorted(out.columns.drop(%22_order%22%2C%20errors%3D%22ignore%22))%5D%0A%20%20%20%20numbers%20%3D%20out.select_dtypes(%22number%22).columns%0A%20%20%20%20out%5Bnumbers%5D%20%3D%20out%5Bnumbers%5D.round(digits)%0A%20%20%20%20return%20out%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(geography%2C%20sample)%3A%0A%20%20%20%20def%20_levels(sample%2C%20column%2C%20statistic)%3A%0A%20%20%20%20%20%20%20%20return%20tuple(estimate(sample%2C%20column%2C%20statistic%2C%20by)%20for%20by%20in%20LEVEL_CODES.values())%0A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%207.1%22%2C%20stack_levels%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20table%3A%20two%20regions%2C%20three%20provinces%2C%20a%20code%20list%20with%20two%20cities%20or%20municipalities%20per%20province%22%2C%20small_estimates())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20fishing%20households%22%2C%20(*_levels(sample%2C%20%22hh_count%22%2C%20%22total%22)%2C%20geography))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20mean%20household%20size%22%2C%20(*_levels(sample%2C%20%22hh_size%22%2C%20%22mean%22)%2C%20geography))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20estimates%22%2C%20summarize%3Dlevel_rows%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Drop%20the%20code%20list's%20duplicates%20before%20naming%20(one%20row%20per%20region%2C%20one%20per%20province)%2C%20give%20each%20table%20its%20level%20and%20the%20national%20one%20domain%20'All%20regions'%2C%20then%20concat.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(geography%2C%20sample)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20households%20at%20every%20level%2C%20named%0A%20%20%20%20try_it(stack_levels%2C%20estimate(sample%2C%20%22hh_count%22%2C%20%22total%22)%2C%20estimate(sample%2C%20%22hh_count%22%2C%20%22total%22%2C%20%5B%22region_code%22%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20estimate(sample%2C%20%22hh_count%22%2C%20%22total%22%2C%20%5B%22region_code%22%2C%20%22province_code%22%5D)%2C%20geography)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%207.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20*MergeError%3A%20Merge%20keys%20are%20not%20unique%20in%20right%20dataset*%20%7C%20The%20code%20list%20still%20has%20a%20row%20per%20city%20or%20municipality.%20%7C%20%60drop_duplicates()%60%20before%20the%20merge%20%7C%0A%20%20%20%20%7C%20Far%20more%20rows%20than%20the%20inputs%20have%2C%20each%20province%20repeated%20%7C%20The%20same%2C%20without%20%60validate%3D%60%3A%20the%20join%20multiplied%20the%20rows.%20%7C%20%60drop_duplicates()%60%2C%20and%20%60validate%3D%22many_to_one%22%60%20to%20catch%20it%20%7C%0A%20%20%20%20%7C%20Every%20province%20named%20after%20its%20region%2C%20or%20no%20%60domain%60%20column%20%7C%20The%20provinces%20were%20named%20from%20%60region%60%2C%20or%20the%20name%20column%20not%20renamed%20to%20%60domain%60.%20%7C%20%60rename(columns%3D%7B%22province%22%3A%20%22domain%22%7D)%60%20%7C%0A%20%20%20%20%7C%20The%20national%20row's%20%60domain%60%20is%20missing%20%7C%20Only%20the%20regional%20and%20provincial%20tables%20were%20named.%20%7C%20%60national.assign(domain%3D%22All%20regions%22)%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'level'*%20%7C%20The%20tables%20were%20stacked%20without%20a%20level.%20%7C%20%60.assign(level%3D...)%60%20on%20each%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20all_estimates(frame%3A%20pd.DataFrame%2C%20geography%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Every%20indicator%20at%20every%20level%2C%20in%20one%20long%20table%3A%20the%20columns%20of%20stack_levels%2C%20with%20indicator%20after%20domain.%22%22%22%0A%20%20%20%20sample%20%3D%20design_sample(frame)%0A%20%20%20%20parts%20%3D%20%5B%5D%0A%20%20%20%20for%20name%2C%20(column%2C%20statistic%2C%20_)%20in%20INDICATORS.items()%3A%0A%20%20%20%20%20%20%20%20levels%20%3D%20%5Bestimate(sample%2C%20column%2C%20statistic%2C%20by)%20for%20by%20in%20LEVEL_CODES.values()%5D%0A%20%20%20%20%20%20%20%20parts.append(stack_levels(*levels%2C%20geography).assign(indicator%3Dname))%0A%20%20%20%20columns%20%3D%20%5B%22level%22%2C%20%22region_code%22%2C%20%22province_code%22%2C%20%22domain%22%2C%20%22indicator%22%2C%20%22estimate%22%2C%20%22se%22%2C%20%22cv%22%2C%20%22n%22%5D%0A%20%20%20%20return%20pd.concat(parts%2C%20ignore_index%3DTrue)%5Bcolumns%5D%0A%0A%0A%40app.cell%0Adef%20_(frame%2C%20geography)%3A%0A%20%20%20%20%23%20The%20four%20indicators%2C%20each%20at%20the%20three%20levels%3A%20one%20long%20table%0A%20%20%20%20estimates%20%3D%20attempt(all_estimates%2C%20frame%2C%20geography)%0A%20%20%20%20mo.stop(estimates%20is%20NOT_YET%2C%20waiting_for(%22Exercise%207.1%22))%0A%20%20%20%20estimates%0A%20%20%20%20return%20(estimates%2C)%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%20Every%20estimate%20goes%20out%20with%20its%20SE%20and%20its%20CV%3A%20the%20CV%20says%20how%20far%20a%20reader%20can%20trust%20the%20number%20beside%20it.%20A%20rule%20that%20marks%20or%20holds%20back%20the%20estimates%20whose%20CV%20is%20too%20large%20is%20the%20stretch%20S2's%2C%20with%20the%20script%20that%20applies%20it.%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%23%20A%20workbook%20with%20openpyxl%0A%0A%20%20%20%20A%20workbook%20is%20a%20%60Workbook%60%3B%20each%20sheet%20in%20it%20a%20worksheet%3B%20each%20cell%20has%20a%20%60value%60%2C%20a%20%60font%60%2C%20and%20a%20%60number_format%60.%20Four%20habits%20cover%20what%20this%20module%20writes%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20workbook%20%3D%20openpyxl.Workbook()%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20a%20new%20workbook%2C%20which%20starts%20with%20one%20empty%20sheet%2C%20%22Sheet%22%0A%20%20%20%20workbook.remove(workbook.active)%20%20%20%20%20%20%20%20%20%20%20%23%20...%20removed%2C%20so%20the%20workbook%20holds%20only%20the%20sheets%20you%20add%0A%20%20%20%20sheet%20%3D%20workbook.create_sheet(%22National%22)%20%20%23%20a%20new%20sheet%2C%20at%20the%20end%0A%20%20%20%20sheet.append(%5B%22domain%22%2C%20%22estimate%22%5D)%20%20%20%20%20%20%20%23%20one%20row%2C%20after%20the%20last%20one%20written%0A%20%20%20%20sheet%5B%22A1%22%5D.font%20%3D%20Font(bold%3DTrue)%20%20%20%20%20%20%20%20%20%23%20a%20bold%20header%20cell%0A%20%20%20%20cell%20%3D%20sheet.cell(row%3D2%2C%20column%3D2%2C%20value%3D3812.4467)%0A%20%20%20%20cell.number_format%20%3D%20%22%23%2C%23%230%22%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20shown%20as%203%2C812%3B%20the%20cell%20keeps%203812.4467%0A%20%20%20%20sheet.freeze_panes%20%3D%20%22A2%22%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20row%201%20stays%20in%20view%20as%20the%20reader%20scrolls%0A%20%20%20%20workbook.save(SCRATCH%20%2F%20%22tables.xlsx%22)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20The%20number%20formats%20this%20module%20uses%20are%20Excel's%20own%20patterns%3A%0A%0A%20%20%20%20%7C%20format%20%7C%20the%20value%203812.4467%20shows%20as%20%7C%20the%20value%200.039631%20shows%20as%20%7C%20for%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60%23%2C%23%230%60%20%7C%203%2C812%20%7C%200%20%7C%20totals%20and%20their%20SEs%2C%20sample%20counts%20%7C%0A%20%20%20%20%7C%20%600.00%60%20%7C%203812.45%20%7C%200.04%20%7C%20means%2C%20such%20as%20the%20mean%20household%20size%20%7C%0A%20%20%20%20%7C%20%600.0%25%60%20%7C%20381244.7%25%20%7C%204.0%25%20%7C%20CVs%2C%20kept%20as%20fractions%20in%20the%20cell%20%7C%0A%20%20%20%20%7C%20%60General%60%20%7C%203812.4467%20%7C%200.039631%20%7C%20text%2C%20and%20anything%20not%20formatted%20%7C%0A%0A%20%20%20%20The%20table's%20last%20column%20shows%20why%20the%20format%20follows%20the%20value%3A%20a%20mean%20household%20size%20of%204.31%2C%20formatted%20for%20totals%2C%20reads%20as%204.%20In%20a%20long%20table%2C%20%60estimate%60%20holds%20totals%20and%20means%20in%20the%20same%20column%2C%20so%20its%20format%20is%20chosen%20row%20by%20row%2C%20from%20the%20row's%20indicator.%0A%0A%20%20%20%20**Two%20habits%20to%20leave%20behind.**%20Writing%20a%20number%20as%20text%20(%60%223%2C812%22%60)%20makes%20Excel%20show%20it%20but%20not%20add%20it%3A%20a%20reader's%20%60SUM%60%20of%20that%20column%20is%200.%20Writing%20a%20DataFrame's%20rows%20with%20%60itertuples()%60%20puts%20the%20row's%20index%20in%20front%20of%20its%20values%2C%20so%20every%20value%20lands%20one%20column%20to%20the%20right%20of%20its%20header%3B%20%60itertuples(index%3DFalse)%60%20or%20%60to_dict(%22records%22)%60%20leaves%20the%20index%20out.%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%23%20Exercise%207.2.%20A%20sheet%20with%20a%20header%2C%20number%20formats%20and%20frozen%20panes%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20function%20every%20sheet%20of%20the%20workbook%20is%20written%20with%2C%20the%20notes%20sheet%20included.%20Get%20the%20formats%20right%20once%20and%20every%20table%20of%20every%20batch%20reads%20the%20same.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20workbook%2C%20a%20sheet%20title%20and%20a%20table%20%E2%86%92%20the%20new%20sheet%2C%20added%20at%20the%20end%20of%20the%20workbook%2C%20holding%3A%0A%0A%20%20%20%20-%20row%201%3A%20the%20table's%20column%20names%2C%20**bold**%3B%0A%20%20%20%20-%20one%20row%20per%20table%20row%2C%20the%20values%20as%20they%20are%20(a%20missing%20value%20as%20an%20empty%20cell)%2C%20in%20the%20table's%20columns%2C%20no%20index%3B%0A%20%20%20%20-%20number%20formats%3A%20%60estimate%60%20and%20%60se%60%20in%20the%20format%20of%20the%20row's%20indicator%20(%60INDICATORS%5Bindicator%5D%5B2%5D%60%3A%20%60%22%23%2C%23%230%22%60%20for%20totals%2C%20%60%220.00%22%60%20for%20the%20mean)%3B%20every%20other%20column%20named%20in%20%60NUMBER_FORMATS%60%20(%60cv%60%20as%20%60%220.0%25%22%60%2C%20%60n%60%20as%20%60%22%23%2C%23%230%22%60)%20in%20its%20format%3B%20anything%20else%20left%20as%20it%20is%20(%60%22General%22%60)%3B%0A%20%20%20%20-%20the%20panes%20frozen%20below%20the%20header%20(%60%22A2%22%60).%0A%0A%20%20%20%20The%20first%20rows%20of%20the%20check's%20small%20made-up%20table%2C%20as%20the%20sheet%20shows%20them%3A%0A%0A%20%20%20%20%7C%20%7C%20A%20%7C%20B%20%7C%20C%20%7C%20D%20%7C%20E%20%7C%20F%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%201%20%7C%20**domain**%20%7C%20**indicator**%20%7C%20**estimate**%20%7C%20**se**%20%7C%20**cv**%20%7C%20**n**%20%7C%0A%20%20%20%20%7C%202%20%7C%20La%20Union%20%7C%20fishing%20households%20%7C%203%2C812%20%7C%20151%20%7C%204.0%25%20%7C%20375%20%7C%0A%20%20%20%20%7C%203%20%7C%20La%20Union%20%7C%20mean%20household%20size%20%7C%204.31%20%7C%200.07%20%7C%201.6%25%20%7C%20375%20%7C%0A%0A%20%20%20%20A%20table%20without%20%60estimate%60%20(the%20notes%20sheet's%20%60item%60%20and%20%60note%60)%20is%20written%20the%20same%20way%2C%20with%20no%20number%20formats.%0A%0A%20%20%20%20**Tools%3A**%20%60workbook.create_sheet%60%2C%20%60sheet.append%60%2C%20%60Font(bold%3DTrue)%60%2C%20%60cell.number_format%60%2C%20%60sheet.freeze_panes%60%2C%20above%3B%20%60table.to_dict(%22records%22)%60%20gives%20the%20rows%20as%20dictionaries%20of%20column%20name%20and%20value.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20add_sheet(workbook%3A%20openpyxl.Workbook%2C%20title%3A%20str%2C%20table%3A%20pd.DataFrame)%3A%0A%20%20%20%20%22%22%22Add%20a%20sheet%20called%20%60title%60%20at%20the%20end%20of%20the%20workbook%2C%20holding%20the%20table%3A%20a%20bold%20header%2C%20the%20values%20(a%20missing%20one%0A%20%20%20%20as%20an%20empty%20cell)%2C%20each%20numeric%20column%20in%20its%20Excel%20number%20format%2C%20the%20panes%20frozen%20below%20the%20header.%20Returns%20it.%22%22%22%0A%20%20%20%20%23%20Exercise%207.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20a%20new%20sheet%20at%20the%20end%20of%20the%20workbook%20(create_sheet)%2C%20and%20the%20column%20names%20as%20its%20first%20row%2C%20in%20bold%0A%20%20%20%20%23%20Step%202%3A%20for%20each%20row%20of%20the%20table%20(to_dict(%22records%22))%3A%20append%20its%20values%2C%20a%20missing%20one%20as%20None%0A%20%20%20%20%23%20Step%203%3A%20on%20that%20row's%20cells%2C%20set%20the%20number%20format%3A%20estimate%20and%20se%20from%20INDICATORS%5Bthe%20row's%20indicator%5D%5B2%5D%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20a%20column%20named%20in%20NUMBER_FORMATS%20from%20there%20(sheet%5Bsheet.max_row%5D%20is%20the%20row%20just%20appended)%0A%20%20%20%20%23%20Step%204%3A%20freeze%20the%20panes%20below%20the%20header%20(%22A2%22)%2C%20and%20return%20the%20sheet%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%207.2%22)%0A%0A%0A%40app.function%0Adef%20blank_workbook(*sheets%3A%20str)%20-%3E%20openpyxl.Workbook%3A%0A%20%20%20%20%22%22%22A%20workbook%20holding%20only%20these%20sheets%2C%20each%20with%20a%20line%20of%20text%20in%20A1%20(none%3A%20a%20workbook%20with%20no%20sheets%20at%20all).%22%22%22%0A%20%20%20%20workbook%20%3D%20openpyxl.Workbook()%0A%20%20%20%20workbook.remove(workbook.active)%0A%20%20%20%20for%20title%20in%20sheets%3A%0A%20%20%20%20%20%20%20%20workbook.create_sheet(title)%5B%22A1%22%5D%20%3D%20f%22the%20sheet%20%7Btitle%7D%2C%20written%20before%22%0A%20%20%20%20return%20workbook%0A%0A%0A%40app.function%0Adef%20sheet_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Four%20made-up%20rows%20as%20a%20level's%20sheet%20holds%20them%3A%20two%20indicators%20of%20different%20formats%2C%20and%20an%20estimate%20with%20no%20value.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22domain%22%3A%20%5B%22La%20Union%22%2C%20%22La%20Union%22%2C%20%22La%20Union%22%2C%20%22Pangasinan%22%5D%2C%0A%20%20%20%20%20%20%20%20%22indicator%22%3A%20%5B%22fishing%20households%22%2C%20%22mean%20household%20size%22%2C%20%22households%20running%20a%20large-scale%20unit%22%2C%20%22households%20running%20a%20large-scale%20unit%22%5D%2C%0A%20%20%20%20%20%20%20%20%22estimate%22%3A%20%5B3812.4467%2C%204.30562%2C%20np.nan%2C%20157.6278%5D%2C%0A%20%20%20%20%20%20%20%20%22se%22%3A%20%5B151.0903%2C%200.068893%2C%20np.nan%2C%2039.5547%5D%2C%0A%20%20%20%20%20%20%20%20%22cv%22%3A%20%5B0.039631%2C%200.016001%2C%200.4122%2C%200.250933%5D%2C%0A%20%20%20%20%20%20%20%20%22n%22%3A%20%5B375%2C%20375%2C%20375%2C%20363%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.function%0Adef%20sheet_summary(sheet)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22What%20a%20check%20of%20Exercise%207.2%20reads%20back%20from%20the%20sheet%3A%20the%20workbook's%20sheets%2C%20the%20title%2C%20the%20frozen%20panes%2C%20the%0A%20%20%20%20header%20with%20its%20boldness%2C%20and%20every%20cell's%20value%20and%20number%20format.%22%22%22%0A%20%20%20%20rows%20%3D%20list(sheet.iter_rows())%0A%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%22sheets%20in%20the%20workbook%22%3A%20sheet.parent.sheetnames%2C%0A%20%20%20%20%20%20%20%20%22title%22%3A%20sheet.title%2C%0A%20%20%20%20%20%20%20%20%22panes%20frozen%20at%22%3A%20sheet.freeze_panes%2C%0A%20%20%20%20%20%20%20%20%22header%20(value%2C%20bold)%22%3A%20%5B(cell.value%2C%20bool(cell.font.b))%20for%20cell%20in%20rows%5B0%5D%5D%20if%20rows%20else%20%5B%5D%2C%0A%20%20%20%20%20%20%20%20%22rows%20(value%2C%20number%20format)%22%3A%20%5B%5B(cell.value%2C%20cell.number_format)%20for%20cell%20in%20row%5D%20for%20row%20in%20rows%5B1%3A%5D%5D%2C%0A%20%20%20%20%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(sample)%3A%0A%20%20%20%20%23%20A%20regional%20sheet%20from%20the%20survey%2C%20with%20codes%20as%20its%20domain%20(no%20Exercise%207.1%20needed)%2C%20and%20a%20notes%20table%0A%20%20%20%20_regional%20%3D%20pd.concat(%5B%0A%20%20%20%20%20%20%20%20estimate(sample%2C%20column%2C%20statistic%2C%20%5B%22region_code%22%5D).assign(indicator%3Dname)%0A%20%20%20%20%20%20%20%20for%20name%2C%20(column%2C%20statistic%2C%20_)%20in%20INDICATORS.items()%0A%20%20%20%20%5D).assign(domain%3Dlambda%20t%3A%20t%5B%22region_code%22%5D)%5B%5B%22region_code%22%2C%20%22domain%22%2C%20%22indicator%22%2C%20%22estimate%22%2C%20%22se%22%2C%20%22cv%22%2C%20%22n%22%5D%5D%0A%20%20%20%20_notes%20%3D%20pd.DataFrame(%7B%22item%22%3A%20%5B%22Source%22%2C%20%22CV%22%5D%2C%20%22note%22%3A%20%5B%22the%20Survey%20of%20Fishing%20Households%202025%22%2C%20%22the%20SE%20divided%20by%20the%20estimate%22%5D%7D)%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%207.2%22%2C%20add_sheet%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22four%20rows%20of%20two%20formats%20and%20an%20estimate%20with%20no%20value%2C%20into%20a%20workbook%20with%20no%20sheets%22%2C%20(blank_workbook()%2C%20%22Provincial%22%2C%20sheet_fixture()))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20four%20indicators%20by%20region%2C%20into%20a%20workbook%20that%20already%20has%20a%20National%20sheet%22%2C%20(blank_workbook(%22National%22)%2C%20%22Regional%22%2C%20_regional))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20notes%20table%2C%20with%20no%20numbers%20to%20format%22%2C%20(blank_workbook(%22National%22%2C%20%22Regional%22)%2C%20%22Notes%22%2C%20_notes))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22workbooks%22%2C%20summarize%3Dsheet_summary%2C%0A%20%20%20%20%20%20%20%20hint%3D%22create_sheet(title)%3B%20a%20bold%20header%3B%20each%20row's%20values%20with%20None%20for%20missing%3B%20estimate%20and%20se%20formatted%20from%20the%20row's%20indicator%2C%20cv%20and%20n%20from%20NUMBER_FORMATS%3B%20freeze_panes%20%3D%20'A2'%3B%20return%20the%20sheet.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20check's%20four%20rows%20as%20a%20sheet%2C%20read%20back%20cell%20by%20cell%20(value%2C%20then%20number%20format)%0A%20%20%20%20def%20_try_sheet()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20sheet%20%3D%20add_sheet(blank_workbook()%2C%20%22Provincial%22%2C%20sheet_fixture())%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20except%20Exception%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20course.try_it(add_sheet%2C%20blank_workbook()%2C%20%22Provincial%22%2C%20sheet_fixture())%20%20%23%20runs%20it%20again%2C%20to%20show%20the%20error%20and%20what%20it%20usually%20means%0A%20%20%20%20%20%20%20%20if%20sheet%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22%60add_sheet%60%20gave%20back%20%60None%60%3A%20end%20it%20with%20%60return%20sheet%60.%22)%2C%20kind%3D%22warn%22)%0A%20%20%20%20%20%20%20%20cells%20%3D%20%5B%5Bf%22%7Bcell.value!r%7D%20%C2%B7%20%7Bcell.number_format%7D%22%20for%20cell%20in%20row%5D%20for%20row%20in%20sheet.iter_rows()%5D%0A%20%20%20%20%20%20%20%20return%20pd.DataFrame(cells%2C%20columns%3D%5Bopenpyxl.utils.get_column_letter(n)%20for%20n%20in%20range(1%2C%20len(cells%5B0%5D)%20%2B%201)%5D)%0A%0A%20%20%20%20_try_sheet()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%207.2%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20*AttributeError%3A%20'NoneType'%20object%20has%20no%20attribute%20...*%20%7C%20%60workbook.active%60%20in%20a%20workbook%20with%20no%20sheets.%20%7C%20%60workbook.create_sheet(title)%60%20%7C%0A%20%20%20%20%7C%20The%20sheets%20in%20the%20workbook%20are%20wrong%2C%20or%20the%20earlier%20sheet's%20text%20is%20gone%20%7C%20The%20table%20was%20written%20into%20the%20open%20sheet%2C%20not%20a%20new%20one.%20%7C%20%60create_sheet%60%20adds%20one%20at%20the%20end%20%7C%0A%20%20%20%20%7C%20Every%20value%20one%20column%20to%20the%20right%20of%20its%20header%2C%20a%20number%20first%20%7C%20The%20rows%20came%20from%20%60itertuples()%60%2C%20which%20starts%20with%20the%20index.%20%7C%20%60to_dict(%22records%22)%60%2C%20or%20%60itertuples(index%3DFalse)%60%20%7C%0A%20%20%20%20%7C%20The%20mean%20household%20size%20formatted%20%60%23%2C%23%230%60%20%7C%20The%20format%20was%20chosen%20by%20column%20only.%20%7C%20%60INDICATORS%5Brecord%5B%22indicator%22%5D%5D%5B2%5D%60%20%7C%0A%20%20%20%20%7C%20%60nan%60%20where%20an%20estimate%20with%20no%20value%20should%20be%20empty%20%7C%20The%20missing%20value%20was%20written%20as%20it%20is.%20%7C%20%60None%20if%20pd.isna(value)%20else%20value%60%20%7C%0A%20%20%20%20%7C%20Header%20not%20bold%2C%20or%20%60panes%20frozen%20at%60%20is%20%60None%60%20%7C%20A%20step%20left%20out.%20%7C%20%60Font(bold%3DTrue)%60%2C%20%60freeze_panes%20%3D%20%22A2%22%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%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%23%20The%20notes%20sheet%2C%20and%20the%20workbook%0A%0A%20%20%20%20A%20table%20read%20away%20from%20its%20source%20needs%20its%20source%20on%20it.%20The%20notes%20sheet%20holds%20what%20a%20reader%20needs%20to%20use%20the%20numbers%3A%20what%20the%20tables%20are%2C%20which%20databases%20they%20come%20from%2C%20which%20regions%20they%20cover%20and%20which%20are%20missing%2C%20which%20households%20were%20left%20out%20or%20moved%20and%20why%2C%20the%20design%20the%20SEs%20assume%2C%20what%20the%20SE%20and%20the%20CV%20mean%2C%20and%20the%20date%20the%20tables%20were%20made.%20%60notes_table%60%20writes%20it%20as%20a%20table%20of%20%60item%60%20and%20%60note%60%2C%20and%20%60write_workbook%60%20puts%20everything%20together%3A%20a%20sheet%20per%20level%2C%20then%20the%20notes%2C%20each%20written%20by%20your%20%60add_sheet%60%2C%20with%20columns%20wide%20enough%20to%20read.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20sheet_table(estimates%3A%20pd.DataFrame%2C%20level%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20level's%20rows%20as%20its%20sheet%20shows%20them%3A%20the%20codes%20a%20level%20has%2C%20then%20domain%2C%20indicator%2C%20estimate%2C%20se%2C%20cv%2C%20n.%22%22%22%0A%20%20%20%20codes%20%3D%20LEVEL_CODES%5Blevel%5D%0A%20%20%20%20rows%20%3D%20estimates%5Bestimates%5B%22level%22%5D%20%3D%3D%20level%5D%0A%20%20%20%20return%20rows%5B%5B*codes%2C%20%22domain%22%2C%20%22indicator%22%2C%20%22estimate%22%2C%20%22se%22%2C%20%22cv%22%2C%20%22n%22%5D%5D.reset_index(drop%3DTrue)%0A%0A%0A%40app.function%0Adef%20notes_table(regions%3A%20list%5Bstr%5D%2C%20missing%3A%20list%5Bstr%5D%2C%20decisions%3A%20pd.DataFrame%2C%20made_on%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20notes%20sheet%3A%20what%20the%20tables%20are%2C%20where%20they%20come%20from%2C%20what%20they%20assume%2C%20what%20their%20columns%20mean%2C%20and%20when%0A%20%20%20%20they%20were%20made.%22%22%22%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(%22Tables%22%2C%20%22Fishing%20households%3A%20estimates%20from%20the%20Survey%20of%20Fishing%20Households%202025%2C%20for%20all%20regions%20together%2C%20by%20region%20and%20by%20province%22)%2C%0A%20%20%20%20%20%20%20%20(%22Source%22%2C%20%22The%20survey's%20databases%2C%20one%20per%20region%3A%20%22%20%2B%20%22%2C%20%22.join(f%22sfh2025_r%7Br%7D.duckdb%22%20for%20r%20in%20regions))%2C%0A%20%20%20%20%20%20%20%20(%22Regions%20in%20the%20tables%22%2C%20%22%2C%20%22.join(regions))%2C%0A%20%20%20%20%20%20%20%20(%22Regions%20missing%22%2C%20%22%2C%20%22.join(missing)%20%2B%20%22%3A%20no%20database%3B%20every%20table%2C%20the%20total%20of%20all%20regions%20included%2C%20leaves%20them%20out%22%20if%20missing%20else%20%22none%22)%2C%0A%20%20%20%20%20%20%20%20(%22Design%20decisions%22%2C%20%22%3B%20%22.join(f%22%7Bregion%7D-%7Bhousehold%7D%3A%20%7Bdecision%7D%2C%20%7Breason%7D%22%20for%20region%2C%20household%2C%20decision%2C%20reason%20in%20decisions%5B%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22decision%22%2C%20%22reason%22%5D%5D.itertuples(index%3DFalse)))%2C%0A%20%20%20%20%20%20%20%20(%22Design%22%2C%20%22Strata%3A%20province%20by%20stratum%20(F%2C%20A%2C%20B%2C%20C).%20Sampling%20units%3A%20the%20enumeration%20areas%3B%20in%20stratum%20F%2C%20where%20every%20registered%20large%20unit%20is%20taken%2C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22each%20household.%20Weight%3A%20sel_final_weight.%20Finite%20population%20correction%20from%20the%20strata's%20counts%20of%20sampling%20units.%20Variance%20by%20Taylor%20linearization%20(svy%20%7Bsvy.__version__%7D).%22)%2C%0A%20%20%20%20%20%20%20%20(%22estimate%22%2C%20%22a%20total%2C%20or%20for%20the%20mean%20household%20size%20a%20mean%2C%20for%20the%20households%20the%20sample%20stands%20for%22)%2C%0A%20%20%20%20%20%20%20%20(%22se%22%2C%20%22the%20estimate's%20standard%20error%2C%20in%20the%20estimate's%20unit%22)%2C%0A%20%20%20%20%20%20%20%20(%22cv%22%2C%20%22the%20coefficient%20of%20variation%3A%20the%20SE%20divided%20by%20the%20estimate%22)%2C%0A%20%20%20%20%20%20%20%20(%22n%22%2C%20%22households%20in%20the%20sample%20in%20the%20domain%22)%2C%0A%20%20%20%20%20%20%20%20(%22Tables%20made%20on%22%2C%20made_on)%2C%0A%20%20%20%20%5D%0A%20%20%20%20return%20pd.DataFrame(rows%2C%20columns%3D%5B%22item%22%2C%20%22note%22%5D)%0A%0A%0A%40app.function%0Adef%20write_workbook(estimates%3A%20pd.DataFrame%2C%20notes%3A%20pd.DataFrame%2C%20path%3A%20Path)%20-%3E%20Path%3A%0A%20%20%20%20%22%22%22The%20workbook%3A%20a%20sheet%20per%20level%20(National%2C%20Regional%2C%20Provincial)%20and%20the%20notes%2C%20each%20column%20as%20wide%20as%20its%20longest%0A%20%20%20%20value%20(up%20to%2060%20characters).%22%22%22%0A%20%20%20%20workbook%20%3D%20openpyxl.Workbook()%0A%20%20%20%20workbook.remove(workbook.active)%20%20%23%20the%20empty%20sheet%20every%20new%20workbook%20starts%20with%0A%20%20%20%20for%20level%2C%20title%20in%20LEVEL_SHEETS.items()%3A%0A%20%20%20%20%20%20%20%20add_sheet(workbook%2C%20title%2C%20sheet_table(estimates%2C%20level))%0A%20%20%20%20add_sheet(workbook%2C%20%22Notes%22%2C%20notes)%0A%20%20%20%20for%20sheet%20in%20workbook.worksheets%3A%0A%20%20%20%20%20%20%20%20for%20column%20in%20sheet.columns%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20longest%20%3D%20max(len(f%22%7Bcell.value%3A%2C.2f%7D%22%20if%20isinstance(cell.value%2C%20float)%20else%20str(cell.value))%20for%20cell%20in%20column%20if%20cell.value%20is%20not%20None)%20if%20any(cell.value%20is%20not%20None%20for%20cell%20in%20column)%20else%208%0A%20%20%20%20%20%20%20%20%20%20%20%20sheet.column_dimensions%5Bcolumn%5B0%5D.column_letter%5D.width%20%3D%20min(60%2C%20longest%20%2B%202)%0A%20%20%20%20workbook.save(path)%0A%20%20%20%20return%20path%0A%0A%0A%40app.cell%0Adef%20_(decisions%2C%20estimates)%3A%0A%20%20%20%20%23%20The%20notebook's%20own%20workbook%20of%20the%20tables%2C%20written%20and%20then%20read%20back%20with%20pandas%0A%20%20%20%20_notes%20%3D%20notes_table(EXPECTED_REGIONS%2C%20%5B%5D%2C%20decisions%2C%20datetime.date.today().isoformat())%0A%20%20%20%20_sheets%20%3D%20attempt(lambda%3A%20pd.read_excel(write_workbook(estimates%2C%20_notes%2C%20SCRATCH%20%2F%20%22tables_from_notebook.xlsx%22)%2C%20sheet_name%3DNone))%0A%20%20%20%20mo.stop(_sheets%20is%20NOT_YET%2C%20waiting_for(%22Exercise%207.2%22))%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22%60scratch%2Fpublish%2Ftables_from_notebook.xlsx%60%2C%20read%20back%3A%20its%20sheets%20are%20**%7B'%2C%20'.join(_sheets)%7D**.%20Open%20it%20in%20Excel%20to%20see%20the%20formats%3B%20pandas%20reads%20the%20values%20the%20cells%20keep%2C%20so%20the%20totals%20come%20back%20with%20all%20their%20decimals.%20The%20national%20sheet%3A%22)%2C%0A%20%20%20%20%20%20%20%20_sheets%5B%22National%22%5D%2C%0A%20%20%20%20%20%20%20%20mo.md(%22The%20notes%20sheet%3A%22)%2C%0A%20%20%20%20%20%20%20%20_sheets%5B%22Notes%22%5D%2C%0A%20%20%20%20%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%23%20xlsxwriter%2C%20the%20alternative%0A%0A%20%20%20%20xlsxwriter%20is%20the%20other%20library%20pandas%20writes%20Excel%20with%20(%60pd.ExcelWriter(path%2C%20engine%3D%22xlsxwriter%22)%60).%20It%20sets%20formats%20as%20it%20writes%2C%20by%20column%20or%20by%20cell%2C%20and%20is%20fast%20on%20large%20tables.%20It%20only%20writes%3A%20it%20cannot%20open%20a%20workbook%20to%20read%20it%20or%20to%20add%20a%20sheet%20to%20it%2C%20which%20openpyxl%20can.%20R's%20writexl%2C%20built%20on%20the%20C%20library%20libxlsxwriter%2C%20only%20writes%20too.%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20with%20pd.ExcelWriter(SCRATCH%20%2F%20%22tables_xlsxwriter.xlsx%22%2C%20engine%3D%22xlsxwriter%22)%20as%20writer%3A%0A%20%20%20%20%20%20%20%20table.to_excel(writer%2C%20sheet_name%3D%22National%22%2C%20index%3DFalse)%0A%20%20%20%20%20%20%20%20thousands%20%3D%20writer.book.add_format(%7B%22num_format%22%3A%20%22%23%2C%23%230%22%7D)%0A%20%20%20%20%20%20%20%20writer.sheets%5B%22National%22%5D.set_column(%22C%3AD%22%2C%2012%2C%20thousands)%20%20%23%20columns%20C%20and%20D%3A%20width%2012%2C%20format%20%23%2C%23%230%0A%20%20%20%20%60%60%60%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%202%20%C2%B7%20The%20tabulation%20as%20one%20script%2C%20from%20the%20flat%20table%20to%20one%20workbook%0A%0A%20%20%20%20**The%20problem.**%20The%20workbook%20above%20came%20from%20a%20notebook%2C%20run%20cell%20by%20cell%2C%20by%20you.%20When%20the%20next%20batch%20of%20data%20arrives%2C%20the%20regions'%20databases%20are%20read%20again%2C%20and%20someone%20else%20makes%20the%20tables.%20A%20tabulation%20is%20a%20script%20for%20that%20reason%2C%20and%20its%20steps%20run%20in%20one%20order%3A%20read%20the%20flat%20table%2C%20create%20the%20variables%20the%20tables%20need%2C%20declare%20the%20design%2C%20estimate%20every%20table%20at%20every%20level%20with%20the%20sample's%20own%20figures%20beside%20each%20estimate%2C%20and%20save%20the%20tables%20to%20one%20workbook%2C%20a%20sheet%20per%20table%20with%20its%20national%2C%20regional%20and%20provincial%20rows%20stacked.%0A%0A%20%20%20%20**The%20R%20way.**%20In%20ReGenesees%20you%20write%20the%20estimation%20settings%20once%2C%20in%20a%20helper%20around%20%60svystatTM%60%3A%20the%20SE%2C%20the%20CV%20in%20percent%2C%20a%2095%25%20confidence%20interval%2C%20no%20design%20effect.%20Every%20table%20then%20calls%20the%20helper%20with%20only%20the%20design%2C%20the%20variable%20and%20the%20domains%2C%20once%20per%20table%20and%20level%3A%0A%0A%20%20%20%20%60%60%60r%0A%20%20%20%20estimate%20%3C-%20function(design%2C%20y%2C%20by%20%3D%20NULL)%20%7B%0A%20%20%20%20%20%20svystatTM(design%2C%20y%2C%20by%20%3D%20by%2C%20vartype%20%3D%20c(%22se%22%2C%20%22cvpct%22)%2C%20conf.int%20%3D%20TRUE%2C%20conf.lev%20%3D%200.95%2C%20deff%20%3D%20FALSE)%0A%20%20%20%20%7D%0A%20%20%20%20estimate(des%2C%20~hh_count)%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%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20national%0A%20%20%20%20estimate(des%2C%20~hh_count%2C%20by%20%3D%20~region_code)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20regional%0A%20%20%20%20estimate(des%2C%20~hh_count%2C%20by%20%3D%20~region_code%3Aprovince_code%3Ascale)%20%20%23%20provincial%2C%20by%20scale%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20%60des%60%20is%20the%20design%20declared%20with%20%60e.svydesign%60%2C%20as%20module%206%20showed%2C%20and%20%60by%20%3D%20~a%3Ab%60%20crosses%20two%20domain%20variables%20(the%20arguments%20as%20ReGenesees'%20reference%20page%20for%20%60svystatTM%60%20documents%20them%2C%20checked%202026-11-20).%20The%20course's%20own%20%60estimate%60%20is%20the%20counterpart%20of%20that%20helper%2C%20with%20one%20more%20column%3A%20%60unweighted%60%2C%20the%20sample's%20own%20figure%2C%20which%20it%20computes%20with%20pandas.%0A%0A%20%20%20%20**The%20Python%20way.**%20The%20script%20below%20does%20the%20same%2C%20from%20pieces%20this%20module%20and%20module%206%20have%20built%3A%0A%0A%20%20%20%20%7C%20step%20%7C%20in%20the%20script%20%7C%20from%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20read%20the%20flat%20table%20%7C%20%60pd.read_parquet(FLAT_TABLE)%60%20%7C%20saved%20once%20below%2C%20as%20module%202%20saved%20its%20flat%20table%20%7C%0A%20%20%20%20%7C%20create%20the%20variables%20%7C%20%60add_variables%60%3A%20%60scale%60%2C%20%22Large-scale%22%20or%20%22Small-scale%22%20%7C%20module%205's%20recodes%20%7C%0A%20%20%20%20%7C%20declare%20the%20design%20%7C%20%60design_sample%60%20%7C%20above%3B%20module%206%2C%20section%201%20%7C%0A%20%20%20%20%7C%20the%20helper%20with%20its%20settings%20fixed%20%7C%20%60estimate%60%2C%20above%20%7C%20SE%2C%20CV%2C%2095%25%20CI%2C%20no%20design%20effect%2C%20the%20sample's%20own%20figures%20%7C%0A%20%20%20%20%7C%20every%20table%2C%20its%20three%20levels%20stacked%20%7C%20%60table_sheets%60%20%7C%20**Exercise%207.3**%20%7C%0A%20%20%20%20%7C%20one%20workbook%2C%20a%20sheet%20per%20table%20%7C%20%60save_tables%60%20%7C%20section%201's%20formats%20%7C%0A%0A%20%20%20%20One%20setting%20differs%20in%20form%20from%20the%20R%20helper's.%20ReGenesees'%20%60cvpct%60%20gives%20the%20CV%20as%20a%20number%20in%20percent%2C%203.96.%20%60estimate%60%20keeps%20svy's%20fraction%2C%200.0396%2C%20and%20the%20workbook%20shows%20it%20as%204.0%25%20with%20the%20number%20format%20%600.0%25%60%2C%20as%20section%201's%20workbook%20does%3A%20a%20reader%20sees%20a%20percentage%2C%20and%20a%20formula%20still%20gets%20the%20CV.%0A%0A%20%20%20%20The%20tables%20the%20script%20makes%20are%20a%20setting%2C%20%60TABLES%60%2C%20in%20the%20setup%20cell%3A%20each%20one's%20name%2C%20the%20indicator%20it%20estimates%20(from%20%60INDICATORS%60)%20and%20the%20variables%20it%20is%20cut%20by%20besides%20the%20level's%20codes.%20A%20table%20cut%20by%20more%20variables%20is%20one%20more%20line%20there%2C%20once%20the%20flat%20table%20holds%20them.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20script's%20tables%3A%20the%20indicator%20each%20estimates%2C%20its%20statistic%2C%20and%20its%20domains%20at%20the%20provincial%20level%0A%20%20%20%20pd.DataFrame(%5B%0A%20%20%20%20%20%20%20%20%7B%22table%22%3A%20table%2C%20%22indicator%22%3A%20indicator%2C%20%22statistic%22%3A%20INDICATORS%5Bindicator%5D%5B1%5D%2C%0A%20%20%20%20%20%20%20%20%20%22by%2C%20at%20the%20provincial%20level%22%3A%20%22%2C%20%22.join(%5B*LEVEL_CODES%5B%22provincial%22%5D%2C%20*domains%5D)%7D%0A%20%20%20%20%20%20%20%20for%20table%2C%20(indicator%2C%20domains)%20in%20TABLES.items()%0A%20%20%20%20%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%23%20The%20flat%20table%2C%20saved%20once%0A%0A%20%20%20%20The%20script%20reads%20one%20file%3A%20the%20households%20as%20section%201%20read%20them%2C%20the%20design%20decisions%20applied%2C%20saved%20as%20Parquet.%20Saving%20it%20once%20means%20every%20run%20of%20the%20script%20starts%20from%20the%20same%20rows%2C%20and%20the%20script%20does%20not%20need%20the%20databases.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20The%20flat%20table%20the%20script%20reads%3A%20section%201's%20households%2C%20the%20design%20decisions%20applied%2C%20as%20Parquet%0A%20%20%20%20flat_table%20%3D%20PATHS.root%20%2F%20FLAT_TABLE%0A%20%20%20%20flat_table.parent.mkdir(parents%3DTrue%2C%20exist_ok%3DTrue)%0A%20%20%20%20frame.to_parquet(flat_table%2C%20index%3DFalse)%0A%20%20%20%20mo.md(f%22Saved%20%60%7BFLAT_TABLE%7D%60%3A%20**%7Blen(pd.read_parquet(flat_table))%3A%2C%7D%20households**%2C%20read%20back%20from%20the%20file.%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20add_variables(frame%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20variable%20the%20tables%20need%20that%20the%20flat%20table%20lacks%3A%20%60scale%60%2C%20%22Large-scale%22%20for%20a%20household%20that%20runs%20a%0A%20%20%20%20large-scale%20unit%20and%20%22Small-scale%22%20for%20one%20whose%20units%20are%20all%20small-scale.%22%22%22%0A%20%20%20%20return%20frame.assign(scale%3Dnp.where(frame%5B%22runs_large_unit%22%5D%20%3D%3D%201%2C%20%22Large-scale%22%2C%20%22Small-scale%22))%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20The%20survey's%20sample%2C%20as%20the%20script%20declares%20it%2C%20with%20the%20variable%20the%20tables%20need%0A%20%20%20%20tab_sample%20%3D%20design_sample(add_variables(frame))%0A%20%20%20%20mo.md(f%22%60tab_sample%60%3A%20**%7Btab_sample.n_records%3A%2C%7D%20households**%20in%20%7Btab_sample.n_strata%7D%20strata%2C%20with%20%60scale%60%20beside%20the%20flat%20table's%20columns.%22)%0A%20%20%20%20return%20(tab_sample%2C)%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%23%20Exercise%207.3.%20Every%20table%2C%20its%20three%20levels%20stacked%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20body%20of%20the%20tabulation%3A%20every%20table%2C%20at%20every%20level%2C%20through%20one%20helper%20whose%20settings%20are%20fixed.%20Written%20once%2C%20the%20script%20calls%20it%20and%20writes%20one%20workbook%3B%20a%20new%20table%20is%20a%20line%20in%20%60TABLES%60%2C%20not%20a%20new%20block%20of%20code.%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%20_rows%20%3D%20%22%5Cn%22.join(%0A%20%20%20%20%20%20%20%20f'%7C%20%60%7Btable%7D%60%20%7C%20%60%7BINDICATORS%5Bindicator%5D%5B0%5D%7D%60%20%7C%20%60%7BINDICATORS%5Bindicator%5D%5B1%5D%7D%60%20%7C%20%60%7B%5B*LEVEL_CODES%5B%22provincial%22%5D%2C%20*domains%5D%7D%60%20%7C'%0A%20%20%20%20%20%20%20%20for%20table%2C%20(indicator%2C%20domains)%20in%20TABLES.items()%0A%20%20%20%20)%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20sample%20%60design_sample%60%20gives%20(from%20a%20frame%20with%20%60scale%60)%20%E2%86%92%20a%20dictionary%20of%20sheets%2C%20one%20per%20table%2C%20in%20the%20order%20of%20%60TABLES%60%3A%0A%0A%20%20%20%20-%20the%20key%3A%20the%20table's%20name%2C%20%60%22fishing_households%22%60%3B%0A%20%20%20%20-%20the%20value%3A%20the%20table's%20three%20levels%20stacked%2C%20national%20first%2C%20then%20regional%2C%20then%20provincial.%20Each%20level's%20rows%20are%20%60estimate(sample%2C%20column%2C%20statistic%2C%20by)%60%2C%20with%20the%20column%20and%20statistic%20of%20the%20table's%20indicator%20(%60INDICATORS%5Bindicator%5D%5B0%5D%60%20and%20%60%5B1%5D%60)%20and%20%60by%60%20the%20level's%20codes%20(%60LEVEL_CODES%5Blevel%5D%60)%20followed%20by%20the%20table's%20own%20variables%2C%20and%20a%20column%20%60level%60%20that%20names%20the%20level%3B%0A%20%20%20%20-%20the%20sheet's%20columns%2C%20in%20this%20order%3A%20%60level%60%2C%20%60region_code%60%2C%20%60province_code%60%2C%20the%20table's%20own%20variables%2C%20then%20%60FIGURES%60%20(%60estimate%60%2C%20%60se%60%2C%20%60cv%60%2C%20%60lci%60%2C%20%60uci%60%2C%20%60unweighted%60%2C%20%60n%60).%20The%20national%20rows%20have%20no%20codes%20and%20the%20regional%20rows%20no%20province%20code%2C%20so%20those%20cells%20are%20missing.%0A%0A%20%20%20%20At%20the%20provincial%20level%3A%0A%0A%20%20%20%20%7C%20key%20%7C%20column%20%7C%20statistic%20%7C%20by%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7B_rows%7D%0A%0A%20%20%20%20At%20the%20national%20level%20%60by%60%20holds%20only%20the%20table's%20own%20variables%2C%20and%20is%20empty%20for%20the%20first%20three%20tables%3A%20%60estimate%60%20takes%20an%20empty%20list%20as%20the%20whole%20survey.%0A%0A%20%20%20%20**Tools%3A**%20a%20%60for%60%20loop%20over%20%60TABLES.items()%60%20and%20one%20inside%20it%20over%20%60LEVEL_CODES.items()%60%3B%20a%20list%20of%20each%20level's%20rows%2C%20then%20%60pd.concat%60%3B%20%60assign(level%3D...)%60%3B%20%60%5B*a%2C%20*b%5D%60%20joins%20two%20lists%3B%20%60estimate%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20table_sheets(sample%3A%20svy.Sample)%20-%3E%20dict%5Bstr%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22Every%20table%20of%20TABLES%20as%20one%20sheet%2C%20in%20that%20order%3A%20its%20national%2C%20regional%20and%20provincial%20rows%20from%20estimate()%2C%0A%20%20%20%20stacked%2C%20with%20a%20column%20level%3B%20the%20columns%20level%2C%20the%20provincial%20codes%2C%20the%20table's%20own%20variables%2C%20then%20FIGURES.%22%22%22%0A%20%20%20%20%23%20Exercise%207.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20an%20empty%20dictionary%20of%20sheets%0A%20%20%20%20%23%20Step%202%3A%20for%20each%20table%20and%20its%20(indicator%2C%20domains)%20in%20TABLES%3A%20the%20indicator's%20column%20and%20statistic%20from%20INDICATORS%0A%20%20%20%20%23%20Step%203%3A%20for%20each%20level%20and%20its%20codes%20in%20LEVEL_CODES%3A%20estimate(sample%2C%20column%2C%20statistic%2C%20%5B*codes%2C%20*domains%5D)%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20with%20a%20column%20level%3B%20keep%20each%20level's%20rows%20in%20a%20list%0A%20%20%20%20%23%20Step%204%3A%20sheets%5Btable%5D%20%3D%20pd.concat%20of%20the%20list%2C%20with%20the%20columns%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20%5B%22level%22%2C%20*LEVEL_CODES%5B%22provincial%22%5D%2C%20*domains%2C%20*FIGURES%5D%3B%20return%20the%20sheets%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%207.3%22)%0A%0A%0A%40app.function%0Adef%20small_households()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Nine%20households%20of%20two%20regions%20as%20the%20flat%20table%20holds%20them%2C%20with%20scale%2C%20used%20by%20the%20checks%20of%20Exercise%207.3%3A%20in%0A%20%20%20%20province%20033%20a%20take-all%20stratum%20F%20of%20two%20large-scale%20households%20and%20an%20area%20stratum%20of%202%20sampling%20units%20out%20of%204%3B%0A%20%20%20%20in%20province%20012%20an%20area%20stratum%20of%202%20sampling%20units%20out%20of%2010%20with%20no%20large-scale%20household%2C%20so%20its%20province%20has%20no%0A%20%20%20%20Large-scale%20row.%22%22%22%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%23%20region_code%2C%20province_code%2C%20stratum_id%2C%20psu_id%2C%20psus_in_stratum%2C%20sel_final_weight%2C%20hh_size%2C%20runs_large_unit%0A%20%20%20%20%20%20%20%20(%2201%22%2C%20%22033%22%2C%20%2201-033-F%22%2C%20%2201-hh-1%22%2C%202%2C%201.0%2C%206%2C%201)%2C%0A%20%20%20%20%20%20%20%20(%2201%22%2C%20%22033%22%2C%20%2201-033-F%22%2C%20%2201-hh-2%22%2C%202%2C%201.0%2C%203%2C%201)%2C%0A%20%20%20%20%20%20%20%20(%2201%22%2C%20%22033%22%2C%20%2201-033-A%22%2C%20%2201-033-A-1001%22%2C%204%2C%202.0%2C%204%2C%200)%2C%0A%20%20%20%20%20%20%20%20(%2201%22%2C%20%22033%22%2C%20%2201-033-A%22%2C%20%2201-033-A-1001%22%2C%204%2C%202.0%2C%205%2C%201)%2C%0A%20%20%20%20%20%20%20%20(%2201%22%2C%20%22033%22%2C%20%2201-033-A%22%2C%20%2201-033-A-1002%22%2C%204%2C%202.0%2C%202%2C%200)%2C%0A%20%20%20%20%20%20%20%20(%2207%22%2C%20%22012%22%2C%20%2207-012-A%22%2C%20%2207-012-A-1001%22%2C%2010%2C%205.0%2C%204%2C%200)%2C%0A%20%20%20%20%20%20%20%20(%2207%22%2C%20%22012%22%2C%20%2207-012-A%22%2C%20%2207-012-A-1001%22%2C%2010%2C%205.0%2C%207%2C%200)%2C%0A%20%20%20%20%20%20%20%20(%2207%22%2C%20%22012%22%2C%20%2207-012-A%22%2C%20%2207-012-A-1002%22%2C%2010%2C%205.0%2C%203%2C%200)%2C%0A%20%20%20%20%20%20%20%20(%2207%22%2C%20%22012%22%2C%20%2207-012-A%22%2C%20%2207-012-A-1002%22%2C%2010%2C%205.0%2C%205%2C%200)%2C%0A%20%20%20%20%5D%0A%20%20%20%20table%20%3D%20pd.DataFrame(rows%2C%20columns%3D%5B%22region_code%22%2C%20%22province_code%22%2C%20%22stratum_id%22%2C%20%22psu_id%22%2C%20%22psus_in_stratum%22%2C%20%22sel_final_weight%22%2C%20%22hh_size%22%2C%20%22runs_large_unit%22%5D)%0A%20%20%20%20return%20add_variables(table.assign(hh_count%3D1))%0A%0A%0A%40app.function%0Adef%20sheets_story(sheets%3A%20dict)%20-%3E%20list%3A%0A%20%20%20%20%22%22%22What%20a%20check%20of%20Exercise%207.3%20compares%3A%20each%20sheet's%20name%20and%20its%20table%2C%20in%20the%20dictionary's%20order%2C%20numbers%0A%20%20%20%20rounded.%22%22%22%0A%20%20%20%20return%20%5B(name%2C%20table.reset_index(drop%3DTrue).round(4))%20for%20name%2C%20table%20in%20sheets.items()%5D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(frame%2C%20tab_sample)%3A%0A%20%20%20%20_small%20%3D%20design_sample(small_households())%0A%20%20%20%20_region%20%3D%20design_sample(add_variables(frame%5Bframe%5B%22region_code%22%5D%20%3D%3D%20%2204%22%5D))%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%207.3%22%2C%20table_sheets%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22nine%20households%3A%20two%20provinces%2C%20one%20with%20no%20large-scale%20household%22%2C%20(_small%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20households%22%2C%20(tab_sample%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20households%20of%20region%2004%20alone%22%2C%20(_region%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22samples%22%2C%20summarize%3Dsheets_story%2C%0A%20%20%20%20%20%20%20%20hint%3D%22For%20each%20table%20of%20TABLES%2C%20then%20each%20level%20of%20LEVEL_CODES%3A%20estimate(sample%2C%20column%2C%20statistic%2C%20%5B*codes%2C%20*domains%5D).assign(level%3Dlevel)%3B%20concat%20the%20levels%2C%20then%20choose%20the%20columns%20%5B'level'%2C%20*LEVEL_CODES%5B'provincial'%5D%2C%20*domains%2C%20*FIGURES%5D.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(tab_sample)%3A%0A%20%20%20%20%23%20Try%20it%3A%20every%20sheet%2C%20each%20as%20its%20table%0A%20%20%20%20def%20_try_sheets()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20sheets%20%3D%20table_sheets(tab_sample)%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20except%20Exception%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20try_it(table_sheets%2C%20tab_sample)%20%20%23%20runs%20it%20again%2C%20to%20show%20the%20error%20and%20what%20it%20usually%20means%0A%20%20%20%20%20%20%20%20if%20not%20isinstance(sheets%2C%20dict)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(f%22%60table_sheets%60%20gave%20back%20a%20%60%7Btype(sheets).__name__%7D%60%3A%20it%20should%20give%20a%20dictionary%20of%20sheet%20names%20and%20tables.%22)%2C%20kind%3D%22warn%22)%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5Bmo.md(f%22**%7Blen(sheets)%7D%20sheets%3A**%20%7B'%2C%20'.join(f'%60%7Bname%7D%60'%20for%20name%20in%20sheets)%7D%22)%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*%5Bmo.vstack(%5Bmo.md(f%22%60%7Bname%7D%60%22)%2C%20table%5D)%20for%20name%2C%20table%20in%20sheets.items()%5D%5D)%0A%0A%20%20%20%20_try_sheets()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%207.3%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Each%20sheet%20holds%20only%20the%20provincial%20rows%20%7C%20The%20sheet%20was%20set%20inside%20the%20loop%20over%20levels%2C%20so%20each%20level%20wrote%20over%20the%20one%20before.%20%7C%20a%20list%20of%20the%20levels'%20rows%2C%20then%20one%20%60pd.concat%60%20per%20table%20%7C%0A%20%20%20%20%7C%20*KeyError*%20naming%20%60region_code%60%20%7C%20The%20columns%20were%20chosen%20on%20each%20level's%20rows%20before%20stacking%3A%20the%20national%20rows%20have%20no%20codes.%20%7C%20choose%20the%20columns%20after%20%60pd.concat%60%20%7C%0A%20%20%20%20%7C%20%60households_by_scale%60%20has%20no%20%60scale%60%20column%2C%20or%20it%20comes%20before%20the%20codes%20%7C%20The%20table's%20own%20variables%20left%20out%20of%20%60by%60%2C%20or%20put%20first.%20%7C%20%60%5B*codes%2C%20*domains%5D%60%20%7C%0A%20%20%20%20%7C%20The%20mean%20household%20size%20is%20a%20total%20in%20the%20tens%20of%20thousands%20%7C%20The%20statistic%20not%20taken%20from%20the%20indicator.%20%7C%20%60INDICATORS%5Bindicator%5D%5B1%5D%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'fishing%20households'*%20%7C%20%60INDICATORS%60%20looked%20up%20by%20the%20table's%20name%2C%20or%20%60TABLES%60%20by%20the%20indicator's.%20%7C%20%60TABLES%60%20gives%20the%20indicator%3B%20%60INDICATORS%5Bindicator%5D%60%20gives%20the%20column%20and%20statistic%20%7C%0A%20%20%20%20%7C%20No%20%60level%60%20column%20%7C%20The%20levels%20were%20stacked%20without%20saying%20which%20is%20which.%20%7C%20%60.assign(level%3Dlevel)%60%20on%20each%20level's%20rows%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%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%23%20One%20workbook%2C%20and%20the%20script%0A%0A%20%20%20%20%60save_tables%60%20writes%20the%20sheets%20to%20one%20workbook%2C%20as%20%60write_xlsx%60%20writes%20a%20named%20list%20of%20tables%2C%20with%20section%201's%20formats%20added%3A%20a%20bold%20header%3B%20the%20estimate%2C%20its%20SE%2C%20its%20interval%20and%20the%20unweighted%20figure%20in%20the%20format%20of%20the%20table's%20indicator%3B%20the%20CV%20as%20a%20percentage%3B%20the%20panes%20frozen%20below%20the%20header.%20A%20missing%20cell%2C%20such%20as%20a%20national%20row's%20codes%2C%20stays%20empty.%20%60tabulate%60%20runs%20the%20steps%20in%20order%20and%20prints%20the%20file%20it%20wrote.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20save_tables(sheets%3A%20dict%5Bstr%2C%20pd.DataFrame%5D%2C%20path%3A%20Path)%20-%3E%20Path%3A%0A%20%20%20%20%22%22%22The%20workbook%3A%20a%20sheet%20per%20entry%20of%20%60sheets%60%2C%20in%20order%2C%20with%20a%20bold%20header%3B%20estimate%2C%20se%2C%20lci%2C%20uci%20and%20unweighted%20in%0A%20%20%20%20the%20format%20of%20the%20table's%20indicator%2C%20the%20columns%20of%20NUMBER_FORMATS%20in%20theirs%3B%20panes%20frozen%20below%20the%20header.%22%22%22%0A%20%20%20%20path.parent.mkdir(parents%3DTrue%2C%20exist_ok%3DTrue)%0A%20%20%20%20with%20pd.ExcelWriter(path%2C%20engine%3D%22openpyxl%22)%20as%20writer%3A%0A%20%20%20%20%20%20%20%20for%20name%2C%20table%20in%20sheets.items()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20table.to_excel(writer%2C%20sheet_name%3Dname%2C%20index%3DFalse)%0A%20%20%20%20%20%20%20%20%20%20%20%20sheet%20%3D%20writer.sheets%5Bname%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20indicator%20%3D%20TABLES%5Bname%5D%5B0%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20formats%20%3D%20%7Bcolumn%3A%20INDICATORS%5Bindicator%5D%5B2%5D%20for%20column%20in%20(%22estimate%22%2C%20%22se%22%2C%20%22lci%22%2C%20%22uci%22%2C%20%22unweighted%22)%7D%20%7C%20NUMBER_FORMATS%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20cells%20in%20sheet.iter_cols()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20header%20%3D%20cells%5B0%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20header.font%20%3D%20Font(bold%3DTrue)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20cell%20in%20cells%5B1%3A%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cell.number_format%20%3D%20formats.get(header.value%2C%20%22General%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sheet.column_dimensions%5Bheader.column_letter%5D.width%20%3D%20max(12%2C%20len(str(header.value))%20%2B%202)%0A%20%20%20%20%20%20%20%20%20%20%20%20sheet.freeze_panes%20%3D%20%22A2%22%0A%20%20%20%20return%20path%0A%0A%0A%40app.function%0Adef%20tabulate(flat%3A%20Path%2C%20out%3A%20Path)%20-%3E%20Path%3A%0A%20%20%20%20%22%22%22The%20tabulation%3A%20the%20flat%20table%20read%2C%20its%20variables%20created%2C%20the%20design%20declared%2C%20every%20table%20estimated%20at%20every%0A%20%20%20%20level%20with%20the%20sample's%20own%20figures%20beside%20each%20estimate%2C%20and%20one%20workbook%20written%20to%20%60out%60.%20Returns%20its%20path.%22%22%22%0A%20%20%20%20frame%20%3D%20add_variables(pd.read_parquet(flat))%0A%20%20%20%20sample%20%3D%20design_sample(frame)%0A%20%20%20%20written%20%3D%20save_tables(table_sheets(sample)%2C%20out)%0A%20%20%20%20print(f%22wrote%20%7Bwritten%7D%22)%0A%20%20%20%20return%20written%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%20The%20script%20is%20these%20functions%20and%20settings%2C%20copied%20out%20of%20this%20notebook's%20file%2C%20with%20the%20imports%20at%20the%20top%20and%20one%20line%20at%20the%20bottom%20that%20runs%20%60tabulate%60.%20%60write_script%60%20does%20the%20copying%3A%20it%20reads%20%6060_publish_tables.py%60%20as%20Python%20code%20(the%20%60ast%60%20module%20parses%20it)%2C%20takes%20each%20setting%20and%20function%20the%20script%20needs%20by%20name%2C%20and%20writes%20them%20into%20%60scratch%2Fpublish%2Ftabulate.py%60.%20Your%20Exercise%207.3%20is%20in%20it%20as%20you%20wrote%20it%2C%20so%20the%20notebook%20stays%20the%20one%20place%20the%20code%20lives%3A%20change%20a%20function%20here%2C%20and%20the%20next%20copy%20of%20the%20script%20changes%20with%20it.%20(marimo%20saves%20the%20notebook%20as%20you%20edit%3B%20the%20copy%20is%20taken%20from%20the%20saved%20file.)%0A%0A%20%20%20%20It%20is%20a%20plain%20script%3A%20no%20options%2C%20and%20its%20paths%20are%20its%20settings%2C%20%60FLAT_TABLE%60%20and%20%60TABLES_FILE%60.%20In%20a%20terminal%2C%20from%20the%20course%20folder%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20uv%20run%20python%20scratch%2Fpublish%2Ftabulate.py%0A%20%20%20%20%60%60%60%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20script_source()%20-%3E%20str%3A%0A%20%20%20%20%22%22%22The%20script's%20text%3A%20what%20it%20does%2C%20its%20imports%2C%20the%20settings%20and%20functions%20it%20needs%20copied%20from%20this%20notebook's%20file%2C%0A%20%20%20%20and%20the%20line%20that%20runs%20it.%22%22%22%0A%20%20%20%20source%20%3D%20(PATHS.root%20%2F%20%2260_publish_tables.py%22).read_text(encoding%3D%22utf-8%22)%0A%20%20%20%20lines%20%3D%20source.splitlines()%0A%20%20%20%20tree%20%3D%20ast.parse(source)%0A%0A%20%20%20%20def%20text(node%3A%20ast.AST)%20-%3E%20str%3A%0A%20%20%20%20%20%20%20%20return%20textwrap.dedent(%22%5Cn%22.join(lines%5Bnode.lineno%20-%201%3Anode.end_lineno%5D))%0A%0A%20%20%20%20setup%20%3D%20next(node%20for%20node%20in%20tree.body%20if%20isinstance(node%2C%20ast.With))%20%20%23%20%60with%20app.setup%3A%60%0A%20%20%20%20settings%20%3D%20%7Btarget.id%3A%20text(node)%20for%20node%20in%20setup.body%20if%20isinstance(node%2C%20ast.Assign)%20for%20target%20in%20node.targets%20if%20isinstance(target%2C%20ast.Name)%7D%0A%20%20%20%20functions%20%3D%20%7Bnode.name%3A%20text(node)%20for%20node%20in%20tree.body%20if%20isinstance(node%2C%20ast.FunctionDef)%7D%0A%20%20%20%20header%20%3D%20f'''%22%22%22Tabulate%20the%20Survey%20of%20Fishing%20Households%202025%3A%20every%20table%20of%20TABLES%20with%20its%20national%2C%20regional%20and%20provincial%0Arows%20stacked%20and%20the%20sample's%20own%20figures%20beside%20each%20estimate%2C%20from%20the%20flat%20table%20to%20one%20workbook%2C%20a%20sheet%20per%20table.%0A%0AWritten%20by%2060_publish_tables.py%20from%20its%20functions%3A%20change%20them%20there%20and%20write%20the%20script%20again.%0ARun%20from%20the%20course%20folder%3A%20uv%20run%20python%20scratch%2Fpublish%2Ftabulate.py%20(svy%20%7Bsvy.__version__%7D).%0A%22%22%22%0A%0Afrom%20pathlib%20import%20Path%0A%0Aimport%20numpy%20as%20np%0Aimport%20pandas%20as%20pd%0Aimport%20polars%20as%20pl%0Aimport%20svy%0Afrom%20openpyxl.styles%20import%20Font'''%0A%20%20%20%20names%20%3D%20%5B%22FLAT_TABLE%22%2C%20%22TABLES_FILE%22%2C%20%22LEVEL_CODES%22%2C%20%22INDICATORS%22%2C%20%22NUMBER_FORMATS%22%2C%20%22FIGURES%22%2C%20%22TABLES%22%5D%0A%20%20%20%20parts%20%3D%20%5B%22add_variables%22%2C%20%22design_sample%22%2C%20%22estimate%22%2C%20%22table_sheets%22%2C%20%22save_tables%22%2C%20%22tabulate%22%5D%0A%20%20%20%20run%20%3D%20'if%20__name__%20%3D%3D%20%22__main__%22%3A%5Cn%20%20%20%20tabulate(Path(FLAT_TABLE)%2C%20Path(TABLES_FILE))'%0A%20%20%20%20return%20%22%5Cn%5Cn%5Cn%22.join(%5Bheader%2C%20%22%5Cn%22.join(settings%5Bname%5D%20for%20name%20in%20names)%2C%20*(functions%5Bname%5D%20for%20name%20in%20parts)%2C%20run%5D)%20%2B%20%22%5Cn%22%0A%0A%0A%40app.function%0Adef%20write_script(path%3A%20Path)%20-%3E%20Path%3A%0A%20%20%20%20%22%22%22Write%20the%20script%20to%20%60path%60%2C%20made%20from%20this%20notebook's%20functions.%22%22%22%0A%20%20%20%20path.parent.mkdir(parents%3DTrue%2C%20exist_ok%3DTrue)%0A%20%20%20%20path.write_text(script_source()%2C%20encoding%3D%22utf-8%22)%0A%20%20%20%20return%20path%0A%0A%0A%40app.cell%0Adef%20_(tab_sample)%3A%0A%20%20%20%20%23%20Write%20the%20script%20and%20run%20it%20from%20the%20course%20folder%2C%20as%20a%20terminal%20would%3B%20then%20say%20what%20it%20wrote%0A%20%20%20%20def%20_run()%3A%0A%20%20%20%20%20%20%20%20if%20attempt(table_sheets%2C%20tab_sample)%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20None%2C%20waiting_for(%22Exercise%207.3%22)%0A%20%20%20%20%20%20%20%20script%20%3D%20write_script(SCRATCH%20%2F%20%22tabulate.py%22)%0A%20%20%20%20%20%20%20%20done%20%3D%20subprocess.run(%5Bsys.executable%2C%20%22scratch%2Fpublish%2Ftabulate.py%22%5D%2C%20cwd%3DPATHS.root%2C%20capture_output%3DTrue%2C%20text%3DTrue%2C%20timeout%3D600)%0A%20%20%20%20%20%20%20%20shown%20%3D%20%5Bmo.md(f%22Wrote%20%60scratch%2Fpublish%2Ftabulate.py%60%2C%20**%7Blen(script.read_text(encoding%3D'utf-8').splitlines())%7D%20lines**%2C%20and%20ran%20it%3A%20exit%20code%20**%7Bdone.returncode%7D**.%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mo.accordion(%7B%22The%20whole%20script%22%3A%20mo.ui.code_editor(value%3Dscript.read_text(encoding%3D%22utf-8%22)%2C%20language%3D%22python%22%2C%20disabled%3DTrue)%7D)%5D%0A%20%20%20%20%20%20%20%20if%20done.stdout.strip()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20shown.append(mo.plain_text(done.stdout.rstrip()))%0A%20%20%20%20%20%20%20%20if%20done.returncode%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20shown.append(mo.plain_text(done.stderr.rstrip()%5B-3000%3A%5D))%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20None%2C%20mo.vstack(shown)%0A%20%20%20%20%20%20%20%20return%20PATHS.root%20%2F%20TABLES_FILE%2C%20mo.vstack(shown)%0A%0A%20%20%20%20tables_written%2C%20_shown%20%3D%20_run()%0A%20%20%20%20_shown%0A%20%20%20%20return%20(tables_written%2C)%0A%0A%0A%40app.cell%0Adef%20_(tables_written)%3A%0A%20%20%20%20%23%20The%20workbook%2C%20read%20back%3A%20each%20sheet's%20rows%2C%20and%20the%20households%20by%20scale%0A%20%20%20%20mo.stop(tables_written%20is%20None)%0A%20%20%20%20workbook_sheets%20%3D%20pd.read_excel(tables_written%2C%20sheet_name%3DNone%2C%20dtype%3D%7B%22region_code%22%3A%20str%2C%20%22province_code%22%3A%20str%7D)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20pd.DataFrame(%5B%7B%22sheet%22%3A%20name%2C%20%22rows%22%3A%20len(table)%2C%20%22national%2C%20regional%2C%20provincial%22%3A%20%22%2C%20%22.join(str(int((table%5B%22level%22%5D%20%3D%3D%20level).sum()))%20for%20level%20in%20LEVEL_CODES)%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20name%2C%20table%20in%20workbook_sheets.items()%5D)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22%60scratch%2Fpublish%2Ftables.xlsx%60%2C%20the%20sheet%20%60households_by_scale%60%3A%22)%2C%0A%20%20%20%20%20%20%20%20workbook_sheets%5B%22households_by_scale%22%5D%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20(workbook_sheets%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(workbook_sheets)%3A%0A%20%20%20%20_households%20%3D%20workbook_sheets%5B%22fishing_households%22%5D.iloc%5B0%5D%0A%20%20%20%20_scale%20%3D%20workbook_sheets%5B%22households_by_scale%22%5D%0A%20%20%20%20_large%20%3D%20_scale%5B(_scale%5B%22level%22%5D%20%3D%3D%20%22national%22)%20%26%20(_scale%5B%22scale%22%5D%20%3D%3D%20%22Large-scale%22)%5D.iloc%5B0%5D%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20Read%20the%20national%20rows%20across.%20The%20sample%20stands%20for%20**%7B_households%5B'estimate'%5D%3A%2C.0f%7D%20fishing%20households**%2C%20and%20rests%20on%20the%20**%7B_households%5B'unweighted'%5D%3A%2C.0f%7D%20households%20interviewed**%20(%60unweighted%60%2C%20which%20for%20a%20count%20of%20households%20equals%20%60n%60).%20Of%20those%20interviewed%2C%20%7B_large%5B'unweighted'%5D%3A%2C.0f%7D%20run%20a%20large-scale%20unit%3A%20%7B_large%5B'unweighted'%5D%20%2F%20_households%5B'unweighted'%5D%3A.1%25%7D%20of%20the%20sample.%20Weighted%2C%20they%20stand%20for%20%7B_large%5B'estimate'%5D%3A%2C.0f%7D%20households%2C%20%7B_large%5B'estimate'%5D%20%2F%20_households%5B'estimate'%5D%3A.1%25%7D%20of%20all%20fishing%20households.%20The%20gap%20is%20the%20design%20at%20work%3A%20stratum%20F%20takes%20every%20registered%20large%20unit%2C%20so%20the%20sample%20holds%20far%20more%20of%20them%20than%20their%20share%2C%20and%20each%20one%20weighs%20little.%0A%0A%20%20%20%20Open%20the%20file%20in%20Excel%20to%20see%20the%20formats%3A%20the%20counts%20with%20thousands%20separators%2C%20the%20mean%20household%20size%20with%20two%20decimals%2C%20the%20CV%20as%20a%20percentage.%20A%20combination%20of%20codes%20and%20categories%20with%20no%20sampled%20household%20gets%20no%20row%2C%20because%20svy%20returns%20none%20for%20it%2C%20as%20the%20check's%20province%20with%20no%20large-scale%20household%20showed%3B%20when%20a%20table's%20shape%20must%20be%20the%20same%20every%20round%2C%20module%206's%20Exercise%206.5%20lays%20the%20estimates%20on%20the%20grid%20of%20every%20category.%0A%0A%20%20%20%20A%20script%20that%20someone%20else%20runs%2C%20perhaps%20on%20a%20schedule%20with%20nobody%20watching%2C%20also%20needs%20a%20contract%3A%20a%20refusal%20when%20a%20region%20is%20missing%2C%20checks%20before%20anything%20is%20estimated%2C%20an%20exit%20code%20for%20each%20outcome%2C%20and%20a%20manifest%20of%20every%20file%20read%20and%20written.%20The%20stretch%20S2%2C%20%5Btabulation%5D(%2F%3Ffile%3D61_stretch_tabulation.py)%2C%20adds%20them%2C%20with%20the%20survey's%20publication%20rule%2C%20which%20flags%20and%20suppresses%20the%20estimates%20whose%20CV%20is%20too%20large.%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%20What%20you%20can%20do%20now%0A%0A%20%20%20%20%7C%20to%20%7C%20use%20%7C%20and%20check%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20put%20every%20level%20in%20one%20table%20%7C%20%60stack_levels%60%3A%20names%20from%20a%20code%20list%20with%20one%20row%20per%20unit%2C%20then%20%60concat%60%20%7C%20%60drop_duplicates%60%20and%20%60validate%3D%22many_to_one%22%60%20before%20naming%20%7C%0A%20%20%20%20%7C%20write%20a%20workbook%20a%20reader%20can%20use%20%7C%20openpyxl%3A%20%60create_sheet%60%2C%20a%20bold%20header%2C%20%60number_format%60%20per%20cell%2C%20%60freeze_panes%60%20%7C%20values%20stay%20numbers%3B%20the%20format%20follows%20the%20indicator%3B%20a%20notes%20sheet%20says%20where%20they%20come%20from%20%7C%0A%20%20%20%20%7C%20write%20a%20workbook%20fast%2C%20with%20formats%20by%20column%20%7C%20xlsxwriter%20through%20%60pd.ExcelWriter(...%2C%20engine%3D%22xlsxwriter%22)%60%20%7C%20it%20writes%20only%3A%20it%20cannot%20open%20a%20workbook%20to%20add%20a%20sheet%20%7C%0A%20%20%20%20%7C%20make%20every%20table%20with%20its%20levels%20stacked%20%7C%20%60table_sheets%60%3A%20%60TABLES%60%20%C3%97%20%60LEVEL_CODES%60%2C%20each%20through%20%60estimate%60%20with%20its%20settings%20fixed%20%7C%20each%20estimate%20has%20the%20sample's%20own%20figure%20and%20count%20beside%20it%20%7C%0A%20%20%20%20%7C%20run%20the%20tabulation%20as%20one%20script%20%7C%20%60tabulate%60%3A%20flat%20table%2C%20variables%2C%20the%20design%2C%20one%20workbook%2C%20written%20out%20by%20%60write_script%60%20%7C%20the%20workbook%20reads%20back%3B%20the%20weighted%20national%20total%20is%20the%20sum%20of%20the%20weights%20%7C%0A%0A%20%20%20%20Next%3A%20the%20stretch%20S2%2C%20%5Btabulation%5D(%2F%3Ffile%3D61_stretch_tabulation.py)%2C%20for%20the%20publication%20rule%2C%20cross%20tables%20and%20the%20script's%20contract%2C%20or%20module%208%2C%20%5B%6070_anomalies_rules.py%60%5D(%2F%3Ffile%3D70_anomalies_rules.py)%2C%20which%20goes%20back%20to%20the%20microdata%20to%20find%20what%20is%20wrong%20before%20anything%20is%20published%3A%20invalid%2C%20implausible%20and%20unusual%20values%2C%20within%20groups%2C%20by%20interviewer.%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%20course.quizzes_at(%227.2%22)%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
5f5315f44be5b3de13f8296a42c4209a