import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22%2C%20app_title%3D%22Stretch%3A%20tabulation%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%20hashlib%0A%20%20%20%20import%20itertools%0A%20%20%20%20import%20logging%0A%20%20%20%20import%20os%0A%20%20%20%20import%20re%0A%20%20%20%20import%20shutil%0A%20%20%20%20import%20subprocess%0A%20%20%20%20import%20sys%0A%20%20%20%20import%20textwrap%0A%20%20%20%20import%20time%0A%20%20%20%20import%20zipfile%0A%20%20%20%20from%20pathlib%20import%20Path%0A%20%20%20%20from%20typing%20import%20Annotated%0A%0A%20%20%20%20import%20duckdb%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%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%20import%20typer%0A%20%20%20%20import%20xlsxwriter%0A%20%20%20%20from%20openpyxl.styles%20import%20Font%0A%20%20%20%20from%20openpyxl.utils%20import%20get_column_letter%0A%20%20%20%20from%20scipy%20import%20special%2C%20stats%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20the%20survey's%20tables%2C%20file%20locations%20and%20exercise%20checks%0A%20%20%20%20from%20course%20import%20NOT_YET%2C%20PATHS%2C%20UNIT_KEY%2C%20attempt%2C%20check_cases%2C%20try_it%2C%20waiting_for%0A%0A%20%20%20%20HOUSEHOLD%20%3D%20%5B%22region_code%22%2C%20%22hh_id%22%5D%20%20%23%20a%20household%3A%20its%20region%20and%20its%20id%2C%20since%20an%20id%20repeats%20across%20regions%0A%20%20%20%20%23%20The%20survey's%20publication%20rule%20(Exercise%20S2.3)%3A%20publish%20up%20to%20a%20CV%20of%2020%25%2C%20caution%20above%20it%20up%20to%2035%25%2C%20suppress%20above%2035%25%20or%20with%20no%20CV%0A%20%20%20%20CAUTION_CV%20%3D%200.20%20%20%23%20above%20this%20CV%20an%20estimate%20is%20flagged%20%22caution%22%3A%20published%2C%20marked%0A%20%20%20%20SUPPRESS_CV%20%3D%200.35%20%20%23%20above%20this%20CV%20(or%20with%20none)%20an%20estimate%20is%20flagged%20%22suppress%22%3A%20neither%20it%2C%20its%20SE%20nor%20its%20interval%20is%20published%0A%20%20%20%20TOTAL%20%3D%20%22Total%22%20%20%23%20the%20label%20of%20a%20margin%3A%20every%20category%20of%20a%20variable%20together%0A%20%20%20%20%23%20The%20two%20categories%20every%20recode%20of%20module%205%20ends%20with%3A%20a%20missing%20answer%2C%20and%20an%20answer%20that%20is%20not%20a%20valid%20code%20or%20value%0A%20%20%20%20NO_ANSWER%2C%20INVALID%20%3D%20%22No%20answer%22%2C%20%22Invalid%20value%22%0A%20%20%20%20SEXES%20%3D%20%5B%22Male%22%2C%20%22Female%22%2C%20INVALID%2C%20NO_ANSWER%5D%0A%20%20%20%20SCALES%20%3D%20%5B%22Small-scale%22%2C%20%22Large-scale%22%2C%20INVALID%2C%20NO_ANSWER%5D%0A%20%20%20%20%23%20How%20the%20cross%20tables'%20workbook%20shows%20a%20cell%3A%20a%20number%20for%20publish%20and%20caution%20(caution%20with%20an%20asterisk%20that%20is%20part%0A%20%20%20%20%23%20of%20the%20number%20format%2C%20so%20the%20cell%20stays%20a%20number)%2C%20and%20a%20symbol%20for%20the%20two%20cells%20that%20have%20no%20number%20to%20show%0A%20%20%20%20NUMBER_FORMAT%20%3D%20%22%23%2C%23%230%22%0A%20%20%20%20CAUTION_FORMAT%20%3D%20'%23%2C%23%230%22*%22'%0A%20%20%20%20SYMBOLS%20%3D%20%7B%22suppress%22%3A%20%22..%22%2C%20%22no%20sample%22%3A%20%22-%22%7D%0A%0A%20%20%20%20%23%20The%20survey%20as%20modules%205%20and%206%20read%20it%0A%20%20%20%20catch%20%3D%20course.child(%22catch%22)%20%20%23%20one%20row%20per%20species%20a%20fishing%20unit%20caught%2C%20keyed%20on%20UNIT_KEY%0A%20%20%20%20species%20%3D%20course.codes(%22species%22)%20%20%23%20the%20species%20code%20list%3A%20code%2C%20name%2C%20group%0A%20%20%20%20provinces%20%3D%20course.codes(%22geography%22)%5B%5B%22region_code%22%2C%20%22province_code%22%2C%20%22province%22%5D%5D.drop_duplicates()%20%20%23%20the%20provinces'%20names%0A%0A%20%20%20%20%23%20The%20tabulation's%20settings%2C%20module%207's%20as%20they%20are%20there%3A%20the%20survey's%20regions%2C%20each%20level's%20code%20columns%2C%20the%0A%20%20%20%20%23%20indicators%20and%20their%20formats%2C%20the%20figures%20each%20estimate%20carries%2C%20and%20the%20tables%20the%20script%20makes%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_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%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%20%23%20What%20each%20exit%20code%20of%20the%20script%20means%3B%20its%20--help%20lists%20them%20from%20here%0A%20%20%20%20EXIT_CODES%20%3D%20%7B%0A%20%20%20%20%20%20%20%200%3A%20%22the%20tables%20and%20the%20manifest%20were%20written%22%2C%0A%20%20%20%20%20%20%20%201%3A%20%22something%20else%20went%20wrong%3B%20the%20log's%20last%20line%20says%20what%22%2C%0A%20%20%20%20%20%20%20%202%3A%20%22an%20option%20is%20wrong%3B%20the%20message%20names%20it%22%2C%0A%20%20%20%20%20%20%20%2065%3A%20%22the%20data%20failed%20a%20check%20before%20publishing%3B%20nothing%20was%20written%22%2C%0A%20%20%20%20%20%20%20%2066%3A%20%22an%20input%20is%20missing%20or%20cannot%20be%20read%3B%20nothing%20was%20written%22%2C%0A%20%20%20%20%7D%0A%20%20%20%20NOTEBOOK%20%3D%20%2261_stretch_tabulation.py%22%20%20%23%20this%20notebook%3A%20the%20script%20is%20written%20from%20its%20functions%0A%20%20%20%20SCRATCH%20%3D%20PATHS.root%20%2F%20%22scratch%22%20%2F%20%22stretch_tabulation%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%20Stretch%3A%20tabulation%0A%0A%20%20%20%20For%20when%20you%20have%20finished%20module%207%20(%6060_publish_tables.py%60)%20and%20want%20more.%20The%20modules%20carry%20a%20survey's%20tabulation%3A%20the%20flat%20table%2C%20the%20variables%2C%20the%20estimates%20with%20their%20SE%2C%20CI%20and%20CV%2C%20the%20sample's%20own%20figures%20beside%20them%2C%20and%20the%20workbook.%20This%20stretch%20holds%20what%20a%20tabulation%20team%20adds%20around%20those%20steps%2C%20in%20four%20parts%2C%20with%20less%20help%20than%20the%20modules%20give%3A%20a%20task%2C%20an%20example%2C%20a%20check.%0A%0A%20%20%20%20%7C%20part%20%7C%20exercises%20%7C%20what%20it%20adds%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%20%C2%B7%20Fishing%20units%20by%20species%20%7C%20S2.1%20%7C%20the%20multiple-response%20table%20of%20module%205%2C%20written%20in%20polars%3A%20a%20unit%20counted%20once%20in%20each%20row%2C%20and%20a%20total%20counted%2C%20not%20summed%20%7C%0A%20%20%20%20%7C%202%20%C2%B7%20How%20precise%2C%20and%20fit%20to%20publish%20%7C%20S2.2%2C%20S2.3%20%7C%20the%20design%20effect%20of%20a%20total%2C%20and%20the%20survey's%20publication%20rule%20on%20the%20CV%3A%20which%20estimates%20a%20table%20publishes%2C%20cautions%20or%20suppresses%20%7C%0A%20%20%20%20%7C%203%20%C2%B7%20Cross%20tables%20%7C%20S2.4%20to%20S2.6%20%7C%20margins%20estimated%20on%20their%20own%2C%20proportion%20intervals%20in%20small%20domains%2C%20and%20the%20cross%20table%20as%20a%20reader%20sees%20it%2C%20in%20a%20second%20workbook%20%7C%0A%20%20%20%20%7C%204%20%C2%B7%20The%20tabulation%20script%20and%20its%20report%20%7C%20S2.7%20to%20S2.11%20%7C%20module%207's%20tabulation%20script%20with%20a%20contract%3A%20its%20estimates%20flagged%20and%20suppressed%20by%20CV%2C%20a%20refusal%20of%20a%20missing%20region%2C%20checks%20before%20anything%20is%20estimated%2C%20an%20exit%20code%20per%20outcome%20and%20a%20manifest%3B%20and%20a%20report%20page%20made%20from%20its%20outputs%20%7C%0A%0A%20%20%20%20The%20parts%20stand%20on%20their%20own%3B%20within%20a%20part%2C%20an%20exercise%20can%20use%20the%20one%20before%20it.%0A%0A%20%20%20%20**Where%20this%20notebook%20writes.**%20Every%20file%20it%20makes%20goes%20into%20%60scratch%2Fstretch_tabulation%2F%60%20in%20the%20course%20folder.%20Nothing%20is%20written%20into%20%60data%2F%60%2C%20which%20holds%20the%20survey%20as%20it%20arrived.%0A%0A%20%20%20%20Exercises%20work%20as%20in%20the%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%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%201%20%C2%B7%20Fishing%20units%20by%20species%3A%20one%20unit%2C%20many%20species%0A%0A%20%20%20%20One%20of%20a%20fishing%20survey's%20usual%20tables%20counts%20fishing%20units%20by%20the%20species%20they%20caught%3A%20the%20top%20eight%20species%2C%20and%20the%20rest%20as%20%22Other%22.%20Module%205%20labeled%20each%20catch%20row%20with%20its%20species%20(Exercise%205.4)%20and%20drew%20the%20table%3B%20here%20you%20count%20it.%0A%0A%20%20%20%20**The%20problem.**%20The%20species%20table%20counts%20units%2C%20and%20a%20unit%20catches%20several%20species%2C%20so%20it%20belongs%20to%20several%20rows%20of%20the%20table.%20That%20is%20a%20**multiple-response**%20table%3A%20its%20rows%20add%20up%20to%20more%20than%20the%20number%20of%20units%2C%20and%20its%20total%20row%20is%20not%20the%20sum%20of%20the%20rows%20above%20it.%20Two%20traps%20sit%20in%20it%3A%0A%0A%20%20%20%20-%20A%20unit%20that%20caught%20two%20species%20pooled%20into%20%22Other%22%20has%20two%20catch%20rows%20labeled%20%22Other%22.%20Counting%20rows%20counts%20it%20twice%20in%20%22Other%22%3B%20counting%20distinct%20units%20counts%20it%20once.%0A%20%20%20%20-%20The%20**Total**%20is%20the%20number%20of%20units%20that%20caught%20anything%3A%20distinct%20units%20over%20the%20whole%20table%2C%20which%20is%20fewer%20than%20the%20sum%20of%20the%20rows.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20top_species(catch%3A%20pd.DataFrame%2C%20species%3A%20pd.DataFrame%2C%20n%3A%20int)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Module%205's%20Exercise%205.4%3A%20each%20catch%20row's%20species%20name%20if%20it%20is%20one%20of%20the%20n%20named%20species%20caught%20most%20(ties%3A%0A%20%20%20%20smaller%20code%20first)%2C%20else%20Other%3B%20No%20answer%20for%20a%20missing%20code%20and%20Invalid%20value%20for%20a%20code%20not%20in%20the%20list.%22%22%22%0A%20%20%20%20codes%20%3D%20catch%5B%22catch_species%22%5D%0A%20%20%20%20names%20%3D%20codes.map(species.set_index(%22code%22)%5B%22name%22%5D)%0A%20%20%20%20may_rank%20%3D%20codes%5Bcodes.isin(species.loc%5Bspecies%5B%22group%22%5D%20!%3D%20%22Other%22%2C%20%22code%22%5D)%5D%0A%20%20%20%20counts%20%3D%20may_rank.value_counts().rename_axis(%22code%22).reset_index()%0A%20%20%20%20top%20%3D%20counts.sort_values(%5B%22count%22%2C%20%22code%22%5D%2C%20ascending%3D%5BFalse%2C%20True%5D)%5B%22code%22%5D.head(n)%0A%20%20%20%20labels%20%3D%20names.where(codes.isin(top)%2C%20%22Other%22)%0A%20%20%20%20return%20labels.mask(codes.notna()%20%26%20names.isna()%2C%20INVALID).mask(codes.isna()%2C%20NO_ANSWER)%0A%0A%0A%40app.function%0Adef%20catch_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Module%205's%20thirteen%20catch%20rows%2C%20used%20by%20the%20check%20of%20Exercise%20S2.1%3A%20three%20units%2C%20the%20same%20household%20id%20in%20two%0A%20%20%20%20regions%2C%20Not%20identified%20caught%20most%2C%20a%20code%20not%20in%20the%20list%2C%20a%20missing%20code%2C%20and%20a%20row%20entered%20twice.%22%22%22%0A%20%20%20%20table%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2201%22%5D%20*%206%20%2B%20%5B%2204%22%5D%20*%206%2C%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B5101%2C%205101%2C%205101%2C%205101%2C%205102%2C%205102%2C%205101%2C%205101%2C%205101%2C%205101%2C%205101%2C%205101%5D%2C%0A%20%20%20%20%20%20%20%20%22operator_line%22%3A%20%5B1%5D%20*%204%20%2B%20%5B2%5D%20*%202%20%2B%20%5B1%5D%20*%206%2C%0A%20%20%20%20%20%20%20%20%22unit_no%22%3A%20%5B1%5D%20*%2012%2C%0A%20%20%20%20%20%20%20%20%22catch_species%22%3A%20%5B304%2C%20203%2C%20998%2C%20101%2C%20998%2C%20998%2C%20203%2C%20101%2C%20998%2C%20150%2C%20None%2C%20304%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20table.loc%5B12%5D%20%3D%20table.loc%5B0%5D%20%20%23%20the%20first%20row%20entered%20twice%0A%20%20%20%20return%20table.astype(%7B%22catch_species%22%3A%20%22Int32%22%7D)%0A%0A%0A%40app.function%0Adef%20as_rows(result%2C%20keys%2C%20digits%3A%20int%20%3D%204)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20result%20table%20in%20one%20shape%20for%20the%20checks%3A%20a%20polars%20table%20as%20pandas%2C%20its%20keys%20as%20columns%2C%20rows%20sorted%20by%20them%2C%0A%20%20%20%20columns%20in%20name%20order%2C%20numbers%20rounded.%20So%20a%20correct%20answer%20passes%20whatever%20its%20row%20order%20or%20index.%22%22%22%0A%20%20%20%20table%20%3D%20result.to_pandas()%20if%20type(result).__module__.startswith(%22polars%22)%20else%20result%0A%20%20%20%20table%20%3D%20table.to_frame()%20if%20isinstance(table%2C%20pd.Series)%20else%20table%0A%20%20%20%20keys%20%3D%20%5Bkeys%5D%20if%20isinstance(keys%2C%20str)%20else%20list(keys)%0A%20%20%20%20if%20any(name%20in%20keys%20for%20name%20in%20table.index.names)%3A%0A%20%20%20%20%20%20%20%20table%20%3D%20table.reset_index()%0A%20%20%20%20table%20%3D%20table.sort_values(keys%2C%20na_position%3D%22last%22).reset_index(drop%3DTrue)%5Bsorted(table.columns)%5D%0A%20%20%20%20numbers%20%3D%20table.select_dtypes(%22number%22).columns%0A%20%20%20%20table%5Bnumbers%5D%20%3D%20table%5Bnumbers%5D.astype(%22float64%22).round(digits)%0A%20%20%20%20return%20table%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20survey's%20catch%2C%20labeled%20as%20module%205%20labels%20it%3A%20the%20top%20eight%20species%2C%20Other%2C%20and%20the%20two%20catch-alls%0A%20%20%20%20species_catch%20%3D%20pl.from_pandas(catch%5BUNIT_KEY%5D.assign(species_label%3Dtop_species(catch%2C%20species%2C%208)))%0A%20%20%20%20return%20(species_catch%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%20S2.1%20%C2%B7%20Fishing%20units%20by%20species%20label%2C%20in%20polars%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20fishing%20units%20by%20the%20top%20eight%20species%20is%20a%20table%20every%20round%20of%20the%20survey%20publishes.%20Its%20%22Other%22%20row%20and%20its%20total%20are%20where%20a%20row%20count%20goes%20wrong%2C%20and%20the%20error%20is%20invisible%20in%20the%20table%3A%20every%20number%20looks%20plausible.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20**polars**%20table%20of%20catch%20rows%20with%20the%20unit's%20key%20(%60UNIT_KEY%60%3A%20%60region_code%60%2C%20%60hh_id%60%2C%20%60operator_line%60%2C%20%60unit_no%60)%20and%20a%20%60species_label%60%20column%20%E2%86%92%20a%20polars%20table%20with%20one%20row%20per%20label%20and%20a%20last%20row%20%60%22Total%22%60%3A%0A%0A%20%20%20%20%7C%20species_label%20%7C%20units%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%20Other%20%7C%20units%20that%20caught%20at%20least%20one%20species%20labeled%20Other%20%7C%0A%20%20%20%20%7C%20Squids%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20Total%20%7C%20units%20that%20caught%20anything%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60group_by(...).agg(pl.struct(UNIT_KEY).n_unique())%60%2C%20%60select(...).item()%60%2C%20%60pl.concat%60%20with%20%60how%3D%22vertical_relaxed%22%60%20(which%20lets%20the%20total's%20whole%20number%20join%20the%20counts'%20column)%2C%20from%20module%205%2C%20section%202.%20The%20row%20order%20does%20not%20matter.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20units_by_label(catch%3A%20pl.DataFrame)%20-%3E%20pl.DataFrame%3A%0A%20%20%20%20%22%22%22Distinct%20fishing%20units%20per%20species_label%2C%20and%20a%20last%20row%20Total%3A%20the%20distinct%20units%20in%20the%20whole%20table.%22%22%22%0A%20%20%20%20%23%20Exercise%20S2.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20per%20species_label%2C%20the%20number%20of%20distinct%20units%3A%20group_by%2C%20then%20pl.struct(UNIT_KEY).n_unique()%0A%20%20%20%20%23%20Step%202%3A%20the%20total%3A%20distinct%20units%20in%20the%20whole%20table%2C%20as%20a%20one-row%20table%20with%20species_label%20%22Total%22%0A%20%20%20%20%23%20Step%203%3A%20stack%20the%20two%20(pl.concat%2C%20how%3D%22vertical_relaxed%22)%20and%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S2.1%22)%0A%0A%0A%40app.function%0Adef%20group_labels(catch%3A%20pd.DataFrame)%20-%3E%20pl.DataFrame%3A%0A%20%20%20%20%22%22%22Catch%20rows%20labeled%20by%20their%20species'%20group%20(Small%20pelagic%20fish%2C%20Crustaceans...)%2C%20as%20a%20polars%20table%2C%20for%20the%20checks%20of%20Exercise%20S2.1.%22%22%22%0A%20%20%20%20groups%20%3D%20catch%5B%22catch_species%22%5D.map(species.set_index(%22code%22)%5B%22group%22%5D).fillna(INVALID)%0A%20%20%20%20return%20pl.from_pandas(catch%5BUNIT_KEY%5D.assign(species_label%3Dgroups))%0A%0A%0A%40app.function%0Adef%20label_rows(table)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Exercise%20S2.1's%20result%20in%20one%20shape%20for%20the%20checks%3A%20as_rows%20by%20species_label.%22%22%22%0A%20%20%20%20return%20as_rows(table%2C%20%22species_label%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_fixture%20%3D%20catch_fixture()%0A%20%20%20%20_fixture_labels%20%3D%20pl.from_pandas(_fixture%5BUNIT_KEY%5D.assign(%0A%20%20%20%20%20%20%20%20species_label%3D%5B%22Threadfin%20breams%22%2C%20%22Other%22%2C%20%22Other%22%2C%20%22Other%22%2C%20%22Threadfin%20breams%22%2C%20%22Other%22%2C%20%22Other%22%2C%20%22Other%22%2C%20%22Other%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%20INVALID%2C%20NO_ANSWER%2C%20%22Threadfin%20breams%22%2C%20%22Threadfin%20breams%22%5D))%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S2.1%22%2C%20units_by_label%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22thirteen%20catch%20rows%20of%20three%20units%3A%20several%20Other%20rows%20per%20unit%2C%20a%20row%20entered%20twice%2C%20the%20same%20id%20in%20two%20regions%22%2C%20(_fixture_labels%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20catch%20by%20species%20group%22%2C%20(group_labels(catch)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20catch%20of%20region%2001%20by%20species%20group%22%2C%20(group_labels(catch%5Bcatch%5B%22region_code%22%5D%20%3D%3D%20%2201%22%5D)%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22catch%20tables%22%2C%20summarize%3Dlabel_rows%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Count%20distinct%20units%20(pl.struct%20of%20UNIT_KEY%2C%20n_unique)%20per%20label%2C%20and%20once%20more%20over%20the%20whole%20table%20for%20Total.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(species_catch)%3A%0A%20%20%20%20%23%20Try%20it%3A%20units%20by%20the%20top%20eight%20species%2C%20largest%20first%0A%20%20%20%20def%20_largest_first(table)%3A%0A%20%20%20%20%20%20%20%20return%20units_by_label(table).sort(%22units%22%2C%20descending%3DTrue)%0A%0A%20%20%20%20try_it(_largest_first%2C%20species_catch)%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%20S2.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%20The%20fixture's%20%60Other%60%20is%207%2C%20not%203%20%7C%20Catch%20rows%20counted%2C%20not%20units.%20%7C%20%60pl.struct(UNIT_KEY).n_unique()%60%20%7C%0A%20%20%20%20%7C%20%60Total%60%20is%20larger%20than%20any%20count%20and%20close%20to%20their%20sum%20%7C%20The%20total%20summed%20from%20the%20rows%3A%20a%20unit%20is%20in%20several%20rows.%20%7C%20count%20the%20whole%20table%20again%20%7C%0A%20%20%20%20%7C%20The%20fixture's%20%60Total%60%20is%202%2C%20not%203%20%7C%20The%20key%20left%20out%20%60region_code%60%3A%20two%20regions'%20household%205101%20merged.%20%7C%20the%20whole%20%60UNIT_KEY%60%20%7C%0A%20%20%20%20%7C%20No%20%60Total%60%20row%20%7C%20The%20total%20was%20computed%20but%20not%20stacked.%20%7C%20%60pl.concat(%5B...%5D)%60%20%7C%0A%20%20%20%20%7C%20*SchemaError*%20or%20*type%20...%20is%20incompatible*%20%7C%20The%20two%20tables'%20count%20columns%20have%20different%20types.%20%7C%20%60how%3D%22vertical_relaxed%22%60%20%7C%0A%20%20%20%20%7C%20*AttributeError%3A%20'DataFrame'%20object%20has%20no%20attribute%20'group_by'*%20%7C%20Given%20a%20pandas%20table%3A%20the%20input%20is%20polars.%20%7C%20%60pl.from_pandas(...)%60%20before%20calling%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%20The%20table%20counts%20the%20units%20interviewed%2C%20unweighted.%20Its%20rows%20add%20up%20to%20more%20than%20its%20total%2C%20so%20the%20published%20table%20carries%20a%20note%3A%20a%20unit%20may%20report%20more%20than%20one%20species.%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%20How%20precise%2C%20and%20fit%20to%20publish%0A%0A%20%20%20%20Module%206%20gave%20every%20estimate%20its%20SE%2C%20CV%20and%20confidence%20interval.%20Two%20questions%20follow%2C%20and%20a%20helper%20with%20its%20settings%20fixed%20(%60deff%20%3D%20FALSE%60%2C%20and%20every%20CV%20printed)%20leaves%20both%20out%3A%20how%20much%20precision%20the%20design%20costs%20against%20a%20simple%20random%20sample%2C%20and%20which%20estimates%20a%20table%20should%20publish%20at%20all.%0A%0A%20%20%20%20**The%20data.**%20One%20row%20per%20household%2C%20as%20module%206%20estimates%20from%3A%20%60hh_frame%60%2C%20the%20households%20with%20their%20design%20columns%2C%20module%204's%20two%20design%20findings%20applied%20(the%20copied%20households%20left%20out%2C%20the%20misfiled%20ones%20back%20in%20their%20area's%20stratum)%2C%20and%20what%20each%20household%20runs%3B%20%60hh_sample%60%2C%20the%20same%20as%20svy%20reads%20it.%20The%20functions%20below%20are%20module%206's%2C%20given%20here.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20survey_design(weight%3A%20str%20%3D%20%22sel_final_weight%22)%3A%0A%20%20%20%20%22%22%22Module%206's%20design%3A%20province%20by%20stratum%2C%20the%20enumeration%20area%20(or%2C%20in%20stratum%20F%2C%20the%20household)%20as%20the%20sampling%0A%20%20%20%20unit%2C%20a%20weight%2C%20and%20the%20strata's%20counts%20of%20sampling%20units%20as%20the%20fpc.%20A%20new%20Design%20each%20call%3A%20in%20svy%200.36.0%20a%0A%20%20%20%20sample's%20%60update_design%60%20method%20also%20changes%20the%20sample%20it%20is%20called%20on%2C%20so%20the%20cells%20below%20build%20a%20new%20sample%20from%0A%20%20%20%20this%20function%20instead%20of%20updating%20a%20shared%20one.%22%22%22%0A%20%20%20%20return%20svy.Design(stratum%3D%22stratum_id%22%2C%20psu%3D%22psu_id%22%2C%20wgt%3Dweight%2C%20pop_size%3D%22psus_in_stratum%22%2C%20singleton%3D%22skip%22)%0A%0A%0A%40app.function%0Adef%20taylor_variance(frame%3A%20pd.DataFrame%2C%20z)%20-%3E%20float%3A%0A%20%20%20%20%22%22%22The%20variance%20of%20the%20total%20of%20z%20under%20the%20design%3A%20z%20added%20up%20per%20sampling%20unit%2C%20the%20units'%20totals'%20spread%20within%0A%20%20%20%20each%20stratum%2C%20times%20n%2F(n-1)%20and%20the%20fpc%2C%20added%20over%20strata.%20z%20has%20one%20value%20per%20row%20of%20the%20frame.%20A%20frame%20without%0A%20%20%20%20psus_in_stratum%20is%20taken%20as%20drawn%20with%20replacement%20(no%20fpc)%3B%20a%20stratum%20with%20one%20sampling%20unit%20adds%20nothing%20(the%20skip%0A%20%20%20%20rule).%22%22%22%0A%20%20%20%20rows%20%3D%20frame%5B%5B%22stratum_id%22%2C%20%22psu_id%22%5D%5D.assign(z%3Dnp.asarray(z%2C%20dtype%3Dfloat))%0A%20%20%20%20rows%5B%22N%22%5D%20%3D%20frame%5B%22psus_in_stratum%22%5D.to_numpy()%20if%20%22psus_in_stratum%22%20in%20frame%20else%20np.inf%0A%20%20%20%20psu%20%3D%20rows.groupby(%5B%22stratum_id%22%2C%20%22psu_id%22%5D).agg(t%3D(%22z%22%2C%20%22sum%22)%2C%20N%3D(%22N%22%2C%20%22first%22)).reset_index()%0A%20%20%20%20stratum%20%3D%20psu.groupby(%22stratum_id%22)%5B%22t%22%5D%0A%20%20%20%20n%20%3D%20stratum.transform(%22size%22)%0A%20%20%20%20terms%20%3D%20(1%20-%20n%20%2F%20psu%5B%22N%22%5D)%20*%20n%20%2F%20(n%20-%201)%20*%20(psu%5B%22t%22%5D%20-%20stratum.transform(%22mean%22))%20**%202%0A%20%20%20%20return%20float(terms%5Bn%20%3E%201%5D.sum())%0A%0A%0A%40app.function%0Adef%20estimation_frame(households%3A%20pd.DataFrame%2C%20defects%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Module%206's%20frame%3A%20one%20row%20per%20household%20for%20estimation%2C%20the%20design's%20columns%2C%20the%20province's%20name%2C%20and%20what%20the%0A%20%20%20%20household%20runs%2C%20counted%20from%20the%20fishing%20units%2C%20members%20and%20boats%20tables.%20Module%204's%20design%20findings%20are%20applied%3A%0A%20%20%20%20the%20copied%20households%20are%20left%20out%2C%20and%20a%20household%20filed%20under%20another%20stratum%20than%20its%20area's%20takes%20its%20area's%0A%20%20%20%20design%20columns.%22%22%22%0A%20%20%20%20key%20%3D%20%5B%22region_code%22%2C%20%22hh_id%22%5D%0A%20%20%20%20copies%20%3D%20defects.loc%5Bdefects%5B%22kind%22%5D%20%3D%3D%20%22household_copied%22%2C%20key%5D%0A%20%20%20%20kept%20%3D%20households.merge(copies%2C%20on%3Dkey%2C%20how%3D%22left%22%2C%20indicator%3DTrue)%0A%20%20%20%20kept%20%3D%20kept%5Bkept%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%23%20The%20misfiled%20household%20takes%20the%20stratum%2C%20PSU%20and%20count%20of%20its%20area's%20other%20households%0A%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%20design%20%3D%20%5B%22sel_stratum%22%2C%20%22stratum_id%22%2C%20%22psu_id%22%2C%20%22psus_in_stratum%22%5D%0A%20%20%20%20misfiled%20%3D%20kept%5Bkey%5D.merge(defects.loc%5Bdefects%5B%22kind%22%5D%20%3D%3D%20%22area_in_two_strata%22%2C%20key%5D.assign(misfiled%3DTrue)%2C%20on%3Dkey%2C%20how%3D%22left%22)%5B%22misfiled%22%5D.eq(True).to_numpy()%0A%20%20%20%20others%20%3D%20kept.loc%5B~misfiled%2C%20%5B*area%2C%20*design%5D%5D.drop_duplicates(area)%0A%20%20%20%20kept.loc%5Bmisfiled%2C%20design%5D%20%3D%20kept.loc%5Bmisfiled%2C%20area%5D.merge(others%2C%20on%3Darea%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%5Bdesign%5D.to_numpy()%0A%20%20%20%20units%20%3D%20course.units()%0A%20%20%20%20units%20%3D%20units.assign(large%3D(units%5B%22unit_scale%22%5D%20%3D%3D%202).astype(int))%0A%20%20%20%20counts%20%3D%20%5B%0A%20%20%20%20%20%20%20%20units.groupby(key).agg(units%3D(%22unit_no%22%2C%20%22size%22)%2C%20large_units%3D(%22large%22%2C%20%22sum%22)).reset_index()%2C%0A%20%20%20%20%20%20%20%20units%5Bunits%5B%22large%22%5D%20%3D%3D%201%5D.groupby(key).agg(large_unit_operators%3D(%22operator_line%22%2C%20%22nunique%22)).reset_index()%2C%0A%20%20%20%20%20%20%20%20course.members().groupby(key).size().rename(%22hh_size%22).reset_index()%2C%0A%20%20%20%20%20%20%20%20course.child(%22boat%22).groupby(key).size().rename(%22n_boats%22).reset_index()%2C%0A%20%20%20%20%5D%0A%20%20%20%20frame%20%3D%20kept.merge(provinces%2C%20on%3D%5B%22region_code%22%2C%20%22province_code%22%5D%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20for%20table%20in%20counts%3A%0A%20%20%20%20%20%20%20%20frame%20%3D%20frame.merge(table%2C%20on%3Dkey%2C%20how%3D%22left%22%2C%20validate%3D%22one_to_one%22)%0A%20%20%20%20numbers%20%3D%20%5B%22units%22%2C%20%22large_units%22%2C%20%22large_unit_operators%22%2C%20%22hh_size%22%2C%20%22n_boats%22%5D%0A%20%20%20%20frame%5Bnumbers%5D%20%3D%20frame%5Bnumbers%5D.fillna(0).astype(%22int64%22)%20%20%23%20a%20household%20with%20no%20boat%20has%20no%20row%20in%20the%20boats%20table%0A%20%20%20%20frame%5B%22small_units%22%5D%20%3D%20frame%5B%22units%22%5D%20-%20frame%5B%22large_units%22%5D%0A%20%20%20%20frame%5B%22runs_large_unit%22%5D%20%3D%20(frame%5B%22large_units%22%5D%20%3E%200).astype(%22int64%22)%0A%20%20%20%20frame%5B%22hh_count%22%5D%20%3D%201%0A%20%20%20%20columns%20%3D%20%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22province%22%2C%20%22sel_stratum%22%2C%20%22sel_ea%22%2C%20%22stratum_id%22%2C%20%22psu_id%22%2C%20%22psus_in_stratum%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22sel_design_weight%22%2C%20%22sel_final_weight%22%2C%20%22hh_count%22%2C%20%22hh_size%22%2C%20%22units%22%2C%20%22large_units%22%2C%20%22small_units%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22large_unit_operators%22%2C%20%22runs_large_unit%22%2C%20%22n_boats%22%5D%0A%20%20%20%20return%20frame%5Bcolumns%5D.astype(%7B%22psus_in_stratum%22%3A%20%22int64%22%7D).reset_index(drop%3DTrue)%0A%0A%0A%40app.function%0Adef%20small_design()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Module%206's%20small%20design%20of%20Exercise%206.3%2C%20used%20here%20by%20the%20check%20of%20Exercise%20S2.2%3A%20a%20take-all%20stratum%20T%20of%20three%20households%2C%0A%20%20%20%20each%20its%20own%20sampling%20unit%2C%20and%20a%20stratum%20S%20of%203%20units%20out%20of%206%2C%20units%20of%20one%20to%20three%20households.%20Columns%20y%20and%20x%0A%20%20%20%20are%20values%2C%20d%20a%20domain%2C%20province%20two%20provinces%20across%20the%20strata.%22%22%22%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%23%20stratum_id%2C%20psu_id%2C%20psus_in_stratum%2C%20sel_final_weight%2C%20y%2C%20x%2C%20d%2C%20province%0A%20%20%20%20%20%20%20%20(%22T%22%2C%20%22T-h1%22%2C%203%2C%201.0%2C%202.0%2C%201.0%2C%20True%2C%20%22P%22)%2C%0A%20%20%20%20%20%20%20%20(%22T%22%2C%20%22T-h2%22%2C%203%2C%201.0%2C%205.0%2C%202.0%2C%20False%2C%20%22Q%22)%2C%0A%20%20%20%20%20%20%20%20(%22T%22%2C%20%22T-h3%22%2C%203%2C%201.0%2C%201.0%2C%201.0%2C%20True%2C%20%22Q%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s1%22%2C%206%2C%204.0%2C%201.0%2C%202.0%2C%20True%2C%20%22P%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s1%22%2C%206%2C%204.0%2C%200.0%2C%201.0%2C%20False%2C%20%22P%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s2%22%2C%206%2C%204.0%2C%203.0%2C%202.0%2C%20False%2C%20%22Q%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s3%22%2C%206%2C%204.0%2C%201.0%2C%203.0%2C%20True%2C%20%22P%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s3%22%2C%206%2C%204.0%2C%201.0%2C%201.0%2C%20False%2C%20%22Q%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s3%22%2C%206%2C%204.0%2C%202.0%2C%202.0%2C%20True%2C%20%22Q%22)%2C%0A%20%20%20%20%5D%0A%20%20%20%20return%20pd.DataFrame(rows%2C%20columns%3D%5B%22stratum_id%22%2C%20%22psu_id%22%2C%20%22psus_in_stratum%22%2C%20%22sel_final_weight%22%2C%20%22y%22%2C%20%22x%22%2C%20%22d%22%2C%20%22province%22%5D)%0A%0A%0A%40app.function%0Adef%20as_values(result%2C%20digits%3A%20int%20%3D%204)%20-%3E%20list%3A%0A%20%20%20%20%22%22%22A%20column's%20values%20in%20order%2C%20rounded%2C%20for%20the%20checks%3A%20its%20name%20and%20index%20don't%20count.%22%22%22%0A%20%20%20%20return%20%5BNone%20if%20pd.isna(value)%20else%20(value%20if%20isinstance(value%2C%20str)%20else%20round(float(value)%2C%20digits))%20for%20value%20in%20pd.Series(result).tolist()%5D%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20households%20module%206%20estimates%20from%2C%20module%204's%20design%20findings%20applied%2C%20and%20the%20svy%20sample%0A%20%20%20%20hh_frame%20%3D%20estimation_frame(course.design_frame()%2C%20course.planted_defects())%0A%20%20%20%20hh_sample%20%3D%20svy.Sample(data%3Dpl.from_pandas(hh_frame)%2C%20design%3Dsurvey_design())%0A%20%20%20%20mo.md(f%22%60hh_frame%60%3A%20**%7Blen(hh_frame)%3A%2C%7D%20households**%2C%20%7Bhh_frame%5B'stratum_id'%5D.nunique()%7D%20strata%2C%20%7Bhh_frame%5B'psu_id'%5D.nunique()%3A%2C%7D%20sampling%20units.%22)%0A%20%20%20%20return%20hh_frame%2C%20hh_sample%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%20design%20effect%0A%0A%20%20%20%20**The%20problem.**%20Is%20the%20design%20costing%20precision%2C%20and%20how%20much%3F%20The%20**design%20effect**%20(deff)%20compares%20the%20design's%20variance%20with%20the%20variance%20a%20simple%20random%20sample%20of%20the%20same%20number%20of%20households%20would%20have%20had%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20deff%20%3D%20V_design%20%2F%20V_SRS%0A%20%20%20%20V_SRS%2C%20for%20a%20total%20%3D%20N%CC%82%C2%B2%20%C2%B7%20(1%20%E2%88%92%20n%20%2F%20N%CC%82)%20%C2%B7%20s%C2%B2%20%2F%20n%0A%20%20%20%20n%20%20%20%20%20%20%3D%20households%20with%20a%20positive%20weight%0A%20%20%20%20N%CC%82%20%20%20%20%20%20%3D%20%CE%A3%20w%0A%20%20%20%20s%C2%B2%20%20%20%20%20%3D%20n%20%2F%20(n%20%E2%88%92%201)%20%C2%B7%20%CE%A3%20w%20(y%20%E2%88%92%20%C8%B3)%C2%B2%20%2F%20%CE%A3%20w%20%20%20%20%20the%20weighted%20variance%20of%20y%2C%20%C8%B3%20the%20weighted%20mean%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20A%20deff%20of%202%20says%20the%20design%20needs%20twice%20the%20sample%20a%20simple%20random%20sample%20would%20for%20the%20same%20precision%3B%20%60n%20%2F%20deff%60%20is%20the%20*effective%20sample%20size*.%20A%20deff%20below%201%20says%20the%20design%20does%20better%2C%20which%20stratification%20can%20do.%20svy%20adds%20a%20%60deff%60%20column%20with%20%60deff%3D%22wor%22%60%20(against%20a%20simple%20random%20sample%20without%20replacement%2C%20Kish's%20definition)%20or%20%60deff%3D%22wr%22%60%20(with%20replacement).%20ReGenesees'%20%60svystatTM%60%20makes%20the%20same%20choice%20with%20%60deff%20%3D%20TRUE%60%20and%20%60deff%20%3D%20%22replace%22%60%20(its%20reference%20page%2C%20checked%202026-11-20).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(hh_sample)%3A%0A%20%20%20%20%23%20svy's%20design%20effects%20for%20a%20total%20and%20for%20a%20mean%20of%20the%20same%20column%0A%20%20%20%20pl.concat(%5B%0A%20%20%20%20%20%20%20%20hh_sample.estimation.total(y%3D%22units%22%2C%20deff%3D%22wor%22).to_polars().select(pl.lit(%22total%20of%20fishing%20units%22).alias(%22estimate%22)%2C%20%22est%22%2C%20%22se%22%2C%20%22deff%22)%2C%0A%20%20%20%20%20%20%20%20hh_sample.estimation.mean(y%3D%22units%22%2C%20deff%3D%22wor%22).to_polars().select(pl.lit(%22mean%20units%20per%20household%22).alias(%22estimate%22)%2C%20%22est%22%2C%20%22se%22%2C%20%22deff%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(hh_frame)%3A%0A%20%20%20%20_one%20%3D%20(hh_frame%5B%22units%22%5D%20%3D%3D%201).mean()%0A%20%20%20%20_areas%20%3D%20hh_frame%5Bhh_frame%5B%22sel_stratum%22%5D%20%3D%3D%20%22C%22%5D.groupby(%22psu_id%22).size()%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20The%20same%20column%2C%20two%20deffs.%20%7B_one%3A.0%25%7D%20of%20the%20households%20run%20one%20unit%2C%20so%20the%20mean%20barely%20varies%20from%20area%20to%20area.%20The%20**total**%20varies%20with%20how%20many%20households%20each%20drawn%20area%20holds%20(%7B_areas.min()%7D%20to%20%7B_areas.max()%7D%20in%20stratum%20C%2C%20once%20the%20removed%20households%20are%20left%20out)%2C%20and%20a%20simple%20random%20sample%20of%20households%20has%20no%20such%20variation.%20A%20large%20deff%20for%20a%20total%20of%20a%20near-constant%20count%20is%20the%20clustering%20at%20work%2C%20not%20a%20fault%20in%20the%20data.%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%20S2.2%20%C2%B7%20The%20design%20effect%20of%20a%20total%2C%20from%20the%20formula%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20deff%20is%20how%20a%20survey's%20precision%20is%20compared%20across%20items%20and%20rounds%2C%20and%20how%20the%20next%20round's%20sample%20size%20is%20planned.%20Computing%20it%20yourself%20shows%20what%20it%20compares%20with%20what.%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_fixture%20%3D%20svy.Sample(data%3Dpl.from_pandas(small_design())%2C%20design%3Dsurvey_design()).estimation.total(y%3D%22y%22%2C%20deff%3D%22wor%22).to_polars()%5B%22deff%22%5D%5B0%5D%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20frame%20and%20the%20name%20of%20a%20column%20%60y%60%20%E2%86%92%20one%20number%2C%20the%20deff%20of%20the%20total%20of%20y%2C%20by%20the%20formula%20above.%20On%20the%20small%20design%20of%20module%206's%20Exercise%206.3%2C%20%60small_design()%60%2C%20svy%20gives%20%7B_fixture%3A.4f%7D.%0A%0A%20%20%20%20**Tools%3A**%20%60taylor_variance(frame%2C%20w%20*%20y)%60%20for%20the%20design's%20variance%2C%20the%20formula%20above%20for%20the%20simple%20random%20sample's.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20design_effect(frame%3A%20pd.DataFrame%2C%20y%3A%20str)%20-%3E%20float%3A%0A%20%20%20%20%22%22%22The%20design%20effect%20of%20the%20total%20of%20y%3A%20the%20design's%20variance%20over%20a%20simple%20random%20sample's%20without%20replacement.%22%22%22%0A%20%20%20%20%23%20Exercise%20S2.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20w%2C%20y%2C%20and%20the%20rows%20with%20a%20positive%20weight%3A%20n%20(their%20number)%20and%20N%20%3D%20the%20sum%20of%20their%20weights%0A%20%20%20%20%23%20Step%202%3A%20%C8%B3%20%3D%20the%20weighted%20mean%3B%20s%C2%B2%20%3D%20n%2F(n%20%E2%88%92%201)%20%C2%B7%20%CE%A3%20w(y%20%E2%88%92%20%C8%B3)%C2%B2%20%2F%20N%0A%20%20%20%20%23%20Step%203%3A%20V_SRS%20%3D%20N%C2%B2%20%C2%B7%20(1%20%E2%88%92%20n%2FN)%20%C2%B7%20s%C2%B2%20%2F%20n%0A%20%20%20%20%23%20Step%204%3A%20V_design%20%3D%20taylor_variance(frame%2C%20w%20*%20y)%3B%20return%20V_design%20%2F%20V_SRS%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S2.2%22)%0A%0A%0A%40app.function%0Adef%20rounded(number)%20-%3E%20float%3A%0A%20%20%20%20%22%22%22One%20number%2C%20rounded%20to%204%20places%2C%20for%20the%20checks.%22%22%22%0A%20%20%20%20return%20round(float(number)%2C%204)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(hh_frame)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S2.2%22%2C%20design_effect%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20small%20design's%20y%22%2C%20(small_design()%2C%20%22y%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20fishing%20units%22%2C%20(hh_frame%2C%20%22units%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20operators%20of%20large-scale%20units%22%2C%20(hh_frame%2C%20%22large_unit_operators%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20boats%20in%20region%2007%22%2C%20(hh_frame%5Bhh_frame%5B%22region_code%22%5D%20%3D%3D%20%2207%22%5D%2C%20%22n_boats%22))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22frames%20and%20columns%22%2C%20summarize%3Drounded%2C%0A%20%20%20%20%20%20%20%20hint%3D%22V_design%20%3D%20taylor_variance(frame%2C%20w*y)%3B%20V_SRS%20%3D%20N%C2%B2%20(1%20%E2%88%92%20n%2FN)%20s%C2%B2%2Fn%20with%20the%20weighted%20s%C2%B2%2C%20n%2F(n%E2%88%921)%20included.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(hh_frame)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20deff%20of%20the%20total%20of%20fishing%20units%3B%20svy's%20is%20in%20the%20table%20above%0A%20%20%20%20try_it(design_effect%2C%20hh_frame%2C%20%22units%22)%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%20S2.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%20Close%2C%20but%20not%20equal%20on%20the%20survey%20%7C%20%60(1%20%E2%88%92%20n%2FN)%60%20left%20out%3A%20that%20is%20%60deff%3D%22wr%22%60.%20%7C%20%60(1%20-%20n%20%2F%20N)%60%20%7C%0A%20%20%20%20%7C%20Far%20off%20on%20the%20survey%2C%20close%20on%20the%20small%20design%20%7C%20The%20unweighted%20variance%20of%20y.%20%7C%20%60s2%60%20with%20%60w%60%20inside%20the%20sum%2C%20divided%20by%20%60N%60%20%7C%0A%20%20%20%20%7C%20Off%20by%20a%20factor%20near%201%20%7C%20The%20n%20%2F%20(n%20%E2%88%92%201)%20factor%20missing%20from%20s%C2%B2.%20%7C%20%60n%20%2F%20(n%20-%201)%60%20%7C%0A%20%20%20%20%7C%20A%20number%20near%201%20%2F%20deff%20%7C%20The%20division%20turned%20round.%20%7C%20%60V_design%20%2F%20V_SRS%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%20A%20CV%20threshold%20for%20publishing%0A%0A%20%20%20%20**The%20problem.**%20An%20estimate%20with%20a%20CV%20of%2040%25%20has%20a%2095%25%20interval%20that%20runs%20from%20about%20a%20fifth%20of%20the%20estimate%20to%20nearly%20twice%20it.%20Printed%20in%20a%20table%20beside%20estimates%20with%20a%20CV%20of%203%25%2C%20it%20reads%20as%20if%20it%20were%20as%20good.%20So%20a%20survey%20publishes%20by%20a%20rule%20on%20the%20CV.%20The%20Survey%20of%20Fishing%20Households%202025%20sets%20its%20own%3A%20an%20estimate%20whose%20CV%20is%20**20%25**%20or%20less%20is%20published%3B%20above%2020%25%20and%20up%20to%20**35%25**%2C%20it%20is%20published%20with%20a%20mark%20of%20caution%3B%20above%2035%25%2C%20it%20is%20suppressed.%20Other%20surveys%20set%20other%20thresholds%2C%20so%20the%20function%20takes%20them%20as%20arguments%2C%20with%20the%20survey's%20as%20defaults.%0A%0A%20%20%20%20%23%23%20S2.3%20%C2%B7%20Publish%2C%20caution%20or%20suppress%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20rule%20is%20applied%20to%20every%20cell%20of%20every%20table%2C%20by%20a%20script%2C%20and%20printed%20in%20the%20table's%20notes.%20It%20must%20handle%20the%20CV%20a%20library%20gives%20(a%20fraction)%2C%20the%20estimates%20whose%20CV%20can't%20be%20computed%20(an%20estimate%20of%200%20gives%20an%20infinite%20CV)%2C%20and%20the%20values%20that%20fall%20exactly%20on%20a%20threshold.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20table%20with%20a%20column%20%60cv%60%20(a%20fraction%2C%20as%20svy%20gives%20it)%2C%20and%20two%20thresholds%20as%20fractions%2C%20%60caution%3D0.20%60%20and%20%60suppress%3D0.35%60%20%E2%86%92%20a%20Series%20of%20labels%2C%20one%20per%20row%2C%20in%20the%20table's%20order%3A%0A%0A%20%20%20%20%7C%20cv%20%7C%20label%20%7C%0A%20%20%20%20%7C--%3A%7C%3A--%7C%0A%20%20%20%20%7C%200.05%20%7C%20%60publish%60%20%7C%0A%20%20%20%20%7C%200.20%20%7C%20%60publish%60%20(at%20the%20threshold%3A%20publish)%20%7C%0A%20%20%20%20%7C%200.25%20%7C%20%60caution%60%20%7C%0A%20%20%20%20%7C%200.35%20%7C%20%60caution%60%20%7C%0A%20%20%20%20%7C%200.36%20%7C%20%60suppress%60%20%7C%0A%20%20%20%20%7C%20missing%2C%20or%20infinite%20%7C%20%60suppress%60%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60np.isfinite%60%2C%20comparisons%2C%20and%20%60np.select%60%20(module%205).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20reliability(table%3A%20pd.DataFrame%2C%20caution%3A%20float%20%3D%200.20%2C%20suppress%3A%20float%20%3D%200.35)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22A%20label%20for%20each%20row's%20CV%3A%20publish%20(cv%20%3C%3D%20caution)%2C%20caution%20(cv%20%3C%3D%20suppress)%2C%20or%20suppress%20(above%2C%20or%20no%20CV).%22%22%22%0A%20%20%20%20%23%20Exercise%20S2.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20cv%20column%20as%20floats%0A%20%20%20%20%23%20Step%202%3A%20np.select%3A%20%22suppress%22%20where%20the%20cv%20is%20not%20finite%20or%20above%20suppress%3B%20%22caution%22%20where%20above%20caution%0A%20%20%20%20%23%20Step%203%3A%20default%20%22publish%22%3B%20return%20it%20as%20a%20Series%20with%20the%20table's%20index%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S2.3%22)%0A%0A%0A%40app.function%0Adef%20rule_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22CVs%20on%20and%20around%20the%20thresholds%2C%20a%20missing%20one%20and%20an%20infinite%20one%2C%20used%20by%20the%20check%20of%20Exercise%20S2.3.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%22est%22%3A%20%5B100.0%2C%2050.0%2C%2040.0%2C%2030.0%2C%2020.0%2C%2010.0%2C%200.0%2C%205.0%5D%2C%20%22cv%22%3A%20%5B0.05%2C%200.20%2C%200.25%2C%200.35%2C%200.36%2C%20None%2C%20np.inf%2C%200.2000001%5D%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(hh_sample)%3A%0A%20%20%20%20_by_province%20%3D%20hh_sample.estimation.total(y%3D%22large_unit_operators%22%2C%20by%3D%22province%22).to_polars().to_pandas()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S2.3%22%2C%20reliability%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22CVs%20on%20and%20around%20the%20thresholds%2C%20a%20missing%20one%20and%20an%20infinite%20one%22%2C%20(rule_fixture()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20operators%20of%20large-scale%20units%20by%20province%22%2C%20(_by_province%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20same%2C%20with%20other%20thresholds%20of%2010%25%20and%2020%25%22%2C%20(_by_province%2C%200.10%2C%200.20))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20small%20CVs%2C%20with%20thresholds%20of%204%25%20and%205%25%22%2C%20(rule_fixture().head(3)%2C%200.04%2C%200.05))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%20of%20CVs%22%2C%20summarize%3Das_values%2C%0A%20%20%20%20%20%20%20%20hint%3D%22cv%20is%20a%20fraction%3B%20suppress%20when%20it%20is%20missing%2C%20infinite%20or%20above%20suppress%3B%20caution%20when%20above%20caution%3B%20publish%20otherwise.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(hh_sample)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20provinces'%20operators%20of%20large-scale%20units%2C%20labeled%0A%20%20%20%20def%20_try_labels()%3A%0A%20%20%20%20%20%20%20%20table%20%3D%20hh_sample.estimation.total(y%3D%22large_unit_operators%22%2C%20by%3D%22province%22).to_polars().to_pandas()%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20labels%20%3D%20reliability(table)%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%20return%20table%5B%5B%22province%22%2C%20%22est%22%2C%20%22cv%22%5D%5D.assign(reliability%3Dlabels)%0A%0A%20%20%20%20_try_labels()%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%20S2.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%20Everything%20%60publish%60%20%7C%20The%20CV%20compared%20with%2020%20and%2035%2C%20not%200.20%20and%200.35.%20%7C%20the%20thresholds%20are%20fractions%20%7C%0A%20%20%20%20%7C%20The%20missing%20CV%20says%20%60publish%60%20%7C%20%60nan%20%3E%200.35%60%20is%20False%2C%20so%20it%20fell%20through%20to%20the%20default.%20%7C%20%60~np.isfinite(cv)%60%20%7C%0A%20%20%20%20%7C%20A%20CV%20of%200.40%20says%20%60caution%60%20%7C%20The%20caution%20condition%20is%20checked%20first.%20%7C%20the%20order%20of%20the%20conditions%20%7C%0A%20%20%20%20%7C%200.20%20says%20%60caution%60%20%7C%20%60%3E%3D%60%20where%20%60%3E%60%20is%20wanted.%20%7C%20%60cv%20%3E%20caution%60%20%7C%0A%20%20%20%20%7C%20Labels%20in%20another%20order%20%7C%20A%20filtered%20or%20sorted%20table%20returned.%20%7C%20keep%20the%20table's%20index%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%20Operators%20of%20large-scale%20units%20by%20province%0A%0A%20%20%20%20The%20number%20of%20members%20who%20run%20a%20large-scale%20unit%20in%20each%20province%2C%20with%20the%20rule%20applied.%20Move%20the%20suppress%20threshold%20to%20see%20which%20provinces%20it%20lets%20through%3A%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%20suppress_at%20%3D%20mo.ui.slider(start%3D20%2C%20stop%3D50%2C%20step%3D5%2C%20value%3D35%2C%20label%3D%22suppress%20above%20a%20CV%20of%20(%25)%22%2C%20show_value%3DTrue)%0A%20%20%20%20suppress_at%0A%20%20%20%20return%20(suppress_at%2C)%0A%0A%0A%40app.cell%0Adef%20_(hh_sample%2C%20suppress_at)%3A%0A%20%20%20%20%23%20The%20operators%20of%20large-scale%20units%20in%20each%20province%2C%20their%20CVs%2C%20and%20the%20rule%0A%20%20%20%20_table%20%3D%20hh_sample.estimation.total(y%3D%22large_unit_operators%22%2C%20by%3D%22province%22).to_polars().to_pandas()%0A%20%20%20%20_labels%20%3D%20attempt(reliability%2C%20_table%2C%20CAUTION_CV%2C%20suppress_at.value%20%2F%20100)%0A%20%20%20%20mo.stop(_labels%20is%20NOT_YET%2C%20waiting_for(%22Exercise%20S2.3%22))%0A%20%20%20%20_table%5B%22reliability%22%5D%20%3D%20_labels%0A%20%20%20%20_colors%20%3D%20%7B%22publish%22%3A%20%22%230072B2%22%2C%20%22caution%22%3A%20%22%23E69F00%22%2C%20%22suppress%22%3A%20%22%23D55E00%22%7D%20%20%23%20Okabe-Ito%3A%20safe%20for%20color%20blindness%0A%20%20%20%20_rows%20%3D%20_table.sort_values(%22cv%22)%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(6.5%2C%203.2))%0A%20%20%20%20for%20_label%2C%20_part%20in%20_rows.groupby(%22reliability%22)%3A%0A%20%20%20%20%20%20%20%20_ax.scatter(_part%5B%22cv%22%5D%20*%20100%2C%20_part%5B%22province%22%5D%2C%20color%3D_colors%5B_label%5D%2C%20label%3D_label%2C%20s%3D60%2C%20zorder%3D3)%0A%20%20%20%20_ax.axvline(CAUTION_CV%20*%20100%2C%20color%3D%22%23888888%22%2C%20linestyle%3D%22--%22%2C%20linewidth%3D0.8)%0A%20%20%20%20_ax.axvline(suppress_at.value%2C%20color%3D%22%23888888%22%2C%20linestyle%3D%22--%22%2C%20linewidth%3D0.8)%0A%20%20%20%20_ax.set_xlabel(%22coefficient%20of%20variation%20(%25)%22)%0A%20%20%20%20_ax.set_title(%22Operators%20of%20large-scale%20units%20by%20province%3A%20CV%20and%20the%20publication%20rule%22)%0A%20%20%20%20_ax.set_xlim(0%2C%20max(55%2C%20_rows%5B%22cv%22%5D.max()%20*%20100%20%2B%205))%0A%20%20%20%20_ax.legend(frameon%3DFalse%2C%20loc%3D%22upper%20left%22%2C%20bbox_to_anchor%3D(1%2C%201))%0A%20%20%20%20_ax.spines%5B%5B%22top%22%2C%20%22right%22%5D%5D.set_visible(False)%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20mo.vstack(%5B_ax%2C%20_table%5B%5B%22province%22%2C%20%22est%22%2C%20%22se%22%2C%20%22cv%22%2C%20%22reliability%22%5D%5D.round(3)%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%203%20%C2%B7%20Cross%20tables%0A%0A%20%20%20%20Module%206%20estimated%20every%20cell%20of%20a%20cross%20table%2C%20such%20as%20operators%20by%20province%20and%20sex%20(Exercise%206.5).%20Module%207%20published%20long%20tables%2C%20one%20estimate%20per%20row%2C%20cut%20by%20area%20and%2C%20for%20one%20table%2C%20by%20scale.%20A%20survey's%20standard%20tables%20cut%20by%20two%20or%20three%20things%20at%20once%2C%20and%20each%20cut%20thins%20the%20sample%20in%20every%20cell.%20Three%20things%20that%20a%20one-way%20table%20never%20meets%20come%20with%20it%3A%0A%0A%20%20%20%20-%20**margins**%2C%20the%20%22Total%22%20row%20and%20column%2C%20whose%20standard%20errors%20do%20not%20add%20up%20from%20the%20cells'%20(S2.4)%3B%0A%20%20%20%20-%20**proportions%20in%20small%20domains**%2C%20whose%20usual%20confidence%20interval%20can%20run%20below%200%25%20(S2.5)%3B%0A%20%20%20%20-%20**a%20reader%20who%20compares%20across%20a%20row**%2C%20for%20whom%20the%20long%20table%20becomes%20a%20cross%20table%20with%20each%20cell's%20reliability%20in%20how%20it%20is%20shown%20(S2.6).%0A%0A%20%20%20%20**The%20data.**%20These%20exercises%20start%20from%20%60operators%60%2C%20built%20in%20the%20next%20cell%20by%20module%206's%20%60operator_table%60%3A%20one%20row%20per%20member%20who%20runs%20a%20fishing%20unit%20of%20a%20household%20in%20%60hh_frame%60%2C%20with%20the%20province%2C%20sex%20and%20scale%20(the%20scale%20of%20the%20largest%20unit%20they%20run)%20as%20labels%2C%20and%20the%20household's%20design%20columns.%20%60domain_cells%60%20is%20module%206's%20Exercise%206.5%2C%20given%20here%3B%20%60labeled_cells%60%20adds%20one%20column%20to%20it%3A%20each%20cell's%20%60reliability%60%2C%20by%20the%20rule%20of%20S2.3%2C%20and%20**no%20sample**%20for%20a%20cell%20nobody%20in%20the%20sample%20fell%20into.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20sex_label(codes%3A%20pd.Series)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Module%206's%20label%20of%20sex%3A%20Male%2C%20Female%2C%20Invalid%20value%20for%20another%20code%2C%20No%20answer%20for%20none.%22%22%22%0A%20%20%20%20labels%20%3D%20codes.map(%7B1%3A%20%22Male%22%2C%202%3A%20%22Female%22%7D)%0A%20%20%20%20return%20labels.mask(codes.notna()%20%26%20labels.isna()%2C%20INVALID).mask(codes.isna()%2C%20NO_ANSWER).astype(%22string%22)%0A%0A%0A%40app.function%0Adef%20scale_label(codes%3A%20pd.Series)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Module%206's%20label%20of%20the%20scale%20of%20an%20operator's%20largest%20unit%3A%20Small-scale%2C%20Large-scale%2C%20Invalid%20value%20for%20another%0A%20%20%20%20code%2C%20No%20answer%20for%20an%20operator%20with%20no%20unit%20in%20the%20units%20table.%22%22%22%0A%20%20%20%20labels%20%3D%20codes.map(%7B1%3A%20%22Small-scale%22%2C%202%3A%20%22Large-scale%22%7D)%0A%20%20%20%20return%20labels.mask(codes.notna()%20%26%20labels.isna()%2C%20INVALID).mask(codes.isna()%2C%20NO_ANSWER).astype(%22string%22)%0A%0A%0A%40app.function%0Adef%20operator_table(frame%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Module%206's%20operators%3A%20one%20row%20per%20operator%20of%20the%20frame's%20households%3A%20the%20operator's%20household%20and%20roster%20line%2C%0A%20%20%20%20the%20province's%20name%2C%20sex%20and%20scale%20as%20labels%2C%20the%20design%20columns%2C%20and%20three%20columns%20to%20estimate%20from%3A%20%60operators%60%0A%20%20%20%20(1%20on%20every%20row)%2C%20%60female%60%20(1%20for%20a%20woman%2C%200%20otherwise)%20and%20%60large_scale%60%20(1%20for%20an%20operator%20of%20a%20large-scale%20unit).%22%22%22%0A%20%20%20%20key%20%3D%20%5B%22region_code%22%2C%20%22hh_id%22%5D%0A%20%20%20%20members%20%3D%20course.members()%0A%20%20%20%20found%20%3D%20members%5Bmembers%5B%22mem_role_operator%22%5D.eq(1).fillna(False)%5D%0A%20%20%20%20%23%20The%20scale%20of%20the%20largest%20unit%20each%20operator%20runs%2C%20by%20their%20roster%20line%20(the%20units'%20operator_line)%0A%20%20%20%20units%20%3D%20course.units()%0A%20%20%20%20largest%20%3D%20units.groupby(%5B*key%2C%20%22operator_line%22%5D)%5B%22unit_scale%22%5D.max().rename(%22unit_scale%22).reset_index()%0A%20%20%20%20found%20%3D%20found.merge(largest.rename(columns%3D%7B%22operator_line%22%3A%20%22mem_line%22%7D)%2C%20on%3D%5B*key%2C%20%22mem_line%22%5D%2C%20how%3D%22left%22%2C%20validate%3D%22one_to_one%22)%0A%20%20%20%20columns%20%3D%20%5B*key%2C%20%22province%22%2C%20%22stratum_id%22%2C%20%22psu_id%22%2C%20%22psus_in_stratum%22%2C%20%22sel_final_weight%22%5D%0A%20%20%20%20table%20%3D%20found.merge(frame%5Bcolumns%5D%2C%20on%3Dkey%2C%20how%3D%22inner%22%2C%20validate%3D%22many_to_one%22)%20%20%23%20inner%3A%20a%20copy's%20operator%20leaves%20with%20it%0A%20%20%20%20table%20%3D%20table.assign(sex%3Dsex_label(table%5B%22mem_sex%22%5D)%2C%20scale%3Dscale_label(table%5B%22unit_scale%22%5D)%2C%20operators%3D1%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%20female%3D(table%5B%22mem_sex%22%5D%20%3D%3D%202).astype(%22int64%22)%2C%20large_scale%3D(table%5B%22unit_scale%22%5D%20%3D%3D%202).astype(%22int64%22))%0A%20%20%20%20keep%20%3D%20%5B*key%2C%20%22mem_line%22%2C%20%22province%22%2C%20%22sex%22%2C%20%22scale%22%2C%20%22operators%22%2C%20%22female%22%2C%20%22large_scale%22%2C%20%22stratum_id%22%2C%20%22psu_id%22%2C%20%22psus_in_stratum%22%2C%20%22sel_final_weight%22%5D%0A%20%20%20%20return%20table%5Bkeep%5D.reset_index(drop%3DTrue)%0A%0A%0A%40app.function%0Adef%20operator_sample(frame%3A%20pd.DataFrame)%20-%3E%20svy.Sample%3A%0A%20%20%20%20%22%22%22The%20operators%20as%20svy%20reads%20them%3A%20the%20households'%20design%2C%20each%20operator%20carrying%20its%20household's%20weight%20and%0A%20%20%20%20sampling%20unit.%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.cell%0Adef%20_(hh_frame)%3A%0A%20%20%20%20%23%20The%20operators%20of%20the%20households%20module%206%20estimates%20from%2C%20and%20the%20svy%20sample%20the%20cross%20tables%20estimate%20from%0A%20%20%20%20operators%20%3D%20operator_table(hh_frame)%0A%20%20%20%20operators_sample%20%3D%20operator_sample(operators)%0A%20%20%20%20PROVINCES%20%3D%20sorted(operators%5B%22province%22%5D.unique())%0A%20%20%20%20_members%20%3D%20course.members()%0A%20%20%20%20_all%20%3D%20int((_members%5B%22mem_role_operator%22%5D%20%3D%3D%201).sum())%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(operators)%3A%2C%7D%20operators**%20in%20%7Blen(PROVINCES)%7D%20provinces%3A%20%7B_all%3A%2C%7D%20members%20with%20%60mem_role_operator%60%20%3D%201%2C%20less%20the%20%7B_all%20-%20len(operators)%7D%20operators%20of%20the%20three%20copied%20households%2C%20which%20%60hh_frame%60%20leaves%20out.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22svy%20sees%20%7Boperators_sample.n_strata%7D%20strata%20and%20%7Boperators_sample.n_psus%3A%2C%7D%20sampling%20units.%20An%20operator's%20sampling%20unit%20and%20weight%20are%20their%20household's%3A%20the%20design%20was%20drawn%20on%20households%2C%20and%20every%20member%20of%20a%20drawn%20household%20was%20interviewed.%22)%2C%0A%20%20%20%20%20%20%20%20operators.head()%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20PROVINCES%2C%20operators_sample%0A%0A%0A%40app.function%0Adef%20reliability_label(cv)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%22%22%22The%20rule%20of%20Exercise%20S2.3%20for%20a%20column%20of%20CVs%2C%20as%20fractions%3A%20publish%20up%20to%20CAUTION_CV%2C%20caution%20up%20to%20SUPPRESS_CV%2C%0A%20%20%20%20suppress%20above%20it%20or%20where%20the%20CV%20is%20missing%20or%20infinite.%22%22%22%0A%20%20%20%20cv%20%3D%20np.asarray(cv%2C%20dtype%3Dfloat)%0A%20%20%20%20return%20np.select(%5B~np.isfinite(cv)%20%7C%20(cv%20%3E%20SUPPRESS_CV)%2C%20cv%20%3E%20CAUTION_CV%5D%2C%20%5B%22suppress%22%2C%20%22caution%22%5D%2C%20default%3D%22publish%22)%0A%0A%0A%40app.function%0Adef%20domain_cells(sample%3A%20svy.Sample%2C%20y%3A%20str%2C%20levels%3A%20dict)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Module%206's%20Exercise%206.5%3A%20one%20row%20per%20combination%20of%20the%20levels%2C%20in%20order%3A%20the%20total%20of%20y%2C%20its%20SE%2C%20CV%20and%20n%2C%20with%0A%20%20%20%200%2C%200%2C%20missing%20and%200%20where%20no%20sampled%20row%20falls%20in%20the%20combination.%22%22%22%0A%20%20%20%20by%20%3D%20list(levels)%0A%20%20%20%20found%20%3D%20sample.estimation.total(y%3Dy%2C%20by%3Dby).to_polars().to_pandas()%0A%20%20%20%20grid%20%3D%20pd.DataFrame(itertools.product(*levels.values())%2C%20columns%3Dby)%0A%20%20%20%20table%20%3D%20grid.merge(found%5B%5B*by%2C%20%22est%22%2C%20%22se%22%2C%20%22cv%22%2C%20%22n%22%5D%5D%2C%20on%3Dby%2C%20how%3D%22left%22%2C%20validate%3D%22one_to_one%22)%0A%20%20%20%20table%5B%5B%22est%22%2C%20%22se%22%5D%5D%20%3D%20table%5B%5B%22est%22%2C%20%22se%22%5D%5D.fillna(0.0)%0A%20%20%20%20table%5B%22n%22%5D%20%3D%20table%5B%22n%22%5D.fillna(0).astype(%22int64%22)%0A%20%20%20%20return%20table%0A%0A%0A%40app.function%0Adef%20labeled_cells(sample%3A%20svy.Sample%2C%20y%3A%20str%2C%20levels%3A%20dict)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Module%206's%20domain_cells%20with%20each%20cell%20labeled%3A%20its%20reliability%20by%20the%20rule%20of%20Exercise%20S2.3%2C%20or%20%22no%20sample%22%0A%20%20%20%20where%20no%20sampled%20row%20falls%20in%20the%20combination.%22%22%22%0A%20%20%20%20table%20%3D%20domain_cells(sample%2C%20y%2C%20levels)%0A%20%20%20%20table%5B%22reliability%22%5D%20%3D%20np.where(table%5B%22n%22%5D%20%3D%3D%200%2C%20%22no%20sample%22%2C%20reliability_label(table%5B%22cv%22%5D))%0A%20%20%20%20return%20table%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**The%20rule%2C%20once%20more.**%20%60reliability_label%60%20is%20S2.3's%20rule%20written%20as%20a%20function%20of%20a%20CV%20column%3A%20**publish**%20up%20to%20a%20CV%20of%2020%25%2C%20**caution**%20above%20it%20up%20to%2035%25%2C%20**suppress**%20above%2035%25%20or%20where%20no%20CV%20can%20be%20computed.%20%60labeled_cells%60%20labels%20every%20cell%20with%20it%2C%20and%20adds%20a%20fourth%20label%2C%20**no%20sample**%2C%20for%20a%20cell%20nobody%20in%20the%20sample%20fell%20into%3A%20%22suppressed%22%20would%20tell%20a%20reader%20that%20an%20estimate%20exists%20but%20is%20too%20uncertain%2C%20which%20is%20not%20true%20of%20an%20empty%20cell.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20small_operators()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Nine%20operators%20in%20two%20strata%2C%20used%20by%20the%20checks%3A%20a%20take-all%20stratum%20T%20of%20three%20households%2C%20each%20its%20own%20sampling%0A%20%20%20%20unit%2C%20and%20a%20stratum%20S%20of%203%20units%20out%20of%206.%20Province%20P%20has%20a%20woman%2C%20Q%20has%20one%20in%20the%20take-all%20stratum%2C%20nobody's%20sex%20is%0A%20%20%20%20invalid%2C%20and%20province%20R%20has%20no%20operator%20at%20all.%22%22%22%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%23%20stratum_id%2C%20psu_id%2C%20psus_in_stratum%2C%20sel_final_weight%2C%20province%2C%20sex%2C%20scale%0A%20%20%20%20%20%20%20%20(%22T%22%2C%20%22T-h1%22%2C%203%2C%201.0%2C%20%22P%22%2C%20%22Male%22%2C%20%22Large-scale%22)%2C%0A%20%20%20%20%20%20%20%20(%22T%22%2C%20%22T-h2%22%2C%203%2C%201.0%2C%20%22Q%22%2C%20%22Female%22%2C%20%22Large-scale%22)%2C%0A%20%20%20%20%20%20%20%20(%22T%22%2C%20%22T-h3%22%2C%203%2C%201.0%2C%20%22Q%22%2C%20%22Male%22%2C%20%22Large-scale%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s1%22%2C%206%2C%204.0%2C%20%22P%22%2C%20%22Male%22%2C%20%22Small-scale%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s1%22%2C%206%2C%204.0%2C%20%22P%22%2C%20%22Female%22%2C%20%22Small-scale%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s2%22%2C%206%2C%204.0%2C%20%22Q%22%2C%20%22Male%22%2C%20%22Small-scale%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s3%22%2C%206%2C%204.0%2C%20%22P%22%2C%20%22Male%22%2C%20%22Small-scale%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s3%22%2C%206%2C%204.0%2C%20%22Q%22%2C%20%22Male%22%2C%20%22Large-scale%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s3%22%2C%206%2C%204.0%2C%20%22Q%22%2C%20%22Male%22%2C%20%22Small-scale%22)%2C%0A%20%20%20%20%5D%0A%20%20%20%20table%20%3D%20pd.DataFrame(rows%2C%20columns%3D%5B%22stratum_id%22%2C%20%22psu_id%22%2C%20%22psus_in_stratum%22%2C%20%22sel_final_weight%22%2C%20%22province%22%2C%20%22sex%22%2C%20%22scale%22%5D)%0A%20%20%20%20return%20table.assign(operators%3D1%2C%20female%3D(table%5B%22sex%22%5D%20%3D%3D%20%22Female%22).astype(%22int64%22)%2C%20large_scale%3D(table%5B%22scale%22%5D%20%3D%3D%20%22Large-scale%22).astype(%22int64%22))%0A%0A%0A%40app.function%0Adef%20small_levels()%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22The%20small%20table's%20categories%3A%20province%20R%20and%20the%20sex%20Invalid%20value%20have%20nobody%20in%20them.%22%22%22%0A%20%20%20%20return%20%7B%22province%22%3A%20%5B%22P%22%2C%20%22Q%22%2C%20%22R%22%5D%2C%20%22sex%22%3A%20%5B%22Male%22%2C%20%22Female%22%2C%20INVALID%5D%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22The%20checks'%20small%20table%3A%20nine%20operators%20in%20two%20strata%22%3A%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22Stratum%20T%20takes%20all%20three%20of%20its%20households%20(each%20its%20own%20sampling%20unit%2C%20weight%201)%3B%20stratum%20S%20drew%203%20of%20its%206%20areas%20(weight%204).%20P's%20woman%20shares%20the%20unit%20%60s1%60%20with%20a%20man%3B%20Q's%20woman%20is%20in%20stratum%20T.%20Province%20R%20and%20the%20sex%20Invalid%20value%20have%20nobody%20in%20them%2C%20so%20every%20cell%20of%20theirs%20is%20empty.%22)%2C%0A%20%20%20%20%20%20%20%20small_operators()%2C%0A%20%20%20%20%5D)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20cell_rows(result%3A%20pd.DataFrame%2C%20digits%3A%20int%20%3D%204)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Module%206's%20form%20of%20a%20table%20of%20cells%20for%20the%20checks%3A%20the%20domain%20variables%20(every%20column%20that%20is%20not%20a%20figure)%20as%0A%20%20%20%20text%2C%20then%20est%2C%20se%2C%20cv%20and%20n%2C%20rounded%2C%20in%20the%20result's%20own%20order%20(the%20order%20is%20part%20of%20the%20answer).%22%22%22%0A%20%20%20%20table%20%3D%20result.reset_index()%20if%20any(name%20is%20not%20None%20for%20name%20in%20result.index.names)%20else%20result%0A%20%20%20%20keys%20%3D%20%5Bc%20for%20c%20in%20table.columns%20if%20c%20not%20in%20(%22est%22%2C%20%22se%22%2C%20%22cv%22%2C%20%22n%22%2C%20%22lci%22%2C%20%22uci%22%2C%20%22df%22)%5D%0A%20%20%20%20out%20%3D%20table%5B%5B*keys%2C%20%22est%22%2C%20%22se%22%2C%20%22cv%22%2C%20%22n%22%5D%5D.reset_index(drop%3DTrue).copy()%0A%20%20%20%20out%5Bkeys%5D%20%3D%20out%5Bkeys%5D.astype(str)%0A%20%20%20%20out%5B%5B%22est%22%2C%20%22se%22%2C%20%22cv%22%5D%5D%20%3D%20out%5B%5B%22est%22%2C%20%22se%22%2C%20%22cv%22%5D%5D.astype(float).round(digits)%0A%20%20%20%20out%5B%22n%22%5D%20%3D%20out%5B%22n%22%5D.astype(%22int64%22)%0A%20%20%20%20return%20out%0A%0A%0A%40app.function%0Adef%20labeled_cell_rows(result%3A%20pd.DataFrame%2C%20digits%3A%20int%20%3D%204)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Module%206's%20cell_rows%20with%20the%20reliability%20column%20labeled_cells%20adds%2C%20as%20text%2C%20last.%22%22%22%0A%20%20%20%20out%20%3D%20cell_rows(result.drop(columns%3D%22reliability%22)%2C%20digits)%0A%20%20%20%20out%5B%22reliability%22%5D%20%3D%20result%5B%22reliability%22%5D.astype(str).to_numpy()%0A%20%20%20%20return%20out%0A%0A%0A%40app.cell%0Adef%20_(PROVINCES%2C%20operators_sample)%3A%0A%20%20%20%20%23%20How%20many%20cells%20each%20label%20gets%20as%20the%20table%20goes%20from%20one%20variable%20to%20three%0A%20%20%20%20def%20_depth()%3A%0A%20%20%20%20%20%20%20%20cuts%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22province%22%3A%20%7B%22province%22%3A%20PROVINCES%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22province%20%C3%97%20sex%22%3A%20%7B%22province%22%3A%20PROVINCES%2C%20%22sex%22%3A%20SEXES%5B%3A2%5D%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22province%20%C3%97%20scale%20%C3%97%20sex%22%3A%20%7B%22province%22%3A%20PROVINCES%2C%20%22scale%22%3A%20SCALES%5B%3A2%5D%2C%20%22sex%22%3A%20SEXES%5B%3A2%5D%7D%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20rows%20%3D%20%7B%7D%0A%20%20%20%20%20%20%20%20for%20name%2C%20levels%20in%20cuts.items()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20cells%20%3D%20labeled_cells(operators_sample%2C%20%22operators%22%2C%20levels)%0A%20%20%20%20%20%20%20%20%20%20%20%20rows%5Bname%5D%20%3D%20cells%5B%22reliability%22%5D.value_counts().reindex(%5B%22publish%22%2C%20%22caution%22%2C%20%22suppress%22%2C%20%22no%20sample%22%5D%2C%20fill_value%3D0)%0A%20%20%20%20%20%20%20%20table%20%3D%20pd.DataFrame(rows).T.rename_axis(%22cells%20by%22)%0A%20%20%20%20%20%20%20%20table%5B%22cells%22%5D%20%3D%20table.sum(axis%3D1)%0A%20%20%20%20%20%20%20%20three%20%3D%20table.loc%5B%22province%20%C3%97%20scale%20%C3%97%20sex%22%5D%0A%20%20%20%20%20%20%20%20women%20%3D%20labeled_cells(operators_sample%2C%20%22operators%22%2C%20%7B%22province%22%3A%20PROVINCES%2C%20%22scale%22%3A%20%5B%22Large-scale%22%5D%2C%20%22sex%22%3A%20%5B%22Female%22%5D%7D)%0A%20%20%20%20%20%20%20%20held_back%20%3D%20int((women%5B%22reliability%22%5D%20!%3D%20%22publish%22).sum())%0A%20%20%20%20%20%20%20%20one%20%3D%20table.loc%5B%22province%22%5D%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20table%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22Each%20cut%20divides%20the%20sample%20again.%20By%20province%20alone%2C%20%7Bone%5B'publish'%5D%7D%20of%20%7Bone%5B'cells'%5D%7D%20cells%20can%20be%20published.%20By%20province%2C%20scale%20and%20sex%2C%20with%20the%20catch-all%20categories%20left%20out%2C%20**%7Bthree%5B'publish'%5D%7D%20of%20%7Bthree%5B'cells'%5D%7D%20cells**%20pass%20the%20rule%2C%20and%20%7Bthree%5B'suppress'%5D%7D%20are%20suppressed.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22The%20women%20who%20run%20a%20large-scale%20unit%20are%20cautioned%20or%20suppressed%20in%20%7Bheld_back%7D%20of%20%7Blen(PROVINCES)%7D%20provinces%3A%20a%20few%20households%20in%20the%20sample%20decide%20each%20of%20those%20cells.%22)%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_depth()%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%20S2.4%20%C2%B7%20Margins%20from%20their%20own%20estimates%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20cross%20table%20has%20a%20%22Total%22%20row%20and%20column%3A%20all%20provinces%2C%20all%20sexes.%20A%20total%20**adds%20up**%3A%20the%20operators%20of%20all%20provinces%20are%20the%20sum%20of%20each%20province's.%20Its%20standard%20error%20does%20**not**.%20The%20cells%20share%20sampling%20units%20(a%20drawn%20area%20holds%20men%20and%20women)%2C%20so%20their%20errors%20move%20together%2C%20and%20the%20margin's%20SE%20is%20neither%20the%20sum%20of%20the%20cells'%20SEs%20nor%20the%20square%20root%20of%20the%20sum%20of%20their%20variances.%20It%20comes%20from%20estimating%20the%20margin%20itself%2C%20on%20the%20full%20design.%20The%20cell%20below%20shows%20it%20on%20the%20nine%20operators%20of%20the%20check%3A%20province%20P's%20two%20cells%2C%20and%20the%20margin%20svy%20estimates%20for%20P.%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%20Province%20P%20of%20the%20small%20table%3A%20its%20two%20cells%2C%20and%20three%20ways%20to%20its%20margin's%20SE%0A%20%20%20%20_small%20%3D%20operator_sample(small_operators())%0A%20%20%20%20_cells%20%3D%20_small.estimation.total(y%3D%22operators%22%2C%20by%3D%5B%22province%22%2C%20%22sex%22%5D).to_polars().to_pandas().query(%22province%20%3D%3D%20'P'%22)%0A%20%20%20%20_margin%20%3D%20_small.estimation.total(y%3D%22operators%22%2C%20by%3D%22province%22).to_polars().to_pandas().query(%22province%20%3D%3D%20'P'%22).iloc%5B0%5D%0A%20%20%20%20_ways%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22the%20margin's%22%3A%20%5B%22estimate%22%2C%20%22SE%22%5D%2C%0A%20%20%20%20%20%20%20%20%22sum%20of%20the%20cells%22%3A%20%5B_cells%5B%22est%22%5D.sum()%2C%20_cells%5B%22se%22%5D.sum()%5D%2C%0A%20%20%20%20%20%20%20%20%22square%20root%20of%20the%20sum%20of%20the%20cells'%20variances%22%3A%20%5Bnp.nan%2C%20np.sqrt((_cells%5B%22se%22%5D%20**%202).sum())%5D%2C%0A%20%20%20%20%20%20%20%20%22svy%2C%20the%20margin%20estimated%20itself%22%3A%20%5B_margin%5B%22est%22%5D%2C%20_margin%5B%22se%22%5D%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20mo.vstack(%5B_cells%5B%5B%22province%22%2C%20%22sex%22%2C%20%22est%22%2C%20%22se%22%2C%20%22n%22%5D%5D%2C%20_ways.round(3)%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20task's%20example%2C%20from%20the%20check's%20small%20table%3A%20P's%20cells%2C%20P's%20margin%2C%20the%20women's%20margin%20and%20the%20grand%20total%2C%0A%20%20%20%20%23%20each%20estimated%20by%20svy%20on%20its%20own%0A%20%20%20%20_small%20%3D%20operator_sample(small_operators())%0A%20%20%20%20_example%20%3D%20labeled_cells(_small%2C%20%22operators%22%2C%20small_levels())%0A%20%20%20%20_p%20%3D%20_small.estimation.total(y%3D%22operators%22%2C%20by%3D%22province%22).to_polars().to_pandas().query(%22province%20%3D%3D%20'P'%22).iloc%5B0%5D%0A%20%20%20%20_f%20%3D%20_small.estimation.total(y%3D%22operators%22%2C%20by%3D%22sex%22).to_polars().to_pandas().query(%22sex%20%3D%3D%20'Female'%22).iloc%5B0%5D%0A%20%20%20%20_whole%20%3D%20_small.estimation.total(y%3D%22operators%22).to_polars().to_pandas().iloc%5B0%5D%0A%0A%20%20%20%20def%20_row(province%2C%20sex%2C%20est%2C%20se%2C%20label)%3A%0A%20%20%20%20%20%20%20%20return%20f%22%7C%20%7Bprovince%7D%20%7C%20%7Bsex%7D%20%7C%20%7Best%3A.1f%7D%20%7C%20%7Bse%3A.2f%7D%20%7C%20%7C%20%7Blabel%7D%20%7C%22%0A%0A%20%20%20%20_lines%20%3D%20%5B_row(r.province%2C%20r.sex%2C%20r.est%2C%20r.se%2C%20r.reliability)%20for%20r%20in%20_example%5B_example%5B%22province%22%5D%20%3D%3D%20%22P%22%5D.itertuples()%5D%0A%20%20%20%20_lines.append(_row(%22P%22%2C%20%22Total%22%2C%20_p%5B%22est%22%5D%2C%20_p%5B%22se%22%5D%2C%20reliability_label(%5B_p%5B%22cv%22%5D%5D)%5B0%5D))%0A%20%20%20%20_lines.append(%22%7C%20%E2%80%A6%20%7C%20%7C%20%7C%20%7C%20%7C%20%7C%22)%0A%20%20%20%20_lines.append(_row(%22Total%22%2C%20%22Female%22%2C%20_f%5B%22est%22%5D%2C%20_f%5B%22se%22%5D%2C%20reliability_label(%5B_f%5B%22cv%22%5D%5D)%5B0%5D))%0A%20%20%20%20_lines.append(_row(%22Total%22%2C%20INVALID%2C%200.0%2C%200.0%2C%20%22no%20sample%22))%0A%20%20%20%20_lines.append(_row(%22Total%22%2C%20%22Total%22%2C%20_whole%5B%22est%22%5D%2C%20_whole%5B%22se%22%5D%2C%20reliability_label(%5B_whole%5B%22cv%22%5D%5D)%5B0%5D))%0A%20%20%20%20_table%20%3D%20%22%5Cn%22.join(_lines)%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20The%20estimates%20agree%3B%20the%20SEs%20disagree%2C%20in%20both%20directions%20depending%20on%20how%20the%20cells'%20errors%20move%20together.%20In%20this%20small%20design%20the%20woman%20and%20the%20man%20of%20P%20share%20a%20sampling%20unit%2C%20%60s1%60%2C%20whose%20total%20moves%20both%20cells%20at%20once.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20same%20arguments%20as%20%60labeled_cells%60%20%E2%86%92%20its%20cells%20**and%20every%20margin**%2C%20one%20row%20per%20combination%20of%20each%20variable's%20categories%20**followed%20by%20%60%22Total%22%60**%2C%20in%20product%20order.%20For%20two%20variables%20that%20is%20the%20cells%2C%20a%20%22Total%22%20sex%20for%20each%20province%2C%20a%20%22Total%22%20province%20for%20each%20sex%2C%20and%20the%20grand%20total.%20On%20the%20check's%20small%20table%3A%0A%0A%20%20%20%20%7C%20province%20%7C%20sex%20%7C%20est%20%7C%20se%20%7C%20%E2%80%A6%20%7C%20reliability%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C--%3A%7C--%3A%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7B_table%7D%0A%0A%20%20%20%20Each%20margin%20is%20estimated%20on%20its%20own%3A%20by%20the%20variables%20it%20keeps%2C%20or%20with%20no%20%60by%3D%60%20at%20all%20for%20the%20grand%20total%2C%20and%20labeled%20by%20the%20same%20rules%20as%20a%20cell.%0A%0A%20%20%20%20**Tools%3A**%20%60itertools.combinations(names%2C%20k)%60%20for%20every%20subset%20of%20the%20variables%2C%20%60labeled_cells%60%20for%20each%2C%20%60assign%60%20to%20put%20%60%22Total%22%60%20in%20the%20variables%20a%20margin%20drops%2C%20%60pd.concat%60%2C%20and%20%60domain_cells%60'%20grid%20with%20%60%22Total%22%60%20added%20to%20the%20end%20of%20every%20variable's%20list.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20with_margins(sample%3A%20svy.Sample%2C%20y%3A%20str%2C%20levels%3A%20dict)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20cells%20of%20labeled_cells%20and%20every%20margin%20(%22Total%22%20in%20each%20variable%20a%20margin%20adds%20over)%2C%20each%20estimated%20on%20the%0A%20%20%20%20full%20design%2C%20in%20product%20order%20with%20%22Total%22%20last%20in%20each%20variable.%22%22%22%0A%20%20%20%20%23%20Exercise%20S2.4%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20for%20every%20subset%20of%20the%20variables%20(itertools.combinations%20of%20every%20size%2C%20the%20empty%20one%20included)%3A%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20estimate%20by%20that%20subset%20(labeled_cells%20with%20only%20those%20levels)%3B%20for%20the%20empty%20subset%2C%20the%20total%20with%20no%20by%3D%0A%20%20%20%20%23%20Step%202%3A%20in%20each%20part%2C%20set%20every%20variable%20the%20subset%20leaves%20out%20to%20%22Total%22%20(TOTAL)%0A%20%20%20%20%23%20Step%203%3A%20pd.concat%20the%20parts%2C%20and%20lay%20them%20on%20the%20grid%20of%20every%20variable's%20levels%20followed%20by%20TOTAL%20(as%20%60domain_cells%60%20does)%0A%20%20%20%20%23%20Step%204%3A%20return%20the%20variables%2C%20est%2C%20se%2C%20cv%2C%20n%20and%20reliability%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S2.4%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(PROVINCES%2C%20operators_sample)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S2.4%22%2C%20with_margins%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%20operators%3A%20a%20province%20and%20a%20sex%20with%20nobody%20in%20them%2C%20two%20cells%20sharing%20a%20sampling%20unit%22%2C%20(operator_sample(small_operators())%2C%20%22operators%22%2C%20small_levels()))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20operators%20by%20province%20and%20sex%22%2C%20(operators_sample%2C%20%22operators%22%2C%20%7B%22province%22%3A%20PROVINCES%2C%20%22sex%22%3A%20SEXES%7D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20operators%20by%20province%2C%20scale%20and%20sex%22%2C%20(operators_sample%2C%20%22operators%22%2C%20%7B%22province%22%3A%20PROVINCES%2C%20%22scale%22%3A%20SCALES%2C%20%22sex%22%3A%20SEXES%7D))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dlabeled_cell_rows%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Every%20subset%20of%20the%20variables%20(itertools.combinations%2C%20sizes%200%20to%20all)%2C%20each%20estimated%20on%20the%20full%20sample%2C%20'Total'%20in%20the%20variables%20it%20leaves%20out%3B%20then%20lay%20them%20on%20the%20grid%20of%20each%20variable's%20levels%20plus%20'Total'.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(PROVINCES%2C%20operators_sample)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20operators%20by%20province%20and%20sex%2C%20with%20every%20margin%0A%20%20%20%20try_it(with_margins%2C%20operators_sample%2C%20%22operators%22%2C%20%7B%22province%22%3A%20PROVINCES%2C%20%22sex%22%3A%20SEXES%7D)%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%20S2.4%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%20The%20margins'%20estimates%20are%20right%2C%20their%20SEs%20are%20not%20%7C%20The%20margins%20added%20up%20from%20the%20cells%3A%20their%20SEs%2C%20or%20their%20variances.%20%7C%20estimate%20each%20margin%20with%20its%20own%20%60by%3D%60%20%7C%0A%20%20%20%20%7C%20No%20grand%20total%2C%20or%20the%20cells%20are%20missing%20%7C%20%60combinations%60%20run%20for%20some%20sizes%20only.%20%7C%20%60range(len(names)%20%2B%201)%60%3A%200%20to%20all%20%7C%0A%20%20%20%20%7C%20The%20%60Total%60%2C%20%60Total%60%20row%20has%20no%20figures%20%7C%20The%20grand%20total's%20part%20left%20its%20variables%20empty%20instead%20of%20%60%22Total%22%60%2C%20so%20the%20grid%20found%20no%20row%20to%20match.%20%7C%20%60%22Total%22%60%20in%20every%20variable%20a%20part%20leaves%20out%20%7C%0A%20%20%20%20%7C%20*MergeError%3A%20Merge%20keys%20are%20not%20unique*%20%7C%20A%20margin%20estimated%20twice%2C%20or%20the%20cells%20added%20again%20as%20a%20margin.%20%7C%20each%20subset%20once%3A%20%60combinations%60%20of%20each%20size%20%7C%0A%20%20%20%20%7C%20Margins%20first%2C%20or%20mixed%20among%20the%20cells%20%7C%20The%20grid%20has%20%60%22Total%22%60%20first%2C%20or%20the%20parts%20were%20not%20laid%20on%20a%20grid.%20%7C%20%60%5B*levels%5Bname%5D%2C%20TOTAL%5D%60%20%7C%0A%20%20%20%20%7C%20Empty%20cells%20or%20empty%20margins%20are%20missing%20%7C%20%60labeled_cells%60'%20grid%20not%20applied%20to%20each%20part.%20%7C%20%60labeled_cells%60%20for%20each%20subset%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%0Adef%20_(PROVINCES%2C%20operators_sample)%3A%0A%20%20%20%20%23%20The%20province%20margins%20beside%20the%20sums%20of%20their%20cells%3A%20the%20estimates%20agree%2C%20the%20SEs%20do%20not%0A%20%20%20%20def%20_root_of_summed_variances(se)%3A%0A%20%20%20%20%20%20%20%20return%20np.sqrt((se%20**%202).sum())%0A%0A%20%20%20%20def%20_margins()%3A%0A%20%20%20%20%20%20%20%20table%20%3D%20attempt(with_margins%2C%20operators_sample%2C%20%22operators%22%2C%20%7B%22province%22%3A%20PROVINCES%2C%20%22sex%22%3A%20SEXES%7D)%0A%20%20%20%20%20%20%20%20if%20table%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercise%20S2.4%22)%0A%20%20%20%20%20%20%20%20cells%20%3D%20table%5B(table%5B%22province%22%5D%20!%3D%20TOTAL)%20%26%20(table%5B%22sex%22%5D%20!%3D%20TOTAL)%5D%0A%20%20%20%20%20%20%20%20summed%20%3D%20cells.groupby(%22province%22).agg(est_sum%3D(%22est%22%2C%20%22sum%22)%2C%20se_sum%3D(%22se%22%2C%20%22sum%22)%2C%20se_root%3D(%22se%22%2C%20_root_of_summed_variances))%0A%20%20%20%20%20%20%20%20margin%20%3D%20table%5B(table%5B%22province%22%5D%20!%3D%20TOTAL)%20%26%20(table%5B%22sex%22%5D%20%3D%3D%20TOTAL)%5D.set_index(%22province%22)%5B%5B%22est%22%2C%20%22se%22%5D%5D%0A%20%20%20%20%20%20%20%20out%20%3D%20margin.join(summed).loc%5BPROVINCES%5D%0A%20%20%20%20%20%20%20%20out.columns%20%3D%20%5B%22margin%2C%20est%22%2C%20%22margin%2C%20SE%22%2C%20%22sum%20of%20cells%2C%20est%22%2C%20%22sum%20of%20cells'%20SEs%22%2C%20%22root%20of%20summed%20variances%22%5D%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5Bout.round(1)%2C%20mo.md(f%22In%20every%20province%20the%20margin's%20estimate%20is%20the%20cells'%20sum%20to%20the%20last%20decimal%20(largest%20difference%20%7B(out%5B'margin%2C%20est'%5D%20-%20out%5B'sum%20of%20cells%2C%20est'%5D).abs().max()%3A.1e%7D)%2C%20and%20its%20SE%20is%20neither%20of%20the%20two%20figures%20beside%20it.%22)%5D)%0A%0A%20%20%20%20_margins()%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%20S2.5%20%C2%B7%20A%20proportion%20and%20its%20confidence%20interval%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20%22what%20share%20of%20the%20operators%20of%20large-scale%20units%20are%20women%2C%20by%20province%3F%22%20is%20a%20proportion%20in%20a%20small%20domain%3A%20a%20province's%20sampled%20operators%20of%20large-scale%20units%2C%20and%20the%20few%20women%20among%20them.%20svy's%20%60estimation.prop%60%20gives%20each%20share%20with%20its%20SE%20and%20a%2095%25%20confidence%20interval.%20Which%20interval%20matters%20here.%20The%20textbook%20interval%2C%20estimate%20plus%20and%20minus%20t%20standard%20errors%20(the%20**Wald**%20interval)%2C%20is%20symmetric%2C%20so%20for%20a%20small%20share%20with%20a%20large%20SE%20it%20runs%20below%200%25%3A%20a%20range%20of%20possible%20shares%20that%20includes%20impossible%20ones.%20svy's%20default%2C%20the%20**logit**%20interval%2C%20is%20computed%20on%20the%20log-odds%20scale%20and%20turned%20back%2C%20so%20it%20stays%20between%200%20and%201%20and%20is%20lopsided%20where%20the%20share%20is%20near%20either%20end%20(module%206%2C%20section%202)%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20t%20%20%20%20%20%20%3D%20Student's%20t%20at%20(1%20%2B%20level)%20%2F%202%2C%20with%20the%20domain's%20degrees%20of%20freedom%0A%20%20%20%20Wald%20%20%20%3D%20p%20%C2%B1%20t%20%C2%B7%20SE%0A%20%20%20%20logit%20%20%3D%20expit(%20logit(p)%20%C2%B1%20t%20%C2%B7%20SE%20%2F%20(p%20%C2%B7%20(1%20%E2%88%92%20p))%20)%2C%20%20%20logit(p)%20%3D%20log(p%20%2F%20(1%20%E2%88%92%20p))%2C%20%20expit(x)%20%3D%201%20%2F%20(1%20%2B%20e%5E(%E2%88%92x))%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20svy's%20%60prop%60%20returns%20**two%20rows%20per%20domain**%2C%20one%20for%20each%20value%20of%20the%200%2F1%20column%3A%20the%20share%20of%20men%20(%60female%60%20%3D%200)%20and%20the%20share%20of%20women%20(%60female%60%20%3D%201).%20Their%20shares%20add%20to%201%20and%20their%20SEs%20are%20equal.%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_example%20%3D%20proportion_fixture_intervals()%0A%20%20%20%20_a%2C%20_b%20%3D%20_example.iloc%5B0%5D%2C%20_example.iloc%5B1%5D%0A%20%20%20%20_t%20%3D%20stats.t.ppf(0.975%2C%20_a%5B%22df%22%5D)%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20svy's%20%60prop%60%20result%20as%20a%20pandas%20table%2C%20the%20name%20%60y%60%20of%20its%200%2F1%20column%2C%20and%20a%20confidence%20%60level%60%20%E2%86%92%20one%20row%20per%20domain%2C%20the%20rows%20where%20%60y%60%20is%201%2C%20with%20the%20domain%20columns%20(%60y%60%20itself%20left%20out)%2C%20%60est%60%2C%20%60se%60%2C%20%60df%60%2C%20and%20four%20new%20columns%3A%20%60wald_lci%60%2C%20%60wald_uci%60%2C%20%60logit_lci%60%2C%20%60logit_uci%60.%0A%0A%20%20%20%20%7C%20province%20%7C%20est%20%7C%20se%20%7C%20df%20%7C%20wald_lci%20%7C%20wald_uci%20%7C%20logit_lci%20%7C%20logit_uci%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%20A%20%7C%20%7B_a%5B'est'%5D%3A.3f%7D%20%7C%20%7B_a%5B'se'%5D%3A.3f%7D%20%7C%20%7B_a%5B'df'%5D%3A.0f%7D%20%7C%20**%7B_a%5B'wald_lci'%5D%3A.3f%7D**%20%7C%20%7B_a%5B'wald_uci'%5D%3A.3f%7D%20%7C%20%7B_a%5B'logit_lci'%5D%3A.3f%7D%20%7C%20%7B_a%5B'logit_uci'%5D%3A.3f%7D%20%7C%0A%20%20%20%20%7C%20B%20%7C%20%7B_b%5B'est'%5D%3A.3f%7D%20%7C%20%7B_b%5B'se'%5D%3A.4f%7D%20%7C%20%7B_b%5B'df'%5D%3A.0f%7D%20%7C%20%7B_b%5B'wald_lci'%5D%3A.3f%7D%20%7C%20%7B_b%5B'wald_uci'%5D%3A.3f%7D%20%7C%20%7B_b%5B'logit_lci'%5D%3A.3f%7D%20%7C%20%7B_b%5B'logit_uci'%5D%3A.3f%7D%20%7C%0A%0A%20%20%20%20These%20are%20the%20first%20two%20domains%20of%20the%20check's%20small%20table%2C%20%60shares_fixture()%60.%20With%20%7B_a%5B'df'%5D%3A.0f%7D%20degrees%20of%20freedom%20t%20is%20%7B_t%3A.2f%7D%3B%20A's%20Wald%20interval%20runs%20to%20%7B_a%5B'wald_lci'%5D%3A.0%25%7D.%0A%0A%20%20%20%20Where%20%60df%60%20is%200%2C%20t%20is%20undefined%2C%20and%20both%20intervals%20are%20missing.%20Where%20the%20SE%20is%200%20(every%20operator%20of%20the%20domain%20in%20the%20take-all%20stratum)%2C%20both%20intervals%20are%20the%20estimate%20itself.%0A%0A%20%20%20%20**Tools%3A**%20%60stats.t.ppf(q%2C%20df)%60%20(scipy%2C%20as%20module%206)%2C%20%60special.logit%60%20and%20%60special.expit%60%2C%20a%20boolean%20filter%2C%20column%20arithmetic.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20proportion_intervals(table%3A%20pd.DataFrame%2C%20y%3A%20str%2C%20level%3A%20float%20%3D%200.95)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20rows%20of%20a%20svy%20prop%20result%20where%20y%20is%201%2C%20with%20the%20Wald%20and%20the%20logit%20confidence%20intervals%20at%20this%20level.%22%22%22%0A%20%20%20%20%23%20Exercise%20S2.5%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20keep%20the%20rows%20where%20y%20is%201%2C%20and%20drop%20the%20y%20column%0A%20%20%20%20%23%20Step%202%3A%20t%20for%20each%20row%3A%20stats.t.ppf(1%20-%20(1%20-%20level)%20%2F%202%2C%20df)%0A%20%20%20%20%23%20Step%203%3A%20the%20Wald%20interval%3A%20est%20-%20t%20*%20se%20and%20est%20%2B%20t%20*%20se%0A%20%20%20%20%23%20Step%204%3A%20the%20logit%20interval%3A%20half%20%3D%20t%20*%20se%20%2F%20(est%20*%20(1%20-%20est))%3B%20expit(logit(est)%20-%20half)%20and%20expit(logit(est)%20%2B%20half)%0A%20%20%20%20%23%20Step%205%3A%20return%20the%20domain%20columns%2C%20est%2C%20se%2C%20df%20and%20the%20four%20interval%20columns%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S2.5%22)%0A%0A%0A%40app.function%0Adef%20shares_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20svy%20prop%20result%20for%20four%20domains%2C%20two%20rows%20each%3A%20a%20small%20share%20with%20a%20large%20SE%2C%20a%20smaller%20one%2C%20a%20domain%20with%0A%20%20%20%200%20degrees%20of%20freedom%20and%20one%20with%20an%20SE%20of%200.%20Used%20by%20the%20check%20of%20Exercise%20S2.5.%22%22%22%0A%20%20%20%20domains%20%3D%20%5B(%22A%22%2C%200.40%2C%200.245%2C%2015)%2C%20(%22B%22%2C%200.023%2C%200.0077%2C%2020)%2C%20(%22C%22%2C%200.30%2C%200.13%2C%200)%2C%20(%22D%22%2C%200.25%2C%200.0%2C%203)%5D%0A%20%20%20%20rows%20%3D%20%5B%5D%0A%20%20%20%20for%20name%2C%20p%2C%20se%2C%20df%20in%20domains%3A%0A%20%20%20%20%20%20%20%20rows%20%2B%3D%20%5B(name%2C%200%2C%201%20-%20p%2C%20se%2C%20df)%2C%20(name%2C%201%2C%20p%2C%20se%2C%20df)%5D%0A%20%20%20%20return%20pd.DataFrame(rows%2C%20columns%3D%5B%22province%22%2C%20%22female%22%2C%20%22est%22%2C%20%22se%22%2C%20%22df%22%5D).assign(cv%3Dlambda%20t%3A%20t%5B%22se%22%5D%20%2F%20t%5B%22est%22%5D%2C%20n%3D10)%0A%0A%0A%40app.function%0Adef%20proportion_fixture_intervals()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20task's%20example%3A%20the%20two%20intervals%20of%20shares_fixture()'s%20rows%20where%20female%20is%201%2C%20by%20the%20formulas%20above%20(the%0A%20%20%20%20text%20shows%20its%20first%20two%20domains).%22%22%22%0A%20%20%20%20rows%20%3D%20shares_fixture().query(%22female%20%3D%3D%201%22)%0A%20%20%20%20t%20%3D%20stats.t.ppf(0.975%2C%20rows%5B%22df%22%5D)%0A%20%20%20%20half%20%3D%20t%20*%20rows%5B%22se%22%5D%20%2F%20(rows%5B%22est%22%5D%20*%20(1%20-%20rows%5B%22est%22%5D))%0A%20%20%20%20return%20rows.assign(wald_lci%3Drows%5B%22est%22%5D%20-%20t%20*%20rows%5B%22se%22%5D%2C%20wald_uci%3Drows%5B%22est%22%5D%20%2B%20t%20*%20rows%5B%22se%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logit_lci%3Dspecial.expit(special.logit(rows%5B%22est%22%5D)%20-%20half)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20logit_uci%3Dspecial.expit(special.logit(rows%5B%22est%22%5D)%20%2B%20half)).reset_index(drop%3DTrue)%0A%0A%0A%40app.function%0Adef%20interval_rows(result%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Exercise%20S2.5's%20result%20in%20one%20form%20for%20the%20checks%3A%20the%20domain%20column%20and%20the%20seven%20numbers%2C%20rounded%20to%204%20places.%22%22%22%0A%20%20%20%20numbers%20%3D%20%5B%22est%22%2C%20%22se%22%2C%20%22df%22%2C%20%22wald_lci%22%2C%20%22wald_uci%22%2C%20%22logit_lci%22%2C%20%22logit_uci%22%5D%0A%20%20%20%20domains%20%3D%20%5Bc%20for%20c%20in%20result.columns%20if%20c%20not%20in%20(*numbers%2C%20%22lci%22%2C%20%22uci%22%2C%20%22cv%22%2C%20%22n%22)%5D%0A%20%20%20%20out%20%3D%20result.reset_index(drop%3DTrue)%5B%5B*domains%2C%20*numbers%5D%5D.copy()%0A%20%20%20%20out%5Bnumbers%5D%20%3D%20out%5Bnumbers%5D.astype(float).round(4)%0A%20%20%20%20return%20out%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(operators_sample)%3A%0A%20%20%20%20_women%20%3D%20operators_sample.estimation.prop(y%3D%22female%22%2C%20by%3D%22province%22).to_polars().to_pandas()%0A%20%20%20%20_large%20%3D%20operators_sample.estimation.prop(y%3D%22female%22%2C%20by%3D%22province%22%2C%20where%3Dsvy.col(%22large_scale%22)%20%3D%3D%201).to_polars().to_pandas()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S2.5%22%2C%20proportion_intervals%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%20domains%3A%20a%20share%20near%200.4%20with%20a%20large%20SE%2C%20a%20small%20share%2C%200%20degrees%20of%20freedom%2C%20an%20SE%20of%200%22%2C%20(shares_fixture()%2C%20%22female%22%2C%200.95))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20share%20of%20women%20among%20operators%2C%20by%20province%22%2C%20(_women%2C%20%22female%22%2C%200.95))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20share%20of%20women%20among%20operators%20of%20large-scale%20units%2C%20by%20province%2C%20at%2090%25%22%2C%20(_large%2C%20%22female%22%2C%200.90))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%20of%20shares%22%2C%20summarize%3Dinterval_rows%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Keep%20the%20rows%20where%20y%20is%201%3B%20t%20%3D%20stats.t.ppf(1%20-%20(1%20-%20level)%20%2F%202%2C%20df)%3B%20Wald%20is%20est%20%C2%B1%20t%C2%B7se%3B%20logit%20is%20expit(logit(est)%20%C2%B1%20t%C2%B7se%20%2F%20(est%C2%B7(1%20%E2%88%92%20est))).%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(operators_sample)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20share%20of%20women%20among%20operators%20of%20large-scale%20units%2C%20by%20province%2C%20both%20intervals%0A%20%20%20%20try_it(proportion_intervals%2C%20operators_sample.estimation.prop(y%3D%22female%22%2C%20by%3D%22province%22%2C%20where%3Dsvy.col(%22large_scale%22)%20%3D%3D%201).to_polars().to_pandas()%2C%20%22female%22%2C%200.95)%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%20S2.5%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%20Twice%20as%20many%20rows%20as%20domains%20%7C%20Both%20of%20svy's%20rows%20kept%2C%20the%20share%20of%20men%20among%20them.%20%7C%20%60table%5Btable%5By%5D%20%3D%3D%201%5D%60%20%7C%0A%20%20%20%20%7C%20Intervals%20a%20little%20too%20narrow%20%7C%201.96%20used%20for%20every%20row.%20%7C%20%60stats.t.ppf(...%2C%20df)%60%2C%20row%20by%20row%20%7C%0A%20%20%20%20%7C%20The%20intervals%20are%20for%2099%25%20at%20a%20level%20of%200.95%20%7C%20%60stats.t.ppf(level%2C%20df)%60%3A%20the%20quantile%20is%20(1%20%2B%20level)%20%2F%202.%20%7C%20%601%20-%20(1%20-%20level)%20%2F%202%60%20%7C%0A%20%20%20%20%7C%20The%20logit%20interval%20is%20far%20too%20wide%20or%20too%20narrow%20%7C%20The%20SE%20not%20divided%20by%20p(1%20%E2%88%92%20p)%2C%20or%20divided%20by%20p%20alone.%20%7C%20%60t%20*%20se%20%2F%20(est%20*%20(1%20-%20est))%60%20%7C%0A%20%20%20%20%7C%20A%20%60female%60%20column%20in%20the%20result%20%7C%20%60y%60%20not%20dropped%20after%20filtering.%20%7C%20%60.drop(columns%3Dy)%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%0Adef%20_(operators_sample)%3A%0A%20%20%20%20%23%20The%20share%20of%20women%20among%20operators%20of%20large-scale%20units%20by%20province%3A%20the%20two%20intervals%20side%20by%20side%20(matplotlib)%0A%20%20%20%20def%20_chart()%3A%0A%20%20%20%20%20%20%20%20prop%20%3D%20operators_sample.estimation.prop(y%3D%22female%22%2C%20by%3D%22province%22%2C%20where%3Dsvy.col(%22large_scale%22)%20%3D%3D%201).to_polars().to_pandas()%0A%20%20%20%20%20%20%20%20table%20%3D%20attempt(proportion_intervals%2C%20prop%2C%20%22female%22%2C%200.95)%0A%20%20%20%20%20%20%20%20if%20table%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercise%20S2.5%22)%0A%20%20%20%20%20%20%20%20table%20%3D%20table.sort_values(%22est%22).reset_index(drop%3DTrue)%0A%20%20%20%20%20%20%20%20fig%2C%20ax%20%3D%20plt.subplots(figsize%3D(6.5%2C%203.6))%0A%20%20%20%20%20%20%20%20rows%20%3D%20np.arange(len(table))%0A%20%20%20%20%20%20%20%20for%20offset%2C%20name%2C%20color%2C%20low%2C%20high%20in%20%5B(-0.15%2C%20%22Wald%22%2C%20%22%23D55E00%22%2C%20%22wald_lci%22%2C%20%22wald_uci%22)%2C%20(0.15%2C%20%22logit%20(svy's%20default)%22%2C%20%22%230072B2%22%2C%20%22logit_lci%22%2C%20%22logit_uci%22)%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20ax.errorbar(table%5B%22est%22%5D%20*%20100%2C%20rows%20%2B%20offset%2C%20xerr%3D%5B(table%5B%22est%22%5D%20-%20table%5Blow%5D)%20*%20100%2C%20(table%5Bhigh%5D%20-%20table%5B%22est%22%5D)%20*%20100%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%20fmt%3D%22o%22%2C%20color%3Dcolor%2C%20capsize%3D3%2C%20label%3Dname)%0A%20%20%20%20%20%20%20%20ax.axvline(0%2C%20color%3D%22%23888888%22%2C%20linewidth%3D0.8)%0A%20%20%20%20%20%20%20%20ax.set_yticks(rows%2C%20table%5B%22province%22%5D)%0A%20%20%20%20%20%20%20%20ax.set_xlabel(%22women%20among%20operators%20of%20large-scale%20units%20(%25)%2C%20with%20a%2095%25%20confidence%20interval%22)%0A%20%20%20%20%20%20%20%20ax.set_title(%22A%20small%20share%20in%20a%20small%20domain%3A%20two%20intervals%22)%0A%20%20%20%20%20%20%20%20ax.legend(frameon%3DFalse%2C%20loc%3D%22lower%20right%22)%0A%20%20%20%20%20%20%20%20ax.spines%5B%5B%22top%22%2C%20%22right%22%5D%5D.set_visible(False)%0A%20%20%20%20%20%20%20%20fig.tight_layout()%0A%20%20%20%20%20%20%20%20below%20%3D%20table%5Btable%5B%22wald_lci%22%5D%20%3C%200%5D%0A%20%20%20%20%20%20%20%20lowest%20%3D%20table.loc%5Btable%5B%22wald_lci%22%5D.idxmin()%5D%0A%20%20%20%20%20%20%20%20note%20%3D%20(f%22**%7Blen(below)%7D%20of%20%7Blen(table)%7D%20Wald%20intervals%20run%20below%200%25**%22%20%2B%20(f%22%2C%20the%20lowest%20to%20%7Blowest%5B'wald_lci'%5D%3A.1%25%7D%20in%20%7Blowest%5B'province'%5D%7D%22%20if%20len(below)%20else%20%22%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2B%20f%22%3B%20every%20logit%20interval%20stays%20between%200%25%20and%20100%25.%20Where%20the%20share%20is%20near%20the%20middle%20and%20the%20SE%20is%20small%2C%20the%20two%20agree%20to%20a%20decimal%20or%20two.%22)%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5Bax%2C%20mo.md(note)%5D)%0A%0A%20%20%20%20_chart()%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**Other%20intervals.**%20svy%20offers%20four%20through%20%60ci_method%3D%60%3A%20%60%22logit%22%60%20(the%20default%2C%20and%20the%20one%20your%20function%20reproduces)%2C%20%60%22beta%22%60%20(Clopper-Pearson%2C%20from%20the%20beta%20distribution%2C%20wider)%2C%20%60%22korn-graubard%22%60%20(Clopper-Pearson%20with%20the%20sample%20size%20adjusted%20for%20the%20design%20effect%20and%20the%20degrees%20of%20freedom)%20and%20%60%22wilson%22%60.%20R's%20%60svyciprop(~female%2C%20design%2C%20method%20%3D%20%22logit%22)%60%20is%20the%20same%20choice%2C%20and%20%60confint(svymean(~female%2C%20design))%60%20gives%20the%20Wald%20interval.%20Which%20one%20a%20survey%20publishes%20is%20its%20own%20decision%2C%20written%20in%20the%20table's%20notes.%20The%20cell%20below%20sets%20the%20four%20beside%20the%20Wald%20interval%20for%20the%20province%20with%20the%20widest%20one%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(operators_sample)%3A%0A%20%20%20%20%23%20One%20domain%2C%20five%20intervals%3A%20Wald%20by%20hand%2C%20and%20svy's%20four%20methods%0A%20%20%20%20_rows%20%3D%20%5B%5D%0A%20%20%20%20for%20_method%20in%20%5B%22logit%22%2C%20%22beta%22%2C%20%22korn-graubard%22%2C%20%22wilson%22%5D%3A%0A%20%20%20%20%20%20%20%20_prop%20%3D%20operators_sample.estimation.prop(y%3D%22female%22%2C%20by%3D%22province%22%2C%20where%3Dsvy.col(%22large_scale%22)%20%3D%3D%201%2C%20ci_method%3D_method).to_polars().to_pandas()%0A%20%20%20%20%20%20%20%20_rows.append(_prop%5B_prop%5B%22female%22%5D%20%3D%3D%201%5D.assign(method%3D_method))%0A%20%20%20%20_all%20%3D%20pd.concat(_rows%2C%20ignore_index%3DTrue)%0A%20%20%20%20_widest%20%3D%20_all.loc%5B_all%5B%22se%22%5D.idxmax()%2C%20%22province%22%5D%0A%20%20%20%20_one%20%3D%20_all%5B_all%5B%22province%22%5D%20%3D%3D%20_widest%5D%0A%20%20%20%20_first%20%3D%20_one.iloc%5B0%5D%0A%20%20%20%20_t%20%3D%20stats.t.ppf(0.975%2C%20_first%5B%22df%22%5D)%0A%20%20%20%20_wald%20%3D%20pd.DataFrame(%5B%7B%22method%22%3A%20%22Wald%2C%20est%20%C2%B1%20t%C2%B7SE%22%2C%20%22lci%22%3A%20_first%5B%22est%22%5D%20-%20_t%20*%20_first%5B%22se%22%5D%2C%20%22uci%22%3A%20_first%5B%22est%22%5D%20%2B%20_t%20*%20_first%5B%22se%22%5D%7D%5D)%0A%20%20%20%20_shown%20%3D%20pd.concat(%5B_wald%2C%20_one%5B%5B%22method%22%2C%20%22lci%22%2C%20%22uci%22%5D%5D%5D%2C%20ignore_index%3DTrue)%0A%20%20%20%20mo.vstack(%5Bmo.md(f%22**%7B_widest%7D**%3A%20%7B_first%5B'est'%5D%3A.1%25%7D%20of%20its%20operators%20of%20large-scale%20units%20are%20women%2C%20SE%20%7B_first%5B'se'%5D%3A.1%25%7D%2C%20%7Bint(_first%5B'df'%5D)%7D%20degrees%20of%20freedom.%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_shown.assign(lci%3Dlambda%20t%3A%20(t%5B%22lci%22%5D%20*%20100).round(1)%2C%20uci%3Dlambda%20t%3A%20(t%5B%22uci%22%5D%20*%20100).round(1)).rename(columns%3D%7B%22lci%22%3A%20%22lower%20(%25)%22%2C%20%22uci%22%3A%20%22upper%20(%25)%22%7D)%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%20S2.6%20%C2%B7%20The%20cross%20table%20as%20a%20reader%20sees%20it%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20module%207%20wrote%20long%20tables%2C%20one%20estimate%20per%20row%20(and%20S2.7%20below%20adds%20a%20%60flag%60%20column).%20A%20cross%20table%20puts%20one%20variable%20down%20the%20side%20and%20one%20across%20the%20top%2C%20so%20a%20reader%20compares%20a%20province's%20women%20and%20men%20in%20one%20line.%20With%20the%20flags%20gone%20as%20a%20column%2C%20each%20cell%20has%20to%20carry%20its%20own%20label%2C%20and%20statistical%20tables%20use%20symbols%20for%20it.%20This%20workbook%20uses%20three%2C%20explained%20in%20its%20notes%20sheet%3A%0A%0A%20%20%20%20%7C%20the%20cell's%20reliability%20%7C%20what%20the%20cell%20shows%20%7C%20what%20the%20cell%20holds%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20publish%20%7C%20%603%2C379%60%20%7C%20the%20estimate%2C%20a%20number%2C%20formatted%20%60%23%2C%23%230%60%20%7C%0A%20%20%20%20%7C%20caution%20%7C%20%60821*%60%20%7C%20the%20estimate%2C%20a%20number%2C%20formatted%20%60%23%2C%23%230%22*%22%60%3A%20the%20asterisk%20is%20part%20of%20the%20**format**%2C%20so%20the%20cell%20still%20adds%20up%20in%20a%20formula%20%7C%0A%20%20%20%20%7C%20suppress%20%7C%20%60..%60%20%7C%20the%20text%20%60..%60%3A%20no%20estimate%20is%20published%20%7C%0A%20%20%20%20%7C%20no%20sample%20%7C%20%60-%60%20%7C%20the%20text%20%60-%60%3A%20nobody%20in%20the%20sample%2C%20so%20nothing%20to%20estimate%20%7C%0A%0A%20%20%20%20A%20number%20format%20can%20carry%20text%20in%20quotes%3A%20Excel%20shows%20%60%23%2C%23%230%22*%22%60%20as%20the%20number%20with%20an%20asterisk%20after%20it%2C%20and%20the%20cell%20keeps%20the%20number.%20Writing%20the%20text%20%60%22821*%22%60%20instead%20would%20make%20the%20cell%20text%2C%20which%20a%20reader's%20%60SUM%60%20skips.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20workbook%2C%20a%20sheet%20title%2C%20a%20long%20table%20of%20cells%20(%60labeled_cells%60'%20or%20S2.4's%20columns%2C%20with%20%60est%60%20and%20%60reliability%60)%2C%20the%20names%20of%20the%20variables%20down%20the%20side%20(%60rows%60%2C%20a%20list%20of%20one%20or%20more)%20and%20the%20name%20of%20the%20variable%20across%20the%20top%20(%60column%60)%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%20names%20in%20%60rows%60%2C%20then%20each%20category%20of%20%60column%60%20in%20the%20order%20it%20first%20appears%20in%20the%20table%2C%20all%20**bold**%3B%0A%20%20%20%20-%20one%20row%20per%20combination%20of%20the%20%60rows%60%20variables%2C%20in%20the%20order%20it%20first%20appears%20in%20the%20table%3A%20the%20combination's%20values%2C%20then%20one%20cell%20per%20category%20of%20%60column%60%2C%20as%20the%20table%20above%20says%3B%0A%20%20%20%20-%20a%20combination%20the%20table%20does%20not%20hold%20shows%20%60-%60%2C%20as%20a%20cell%20with%20no%20sample%20does%3B%0A%20%20%20%20-%20the%20panes%20frozen%20at%20the%20first%20cell%20of%20numbers%3A%20%60B2%60%20with%20one%20variable%20down%20the%20side%2C%20%60C2%60%20with%20two.%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%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%201%20%7C%20**province**%20%7C%20**scale**%20%7C%20**Male**%20%7C%20**Female**%20%7C%0A%20%20%20%20%7C%202%20%7C%20La%20Union%20%7C%20Large-scale%20%7C%20%6060*%60%20%7C%20%60..%60%20%7C%0A%20%20%20%20%7C%203%20%7C%20La%20Union%20%7C%20Small-scale%20%7C%20%603%2C379%60%20%7C%20%60821*%60%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60workbook.create_sheet%60%2C%20%60sheet.append%60%2C%20%60sheet.cell(row%3D...%2C%20column%3D...%2C%20value%3D...)%60%2C%20%60cell.number_format%60%2C%20%60Font(bold%3DTrue)%60%2C%20%60sheet.freeze_panes%60%20(module%207%2C%20Exercise%207.2)%3B%20%60pd.unique%60%20keeps%20the%20order%20of%20first%20appearance%3B%20%60drop_duplicates%60%20on%20the%20%60rows%60%20columns%20does%20the%20same%20for%20combinations%3B%20%60get_column_letter(n)%60%20turns%203%20into%20%60%22C%22%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20cross_sheet(workbook%3A%20openpyxl.Workbook%2C%20title%3A%20str%2C%20table%3A%20pd.DataFrame%2C%20rows%3A%20list%2C%20column%3A%20str)%3A%0A%20%20%20%20%22%22%22Add%20a%20sheet%20called%20%60title%60%3A%20the%20%60rows%60%20variables%20down%20the%20side%2C%20the%20categories%20of%20%60column%60%20across%20the%20top%2C%20each%0A%20%20%20%20cell%20its%20estimate%20(formatted%2C%20with%20an%20asterisk%20for%20caution)%20or%20a%20symbol%20(..%20suppressed%2C%20-%20no%20sample).%20Returns%20it.%22%22%22%0A%20%20%20%20%23%20Exercise%20S2.6%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%20(create_sheet)%3B%20the%20header%3A%20the%20rows'%20names%2C%20then%20column's%20categories%20in%20order%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20of%20first%20appearance%20(pd.unique)%3B%20make%20the%20header%20bold%0A%20%20%20%20%23%20Step%202%3A%20look%20up%20a%20cell%20by%20its%20keys%3A%20the%20table%20indexed%20by%20the%20rows%20variables%20and%20the%20column%0A%20%20%20%20%23%20Step%203%3A%20for%20each%20combination%20of%20the%20rows%20variables%20(drop_duplicates%2C%20in%20order)%3A%20write%20the%20combination%2C%20then%20for%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20each%20category%20the%20cell%3A%20the%20estimate%20with%20NUMBER_FORMAT%20(publish)%20or%20CAUTION_FORMAT%20(caution)%2C%20or%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20SYMBOLS%5Breliability%5D%20for%20suppress%20and%20no%20sample%2C%20or%20%22-%22%20where%20the%20table%20has%20no%20such%20cell%0A%20%20%20%20%23%20Step%204%3A%20freeze%20the%20panes%20at%20the%20first%20cell%20of%20numbers%20(get_column_letter(len(rows)%20%2B%201)%20%2B%20%222%22)%3B%20return%20the%20sheet%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S2.6%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%20cross_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20made-up%20long%20table%20of%20cells%20with%20two%20variables%20down%20the%20side%20and%20one%20across%2C%20every%20reliability%20once%2C%20and%20one%0A%20%20%20%20combination%20missing%20(Bohol%2C%20Large-scale%2C%20Female).%20Used%20by%20the%20check%20of%20Exercise%20S2.6.%22%22%22%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%23%20province%2C%20scale%2C%20sex%2C%20est%2C%20reliability%0A%20%20%20%20%20%20%20%20(%22La%20Union%22%2C%20%22Large-scale%22%2C%20%22Male%22%2C%2059.53%2C%20%22caution%22)%2C%0A%20%20%20%20%20%20%20%20(%22La%20Union%22%2C%20%22Large-scale%22%2C%20%22Female%22%2C%2032.62%2C%20%22suppress%22)%2C%0A%20%20%20%20%20%20%20%20(%22La%20Union%22%2C%20%22Small-scale%22%2C%20%22Male%22%2C%203378.68%2C%20%22publish%22)%2C%0A%20%20%20%20%20%20%20%20(%22La%20Union%22%2C%20%22Small-scale%22%2C%20%22Female%22%2C%20821.15%2C%20%22caution%22)%2C%0A%20%20%20%20%20%20%20%20(%22Bohol%22%2C%20%22Large-scale%22%2C%20%22Male%22%2C%2068.74%2C%20%22suppress%22)%2C%0A%20%20%20%20%20%20%20%20(%22Bohol%22%2C%20%22Small-scale%22%2C%20%22Male%22%2C%203073.55%2C%20%22publish%22)%2C%0A%20%20%20%20%20%20%20%20(%22Bohol%22%2C%20%22Small-scale%22%2C%20%22Female%22%2C%200.0%2C%20%22no%20sample%22)%2C%0A%20%20%20%20%5D%0A%20%20%20%20return%20pd.DataFrame(rows%2C%20columns%3D%5B%22province%22%2C%20%22scale%22%2C%20%22sex%22%2C%20%22est%22%2C%20%22reliability%22%5D)%0A%0A%0A%40app.function%0Adef%20cross_summary(sheet)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22What%20a%20check%20of%20Exercise%20S2.6%20reads%20back%3A%20module%207's%20sheet_summary%2C%20with%20each%20number%20rounded%20to%204%20places%2C%20since%20a%0A%20%20%20%20cross%20table's%20cells%20are%20estimates%20straight%20from%20svy.%22%22%22%0A%20%20%20%20lines%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%20lines%5B0%5D%5D%20if%20lines%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(round(cell.value%2C%204)%20if%20isinstance(cell.value%2C%20float)%20else%20cell.value%2C%20cell.number_format)%20for%20cell%20in%20line%5D%20for%20line%20in%20lines%5B1%3A%5D%5D%2C%0A%20%20%20%20%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(operators_sample)%3A%0A%20%20%20%20%23%20The%20survey's%20operators%20by%20province%20and%20sex%20straight%20from%20svy%2C%20labeled%20by%20S2.3's%20rule%20(no%20grid)%0A%20%20%20%20_cells%20%3D%20operators_sample.estimation.total(y%3D%22operators%22%2C%20by%3D%5B%22province%22%2C%20%22sex%22%5D).to_polars().to_pandas()%0A%20%20%20%20_cells%5B%22reliability%22%5D%20%3D%20reliability_label(_cells%5B%22cv%22%5D)%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S2.6%22%2C%20cross_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(%22two%20variables%20down%20the%20side%2C%20every%20reliability%20once%2C%20a%20combination%20missing%2C%20into%20a%20workbook%20with%20no%20sheets%22%2C%20(blank_workbook()%2C%20%22By%20scale%20and%20sex%22%2C%20cross_fixture()%2C%20%5B%22province%22%2C%20%22scale%22%5D%2C%20%22sex%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20same%20cells%2C%20one%20variable%20down%20the%20side%2C%20into%20a%20workbook%20that%20has%20a%20sheet%22%2C%20(blank_workbook(%22Notes%22)%2C%20%22By%20sex%22%2C%20cross_fixture().query(%22scale%20%3D%3D%20'Large-scale'%22)%2C%20%5B%22province%22%5D%2C%20%22sex%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20operators%20by%20province%20and%20sex%2C%20from%20svy%22%2C%20(blank_workbook()%2C%20%22Province%20by%20sex%22%2C%20_cells%2C%20%5B%22province%22%5D%2C%20%22sex%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20same%2C%20turned%3A%20sex%20down%20the%20side%2C%20provinces%20across%22%2C%20(blank_workbook()%2C%20%22Sex%20by%20province%22%2C%20_cells%2C%20%5B%22sex%22%5D%2C%20%22province%22))%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%3Dcross_summary%2C%0A%20%20%20%20%20%20%20%20hint%3D%22create_sheet%3B%20a%20bold%20header%20of%20the%20rows'%20names%20and%20the%20column's%20categories%20(pd.unique)%3B%20per%20combination%2C%20the%20estimate%20formatted%20'%23%2C%23%230'%20or%20'%23%2C%23%230%5C%22*%5C%22'%2C%20or%20'..'%20or%20'-'%3B%20freeze%20at%20get_column_letter(len(rows)%20%2B%201)%20%2B%20'2'.%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%20small%20table%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%20cross_sheet(blank_workbook()%2C%20%22By%20scale%20and%20sex%22%2C%20cross_fixture()%2C%20%5B%22province%22%2C%20%22scale%22%5D%2C%20%22sex%22)%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(cross_sheet%2C%20blank_workbook()%2C%20%22By%20scale%20and%20sex%22%2C%20cross_fixture()%2C%20%5B%22province%22%2C%20%22scale%22%5D%2C%20%22sex%22)%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%60cross_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%20line%5D%20for%20line%20in%20sheet.iter_rows()%5D%0A%20%20%20%20%20%20%20%20return%20pd.DataFrame(cells%2C%20columns%3D%5Bget_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%20S2.6%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%60Female%60%20before%20%60Male%60%2C%20or%20the%20provinces%20in%20another%20order%20%7C%20%60pivot%60%20or%20%60groupby%60%20sorted%20the%20categories.%20%7C%20%60pd.unique%60%2C%20%60drop_duplicates%60%20%7C%0A%20%20%20%20%7C%20%60821*%60%20stored%20as%20text%20%7C%20The%20asterisk%20was%20added%20to%20the%20value.%20%7C%20the%20value%20a%20number%2C%20the%20asterisk%20in%20%60CAUTION_FORMAT%60%20%7C%0A%20%20%20%20%7C%20A%20suppressed%20cell%20shows%20a%20number%20%7C%20The%20estimate%20written%20for%20every%20reliability.%20%7C%20%60SYMBOLS%5Breliability%5D%60%20for%20suppress%20and%20no%20sample%20%7C%0A%20%20%20%20%7C%20*KeyError*%20on%20Bohol%2C%20Large-scale%2C%20Female%20%7C%20A%20combination%20the%20table%20lacks%20was%20looked%20up.%20%7C%20check%20%60where%20in%20cells.index%60%20first%2C%20and%20write%20%60-%60%20%7C%0A%20%20%20%20%7C%20%60panes%20frozen%20at%60%20is%20%60B2%60%20with%20two%20variables%20down%20the%20side%20%7C%20The%20panes%20fixed%20at%20%60B2%60.%20%7C%20%60get_column_letter(len(rows)%20%2B%201)%60%20%7C%0A%20%20%20%20%7C%20The%20sheets%20in%20the%20workbook%20are%20wrong%20%7C%20The%20table%20written%20into%20the%20open%20sheet.%20%7C%20%60create_sheet(title)%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%20second%20workbook%0A%0A%20%20%20%20The%20cell%20below%20puts%20the%20three%20exercises%20together%20into%20%60scratch%2Fstretch_tabulation%2Foperators.xlsx%60%3A%20the%20operators%20by%20province%20and%20sex%20with%20every%20margin%20(S2.4%2C%20S2.6)%2C%20the%20same%20by%20province%20and%20scale%20(S2.6%20with%20two%20variables%20down%20the%20side)%2C%20the%20share%20of%20women%20among%20all%20operators%20and%20among%20operators%20of%20large-scale%20units%20with%20their%20logit%20intervals%20(S2.5)%2C%20and%20a%20notes%20sheet%20that%20says%20where%20the%20numbers%20come%20from%2C%20which%20households%20were%20left%20out%20or%20moved%20and%20why%2C%20and%20what%20each%20symbol%20means.%20Every%20number%20in%20it%20is%20computed%20from%20the%20survey%20when%20the%20cell%20runs.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20plain_sheet(workbook%3A%20openpyxl.Workbook%2C%20title%3A%20str%2C%20table%3A%20pd.DataFrame%2C%20formats%3A%20dict%20%7C%20None%20%3D%20None)%3A%0A%20%20%20%20%22%22%22A%20sheet%20of%20a%20plain%20table%3A%20a%20bold%20header%2C%20the%20values%2C%20a%20number%20format%20per%20named%20column%2C%20panes%20frozen%20below%20the%20header.%22%22%22%0A%20%20%20%20sheet%20%3D%20workbook.create_sheet(title)%0A%20%20%20%20sheet.append(list(table.columns))%0A%20%20%20%20for%20cell%20in%20sheet%5B1%5D%3A%0A%20%20%20%20%20%20%20%20cell.font%20%3D%20Font(bold%3DTrue)%0A%20%20%20%20for%20record%20in%20table.to_dict(%22records%22)%3A%0A%20%20%20%20%20%20%20%20sheet.append(%5BNone%20if%20pd.isna(value)%20else%20value%20for%20value%20in%20record.values()%5D)%0A%20%20%20%20%20%20%20%20for%20cell%2C%20name%20in%20zip(sheet%5Bsheet.max_row%5D%2C%20table.columns)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20formats%20and%20name%20in%20formats%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cell.number_format%20%3D%20formats%5Bname%5D%0A%20%20%20%20sheet.freeze_panes%20%3D%20%22A2%22%0A%20%20%20%20return%20sheet%0A%0A%0A%40app.function%0Adef%20operators_notes(decisions%3A%20pd.DataFrame%2C%20regions%3A%20list%2C%20made_on%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20notes%20sheet%20of%20the%20second%20workbook%3A%20what%20the%20tables%20are%2C%20what%20was%20left%20out%2C%20the%20rule%20and%20the%20symbols.%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%22Operators%20of%20fishing%20units%3A%20estimates%20from%20the%20Survey%20of%20Fishing%20Households%202025%2C%20by%20province%20and%20sex%2C%20and%20by%20province%2C%20scale%20and%20sex%2C%20with%20every%20margin%3B%20the%20share%20of%20women%20among%20operators%22)%2C%0A%20%20%20%20%20%20%20%20(%22Unit%22%2C%20%22An%20operator%3A%20a%20household%20member%20who%20runs%20a%20fishing%20unit%20(mem_role_operator%20%3D%201)%2C%20counted%20once%20however%20many%20units%20they%20run%3B%20large-scale%20if%20one%20of%20those%20units%20is%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(%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%20f%22Strata%3A%20province%20by%20stratum%20(F%2C%20A%2C%20B%2C%20C).%20Sampling%20units%3A%20the%20enumeration%20areas%3B%20in%20stratum%20F%2C%20each%20household.%20Weight%3A%20sel_final_weight%2C%20the%20household's.%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(%22Publication%20rule%22%2C%20f%22The%20survey's%20own%3A%20publish%20up%20to%20a%20CV%20of%20%7BCAUTION_CV%3A.0%25%7D%3B%20caution%20above%20it%2C%20up%20to%20%7BSUPPRESS_CV%3A.0%25%7D%3B%20suppress%20above%20%7BSUPPRESS_CV%3A.0%25%7D%2C%20or%20with%20no%20CV%22)%2C%0A%20%20%20%20%20%20%20%20(%22*%22%2C%20f%22after%20a%20number%3A%20the%20estimate's%20CV%20is%20above%20%7BCAUTION_CV%3A.0%25%7D%2C%20up%20to%20%7BSUPPRESS_CV%3A.0%25%7D.%20Use%20with%20caution%22)%2C%0A%20%20%20%20%20%20%20%20(%22..%22%2C%20f%22suppressed%3A%20the%20estimate's%20CV%20is%20above%20%7BSUPPRESS_CV%3A.0%25%7D%2C%20or%20cannot%20be%20computed%22)%2C%0A%20%20%20%20%20%20%20%20(%22-%22%2C%20%22no%20operator%20in%20the%20sample%20falls%20in%20this%20cell%22)%2C%0A%20%20%20%20%20%20%20%20(%22Total%22%2C%20%22an%20estimate%20for%20every%20category%20of%20the%20variable%20together%2C%20estimated%20on%20its%20own%3A%20its%20SE%20does%20not%20follow%20from%20the%20cells'%22)%2C%0A%20%20%20%20%20%20%20%20(%22Shares%22%2C%20%22the%20share%20of%20women%2C%20with%20a%2095%25%20confidence%20interval%20on%20the%20logit%20scale%20(svy's%20default)%2C%20which%20stays%20between%200%25%20and%20100%25%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.cell%0Adef%20_(PROVINCES%2C%20operators_sample)%3A%0A%20%20%20%20%23%20The%20second%20workbook%2C%20from%20the%20three%20exercises'%20functions%2C%20written%20and%20read%20back%20with%20pandas%0A%20%20%20%20def%20_write()%3A%0A%20%20%20%20%20%20%20%20by_sex%20%3D%20attempt(with_margins%2C%20operators_sample%2C%20%22operators%22%2C%20%7B%22province%22%3A%20PROVINCES%2C%20%22sex%22%3A%20SEXES%7D)%0A%20%20%20%20%20%20%20%20by_scale%20%3D%20attempt(with_margins%2C%20operators_sample%2C%20%22operators%22%2C%20%7B%22province%22%3A%20PROVINCES%2C%20%22scale%22%3A%20SCALES%5B%3A2%5D%2C%20%22sex%22%3A%20SEXES%5B%3A2%5D%7D)%0A%20%20%20%20%20%20%20%20women%20%3D%20attempt(proportion_intervals%2C%20operators_sample.estimation.prop(y%3D%22female%22%2C%20by%3D%22province%22).to_polars().to_pandas()%2C%20%22female%22%2C%200.95)%0A%20%20%20%20%20%20%20%20women_large%20%3D%20attempt(proportion_intervals%2C%20operators_sample.estimation.prop(y%3D%22female%22%2C%20by%3D%22province%22%2C%20where%3Dsvy.col(%22large_scale%22)%20%3D%3D%201).to_polars().to_pandas()%2C%20%22female%22%2C%200.95)%0A%20%20%20%20%20%20%20%20if%20any(part%20is%20NOT_YET%20for%20part%20in%20(by_sex%2C%20by_scale%2C%20women%2C%20women_large))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercises%20S2.4%20and%20S2.5%22)%0A%20%20%20%20%20%20%20%20workbook%20%3D%20openpyxl.Workbook()%0A%20%20%20%20%20%20%20%20workbook.remove(workbook.active)%0A%20%20%20%20%20%20%20%20sheets%20%3D%20%5Battempt(cross_sheet%2C%20workbook%2C%20%22Province%20by%20sex%22%2C%20by_sex%2C%20%5B%22province%22%5D%2C%20%22sex%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20attempt(cross_sheet%2C%20workbook%2C%20%22Province%20by%20scale%20and%20sex%22%2C%20by_scale%2C%20%5B%22province%22%2C%20%22scale%22%5D%2C%20%22sex%22)%5D%0A%20%20%20%20%20%20%20%20if%20any(sheet%20is%20NOT_YET%20for%20sheet%20in%20sheets)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercise%20S2.6%22)%0A%20%20%20%20%20%20%20%20shares%20%3D%20pd.concat(%5Bwomen.assign(operators%3D%22all%20operators%22)%2C%20women_large.assign(operators%3D%22operators%20of%20large-scale%20units%22)%5D)%0A%20%20%20%20%20%20%20%20shares%20%3D%20shares%5B%5B%22operators%22%2C%20%22province%22%2C%20%22est%22%2C%20%22logit_lci%22%2C%20%22logit_uci%22%5D%5D.rename(columns%3D%7B%22est%22%3A%20%22share%20of%20women%22%2C%20%22logit_lci%22%3A%20%22lower%2C%2095%25%22%2C%20%22logit_uci%22%3A%20%22upper%2C%2095%25%22%7D)%0A%20%20%20%20%20%20%20%20plain_sheet(workbook%2C%20%22Share%20of%20women%22%2C%20shares%2C%20%7B%22share%20of%20women%22%3A%20%220.0%25%22%2C%20%22lower%2C%2095%25%22%3A%20%220.0%25%22%2C%20%22upper%2C%2095%25%22%3A%20%220.0%25%22%7D)%0A%20%20%20%20%20%20%20%20plain_sheet(workbook%2C%20%22Notes%22%2C%20operators_notes(design_decisions(course.planted_defects())%2C%20course.REGIONS%2C%20datetime.date.today().isoformat()))%0A%20%20%20%20%20%20%20%20for%20sheet%20in%20workbook.worksheets%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20cells%20in%20sheet.columns%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20longest%20%3D%20max((len(str(cell.value))%20for%20cell%20in%20cells%20if%20cell.value%20is%20not%20None)%2C%20default%3D8)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sheet.column_dimensions%5Bcells%5B0%5D.column_letter%5D.width%20%3D%20min(60%2C%20max(10%2C%20longest%20%2B%202))%0A%20%20%20%20%20%20%20%20SCRATCH.mkdir(parents%3DTrue%2C%20exist_ok%3DTrue)%0A%20%20%20%20%20%20%20%20workbook.save(SCRATCH%20%2F%20%22operators.xlsx%22)%0A%20%20%20%20%20%20%20%20back%20%3D%20pd.read_excel(SCRATCH%20%2F%20%22operators.xlsx%22%2C%20sheet_name%3DNone)%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22%60scratch%2Fstretch_tabulation%2Foperators.xlsx%60%2C%20read%20back%3A%20its%20sheets%20are%20**%7B'%2C%20'.join(back)%7D**.%20Open%20it%20in%20Excel%20to%20see%20the%20formats%20and%20the%20asterisks%3B%20pandas%20reads%20the%20values%20the%20cells%20keep.%20The%20first%20sheet%3A%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20back%5B%22Province%20by%20sex%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(%22The%20notes%3A%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20back%5B%22Notes%22%5D%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_write()%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%204%20%C2%B7%20The%20tabulation%20script%20and%20its%20report%0A%0A%20%20%20%20Module%207's%20section%202%20made%20the%20tabulation%20one%20plain%20script%2C%20%60scratch%2Fpublish%2Ftabulate.py%60%3A%20it%20reads%20the%20flat%20table%2C%20creates%20the%20variables%2C%20declares%20the%20design%2C%20estimates%20every%20table%20at%20every%20level%20with%20the%20sample's%20own%20figures%20beside%20each%20estimate%2C%20and%20writes%20one%20workbook%2C%20a%20sheet%20per%20table.%20This%20part%20gives%20that%20script%20what%20it%20needs%20when%20someone%20else%20runs%20it%2C%20perhaps%20on%20a%20schedule%20with%20nobody%20watching%3A%20its%20estimates%20flagged%20and%20suppressed%20by%20the%20survey's%20publication%20rule%2C%20a%20refusal%20when%20a%20region%20is%20missing%20from%20the%20flat%20table%2C%20checks%20before%20anything%20is%20estimated%2C%20an%20exit%20code%20per%20outcome%2C%20and%20a%20manifest%20of%20every%20file%20read%20and%20written.%20Then%20a%20report%20page%20whose%20every%20number%20is%20read%20from%20the%20script's%20outputs.%0A%0A%20%20%20%20**The%20data.**%20Module%207's%20functions%20and%20settings%2C%20given%20here%20as%20they%20are%20there%3A%20%60read_survey%60%2C%20%60read_decisions%60%2C%20%60design_decisions%60%20and%20%60read_geography%60%20read%20the%20survey%20from%20the%20regional%20databases%2C%20as%20module%207's%20section%201%20does%3B%20%60add_variables%60%2C%20%60design_sample%60%2C%20%60estimate%60%20(with%20its%20settings%20fixed%3A%20SE%2C%20CV%2C%20a%2095%25%20interval%2C%20no%20design%20effect%2C%20and%20the%20sample's%20own%20figures)%2C%20%60table_sheets%60%20(module%207's%20Exercise%207.3)%20and%20%60save_tables%60%20are%20the%20script's%20steps%3B%20%60add_sheet%60%20(module%207's%20Exercise%207.2)%20writes%20a%20notes%20sheet%3B%20%60LEVEL_CODES%60%2C%20%60INDICATORS%60%2C%20%60NUMBER_FORMATS%60%2C%20%60FIGURES%60%20and%20%60TABLES%60%20in%20the%20setup%20cell%20are%20module%207's%20settings.%20Module%204's%20design%20findings%20arrive%20as%20a%20file%20of%20decisions%2C%20%60design_decisions.csv%60%2C%20written%20from%20the%20course's%20list%20of%20planted%20defects%2C%20and%20the%20households%2C%20the%20decisions%20applied%2C%20are%20saved%20as%20the%20flat%20table%20the%20script%20reads%2C%20%60scratch%2Fstretch_tabulation%2Fhouseholds.parquet%60%2C%20as%20module%207%20saves%20its%20own.%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%3B%20the%20survey%20with%20them%20applied%3B%20and%20the%20flat%20table%20the%20script%0A%20%20%20%20%23%20reads%2C%20saved%20once%20as%20module%207%20saves%20its%20own%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%20frame.to_parquet(SCRATCH%20%2F%20%22households.parquet%22%2C%20index%3DFalse)%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%2Fstretch_tabulation%2Fdesign_decisions.csv%60%2C%20saved%20as%20%60scratch%2Fstretch_tabulation%2Fhouseholds.parquet%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.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.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.function%0Adef%20table_sheets(sample%3A%20svy.Sample)%20-%3E%20dict%5Bstr%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22Module%207's%20Exercise%207.3%3A%20every%20table%20of%20TABLES%20as%20one%20sheet%2C%20in%20that%20order%3A%20its%20national%2C%20regional%20and%20provincial%0A%20%20%20%20rows%20from%20estimate()%2C%20stacked%2C%20with%20a%20column%20level%3B%20the%20columns%20level%2C%20the%20provincial%20codes%2C%20the%20table's%20own%0A%20%20%20%20variables%2C%20then%20FIGURES.%22%22%22%0A%20%20%20%20sheets%20%3D%20%7B%7D%0A%20%20%20%20for%20table%2C%20(indicator%2C%20domains)%20in%20TABLES.items()%3A%0A%20%20%20%20%20%20%20%20column%2C%20statistic%2C%20_%20%3D%20INDICATORS%5Bindicator%5D%0A%20%20%20%20%20%20%20%20parts%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20level%2C%20codes%20in%20LEVEL_CODES.items()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20parts.append(estimate(sample%2C%20column%2C%20statistic%2C%20%5B*codes%2C%20*domains%5D).assign(level%3Dlevel))%0A%20%20%20%20%20%20%20%20sheets%5Btable%5D%20%3D%20pd.concat(parts%2C%20ignore_index%3DTrue)%5B%5B%22level%22%2C%20*LEVEL_CODES%5B%22provincial%22%5D%2C%20*domains%2C%20*FIGURES%5D%5D%0A%20%20%20%20return%20sheets%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%20add_sheet(workbook%3A%20openpyxl.Workbook%2C%20title%3A%20str%2C%20table%3A%20pd.DataFrame)%3A%0A%20%20%20%20%22%22%22Module%207's%20Exercise%207.2%3A%20add%20a%20sheet%20called%20%60title%60%20at%20the%20end%20of%20the%20workbook%2C%20holding%20the%20table%3A%20a%20bold%20header%2C%20the%0A%20%20%20%20values%20(a%20missing%20one%20as%20an%20empty%20cell)%2C%20each%20numeric%20column%20in%20its%20Excel%20number%20format%2C%20the%20panes%20frozen%20below%20the%0A%20%20%20%20header.%20Returns%20it.%22%22%22%0A%20%20%20%20sheet%20%3D%20workbook.create_sheet(title)%0A%20%20%20%20sheet.append(list(table.columns))%0A%20%20%20%20for%20cell%20in%20sheet%5B1%5D%3A%0A%20%20%20%20%20%20%20%20cell.font%20%3D%20Font(bold%3DTrue)%0A%20%20%20%20for%20record%20in%20table.to_dict(%22records%22)%3A%0A%20%20%20%20%20%20%20%20sheet.append(%5BNone%20if%20pd.isna(value)%20else%20value%20for%20value%20in%20record.values()%5D)%0A%20%20%20%20%20%20%20%20for%20cell%2C%20name%20in%20zip(sheet%5Bsheet.max_row%5D%2C%20table.columns)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20name%20in%20(%22estimate%22%2C%20%22se%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cell.number_format%20%3D%20INDICATORS%5Brecord%5B%22indicator%22%5D%5D%5B2%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20elif%20name%20in%20NUMBER_FORMATS%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cell.number_format%20%3D%20NUMBER_FORMATS%5Bname%5D%0A%20%20%20%20sheet.freeze_panes%20%3D%20%22A2%22%0A%20%20%20%20return%20sheet%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%20and%20every%20sheet%20from%20module%207's%20table_sheets%0A%20%20%20%20sample%20%3D%20design_sample(add_variables(frame))%0A%20%20%20%20tables%20%3D%20table_sheets(sample)%0A%20%20%20%20mo.md(f%22%60tables%60%3A%20**%7Blen(tables)%7D%20sheets**%2C%20one%20per%20table%20of%20%60TABLES%60%2C%20each%20with%20its%20%7Blen(LEVEL_CODES)%7D%20levels%20stacked%2C%20as%20module%207's%20script%20writes%20them.%22)%0A%20%20%20%20return%20sample%2C%20tables%0A%0A%0A%40app.cell%0Adef%20_(sample)%3A%0A%20%20%20%20%23%20La%20Union's%20households%20and%20its%20households%20running%20a%20large-scale%20unit%3A%20one%20estimate%20a%20reader%20can%20trust%2C%20one%20to%20read%20with%20care%0A%20%20%20%20def%20_la_union(column)%3A%0A%20%20%20%20%20%20%20%20return%20estimate(sample%2C%20column%2C%20%22total%22%2C%20%5B%22region_code%22%2C%20%22province_code%22%5D).query(%22province_code%20%3D%3D%20'033'%22).iloc%5B0%5D%0A%0A%20%20%20%20_all%2C%20_large%20%3D%20_la_union(%22hh_count%22)%2C%20_la_union(%22runs_large_unit%22)%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%23%23%23%20CV%20flags%3A%20which%20estimates%20a%20reader%20can%20trust%0A%0A%20%20%20%20The%20CV%20is%20the%20SE%20divided%20by%20the%20estimate%3A%20how%20large%20the%20sampling%20error%20is%2C%20relative%20to%20what%20is%20estimated.%20In%20La%20Union%2C%20the%20total%20of%20%7B_all%5B'estimate'%5D%3A%2C.0f%7D%20fishing%20households%20has%20an%20SE%20of%20%7B_all%5B'se'%5D%3A%2C.0f%7D%2C%20a%20CV%20of%20%7B_all%5B'cv'%5D%3A.1%25%7D.%20The%20total%20of%20%7B_large%5B'estimate'%5D%3A%2C.0f%7D%20households%20running%20a%20large-scale%20unit%20has%20an%20SE%20of%20%7B_large%5B'se'%5D%3A%2C.0f%7D%2C%20a%20CV%20of%20%7B_large%5B'cv'%5D%3A.0%25%7D%3A%20its%2095%25%20confidence%20interval%20runs%20from%20%7B_large%5B'lci'%5D%3A%2C.0f%7D%20to%20%7B_large%5B'uci'%5D%3A%2C.0f%7D.%20Publishing%20the%20%7B_large%5B'estimate'%5D%3A%2C.0f%7D%20without%20a%20mark%20invites%20a%20reader%20to%20quote%20it%20as%20if%20it%20were%20as%20good%20as%20the%20first.%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%20The%20survey's%20publication%20rule%20marks%20every%20estimate%20by%20its%20band.%20The%20tables%20use%20the%20rule%20of%20Exercise%20S2.3%20and%20its%20words%2C%20with%20its%20two%20thresholds%20kept%20in%20the%20setup%20cell%20so%20that%20the%20notebook%20and%20the%20script%20use%20the%20same%20values%3A%0A%0A%20%20%20%20%7C%20CV%20%7C%20S2.3's%20label%20%7C%20the%20%60flag%60%20column%20%7C%20what%20the%20tables%20do%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20up%20to%2020%25%20(%60CAUTION_CV%60)%20%7C%20publish%20%7C%20*(empty)*%20%7C%20publish%20%7C%0A%20%20%20%20%7C%20above%2020%25%2C%20up%20to%2035%25%20(%60SUPPRESS_CV%60)%20%7C%20caution%20%7C%20%60caution%60%20%7C%20publish%2C%20flagged%3A%20use%20with%20caution%20%7C%0A%20%20%20%20%7C%20above%2035%25%2C%20or%20no%20CV%20(missing%20or%20infinite)%20%7C%20suppress%20%7C%20%60suppress%60%20%7C%20publish%20the%20flag%20and%20the%20CV%2C%20but%20neither%20the%20estimate%2C%20nor%20its%20SE%2C%20nor%20its%20interval%20%7C%0A%0A%20%20%20%20Changing%20the%20two%20settings%20changes%20every%20table%20and%20the%20notes%20with%20them.%20The%20SE%20and%20the%20interval%20go%20with%20the%20estimate%20because%20each%20gives%20it%20back%3A%20the%20estimate%20is%20the%20SE%20divided%20by%20the%20CV%2C%20and%20the%20middle%20of%20its%20interval.%20An%20estimate%20with%20no%20CV%2C%20such%20as%20a%20total%20of%20zero%20(svy%20gives%20its%20CV%20as%20infinite)%2C%20cannot%20show%20its%20precision%2C%20so%20it%20is%20treated%20as%20the%20least%20reliable.%0A%0A%20%20%20%20The%20rule%20judges%20the%20weighted%20estimate%20and%20what%20goes%20with%20it.%20The%20columns%20%60unweighted%60%20and%20%60n%60%20describe%20the%20sample%2C%20not%20the%20population%3A%20they%20say%20how%20many%20interviews%20an%20estimate%20rests%20on%2C%20so%20they%20stay%20on%20every%20row%2C%20suppressed%20or%20not.%20Which%20estimates%20fall%20outside%20the%20publish%20band%3F%20The%20table%20below%20counts%20them%20per%20table%20of%20%60TABLES%60%2C%20over%20the%20three%20levels%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(tables)%3A%0A%20%20%20%20%23%20How%20many%20estimates%20fall%20in%20each%20CV%20band%2C%20per%20table%2C%20over%20the%20three%20levels%0A%20%20%20%20_all%20%3D%20pd.concat(%5Btable.assign(table%3Dname)%20for%20name%2C%20table%20in%20tables.items()%5D)%0A%20%20%20%20_band%20%3D%20pd.cut(_all%5B%22cv%22%5D%2C%20%5B0%2C%20CAUTION_CV%2C%20SUPPRESS_CV%2C%20np.inf%5D%2C%20labels%3D%5Bf%22publish%3A%20up%20to%20%7BCAUTION_CV%3A.0%25%7D%22%2C%20f%22caution%3A%20%7BCAUTION_CV%3A.0%25%7D%20to%20%7BSUPPRESS_CV%3A.0%25%7D%22%2C%20f%22suppress%3A%20above%20%7BSUPPRESS_CV%3A.0%25%7D%22%5D)%0A%20%20%20%20pd.crosstab(_all%5B%22table%22%5D%2C%20_band%2C%20dropna%3DFalse)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame%2C%20tables)%3A%0A%20%20%20%20%23%20Which%20rows%20leave%20the%20publish%20band%2C%20and%20where%20the%20sample's%20large-scale%20households%20are%20and%20how%20much%20each%20one%20weighs%0A%20%20%20%20_all%20%3D%20pd.concat(%5Btable.assign(table%3Dname)%20for%20name%2C%20table%20in%20tables.items()%5D)%0A%20%20%20%20_out%20%3D%20_all%5B_all%5B%22cv%22%5D%20%3E%20CAUTION_CV%5D%0A%20%20%20%20_where%20%3D%20%22%2C%20%22.join(sorted(f%22%60%7Bname%7D%60%22%20for%20name%20in%20_out%5B%22table%22%5D.unique()))%0A%20%20%20%20_scales%20%3D%20sorted(_out%5B%22scale%22%5D.dropna().unique())%20if%20%22scale%22%20in%20_out%20else%20%5B%5D%0A%20%20%20%20_large%20%3D%20frame%5Bframe%5B%22runs_large_unit%22%5D%20%3D%3D%201%5D%0A%20%20%20%20_in_area%20%3D%20_large%5B_large%5B%22sel_stratum%22%5D%20!%3D%20%22F%22%5D%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%7Blen(_out)%7D%20of%20%7Blen(_all)%7D%20estimates%20leave%20the%20publish%20band%2C%20all%20in%20%7B_where%7D%7B'%2C%20in%20its%20'%20%2B%20'%20and%20'.join(_scales)%20%2B%20'%20rows'%20if%20_scales%20else%20''%7D.%20Of%20the%20%7Blen(_large)%7D%20households%20in%20the%20sample%20that%20run%20a%20large-scale%20unit%2C%20%7Blen(_large)%20-%20len(_in_area)%7D%20were%20drawn%20in%20stratum%20F%2C%20which%20takes%20every%20registered%20large%20unit%2C%20so%20they%20add%20no%20sampling%20error.%20The%20other%20%7Blen(_in_area)%7D%20were%20drawn%20in%20the%20area%20strata%2C%20with%20a%20median%20weight%20of%20%7B_in_area%5B'sel_final_weight'%5D.median()%3A.0f%7D.%20Each%20one%20stands%20for%20that%20many%20households%2C%20and%20whether%20a%20drawn%20area%20holds%20one%20or%20none%20swings%20the%20total.%20The%20CV%20says%20so%2C%20and%20the%20flags%20pass%20it%20on%20to%20the%20reader.%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%20S2.7%20%C2%B7%20Flag%20and%20suppress%20by%20CV%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20flag%20computed%20by%20hand%20in%20Excel%20is%20a%20step%20someone%20forgets%20on%20the%20next%20batch.%20Computed%20by%20the%20script%2C%20from%20thresholds%20set%20in%20one%20place%2C%20it%20is%20there%20every%20time%2C%20and%20the%20notes%20sheet%20can%20count%20the%20flags.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20sheet%20as%20module%207's%20%60table_sheets%60%20gives%20it%20(the%20level%20and%20the%20domain's%20codes%2C%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)%2C%20and%20the%20two%20thresholds%20%E2%86%92%20a%20**copy**%20of%20the%20sheet%20with%20a%20new%20column%20%60flag%60%20and%20the%20unreliable%20estimates%20removed%3A%0A%0A%20%20%20%20%7C%20estimate%20%7C%20se%20%7C%20cv%20%7C%20lci%20%7C%20uci%20%7C%20%E2%86%92%20flag%20%7C%20%E2%86%92%20estimate%2C%20se%2C%20lci%2C%20uci%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%20100%20%7C%205%20%7C%200.05%20%7C%2090%20%7C%20110%20%7C%20%60%22%5C%22%60%20(empty%20text)%20%7C%20as%20they%20were%20%7C%0A%20%20%20%20%7C%20200%20%7C%2040%20%7C%200.20%20%7C%20120%20%7C%20280%20%7C%20%60%22%5C%22%60%20%7C%20as%20they%20were%20%7C%0A%20%20%20%20%7C%20300%20%7C%2075%20%7C%200.25%20%7C%20150%20%7C%20450%20%7C%20%60%22caution%22%60%20%7C%20as%20they%20were%20%7C%0A%20%20%20%20%7C%20500%20%7C%20200%20%7C%200.40%20%7C%20100%20%7C%20900%20%7C%20%60%22suppress%22%60%20%7C%20*missing*%20%7C%0A%20%20%20%20%7C%200%20%7C%200%20%7C%20*missing*%2C%20or%20infinite%20%7C%20*missing*%20%7C%20*missing*%20%7C%20%60%22suppress%22%60%20%7C%20*missing*%20%7C%0A%0A%20%20%20%20A%20CV%20equal%20to%20a%20threshold%20stays%20in%20the%20lower%20band%2C%20as%20in%20S2.3%3A%200.20%20has%20no%20flag%2C%200.35%20is%20%60caution%60.%20%60unweighted%60%20and%20%60n%60%20stay%20as%20they%20are.%20The%20input%20sheet%20must%20not%20change.%0A%0A%20%20%20%20**Tools%3A**%20%60np.select(conditions%2C%20choices%2C%20default%3D...)%60%20from%20module%205%2C%20%60.copy()%60%2C%20and%20%60.loc%5Brows%2C%20columns%5D%20%3D%20np.nan%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20apply_cv_flags(table%3A%20pd.DataFrame%2C%20caution%3A%20float%2C%20suppress%3A%20float)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20copy%20of%20the%20sheet%20with%20%60flag%60%3A%20%22%22%20up%20to%20%60caution%60%2C%20%22caution%22%20above%20it%20up%20to%20%60suppress%60%2C%20%22suppress%22%20above%0A%20%20%20%20%60suppress%60%20or%20without%20a%20finite%20CV%3B%20estimate%2C%20se%2C%20lci%20and%20uci%20are%20missing%20where%20the%20flag%20is%20%22suppress%22.%22%22%22%0A%20%20%20%20%23%20Exercise%20S2.7%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20a%20copy%20of%20the%20table%2C%20so%20the%20input%20stays%20as%20it%20was%0A%20%20%20%20%23%20Step%202%3A%20flag%3A%20np.select%20with%20the%20conditions%20cv%20%3C%3D%20caution%20and%20cv%20%3C%3D%20suppress%2C%20the%20choices%20%22%22%20and%20%22caution%22%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20and%20the%20default%20%22suppress%22%20(which%20also%20catches%20a%20missing%20or%20infinite%20cv%3A%20both%20fail%20the%20comparisons)%0A%20%20%20%20%23%20Step%203%3A%20where%20the%20flag%20is%20%22suppress%22%2C%20set%20estimate%2C%20se%2C%20lci%20and%20uci%20to%20missing%20(np.nan)%2C%20and%20return%20the%20copy%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S2.7%22)%0A%0A%0A%40app.function%0Adef%20cv_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Seven%20estimates%20of%20one%20province's%20sheet%20whose%20CVs%20sit%20below%2C%20on%20and%20between%20the%20thresholds%20of%20S2.3%2C%20one%20with%20no%20CV%0A%20%20%20%20and%20one%20with%20an%20infinite%20CV%20(svy's%20for%20a%20total%20of%20zero)%2C%20each%20with%20its%20interval%20and%20sample%20figures%2C%20used%20by%20the%0A%20%20%20%20checks.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22level%22%3A%20%5B%22provincial%22%5D%20*%207%2C%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2201%22%5D%20*%207%2C%0A%20%20%20%20%20%20%20%20%22province_code%22%3A%20%5B%22033%22%5D%20*%207%2C%0A%20%20%20%20%20%20%20%20%22category%22%3A%20%5B%22A%22%2C%20%22B%22%2C%20%22C%22%2C%20%22D%22%2C%20%22E%22%2C%20%22F%22%2C%20%22G%22%5D%2C%0A%20%20%20%20%20%20%20%20%22estimate%22%3A%20%5B100.0%2C%20200.0%2C%20300.0%2C%20400.0%2C%20500.0%2C%200.0%2C%200.0%5D%2C%0A%20%20%20%20%20%20%20%20%22se%22%3A%20%5B5.0%2C%2040.0%2C%2075.0%2C%20140.0%2C%20200.0%2C%200.0%2C%200.0%5D%2C%0A%20%20%20%20%20%20%20%20%22cv%22%3A%20%5B0.05%2C%200.20%2C%200.25%2C%200.35%2C%200.40%2C%20np.nan%2C%20np.inf%5D%2C%0A%20%20%20%20%20%20%20%20%22lci%22%3A%20%5B90.0%2C%20120.0%2C%20150.0%2C%20120.0%2C%20100.0%2C%20np.nan%2C%20np.nan%5D%2C%0A%20%20%20%20%20%20%20%20%22uci%22%3A%20%5B110.0%2C%20280.0%2C%20450.0%2C%20680.0%2C%20900.0%2C%20np.nan%2C%20np.nan%5D%2C%0A%20%20%20%20%20%20%20%20%22unweighted%22%3A%20%5B12.0%2C%2015.0%2C%209.0%2C%208.0%2C%206.0%2C%200.0%2C%200.0%5D%2C%0A%20%20%20%20%20%20%20%20%22n%22%3A%20%5B10%2C%2010%2C%2010%2C%2010%2C%2010%2C%2010%2C%2010%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.function%0Adef%20flags_story(table%3A%20pd.DataFrame%2C%20caution%3A%20float%2C%20suppress%3A%20float)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22What%20a%20check%20of%20Exercise%20S2.7%20compares%3A%20the%20flagged%20sheet%2C%20and%20whether%20the%20sheet%20given%20to%20the%20function%20is%20still%0A%20%20%20%20as%20it%20was%20(a%20solution%20that%20flags%20in%20place%20changes%20it).%22%22%22%0A%20%20%20%20before%20%3D%20table.copy()%0A%20%20%20%20flagged%20%3D%20apply_cv_flags(table%2C%20caution%2C%20suppress)%0A%20%20%20%20return%20%7B%22flagged%22%3A%20flagged%5Bsorted(flagged.columns)%5D.reset_index(drop%3DTrue).round(6)%2C%20%22input%20unchanged%22%3A%20table.equals(before)%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(sample%2C%20tables)%3A%0A%20%20%20%20%23%20The%20survey's%20households%20running%20a%20large-scale%20unit%20by%20province%2C%20from%20estimate()%3B%20its%20households%20by%20scale%2C%20from%20table_sheets%0A%20%20%20%20_large%20%3D%20estimate(sample%2C%20%22runs_large_unit%22%2C%20%22total%22%2C%20%5B%22region_code%22%2C%20%22province_code%22%5D)%0A%20%20%20%20_by_scale%20%3D%20tables%5B%22households_by_scale%22%5D%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S2.7%22%2C%20flags_story%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22CVs%20below%2C%20on%20and%20between%20the%20thresholds%200.20%20and%200.35%2C%20one%20missing%20and%20one%20infinite%22%2C%20(cv_fixture()%2C%200.20%2C%200.35))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20same%2C%20with%20other%20thresholds%20of%200.10%20and%200.20%22%2C%20(cv_fixture()%2C%200.10%2C%200.20))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20households%20running%20a%20large-scale%20unit%2C%20by%20province%22%2C%20(_large%2C%20CAUTION_CV%2C%20SUPPRESS_CV))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20households%20by%20scale%2C%20every%20level%22%2C%20(_by_scale%2C%20CAUTION_CV%2C%20SUPPRESS_CV))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sheets%20and%20thresholds%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22np.select(%5Bcv%20%3C%3D%20caution%2C%20cv%20%3C%3D%20suppress%5D%2C%20%5B''%2C%20'caution'%5D%2C%20default%3D'suppress')%20on%20a%20copy%3B%20then%20estimate%2C%20se%2C%20lci%20and%20uci%20missing%20where%20the%20flag%20is%20'suppress'.%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%20seven%20CVs%20of%20the%20check's%20small%20sheet%2C%20flagged%20with%20the%20survey's%20thresholds%0A%20%20%20%20try_it(apply_cv_flags%2C%20cv_fixture()%2C%20CAUTION_CV%2C%20SUPPRESS_CV)%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%20S2.7%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%20Every%20row%20%60suppress%60%20%7C%20The%20thresholds%20compared%20with%20a%20CV%20in%20percent%20(%60cv%20*%20100%20%3C%3D%200.20%60)%2C%20or%20the%20tests%20in%20the%20wrong%20order.%20%7C%20the%20CV%20is%20a%20fraction%3B%20%60cv%20%3C%3D%20caution%60%20first%20%7C%0A%20%20%20%20%7C%20The%20row%20with%20CV%200.20%20flagged%20%60caution%60%2C%20or%200.35%20flagged%20%60suppress%60%20%7C%20%60%3C%60%20where%20the%20band%20includes%20its%20threshold.%20%7C%20%60%3C%3D%60%20%7C%0A%20%20%20%20%7C%20The%20row%20with%20no%20CV%20has%20no%20flag%20%7C%20%60cv%20%3E%20suppress%60%20tested%20for%20%60suppress%60%3A%20a%20missing%20value%20fails%20every%20comparison.%20%7C%20let%20%60suppress%60%20be%20the%20default%20%7C%0A%20%20%20%20%7C%20%60input%20unchanged%60%20is%20%60False%60%20%7C%20The%20column%20was%20added%20to%20the%20sheet%20itself.%20%7C%20%60table.copy()%60%20first%20%7C%0A%20%20%20%20%7C%20Suppressed%20rows%20still%20show%20an%20SE%20or%20an%20interval%20%7C%20Only%20the%20estimate%20was%20removed.%20%7C%20%60.loc%5B...%2C%20%5B%22estimate%22%2C%20%22se%22%2C%20%22lci%22%2C%20%22uci%22%5D%5D%60%20%7C%0A%20%20%20%20%7C%20Suppressed%20rows%20lose%20%60unweighted%60%20or%20%60n%60%20too%20%7C%20Every%20number%20of%20the%20row%20was%20removed.%20%7C%20only%20the%20four%20columns%20of%20the%20estimate%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%0Adef%20_(tables)%3A%0A%20%20%20%20%23%20The%20households%20by%20scale%20as%20the%20script%20will%20write%20them%3A%20flagged%2C%20the%20suppressed%20estimates%20removed%0A%20%20%20%20_flagged%20%3D%20attempt(apply_cv_flags%2C%20tables%5B%22households_by_scale%22%5D%2C%20CAUTION_CV%2C%20SUPPRESS_CV)%0A%20%20%20%20mo.stop(_flagged%20is%20NOT_YET%2C%20waiting_for(%22Exercise%20S2.7%22))%0A%20%20%20%20_flagged%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%20What%20a%20saved%20workbook%20remembers%0A%0A%20%20%20%20The%20script%20must%20write%20the%20same%20outputs%20when%20it%20runs%20twice%20on%20the%20same%20inputs%2C%20and%20%22the%20same%22%20needs%20a%20definition.%20openpyxl%20and%20xlsxwriter%20differ%20here.%20A%20workbook%20is%20a%20zip%20file%20of%20XML%20documents%2C%20and%20it%20records%20when%20it%20was%20saved.%20openpyxl%20writes%20the%20moment%20of%20saving%20into%20the%20file%20each%20time%3B%20xlsxwriter%20writes%20the%20creation%20date%20you%20give%20it%2C%20or%20the%20current%20one.%20The%20cell%20below%20saves%20the%20same%20small%20table%20twice%20with%20each%20library%2C%20a%20second%20apart%2C%20and%20compares%20the%20files%20byte%20for%20byte.%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%20same%20small%20table%2C%20saved%20twice%20by%20each%20library%20a%20second%20apart%3A%20are%20the%20two%20files%20identical%2C%20byte%20for%20byte%3F%0A%20%20%20%20def%20_openpyxl(path)%3A%0A%20%20%20%20%20%20%20%20workbook%20%3D%20openpyxl.Workbook()%0A%20%20%20%20%20%20%20%20workbook.active.append(%5B%22domain%22%2C%20%22estimate%22%5D)%0A%20%20%20%20%20%20%20%20workbook.active.append(%5B%22La%20Union%22%2C%203812.4467%5D)%0A%20%20%20%20%20%20%20%20workbook.save(path)%0A%20%20%20%20%20%20%20%20return%20path.read_bytes()%0A%0A%20%20%20%20def%20_xlsxwriter(path)%3A%0A%20%20%20%20%20%20%20%20workbook%20%3D%20xlsxwriter.Workbook(path)%0A%20%20%20%20%20%20%20%20workbook.set_properties(%7B%22created%22%3A%20datetime.datetime(2026%2C%201%2C%201)%7D)%20%20%23%20a%20fixed%20creation%20date%0A%20%20%20%20%20%20%20%20sheet%20%3D%20workbook.add_worksheet()%0A%20%20%20%20%20%20%20%20sheet.write_row(0%2C%200%2C%20%5B%22domain%22%2C%20%22estimate%22%5D)%0A%20%20%20%20%20%20%20%20sheet.write_row(1%2C%200%2C%20%5B%22La%20Union%22%2C%203812.4467%5D)%0A%20%20%20%20%20%20%20%20workbook.close()%0A%20%20%20%20%20%20%20%20return%20path.read_bytes()%0A%0A%20%20%20%20SCRATCH.mkdir(parents%3DTrue%2C%20exist_ok%3DTrue)%0A%20%20%20%20_first%20%3D%20%7B%22openpyxl%22%3A%20_openpyxl(SCRATCH%20%2F%20%22saved_1.xlsx%22)%2C%20%22xlsxwriter%22%3A%20_xlsxwriter(SCRATCH%20%2F%20%22saved_1w.xlsx%22)%7D%0A%20%20%20%20time.sleep(1.1)%0A%20%20%20%20_second%20%3D%20%7B%22openpyxl%22%3A%20_openpyxl(SCRATCH%20%2F%20%22saved_2.xlsx%22)%2C%20%22xlsxwriter%22%3A%20_xlsxwriter(SCRATCH%20%2F%20%22saved_2w.xlsx%22)%7D%0A%20%20%20%20with%20zipfile.ZipFile(SCRATCH%20%2F%20%22saved_2.xlsx%22)%20as%20_zip%3A%0A%20%20%20%20%20%20%20%20_stamp%20%3D%20re.search(r%22%3Cdcterms%3Amodified%5B%5E%3E%5D*%3E(%5B%5E%3C%5D%2B)%3C%22%2C%20_zip.read(%22docProps%2Fcore.xml%22).decode()).group(1)%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%7C%20library%20%7C%20the%20two%20files%20identical%3F%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20openpyxl%20%7C%20**%7B'yes'%20if%20_first%5B'openpyxl'%5D%20%3D%3D%20_second%5B'openpyxl'%5D%20else%20'no'%7D**%20%7C%0A%20%20%20%20%7C%20xlsxwriter%2C%20with%20a%20fixed%20creation%20date%20%7C%20**%7B'yes'%20if%20_first%5B'xlsxwriter'%5D%20%3D%3D%20_second%5B'xlsxwriter'%5D%20else%20'no'%7D**%20%7C%0A%0A%20%20%20%20Inside%20openpyxl's%20second%20file%2C%20%60docProps%2Fcore.xml%60%20says%20it%20was%20modified%20at%20**%7B_stamp%7D**%20(UTC)%3A%20the%20second%20it%20was%20saved.%20Module%207's%20%60save_tables%60%20writes%20through%20openpyxl%2C%20so%20the%20script%20needs%20a%20way%20to%20say%20two%20runs%20wrote%20%22the%20same%20tables%22%3B%20comparing%20the%20files'%20bytes%20cannot%20be%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%20The%20tabulation%20as%20one%20re-runnable%20script%20with%20a%20log%20and%20an%20output%20manifest%0A%0A%20%20%20%20**The%20problem.**%20Module%207's%20script%20runs%20every%20step%20in%20order%2C%20and%20it%20does%20what%20it%20is%20given.%20On%20the%20next%20batch%20someone%20else%20runs%20it%2C%20perhaps%20on%20a%20schedule%20with%20nobody%20watching%2C%20and%20the%20failure%20that%20matters%20is%20a%20quiet%20one.%20Suppose%20the%20flat%20table%20was%20made%20before%20region%2004's%20database%20arrived.%20The%20script%20reads%20it%2C%20estimates%2C%20and%20writes%20a%20workbook%20that%20looks%20like%20every%20other%20run's%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(decisions%2C%20frame)%3A%0A%20%20%20%20%23%20What%20the%20script%20would%20publish%20from%20a%20flat%20table%20made%20before%20region%2004%20arrived%0A%20%20%20%20_all%20%3D%20estimate(design_sample(frame)%2C%20%22hh_count%22%2C%20%22total%22)%0A%20%20%20%20_two%20%3D%20estimate(design_sample(read_survey(PATHS.data%2C%20%5B%2201%22%2C%20%2207%22%5D%2C%20decisions))%2C%20%22hh_count%22%2C%20%22total%22)%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%7C%20the%20%22national%22%20total%20of%20fishing%20households%2C%20from%20%7C%20estimate%20%7C%20CV%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20all%20three%20regions%20%7C%20%7B_all%5B'estimate'%5D.iloc%5B0%5D%3A%2C.0f%7D%20%7C%20%7B_all%5B'cv'%5D.iloc%5B0%5D%3A.1%25%7D%20%7C%0A%20%20%20%20%7C%20regions%2001%20and%2007%20only%20%7C%20%7B_two%5B'estimate'%5D.iloc%5B0%5D%3A%2C.0f%7D%20%7C%20%7B_two%5B'cv'%5D.iloc%5B0%5D%3A.1%25%7D%20%7C%0A%0A%20%20%20%20The%20second%20total%20is%20**%7B1%20-%20_two%5B'estimate'%5D.iloc%5B0%5D%20%2F%20_all%5B'estimate'%5D.iloc%5B0%5D%3A.0%25%7D%20too%20low**%2C%20and%20its%20CV%20is%20about%20as%20small%20as%20the%20first%20one's.%20Nothing%20in%20the%20table%20says%20a%20region%20is%20missing%3A%20the%20precision%20is%20real%2C%20the%20coverage%20is%20not.%20The%20script%20must%20refuse%20to%20publish%20it%20unless%20someone%20decides%2C%20on%20purpose%2C%20that%20a%20partial%20table%20is%20what%20they%20want.%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**The%20tool.**%20A%20Python%20script%20run%20from%20a%20terminal%2C%20with%20**options**%20given%20on%20the%20command%20line%2C%20is%20R's%20habit%20of%20%60Rscript%20tabulate.R%20--data%20data%60%20in%20another%20language.%20Module%207's%20script%20has%20no%20options%3A%20its%20paths%20are%20settings.%20This%20part%20adds%20them%2C%20and%20builds%20%60scratch%2Fstretch_tabulation%2Ftabulate.py%60%2C%20which%20does%20what%20a%20command%20anyone%20can%20run%20must%20do%3A%0A%0A%20%20%20%20%7C%20what%20it%20does%20%7C%20why%20%7C%20R's%20way%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20reads%20its%20options%2C%20typed%2C%20and%20shows%20them%20with%20%60--help%60%2C%20an%20example%20included%20%7C%20the%20next%20person%20learns%20how%20to%20run%20it%20from%20the%20command%2C%20not%20from%20you%20%7C%20%60optparse%60%20or%20%60argparse%60%20packages%20%7C%0A%20%20%20%20%7C%20refuses%20when%20an%20expected%20region%20has%20no%20rows%20in%20the%20flat%20table%2C%20unless%20%60--allow-missing%60%20is%20given%20%7C%20a%20partial%20table%20looks%20like%20a%20whole%20one%3B%20accepting%20it%20must%20be%20a%20decision%20%7C%20%60stop()%60%20%7C%0A%20%20%20%20%7C%20writes%20the%20tables%20to%20files%20and%20its%20log%20to%20the%20terminal's%20error%20stream%20(stderr)%20%7C%20the%20log%20is%20for%20the%20person%20running%20it%3B%20the%20files%20are%20the%20product%20%7C%20%60message()%60%20writes%20to%20stderr%2C%20%60cat()%60%20to%20stdout%20%7C%0A%20%20%20%20%7C%20ends%20with%20an%20exit%20code%20that%20says%20how%20it%20went%20%7C%20a%20schedule%2C%20or%20another%20script%2C%20can%20tell%20success%20from%20failure%20without%20reading%20the%20log%20%7C%20%60quit(status%20%3D%2066)%60%20%7C%0A%20%20%20%20%7C%20writes%20the%20same%20outputs%20when%20run%20twice%20on%20the%20same%20inputs%20%7C%20a%20re-run%20fixes%20nothing%20by%20accident%20and%20breaks%20nothing%20either%2C%20and%20its%20manifest%20proves%20it%20%7C%20%7C%0A%0A%20%20%20%20Two%20ideas%20carry%20this%20part.%20**Exit%20codes**%3A%20a%20program%20that%20ends%20returns%20a%20number%20to%20whatever%20ran%20it%3B%200%20means%20success%2C%20anything%20else%20a%20failure%2C%20and%20the%20number%20says%20which.%20This%20script%20uses%20the%20conventions%20of%20%60sysexits.h%60%2C%20a%20list%20most%20command-line%20tools%20share%3A%2066%20for%20an%20input%20that%20is%20missing%2C%2065%20for%20data%20that%20fails%20a%20check%2C%202%20for%20an%20option%20written%20wrong.%20**stdout%20and%20stderr**%3A%20a%20program%20has%20two%20output%20streams.%20Standard%20output%20(stdout)%20is%20for%20the%20result%2C%20what%20another%20program%20might%20read%3B%20standard%20error%20(stderr)%20is%20for%20messages%20to%20the%20person%2C%20the%20log%20included.%20A%20terminal%20shows%20both%2C%20but%20%602%3E%20run.log%60%20keeps%20the%20log%20in%20a%20file%20and%20leaves%20stdout%20alone.%0A%0A%20%20%20%20The%20script%20is%20module%207's%20functions%20with%20the%20new%20ones%20added%2C%20all%20in%20this%20notebook.%20First%2C%20the%20refusal.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.class_definition%0Aclass%20MissingRegions(Exception)%3A%0A%20%20%20%20%22%22%22Raised%20when%20an%20expected%20region%20has%20no%20rows%20in%20the%20flat%20table%3A%20tables%20made%20without%20it%20would%20leave%20it%20out%20without%0A%20%20%20%20saying%20so.%22%22%22%0A%0A%20%20%20%20def%20__init__(self%2C%20missing%3A%20list%5Bstr%5D)%20-%3E%20None%3A%0A%20%20%20%20%20%20%20%20self.missing%20%3D%20list(missing)%0A%20%20%20%20%20%20%20%20super().__init__(%22no%20rows%20for%20region%20%22%20%2B%20%22%2C%20%22.join(self.missing))%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%60MissingRegions%60%20is%20an%20**exception**%20of%20the%20script's%20own%3A%20an%20error%20with%20a%20name%2C%20raised%20where%20the%20problem%20is%20found%20and%20caught%20where%20the%20decision%20is%20made.%20%60raise%20MissingRegions(%5B%2204%22%5D)%60%20stops%20the%20function%20with%20the%20message%20%60no%20rows%20for%20region%2004%60%3B%20the%20command%20catches%20it%2C%20writes%20the%20message%20to%20the%20log%2C%20and%20exits%20with%2066.%20Reading%20an%20error%20from%20its%20last%20line%2C%20as%20every%20module%20has%20taught%2C%20works%20here%20too%3A%20%60MissingRegions%3A%20no%20rows%20for%20region%2004%60%20names%20both%20the%20kind%20of%20error%20and%20the%20region.%0A%0A%20%20%20%20%23%23%20S2.8%20%C2%B7%20Refuse%20a%20missing%20region%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20function%20is%20the%20difference%20between%20a%20script%20that%20stops%20and%20says%20%22region%2004%20is%20missing%22%20and%20one%20that%20publishes%20the%20total%20above.%20Its%20default%20is%20to%20refuse%3B%20accepting%20a%20partial%20run%20takes%20a%20flag%20someone%20has%20to%20type.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20flat%20table%20(one%20row%20per%20household%2C%20with%20%60region_code%60%20as%20zero-padded%20text)%2C%20the%20list%20of%20expected%20regions%2C%20and%20%60allow_missing%60%20%E2%86%92%20the%20expected%20regions%20that%20have%20at%20least%20one%20row%20in%20the%20table%2C%20in%20the%20order%20of%20%60expected%60%3A%0A%0A%20%20%20%20%7C%20the%20table's%20region_code%20values%20%7C%20expected%20%7C%20allow_missing%20%7C%20result%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%2001%2C%2004%2C%2007%20%7C%2001%2C%2004%2C%2007%20%7C%20False%20%7C%20%60%5B%2201%22%2C%20%2204%22%2C%20%2207%22%5D%60%20%7C%0A%20%20%20%20%7C%2007%2C%2001%2C%207%2C%2013%20%7C%2001%2C%2004%2C%2007%20%7C%20False%20%7C%20raises%20%60MissingRegions(%5B%2204%22%5D)%60%20%7C%0A%20%20%20%20%7C%2007%2C%2001%2C%207%2C%2013%20%7C%2001%2C%2004%2C%2007%20%7C%20True%20%7C%20%60%5B%2201%22%2C%20%2207%22%5D%60%20%7C%0A%0A%20%20%20%20Only%20the%20expected%20codes%20count%3A%20%607%60%20is%20not%20region%2007%20(module%202%20keeps%20codes%20as%20zero-padded%20text)%2C%20and%20%6013%60%20is%20not%20a%20region%20this%20survey%20expects.%20Rows%20like%20those%20are%20not%20this%20function's%20to%20judge%3A%20their%20strata%20have%20no%20count%20in%20%60stratum_counts.csv%60%2C%20so%20the%20check%20of%20S2.9%20stops%20them.%0A%0A%20%20%20%20**Tools%3A**%20%60.isin%60%20or%20%60set%60%20from%20Arrays%20%26%20DataFrames%3B%20a%20list%20comprehension%20over%20%60expected%60%3B%20%60raise%20MissingRegions(missing)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20regions_to_read(flat%3A%20pd.DataFrame%2C%20expected%3A%20list%5Bstr%5D%2C%20allow_missing%3A%20bool)%20-%3E%20list%5Bstr%5D%3A%0A%20%20%20%20%22%22%22The%20expected%20regions%20with%20at%20least%20one%20row%20in%20the%20flat%20table%2C%20in%20the%20order%20of%20%60expected%60.%20Raises%20MissingRegions%0A%20%20%20%20naming%20the%20others%2C%20unless%20allow_missing%20is%20True.%22%22%22%0A%20%20%20%20%23%20Exercise%20S2.8%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20region%20codes%20the%20table%20holds%20(its%20region_code%20values%2C%20once%20each)%0A%20%20%20%20%23%20Step%202%3A%20the%20expected%20regions%20among%20them%2C%20in%20the%20order%20of%20expected%3B%20and%20the%20expected%20regions%20not%20among%20them%0A%20%20%20%20%23%20Step%203%3A%20if%20any%20are%20missing%20and%20allow_missing%20is%20False%2C%20raise%20MissingRegions(missing)%0A%20%20%20%20%23%20Step%204%3A%20return%20the%20regions%20that%20are%20there%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S2.8%22)%0A%0A%0A%40app.function%0Adef%20region_tables()%20-%3E%20dict%5Bstr%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22Three%20small%20flat%20tables%2C%20for%20the%20checks%3A%20every%20region%20there%3B%20region%2004%20missing%20beside%20rows%20coded%207%20and%2013%2C%20out%20of%0A%20%20%20%20order%3B%20and%20a%20table%20with%20no%20rows.%22%22%22%0A%20%20%20%20codes%20%3D%20%7B%22all_three%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2204%22%2C%20%2207%22%5D%2C%20%2204_missing%22%3A%20%5B%2207%22%2C%20%2201%22%2C%20%227%22%2C%20%2213%22%2C%20%2201%22%5D%2C%20%22empty%22%3A%20%5B%5D%7D%0A%20%20%20%20return%20%7Bname%3A%20pd.DataFrame(%7B%22region_code%22%3A%20pd.Series(values%2C%20dtype%3D%22str%22)%2C%20%22hh_count%22%3A%201%7D)%20for%20name%2C%20values%20in%20codes.items()%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(frame)%3A%0A%20%20%20%20_tables%20%3D%20region_tables()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S2.8%22%2C%20regions_to_read%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22all%20three%20regions%20there%22%2C%20(_tables%5B%22all_three%22%5D%2C%20EXPECTED_REGIONS%2C%20False))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2004%20missing%2C%20beside%20rows%20coded%207%20and%2013%22%2C%20(_tables%5B%2204_missing%22%5D%2C%20EXPECTED_REGIONS%2C%20False))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20same%20table%2C%20with%20allow_missing%22%2C%20(_tables%5B%2204_missing%22%5D%2C%20EXPECTED_REGIONS%2C%20True))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20table%20with%20no%20rows%2C%20with%20allow_missing%22%2C%20(_tables%5B%22empty%22%5D%2C%20EXPECTED_REGIONS%2C%20True))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20flat%20table%22%2C%20(frame%2C%20EXPECTED_REGIONS%2C%20False))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20flat%20table%20without%20region%2004%22%2C%20(frame%5Bframe%5B%22region_code%22%5D%20!%3D%20%2204%22%5D%2C%20EXPECTED_REGIONS%2C%20False))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22flat%20tables%22%2C%20raises%3DTrue%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Go%20through%20the%20expected%20regions%2C%20keep%20the%20ones%20with%20rows%20in%20the%20table%2C%20and%20raise%20MissingRegions(missing)%20when%20some%20are%20missing%20and%20allow_missing%20is%20False.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20survey's%20flat%20table%3B%20then%20try%20frame%5Bframe%5B%22region_code%22%5D%20!%3D%20%2204%22%5D%20in%20its%20place%0A%20%20%20%20try_it(regions_to_read%2C%20frame%2C%20EXPECTED_REGIONS%2C%20False)%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%20S2.8%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%60%5B'01'%2C%20'07'%2C%20'13'%2C%20'7'%5D%60%2C%20or%20no%20refusal%20when%2004%20is%20missing%20%7C%20The%20table's%20own%20codes%20were%20taken%20(%60unique()%60)%2C%20so%20every%20code%20counts.%20%7C%20loop%20over%20%60expected%60%20%7C%0A%20%20%20%20%7C%20A%20list%20where%20the%20check%20expects%20%60MissingRegions%3A%20no%20rows%20for%20region%2004%60%20%7C%20The%20missing%20regions%20were%20found%20but%20not%20raised.%20%7C%20%60if%20missing%20and%20not%20allow_missing%3A%20raise%20MissingRegions(missing)%60%20%7C%0A%20%20%20%20%7C%20%60MissingRegions%60%20raised%20with%20%60allow_missing%3DTrue%60%20%7C%20The%20flag%20was%20not%20consulted.%20%7C%20%60and%20not%20allow_missing%60%20%7C%0A%20%20%20%20%7C%20%60MissingRegions%3A%20no%20rows%20for%20region%200%2C%204%60%20%7C%20The%20region%20was%20passed%20as%20text%2C%20not%20in%20a%20list.%20%7C%20%60MissingRegions(missing)%60%2C%20a%20list%20%7C%0A%20%20%20%20%7C%20The%20regions%20in%20another%20order%20%7C%20Built%20from%20the%20table's%20rows%20rather%20than%20from%20%60expected%60.%20%7C%20loop%20over%20%60expected%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%20Checks%20before%20publishing%0A%0A%20%20%20%20A%20missing%20region%20is%20one%20way%20the%20input%20can%20be%20wrong%3B%20the%20data%20can%20be%20wrong%20in%20ways%20no%20count%20of%20regions%20shows.%20The%20script%20checks%20what%20the%20estimates%20rest%20on%20before%20it%20computes%20them%2C%20so%20that%20a%20problem%20stops%20the%20run%20with%20a%20sentence%20a%20person%20can%20act%20on%2C%20rather%20than%20an%20error%20from%20deep%20inside%20svy%20or%2C%20worse%2C%20a%20table%20that%20looks%20fine.%0A%0A%20%20%20%20The%20first%20check%20is%20one%20svy%20needs%3A%20in%20every%20stratum%2C%20the%20sample%20cannot%20hold%20more%20sampling%20units%20than%20the%20population%20has.%20The%20finite%20population%20correction%20is%201%20%E2%88%92%20n%2FN%2C%20the%20share%20of%20the%20stratum's%20units%20left%20out%20of%20the%20sample%3B%20with%20n%20larger%20than%20N%20it%20is%20negative%2C%20which%20has%20no%20meaning.%20Here%20is%20what%20happens%20when%20the%20design%20decisions%20are%20not%20applied%2C%20so%20the%20three%20copied%20households%20stay%20in%3A%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%20survey%20without%20the%20design%20decisions%2C%20the%20copies%20left%20in%3A%20svy%20refuses%2C%20and%20its%20message's%20last%20lines%20say%20why%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20estimate(design_sample(read_survey(PATHS.data%2C%20EXPECTED_REGIONS))%2C%20%22hh_count%22%2C%20%22total%22)%0A%20%20%20%20%20%20%20%20_said%20%3D%20%22no%20error%22%0A%20%20%20%20except%20Exception%20as%20_error%3A%0A%20%20%20%20%20%20%20%20_said%20%3D%20f%22%7Btype(_error).__name__%7D%3A%20%22%20%2B%20%22%20%22.join(str(_error).split())%0A%20%20%20%20mo.md(f%22%60estimate(...)%60%20on%20the%20survey%20with%20the%20copies%20left%20in%3A%5Cn%5Cn%60%60%60text%5Cn%7Btextwrap.fill(_said%2C%20110)%7D%5Cn%60%60%60%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%20svy%20names%20the%20code%20%60FPC_POP_LT_SAMPLE%60%20and%20a%20stratum%20by%20its%20position%2C%20not%20its%20name.%20The%20script's%20own%20check%20can%20do%20better%3A%20name%20every%20stratum%20that%20fails%2C%20give%20both%20counts%2C%20and%20suggest%20the%20likely%20cause.%0A%0A%20%20%20%20%23%23%20S2.9%20%C2%B7%20Sampled%20units%20against%20the%20population%2C%20by%20stratum%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20check%20catches%20a%20household%20counted%20twice%20in%20a%20stratum%20that%20takes%20every%20unit%2C%20a%20stratum%20%60stratum_counts.csv%60%20lacks%2C%20and%20counts%20from%20another%20round%2C%20before%20any%20estimate%20is%20computed.%20A%20sampling%20unit%20holds%20many%20households%2C%20so%20counting%20rows%20here%20would%20flag%20every%20area%20stratum%20that%20is%20fine.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20design%20frame%20(one%20row%20per%20household%2C%20with%20%60stratum_id%60%2C%20%60psu_id%60%20and%20%60psus_in_stratum%60)%20%E2%86%92%20one%20row%20per%20stratum%2C%20sorted%20by%20stratum%3A%0A%0A%20%20%20%20%7C%20stratum_id%20%7C%20psus_sampled%20%7C%20psus_in_stratum%20%7C%20ok%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C%3A--%7C%0A%20%20%20%20%7C%20A-1%20%7C%202%20%7C%202%20%7C%20True%20%7C%0A%20%20%20%20%7C%20A-2%20%7C%203%20%7C%202%20%7C%20False%20%7C%0A%20%20%20%20%7C%20B-1%20%7C%202%20%7C%20*missing*%20%7C%20False%20%7C%0A%0A%20%20%20%20-%20%60psus_sampled%60%3A%20the%20number%20of%20**different**%20sampling%20units%20in%20the%20stratum's%20rows%2C%20not%20the%20number%20of%20rows%3B%0A%20%20%20%20-%20%60psus_in_stratum%60%3A%20the%20stratum's%20count%20of%20sampling%20units%2C%20from%20its%20rows%3B%0A%20%20%20%20-%20%60ok%60%3A%20True%20when%20%60psus_sampled%60%20is%20no%20larger%20than%20%60psus_in_stratum%60%3B%20False%20when%20it%20is%20larger%2C%20or%20the%20count%20is%20missing.%0A%0A%20%20%20%20**Tools%3A**%20%60groupby(%22stratum_id%22).agg(...)%60%20with%20%60%22nunique%22%60%20and%20%60%22first%22%60%2C%20from%20Arrays%20%26%20DataFrames%3B%20a%20comparison%20makes%20the%20%60ok%60%20column.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20stratum_check(frame%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20stratum%3A%20psus_sampled%20(different%20sampling%20units%20in%20the%20frame)%2C%20psus_in_stratum%20and%20ok%2C%20True%20when%20the%0A%20%20%20%20sample%20holds%20no%20more%20sampling%20units%20than%20the%20population.%22%22%22%0A%20%20%20%20%23%20Exercise%20S2.9%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20group%20by%20stratum_id%3A%20psus_sampled%20%3D%20the%20number%20of%20different%20psu_id%20values%2C%20psus_in_stratum%20%3D%20its%20first%20value%0A%20%20%20%20%23%20Step%202%3A%20make%20stratum_id%20a%20column%20again%20(reset_index)%0A%20%20%20%20%23%20Step%203%3A%20ok%20%3D%20psus_sampled%20%3C%3D%20psus_in_stratum%2C%20and%20return%20the%20table%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S2.9%22)%0A%0A%0A%40app.function%0Adef%20strata_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Nine%20households%20in%20three%20strata%2C%20used%20by%20the%20checks%3A%20A-1%20has%20two%20sampling%20units%20(three%20households)%20of%20two%3B%20A-2%0A%20%20%20%20three%20of%20two%3B%20B-1%20has%20no%20count.%20Unit%20labels%20repeat%20across%20strata%2C%20as%20they%20do%20in%20a%20frame%20whose%20units%20are%20numbered%0A%20%20%20%20within%20each%20stratum.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22stratum_id%22%3A%20%5B%22A-1%22%2C%20%22A-1%22%2C%20%22A-1%22%2C%20%22A-2%22%2C%20%22A-2%22%2C%20%22A-2%22%2C%20%22A-2%22%2C%20%22B-1%22%2C%20%22B-1%22%5D%2C%0A%20%20%20%20%20%20%20%20%22psu_id%22%3A%20%5B%221%22%2C%20%221%22%2C%20%222%22%2C%20%221%22%2C%20%222%22%2C%20%223%22%2C%20%223%22%2C%20%221%22%2C%20%222%22%5D%2C%0A%20%20%20%20%20%20%20%20%22psus_in_stratum%22%3A%20%5B2%2C%202%2C%202%2C%202%2C%202%2C%202%2C%202%2C%20np.nan%2C%20np.nan%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.function%0Adef%20strata_rows(table)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Exercise%20S2.9's%20result%20in%20one%20shape%20for%20the%20checks%3A%20as_rows%20by%20stratum_id.%22%22%22%0A%20%20%20%20return%20as_rows(table%2C%20%5B%22stratum_id%22%5D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(frame)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S2.9%22%2C%20stratum_check%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%20in%20three%20strata%3A%20two%20units%20of%20two%2C%20three%20of%20two%2C%20and%20no%20count%22%2C%20(strata_fixture()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey%2C%20the%20design%20decisions%20applied%22%2C%20(frame%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey%20without%20the%20decisions%3A%20the%20three%20copied%20households%20left%20in%22%2C%20(read_survey(PATHS.data%2C%20EXPECTED_REGIONS)%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22design%20frames%22%2C%20summarize%3Dstrata_rows%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Group%20by%20stratum_id%3B%20psus_sampled%20counts%20the%20different%20psu_id%20values%20(nunique)%2C%20not%20the%20rows%3B%20ok%20is%20psus_sampled%20%3C%3D%20psus_in_stratum.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20survey's%20strata%0A%20%20%20%20try_it(stratum_check%2C%20frame)%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%20S2.9%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%60psus_sampled%60%20far%20above%20the%20stratum's%20count%2C%20and%20%60ok%60%20False%20in%20every%20area%20stratum%20%7C%20The%20rows%20were%20counted%2C%20not%20the%20sampling%20units.%20%7C%20%60(%22psu_id%22%2C%20%22nunique%22)%60%20%7C%0A%20%20%20%20%7C%20%60ok%60%20True%20where%20the%20count%20is%20missing%20%7C%20The%20test%20was%20written%20as%20%60not%20(psus_sampled%20%3E%20psus_in_stratum)%60%2C%20which%20is%20True%20when%20the%20count%20is%20missing.%20%7C%20%60psus_sampled%20%3C%3D%20psus_in_stratum%60%20%7C%0A%20%20%20%20%7C%20A-1%20counted%20as%20having%20one%20unit%20%7C%20The%20units%20were%20made%20unique%20over%20the%20whole%20frame%20(%60drop_duplicates(%22psu_id%22)%60)%20before%20grouping%2C%20and%20unit%201%20of%20A-2%20took%20A-1's.%20%7C%20count%20within%20each%20stratum%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'stratum_id'*%20%7C%20%60stratum_id%60%20is%20the%20index%2C%20not%20a%20column.%20%7C%20%60.reset_index()%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.function%0Adef%20pre_publication_problems(frame%3A%20pd.DataFrame)%20-%3E%20list%5Bstr%5D%3A%0A%20%20%20%20%22%22%22What%20stops%20the%20tables%20before%20any%20estimate%3A%20a%20stratum%20with%20more%20sampling%20units%20sampled%20than%20it%20has%2C%20a%20household%0A%20%20%20%20twice%2C%20a%20household%20with%20no%20positive%20weight.%20Each%20problem%20is%20one%20sentence%20a%20person%20can%20act%20on%3B%20none%20means%20the%20data%0A%20%20%20%20passed.%22%22%22%0A%20%20%20%20problems%20%3D%20%5B%5D%0A%20%20%20%20checked%20%3D%20stratum_check(frame)%0A%20%20%20%20for%20row%20in%20checked%5B~checked%5B%22ok%22%5D%5D.itertuples()%3A%0A%20%20%20%20%20%20%20%20problems.append(f%22stratum%20%7Brow.stratum_id%7D%3A%20%7Brow.psus_sampled%7D%20sampling%20units%20in%20the%20sample%20but%20%7Brow.psus_in_stratum%7D%20in%20the%20population%20%22%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%22(stratum_counts.csv)%3B%20a%20household%20counted%20twice%2C%20or%20a%20stratum%20the%20file%20lacks%3F%22)%0A%20%20%20%20twice%20%3D%20int(frame.duplicated(%5B%22region_code%22%2C%20%22hh_id%22%5D).sum())%0A%20%20%20%20if%20twice%3A%0A%20%20%20%20%20%20%20%20problems.append(f%22%7Btwice%7D%20households%20appear%20twice%20(the%20same%20region_code%20and%20hh_id)%22)%0A%20%20%20%20unweighted%20%3D%20int((frame%5B%22sel_final_weight%22%5D.isna()%20%7C%20(frame%5B%22sel_final_weight%22%5D%20%3C%3D%200)).sum())%0A%20%20%20%20if%20unweighted%3A%0A%20%20%20%20%20%20%20%20problems.append(f%22%7Bunweighted%7D%20households%20have%20no%20positive%20weight%20(sel_final_weight)%22)%0A%20%20%20%20return%20problems%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%60pre_publication_problems%60%20runs%20your%20check%20and%20two%20more%20(a%20household%20twice%2C%20a%20household%20with%20no%20positive%20weight)%2C%20and%20turns%20each%20failure%20into%20a%20sentence.%20An%20empty%20list%20means%20the%20data%20passed.%20Module%206's%20Exercise%206.1%20drops%20the%20rows%20without%20a%20weight%20before%20the%20design%2C%20so%20a%20household%20with%20none%20here%20means%20the%20flat%20table%20was%20made%20without%20that%20step.%0A%0A%20%20%20%20%23%23%23%20Over%20what%20equality%3A%20the%20manifest%0A%0A%20%20%20%20A%20re-runnable%20script%20writes%20the%20same%20outputs%20when%20it%20runs%20twice%20on%20the%20same%20inputs.%20%22The%20same%22%20needs%20a%20definition.%20For%20a%20CSV%20file%20it%20can%20be%20the%20bytes%3A%20pandas%20writes%20the%20same%20table%20the%20same%20way%20every%20time.%20So%20can%20the%20flat%20table's%2C%20since%20the%20script%20reads%20it%20and%20never%20writes%20it.%20For%20a%20workbook%20it%20cannot%2C%20as%20the%20cell%20above%20measured%3A%20openpyxl%20writes%20the%20moment%20of%20saving%20into%20the%20file.%20What%20must%20not%20change%20is%20what%20the%20workbook%20**holds**%3A%20its%20sheets%20and%20every%20cell's%20value.%0A%0A%20%20%20%20The%20script%20records%20that%20in%20a%20**manifest**%3A%20a%20small%20table%2C%20written%20beside%20the%20outputs%2C%20with%20one%20row%20per%20file%20it%20read%20or%20wrote%20and%20a%20fingerprint%20of%20each.%20A%20fingerprint%20here%20is%20a%20SHA-256%20hash%3A%20a%2064-character%20code%20computed%20from%20the%20file's%20content%2C%20which%20changes%20if%20one%20byte%20of%20that%20content%20changes%20and%20from%20which%20the%20content%20cannot%20be%20read%20back.%20Two%20runs%20whose%20manifests%20match%20read%20the%20same%20inputs%20and%20wrote%20the%20same%20tables.%20A%20manifest%20also%20answers%20the%20question%20a%20published%20table%20raises%20a%20year%20later%3A%20which%20flat%20table%2C%20exactly%2C%20were%20these%20numbers%20made%20from%3F%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20hashlib.sha256(path.read_bytes()).hexdigest()%20%20%20%23%20a%20file's%20fingerprint%2C%20from%20its%20bytes%3A%20'9f86d08...'%20(64%20characters)%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%20S2.10%20%C2%B7%20A%20manifest%20of%20files%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20with%20the%20manifest%2C%20%22did%20the%20second%20run%20change%20anything%3F%22%20is%20answered%20by%20comparing%20two%20small%20tables%2C%20and%20%22which%20data%20were%20these%20tables%20made%20from%3F%22%20by%20reading%20one.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20list%20of%20file%20paths%20and%20a%20folder%20they%20are%20all%20in%20(%60root%60)%20%E2%86%92%20one%20row%20per%20file%2C%20sorted%20by%20%60file%60%3A%0A%0A%20%20%20%20%7C%20file%20%7C%20fingerprint%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60run1%2Fnational.csv%60%20%7C%20the%20SHA-256%20of%20the%20file's%20bytes%20%7C%0A%20%20%20%20%7C%20%60run1%2Ftables.xlsx%60%20%7C%20the%20SHA-256%20of%20what%20the%20workbook%20holds%3A%20each%20sheet's%20title%20and%20each%20row's%20values%20%7C%0A%0A%20%20%20%20-%20%60file%60%3A%20the%20path%20relative%20to%20%60root%60%2C%20written%20with%20%60%2F%60%20(%60path.relative_to(root).as_posix()%60)%2C%20so%20the%20manifest%20reads%20the%20same%20in%20any%20folder%20and%20on%20any%20computer%3B%0A%20%20%20%20-%20%60fingerprint%60%3A%20for%20a%20workbook%20(%60.xlsx%60)%2C%20the%20hash%20of%20its%20contents%2C%20read%20with%20openpyxl%3B%20for%20any%20other%20file%2C%20the%20hash%20of%20its%20bytes.%0A%0A%20%20%20%20For%20the%20workbook%2C%20%60openpyxl.load_workbook(path%2C%20read_only%3DTrue)%60%20opens%20it%3B%20%60workbook.worksheets%60%20are%20its%20sheets%2C%20%60sheet.title%60%20a%20sheet's%20name%20and%20%60sheet.iter_rows(values_only%3DTrue)%60%20its%20rows%20as%20tuples%20of%20values.%20Put%20them%20in%20one%20list%2C%20turn%20it%20into%20text%20with%20%60repr(...)%60%2C%20and%20hash%20%60text.encode(%22utf-8%22)%60.%20Close%20the%20workbook%20after%20reading%20(%60workbook.close()%60).%0A%0A%20%20%20%20**Tools%3A**%20%60hashlib.sha256(...).hexdigest()%60%2C%20%60Path.read_bytes%60%2C%20%60Path.suffix%60%2C%20%60Path.relative_to%60%2C%20openpyxl%20as%20above%2C%20and%20%60pd.DataFrame(rows)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20manifest(paths%3A%20list%5BPath%5D%2C%20root%3A%20Path)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20file%2C%20sorted%20by%20file%3A%20its%20path%20relative%20to%20root%20(with%20%2F)%20and%20its%20fingerprint%2C%20the%20SHA-256%20of%20its%0A%20%20%20%20bytes%2C%20or%20for%20a%20workbook%20of%20what%20it%20holds%20(each%20sheet's%20title%20and%20rows%20of%20values).%22%22%22%0A%20%20%20%20%23%20Exercise%20S2.10%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20for%20each%20path%3A%20if%20it%20is%20a%20workbook%20(path.suffix%20%3D%3D%20%22.xlsx%22)%2C%20open%20it%20with%20openpyxl%20(read_only%3DTrue)%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20make%20a%20list%20of%20(sheet%20title%2C%20list%20of%20rows%20of%20values)%20for%20its%20sheets%2C%20close%20it%2C%20and%20encode%20repr()%20of%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20the%20list%20as%20UTF-8%3B%20otherwise%2C%20take%20the%20file's%20bytes%0A%20%20%20%20%23%20Step%202%3A%20a%20row%20with%20file%20%3D%20the%20path%20relative%20to%20root%2C%20as_posix()%2C%20and%20fingerprint%20%3D%20sha256%20of%20the%20content%2C%20hexdigest()%0A%20%20%20%20%23%20Step%203%3A%20a%20DataFrame%20of%20the%20rows%2C%20sorted%20by%20file%2C%20and%20return%20it%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S2.10%22)%0A%0A%0A%40app.function%0Adef%20manifest_runs()%20-%3E%20Path%3A%0A%20%20%20%20%22%22%22Three%20runs'%20outputs%2C%20for%20the%20checks%3A%20run2%20holds%20the%20same%20tables%20as%20run1%20(its%20workbook%20saved%20later%2C%20so%20its%20bytes%0A%20%20%20%20differ)%2C%20run3%20one%20number%20changed%20in%20each%20file.%20Returns%20the%20folder%20that%20holds%20the%20three.%22%22%22%0A%20%20%20%20root%20%3D%20SCRATCH%20%2F%20%22check_manifest%22%0A%20%20%20%20shutil.rmtree(root%2C%20ignore_errors%3DTrue)%0A%20%20%20%20for%20run%2C%20households%20in%20((%22run1%22%2C%203812.4467)%2C%20(%22run2%22%2C%203812.4467)%2C%20(%22run3%22%2C%203812.4468))%3A%0A%20%20%20%20%20%20%20%20(root%20%2F%20run).mkdir(parents%3DTrue)%0A%20%20%20%20%20%20%20%20table%20%3D%20pd.DataFrame(%7B%22domain%22%3A%20%5B%22La%20Union%22%2C%20%22Pangasinan%22%5D%2C%20%22estimate%22%3A%20%5Bhouseholds%2C%203787.4583%5D%7D)%0A%20%20%20%20%20%20%20%20table.to_csv(root%20%2F%20run%20%2F%20%22national.csv%22%2C%20index%3DFalse)%0A%20%20%20%20%20%20%20%20workbook%20%3D%20openpyxl.Workbook()%0A%20%20%20%20%20%20%20%20workbook.active.title%20%3D%20%22National%22%0A%20%20%20%20%20%20%20%20for%20row%20in%20%5Blist(table.columns)%2C%20*table.values.tolist()%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20workbook.active.append(row)%0A%20%20%20%20%20%20%20%20workbook.save(root%20%2F%20run%20%2F%20%22tables.xlsx%22)%0A%20%20%20%20%23%20run2's%20workbook%2C%20saved%20%22later%22%3A%20the%20same%20contents%2C%20other%20timestamps%20in%20the%20zip%2C%20so%20other%20bytes%0A%20%20%20%20original%20%3D%20(root%20%2F%20%22run2%22%20%2F%20%22tables.xlsx%22).read_bytes()%0A%20%20%20%20with%20zipfile.ZipFile(root%20%2F%20%22run2%22%20%2F%20%22tables.xlsx%22)%20as%20source%3A%0A%20%20%20%20%20%20%20%20entries%20%3D%20%5B(item.filename%2C%20source.read(item.filename))%20for%20item%20in%20source.infolist()%5D%0A%20%20%20%20with%20zipfile.ZipFile(root%20%2F%20%22run2%22%20%2F%20%22tables.xlsx%22%2C%20%22w%22%2C%20zipfile.ZIP_DEFLATED)%20as%20later%3A%0A%20%20%20%20%20%20%20%20for%20name%2C%20data%20in%20entries%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20later.writestr(zipfile.ZipInfo(name%2C%20date_time%3D(2030%2C%201%2C%201%2C%2012%2C%200%2C%200))%2C%20data%2C%20compress_type%3Dzipfile.ZIP_DEFLATED)%0A%20%20%20%20if%20(root%20%2F%20%22run2%22%20%2F%20%22tables.xlsx%22).read_bytes()%20%3D%3D%20original%3A%0A%20%20%20%20%20%20%20%20raise%20RuntimeError(%22the%20re-saved%20workbook%20has%20the%20same%20bytes%20as%20before%3A%20the%20check%20would%20not%20tell%20bytes%20from%20contents%22)%0A%20%20%20%20return%20root%0A%0A%0A%40app.function%0Adef%20manifest_summary(table%3A%20pd.DataFrame)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22What%20a%20check%20of%20Exercise%20S2.10%20compares%3A%20the%20files%20in%20the%20manifest's%20order%2C%20which%20of%20them%20share%20a%20fingerprint%2C%20and%0A%20%20%20%20whether%20every%20fingerprint%20is%20a%20SHA-256%20hex%20code.%20Not%20the%20codes%20themselves%3A%20any%20way%20of%20hashing%20the%20same%20contents%20passes.%22%22%22%0A%20%20%20%20groups%3A%20dict%5Bstr%2C%20list%5Bstr%5D%5D%20%3D%20%7B%7D%0A%20%20%20%20for%20file%2C%20fingerprint%20in%20zip(table%5B%22file%22%5D%2C%20table%5B%22fingerprint%22%5D)%3A%0A%20%20%20%20%20%20%20%20groups.setdefault(fingerprint%2C%20%5B%5D).append(file)%0A%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%22files%2C%20in%20order%22%3A%20list(table%5B%22file%22%5D)%2C%0A%20%20%20%20%20%20%20%20%22files%20with%20the%20same%20fingerprint%22%3A%20sorted(sorted(files)%20for%20files%20in%20groups.values()%20if%20len(files)%20%3E%201)%2C%0A%20%20%20%20%20%20%20%20%22every%20fingerprint%20is%20a%20SHA-256%20hex%20code%22%3A%20all(re.fullmatch(r%22%5B0-9a-f%5D%7B64%7D%22%2C%20str(code))%20is%20not%20None%20for%20code%20in%20table%5B%22fingerprint%22%5D)%2C%0A%20%20%20%20%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_root%20%3D%20manifest_runs()%0A%0A%20%20%20%20def%20_files(*names)%3A%0A%20%20%20%20%20%20%20%20return%20%5B_root%20%2F%20name%20for%20name%20in%20names%5D%0A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S2.10%22%2C%20manifest%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22two%20runs'%20CSV%20files%20with%20the%20same%20bytes%2C%20listed%20out%20of%20order%22%2C%20(_files(%22run2%2Fnational.csv%22%2C%20%22run1%2Fnational.csv%22)%2C%20_root))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20runs'%20workbooks%3A%20run2%20the%20same%20tables%20saved%20later%2C%20run3%20one%20number%20changed%22%2C%20(_files(%22run3%2Ftables.xlsx%22%2C%20%22run1%2Ftables.xlsx%22%2C%20%22run2%2Ftables.xlsx%22)%2C%20_root))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22every%20file%20of%20the%20three%20runs%22%2C%20(_files(%22run1%2Ftables.xlsx%22%2C%20%22run3%2Fnational.csv%22%2C%20%22run2%2Ftables.xlsx%22%2C%20%22run1%2Fnational.csv%22%2C%20%22run3%2Ftables.xlsx%22%2C%20%22run2%2Fnational.csv%22)%2C%20_root))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22one%20run's%20files%2C%20relative%20to%20that%20run's%20folder%22%2C%20(_files(%22run1%2Ftables.xlsx%22%2C%20%22run1%2Fnational.csv%22)%2C%20_root%20%2F%20%22run1%22))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22lists%20of%20files%22%2C%20summarize%3Dmanifest_summary%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Hash%20a%20workbook's%20sheet%20titles%20and%20values%20(openpyxl%2C%20values_only%3DTrue)%2C%20any%20other%20file's%20bytes%3B%20file%20is%20relative%20to%20root%20with%20as_posix()%3B%20sort%20by%20file.%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%20three%20runs'%20files%20used%20by%20the%20check.%20run1%20and%20run2%20should%20share%20their%20fingerprints%2C%20file%20by%20file.%0A%20%20%20%20_root%20%3D%20manifest_runs()%0A%20%20%20%20try_it(manifest%2C%20sorted(_root.glob(%22*%2F*%22))%2C%20_root)%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%20S2.10%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%20run1's%20and%20run2's%20workbooks%20with%20different%20fingerprints%20%7C%20The%20workbook's%20bytes%20were%20hashed%3A%20they%20hold%20the%20time%20it%20was%20saved.%20%7C%20open%20it%20with%20openpyxl%20and%20hash%20its%20values%20%7C%0A%20%20%20%20%7C%20run1's%20and%20run3's%20workbooks%20with%20the%20same%20fingerprint%20%7C%20Only%20the%20sheet%20titles%20were%20hashed%2C%20or%20the%20rows%20were%20not%20read.%20%7C%20%60list(sheet.iter_rows(values_only%3DTrue))%60%20%7C%0A%20%20%20%20%7C%20Files%20written%20as%20%60%2Fhome%2F...%60%20or%20with%20%60%5C%60%20%7C%20The%20full%20path%2C%20or%20the%20path%20as%20Windows%20writes%20it.%20%7C%20%60path.relative_to(root).as_posix()%60%20%7C%0A%20%20%20%20%7C%20The%20files%20in%20the%20order%20they%20were%20given%20%7C%20The%20table%20was%20not%20sorted.%20%7C%20%60.sort_values(%22file%22)%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20...%20is%20not%20in%20the%20subpath%20of%20...*%20%7C%20A%20file%20outside%20%60root%60%2C%20or%20%60root%60%20and%20the%20paths%20written%20one%20relative%2C%20one%20absolute.%20%7C%20the%20%60root%60%20the%20call%20gives%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%20command%0A%0A%20%20%20%20Everything%20is%20in%20place%20but%20the%20command%20itself%3A%20the%20function%20that%20reads%20the%20options%2C%20runs%20the%20steps%20in%20order%2C%20logs%20each%2C%20and%20turns%20every%20outcome%20into%20an%20exit%20code.%20It%20is%20written%20for%20**typer**%2C%20a%20library%20that%20builds%20a%20command%20from%20a%20typed%20Python%20function%3A%20each%20parameter%20becomes%20an%20option%20(%60allow_missing%60%20becomes%20%60--allow-missing%60)%2C%20its%20type%20says%20what%20the%20option%20accepts%2C%20and%20its%20help%20text%20goes%20into%20%60--help%60.%20A%20value%20of%20the%20wrong%20type%20(a%20date%20written%20%602026-13-01%60)%20is%20refused%20before%20your%20code%20runs%2C%20with%20exit%20code%202%20and%20a%20message%20naming%20the%20option.%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20def%20tabulate_command(%0A%20%20%20%20%20%20%20%20flat%3A%20Annotated%5BPath%2C%20typer.Option(help%3D%22The%20flat%20table%20...%22)%5D%20%3D%20Path(%22scratch%2Fstretch_tabulation%2Fhouseholds.parquet%22)%2C%0A%20%20%20%20%20%20%20%20allow_missing%3A%20Annotated%5Bbool%2C%20typer.Option(%22--allow-missing%22%2C%20help%3D%22...%22)%5D%20%3D%20False%2C%0A%20%20%20%20)%20-%3E%20None%3A%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20%60Annotated%5BPath%2C%20typer.Option(...)%5D%60%20says%20two%20things%20at%20once%3A%20to%20Python%2C%20%60flat%60%20is%20a%20%60Path%60%3B%20to%20typer%2C%20it%20is%20an%20option%20with%20this%20help.%20The%20default%20after%20%60%3D%60%20is%20what%20the%20option%20is%20when%20it%20is%20not%20given.%0A%0A%20%20%20%20**argparse**%2C%20in%20Python's%20standard%20library%2C%20is%20the%20alternative%20that%20needs%20no%20package.%20It%20writes%20each%20option's%20type%20in%20one%20place%20and%20reads%20the%20value%20from%20another%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20parser%20%3D%20argparse.ArgumentParser(description%3D%22Tabulate%20the%20Survey%20of%20Fishing%20Households%202025.%22)%0A%20%20%20%20parser.add_argument(%22--flat%22%2C%20type%3DPath%2C%20default%3DPath(%22scratch%2Fstretch_tabulation%2Fhouseholds.parquet%22)%2C%20help%3D%22The%20flat%20table%20...%22)%0A%20%20%20%20parser.add_argument(%22--allow-missing%22%2C%20action%3D%22store_true%22%2C%20help%3D%22...%22)%0A%20%20%20%20args%20%3D%20parser.parse_args()%20%20%20%20%20%20%20%20%20%20%23%20args.flat%20is%20a%20Path%2C%20args.allow_missing%20a%20bool%2C%20as%20far%20as%20argparse%20knows%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20%60run_tabulation%60%20is%20module%207's%20%60tabulate%60%20with%20the%20contract%20added%20around%20its%20steps.%20Its%20steps%2C%20in%20order%3A%0A%0A%20%20%20%201.%20read%20the%20flat%20table%20and%20create%20its%20variables%20(exit%2066%20if%20the%20file%20is%20missing%20or%20cannot%20be%20read)%3B%0A%20%20%20%202.%20find%20the%20expected%20regions%20in%20it%2C%20refusing%20a%20missing%20one%20unless%20%60--allow-missing%60%20(exit%2066)%3B%0A%20%20%20%203.%20run%20the%20checks%20before%20publishing%20(exit%2065%2C%20naming%20each%20problem)%3B%0A%20%20%20%204.%20declare%20the%20design%2C%20make%20every%20sheet%20with%20%60table_sheets%60%2C%20flag%20each%20with%20%60apply_cv_flags%60%2C%20and%20write%20the%20workbook%20with%20%60save_tables%60%2C%20a%20notes%20sheet%20added%3B%0A%20%20%20%205.%20write%20the%20manifest%20of%20every%20file%20read%20and%20written%2C%20and%20print%20its%20path%20on%20stdout%20(exit%200).%0A%0A%20%20%20%20Steps%201%20and%204%20are%20module%207's%20script%3B%20the%20rest%20is%20the%20contract.%20Nothing%20is%20written%20before%20step%204%2C%20so%20a%20refused%20run%20leaves%20the%20outputs%20of%20the%20last%20good%20run%20as%20they%20were.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20start_log()%20-%3E%20logging.Logger%3A%0A%20%20%20%20%22%22%22The%20run's%20log%3A%20a%20line%20per%20step%2C%20with%20the%20time%20and%20how%20serious%20it%20is%2C%20written%20to%20stderr.%22%22%22%0A%20%20%20%20log%20%3D%20logging.getLogger(%22tabulate%22)%0A%20%20%20%20handler%20%3D%20logging.StreamHandler(sys.stderr)%0A%20%20%20%20handler.setFormatter(logging.Formatter(%22%25(asctime)s%20%25(levelname)s%20%25(message)s%22%2C%20datefmt%3D%22%25H%3A%25M%3A%25S%22))%0A%20%20%20%20log.handlers%5B%3A%5D%20%3D%20%5Bhandler%5D%0A%20%20%20%20log.setLevel(logging.INFO)%0A%20%20%20%20log.propagate%20%3D%20False%0A%20%20%20%20return%20log%0A%0A%0A%40app.function%0Adef%20tables_notes(flat%3A%20Path%2C%20regions%3A%20list%5Bstr%5D%2C%20missing%3A%20list%5Bstr%5D%2C%20made_on%3A%20str%2C%20sheets%3A%20dict%5Bstr%2C%20pd.DataFrame%5D)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20workbook's%20notes%20sheet%3A%20what%20its%20sheets%20are%2C%20where%20they%20come%20from%2C%20which%20regions%20they%20cover%20and%20which%20they%0A%20%20%20%20leave%20out%2C%20the%20publication%20rule%20with%20the%20flags%20it%20gave%2C%20and%20when%20the%20tables%20were%20made.%22%22%22%0A%20%20%20%20flags%20%3D%20pd.concat(%5Btable%5B%22flag%22%5D%20for%20table%20in%20sheets.values()%20if%20%22flag%22%20in%20table%5D%20or%20%5Bpd.Series(dtype%3D%22str%22)%5D).value_counts()%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(%22Tables%22%2C%20%22Each%20table%20on%20one%20sheet%2C%20its%20national%2C%20regional%20and%20provincial%20rows%20stacked%20(the%20column%20level)%3A%20the%20estimate%20from%20the%20survey's%20design%20with%20its%20SE%2C%20CV%20and%2095%25%20interval%2C%20then%20the%20sample's%20own%20figures%2C%20unweighted%20(the%20sum%20over%20the%20households%20interviewed%2C%20or%20their%20plain%20mean)%20and%20n%20(the%20households%20interviewed)%22)%2C%0A%20%20%20%20%20%20%20%20(%22Source%22%2C%20f%22%7Bflat.name%7D%3A%20the%20flat%20table%2C%20one%20row%20per%20household%20of%20the%20survey%2C%20module%204's%20design%20decisions%20applied%22)%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%20rows%20in%20the%20flat%20table%3B%20every%20table%2C%20the%20national%20one%20included%2C%20leaves%20them%20out%22%20if%20missing%20else%20%22none%22)%2C%0A%20%20%20%20%20%20%20%20(%22Publication%20rule%22%2C%20f%22The%20survey's%20own%20(Exercise%20S2.3)%2C%20on%20every%20estimate%3A%20publish%20up%20to%20a%20CV%20of%20%7BCAUTION_CV%3A.0%25%7D%3B%20caution%20above%20it%2C%20up%20to%20%7BSUPPRESS_CV%3A.0%25%7D%3B%20suppress%20above%20%7BSUPPRESS_CV%3A.0%25%7D%2C%20or%20with%20no%20CV%22)%2C%0A%20%20%20%20%20%20%20%20(%22flag%20caution%22%2C%20f%22CV%20above%20%7BCAUTION_CV%3A.0%25%7D%2C%20up%20to%20%7BSUPPRESS_CV%3A.0%25%7D%3A%20published%2C%20use%20with%20caution%20(%7Bflags.get('caution'%2C%200)%7D%20estimates%20in%20this%20file)%22)%2C%0A%20%20%20%20%20%20%20%20(%22flag%20suppress%22%2C%20f%22CV%20above%20%7BSUPPRESS_CV%3A.0%25%7D%2C%20or%20no%20CV%3A%20the%20estimate%2C%20its%20SE%20and%20its%20interval%20are%20not%20published%20(%7Bflags.get('suppress'%2C%200)%7D%20estimates%20in%20this%20file)%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%20add_notes(path%3A%20Path%2C%20notes%3A%20pd.DataFrame)%20-%3E%20Path%3A%0A%20%20%20%20%22%22%22Add%20the%20notes%20sheet%20at%20the%20end%20of%20the%20workbook%20save_tables%20wrote%2C%20with%20module%207's%20add_sheet.%22%22%22%0A%20%20%20%20workbook%20%3D%20openpyxl.load_workbook(path)%0A%20%20%20%20sheet%20%3D%20add_sheet(workbook%2C%20%22Notes%22%2C%20notes)%0A%20%20%20%20sheet.column_dimensions%5B%22A%22%5D.width%20%3D%2024%0A%20%20%20%20sheet.column_dimensions%5B%22B%22%5D.width%20%3D%20100%0A%20%20%20%20workbook.save(path)%0A%20%20%20%20return%20path%0A%0A%0A%40app.function%0Adef%20run_tabulation(flat%3A%20Path%2C%20out%3A%20Path%2C%20allow_missing%3A%20bool%2C%20made_on%3A%20str%2C%20log%3A%20logging.Logger)%20-%3E%20int%3A%0A%20%20%20%20%22%22%22Module%207's%20tabulate%20with%20its%20contract%3A%20the%20steps%2C%20logged%2C%20from%20the%20flat%20table%20to%20one%20workbook%20and%20a%20manifest%3B%0A%20%20%20%20returns%20the%20exit%20code%20(EXIT_CODES%20says%20what%20each%20means).%22%22%22%0A%20%20%20%20if%20not%20flat.exists()%3A%0A%20%20%20%20%20%20%20%20log.error(f%22no%20flat%20table%20at%20%7Bflat%7D%3B%20nothing%20was%20written%22)%0A%20%20%20%20%20%20%20%20return%2066%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20frame%20%3D%20add_variables(pd.read_parquet(flat))%0A%20%20%20%20except%20(OSError%2C%20ValueError%2C%20KeyError)%20as%20error%3A%0A%20%20%20%20%20%20%20%20log.error(f%22cannot%20read%20the%20flat%20table%20%7Bflat%7D%3A%20%7Btype(error).__name__%7D%3A%20%7Berror%7D%3B%20nothing%20was%20written%22)%0A%20%20%20%20%20%20%20%20return%2066%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20regions%20%3D%20regions_to_read(frame%2C%20EXPECTED_REGIONS%2C%20allow_missing)%0A%20%20%20%20except%20MissingRegions%20as%20error%3A%0A%20%20%20%20%20%20%20%20log.error(f%22%7Berror%7D%20in%20%7Bflat%7D%3B%20nothing%20was%20written.%20Make%20the%20flat%20table%20again%20with%20every%20region's%20database%2C%20or%20pass%20--allow-missing%20to%20tabulate%20the%20regions%20it%20holds.%22)%0A%20%20%20%20%20%20%20%20return%2066%0A%20%20%20%20missing%20%3D%20%5Bregion%20for%20region%20in%20EXPECTED_REGIONS%20if%20region%20not%20in%20regions%5D%0A%20%20%20%20for%20region%20in%20missing%3A%0A%20%20%20%20%20%20%20%20log.warning(f%22region%20%7Bregion%7D%3A%20no%20rows%20in%20%7Bflat%7D%3B%20every%20table%20leaves%20it%20out%20(--allow-missing)%22)%0A%20%20%20%20if%20not%20regions%3A%0A%20%20%20%20%20%20%20%20log.error(f%22no%20expected%20region%20has%20rows%20in%20%7Bflat%7D%3B%20nothing%20was%20written%22)%0A%20%20%20%20%20%20%20%20return%2066%0A%20%20%20%20log.info(f%22read%20%7Blen(frame)%3A%2C%7D%20households%20of%20regions%20%7B'%2C%20'.join(regions)%7D%20from%20%7Bflat%7D%22)%0A%20%20%20%20problems%20%3D%20pre_publication_problems(frame)%0A%20%20%20%20if%20problems%3A%0A%20%20%20%20%20%20%20%20for%20problem%20in%20problems%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20log.error(problem)%0A%20%20%20%20%20%20%20%20log.error(f%22the%20data%20failed%20%7Blen(problems)%7D%20check(s)%20before%20publishing%3B%20nothing%20was%20written%22)%0A%20%20%20%20%20%20%20%20return%2065%0A%20%20%20%20sheets%20%3D%20%7Bname%3A%20apply_cv_flags(table%2C%20CAUTION_CV%2C%20SUPPRESS_CV)%20for%20name%2C%20table%20in%20table_sheets(design_sample(frame)).items()%7D%0A%20%20%20%20path%20%3D%20save_tables(sheets%2C%20out%20%2F%20%22tables.xlsx%22)%0A%20%20%20%20add_notes(path%2C%20tables_notes(flat%2C%20regions%2C%20missing%2C%20made_on%2C%20sheets))%0A%20%20%20%20log.info(f%22wrote%20%7Bpath%7D%22)%0A%20%20%20%20listing%20%3D%20pd.concat(%5B%0A%20%20%20%20%20%20%20%20manifest(%5Bflat%5D%2C%20flat.parent).assign(role%3D%22input%22)%2C%0A%20%20%20%20%20%20%20%20manifest(%5Bpath%5D%2C%20out).assign(role%3D%22output%22)%2C%0A%20%20%20%20%5D)%5B%5B%22role%22%2C%20%22file%22%2C%20%22fingerprint%22%5D%5D%0A%20%20%20%20listing.to_csv(out%20%2F%20%22manifest.csv%22%2C%20index%3DFalse)%0A%20%20%20%20log.info(f%22wrote%20%7Bout%20%2F%20'manifest.csv'%7D%3A%20%7Blen(listing)%7D%20files%20read%20and%20written%22)%0A%20%20%20%20print(out%20%2F%20%22manifest.csv%22)%20%20%23%20stdout%3A%20the%20result%2C%20for%20whatever%20ran%20the%20script%0A%20%20%20%20return%200%0A%0A%0A%40app.function%0Adef%20tabulate_command(%0A%20%20%20%20flat%3A%20Annotated%5BPath%2C%20typer.Option(help%3D%22The%20flat%20table%3A%20one%20row%20per%20household%2C%20the%20design%20decisions%20applied%2C%20as%20Parquet%2C%20with%20the%20design's%20columns.%22)%5D%20%3D%20Path(%22scratch%2Fstretch_tabulation%2Fhouseholds.parquet%22)%2C%0A%20%20%20%20out%3A%20Annotated%5BPath%2C%20typer.Option(help%3D%22The%20folder%20the%20workbook%20and%20the%20manifest%20are%20written%20to%3B%20made%20if%20missing%3B%20its%20files%20are%20replaced.%22)%5D%20%3D%20Path(%22scratch%2Fstretch_tabulation%2Ftables%22)%2C%0A%20%20%20%20allow_missing%3A%20Annotated%5Bbool%2C%20typer.Option(%22--allow-missing%22%2C%20help%3D%22When%20an%20expected%20region%20has%20no%20rows%20in%20the%20flat%20table%2C%20tabulate%20the%20others%20and%20say%20so%20in%20the%20log%20and%20the%20notes%2C%20instead%20of%20stopping.%22)%5D%20%3D%20False%2C%0A%20%20%20%20as_of%3A%20Annotated%5Bdatetime.datetime%20%7C%20None%2C%20typer.Option(formats%3D%5B%22%25Y-%25m-%25d%22%5D%2C%20help%3D%22The%20date%20the%20notes%20give%20for%20the%20tables%20(YYYY-MM-DD)%3B%20today%20if%20not%20given.%20Give%20it%20to%20remake%20an%20earlier%20run%20exactly.%22)%5D%20%3D%20None%2C%0A)%20-%3E%20None%3A%0A%20%20%20%20%22%22%22Tabulate%20the%20Survey%20of%20Fishing%20Households%202025%3A%20every%20table%20with%20its%20national%2C%20regional%20and%20provincial%20rows%0A%20%20%20%20stacked%2C%20each%20estimate%20flagged%20by%20its%20CV%20and%20the%20sample's%20own%20figures%20beside%20it%2C%20to%20one%20workbook%2C%20with%20a%20manifest%20of%0A%20%20%20%20every%20file%20read%20and%20written.%20The%20log%20goes%20to%20stderr%3B%20the%20manifest's%20path%20to%20stdout.%22%22%22%0A%20%20%20%20log%20%3D%20start_log()%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20code%20%3D%20run_tabulation(flat%2C%20out%2C%20allow_missing%2C%20(as_of%20or%20datetime.datetime.now()).date().isoformat()%2C%20log)%0A%20%20%20%20except%20NotImplementedError%20as%20error%3A%0A%20%20%20%20%20%20%20%20log.error(f%22%7Berror%7D%20is%20not%20written%20yet%3A%20finish%20it%20in%20%7BNOTEBOOK%7D%2C%20then%20write%20the%20script%20again%22)%0A%20%20%20%20%20%20%20%20code%20%3D%201%0A%20%20%20%20except%20Exception%20as%20error%3A%20%20%23%20anything%20unforeseen%20still%20ends%20with%20a%20sentence%20and%20an%20exit%20code%2C%20not%20a%20traceback%0A%20%20%20%20%20%20%20%20log.error(f%22%7Btype(error).__name__%7D%3A%20%7Berror%7D%22)%0A%20%20%20%20%20%20%20%20code%20%3D%201%0A%20%20%20%20raise%20typer.Exit(code)%0A%0A%0A%40app.function%0Adef%20help_epilog()%20-%3E%20str%3A%0A%20%20%20%20%22%22%22The%20end%20of%20--help%3A%20an%20example%2C%20and%20every%20exit%20code%20from%20EXIT_CODES.%20(A%20line%20holding%20only%20%5C%5Cb%20keeps%20the%20lines%20after%0A%20%20%20%20it%20as%20written.)%22%22%22%0A%20%20%20%20codes%20%3D%20%22%5Cn%22.join(f%22%20%20%7Bcode%3A%3C3%7D%20%7Bmeaning%7D%22%20for%20code%2C%20meaning%20in%20EXIT_CODES.items())%0A%20%20%20%20return%20(%22%5Cb%5CnExample%2C%20from%20the%20course%20folder%3A%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%20%20uv%20run%20python%20scratch%2Fstretch_tabulation%2Ftabulate.py%20--flat%20scratch%2Fstretch_tabulation%2Fhouseholds.parquet%5Cn%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%5Cb%5CnExit%20codes%3A%5Cn%7Bcodes%7D%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%23%23%20One%20script%2C%20written%20from%20this%20notebook%0A%0A%20%20%20%20The%20script%20is%20written%20as%20module%207's%20is%3A%20%60write_script%60%20reads%20%6061_stretch_tabulation.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%20in%20order%20into%20%60scratch%2Fstretch_tabulation%2Ftabulate.py%60%2C%20with%20the%20imports%20at%20the%20top%20and%20the%20command%20at%20the%20bottom.%20Module%207's%20functions%20go%20in%20as%20they%20are%2C%20and%20your%20exercises%20as%20you%20wrote%20them.%20So%20the%20notebook%20stays%20the%20one%20place%20the%20code%20lives%3A%20change%20a%20function%20here%20and%20write%20the%20script%20again%2C%20and%20the%20script%20changes%20with%20it.%20(marimo%20saves%20the%20notebook%20as%20you%20edit%3B%20the%20copy%20is%20taken%20from%20the%20saved%20file.)%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20script_header()%20-%3E%20str%3A%0A%20%20%20%20%22%22%22The%20top%20of%20the%20script%3A%20what%20it%20does%2C%20what%20it%20assumes%2C%20and%20its%20imports.%22%22%22%0A%20%20%20%20return%20f'''%22%22%22Tabulate%20the%20Survey%20of%20Fishing%20Households%202025%3A%20every%20table%20with%20its%20national%2C%20regional%20and%20provincial%20rows%0Astacked%2C%20each%20estimate%20flagged%20by%20its%20CV%20and%20the%20sample's%20own%20figures%20beside%20it%2C%20from%20the%20flat%20table%20to%20one%20workbook%2C%0Awith%20a%20manifest%20of%20every%20file%20read%20and%20written.%20Module%207's%20tabulation%20script%20with%20a%20contract%3A%20a%20refusal%20of%20a%20missing%0Aregion%2C%20checks%20before%20publishing%2C%20exit%20codes%20and%20the%20manifest.%0A%0AWritten%20by%20%7BNOTEBOOK%7D%20from%20its%20functions%3A%20change%20them%20there%20and%20write%20the%20script%20again.%0A%0AAssumptions%3A%20run%20from%20the%20course%20folder%2C%20in%20the%20course's%20environment%20(uv%20run%20python%20scratch%2Fstretch_tabulation%2Ftabulate.py%20--help)%3B%0Athe%20flat%20table%20holds%20one%20row%20per%20household%20of%20the%20survey%2C%20the%20design%20decisions%20applied%2C%20with%20the%20design's%20columns%3B%0Asvy%20%7Bsvy.__version__%7D.%0A%22%22%22%0A%0Aimport%20datetime%0Aimport%20hashlib%0Aimport%20logging%0Aimport%20sys%0Afrom%20pathlib%20import%20Path%0Afrom%20typing%20import%20Annotated%0A%0Aimport%20numpy%20as%20np%0Aimport%20openpyxl%0Aimport%20pandas%20as%20pd%0Aimport%20polars%20as%20pl%0Aimport%20svy%0Aimport%20typer%0Afrom%20openpyxl.styles%20import%20Font%0A%0ANOTEBOOK%20%3D%20%22%7BNOTEBOOK%7D%22'''%0A%0A%0A%40app.function%0Adef%20script_source()%20-%3E%20str%3A%0A%20%20%20%20%22%22%22The%20script's%20text%3A%20its%20header%2C%20the%20settings%20and%20functions%20it%20needs%20copied%20from%20this%20notebook's%20file%2C%20and%20the%0A%20%20%20%20command.%22%22%22%0A%20%20%20%20source%20%3D%20(PATHS.root%20%2F%20NOTEBOOK).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%20definitions%20%3D%20%7Bnode.name%3A%20text(node)%20for%20node%20in%20tree.body%20if%20isinstance(node%2C%20(ast.FunctionDef%2C%20ast.ClassDef))%7D%0A%20%20%20%20names%20%3D%20%5B%22EXPECTED_REGIONS%22%2C%20%22LEVEL_CODES%22%2C%20%22INDICATORS%22%2C%20%22NUMBER_FORMATS%22%2C%20%22FIGURES%22%2C%20%22TABLES%22%2C%20%22CAUTION_CV%22%2C%20%22SUPPRESS_CV%22%2C%20%22EXIT_CODES%22%5D%0A%20%20%20%20parts%20%3D%20%5B%22MissingRegions%22%2C%20%22add_variables%22%2C%20%22design_sample%22%2C%20%22estimate%22%2C%20%22table_sheets%22%2C%20%22save_tables%22%2C%20%22add_sheet%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%22regions_to_read%22%2C%20%22stratum_check%22%2C%20%22pre_publication_problems%22%2C%20%22apply_cv_flags%22%2C%20%22tables_notes%22%2C%20%22add_notes%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%22manifest%22%2C%20%22start_log%22%2C%20%22run_tabulation%22%2C%20%22tabulate_command%22%2C%20%22help_epilog%22%5D%0A%20%20%20%20command%20%3D%20('cli%20%3D%20typer.Typer(add_completion%3DFalse%2C%20rich_markup_mode%3DNone)%5Cn'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20'cli.command(epilog%3Dhelp_epilog())(tabulate_command)%5Cn%5Cn'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20'if%20__name__%20%3D%3D%20%22__main__%22%3A%5Cn%20%20%20%20cli()')%0A%20%20%20%20return%20%22%5Cn%5Cn%5Cn%22.join(%5Bscript_header()%2C%20%22%5Cn%22.join(settings%5Bname%5D%20for%20name%20in%20names)%2C%20*(definitions%5Bname%5D%20for%20name%20in%20parts)%2C%20command%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.function%0Adef%20run_script(*options%3A%20str)%20-%3E%20subprocess.CompletedProcess%3A%0A%20%20%20%20%22%22%22Run%20scratch%2Fstretch_tabulation%2Ftabulate.py%20from%20the%20course%20folder%20with%20these%20options%2C%20as%20a%20terminal%20would%2C%20keeping%20what%20it%0A%20%20%20%20writes%20to%20stdout%20and%20to%20stderr%20apart.%22%22%22%0A%20%20%20%20return%20subprocess.run(%5Bsys.executable%2C%20%22scratch%2Fstretch_tabulation%2Ftabulate.py%22%2C%20*options%5D%2C%20cwd%3DPATHS.root%2C%20capture_output%3DTrue%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%20text%3DTrue%2C%20timeout%3D600%2C%20env%3D%7B**os.environ%2C%20%22NO_COLOR%22%3A%20%221%22%2C%20%22COLUMNS%22%3A%20%22110%22%7D)%0A%0A%0A%40app.function%0Adef%20show_run(done%3A%20subprocess.CompletedProcess)%20-%3E%20mo.Html%3A%0A%20%20%20%20%22%22%22A%20run%20of%20the%20script%3A%20the%20command%2C%20its%20exit%20code%20and%20what%20it%20means%2C%20then%20what%20it%20wrote%20to%20stdout%20and%20to%20stderr.%22%22%22%0A%20%20%20%20command%20%3D%20%22uv%20run%20python%20%22%20%2B%20%22%20%22.join(done.args%5B1%3A%5D)%0A%20%20%20%20meaning%20%3D%20EXIT_CODES.get(done.returncode%2C%20%22not%20a%20code%20this%20script%20returns%22)%0A%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22%60%7Bcommand%7D%60%5Cn%5Cnexit%20code%20**%7Bdone.returncode%7D**%3A%20%7Bmeaning%7D%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22**stdout**%22%20%2B%20(%22%22%20if%20done.stdout.strip()%20else%20%22%3A%20nothing%22))%2C%0A%20%20%20%20%20%20%20%20*(%5Bmo.plain_text(done.stdout.rstrip())%5D%20if%20done.stdout.strip()%20else%20%5B%5D)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22**stderr**%3A%20the%20log%22%20%2B%20(%22%22%20if%20done.stderr.strip()%20else%20%22%3A%20nothing%22))%2C%0A%20%20%20%20%20%20%20%20*(%5Bmo.plain_text(done.stderr.rstrip())%5D%20if%20done.stderr.strip()%20else%20%5B%5D)%2C%0A%20%20%20%20%5D)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Write%20the%20script%20from%20this%20notebook's%20functions%2C%20and%20show%20it%0A%20%20%20%20script%20%3D%20write_script(SCRATCH%20%2F%20%22tabulate.py%22)%0A%20%20%20%20_text%20%3D%20script.read_text(encoding%3D%22utf-8%22)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22Wrote%20%60scratch%2Fstretch_tabulation%2Ftabulate.py%60%3A%20**%7Blen(_text.splitlines())%3A%2C%7D%20lines**%2C%20module%207's%20functions%20and%20the%20ones%20above%2C%20in%20the%20order%20the%20script%20needs%20them.%22)%2C%0A%20%20%20%20%20%20%20%20mo.accordion(%7B%22The%20whole%20script%22%3A%20mo.ui.code_editor(value%3D_text%2C%20language%3D%22python%22%2C%20disabled%3DTrue)%7D)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Which%20exercises%20the%20script%20still%20waits%20for%3A%20each%20is%20tried%20on%20a%20small%20input%0A%20%20%20%20_tries%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22S2.7%22%3A%20(apply_cv_flags%2C%20(cv_fixture()%2C%20CAUTION_CV%2C%20SUPPRESS_CV))%2C%0A%20%20%20%20%20%20%20%20%22S2.8%22%3A%20(regions_to_read%2C%20(region_tables()%5B%22all_three%22%5D%2C%20EXPECTED_REGIONS%2C%20False))%2C%0A%20%20%20%20%20%20%20%20%22S2.9%22%3A%20(stratum_check%2C%20(strata_fixture()%2C))%2C%0A%20%20%20%20%20%20%20%20%22S2.10%22%3A%20(manifest%2C%20(%5BSCRATCH%20%2F%20%22design_decisions.csv%22%5D%2C%20SCRATCH))%2C%0A%20%20%20%20%7D%0A%20%20%20%20pending%20%3D%20%5Bnumber%20for%20number%2C%20(function%2C%20args)%20in%20_tries.items()%20if%20attempt(function%2C%20*args)%20is%20NOT_YET%5D%0A%20%20%20%20waiting%20%3D%20waiting_for(%22Exercises%20%22%20%2B%20%22%2C%20%22.join(pending))%20if%20pending%20else%20None%0A%20%20%20%20return%20pending%2C%20waiting%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%20Running%20it%0A%0A%20%20%20%20In%20a%20terminal%2C%20in%20the%20course%20folder%2C%20%60uv%20run%20python%20scratch%2Fstretch_tabulation%2Ftabulate.py%20--help%60%20shows%20what%20the%20script%20takes.%20The%20cells%20below%20run%20it%20exactly%20that%20way%20and%20show%20what%20it%20writes%20to%20each%20stream.%20First%2C%20the%20help%3A%20typer%20made%20it%20from%20the%20function's%20parameters%20and%20docstring%2C%20and%20the%20example%20and%20the%20exit%20codes%20come%20from%20%60help_epilog%60%2C%20so%20the%20list%20of%20codes%20cannot%20drift%20from%20%60EXIT_CODES%60.%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%20--help%3A%20no%20exercise%20needed%0A%20%20%20%20show_run(run_script(%22--help%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%20Now%20a%20run%20on%20a%20flat%20table%20made%20without%20the%20design%20decisions%2C%20so%20the%20three%20copied%20households%20stay%20in%3A%20the%20cell%20writes%20it%20as%20%60scratch%2Fstretch_tabulation%2Fhouseholds_unedited.parquet%60%2C%20and%20runs%20the%20script%20on%20it.%20The%20checks%20before%20publishing%20find%20a%20take-all%20stratum%20F%20that%20holds%20more%20sampling%20units%20than%20it%20has%2C%20and%20the%20script%20stops%20with%2065%20before%20it%20writes%20anything.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(pending%2C%20waiting)%3A%0A%20%20%20%20mo.stop(bool(pending)%2C%20waiting)%0A%20%20%20%20read_survey(PATHS.data%2C%20EXPECTED_REGIONS).to_parquet(SCRATCH%20%2F%20%22households_unedited.parquet%22%2C%20index%3DFalse)%0A%20%20%20%20show_run(run_script(%22--flat%22%2C%20%22scratch%2Fstretch_tabulation%2Fhouseholds_unedited.parquet%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%20On%20the%20flat%20table%20with%20the%20decisions%20applied%2C%20the%20run%20goes%20through%3A%20the%20log%20names%20each%20step%20and%20the%20file%2C%20stdout%20holds%20one%20line%20(the%20manifest's%20path)%2C%20and%20the%20exit%20code%20is%200.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(pending%2C%20waiting)%3A%0A%20%20%20%20mo.stop(bool(pending)%2C%20waiting)%0A%20%20%20%20first_run%20%3D%20run_script(%22--flat%22%2C%20%22scratch%2Fstretch_tabulation%2Fhouseholds.parquet%22%2C%20%22--out%22%2C%20%22scratch%2Fstretch_tabulation%2Ftables%22)%0A%20%20%20%20show_run(first_run)%0A%20%20%20%20return%20(first_run%2C)%0A%0A%0A%40app.cell%0Adef%20_(first_run)%3A%0A%20%20%20%20%23%20What%20the%20run%20wrote%3A%20the%20manifest%2C%20and%20from%20the%20workbook%20the%20households%20by%20scale%2C%20flagged%2C%20and%20the%20notes%0A%20%20%20%20mo.stop(first_run.returncode%20!%3D%200%2C%20mo.callout(mo.md(%22The%20run%20above%20did%20not%20finish%3A%20its%20log%20says%20why.%22)%2C%20kind%3D%22warn%22))%0A%20%20%20%20first_manifest%20%3D%20(SCRATCH%20%2F%20%22tables%22%20%2F%20%22manifest.csv%22).read_bytes()%0A%20%20%20%20_book%20%3D%20pd.read_excel(SCRATCH%20%2F%20%22tables%22%20%2F%20%22tables.xlsx%22%2C%20sheet_name%3DNone)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22%60scratch%2Fstretch_tabulation%2Ftables%2Fmanifest.csv%60%3A%22)%2C%0A%20%20%20%20%20%20%20%20pd.read_csv(SCRATCH%20%2F%20%22tables%22%20%2F%20%22manifest.csv%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22%60scratch%2Fstretch_tabulation%2Ftables%2Ftables.xlsx%60%20holds%20**%7Blen(_book)%7D%20sheets**%3A%20module%207's%20%7Blen(_book)%20-%201%7D%20and%20the%20notes.%20Its%20households%20by%20scale%2C%20flagged%3A%22)%2C%0A%20%20%20%20%20%20%20%20_book%5B%22households_by_scale%22%5D%2C%0A%20%20%20%20%20%20%20%20mo.md(%22Its%20notes%3A%22)%2C%0A%20%20%20%20%20%20%20%20_book%5B%22Notes%22%5D%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20(first_manifest%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**A%20second%20run.**%20Run%20the%20same%20command%20again.%20It%20replaces%20the%20files%20rather%20than%20adding%20to%20them%2C%20and%20its%20manifest%20should%20match%20the%20first%20one's%20to%20the%20byte%3A%20same%20input%2C%20same%20tables.%20(The%20notes%20give%20the%20date%20the%20tables%20were%20made%2C%20which%20is%20today%20for%20both%20runs.%20%60--as-of%202026-11-08%60%20fixes%20that%20date%2C%20to%20remake%20an%20earlier%20run%20exactly.)%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(first_manifest%2C%20first_run)%3A%0A%20%20%20%20mo.stop(first_run.returncode%20!%3D%200)%0A%20%20%20%20_second%20%3D%20run_script(%22--flat%22%2C%20%22scratch%2Fstretch_tabulation%2Fhouseholds.parquet%22%2C%20%22--out%22%2C%20%22scratch%2Fstretch_tabulation%2Ftables%22)%0A%20%20%20%20_same%20%3D%20(SCRATCH%20%2F%20%22tables%22%20%2F%20%22manifest.csv%22).read_bytes()%20%3D%3D%20first_manifest%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20show_run(_second)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22The%20second%20run's%20manifest%20is%20**%7B'identical%20to'%20if%20_same%20else%20'different%20from'%7D**%20the%20first%20one's.%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2B%20(%22%20The%20flat%20table%20and%20the%20workbook%20have%20the%20same%20fingerprints%2C%20although%20openpyxl%20saved%20the%20workbook%20again.%22%20if%20_same%20else%20%22%20Compare%20the%20two%20to%20find%20which%20file%20changed.%22))%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**A%20missing%20region.**%20The%20cell%20below%20writes%20a%20flat%20table%20without%20region%2004's%20households%2C%20as%20if%20its%20database%20had%20not%20arrived%20when%20the%20table%20was%20made%2C%20in%20%60scratch%2Fstretch_tabulation%2Fhouseholds_two_regions.parquet%60%2C%20and%20runs%20the%20script%20on%20it%20twice%3A%20as%20it%20is%2C%20then%20with%20%60--allow-missing%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame%2C%20pending%2C%20waiting)%3A%0A%20%20%20%20mo.stop(bool(pending)%2C%20waiting)%0A%20%20%20%20%23%20A%20flat%20table%20with%20region%2004's%20households%20missing%0A%20%20%20%20frame%5Bframe%5B%22region_code%22%5D%20!%3D%20%2204%22%5D.to_parquet(SCRATCH%20%2F%20%22households_two_regions.parquet%22%2C%20index%3DFalse)%0A%20%20%20%20_options%20%3D%20%5B%22--flat%22%2C%20%22scratch%2Fstretch_tabulation%2Fhouseholds_two_regions.parquet%22%2C%20%22--out%22%2C%20%22scratch%2Fstretch_tabulation%2Ftables_two_regions%22%5D%0A%20%20%20%20mo.vstack(%5Bshow_run(run_script(*_options))%2C%20show_run(run_script(*_options%2C%20%22--allow-missing%22))%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%20The%20first%20run%20stops%20with%2066%20and%20says%20which%20region%20is%20missing.%20The%20second%2C%20told%20on%20purpose%20to%20go%20ahead%2C%20warns%20about%20region%2004%20in%20its%20log%2C%20and%20the%20workbook's%20notes%20sheet%20says%20the%20tables%20leave%20the%20region%20out.%20The%20decision%20is%20now%20on%20record%20in%20two%20places.%0A%0A%20%20%20%20**Further%20reading.**%20A%20command%20used%20by%20many%20people%2C%20or%20by%20other%20programs%2C%20does%20more%20than%20this%20one.%20Checklists%20for%20command-line%20tools%2C%20such%20as%20the%20*Command%20Line%20Interface%20Guidelines*%20(clig.dev)%2C%20cover%20what%20to%20add%20next%3A%20a%20%60--version%60%20option%3B%20%60--dry-run%60%2C%20which%20says%20what%20a%20run%20would%20write%20without%20writing%20it%3B%20%60--verbose%60%20and%20%60--quiet%60%20to%20change%20how%20much%20the%20log%20says%3B%20%60--json%60%2C%20for%20output%20another%20program%20reads%3B%20one%20module%20of%20failure%20kinds%20and%20exit%20codes%20that%20every%20script%20of%20a%20team%20shares%3B%20a%20state%20file%20for%20scripts%20of%20many%20slow%20steps%3B%20configuration%20files%3B%20and%20tests%20that%20run%20the%20command%20and%20check%20its%20exit%20codes%2C%20as%20this%20course's%20own%20tests%20do%20for%20this%20script.%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%20marimo%20notebook%20as%20the%20report%0A%0A%20%20%20%20**The%20problem.**%20The%20workbook%20is%20for%20people%20who%20use%20the%20numbers.%20A%20director%2C%20a%20provincial%20office%20or%20a%20journalist%20wants%20a%20page%20they%20can%20read%3A%20what%20the%20survey%20estimates%2C%20which%20estimates%20are%20reliable%2C%20and%20where%20the%20numbers%20come%20from.%20A%20report%20written%20by%20hand%20in%20a%20word%20processor%20copies%20numbers%20out%20of%20the%20workbook%2C%20and%20every%20copy%20is%20a%20place%20where%20the%20report%20and%20the%20tables%20can%20disagree%3A%20a%20number%20retyped%20wrong%2C%20or%20left%20as%20it%20was%20when%20the%20tables%20were%20remade.%0A%0A%20%20%20%20**The%20tool.**%20A%20marimo%20notebook%20whose%20cells%20**read**%20the%20script's%20outputs%3A%20the%20workbook%2C%20its%20notes%20and%20the%20manifest.%20Its%20sentences%20are%20f-strings%2C%20so%20every%20number%20in%20them%20comes%20from%20a%20file%2C%20and%20when%20the%20tables%20are%20remade%20the%20report%20is%20too.%20Exported%20as%20HTML%20without%20its%20code%2C%20it%20is%20a%20single%20page%20anyone%20can%20open%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20uv%20run%20marimo%20export%20html%20scratch%2Fstretch_tabulation%2Freport.py%20-o%20scratch%2Fstretch_tabulation%2Freport.html%20--no-include-code%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20R%20users%20know%20the%20idea%20as%20R%20Markdown%20or%20Quarto%20(%60quarto%20render%20report.qmd%60).%20The%20difference%20here%20is%20that%20the%20report%20is%20a%20marimo%20notebook%20like%20this%20one%2C%20so%20it%20runs%2C%20and%20can%20be%20checked%2C%20the%20same%20way.%0A%0A%20%20%20%20A%20report%20needs%20a%20picture%20of%20reliability.%20The%20exercise%20below%20draws%20one%3B%20the%20report%20then%20uses%20it.%0A%0A%20%20%20%20%23%23%20S2.11%20%C2%B7%20A%20chart%20of%20the%20CVs%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20reader%20takes%20in%20at%20a%20glance%20which%20provinces%20have%20reliable%20figures%2C%20which%20no%20table%20of%20CVs%20gives.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20long%20table%20of%20one%20level's%20estimates%20(a%20row%20per%20domain%20and%20indicator%2C%20with%20%60domain%60%2C%20%60indicator%60%20and%20%60cv%60)%20and%20the%20name%20of%20an%20indicator%20%E2%86%92%20a%20bar%20chart%20with%20**one%20bar%20per%20domain**%20of%20that%20indicator%2C%20its%20length%20the%20**CV%20in%20percent**%20(a%20CV%20of%200.2957%20is%20a%20bar%20of%2029.57).%20The%20rows%20of%20the%20check's%20small%20made-up%20table%3A%0A%0A%20%20%20%20%7C%20domain%20%7C%20indicator%20%7C%20cv%20%7C%20%E2%86%92%20bar%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20La%20Union%20%7C%20households%20running%20a%20large-scale%20unit%20%7C%200.2957%20%7C%2029.57%20%7C%0A%20%20%20%20%7C%20Pangasinan%20%7C%20households%20running%20a%20large-scale%20unit%20%7C%200.2509%20%7C%2025.09%20%7C%0A%20%20%20%20%7C%20La%20Union%20%7C%20fishing%20households%20%7C%200.0334%20%7C%20*(another%20indicator%3A%20no%20bar)*%20%7C%0A%0A%20%20%20%20Draw%20it%20with%20matplotlib%2C%20seaborn%20or%20Altair%3B%20horizontal%20or%20vertical%20bars%20both%20pass.%20Give%20it%20a%20title%20and%20label%20the%20CV%20axis%20with%20its%20unit%3B%20lines%20at%20the%20two%20thresholds%20help%20the%20reader%2C%20and%20the%20check%20ignores%20them.%20Return%20the%20chart%3A%20%60ax%60%20for%20matplotlib%20or%20seaborn%2C%20the%20chart%20for%20Altair.%0A%0A%20%20%20%20**Tools%3A**%20a%20filter%20on%20%60indicator%60%2C%20%60cv%20*%20100%60%2C%20and%20a%20bar%20chart%3A%20%60ax.barh(...)%60%20(Arrays%20%26%20DataFrames%2C%20module%207)%2C%20%60sns.barplot%60%2C%20or%20%60alt.Chart(...).mark_bar()%60%3B%20%60ax.axvline(x)%60%20for%20a%20threshold%20line.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20cv_chart(table%3A%20pd.DataFrame%2C%20indicator%3A%20str)%3A%0A%20%20%20%20%22%22%22A%20bar%20chart%20of%20one%20indicator's%20CVs%3A%20one%20bar%20per%20domain%2C%20its%20length%20the%20CV%20in%20percent.%20Returns%20the%20chart.%22%22%22%0A%20%20%20%20%23%20Exercise%20S2.11%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20rows%20of%20this%20indicator%20only%0A%20%20%20%20%23%20Step%202%3A%20a%20bar%20per%20domain%2C%20of%20length%20cv%20*%20100%20(matplotlib%3A%20fig%2C%20ax%20%3D%20plt.subplots()%3B%20ax.barh(domains%2C%20lengths))%0A%20%20%20%20%23%20Step%203%3A%20label%20the%20CV%20axis%20with%20its%20unit%2C%20give%20the%20chart%20a%20title%3B%20lines%20at%20the%20thresholds%20if%20you%20like%0A%20%20%20%20%23%20Step%204%3A%20return%20the%20chart%20(ax%2C%20or%20the%20Altair%20chart)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S2.11%22)%0A%0A%0A%40app.function%0Adef%20chart_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Three%20domains%20with%20two%20indicators%20each%2C%20made%20up%2C%20used%20by%20the%20checks%3A%20the%20same%20domains%20under%20both%20indicators%2C%20so%20a%0A%20%20%20%20chart%20of%20both%20draws%20each%20domain%20twice.%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%22Pangasinan%22%2C%20%22Bohol%22%5D%20*%202%2C%0A%20%20%20%20%20%20%20%20%22indicator%22%3A%20%5B%22households%20running%20a%20large-scale%20unit%22%5D%20*%203%20%2B%20%5B%22fishing%20households%22%5D%20*%203%2C%0A%20%20%20%20%20%20%20%20%22estimate%22%3A%20%5B92.1%2C%20157.6%2C%2092.2%2C%203965.9%2C%203787.5%2C%203599.2%5D%2C%0A%20%20%20%20%20%20%20%20%22cv%22%3A%20%5B0.2957%2C%200.2509%2C%200.2917%2C%200.0334%2C%200.0338%2C%200.0349%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.function%0Adef%20chart_summary(chart)%20-%3E%20list%5Bdict%5D%3A%0A%20%20%20%20%22%22%22What%20a%20check%20of%20Exercise%20S2.11%20reads%3A%20the%20bars%20(each%20domain%20and%20its%20length)%2C%20whichever%20library%20drew%20them%3B%20then%20the%0A%20%20%20%20figure%20is%20closed%2C%20so%20the%20next%20chart%20starts%20on%20a%20fresh%20one.%22%22%22%0A%20%20%20%20bars%20%3D%20%5B%7B**row%2C%20%22y%22%3A%20round(row%5B%22y%22%5D%2C%202)%7D%20for%20row%20in%20course.chart_data(chart%2C%20marks%3D(%22bar%22%2C)%2C%20series%3DFalse)%5D%0A%20%20%20%20figure%20%3D%20getattr(chart%2C%20%22figure%22%2C%20None)%0A%20%20%20%20if%20type(figure).__name__%20%3D%3D%20%22Figure%22%3A%0A%20%20%20%20%20%20%20%20plt.close(figure)%0A%20%20%20%20return%20bars%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(geography%2C%20sample)%3A%0A%20%20%20%20%23%20The%20survey's%20provinces%20and%20regions%2C%20named%20from%20the%20code%20list%0A%20%20%20%20_provinces%20%3D%20geography%5B%5B%22region_code%22%2C%20%22province_code%22%2C%20%22province%22%5D%5D.drop_duplicates().rename(columns%3D%7B%22province%22%3A%20%22domain%22%7D)%0A%20%20%20%20_regions%20%3D%20geography%5B%5B%22region_code%22%2C%20%22region%22%5D%5D.drop_duplicates().rename(columns%3D%7B%22region%22%3A%20%22domain%22%7D)%0A%20%20%20%20_by_province%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%2C%20%22province_code%22%5D).merge(_provinces%2C%20on%3D%5B%22region_code%22%2C%20%22province_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)%0A%20%20%20%20_by_region%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).merge(_regions%2C%20on%3D%22region_code%22).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)%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S2.11%22%2C%20cv_chart%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20domains%2C%20two%20indicators%3A%20households%20running%20a%20large-scale%20unit%22%2C%20(chart_fixture()%2C%20%22households%20running%20a%20large-scale%20unit%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20provinces%3A%20households%20running%20a%20large-scale%20unit%22%2C%20(_by_province%2C%20%22households%20running%20a%20large-scale%20unit%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20provinces%3A%20mean%20household%20size%22%2C%20(_by_province%2C%20%22mean%20household%20size%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20regions%3A%20household%20members%22%2C%20(_by_region%2C%20%22household%20members%22))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22charts%22%2C%20summarize%3Dchart_summary%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Keep%20the%20indicator's%20rows%20only%3B%20one%20bar%20per%20domain%2C%20its%20length%20cv%20*%20100%3B%20return%20ax%20(or%20the%20Altair%20chart).%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%20small%20table's%20households%20running%20a%20large-scale%20unit%0A%20%20%20%20def%20_try_chart()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20chart%20%3D%20cv_chart(chart_fixture()%2C%20%22households%20running%20a%20large-scale%20unit%22)%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(cv_chart%2C%20chart_fixture()%2C%20%22households%20running%20a%20large-scale%20unit%22)%20%20%23%20runs%20it%20again%2C%20to%20show%20the%20error%0A%20%20%20%20%20%20%20%20if%20chart%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22%60cv_chart%60%20gave%20back%20%60None%60%3A%20end%20it%20with%20%60return%20ax%60%20(matplotlib%20or%20seaborn)%20or%20%60return%20chart%60%20(Altair).%22)%2C%20kind%3D%22warn%22)%0A%20%20%20%20%20%20%20%20return%20chart%0A%0A%20%20%20%20_try_chart()%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%20S2.11%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%20Twice%20as%20many%20bars%20as%20domains%2C%20or%20the%20bar%20for%20La%20Union%20at%203.34%20%7C%20Every%20indicator's%20rows%20were%20drawn.%20%7C%20%60table%5Btable%5B%22indicator%22%5D%20%3D%3D%20indicator%5D%60%20%7C%0A%20%20%20%20%7C%20Bars%20of%200.30%20instead%20of%2029.57%20%7C%20The%20CV%20drawn%20as%20a%20fraction.%20%7C%20%60cv%20*%20100%60%20%7C%0A%20%20%20%20%7C%20%60None%60%20%7C%20The%20function%20draws%20but%20returns%20nothing.%20%7C%20%60return%20ax%60%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20chart_data%20reads%20a%20matplotlib%2C%20seaborn%20or%20Altair%20chart*%20%7C%20The%20figure's%20%60Figure%60%20was%20returned%20with%20several%20Axes%2C%20or%20something%20that%20is%20not%20a%20chart.%20%7C%20return%20the%20%60ax%60%20you%20drew%20on%20%7C%0A%20%20%20%20%7C%20The%20bars'%20lengths%20are%20the%20estimates%20%7C%20%60estimate%60%20drawn%20instead%20of%20%60cv%60.%20%7C%20the%20%60cv%60%20column%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%20report%0A%0A%20%20%20%20%60write_report%60%20writes%20the%20report%20notebook%2C%20%60scratch%2Fstretch_tabulation%2Freport.py%60%2C%20beside%20the%20script's%20%60tables%2F%60%20folder.%20Its%20text%20is%20below%3A%20a%20marimo%20notebook%20like%20this%20one%2C%20with%20your%20%60cv_chart%60%20and%20the%20setting%20%60TABLES%60%20copied%20in.%20It%20turns%20the%20provincial%20rows%20of%20each%20sheet%20into%20the%20long%20table%20%60cv_chart%60%20takes%2C%20a%20row%20per%20province%20and%20table%20(a%20table%20cut%20by%20its%20own%20variables%2C%20as%20the%20households%20by%20scale%2C%20gives%20a%20row%20per%20category)%2C%20with%20the%20provinces%20named%20from%20the%20course's%20code%20list.%20Every%20sentence%20that%20holds%20a%20number%20reads%20it%20from%20the%20outputs%2C%20and%20the%20last%20part%20lists%20the%20files%20the%20tables%20were%20made%20from%2C%20with%20their%20fingerprints%20from%20the%20manifest.%20The%20cell%20after%20it%20exports%20the%20report%20as%20HTML%2C%20as%20the%20command%20above%20does.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20report_source()%20-%3E%20str%3A%0A%20%20%20%20%22%22%22The%20report%20notebook's%20text%2C%20with%20cv_chart%20copied%20in%20from%20this%20notebook's%20file%20and%20TABLES%20and%20the%20thresholds%20as%0A%20%20%20%20they%20are%20here.%22%22%22%0A%20%20%20%20source%20%3D%20(PATHS.root%20%2F%20NOTEBOOK).read_text(encoding%3D%22utf-8%22)%0A%20%20%20%20lines%20%3D%20source.splitlines()%0A%20%20%20%20node%20%3D%20next(n%20for%20n%20in%20ast.parse(source).body%20if%20isinstance(n%2C%20ast.FunctionDef)%20and%20n.name%20%3D%3D%20%22cv_chart%22)%0A%20%20%20%20chart%20%3D%20textwrap.dedent(%22%5Cn%22.join(lines%5Bnode.lineno%20-%201%3Anode.end_lineno%5D))%0A%20%20%20%20report%20%3D%20'''import%20marimo%0A%0Aapp%20%3D%20marimo.App(width%3D%22medium%22%2C%20app_title%3D%22Fishing%20households%3A%20the%20tables%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20pandas%20as%20pd%0A%0A%20%20%20%20OUT%20%3D%20mo.notebook_dir()%20%2F%20%22tables%22%20%20%23%20the%20script's%20outputs%2C%20beside%20this%20report%0A%20%20%20%20CODES%20%3D%20mo.notebook_dir().parents%5B1%5D%20%2F%20%22data%22%20%2F%20%22codes%22%20%2F%20%22geography.csv%22%20%20%23%20the%20course's%20code%20list%2C%20for%20the%20provinces'%20names%0A%20%20%20%20CAUTION_CV%20%3D%20__CAUTION__%0A%20%20%20%20SUPPRESS_CV%20%3D%20__SUPPRESS__%0A%20%20%20%20TABLES%20%3D%20__TABLES__%20%20%23%20the%20script's%20tables%3A%20each%20one's%20indicator%20and%20its%20own%20variables%0A%0A%0A%40app.function%0A__CHART__%0A%0A%0A%40app.function%0Adef%20long_table(sheets%3A%20dict%2C%20names%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20provincial%20rows%20of%20every%20sheet%20as%20one%20long%20table%3A%20a%20row%20per%20province%20and%20table%2C%20its%20indicator%20followed%20by%20the%0A%20%20%20%20table's%20own%20categories%20(as%20%22fishing%20households%2C%20Large-scale%22)%2C%20with%20the%20province%20named%20from%20%60names%60.%22%22%22%0A%20%20%20%20parts%20%3D%20%5B%5D%0A%20%20%20%20for%20table%2C%20(indicator%2C%20domains)%20in%20TABLES.items()%3A%0A%20%20%20%20%20%20%20%20sheet%20%3D%20sheets%5Btable%5D%0A%20%20%20%20%20%20%20%20sheet%20%3D%20sheet%5Bsheet%5B%22level%22%5D%20%3D%3D%20%22provincial%22%5D%0A%20%20%20%20%20%20%20%20label%20%3D%20indicator%20%2B%20%22%2C%20%22%20%2B%20sheet%5Bdomains%5D.astype(str).agg(%22%2C%20%22.join%2C%20axis%3D1)%20if%20domains%20else%20indicator%0A%20%20%20%20%20%20%20%20parts.append(sheet.assign(indicator%3Dlabel))%0A%20%20%20%20long%20%3D%20pd.concat(parts%2C%20ignore_index%3DTrue).merge(names%2C%20on%3D%5B%22region_code%22%2C%20%22province_code%22%5D%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20return%20long.rename(columns%3D%7B%22province%22%3A%20%22domain%22%7D)%5B%5B%22domain%22%2C%20%22indicator%22%2C%20%22estimate%22%2C%20%22cv%22%2C%20%22flag%22%2C%20%22n%22%5D%5D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20sheets%20%3D%20pd.read_excel(OUT%20%2F%20%22tables.xlsx%22%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%20notes%20%3D%20sheets%5B%22Notes%22%5D.set_index(%22item%22)%5B%22note%22%5D%0A%20%20%20%20manifest%20%3D%20pd.read_csv(OUT%20%2F%20%22manifest.csv%22)%0A%20%20%20%20names%20%3D%20pd.read_csv(CODES%2C%20dtype%3Dstr)%0A%20%20%20%20names%20%3D%20names.assign(region_code%3Dnames%5B%22region_code%22%5D.str.zfill(2)%2C%20province_code%3Dnames%5B%22province_code%22%5D.str.zfill(3))%5B%5B%22region_code%22%2C%20%22province_code%22%2C%20%22province%22%5D%5D.drop_duplicates()%0A%20%20%20%20by_province%20%3D%20long_table(sheets%2C%20names)%0A%20%20%20%20return%20by_province%2C%20manifest%2C%20notes%2C%20sheets%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(notes%2C%20sheets)%3A%0A%20%20%20%20def%20national(table)%3A%0A%20%20%20%20%20%20%20%20rows%20%3D%20sheets%5Btable%5D%0A%20%20%20%20%20%20%20%20return%20rows%5Brows%5B%22level%22%5D%20%3D%3D%20%22national%22%5D%0A%0A%20%20%20%20households%20%3D%20national(%22fishing_households%22).iloc%5B0%5D%0A%20%20%20%20size%20%3D%20national(%22mean_household_size%22).iloc%5B0%5D%0A%20%20%20%20large%20%3D%20national(%22households_by_scale%22).set_index(%22scale%22).loc%5B%22Large-scale%22%5D%0A%20%20%20%20flag%20%3D%20f%22%2C%20flagged%20%7Blarge%5B'flag'%5D%7D%3A%20read%20it%20with%20care%22%20if%20isinstance(large%5B%22flag%22%5D%2C%20str)%20and%20large%5B%22flag%22%5D%20else%20%22%22%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%23%20Fishing%20households%3A%20the%20tables%0A%0A%20%20%20%20The%20Survey%20of%20Fishing%20Households%202025%20stands%20for%20**%7Bhouseholds%5B'estimate'%5D%3A%2C.0f%7D%20fishing%20households**%20in%20regions%20%7Bnotes%5B'Regions%20in%20the%20tables'%5D%7D%20(CV%20%7Bhouseholds%5B'cv'%5D%3A.1%25%7D)%2C%20with%20a%20mean%20of%20**%7Bsize%5B'estimate'%5D%3A.2f%7D%20members**%20each.%20About%20**%7Blarge%5B'estimate'%5D%3A%2C.0f%7D**%20of%20them%20run%20a%20large-scale%20unit%2C%20one%20with%20a%20boat%20of%205.0%20gross%20tons%20or%20more%3B%20that%20estimate's%20CV%20is%20%7Blarge%5B'cv'%5D%3A.1%25%7D%7Bflag%7D.%0A%0A%20%20%20%20Regions%20missing%3A%20%7Bnotes%5B'Regions%20missing'%5D%7D.%20Tables%20made%20on%20%7Bnotes%5B'Tables%20made%20on'%5D%7D.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(by_province)%3A%0A%20%20%20%20shown%20%3D%20by_province.assign(%0A%20%20%20%20%20%20%20%20estimate%3Dby_province%5B%22estimate%22%5D.map(lambda%20v%3A%20%22%22%20if%20pd.isna(v)%20else%20f%22%7Bv%3A%2C.2f%7D%22%20if%20v%20%3C%20100%20else%20f%22%7Bv%3A%2C.0f%7D%22)%2C%0A%20%20%20%20%20%20%20%20cv%3Dby_province%5B%22cv%22%5D.map(%22%7B%3A.1%25%7D%22.format)%2C%0A%20%20%20%20%20%20%20%20flag%3Dby_province%5B%22flag%22%5D.fillna(%22%22)%2C%0A%20%20%20%20)%0A%20%20%20%20mo.vstack(%5Bmo.md(%22%23%23%20By%20province%22)%2C%20mo.ui.table(shown%2C%20selection%3DNone%2C%20page_size%3D30)%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(by_province)%3A%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22%23%23%20How%20reliable%20the%20provinces'%20figures%20are%22)%2C%0A%20%20%20%20%20%20%20%20cv_chart(by_province%2C%20%22fishing%20households%2C%20Large-scale%22)%2C%0A%20%20%20%20%20%20%20%20cv_chart(by_province%2C%20%22fishing%20households%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(manifest%2C%20notes)%3A%0A%20%20%20%20flags%20%3D%20%22%5C%5Cn%22.join(f%22-%20**%7Bitem%7D**%3A%20%7Bnotes%5Bitem%5D%7D%22%20for%20item%20in%20(%22Source%22%2C%20%22Publication%20rule%22%2C%20%22flag%20caution%22%2C%20%22flag%20suppress%22))%0A%20%20%20%20files%20%3D%20%22%5C%5Cn%22.join(f%22%7C%20%7Brow.role%7D%20%7C%20%60%7Brow.file%7D%60%20%7C%20%60%7Brow.fingerprint%5B%3A16%5D%7D%60%20%7C%22%20for%20row%20in%20manifest.itertuples())%0A%20%20%20%20table%20%3D%20%22%7C%20role%20%7C%20file%20%7C%20fingerprint%20(first%2016%20characters)%20%7C%5C%5Cn%7C%3A--%7C%3A--%7C%3A--%7C%5C%5Cn%22%20%2B%20files%0A%20%20%20%20mo.md(%22%5C%5Cn%5C%5Cn%22.join(%5B%22%23%23%20What%20the%20numbers%20rest%20on%22%2C%20%22From%20the%20workbook's%20notes%3A%22%2C%20flags%2C%20table%5D))%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'''%0A%20%20%20%20return%20(report.replace(%22__CHART__%22%2C%20chart).replace(%22__CAUTION__%22%2C%20repr(CAUTION_CV)).replace(%22__SUPPRESS__%22%2C%20repr(SUPPRESS_CV))%0A%20%20%20%20%20%20%20%20%20%20%20%20.replace(%22__TABLES__%22%2C%20repr(TABLES)))%0A%0A%0A%40app.function%0Adef%20write_report(path%3A%20Path)%20-%3E%20Path%3A%0A%20%20%20%20%22%22%22Write%20the%20report%20notebook%20to%20%60path%60.%22%22%22%0A%20%20%20%20path.parent.mkdir(parents%3DTrue%2C%20exist_ok%3DTrue)%0A%20%20%20%20path.write_text(report_source()%2C%20encoding%3D%22utf-8%22)%0A%20%20%20%20return%20path%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20report%20notebook's%20text%20(your%20cv_chart%20is%20copied%20in%20where%20it%20says%20%40app.function)%0A%20%20%20%20mo.accordion(%7B%22The%20report%20notebook%2C%20scratch%2Fstretch_tabulation%2Freport.py%22%3A%20mo.ui.code_editor(value%3Dreport_source()%2C%20language%3D%22python%22%2C%20disabled%3DTrue)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(first_run%2C%20pending)%3A%0A%20%20%20%20%23%20Write%20the%20report%2C%20and%20export%20it%20as%20one%20HTML%20page%20without%20its%20code%0A%20%20%20%20mo.stop(bool(pending)%20or%20attempt(cv_chart%2C%20chart_fixture()%2C%20%22fishing%20households%22)%20is%20NOT_YET%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20waiting_for(%22Exercise%20S2.11%20and%20the%20script's%20run%20above%22))%0A%20%20%20%20mo.stop(first_run.returncode%20!%3D%200%2C%20mo.callout(mo.md(%22The%20script's%20run%20above%20did%20not%20finish%2C%20so%20the%20report%20has%20no%20tables%20to%20read.%22)%2C%20kind%3D%22warn%22))%0A%20%20%20%20plt.close(%22all%22)%0A%20%20%20%20write_report(SCRATCH%20%2F%20%22report.py%22)%0A%20%20%20%20_export%20%3D%20subprocess.run(%5Bsys.executable%2C%20%22-m%22%2C%20%22marimo%22%2C%20%22export%22%2C%20%22html%22%2C%20%22scratch%2Fstretch_tabulation%2Freport.py%22%2C%20%22-o%22%2C%20%22scratch%2Fstretch_tabulation%2Freport.html%22%2C%20%22--no-include-code%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cwd%3DPATHS.root%2C%20capture_output%3DTrue%2C%20text%3DTrue%2C%20timeout%3D600)%0A%20%20%20%20_page%20%3D%20SCRATCH%20%2F%20%22report.html%22%0A%20%20%20%20if%20_export.returncode%20%3D%3D%200%20and%20_page.exists()%3A%0A%20%20%20%20%20%20%20%20_said%20%3D%20mo.md(f%22%60uv%20run%20marimo%20export%20html%20scratch%2Fstretch_tabulation%2Freport.py%20-o%20scratch%2Fstretch_tabulation%2Freport.html%20--no-include-code%60%20wrote%20**%60scratch%2Fstretch_tabulation%2Freport.html%60**%2C%20%7B_page.stat().st_size%20%2F%201024%3A%2C.0f%7D%20KB.%20Open%20it%20in%20a%20browser%3A%20the%20tables%2C%20the%20charts%20and%20the%20sources%2C%20and%20no%20code.%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20_said%20%3D%20mo.callout(mo.md(f%22The%20export%20did%20not%20finish%20(exit%20code%20%7B_export.returncode%7D).%20Its%20last%20lines%3A%5Cn%5Cn%60%60%60text%5Cn%7B_export.stderr%5B-1500%3A%5D%7D%5Cn%60%60%60%22)%2C%20kind%3D%22warn%22)%0A%20%20%20%20_said%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%20To%20work%20on%20the%20report%20itself%2C%20open%20it%20as%20a%20notebook%3A%20%60uv%20run%20marimo%20edit%20scratch%2Fstretch_tabulation%2Freport.py%60.%20To%20show%20it%20as%20an%20app%20with%20its%20controls%20working%2C%20%60uv%20run%20marimo%20run%20scratch%2Fstretch_tabulation%2Freport.py%60.%20On%20the%20next%20batch%2C%20the%20order%20is%20the%20same%3A%20run%20the%20script%2C%20then%20export%20the%20report.%20Neither%20step%20involves%20copying%20a%20number%20by%20hand.%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%20these%20eleven%20add%20up%20to%0A%0A%20%20%20%20%7C%20exercise%20%7C%20what%20a%20module's%20tables%20leave%20out%20%7C%20the%20habit%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20S2.1%20%7C%20a%20unit%20in%20several%20rows%20of%20one%20table%20%7C%20count%20units%2C%20not%20rows%2C%20in%20every%20row%20and%20in%20the%20total%20%7C%0A%20%20%20%20%7C%20S2.2%20%7C%20what%20the%20design%20costs%20in%20precision%20%7C%20the%20deff%3A%20the%20design's%20variance%20over%20a%20simple%20random%20sample's%20of%20the%20same%20size%20%7C%0A%20%20%20%20%7C%20S2.3%20%7C%20which%20estimates%20are%20fit%20to%20publish%20%7C%20a%20rule%20on%20the%20CV%2C%20its%20thresholds%20as%20arguments%3B%20a%20missing%20CV%20is%20the%20least%20reliable%20%7C%0A%20%20%20%20%7C%20S2.4%20%7C%20margins%20%7C%20each%20margin%20estimated%20on%20its%20own%3A%20totals%20add%20up%2C%20standard%20errors%20do%20not%20%7C%0A%20%20%20%20%7C%20S2.5%20%7C%20small%20shares%20in%20small%20domains%20%7C%20an%20interval%20on%20the%20logit%20scale%2C%20which%20stays%20between%200%20and%201%3B%20the%20method%20named%20in%20the%20notes%20%7C%0A%20%20%20%20%7C%20S2.6%20%7C%20a%20reader%20who%20compares%20across%20a%20row%20%7C%20the%20cross%20table%2C%20with%20a%20cell's%20reliability%20in%20how%20it%20is%20shown%20and%20the%20number%20kept%20a%20number%20%7C%0A%20%20%20%20%7C%20S2.7%20%7C%20the%20rule%20in%20the%20published%20tables%20%7C%20flags%20from%20thresholds%20set%20once%3B%20a%20suppressed%20estimate%20goes%20with%20its%20SE%20and%20its%20interval%20%7C%0A%20%20%20%20%7C%20S2.8%20%7C%20a%20region%20missing%20from%20the%20flat%20table%20%7C%20refuse%20by%20default%3B%20a%20partial%20run%20is%20a%20decision%20someone%20types%20%7C%0A%20%20%20%20%7C%20S2.9%20%7C%20a%20stratum%20that%20holds%20more%20sampling%20units%20than%20it%20has%20%7C%20count%20units%2C%20not%20rows%2C%20and%20name%20the%20stratum%20%7C%0A%20%20%20%20%7C%20S2.10%20%7C%20proof%20that%20a%20re-run%20changed%20nothing%20%7C%20a%20manifest%20of%20fingerprints%3A%20bytes%20for%20text%20files%2C%20contents%20for%20workbooks%20%7C%0A%20%20%20%20%7C%20S2.11%20%7C%20a%20page%20for%20readers%20%7C%20a%20chart%20and%20a%20report%20whose%20every%20number%20is%20read%20from%20the%20script's%20outputs%20%7C%0A%0A%20%20%20%20Next%3A%20module%208%20(%5B%6070_anomalies_rules.py%60%5D(%2F%3Ffile%3D70_anomalies_rules.py))%20goes%20back%20to%20the%20microdata%2C%20to%20find%20what%20is%20wrong%20before%20anything%20is%20published.%20The%20stretch%20S3%20(%5B%6081_stretch_detection.py%60%5D(%2F%3Ffile%3D81_stretch_detection.py))%2C%20after%20module%209%2C%20takes%20detection%20further.%0A%20%20%20%20%22%22%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
0ef02ad938b6ef1623bb0ec5d4ab5314