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%22Capstone%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20settings%20every%20other%20cell%20uses.%0A%20%20%20%20import%20atexit%0A%20%20%20%20import%20hashlib%0A%20%20%20%20import%20json%0A%20%20%20%20import%20os%0A%20%20%20%20import%20re%0A%20%20%20%20import%20shutil%0A%20%20%20%20import%20tempfile%0A%20%20%20%20from%20datetime%20import%20date%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20duckdb%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20polars%20as%20pl%0A%20%20%20%20import%20svy%0A%20%20%20%20from%20openpyxl%20import%20load_workbook%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20file%20locations%2C%20the%20survey%20read%20the%20way%20the%20course%20teaches%2C%20and%20exercise%20checks%0A%20%20%20%20from%20course%20import%20CHILD_PREFIX%2C%20NOT_YET%2C%20PATHS%2C%20SURVEY%2C%20UNIT_KEY%2C%20attempt%2C%20check_cases%2C%20waiting_for%0A%0A%20%20%20%20BATCH%20%3D%20PATHS.batch2%20%20%23%20the%20capstone's%20batch%3A%20data%2Fbatch2%2F%2C%20regions%2002%20and%2011%0A%20%20%20%20CAPSTONE%20%3D%20PATHS.root%20%2F%20%22capstone%22%20%20%23%20where%20each%20group's%20plan%2C%20script%20and%20outputs%20go%0A%20%20%20%20REVIEW_DEPTH%20%3D%20100%20%20%23%20the%20households%20the%20supervisors%20can%20revisit%20before%20the%20batch%20closes%0A%20%20%20%20RECALL_PERCENT%20%3D%2080%20%20%23%20the%20share%20of%20the%20planted%20defects%2C%20in%20percent%2C%20a%20review%20table%20must%20put%20in%20its%20first%20REVIEW_DEPTH%20rows%0A%20%20%20%20RECALL_BAR%20%3D%20RECALL_PERCENT%20%2F%20100%0A%20%20%20%20SHEETS%20%3D%20%5B%22Estimates%22%2C%20%22Rule%20failures%22%2C%20%22Orphans%22%2C%20%22Notes%22%5D%20%20%23%20the%20workbook's%20sheets%2C%20in%20this%20order%0A%20%20%20%20%23%20The%20batches%20the%20checks%20feed%20your%20functions%20are%20built%20once%20per%20session%2C%20in%20a%20folder%20of%20their%20own%20that%20is%20removed%0A%20%20%20%20%23%20when%20the%20notebook%20stops.%0A%20%20%20%20CHECK_BATCHES%20%3D%20Path(tempfile.mkdtemp(prefix%3D%22capstone-batches-%22))%0A%20%20%20%20atexit.register(shutil.rmtree%2C%20CHECK_BATCHES%2C%20ignore_errors%3DTrue)%0A%0A%0A%40app.function%0Adef%20group_slug(group%3A%20str)%20-%3E%20str%3A%0A%20%20%20%20%22%22%22A%20group's%20name%20as%20a%20file%20name%3A%20lower%20case%2C%20words%20joined%20by%20hyphens.%22%22%22%0A%20%20%20%20return%20re.sub(r%22%5B%5Ea-z0-9%5D%2B%22%2C%20%22-%22%2C%20group.lower()).strip(%22-%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%20Capstone%0A%0A%20%20%20%20**A%20new%20batch%20arrives%2C%20and%20nothing%20is%20built%20for%20it%20yet.%20Your%20group%20builds%20it.**%0A%0A%20%20%20%20The%20capstone%20is%20done%20in%20groups%3A%20your%20table%20is%20your%20group.%20The%20second%20batch%20of%20the%20Survey%20of%20Fishing%20Households%202025%20has%20come%20in%2C%20from%20two%20regions%20the%20first%20batch%20did%20not%20cover.%20Your%20group%20takes%20it%20from%20its%20two%20databases%20to%20the%20tables%20a%20director%20publishes%20and%20the%20households%20a%20supervisor%20revisits%2C%20with%20code%20the%20group%20writes%20itself.%20No%20cell%20in%20this%20notebook%20builds%20any%20part%20of%20it%20for%20you%3A%20every%20function%20below%20is%20empty%2C%20and%20its%20check%20reads%20only%20what%20your%20function%20gives%20back%2C%20so%20any%20code%20that%20gives%20the%20right%20outputs%20passes.%0A%0A%20%20%20%20Everything%20you%20need%20you%20have%20used%20in%20the%20course%3A%20reading%20regional%20databases%20into%20one%20flat%20table%20(module%202)%2C%20matching%20keys%20(module%203)%2C%20edit%20rules%20(module%204)%2C%20recoding%20(module%205)%2C%20design-based%20estimates%20(module%206)%2C%20workbooks%20and%20a%20re-runnable%20script%20(module%207)%2C%20and%20finding%20anomalies%20(modules%208%20and%209).%20What%20is%20new%20is%20that%20nobody%20tells%20you%20where%20the%20problems%20are.%0A%0A%20%20%20%20%7C%20tier%20%7C%20what%20your%20group%20produces%20%7C%20what%20makes%20it%20done%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20**Baseline**%20%7C%20the%20flat%20table%2C%20the%20matching%20report%2C%20the%20rules%20report%20and%20the%20domain%20table%20(outputs%201%20to%204)%20%7C%20their%20four%20checks%20green%20(Exercises%20K1.1%20to%20K1.4)%20%7C%0A%20%20%20%20%7C%20**Full**%20%7C%20the%20workbook%20and%20the%20ranked%20review%20table%20(outputs%205%20and%206)%2C%20and%20one%20script%20that%20writes%20all%20six%20%7C%20their%20two%20checks%20green%20(K1.5%2C%20K1.6)%2C%20and%20the%20acceptance%20table%20at%20the%20end%20of%20section%202%20green%20for%20the%20files%20your%20script%20wrote%20%7C%0A%20%20%20%20%7C%20**Own%20data**%20%7C%20the%20same%20pipeline%20on%20tables%20your%20own%20institution%20holds%20%7C%20in%20the%20room%20only%2C%20under%20the%20rules%20at%20the%20end%20of%20section%202%3B%20checks%20your%20group%20writes%20%7C%0A%0A%20%20%20%20The%20timings%20are%20your%20facilitator's%3B%20on%20your%20own%2C%20give%20the%20plan%20an%20hour%20and%20the%20build%20two%20days.%0A%0A%20%20%20%20Type%20your%20group's%20name%3A%20the%20plan%2C%20the%20checklist%20and%20the%20acceptance%20table%20below%20use%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%20group_name%20%3D%20mo.ui.text(label%3D%22Your%20group's%20name%20(your%20table)%3A%22%2C%20placeholder%3D%22like%20Table%203%22%2C%20full_width%3DTrue)%0A%20%20%20%20group_name%0A%20%20%20%20return%20(group_name%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%201%20%C2%B7%20The%20problem%20and%20the%20plan%0A%0A%20%20%20%20%23%23%23%20The%20problem%0A%0A%20%20%20%20**The%20users.**%20The%20survey's%20second%20batch%20has%20come%20in%20from%20Region%20II%20(Cagayan%20Valley%3A%20Cagayan%20and%20Isabela)%20and%20Region%20XI%20(Davao%20Region%3A%20Davao%20del%20Sur%20and%20Davao%20Oriental).%20Two%20people%20are%20waiting%20on%20it.%20The%20**director**%20publishes%20the%20number%20of%20fishing%20units%20in%20each%20province%2C%20small-scale%20and%20large-scale%2C%20and%20will%20not%20sign%20a%20figure%20without%20its%20standard%20error%20and%20coefficient%20of%20variation%20(CV).%20The%20**field%20supervisors**%20can%20revisit%20a%20hundred%20households%20before%20the%20batch%20closes%2C%20and%20want%20to%20know%20which%20hundred.%0A%0A%20%20%20%20**The%20data**%20is%20in%20%60data%2Fbatch2%2F%60%2C%20kept%20as%20the%20first%20batch%20was%3A%0A%0A%20%20%20%20%7C%20file%20%7C%20what%20it%20is%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60sfh2025_r02.duckdb%60%2C%20%60sfh2025_r11.duckdb%60%20%7C%20one%20database%20per%20region%2C%20with%20the%20same%20ten%20tables%20as%20the%20first%20batch's%20(%60household%60%2C%20%60selection%60%2C%20%60member%60%2C%20%60fishing_unit%60%20and%20the%20unit's%20six%20child%20tables)%20%7C%0A%20%20%20%20%7C%20%60stratum_counts.csv%60%20%7C%20each%20province's%20strata%3A%20how%20many%20enumeration%20areas%20the%20frame%20holds%2C%20%60eas_in_frame%60%20(in%20stratum%20F%2C%20the%20register's%20large%20units%2C%20each%20its%20own%20sampling%20unit)%2C%20and%20how%20many%20were%20drawn%2C%20%60eas_sampled%60%20%7C%0A%0A%20%20%20%20The%20codebook%2C%20the%20rules%20in%20words%20(%60codebook_rules.csv%60)%20and%20the%20code%20lists%20(%60codes%2F%60)%20in%20%60data%2F%60%20are%20the%20first%20batch's%2C%20and%20they%20hold%20for%20the%20second%3A%20it%20is%20the%20same%20questionnaire.%20%60course.table(name%2C%20folder%3DBATCH)%60%20reads%20one%20table%20of%20the%20batch%20with%20the%20removed%20households%20and%20voided%20rows%20left%20out%2C%20the%20way%20module%202%20builds%20it%3B%20use%20it%2C%20or%20write%20the%20reading%20yourself.%20%60BATCH%60%20is%20set%20at%20the%20top%20of%20this%20notebook%20to%20%60data%2Fbatch2%2F%60.%0A%0A%20%20%20%20**What%20is%20different.**%20The%20second%20batch%20has%20defects%20in%20other%20households%20than%20the%20first%2C%20and%20**at%20least%20two%20kinds%20that%20no%20module%20planted**.%20A%20group%20that%20reruns%20only%20what%20the%20modules%20did%20will%20miss%20them.%20Looking%20at%20the%20data%20before%20trusting%20it%20is%20part%20of%20the%20work%3A%20frequency%20tables%2C%20ranges%2C%20a%20few%20rows%20read%20by%20eye.%0A%0A%20%20%20%20**There%20is%20no%20list%20of%20planted%20defects%20in%20your%20folder.**%20The%20course%20knows%20which%20households%20carry%20a%20planted%20defect%2C%20and%20the%20review%20table's%20check%20counts%20how%20many%20of%20them%20your%20table%20finds%20without%20naming%20them%20(Exercise%20K1.6%20says%20how).%0A%0A%20%20%20%20%23%23%23%20What%20your%20group%20delivers%0A%0A%20%20%20%20Six%20outputs%2C%20each%20one%20a%20function%20of%20the%20batch's%20folder.%20The%20checks%20feed%20each%20function%20three%20batches%3A%20a%20**small%20batch**%20cut%20from%20this%20one%2C%20with%20a%20trap%20of%20every%20kind%20the%20modules%20taught%20planted%20in%20it%3B%20**region%2011%20alone**%3B%20and%20**the%20batch**%20itself.%20A%20function%20that%20hard-codes%20the%20batch's%20regions%2C%20files%20or%20figures%20fails%20the%20first%20two.%0A%0A%20%20%20%20%7C%20output%20%7C%20tier%20%7C%20exercise%20%7C%20what%20the%20check%20reads%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%20%C2%B7%20the%20flat%20table%3A%20one%20row%20per%20fishing%20unit%20%7C%20baseline%20%7C%20K1.1%20%7C%20its%20row%20count%2C%20and%20every%20unit's%20key%2C%20area%20codes%2C%20scale%2C%20stratum%2C%20area%20and%20weight%20%7C%0A%20%20%20%20%7C%202%20%C2%B7%20the%20matching%20report%3A%20units%20and%20catch%20rows%20with%20no%20match%20%7C%20baseline%20%7C%20K1.2%20%7C%20the%20unmatched%20keys%2C%20both%20ways%20%7C%0A%20%20%20%20%7C%203%20%C2%B7%20the%20rules%20report%3A%20which%20households%20fail%20which%20rule%20%7C%20baseline%20%7C%20K1.3%20%7C%20every%20rule%20and%20household%20pair%20%7C%0A%20%20%20%20%7C%204%20%C2%B7%20the%20domain%20table%3A%20fishing%20units%20by%20province%20and%20scale%2C%20with%20SE%20and%20CV%20%7C%20baseline%20%7C%20K1.4%20%7C%20every%20total%20and%20SE%20to%20four%20significant%20figures%2C%20every%20CV%20to%20three%20%7C%0A%20%20%20%20%7C%205%20%C2%B7%20the%20workbook%20the%20director%20reads%20%7C%20full%20%7C%20K1.5%20%7C%20its%20sheets%2C%20and%20the%20tables%20on%20them%20%7C%0A%20%20%20%20%7C%206%20%C2%B7%20the%20review%20table%3A%20every%20household%2C%20ranked%20for%20revisiting%20%7C%20full%20%7C%20K1.6%20%7C%20one%20row%20per%20household%2C%20its%20ranks%2C%20and%20how%20many%20planted%20defects%20are%20in%20its%20first%20100%20rows%20%7C%0A%0A%20%20%20%20For%20the%20full%20tier%2C%20one%20script%20writes%20all%20six%20as%20files%3B%20the%20acceptance%20table%20at%20the%20end%20of%20section%202%20reads%20them%20with%20the%20same%20checks.%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%20Your%20plan%0A%0A%20%20%20%20Before%20anyone%20writes%20code%2C%20answer%20these%20as%20a%20group.%20The%20module%20exercises%20gave%20you%20steps%3B%20these%20give%20you%20none%2C%20because%20deciding%20the%20steps%20is%20the%20work.%20Twenty%20minutes%20here%20saves%20an%20afternoon%3A%20most%20groups%20that%20run%20out%20of%20time%20built%20output%204%20on%20a%20flat%20table%20nobody%20had%20checked.%0A%0A%20%20%20%20The%20plan%20is%20saved%20as%20%60capstone%2F%3Cgroup%3E-plan.md%60%2C%20a%20text%20file.%20It%20is%20also%20your%20demo's%20script.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20plan_questions()%20-%3E%20list%5Btuple%5Bstr%2C%20str%5D%5D%3A%0A%20%20%20%20%22%22%22The%20planning%20template%3A%20each%20question's%20key%20and%20its%20text.%22%22%22%0A%20%20%20%20return%20%5B%0A%20%20%20%20%20%20%20%20(%22order%22%2C%20%22**The%20order.**%20Which%20output%20do%20you%20build%20first%2C%20and%20which%20outputs%20does%20each%20one%20read%3F%20Draw%20the%20chain%20from%20the%20databases%20to%20the%20review%20table.%22)%2C%0A%20%20%20%20%20%20%20%20(%22who%22%2C%20%22**Who%20does%20what.**%20Who%20owns%20which%20output%2C%20who%20reads%20the%20data%20by%20eye%2C%20who%20keeps%20the%20plan%20and%20the%20demo.%22)%2C%0A%20%20%20%20%20%20%20%20(%22look%22%2C%20%22**Looking%20first.**%20Before%20you%20trust%20the%20batch%3A%20which%20tables%2C%20columns%20and%20counts%20will%20you%20look%20at%2C%20and%20what%20would%20surprise%20you%3F%20The%20modules'%20traps%20are%20a%20start%2C%20not%20the%20list.%22)%2C%0A%20%20%20%20%20%20%20%20(%22right%22%2C%20%22**Right%20before%20green.**%20For%20each%20output%2C%20how%20will%20you%20know%20a%20number%20is%20right%20before%20the%20check%20says%20so%3A%20a%20count%20by%20hand%2C%20a%20total%20that%20must%20match%20another%2C%20a%20household%20read%20row%20by%20row.%22)%2C%0A%20%20%20%20%20%20%20%20(%22rank%22%2C%20%22**The%20ranking.**%20What%20will%20your%20review%20table%20rank%20households%20on%2C%20and%20why%20should%20a%20supervisor%20trust%20its%20first%20100%20rows%3F%22)%2C%0A%20%20%20%20%20%20%20%20(%22demo%22%2C%20%22**The%20demo.**%20What%20will%20you%20show%20working%2C%20and%20which%20number%20will%20you%20point%20at%3F%22)%2C%0A%20%20%20%20%5D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(group_name)%3A%0A%20%20%20%20_saved%20%3D%20(CAPSTONE%20%2F%20f%22%7Bgroup_slug(group_name.value)%7D-plan.md%22)%20if%20group_slug(group_name.value)%20else%20None%0A%20%20%20%20_text%20%3D%20_saved.read_text(encoding%3D%22utf-8%22)%20if%20_saved%20and%20_saved.exists()%20else%20%22%22%0A%0A%20%20%20%20def%20_answer(key%3A%20str)%20-%3E%20str%3A%0A%20%20%20%20%20%20%20%20found%20%3D%20re.search(rf%22%3C!--%20%7Bkey%7D%20--%3E%5Cn(.*%3F)%5Cn%3C!--%20%2F%7Bkey%7D%20--%3E%22%2C%20_text%2C%20flags%3Dre.S)%0A%20%20%20%20%20%20%20%20return%20found.group(1).strip()%20if%20found%20else%20%22%22%0A%0A%20%20%20%20plan_form%20%3D%20mo.ui.dictionary(%7Bkey%3A%20mo.ui.text_area(value%3D_answer(key)%2C%20label%3Dquestion%2C%20full_width%3DTrue%2C%20rows%3D3)%20for%20key%2C%20question%20in%20plan_questions()%7D)%0A%20%20%20%20save_plan%20%3D%20mo.ui.run_button(label%3D%22Save%20the%20plan%22)%0A%20%20%20%20mo.vstack(%5Bplan_form%2C%20save_plan%5D)%0A%20%20%20%20return%20plan_form%2C%20save_plan%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(group_name%2C%20plan_form%2C%20save_plan)%3A%0A%20%20%20%20mo.stop(not%20save_plan.value)%0A%20%20%20%20_slug%20%3D%20group_slug(group_name.value)%0A%20%20%20%20if%20not%20_slug%3A%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(%22Not%20saved%20yet%3A%20type%20your%20group's%20name%20at%20the%20top.%22)%2C%20kind%3D%22warn%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20CAPSTONE.mkdir(exist_ok%3DTrue)%0A%20%20%20%20%20%20%20%20_path%20%3D%20CAPSTONE%20%2F%20f%22%7B_slug%7D-plan.md%22%0A%20%20%20%20%20%20%20%20_body%20%3D%20%5Bf%22%23%20Capstone%20plan%3A%20%7Bgroup_name.value.strip()%7D%22%2C%20f%22Saved%20%7Bdate.today().isoformat()%7D.%22%5D%0A%20%20%20%20%20%20%20%20_body%20%2B%3D%20%5Bf%22%7Bquestion%7D%5Cn%5Cn%3C!--%20%7Bkey%7D%20--%3E%5Cn%7Bplan_form.value%5Bkey%5D.strip()%7D%5Cn%3C!--%20%2F%7Bkey%7D%20--%3E%22%20for%20key%2C%20question%20in%20plan_questions()%5D%0A%20%20%20%20%20%20%20%20_path.write_text(%22%5Cn%5Cn%22.join(_body)%20%2B%20%22%5Cn%22%2C%20encoding%3D%22utf-8%22)%0A%20%20%20%20%20%20%20%20_out%20%3D%20mo.callout(mo.md(f%22Saved%3A%20%60%7B_path.relative_to(PATHS.root).as_posix()%7D%60.%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20_out%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202%20%C2%B7%20Build%20it%2C%20in%20groups%0A%0A%20%20%20%20**Write%20your%20code%20in%20this%20notebook.**%20Each%20output%20below%20is%20a%20function%20with%20an%20empty%20body%3A%20delete%20its%20%60raise%20NotImplementedError%60%20line%20and%20write%20the%20body.%20Add%20cells%20of%20your%20own%20anywhere%20to%20look%20at%20the%20data%20and%20try%20things%3B%20a%20helper%20function%20your%20outputs%20share%20goes%20in%20a%20cell%20of%20its%20own%2C%20defined%20with%20%60def%60%20at%20the%20top%20level%20of%20the%20cell.%20One%20person%20types%2C%20the%20others%20read%20the%20data%2C%20count%20by%20hand%20and%20look%20things%20up%3B%20swap%20often.%0A%0A%20%20%20%20**The%20order%20that%20works%3A**%0A%0A%20%20%20%201.%20**Look%20at%20the%20batch**%20before%20writing%20an%20output%3A%20each%20table's%20row%20counts%20by%20region%2C%20the%20frequencies%20of%20its%20codes%2C%20the%20ranges%20of%20its%20numbers.%20Note%20anything%20the%20modules%20never%20showed%20you.%0A%20%20%20%202.%20**Build%20outputs%201%20to%204%20in%20order**%2C%20each%20one%20checked%20before%20the%20next%3A%20output%204%20is%20only%20as%20good%20as%20output%201.%20When%20a%20check%20is%20red%2C%20its%20message%20names%20the%20first%20batch%20your%20function%20got%20wrong%20and%20what%20it%20gave%20back%3B%20the%20common%20causes%20below%20each%20check%20say%20where%20to%20look.%0A%20%20%20%203.%20**Then%20outputs%205%20and%206**%2C%20then%20the%20script.%0A%0A%20%20%20%20When%20something%20goes%20red%2C%20read%20the%20error's%20last%20line%20first%3A%20it%20names%20what%20went%20wrong%2C%20and%20usually%20where.%0A%0A%20%20%20%20**What%20the%20checks%20feed%20your%20functions.**%20The%20*small%20batch*%20is%20cut%20from%20the%20capstone's%20batch%3A%20a%20few%20households%20of%20one%20province%20in%20each%20region.%20In%20region%2002%20a%20household%20is%20deleted%20in%20the%20capture%20system%2C%20one%20is%20voided%20in%20editing%20and%20one%20is%20excluded%20by%20the%20sampling%20side%3B%20a%20catch%20row%20is%20voided%2C%20a%20unit%20and%20a%20catch%20row%20are%20entered%20twice%2C%20a%20catch%20row's%20key%20differs%20and%20a%20unit's%20catch%20is%20gone.%20Between%20the%20two%20regions%2C%20every%20rule%20of%20Exercise%20K1.3%20fails%20at%20least%20once.%20The%20small%20batch's%20region%2011%20stores%20its%20area%20codes%20as%20text%2C%20as%20region%2004%20does%20in%20the%20first%20batch%2C%20and%20reuses%20region%2002's%20household%20ids.%20*Region%2011%20alone*%20is%20the%20real%20region%20in%20a%20folder%20of%20its%20own.%20*The%20batch*%20is%20%60data%2Fbatch2%2F%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20small_batch(folder%3A%20Path)%20-%3E%20Path%3A%0A%20%20%20%20%22%22%22The%20small%20batch%20the%20checks%20use%3A%20in%20each%20region's%20first%20province%2C%20three%20households%20of%20stratum%20F%20and%20the%20first%0A%20%20%20%20households%20of%20two%20areas%20in%20each%20of%20strata%20A%2C%20B%20(two%20each)%20and%20C%20(three%20each)%2C%20cut%20from%20the%20capstone's%20batch%2C%20with%0A%20%20%20%20every%20removal%20mark%20cleared.%20Then%2C%20so%20that%20every%20check%20has%20something%20to%20catch%3A%20in%20region%2002%20a%20household%20deleted%20in%0A%20%20%20%20the%20capture%20system%2C%20one%20voided%20in%20editing%20and%20one%20excluded%20by%20the%20sampling%20side%20(all%20in%20stratum%20C)%2C%20a%20voided%20catch%0A%20%20%20%20row%2C%20a%20unit%20and%20a%20catch%20row%20entered%20twice%20(U1%2C%20C4)%2C%20a%20catch%20row%20whose%20unit%20number%20differs%2C%20a%20unit%20whose%20catch%20is%0A%20%20%20%20gone%2C%20failures%20of%20M1%2C%20M2%2C%20M5%2C%20H1%2C%20B1%20and%20C1%2C%20and%20a%20boat%20measured%20in%20inches%20under%20the%20code%20for%20feet%20(U3)%3B%20in%20region%0A%20%20%20%2011%20failures%20of%20M3%2C%20M4%2C%20M6%2C%20U2%20to%20U8%2C%20B2%20to%20B5%20and%20W1%2C%20its%20area%20codes%20stored%20as%20zero-padded%20text%2C%20and%20its%20household%0A%20%20%20%20ids%20renumbered%20to%20region%2002's.%22%22%22%0A%20%20%20%20tables%20%3D%20%5B%22household%22%2C%20%22selection%22%2C%20%22member%22%2C%20%22fishing_unit%22%2C%20%22crew%22%2C%20%22boat%22%2C%20%22gear%22%2C%20%22catch%22%2C%20%22landing%22%2C%20%22assistance%22%5D%0A%20%20%20%20folder.mkdir(parents%3DTrue%2C%20exist_ok%3DTrue)%0A%20%20%20%20shutil.copy(BATCH%20%2F%20%22stratum_counts.csv%22%2C%20folder%20%2F%20%22stratum_counts.csv%22)%0A%20%20%20%20first_ids%3A%20list%5Bint%5D%20%3D%20%5B%5D%0A%20%20%20%20for%20reg%20in%20(%2202%22%2C%20%2211%22)%3A%0A%20%20%20%20%20%20%20%20target%20%3D%20folder%20%2F%20f%22%7BSURVEY%7D_r%7Breg%7D.duckdb%22%0A%20%20%20%20%20%20%20%20target.unlink(missing_ok%3DTrue)%0A%20%20%20%20%20%20%20%20with%20duckdb.connect(str(target))%20as%20con%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20con.execute(f%22ATTACH%20'%7B(BATCH%20%2F%20f'%7BSURVEY%7D_r%7Breg%7D.duckdb').as_posix()%7D'%20AS%20source%20(READ_ONLY)%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20homes%20%3D%20con.execute(%22SELECT%20s.hh_id%20AS%20id%2C%20s.sel_stratum%20AS%20stratum%2C%20s.sel_ea%20AS%20ea%2C%20h.province_code%20AS%20prov%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%20%20%20%20%20%20%20%20%22FROM%20source.selection%20s%20JOIN%20source.household%20h%20USING%20(hh_id)%20ORDER%20BY%20id%22).df()%0A%20%20%20%20%20%20%20%20%20%20%20%20homes%20%3D%20homes%5Bhomes%5B%22prov%22%5D%20%3D%3D%20homes%5B%22prov%22%5D.min()%5D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20def%20first(stratum%3A%20str%2C%20areas%3A%20int%2C%20each%3A%20int)%20-%3E%20list%5Bint%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20rows%20%3D%20homes%5Bhomes%5B%22stratum%22%5D%20%3D%3D%20stratum%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20%5Bint(i)%20for%20ea%20in%20sorted(rows%5B%22ea%22%5D.unique())%5B%3Aareas%5D%20for%20i%20in%20rows.loc%5Brows%5B%22ea%22%5D%20%3D%3D%20ea%2C%20%22id%22%5D.head(each)%5D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20f%20%3D%20%5Bint(i)%20for%20i%20in%20homes.loc%5Bhomes%5B%22stratum%22%5D%20%3D%3D%20%22F%22%2C%20%22id%22%5D.head(3)%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20a%2C%20b%2C%20c%20%3D%20first(%22A%22%2C%202%2C%202)%2C%20first(%22B%22%2C%202%2C%202)%2C%20first(%22C%22%2C%202%2C%203)%0A%20%20%20%20%20%20%20%20%20%20%20%20ids%20%3D%20f%20%2B%20a%20%2B%20b%20%2B%20c%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20name%20in%20tables%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(f%22CREATE%20TABLE%20%7Bname%7D%20AS%20SELECT%20*%20FROM%20source.%7Bname%7D%20WHERE%20hh_id%20IN%20(%7B'%2C%20'.join(map(str%2C%20ids))%7D)%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22DETACH%20source%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Start%20clean%3A%20no%20household%20removed%20and%20no%20row%20voided%2C%20whatever%20the%20batch%20says%0A%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20household%20SET%20hh_deleted_on%20%3D%20NULL%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20selection%20SET%20sel_excluded%20%3D%20NULL%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20name%20in%20tables%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(f%22UPDATE%20%7Bname%7D%20SET%20row_void%20%3D%20NULL%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20used%3A%20set%5Bint%5D%20%3D%20set()%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20def%20take(table%3A%20str%2C%20where%3A%20str%20%3D%20%22TRUE%22)%20-%3E%20tuple%5Bint%2C%20int%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22The%20first%20row%20of%20%60table%60%20that%20%60where%60%20accepts%2C%20in%20a%20household%20no%20other%20trap%20of%20this%20region%20has%20used%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20its%20row%20id%20and%20its%20household.%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20row%2C%20hh%20in%20con.execute(f%22SELECT%20row_id%2C%20hh_id%20FROM%20%7Btable%7D%20WHERE%20%7Bwhere%7D%20ORDER%20BY%20hh_id%2C%20row_id%22).fetchall()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20hh%20not%20in%20used%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20used.add(hh)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20row%2C%20hh%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20raise%20ValueError(f%22the%20small%20batch%20of%20region%20%7Breg%7D%20has%20no%20household%20left%20for%20a%20row%20of%20%7Btable%7D%20where%20%7Bwhere%7D%22)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20def%20copy_row(table%3A%20str%2C%20row%3A%20int)%20-%3E%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(f%22INSERT%20INTO%20%7Btable%7D%20SELECT%20*%20REPLACE%20((SELECT%20max(row_id)%20%2B%201%20FROM%20%7Btable%7D)%20AS%20row_id)%20FROM%20%7Btable%7D%20WHERE%20row_id%20%3D%20%3F%22%2C%20%5Brow%5D)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20unit_of_boat%20%3D%20(%22SELECT%20b.row_id%20FROM%20boat%20b%20JOIN%20fishing_unit%20u%20ON%20u.hh_id%20%3D%20b.hh_id%20AND%20u.operator_line%20%3D%20b.boat_operator_line%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%20%20%20%20%22AND%20u.unit_no%20%3D%20b.boat_unit_no%20WHERE%20u.unit_scale%20%3D%201%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20reg%20%3D%3D%20%2202%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20used.update(%5Bc%5B0%5D%2C%20c%5B1%5D%2C%20c%5B3%5D%5D)%20%20%23%20the%20three%20removals%2C%20in%20two%20areas%20of%20stratum%20C%2C%20so%20each%20area%20keeps%20a%20household%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20household%20SET%20hh_deleted_on%20%3D%20DATE%20'2026-06-02'%20WHERE%20hh_id%20%3D%20%3F%22%2C%20%5Bc%5B0%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20household%20SET%20row_void%20%3D%201%20WHERE%20hh_id%20%3D%20%3F%22%2C%20%5Bc%5B1%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20selection%20SET%20sel_excluded%20%3D%201%20WHERE%20hh_id%20%3D%20%3F%22%2C%20%5Bc%5B3%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20copy_row(%22fishing_unit%22%2C%20take(%22fishing_unit%22)%5B0%5D)%20%20%23%20a%20unit%20entered%20twice%3A%20U1%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20catch%20SET%20catch_unit_no%20%3D%20catch_unit_no%20%2B%203%20WHERE%20row_id%20%3D%20%3F%22%2C%20%5Btake(%22catch%22)%5B0%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22DELETE%20FROM%20catch%20WHERE%20hh_id%20%3D%20%3F%22%2C%20%5Btake(%22catch%22)%5B1%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20catch%20SET%20row_void%20%3D%201%20WHERE%20row_id%20%3D%20%3F%22%2C%20%5Btake(%22catch%22)%5B0%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20copy_row(%22catch%22%2C%20take(%22catch%22)%5B0%5D)%20%20%23%20a%20catch%20row%20entered%20twice%3A%20C4%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20member%20SET%20mem_sex%20%3D%209%20WHERE%20row_id%20%3D%20%3F%22%2C%20%5Btake(%22member%22)%5B0%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20member%20SET%20mem_age%20%3D%20150%20WHERE%20row_id%20%3D%20%3F%22%2C%20%5Btake(%22member%22)%5B0%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20member%20SET%20mem_education%20%3D%2099%20WHERE%20row_id%20%3D%20%3F%22%2C%20%5Btake(%22member%22%2C%20%22mem_age%20%3E%3D%2018%22)%5B0%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20household%20SET%20hh_roster_members%20%3D%20hh_roster_members%20%2B%201%20WHERE%20hh_id%20%3D%20%3F%22%2C%20%5Btake(%22household%22)%5B1%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20boat%20SET%20boat_hull%20%3D%207%20WHERE%20row_id%20%3D%20%3F%22%2C%20%5Btake(%22boat%22)%5B0%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20catch%20SET%20catch_species%20%3D%20NULL%20WHERE%20row_id%20%3D%20%3F%22%2C%20%5Btake(%22catch%22)%5B0%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20A%20boat%20of%20a%20small-scale%20unit%2C%2030%20by%204%20by%202.5%20feet%2C%20written%20in%20inches%20under%20the%20code%20for%20feet%3A%20its%20tonnage%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20computed%20and%20stored%20from%20those%20numbers%2C%20puts%20it%20far%20over%205%20gross%20tons%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20boat%20SET%20boat_measured_in%20%3D%203%2C%20boat_length%20%3D%20360%2C%20boat_beam%20%3D%2048%2C%20boat_depth%20%3D%2030%2C%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%20%20%20%20%22boat_tonnage%20%3D%20round(360%20*%2048%20*%2030%20*%200.006%2C%202)%2C%20boat_tonnage_reported%20%3D%20NULL%20WHERE%20row_id%20%3D%20%3F%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5Btake(%22boat%22%2C%20f%22row_id%20IN%20(%7Bunit_of_boat%7D)%22)%5B0%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20first_ids%20%3D%20ids%0A%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20member%20SET%20mem_age%20%3D%208%20WHERE%20row_id%20%3D%20%3F%22%2C%20%5Btake(%22member%22%2C%20%22mem_role_operator%20%3D%201%22)%5B0%5D%5D)%20%20%23%20M3%20and%20M4%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20member%20SET%20mem_units_run%20%3D%20mem_units_run%20%2B%201%20WHERE%20row_id%20%3D%20%3F%22%2C%20%5Btake(%22member%22%2C%20%22mem_units_run%20IS%20NOT%20NULL%22)%5B0%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20member%20SET%20mem_role_crew%20%3D%200%2C%20mem_role_hired%20%3D%200%2C%20mem_role_shore%20%3D%200%20WHERE%20row_id%20%3D%20%3F%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5Btake(%22member%22%2C%20%22mem_fished%20%3D%201%20AND%20mem_role_operator%20%3D%200%22)%5B0%5D%5D)%20%20%23%20M6%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20with_boats%20%3D%20%22EXISTS%20(SELECT%201%20FROM%20boat%20b%20WHERE%20b.hh_id%20%3D%20fishing_unit.hh_id%20AND%20b.boat_operator_line%20%3D%20fishing_unit.operator_line%20AND%20b.boat_unit_no%20%3D%20fishing_unit.unit_no)%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20fishing_unit%20SET%20unit_uses_boat%20%3D%20NULL%20WHERE%20row_id%20%3D%20%3F%22%2C%20%5Btake(%22fishing_unit%22%2C%20with_boats)%5B0%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20fishing_unit%20SET%20unit_uses_boat%20%3D%200%20WHERE%20row_id%20%3D%20%3F%22%2C%20%5Btake(%22fishing_unit%22%2C%20with_boats)%5B0%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20fishing_unit%20SET%20unit_hired_crew%20%3D%201%20WHERE%20row_id%20%3D%20%3F%22%2C%20%5Btake(%22fishing_unit%22%2C%20%22unit_hired_crew%20%3D%200%22)%5B0%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20fishing_unit%20SET%20unit_boats%20%3D%20unit_boats%20%2B%201%20WHERE%20row_id%20%3D%20%3F%22%2C%20%5Btake(%22fishing_unit%22%2C%20with_boats)%5B0%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20fishing_unit%20SET%20unit_months_active%20%3D%2013%20WHERE%20row_id%20%3D%20%3F%22%2C%20%5Btake(%22fishing_unit%22)%5B0%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20An%20outrigger%20boat%20of%20a%20small-scale%20unit%20with%20no%20engine%20and%20over%205%20gross%20tons%3A%20B2%20and%20U3%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20boat%20SET%20boat_hull%20%3D%202%2C%20boat_engine%20%3D%200%2C%20boat_engine_hp%20%3D%20NULL%2C%20boat_measured_in%20%3D%202%2C%20boat_length%20%3D%2012.5%2C%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%20%20%20%20%22boat_beam%20%3D%201.9%2C%20boat_depth%20%3D%201.1%2C%20boat_tonnage%20%3D%20round(12.5%20*%201.9%20*%201.1%20*%200.212%2C%202)%2C%20boat_tonnage_reported%20%3D%20NULL%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%20%20%20%20%22WHERE%20row_id%20%3D%20%3F%22%2C%20%5Btake(%22boat%22%2C%20f%22row_id%20IN%20(%7Bunit_of_boat%7D)%22)%5B0%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20A%20boat%20whose%20stored%20tonnage%20is%20ten%20times%20what%20its%20dimensions%20give%3A%20B3%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20boat%20SET%20boat_hull%20%3D%202%2C%20boat_engine%20%3D%201%2C%20boat_engine_hp%20%3D%209%2C%20boat_measured_in%20%3D%202%2C%20boat_length%20%3D%208.0%2C%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%20%20%20%20%22boat_beam%20%3D%201.0%2C%20boat_depth%20%3D%200.6%2C%20boat_tonnage%20%3D%20round(8.0%20*%201.0%20*%200.6%20*%200.212%20*%2010%2C%202)%2C%20boat_tonnage_reported%20%3D%20NULL%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%20%20%20%20%22WHERE%20row_id%20%3D%20%3F%22%2C%20%5Btake(%22boat%22%2C%20f%22row_id%20IN%20(%7Bunit_of_boat%7D)%22)%5B0%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20boat%20SET%20boat_engine_hp%20%3D%20NULL%20WHERE%20row_id%20%3D%20%3F%22%2C%20%5Btake(%22boat%22%2C%20%22boat_engine%20%3E%200%22)%5B0%5D%5D)%20%20%23%20B4%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_%2C%20shared%20%3D%20take(%22boat%22%2C%20%22hh_id%20IN%20(SELECT%20hh_id%20FROM%20boat%20GROUP%20BY%20hh_id%20HAVING%20count(*)%20%3E%3D%202)%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20boat%20SET%20boat_reg_no%20%3D%20'R11-09999'%20WHERE%20row_id%20IN%20(SELECT%20row_id%20FROM%20boat%20WHERE%20hh_id%20%3D%20%3F%20ORDER%20BY%20row_id%20LIMIT%202)%22%2C%20%5Bshared%5D)%20%20%23%20B5%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22UPDATE%20crew%20SET%20crew_sex%20%3D%20NULL%20WHERE%20row_id%20%3D%20%3F%22%2C%20%5Btake(%22crew%22)%5B0%5D%5D)%20%20%23%20W1%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20Region%2002's%20ids%20in%20order%2C%20then%20new%20ones%20after%20them%20if%20region%2011%20has%20more%20households%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20reused%20%3D%20first_ids%20%2B%20%5Bmax(first_ids)%20%2B%201%20%2B%20k%20for%20k%20in%20range(len(ids)%20-%20len(first_ids))%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20renumber%20%3D%20%22%20%22.join(f%22WHEN%20%7Bold%7D%20THEN%20%7Bnew%7D%22%20for%20old%2C%20new%20in%20zip(ids%2C%20reused))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20name%20in%20tables%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(f%22UPDATE%20%7Bname%7D%20SET%20hh_id%20%3D%20CASE%20hh_id%20%7Brenumber%7D%20END%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22CREATE%20TABLE%20coded%20AS%20SELECT%20*%20REPLACE%20(lpad(CAST(region_code%20AS%20VARCHAR)%2C%202%2C%20'0')%20AS%20region_code%2C%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%20%20%20%20%22lpad(CAST(province_code%20AS%20VARCHAR)%2C%203%2C%20'0')%20AS%20province_code%2C%20lpad(CAST(citymun_code%20AS%20VARCHAR)%2C%202%2C%20'0')%20AS%20citymun_code%2C%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%20%20%20%20%22lpad(CAST(barangay_code%20AS%20VARCHAR)%2C%203%2C%20'0')%20AS%20barangay_code)%20FROM%20household%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22DROP%20TABLE%20household%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20con.execute(%22ALTER%20TABLE%20coded%20RENAME%20TO%20household%22)%0A%20%20%20%20return%20folder%0A%0A%0A%40app.function%0Adef%20check_batches()%20-%3E%20dict%5Bstr%2C%20Path%5D%3A%0A%20%20%20%20%22%22%22The%20three%20batches%20every%20check%20feeds%20your%20functions%2C%20built%20on%20first%20use%3A%20the%20small%20batch%2C%20region%2011%20alone%2C%20and%0A%20%20%20%20the%20capstone's%20batch.%22%22%22%0A%20%20%20%20small%2C%20alone%20%3D%20CHECK_BATCHES%20%2F%20%22small%22%2C%20CHECK_BATCHES%20%2F%20%22region11%22%0A%20%20%20%20if%20not%20(small%20%2F%20f%22%7BSURVEY%7D_r11.duckdb%22).exists()%3A%0A%20%20%20%20%20%20%20%20small_batch(small)%0A%20%20%20%20if%20not%20(alone%20%2F%20f%22%7BSURVEY%7D_r11.duckdb%22).exists()%3A%0A%20%20%20%20%20%20%20%20alone.mkdir(parents%3DTrue%2C%20exist_ok%3DTrue)%0A%20%20%20%20%20%20%20%20shutil.copy(BATCH%20%2F%20f%22%7BSURVEY%7D_r11.duckdb%22%2C%20alone%20%2F%20f%22%7BSURVEY%7D_r11.duckdb%22)%0A%20%20%20%20%20%20%20%20shutil.copy(BATCH%20%2F%20%22stratum_counts.csv%22%2C%20alone%20%2F%20%22stratum_counts.csv%22)%0A%20%20%20%20return%20%7B%22the%20small%20batch%22%3A%20small%2C%20%22region%2011%20alone%22%3A%20alone%2C%20%22the%20batch%22%3A%20BATCH%7D%0A%0A%0A%40app.function%0Adef%20batch_cases(*extra)%20-%3E%20list%5Btuple%5Bstr%2C%20tuple%5D%5D%3A%0A%20%20%20%20%22%22%22The%20check%20cases%3A%20each%20of%20the%20three%20batches%2C%20given%20to%20the%20function%20with%20any%20further%20arguments%20made%20for%20it.%22%22%22%0A%20%20%20%20return%20%5B(name%2C%20(folder%2C%20*(make(name)%20for%20make%20in%20extra)))%20for%20name%2C%20folder%20in%20check_batches().items()%5D%0A%0A%0A%40app.function%0Adef%20as_pandas(table)%3A%0A%20%20%20%20%22%22%22A%20table%20as%20pandas%2C%20whether%20a%20function%20gave%20back%20a%20pandas%20or%20a%20polars%20DataFrame.%22%22%22%0A%20%20%20%20return%20table.to_pandas()%20if%20isinstance(table%2C%20pl.DataFrame)%20else%20table%0A%0A%0A%40app.function%0Adef%20code(value)%20-%3E%20str%20%7C%20None%3A%0A%20%20%20%20%22%22%22A%20code%20as%20the%20checks%20compare%20it%3A%20as%20text%2C%20exactly%20as%20written.%22%22%22%0A%20%20%20%20return%20None%20if%20value%20is%20None%20or%20(not%20isinstance(value%2C%20str)%20and%20pd.isna(value))%20else%20str(value)%0A%0A%0A%40app.function%0Adef%20whole(value)%20-%3E%20int%20%7C%20None%3A%0A%20%20%20%20%22%22%22A%20whole%20number%20as%20the%20checks%20compare%20it.%22%22%22%0A%20%20%20%20return%20None%20if%20value%20is%20None%20or%20pd.isna(value)%20else%20int(value)%0A%0A%0A%40app.function%0Adef%20figures(value%2C%20digits%3A%20int)%20-%3E%20float%20%7C%20None%3A%0A%20%20%20%20%22%22%22A%20number%20to%20so%20many%20significant%20figures%3A%20the%20checks'%20tolerance%20for%20estimates.%22%22%22%0A%20%20%20%20return%20None%20if%20value%20is%20None%20or%20pd.isna(value)%20else%20float(f%22%7Bfloat(value)%3A.%7Bdigits%7Dg%7D%22)%0A%0A%0A%40app.function%0Adef%20missing_columns(table%2C%20columns%3A%20list%5Bstr%5D)%20-%3E%20dict%20%7C%20None%3A%0A%20%20%20%20%22%22%22What%20the%20checks%20say%20when%20a%20table%20lacks%20a%20column%20they%20read.%22%22%22%0A%20%20%20%20missing%20%3D%20%5Bc%20for%20c%20in%20columns%20if%20c%20not%20in%20table.columns%5D%0A%20%20%20%20return%20%7B%22columns%20missing%22%3A%20missing%7D%20if%20missing%20else%20None%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%20K1.1.%20The%20flat%20table%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20every%20other%20output%20reads%20it.%20A%20row%20too%20many%20here%20is%20a%20fishing%20unit%20counted%20twice%20in%20the%20director's%20table%3B%20a%20code%20that%20lost%20its%20zero%20is%20a%20province%20no%20table%20can%20find.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20batch's%20folder%20(%60BATCH%60%2C%20or%20any%20folder%20holding%20%60sfh2025_r*.duckdb%60%20files)%20%E2%86%92%20a%20pandas%20or%20polars%20DataFrame%20with%20**one%20row%20per%20fishing%20unit**%20in%20the%20batch%3A%0A%0A%20%20%20%20-%20the%20households%20deleted%20in%20the%20capture%20system%20(%60hh_deleted_on%60%20filled)%20or%20voided%20in%20editing%20(%60row_void%20%3D%201%60%20in%20%60household%60)%2C%20the%20households%20excluded%20by%20the%20sampling%20side%20(%60sel_excluded%20%3D%201%60%20in%20%60selection%60)%20and%20the%20unit%20rows%20voided%20(%60row_void%20%3D%201%60)%20left%20out%3B%0A%20%20%20%20-%20a%20unit%20entered%20twice%20kept%20once%2C%20so%20the%20unit's%20key%20(%60region_code%60%2C%20%60hh_id%60%2C%20%60operator_line%60%2C%20%60unit_no%60%3A%20%60UNIT_KEY%60)%20never%20repeats%3B%0A%20%20%20%20-%20these%20columns%2C%20others%20welcome%20after%20them%3A%20%60region_code%60%2C%20%60province_code%60%2C%20%60citymun_code%60%2C%20%60barangay_code%60%20as%20zero-padded%20**text**%20(2%2C%203%2C%202%20and%203%20characters)%3B%20%60hh_id%60%3B%20the%20key%20parts%20%60operator_line%60%20and%20%60unit_no%60%3B%20the%20unit's%20scale%20%60unit_scale%60%3B%20and%20its%20household's%20stratum%20%60sel_stratum%60%2C%20enumeration%20area%20%60sel_ea%60%20and%20final%20weight%20%60sel_final_weight%60.%0A%0A%20%20%20%20%7C%20region_code%20%7C%20province_code%20%7C%20citymun_code%20%7C%20barangay_code%20%7C%20hh_id%20%7C%20operator_line%20%7C%20unit_no%20%7C%20unit_scale%20%7C%20sel_stratum%20%7C%20sel_ea%20%7C%20sel_final_weight%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C%3A--%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20%6002%60%20%7C%20%60015%60%20%7C%20%6001%60%20%7C%20%60012%60%20%7C%20%E2%80%A6%20%7C%201%20%7C%201%20%7C%202%20%7C%20%60F%60%20%7C%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20%6011%60%20%7C%20%60024%60%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%201%20%7C%20%60C%60%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%5Bmodule%202%5D(%2F%3Ffile%3D10_flat_table.py)%3A%20one%20DuckDB%20connection%20that%20attaches%20every%20region's%20file%2C%20as%20a%20script%20in%20R%20loops%20over%20regional%20databases%3B%20codes%20kept%20whole%3B%20or%20%60course.table(name%2C%20folder%3Dfolder)%60%20for%20each%20table%2C%20then%20joins%20on%20the%20household%20(module%203)%20and%20%60drop_duplicates%60%20on%20the%20key%20(module%204).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20flat_table(folder%3A%20Path)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20fishing%20unit%20of%20the%20batch%20in%20%60folder%60%2C%20with%20its%20area%20codes%20as%20text%20and%20its%20household's%20design.%22%22%22%0A%20%20%20%20%23%20Exercise%20K1.1%3A%20what%20goes%20in%2C%20what%20comes%20out%20and%20how%20it%20is%20checked%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Your%20group's%20code.%20There%20are%20no%20steps%20here%3A%20your%20plan%20says%20what%20they%20are.%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20K1.1%22)%0A%0A%0A%40app.function%0Adef%20flat_summary(table)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22What%20the%20check%20of%20Exercise%20K1.1%20reads%3A%20the%20row%20count%2C%20and%20every%20unit's%20key%2C%20codes%2C%20scale%2C%20stratum%2C%20area%20and%20weight.%22%22%22%0A%20%20%20%20t%20%3D%20as_pandas(table)%0A%20%20%20%20columns%20%3D%20%5B%22region_code%22%2C%20%22province_code%22%2C%20%22citymun_code%22%2C%20%22barangay_code%22%2C%20%22hh_id%22%2C%20%22operator_line%22%2C%20%22unit_no%22%2C%20%22unit_scale%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22sel_stratum%22%2C%20%22sel_ea%22%2C%20%22sel_final_weight%22%5D%0A%20%20%20%20if%20missing%20%3A%3D%20missing_columns(t%2C%20columns)%3A%0A%20%20%20%20%20%20%20%20return%20missing%0A%20%20%20%20rows%20%3D%20%5B(code(reg)%2C%20code(prov)%2C%20code(citymun)%2C%20code(brgy)%2C%20whole(hh)%2C%20whole(line)%2C%20whole(number)%2C%20whole(scale)%2C%20code(stratum)%2C%20whole(ea)%2C%20figures(weight%2C%208))%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20reg%2C%20prov%2C%20citymun%2C%20brgy%2C%20hh%2C%20line%2C%20number%2C%20scale%2C%20stratum%2C%20ea%2C%20weight%20in%20zip(*(t%5Bc%5D%20for%20c%20in%20columns))%5D%0A%20%20%20%20return%20%7B%22rows%22%3A%20len(t)%2C%20%22units%22%3A%20sorted(rows%2C%20key%3Drepr)%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%20K1.1%22%2C%20flat_table%2C%20batch_cases()%2C%0A%20%20%20%20%20%20%20%20what%3D%22batches%22%2C%20summarize%3Dflat_summary%2C%0A%20%20%20%20%20%20%20%20hint%3D%22In%20the%20small%20batch%2C%20region%2002%20loses%20three%20households%20to%20three%20different%20removal%20marks%20and%20has%20one%20unit%20entered%20twice%3B%20region%2011%20stores%20its%20area%20codes%20as%20text%20and%20reuses%20region%2002's%20household%20ids.%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%20your%20flat%20table%20of%20the%20batch.%0A%20%20%20%20course.try_it(flat_table%2C%20BATCH)%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%20K1.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%20A%20few%20rows%20too%20many%20in%20the%20small%20batch%20%7C%20One%20of%20the%20three%20removal%20marks%20is%20not%20applied%3A%20%60hh_deleted_on%60%20and%20%60row_void%60%20in%20%60household%60%2C%20%60sel_excluded%60%20in%20%60selection%60.%20%7C%20module%202%2C%20section%202%20%7C%0A%20%20%20%20%7C%20One%20row%20too%20many%2C%20and%20a%20key%20that%20repeats%20%7C%20The%20unit%20entered%20twice%20is%20still%20there%20twice.%20%7C%20%60drop_duplicates%60%20on%20the%20whole%20key%20%7C%0A%20%20%20%20%7C%20Rows%20multiplied%2C%20or%20*MergeError*%20with%20%60validate%3D%60%20%7C%20A%20join%20on%20%60hh_id%60%20alone%3A%20in%20the%20small%20batch%20region%2011%20reuses%20region%2002's%20ids.%20%7C%20join%20on%20%60region_code%60%20and%20%60hh_id%60%20%7C%0A%20%20%20%20%7C%20%60region_code%60%20shows%20%602%60%2C%20or%20%60province_code%60%20shows%20%6015%60%20%7C%20The%20codes%20were%20read%20as%20numbers.%20%7C%20zero-padded%20text%3A%20%60str.zfill%60%2C%20or%20%60lpad%60%20in%20SQL%20%7C%0A%20%20%20%20%7C%20Region%2011%20alone%20passes%2C%20the%20small%20batch%20fails%20%7C%20Region%2011%20of%20the%20small%20batch%20stores%20its%20codes%20as%20text%2C%20and%20the%20code%20treats%20them%20as%20numbers.%20%7C%20read%20both%20kinds%20of%20region%20the%20same%20way%20%7C%0A%20%20%20%20%7C%20Every%20unit%20of%20stratum%20F%20is%20missing%20%7C%20A%20filter%20or%20a%20join%20on%20%60sel_ea%60%20dropped%20the%20blank%20areas%3A%20stratum%20F%20has%20no%20area.%20%7C%20keep%20%60sel_ea%60%20blank%20where%20it%20is%20blank%20%7C%0A%20%20%20%20%7C%20%60%7B'columns%20missing'%3A%20%5B...%5D%7D%60%20%7C%20A%20column%20the%20brief%20names%20is%20missing%20or%20renamed.%20%7C%20the%20list%20above%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%20Exercise%20K1.2.%20The%20matching%20report%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20catch%20row%20whose%20key%20matches%20no%20unit%20is%20catch%20that%20no%20unit%20reports%2C%20and%20a%20unit%20with%20no%20catch%20row%20reports%20no%20catch.%20Neither%20is%20visible%20in%20a%20sum%3B%20both%20change%20what%20the%20batch%20says.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20batch's%20folder%20%E2%86%92%20one%20row%20per%20**key%20with%20no%20match%20on%20the%20other%20side**%2C%20between%20the%20units%20(%60fishing_unit%60)%20and%20the%20catch%20(%60catch%60)%2C%20both%20after%20the%20removals%2C%20with%20the%20columns%20%60region_code%60%2C%20%60hh_id%60%2C%20%60operator_line%60%2C%20%60unit_no%60%20(the%20catch's%20key%20parts%20under%20the%20unit's%20names)%20and%20%60side%60%2C%20which%20is%20%60unit%20without%20catch%60%20or%20%60catch%20without%20unit%60.%0A%0A%20%20%20%20%7C%20region_code%20%7C%20hh_id%20%7C%20operator_line%20%7C%20unit_no%20%7C%20side%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%3A--%7C%0A%20%20%20%20%7C%20%6002%60%20%7C%20%E2%80%A6%20%7C%201%20%7C%204%20%7C%20%60catch%20without%20unit%60%20%7C%0A%20%20%20%20%7C%20%6002%60%20%7C%20%E2%80%A6%20%7C%201%20%7C%201%20%7C%20%60unit%20without%20catch%60%20%7C%0A%0A%20%20%20%20A%20key%20listed%20twice%20is%20read%20once.%20Repairing%20the%20keys%20is%20not%20part%20of%20this%20output%3A%20module%203%20did%20that%2C%20and%20the%20batch's%20editors%20will.%0A%0A%20%20%20%20**Tools%3A**%20%5Bmodule%203%5D(%2F%3Ffile%3D20_key_matching.py)%3A%20an%20%60anti_join%60%20in%20R%20runs%20one%20way%20at%20a%20time%3B%20in%20pandas%20one%20%60merge(how%3D%22outer%22%2C%20indicator%3DTrue)%60%20gives%20both%20sides%20at%20once.%20The%20catch's%20key%20parts%20carry%20the%20table's%20prefix%3A%20%60catch_operator_line%60%2C%20%60catch_unit_no%60%20(%60CHILD_PREFIX%60).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20matching_report(folder%3A%20Path)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Every%20unit%20key%20with%20no%20catch%20row%2C%20and%20every%20catch%20key%20with%20no%20unit%2C%20in%20the%20batch%20in%20%60folder%60.%22%22%22%0A%20%20%20%20%23%20Exercise%20K1.2%3A%20what%20goes%20in%2C%20what%20comes%20out%20and%20how%20it%20is%20checked%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Your%20group's%20code.%20There%20are%20no%20steps%20here%3A%20your%20plan%20says%20what%20they%20are.%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20K1.2%22)%0A%0A%0A%40app.function%0Adef%20orphans_summary(table)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22What%20the%20check%20of%20Exercise%20K1.2%20reads%3A%20the%20unmatched%20keys%2C%20each%20once%2C%20with%20their%20side.%22%22%22%0A%20%20%20%20t%20%3D%20as_pandas(table)%0A%20%20%20%20if%20missing%20%3A%3D%20missing_columns(t%2C%20%5B*UNIT_KEY%2C%20%22side%22%5D)%3A%0A%20%20%20%20%20%20%20%20return%20missing%0A%20%20%20%20keys%20%3D%20%7B(code(side)%2C%20code(reg)%2C%20whole(hh)%2C%20whole(line)%2C%20whole(number))%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20reg%2C%20hh%2C%20line%2C%20number%2C%20side%20in%20zip(*(t%5Bc%5D%20for%20c%20in%20%5B*UNIT_KEY%2C%20%22side%22%5D))%7D%0A%20%20%20%20by_side%20%3D%20%7Bside%3A%20sum(1%20for%20k%20in%20keys%20if%20k%5B0%5D%20%3D%3D%20side)%20for%20side%20in%20(%22unit%20without%20catch%22%2C%20%22catch%20without%20unit%22)%7D%0A%20%20%20%20return%20%7B%22keys%2C%20by%20side%22%3A%20by_side%2C%20%22keys%22%3A%20sorted(keys%2C%20key%3Drepr)%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%20K1.2%22%2C%20matching_report%2C%20batch_cases()%2C%0A%20%20%20%20%20%20%20%20what%3D%22batches%22%2C%20summarize%3Dorphans_summary%2C%0A%20%20%20%20%20%20%20%20hint%3D%22In%20the%20small%20batch%2C%20region%2002%20has%20a%20catch%20row%20whose%20unit%20number%20differs%20from%20its%20unit's%2C%20a%20unit%20whose%20catch%20rows%20are%20gone%2C%20and%20a%20voided%20catch%20row.%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%20your%20matching%20report%20of%20the%20batch.%0A%20%20%20%20course.try_it(matching_report%2C%20BATCH)%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%20K1.2%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20*KeyError*%20naming%20%60operator_line%60%20on%20the%20catch%20%7C%20The%20catch's%20key%20parts%20carry%20the%20prefix%20%60catch_%60.%20%7C%20rename%20them%20to%20the%20unit's%20names%20%7C%0A%20%20%20%20%7C%20Every%20catch%20row%20is%20unmatched%20%7C%20The%20key%20parts%20are%20text%20on%20one%20side%20and%20numbers%20on%20the%20other%2C%20or%20%60region_code%60%20is%20%602%60%20on%20one%20side%20and%20%6002%60%20on%20the%20other.%20%7C%20the%20key's%20types%2C%20side%20by%20side%20%7C%0A%20%20%20%20%7C%20Too%20few%20keys%20in%20the%20small%20batch%20%7C%20The%20join%20ignores%20%60region_code%60%2C%20so%20region%2011's%20keys%20find%20matches%20in%20region%2002.%20%7C%20the%20whole%20key%2C%20region%20first%20%7C%0A%20%20%20%20%7C%20A%20unit%20without%20catch%20is%20missing%20%7C%20The%20catch%20was%20read%20with%20%60clean%3DFalse%60%2C%20so%20the%20voided%20catch%20row%20still%20counts%20as%20catch.%20%7C%20the%20removals%20%7C%0A%20%20%20%20%7C%20The%20right%20keys%2C%20but%20the%20check%20is%20red%20%7C%20%60side%60%20is%20not%20written%20exactly%20%60unit%20without%20catch%60%20or%20%60catch%20without%20unit%60.%20%7C%20the%20two%20labels%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%20Exercise%20K1.3.%20The%20rules%20report%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20rules%20are%20what%20the%20questionnaire%20says%20must%20be%20true.%20A%20household%20that%20fails%20one%20goes%20back%20to%20an%20editor%20before%20its%20numbers%20are%20published%2C%20so%20the%20editors%20need%20the%20list%2C%20rule%20by%20rule.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20batch's%20folder%20%E2%86%92%20one%20row%20per%20**rule%20and%20household%20that%20fails%20it**%2C%20with%20the%20columns%20%60rule%60%20(the%20rule's%20id%2C%20as%20%60M1%60%20or%20%60U3%60)%2C%20%60region_code%60%20and%20%60hh_id%60%3B%20add%20what%20helps%20an%20editor.%20Module%204's%20failure%20report%20has%20all%20three%20columns.%20A%20household%20that%20fails%20a%20rule%20on%20two%20rows%20may%20be%20listed%20twice%3A%20the%20check%20reads%20each%20rule%20and%20household%20once.%20The%20rules%20run%20on%20the%20tables%20after%20the%20removals%2C%20before%20the%20flat%20table%20drops%20a%20unit%20entered%20twice.%20C2%20and%20C3%20are%20the%20matching%20report's.%0A%0A%20%20%20%20The%20rules%20are%20those%20of%20%60data%2Fcodebook_rules.csv%60%2C%20made%20exact%20here%2C%20because%20a%20check%20can%20only%20compare%20a%20rule%20that%20says%20what%20happens%20at%20its%20edges.%20%22Blank%20fails%22%20means%20a%20blank%20value%20is%20a%20failure%3B%20a%20child%20table's%20key%20parts%20are%20read%20under%20the%20unit's%20names%2C%20as%20in%20Exercise%20K1.2.%0A%0A%20%20%20%20%7C%20rule%20%7C%20a%20row%20fails%20when%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20M1%20%7C%20the%20member's%20sex%20%60mem_sex%60%20is%20not%201%20or%202%20(blank%20fails)%20%7C%0A%20%20%20%20%7C%20M2%20%7C%20the%20member's%20age%20%60mem_age%60%20is%20blank%2C%20or%20outside%200%20to%20110%20%7C%0A%20%20%20%20%7C%20M3%20%7C%20a%20member%20under%2010%20has%20any%20of%20%60mem_fished%60%2C%20%60mem_role_operator%60%2C%20%60mem_role_crew%60%2C%20%60mem_role_hired%60%2C%20%60mem_role_shore%60%20answered%20%7C%0A%20%20%20%20%7C%20M4%20%7C%20a%20member%20who%20runs%20a%20unit%20(%60mem_role_operator%20%3D%201%60)%20is%20under%2016%20%7C%0A%20%20%20%20%7C%20M5%20%7C%20a%20member%206%20or%20over%20has%20an%20education%20%60mem_education%60%20that%20is%20not%20a%20code%20in%20%60codes%2Feducation.csv%60%20(blank%20fails)%20%7C%0A%20%20%20%20%7C%20M6%20%7C%20a%20member%20who%20fished%20(%60mem_fished%20%3D%201%60)%20has%20none%20of%20the%20four%20%60mem_role_%60%20flags%20equal%20to%201%20%7C%0A%20%20%20%20%7C%20H1%20%7C%20the%20household's%20%60hh_roster_members%60%20differs%20from%20its%20number%20of%20member%20rows%20%7C%0A%20%20%20%20%7C%20U1%20%7C%20a%20unit's%20key%20(with%20%60region_code%60)%20is%20on%20more%20than%20one%20row%20%7C%0A%20%20%20%20%7C%20U2%20%7C%20the%20unit's%20%60operator_line%60%20is%20no%20member's%20%60mem_line%60%20in%20the%20household%2C%20or%20that%20member's%20%60mem_role_operator%60%20is%20not%201%2C%20or%20its%20%60mem_units_run%60%20differs%20from%20the%20number%20of%20the%20household's%20units%20that%20name%20the%20line%2C%20counted%20by%20distinct%20%60unit_no%60%20%7C%0A%20%20%20%20%7C%20U3%20%7C%20the%20unit's%20%60unit_scale%60%20is%20not%202%20when%20one%20of%20its%20boats%20is%205.0%20gross%20tons%20or%20more%2C%20or%20not%201%20when%20none%20is%20(a%20unit%20with%20no%20boat%3A%201)%20%7C%0A%20%20%20%20%7C%20U4%20%7C%20%60unit_uses_boat%60%20is%20not%201%20or%200%20(blank%20fails)%20%7C%0A%20%20%20%20%7C%20U5%20%7C%20%60unit_uses_boat%60%20is%20not%201%20for%20a%20unit%20with%20rows%20in%20%60boat%60%2C%20or%20not%200%20for%20a%20unit%20with%20none%20(blank%20fails)%20%7C%0A%20%20%20%20%7C%20U6%20%7C%20a%20unit%20with%20%60unit_hired_crew%20%3D%201%60%20has%20no%20row%20in%20%60crew%60%20with%20%60crew_hired%20%3D%201%60%20%7C%0A%20%20%20%20%7C%20U7%20%7C%20the%20unit's%20%60unit_boats%60%2C%20a%20blank%20read%20as%200%2C%20differs%20from%20its%20number%20of%20rows%20in%20%60boat%60%20%7C%0A%20%20%20%20%7C%20U8%20%7C%20%60unit_months_active%60%20is%20blank%2C%20or%20outside%201%20to%2012%20%7C%0A%20%20%20%20%7C%20B1%20%7C%20the%20boat's%20hull%20%60boat_hull%60%20is%20not%201%2C%202%20or%203%2C%20or%20its%20engine%20%60boat_engine%60%20is%20not%200%2C%201%20or%202%20(blank%20fails)%20%7C%0A%20%20%20%20%7C%20B2%20%7C%20a%20boat%20with%20no%20engine%20(%60boat_engine%20%3D%200%60)%20is%205.0%20gross%20tons%20or%20more%20%7C%0A%20%20%20%20%7C%20B3%20%7C%20the%20stored%20%60boat_tonnage%60%20is%20more%20than%200.01%20from%20the%20boat's%20gross%20tonnage%20(a%20blank%20on%20either%20side%20fails)%20%7C%0A%20%20%20%20%7C%20B4%20%7C%20a%20boat%20with%20%60boat_engine%20%3D%200%60%20has%20an%20engine%20power%20%60boat_engine_hp%60%2C%20or%20a%20boat%20with%20another%20engine%20code%20has%20none%20%7C%0A%20%20%20%20%7C%20B5%20%7C%20a%20registration%20number%20%60boat_reg_no%60%20(not%20blank)%20is%20on%20more%20than%20one%20boat%20row%20of%20the%20household%20%7C%0A%20%20%20%20%7C%20C1%20%7C%20the%20species%20%60catch_species%60%20is%20not%20a%20code%20in%20%60codes%2Fspecies.csv%60%20(blank%20fails)%20%7C%0A%20%20%20%20%7C%20C4%20%7C%20one%20species%20code%20is%20on%20more%20than%20one%20catch%20row%20of%20a%20unit%20%7C%0A%20%20%20%20%7C%20W1%20%7C%20the%20crew%20member's%20sex%20%60crew_sex%60%20is%20not%201%20or%202%20(blank%20fails)%20%7C%0A%0A%20%20%20%20A%20boat's%20gross%20tonnage%2C%20in%20U3%2C%20B2%20and%20B3%2C%20is%20length%20%C3%97%20beam%20%C3%97%20depth%20%C3%97%200.212%20when%20it%20was%20measured%20in%20meters%20(%60boat_measured_in%20%3D%202%60)%2C%20%C3%97%200.006%20when%20measured%20in%20feet%20(%60boat_measured_in%20%3D%203%60)%2C%20and%20the%20papers'%20%60boat_tonnage_reported%60%20when%20its%20size%20comes%20from%20its%20registration%20papers%20(%60boat_measured_in%20%3D%201%60).%0A%0A%20%20%20%20%7C%20rule%20%7C%20region_code%20%7C%20hh_id%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%20%60M1%60%20%7C%20%6002%60%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20%60U3%60%20%7C%20%6011%60%20%7C%20%E2%80%A6%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%5Bmodule%204%5D(%2F%3Ffile%3D30_edit_rules.py)%3A%20rules%20as%20data%2C%20run%20to%20a%20failure%20report%3B%20in%20R%20each%20rule%20is%20a%20%60case_when%60%2C%20and%20here%20each%20is%20a%20boolean%20mask%20over%20its%20table.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20rules_report(folder%3A%20Path)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Every%20rule%20of%20the%20codebook%20but%20C2%20and%20C3%2C%20and%20every%20household%20of%20the%20batch%20in%20%60folder%60%20that%20fails%20it.%22%22%22%0A%20%20%20%20%23%20Exercise%20K1.3%3A%20what%20goes%20in%2C%20what%20comes%20out%20and%20how%20it%20is%20checked%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Your%20group's%20code.%20There%20are%20no%20steps%20here%3A%20your%20plan%20says%20what%20they%20are.%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20K1.3%22)%0A%0A%0A%40app.function%0Adef%20failures_summary(table)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22What%20the%20check%20of%20Exercise%20K1.3%20reads%3A%20every%20rule%20and%20household%20pair%2C%20each%20once.%22%22%22%0A%20%20%20%20t%20%3D%20as_pandas(table)%0A%20%20%20%20if%20missing%20%3A%3D%20missing_columns(t%2C%20%5B%22rule%22%2C%20%22region_code%22%2C%20%22hh_id%22%5D)%3A%0A%20%20%20%20%20%20%20%20return%20missing%0A%20%20%20%20pairs%20%3D%20%7B(code(rule)%2C%20code(reg)%2C%20whole(hh))%20for%20rule%2C%20reg%2C%20hh%20in%20zip(t%5B%22rule%22%5D%2C%20t%5B%22region_code%22%5D%2C%20t%5B%22hh_id%22%5D)%7D%0A%20%20%20%20counts%20%3D%20%7Brule%3A%20sum(1%20for%20p%20in%20pairs%20if%20p%5B0%5D%20%3D%3D%20rule)%20for%20rule%20in%20sorted(%7Bp%5B0%5D%20for%20p%20in%20pairs%7D%2C%20key%3Dstr)%7D%0A%20%20%20%20return%20%7B%22households%2C%20by%20rule%22%3A%20counts%2C%20%22failures%22%3A%20sorted(pairs%2C%20key%3Drepr)%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%20K1.3%22%2C%20rules_report%2C%20batch_cases()%2C%0A%20%20%20%20%20%20%20%20what%3D%22batches%22%2C%20summarize%3Dfailures_summary%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20small%20batch%20fails%20every%20rule%20of%20the%20table%20at%20least%20once%3B%20the%20message%20shows%20how%20many%20households%20your%20code%20finds%20for%20each%20rule.%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%20your%20rules%20report%20of%20the%20batch.%0A%20%20%20%20course.try_it(rules_report%2C%20BATCH)%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%20K1.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%20A%20rule%20missing%20from%20*households%2C%20by%20rule*%20%7C%20Its%20mask%20is%20never%20True%3A%20often%20a%20comparison%20with%20a%20blank%2C%20which%20pandas%20makes%20False%20or%20missing.%20%7C%20how%20the%20rule%20treats%20a%20blank%2C%20in%20the%20table%20above%20%7C%0A%20%20%20%20%7C%20M4%20or%20M5%20fails%20hundreds%20of%20households%20%7C%20A%20blank%20made%20a%20failure%20where%20it%20only%20means%20%22not%20asked%22%3A%20a%20child%20under%2010%20has%20no%20%60mem_role_operator%60%2C%20a%20child%20under%206%20no%20%60mem_education%60%2C%20and%20a%20blank%20compared%20with%201%20is%20missing%2C%20not%20False.%20%7C%20read%20a%20flag%20as%20True%20only%20where%20it%20is%201%3B%20M5's%20scope%2C%20members%206%20or%20over%20%7C%0A%20%20%20%20%7C%20C1%20finds%20fewer%20households%20than%20you%20expect%20%7C%20A%20blank%20species%20was%20let%20through%3A%20%60notna()%20%26%20~isin(...)%60%20passes%20it%2C%20and%20the%20table%20says%20a%20blank%20fails.%20%7C%20C1%20%7C%0A%20%20%20%20%7C%20U3%20fails%20nearly%20every%20unit%20without%20a%20boat%20%7C%20The%20largest%20boat%20of%20no%20boats%20is%20missing%2C%20and%20the%20comparison%20made%20it%20a%20failure.%20%7C%20a%20unit%20with%20no%20boat%20must%20be%201%20%7C%0A%20%20%20%20%7C%20B3%20fails%20every%20boat%20measured%20in%20feet%2C%20or%20every%20boat%20from%20its%20papers%20%7C%20The%20meters%20factor%2C%200.212%2C%20used%20for%20feet%2C%20or%20the%20papers'%20figure%20left%20out.%20%7C%20the%20three%20ways%20a%20tonnage%20is%20known%20%7C%0A%20%20%20%20%7C%20U1%2C%20U2%20or%20H1%20too%20many%20in%20the%20small%20batch%20%7C%20Keys%20compared%20without%20%60region_code%60%2C%20so%20region%2011's%20households%20meet%20region%2002's.%20%7C%20the%20region%20in%20every%20key%20%7C%0A%20%20%20%20%7C%20H1%20is%20wrong%20for%20one%20household%20%7C%20The%20member%20rows%20were%20counted%20with%20%60clean%3DFalse%60%2C%20or%20households%20with%20no%20member%20rows%20were%20dropped%20by%20the%20join.%20%7C%20count%20after%20the%20removals%3B%20%60how%3D%22left%22%60%20%7C%0A%20%20%20%20%7C%20Rule%20ids%20like%20%60m1%60%20or%20%60M01%60%20%7C%20The%20check%20compares%20the%20ids%20as%20written.%20%7C%20the%20ids%20of%20%60codebook_rules.csv%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%20Exercise%20K1.4.%20The%20domain%20table%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20director's%20table%3A%20the%20estimated%20number%20of%20fishing%20units%20in%20each%20province%2C%20small-scale%20and%20large-scale%2C%20each%20with%20the%20standard%20error%20and%20the%20CV%20that%20say%20how%20far%20to%20trust%20it.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20batch's%20folder%20%E2%86%92%20one%20row%20per%20**province%20of%20the%20batch%20and%20scale**%2C%20with%20%60region_code%60%20and%20%60province_code%60%20as%20zero-padded%20text%2C%20%60scale%60%20(%60small-scale%60%20for%20a%20unit%20with%20%60unit_scale%20%3D%201%60%2C%20%60large-scale%60%20for%20%60unit_scale%20%3D%202%60%2C%20as%20reported)%2C%20and%20%60total%60%2C%20%60se%60%20and%20%60cv%60%20(the%20CV%20in%20percent%3A%20100%20%C3%97%20SE%20%C3%B7%20total).%20A%20province%20with%20no%20unit%20of%20a%20scale%20gets%20the%20row%20anyway%2C%20with%20total%200%2C%20SE%200%20and%20a%20blank%20CV.%0A%0A%20%20%20%20%7C%20region_code%20%7C%20province_code%20%7C%20scale%20%7C%20total%20%7C%20se%20%7C%20cv%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%7C%20%6002%60%20%7C%20%60015%60%20%7C%20%60large-scale%60%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%20%20%20%20%7C%20%6002%60%20%7C%20%60015%60%20%7C%20%60small-scale%60%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%0A%20%20%20%20**The%20design**%2C%20as%20%5Bmodule%206%5D(%2F%3Ffile%3D50_design_estimates.py)%20set%20it%20out%3A%20the%20unit%20is%20the%20household%2C%20with%20weight%20%60sel_final_weight%60%3B%20a%20household's%20value%20is%20its%20number%20of%20units%20of%20the%20scale%2C%20from%20the%20flat%20table.%20The%20strata%20are%20province%20%C3%97%20%60sel_stratum%60.%20The%20PSU%20is%20the%20enumeration%20area%20%60sel_ea%60%2C%20numbered%20within%20its%20province%3B%20in%20stratum%20F%2C%20where%20every%20registered%20large%20unit%20is%20taken%2C%20each%20household%20is%20its%20own%20PSU.%20The%20finite%20population%20correction%20uses%20each%20stratum's%20number%20of%20PSUs%2C%20%60eas_in_frame%60%20in%20the%20batch's%20%60stratum_counts.csv%60%2C%20as%20a%20count.%20The%20variance%20is%20the%20stratified%20between-PSU%20(Taylor)%20variance%20with%20that%20correction.%20No%20stratum%20of%20the%20batch%20has%20a%20single%20PSU.%0A%0A%20%20%20%20The%20check%20compares%20totals%20and%20SEs%20to%20four%20significant%20figures%20and%20CVs%20to%20three%2C%20so%20svy%20and%20a%20function%20your%20group%20writes%20from%20the%20formula%20both%20pass.%0A%0A%20%20%20%20**Tools%3A**%20svy's%20%60Design(stratum%3D%2C%20psu%3D%2C%20wgt%3D%2C%20pop_size%3D)%60%20and%20%60Sample(...).estimation.total(y%3D%2C%20by%3D)%60%20(module%206%2C%20section%202)%2C%20on%20a%20polars%20table%3B%20or%20the%20formula%20as%20your%20own%20function%20(module%206%2C%20section%204).%20In%20ReGenesees%20you%20write%20the%20correction%20as%20%60fpc%20%3D%20~fpc%60%20in%20%60e.svydesign%60%2C%20where%20it%20may%20be%20the%20stratum's%20count%20or%20its%20sampled%20share%20(%5Bits%20documentation%5D(https%3A%2F%2Fgithub.com%2FDiegoZardetto%2FReGenesees%2Fblob%2Fmaster%2Fman%2Fe.svydesign.Rd)%2C%20checked%202026-11-20)%3B%20svy's%20%60pop_size%3D%60%20wants%20the%20count.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20domain_table(folder%3A%20Path)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20estimated%20number%20of%20fishing%20units%20by%20province%20and%20scale%20in%20the%20batch%20in%20%60folder%60%2C%20with%20SE%20and%20CV%20(%25).%22%22%22%0A%20%20%20%20%23%20Exercise%20K1.4%3A%20what%20goes%20in%2C%20what%20comes%20out%20and%20how%20it%20is%20checked%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Your%20group's%20code.%20There%20are%20no%20steps%20here%3A%20your%20plan%20says%20what%20they%20are.%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20K1.4%22)%0A%0A%0A%40app.function%0Adef%20domain_summary(table)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22What%20the%20check%20of%20Exercise%20K1.4%20reads%3A%20every%20row's%20province%20and%20scale%2C%20its%20total%20and%20SE%20to%20four%20significant%0A%20%20%20%20figures%2C%20and%20its%20CV%20to%20three.%22%22%22%0A%20%20%20%20t%20%3D%20as_pandas(table)%0A%20%20%20%20columns%20%3D%20%5B%22region_code%22%2C%20%22province_code%22%2C%20%22scale%22%2C%20%22total%22%2C%20%22se%22%2C%20%22cv%22%5D%0A%20%20%20%20if%20missing%20%3A%3D%20missing_columns(t%2C%20columns)%3A%0A%20%20%20%20%20%20%20%20return%20missing%0A%20%20%20%20rows%20%3D%20%5B(code(reg)%2C%20code(prov)%2C%20code(scale)%2C%20figures(total%2C%204)%2C%20figures(se%2C%204)%2C%20figures(cv%2C%203))%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20reg%2C%20prov%2C%20scale%2C%20total%2C%20se%2C%20cv%20in%20zip(*(t%5Bc%5D%20for%20c%20in%20columns))%5D%0A%20%20%20%20return%20%7B%22rows%22%3A%20sorted(rows%2C%20key%3Drepr)%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%20K1.4%22%2C%20domain_table%2C%20batch_cases()%2C%0A%20%20%20%20%20%20%20%20what%3D%22batches%22%2C%20summarize%3Ddomain_summary%2C%0A%20%20%20%20%20%20%20%20hint%3D%22In%20the%20small%20batch%2C%20each%20region%20has%20one%20province%2C%20three%20households%20in%20stratum%20F%20and%20two%20areas%20in%20each%20of%20strata%20A%2C%20B%20and%20C%3B%20region%2002%20loses%20three%20households%20of%20stratum%20C%20to%20the%20removals.%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%20your%20domain%20table%20of%20the%20batch.%0A%20%20%20%20course.try_it(domain_table%2C%20BATCH)%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%20K1.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%20Totals%20right%2C%20every%20SE%20too%20large%20%7C%20The%20finite%20population%20correction%20left%20out.%20%7C%20%60pop_size%3D%60%20with%20%60eas_in_frame%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20Invalid%20FPC%20values*%20from%20svy%20%7C%20The%20correction%20given%20as%20a%20sampling%20fraction%2C%20%60eas_sampled%60%20%C3%B7%20%60eas_in_frame%60%2C%20as%20ReGenesees%20also%20accepts%3B%20svy%20wants%20the%20count%20of%20PSUs.%20%7C%20%60eas_in_frame%60%2C%20as%20it%20is%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20singleton%20PSU(s)%20detected*%20from%20svy%2C%20or%20SEs%20of%200%20in%20stratum%20F%20from%20your%20own%20function%20%7C%20Stratum%20F's%20households%20all%20have%20a%20blank%20%60sel_ea%60%2C%20so%20they%20form%20one%20PSU.%20%7C%20each%20household%20its%20own%20PSU%20in%20stratum%20F%20%7C%0A%20%20%20%20%7C%20Every%20household%20of%20stratum%20F%20is%20missing%2C%20and%20the%20totals%20are%20too%20small%20%7C%20A%20%60groupby%60%20that%20drops%20blank%20keys%3A%20%60sel_ea%60%20is%20blank%20in%20stratum%20F.%20%7C%20%60dropna%3DFalse%60%2C%20or%20leave%20%60sel_ea%60%20out%20of%20the%20grouping%20%7C%0A%20%20%20%20%7C%20Totals%20too%20large%20%7C%20Each%20unit's%20row%20taken%20as%20a%20household%2C%20or%20the%20unit%20entered%20twice%20counted%20twice.%20%7C%20one%20row%20per%20household%2C%20from%20the%20flat%20table%20%7C%0A%20%20%20%20%7C%20CVs%20a%20hundred%20times%20too%20small%20%7C%20svy's%20CV%20is%20a%20fraction.%20%7C%20100%20%C3%97%20SE%20%C3%B7%20total%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20You%20are%20trying%20to%20merge%20on%20string%20and%20int64%20columns%20for%20key%20'region_code'*%20%7C%20The%20stratum%20counts'%20codes%20are%20numbers%20in%20the%20CSV%2C%20and%20the%20flat%20table's%20are%20text.%20%7C%20%60str.zfill%60%20on%20the%20counts'%20codes%20%7C%0A%20%20%20%20%7C%20%60scale%60%20with%20other%20labels%20%7C%20The%20check%20compares%20%60small-scale%60%20and%20%60large-scale%60%20as%20written.%20%7C%20the%20labels%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%20Exercise%20K1.5.%20The%20workbook%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20director%20reads%20Excel%2C%20and%20so%20do%20the%20editors.%20A%20table%20retyped%20from%20a%20notebook%20is%20a%20table%20with%20typing%20errors%20in%20it%3B%20a%20workbook%20written%20by%20code%20is%20the%20same%20every%20time%20it%20runs.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20batch's%20folder%20and%20a%20file%20path%20%E2%86%92%20the%20workbook%20written%20at%20that%20path%20(its%20folder%20made%20if%20need%20be)%2C%20and%20the%20path%20given%20back.%20It%20has%20**four%20sheets%2C%20in%20this%20order%3A%20%60Estimates%60%2C%20%60Rule%20failures%60%2C%20%60Orphans%60%2C%20%60Notes%60**.%0A%0A%20%20%20%20-%20%60Estimates%60%20holds%20the%20domain%20table%20(output%204)%2C%20%60Rule%20failures%60%20the%20rules%20report%20(output%203)%20and%20%60Orphans%60%20the%20matching%20report%20(output%202)%2C%20each%20with%20these%20columns%20in%20this%20order%3A%20%60region_code%60%2C%20%60province_code%60%2C%20%60scale%60%2C%20%60total%60%2C%20%60se%60%2C%20%60cv%60%3B%20%60region_code%60%2C%20%60hh_id%60%2C%20%60rule%60%3B%20%60region_code%60%2C%20%60hh_id%60%2C%20%60operator_line%60%2C%20%60unit_no%60%2C%20%60side%60.%0A%20%20%20%20-%20A%20title%20and%20a%20note%20above%20a%20table%20are%20welcome.%20The%20check%20finds%20each%20table%20by%20its%20header%20row%2C%20the%20first%20row%20whose%20first%20cell%20is%20%60region_code%60%2C%20and%20reads%20down%20to%20the%20first%20row%20whose%20first%20cell%20is%20empty.%0A%20%20%20%20-%20Codes%20are%20text%20(%6002%60%2C%20%60015%60)%2C%20and%20totals%2C%20SEs%20and%20CVs%20are%20numbers%20Excel%20can%20add%20up%2C%20rounded%20or%20not.%0A%20%20%20%20-%20%60Notes%60%20says%20which%20weight%20the%20estimates%20use%2C%20naming%20the%20variable%20%60sel_final_weight%60%2C%20where%20the%20data%20came%20from%20and%20when%20the%20workbook%20was%20made.%0A%0A%20%20%20%20**Tools%3A**%20%5Bmodule%207%2C%20section%201%5D(%2F%3Ffile%3D60_publish_tables.py)%3A%20%60pd.ExcelWriter%60%20with%20%60startrow%3D%60%20and%20%60index%3DFalse%60%2C%20and%20openpyxl%20for%20titles%20and%20number%20formats%3B%20in%20R%2C%20%60writexl%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20write_workbook(folder%3A%20Path%2C%20path%3A%20Path)%20-%3E%20Path%3A%0A%20%20%20%20%22%22%22The%20director's%20workbook%20for%20the%20batch%20in%20%60folder%60%2C%20written%20at%20%60path%60%3A%20estimates%2C%20rule%20failures%2C%20orphans%2C%20notes.%22%22%22%0A%20%20%20%20%23%20Exercise%20K1.5%3A%20what%20goes%20in%2C%20what%20comes%20out%20and%20how%20it%20is%20checked%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Your%20group's%20code.%20There%20are%20no%20steps%20here%3A%20your%20plan%20says%20what%20they%20are.%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20K1.5%22)%0A%0A%0A%40app.function%0Adef%20workbook_summary(path)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22What%20the%20check%20of%20Exercise%20K1.5%20reads%3A%20the%20sheets%2C%20whether%20codes%20are%20text%20and%20figures%20are%20numbers%2C%20whether%20the%0A%20%20%20%20notes%20name%20the%20weight%2C%20and%20the%20three%20tables%20as%20the%20checks%20of%20outputs%202%20to%204%20read%20them.%22%22%22%0A%20%20%20%20if%20not%20isinstance(path%2C%20(str%2C%20Path))%20or%20not%20Path(path).exists()%3A%0A%20%20%20%20%20%20%20%20return%20%7B%22a%20workbook%22%3A%20f%22no%20file%20at%20%7Bpath!r%7D%22%7D%0A%20%20%20%20book%20%3D%20load_workbook(path%2C%20data_only%3DTrue)%0A%0A%20%20%20%20def%20table_on(name%3A%20str)%20-%3E%20pd.DataFrame%20%7C%20None%3A%0A%20%20%20%20%20%20%20%20if%20name%20not%20in%20book.sheetnames%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20None%0A%20%20%20%20%20%20%20%20rows%20%3D%20list(book%5Bname%5D.iter_rows(values_only%3DTrue))%0A%20%20%20%20%20%20%20%20start%20%3D%20next((i%20for%20i%2C%20row%20in%20enumerate(rows)%20if%20row%20and%20row%5B0%5D%20%3D%3D%20%22region_code%22)%2C%20None)%0A%20%20%20%20%20%20%20%20if%20start%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20None%0A%20%20%20%20%20%20%20%20header%20%3D%20%5Bstr(h)%20if%20h%20is%20not%20None%20else%20f%22column%20%7Bi%7D%22%20for%20i%2C%20h%20in%20enumerate(rows%5Bstart%5D)%5D%0A%20%20%20%20%20%20%20%20body%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20row%20in%20rows%5Bstart%20%2B%201%3A%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20not%20row%20or%20row%5B0%5D%20is%20None%20or%20row%5B0%5D%20%3D%3D%20%22%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20%20%20%20%20%20%20%20%20body.append(row)%0A%20%20%20%20%20%20%20%20return%20pd.DataFrame(body%2C%20columns%3Dheader)%0A%0A%20%20%20%20estimates%2C%20failures%2C%20orphans%20%3D%20table_on(%22Estimates%22)%2C%20table_on(%22Rule%20failures%22)%2C%20table_on(%22Orphans%22)%0A%20%20%20%20tables%20%3D%20%5Bt%20for%20t%20in%20(estimates%2C%20failures%2C%20orphans)%20if%20t%20is%20not%20None%5D%0A%20%20%20%20texts%20%3D%20all(isinstance(v%2C%20str)%20and%20len(v)%20%3D%3D%202%20for%20t%20in%20tables%20for%20v%20in%20t%5B%22region_code%22%5D)%0A%20%20%20%20numbers%20%3D%20estimates%20is%20not%20None%20and%20all(isinstance(v%2C%20(int%2C%20float))%20for%20c%20in%20(%22total%22%2C%20%22se%22)%20if%20c%20in%20estimates%20for%20v%20in%20estimates%5Bc%5D)%0A%20%20%20%20notes%20%3D%20%22Notes%22%20in%20book.sheetnames%20and%20any(%22sel_final_weight%22%20in%20str(v)%20for%20row%20in%20book%5B%22Notes%22%5D.iter_rows(values_only%3DTrue)%20for%20v%20in%20row%20if%20v%20is%20not%20None)%0A%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%22sheets%22%3A%20book.sheetnames%2C%20%22codes%20as%20text%22%3A%20texts%2C%20%22figures%20as%20numbers%22%3A%20numbers%2C%20%22notes%20name%20sel_final_weight%22%3A%20notes%2C%0A%20%20%20%20%20%20%20%20%22Estimates%22%3A%20domain_summary(estimates)%20if%20estimates%20is%20not%20None%20else%20%22no%20table%20found%22%2C%0A%20%20%20%20%20%20%20%20%22Rule%20failures%22%3A%20failures_summary(failures)%20if%20failures%20is%20not%20None%20else%20%22no%20table%20found%22%2C%0A%20%20%20%20%20%20%20%20%22Orphans%22%3A%20orphans_summary(orphans)%20if%20orphans%20is%20not%20None%20else%20%22no%20table%20found%22%2C%0A%20%20%20%20%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%20K1.5%22%2C%20write_workbook%2C%20batch_cases(lambda%20name%3A%20CHECK_BATCHES%20%2F%20%22workbooks%22%20%2F%20f%22%7Bgroup_slug(name)%7D.xlsx%22)%2C%0A%20%20%20%20%20%20%20%20what%3D%22batches%22%2C%20summarize%3Dworkbook_summary%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20message%20starts%20with%20what%20the%20check%20found%3A%20the%20sheets%2C%20whether%20codes%20are%20text%20and%20figures%20numbers%2C%20and%20whether%20the%20notes%20name%20sel_final_weight.%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%20your%20workbook%20of%20the%20batch%2C%20written%20into%20capstone%2F.%20Open%20it%20in%20Excel%20and%20read%20it%20as%20the%20director%20would.%0A%20%20%20%20course.try_it(write_workbook%2C%20BATCH%2C%20CAPSTONE%20%2F%20%22try%22%20%2F%20%22tables.xlsx%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%20K1.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%20%60'sheets'%60%20in%20another%20order%2C%20or%20with%20%60Sheet%60%20among%20them%20%7C%20The%20sheets%20are%20written%20in%20another%20order%2C%20or%20the%20empty%20sheet%20openpyxl%20starts%20with%20is%20still%20there.%20%7C%20the%20four%20names%2C%20in%20order%20%7C%0A%20%20%20%20%7C%20%60no%20table%20found%60%20%7C%20The%20header%20row's%20first%20cell%20is%20not%20%60region_code%60%3A%20%60to_excel%60%20writes%20the%20index%20as%20the%20first%20column%20unless%20told%20not%20to.%20%7C%20%60index%3DFalse%60%20%7C%0A%20%20%20%20%7C%20%60'codes%20as%20text'%3A%20False%60%20%7C%20A%20code%20was%20written%20as%20a%20number%20(%602%60%20for%20%6002%60)%2C%20or%20turned%20into%20one%20on%20the%20way.%20%7C%20the%20codes'%20type%20before%20writing%20%7C%0A%20%20%20%20%7C%20%60'figures%20as%20numbers'%3A%20False%60%20%7C%20Totals%20or%20SEs%20written%20as%20formatted%20text%20(%60%225%2C131%22%60)%2C%20which%20Excel%20cannot%20add%20up.%20%7C%20a%20number%20format%2C%20not%20a%20text%20%7C%0A%20%20%20%20%7C%20%60'notes%20name%20sel_final_weight'%3A%20False%60%20%7C%20The%20notes%20describe%20the%20weight%20in%20words%20only.%20%7C%20name%20the%20variable%20%7C%0A%20%20%20%20%7C%20The%20tables%20found%2C%20the%20figures%20wrong%20%7C%20Outputs%202%20to%204%20are%20not%20green%20yet%3A%20the%20workbook%20only%20writes%20what%20they%20give.%20%7C%20Exercises%20K1.2%20to%20K1.4%20%7C%0A%20%20%20%20%7C%20*PermissionError*%20on%20the%20Try%20it%20%7C%20The%20file%20is%20open%20in%20Excel%2C%20which%20locks%20it.%20%7C%20close%20it%2C%20run%20again%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(rf%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%20K1.6.%20The%20review%20table%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20supervisors%20can%20revisit%20%7BREVIEW_DEPTH%7D%20households.%20A%20list%20in%20which%20the%20planted%20defects%20sit%20at%20the%20top%20sends%20them%20where%20the%20errors%20are%3B%20a%20list%20in%20household%20order%20sends%20them%20nowhere%20in%20particular.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20batch's%20folder%20%E2%86%92%20**one%20row%20per%20household**%20of%20the%20batch%20(after%20the%20removals)%2C%20with%20%60region_code%60%2C%20%60hh_id%60%20and%20%60rank%60%20(1%20is%20the%20first%20to%20revisit%2C%20then%202%2C%203%20and%20so%20on%2C%20each%20rank%20once)%3B%20add%20%60score%60%20and%20%60reasons%60%20so%20a%20supervisor%20knows%20why%20a%20household%20is%20there.%20What%20you%20rank%20on%20is%20your%20group's%20design%3A%20the%20rules%20and%20the%20matching%20report%2C%20the%20outliers%2C%20ratios%20and%20relations%20of%20%5Bmodule%208%5D(%2F%3Ffile%3D70_anomalies_rules.py)%2C%20a%20detector%20of%20%5Bmodule%209%5D(%2F%3Ffile%3D80_anomalies_ml.py)%2C%20or%20a%20mix.%0A%0A%20%20%20%20%7C%20region_code%20%7C%20hh_id%20%7C%20rank%20%7C%20score%20%7C%20reasons%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%3A--%7C%0A%20%20%20%20%7C%20%6011%60%20%7C%20%E2%80%A6%20%7C%201%20%7C%20%E2%80%A6%20%7C%20%60a%20boat's%20size%3B%20a%20rule%60%20%7C%0A%20%20%20%20%7C%20%6002%60%20%7C%20%E2%80%A6%20%7C%202%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%0A%20%20%20%20**How%20it%20is%20judged.**%20The%20check%20reads%20the%20table%20sorted%20by%20%60rank%60%20and%20counts%20how%20many%20of%20the%20batch's%20planted%20defects%20are%20among%20its%20first%20%7BREVIEW_DEPTH%7D%20rows.%20It%20passes%20at%20**%7BRECALL_PERCENT%7D%25%20or%20more**%20(the%20recall%20at%20%7BREVIEW_DEPTH%7D).%20A%20planted%20defect%20is%20a%20household%20that%20the%20course%20changed%20on%20purpose.%20Two%20kinds%20do%20not%20count%3A%20the%20removed%20households%20and%20voided%20rows%2C%20which%20your%20flat%20table%20already%20leaves%20out%2C%20and%20the%20households%20of%20an%20area%20stratum%20that%20run%20a%20large-scale%20unit%2C%20which%20are%20no%20error%20but%20a%20heavy%20weight%20in%20a%20small%20domain.%20The%20course%20does%20not%20ship%20its%20list.%20The%20check%20holds%20a%20**digest**%20of%20each%20planted%20household%20instead%3A%20a%20code%20of%2016%20characters%20made%20from%20its%20region%20and%20household%20id%20by%20a%20one-way%20function%20(SHA-256)%2C%20from%20which%20the%20household%20cannot%20be%20read.%20The%20check%20makes%20the%20same%20code%20from%20each%20household%20in%20your%20table%20and%20counts%20the%20matches.%20The%20cell%20below%20the%20check%20shows%20your%20recall%20as%20a%20number%2C%20for%20the%20demo.%0A%0A%20%20%20%20**Tools%3A**%20module%208%2C%20section%205%20(a%20review%20table%20from%20ranked%20flags)%20and%20module%209%2C%20section%203%20(judging%20a%20detector%20by%20recall).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20review_table(folder%3A%20Path)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Every%20household%20of%20the%20batch%20in%20%60folder%60%2C%20ranked%20for%20revisiting%3A%20rank%201%20first%2C%20with%20its%20score%20and%20reasons.%22%22%22%0A%20%20%20%20%23%20Exercise%20K1.6%3A%20what%20goes%20in%2C%20what%20comes%20out%20and%20how%20it%20is%20checked%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Your%20group's%20code.%20There%20are%20no%20steps%20here%3A%20your%20plan%20says%20what%20they%20are.%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20K1.6%22)%0A%0A%0A%40app.function%0Adef%20planted_digests()%20-%3E%20frozenset%5Bstr%5D%3A%0A%20%20%20%20%22%22%22A%20digest%20of%20each%20household%20of%20the%20batch%20in%20which%20the%20course%20planted%20a%20defect%20(not%20the%20removed%20households%20and%0A%20%20%20%20voided%20rows%2C%20not%20the%20households%20of%20an%20area%20stratum%20that%20run%20a%20large-scale%20unit)%3A%20the%20first%2016%20characters%20of%20SHA-256%0A%20%20%20%20of%20%22%3Csalt%3E%7C%3Cregion%3E%7C%3Chousehold%20id%3E%22%2C%20as%20unit_digest()%20makes%20them.%20Written%20from%20the%20list%20of%20planted%20defects%20the%0A%20%20%20%20build%20holds%20back%3B%20after%20a%20change%20to%20scripts%2Fmake_data.py%2C%20the%20trainer%20writes%20them%20again%20from%0A%20%20%20%20.build%2Fheld_back%2Fbatch2_defects.csv%20with%20unit_digest()%2C%20and%20the%20build%20refuses%20to%20record%20the%20check%20of%20Exercise%20K1.6%0A%20%20%20%20until%20they%20match%20the%20batch.%22%22%22%0A%20%20%20%20return%20frozenset(%22%22%22%0A%20%20%20%20035f55fc6842cef1%200430836e7d802f94%20061ad22d5fc645bb%2008c338c6bd23258d%2008d75dd9882e5703%200a77f8a6097aa0ed%0A%20%20%20%200c01c631736f5703%201409a14992a64f1a%2014b5978324c02720%2014d5e243edacfaa1%2019b687e73a590f5f%201c85bfb8808fa423%0A%20%20%20%202441d195210a1d09%2024e38d4209a9c1f6%20261823b9c566368e%202b9c4ae9f84282fe%202d1585d7e926f891%202dfed40af50ebf73%0A%20%20%20%2034bfc227dff67417%203a296c74c7a0b738%203a7c81a05a78d6c8%203c69805b67f4212e%2041cec5bc240228e5%2041e97165dacf6040%0A%20%20%20%20450f500ea409e9a8%20462138d902204bb0%204a557049887247d4%204af6df6a7630b252%204bf03fcdd4cef4e7%204c45dce7a690ba07%0A%20%20%20%204deedebf8b4c573a%204f270e8a94d13a19%2050fec0cd3705e681%20542cc8f629c77f44%2056884d746855b322%20581e7ab6b7140ca4%0A%20%20%20%205a8199869c0b0f21%205a9615d55c11004c%205b2c7028808b433d%205d793a2cf23952df%205e6d72472ebeeb08%20606d824c0e47ad65%0A%20%20%20%2064696c0a3e747304%2068c5066627a443fa%206c29d114e8cbf1bc%207000ad424d2b76e1%2070d614d9edc3fd51%2072a9c09ee86ba167%0A%20%20%20%2073bc9bfc52531182%20794a4509acd5fdfc%207e58b391870db2e5%207ea441f0ee4a8c6c%207f48c10c437c3872%2080b25f2bea7a1302%0A%20%20%20%2082f76ac205e8bb62%2089ce4d109551d3cb%208ec6d51bf3a07a62%208ef163eb2d3e6513%20940d0532cfaafaee%209e69c11b2ed281e4%0A%20%20%20%20a2e9c600273522af%20a71bb9befa83db71%20a74e130d56ef14ce%20a7ef10e05616a581%20aa579c76bf655975%20abd844d722aa3ef5%0A%20%20%20%20b674404b0567a054%20b7bc40ccbdd5414c%20baba26b01f4cb2b1%20c5559110191dcc56%20c9e6467b94466865%20cd4b45d8b38209b5%0A%20%20%20%20cd6bd66b53ebfb42%20ce9bdf323c5fdb1b%20d1d3d553cf4c6e50%20d4e3366cf7da2c7c%20d7fb8fa73a5ca09a%20dc2a2bdbd6a4a9fb%0A%20%20%20%20e1a6a86fbeb22c24%20e226f6c0556dfd8e%20e8d999ae6be93113%20eb20bf2187820c29%20eb6a5f30997b0477%20ee8596bfd7de7740%0A%20%20%20%20eebfdfdedf724c37%20eefaaec426cbb0d6%20f776163542356916%20fd6e97031c118e0f%20fe08a1902e3a5264%0A%20%20%20%20%22%22%22.split())%0A%0A%0A%40app.function%0Adef%20unit_digest(region%3A%20str%2C%20household%3A%20int)%20-%3E%20str%3A%0A%20%20%20%20%22%22%22A%20household's%20digest%3A%20what%20the%20check%20of%20Exercise%20K1.6%20compares%2C%20so%20the%20planted%20households%20are%20never%20named.%22%22%22%0A%20%20%20%20return%20hashlib.sha256(f%22survey-statistics%20capstone%20batch%202%7C%7Bregion%7D%7C%7Bhousehold%7D%22.encode(%22utf-8%22)).hexdigest()%5B%3A16%5D%0A%0A%0A%40app.function%0Adef%20recall_found(table)%20-%3E%20tuple%5Bint%2C%20int%5D%3A%0A%20%20%20%20%22%22%22How%20many%20planted%20households%20are%20in%20the%20first%20REVIEW_DEPTH%20rows%20of%20a%20review%20table%2C%20and%20how%20many%20are%20in%20it%20at%20all.%22%22%22%0A%20%20%20%20t%20%3D%20as_pandas(table)%0A%20%20%20%20ordered%20%3D%20t.assign(_rank%3Dt%5B%22rank%22%5D.map(whole)).sort_values(%22_rank%22%2C%20kind%3D%22stable%22%2C%20na_position%3D%22last%22)%0A%20%20%20%20planted%20%3D%20planted_digests()%0A%20%20%20%20marks%20%3D%20%5Bunit_digest(code(reg)%2C%20whole(hh))%20in%20planted%20for%20reg%2C%20hh%20in%20zip(ordered%5B%22region_code%22%5D%2C%20ordered%5B%22hh_id%22%5D)%5D%0A%20%20%20%20return%20sum(marks%5B%3AREVIEW_DEPTH%5D)%2C%20sum(marks)%0A%0A%0A%40app.function%0Adef%20review_summary(table)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22What%20the%20check%20of%20Exercise%20K1.6%20reads%3A%20the%20row%20count%2C%20one%20row%20per%20household%2C%20ranks%201%20to%20n%2C%20and%20whether%20the%0A%20%20%20%20first%20REVIEW_DEPTH%20rows%20hold%20at%20least%20RECALL_BAR%20of%20the%20planted%20households%20in%20the%20table.%22%22%22%0A%20%20%20%20t%20%3D%20as_pandas(table)%0A%20%20%20%20if%20missing%20%3A%3D%20missing_columns(t%2C%20%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22rank%22%5D)%3A%0A%20%20%20%20%20%20%20%20return%20missing%0A%20%20%20%20homes%20%3D%20list(zip(t%5B%22region_code%22%5D.map(code)%2C%20t%5B%22hh_id%22%5D.map(whole)))%0A%20%20%20%20ranks%20%3D%20sorted(t%5B%22rank%22%5D.map(whole)%2C%20key%3Dlambda%20r%3A%20(r%20is%20None%2C%20r%20or%200))%0A%20%20%20%20found%2C%20planted%20%3D%20recall_found(t)%0A%20%20%20%20meets%20%3D%20planted%20%3D%3D%200%20or%20found%20%2F%20planted%20%3E%3D%20RECALL_BAR%0A%20%20%20%20if%20os.environ.get(%22COURSE_RECORD_CHECKS%22)%20and%20len(t)%20%3E%20REVIEW_DEPTH%20and%20(planted%20%3D%3D%200%20or%20not%20meets)%3A%0A%20%20%20%20%20%20%20%20raise%20RuntimeError(f%22the%20review%20table's%20solution%20finds%20%7Bfound%7D%20of%20%7Bplanted%7D%20planted%20households%3A%20the%20digests%20in%20planted_digests()%20no%20longer%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%20%20%20%22match%20the%20batch%3B%20write%20them%20again%20from%20.build%2Fheld_back%2Fbatch2_defects.csv%22)%0A%20%20%20%20return%20%7B%22rows%22%3A%20len(t)%2C%20%22one%20row%20per%20household%22%3A%20len(set(homes))%20%3D%3D%20len(homes)%2C%20%22ranks%201%20to%20n%22%3A%20ranks%20%3D%3D%20list(range(1%2C%20len(t)%20%2B%201))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22at%20least%20%7BRECALL_BAR%3A.0%25%7D%20of%20the%20planted%20defects%20in%20the%20first%20%7BREVIEW_DEPTH%7D%20rows%22%3A%20meets%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%20K1.6%22%2C%20review_table%2C%20batch_cases()%2C%0A%20%20%20%20%20%20%20%20what%3D%22batches%22%2C%20summarize%3Dreview_summary%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20message%20starts%20with%20your%20table's%20row%20count%2C%20then%20whether%20each%20household%20is%20there%20once%2C%20whether%20the%20ranks%20run%20from%201%20without%20a%20gap%20or%20a%20tie%2C%20and%20whether%20the%20recall%20reaches%20the%20bar.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_table%20%3D%20attempt(review_table%2C%20BATCH)%0A%20%20%20%20mo.stop(_table%20is%20NOT_YET%2C%20waiting_for(%22Exercise%20K1.6%22))%0A%20%20%20%20_found%2C%20_planted%20%3D%20recall_found(_table)%0A%20%20%20%20mo.md(f%22**Your%20review%20table's%20first%20%7BREVIEW_DEPTH%7D%20rows%20hold%20%7B_found%7D%20of%20the%20%7B_planted%7D%20planted%20households%3A%20a%20recall%20of%20%7B_found%20%2F%20_planted%3A.0%25%7D**%20(the%20bar%20is%20%7BRECALL_BAR%3A.0%25%7D).%20%22%0A%20%20%20%20%20%20%20%20%20%20%22The%20rules%20report%20and%20the%20matching%20report%20alone%20fall%20short%20of%20the%20bar.%20Checks%20of%20what%20is%20unlikely%2C%20not%20only%20what%20is%20invalid%20(module%208)%2C%20and%20a%20reading%20of%20the%20text%20answers%20against%20the%20code%20lists%20take%20a%20ranking%20past%20it.%20Say%20in%20your%20demo%20what%20your%20ranking%20missed%2C%20and%20why.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20review%20table%20of%20the%20batch.%0A%20%20%20%20course.try_it(review_table%2C%20BATCH)%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%20K1.6%22%3A%20mo.md(rf%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'rows'%60%20differs%20from%20the%20flat%20table's%20households%20%7C%20Only%20the%20flagged%20households%20are%20listed%2C%20or%20the%20removed%20ones%20are%20back.%20%7C%20every%20household%20after%20the%20removals%20%7C%0A%20%20%20%20%7C%20%60'one%20row%20per%20household'%3A%20False%60%20%7C%20A%20household%20listed%20once%20per%20flag.%20%7C%20one%20row%20per%20%60region_code%60%20and%20%60hh_id%60%20%7C%0A%20%20%20%20%7C%20%60'ranks%201%20to%20n'%3A%20False%60%20%7C%20Ranks%20that%20start%20at%200%2C%20ties%20given%20the%20same%20rank%2C%20or%20a%20rank%20per%20flag.%20%7C%20%60range(1%2C%20n%20%2B%201)%60%20after%20sorting%20%7C%0A%20%20%20%20%7C%20The%20recall%20is%20under%20the%20bar%20%7C%20The%20ranking%20rests%20on%20part%20of%20the%20evidence%3A%20rules%20only%2C%20or%20one%20detector%20alone.%20%7C%20the%20matching%20report%2C%20the%20rules%2C%20module%208's%20ratios%20and%20relations%2C%20and%20the%20text%20answers%2C%20together%20%7C%0A%20%20%20%20%7C%20The%20recall%20is%20under%20the%20bar%20on%20the%20batch%20only%20%7C%20Flags%20compared%20with%20%60hh_id%60%20alone%3A%20in%20the%20small%20batch%20every%20household%20is%20in%20the%20first%20%7BREVIEW_DEPTH%7D%20rows%20anyway.%20%7C%20the%20region%20in%20every%20key%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%20Full%20tier%3A%20one%20script%20that%20writes%20all%20six%0A%0A%20%20%20%20The%20notebook%20is%20where%20the%20group%20built%20and%20checked%20its%20functions.%20The%20survey's%20next%20batch%20is%20not%20run%20from%20a%20notebook%3A%20it%20is%20run%20as%20one%20command%2C%20by%20someone%20who%20was%20not%20in%20the%20room.%20Move%20your%20functions%20into%20a%20script%2C%20%60capstone%2F%3Cgroup%3E%2Frun.py%60%2C%20that%20takes%20the%20batch's%20folder%20and%20writes%20the%20six%20outputs%20into%20%60capstone%2F%3Cgroup%3E%2Fout%2F%60%20under%20these%20names%3A%0A%0A%20%20%20%20%7C%20output%20%7C%20file%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%20%C2%B7%20the%20flat%20table%20%7C%20%60flat_table.parquet%60%20%7C%0A%20%20%20%20%7C%202%20%C2%B7%20the%20matching%20report%20%7C%20%60matching_report.csv%60%20%7C%0A%20%20%20%20%7C%203%20%C2%B7%20the%20rules%20report%20%7C%20%60rules_report.csv%60%20%7C%0A%20%20%20%20%7C%204%20%C2%B7%20the%20domain%20table%20%7C%20%60domain_table.csv%60%20%7C%0A%20%20%20%20%7C%205%20%C2%B7%20the%20workbook%20%7C%20%60tables.xlsx%60%20%7C%0A%20%20%20%20%7C%206%20%C2%B7%20the%20review%20table%20%7C%20%60review_table.csv%60%20%7C%0A%0A%20%20%20%20Give%20it%20what%20%5Bthe%20stretch%20S2%2C%20part%204%5D(%2F%3Ffile%3D61_stretch_tabulation.py)%20gave%20the%20tabulation%20script%3A%20%60--help%60%20with%20an%20example%2C%20a%20refusal%20with%20a%20clear%20message%20when%20an%20expected%20region%20has%20no%20rows%2C%20the%20log%20on%20stderr%2C%20and%20an%20exit%20code%20that%20says%20whether%20it%20worked.%20Run%20it%20from%20the%20course%20folder%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20uv%20run%20python%20capstone%2F%3Cyour-group%3E%2Frun.py%20data%2Fbatch2%20--out%20capstone%2F%3Cyour-group%3E%2Fout%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20The%20table%20below%20reads%20the%20files%20the%20script%20wrote%20and%20checks%20each%20with%20the%20same%20check%20as%20its%20exercise%2C%20on%20the%20batch.%20Write%20the%20CSVs%20without%20the%20index%2C%20and%20with%20the%20codes%20as%20they%20are%3B%20the%20table%20reads%20the%20codes%20back%20as%20text.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20read_output(path%3A%20Path)%3A%0A%20%20%20%20%22%22%22An%20output%20file%20as%20the%20acceptance%20table%20reads%20it%3A%20Parquet%20as%20it%20is%2C%20a%20CSV%20with%20its%20codes%20and%20labels%20as%20text%2C%20and%0A%20%20%20%20the%20workbook%20as%20its%20path.%22%22%22%0A%20%20%20%20if%20path.suffix%20%3D%3D%20%22.parquet%22%3A%0A%20%20%20%20%20%20%20%20return%20pd.read_parquet(path)%0A%20%20%20%20if%20path.suffix%20%3D%3D%20%22.csv%22%3A%0A%20%20%20%20%20%20%20%20return%20pd.read_csv(path%2C%20dtype%3D%7Bc%3A%20str%20for%20c%20in%20(%22region_code%22%2C%20%22province_code%22%2C%20%22citymun_code%22%2C%20%22barangay_code%22%2C%20%22sel_stratum%22%2C%20%22side%22%2C%20%22rule%22%2C%20%22scale%22)%7D)%0A%20%20%20%20return%20path%0A%0A%0A%40app.function%0Adef%20acceptance(out%3A%20Path)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Each%20output%20file%20in%20%60out%60%20checked%20as%20its%20exercise%20checks%20the%20batch%3A%20found%20or%20not%2C%20and%20right%20or%20not.%22%22%22%0A%20%20%20%20outputs%20%3D%20%5B(%22flat_table.parquet%22%2C%20%22Exercise%20K1.1%20%C2%B7%20flat_table%22%2C%20flat_summary)%2C%20(%22matching_report.csv%22%2C%20%22Exercise%20K1.2%20%C2%B7%20matching_report%22%2C%20orphans_summary)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%22rules_report.csv%22%2C%20%22Exercise%20K1.3%20%C2%B7%20rules_report%22%2C%20failures_summary)%2C%20(%22domain_table.csv%22%2C%20%22Exercise%20K1.4%20%C2%B7%20domain_table%22%2C%20domain_summary)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(%22tables.xlsx%22%2C%20%22Exercise%20K1.5%20%C2%B7%20write_workbook%22%2C%20workbook_summary)%2C%20(%22review_table.csv%22%2C%20%22Exercise%20K1.6%20%C2%B7%20review_table%22%2C%20review_summary)%5D%0A%20%20%20%20recorded%20%3D%20json.loads((PATHS.root%20%2F%20%22checks.json%22).read_text(encoding%3D%22utf-8%22))%20if%20(PATHS.root%20%2F%20%22checks.json%22).exists()%20else%20%7B%7D%0A%20%20%20%20rows%20%3D%20%5B%5D%0A%20%20%20%20for%20name%2C%20key%2C%20summarize%20in%20outputs%3A%0A%20%20%20%20%20%20%20%20path%20%3D%20out%20%2F%20name%0A%20%20%20%20%20%20%20%20if%20not%20path.exists()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20rows.append(%7B%22file%22%3A%20name%2C%20%22found%22%3A%20%22not%20yet%22%2C%20%22right%22%3A%20%22%22%7D)%0A%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20right%20%3D%20key%20in%20recorded%20and%20course.fingerprint(summarize(read_output(path)))%20%3D%3D%20recorded%5Bkey%5D%5B-1%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20rows.append(%7B%22file%22%3A%20name%2C%20%22found%22%3A%20%22yes%22%2C%20%22right%22%3A%20%22yes%22%20if%20right%20else%20%22not%20yet%3A%20run%20its%20exercise's%20check%20to%20see%20what%20differs%22%7D)%0A%20%20%20%20%20%20%20%20except%20Exception%20as%20error%3A%20%20%23%20a%20file%20that%20does%20not%20read%20is%20a%20result%20too%3A%20say%20what%20went%20wrong%0A%20%20%20%20%20%20%20%20%20%20%20%20rows.append(%7B%22file%22%3A%20name%2C%20%22found%22%3A%20%22yes%22%2C%20%22right%22%3A%20f%22cannot%20read%20it%3A%20%7Btype(error).__name__%7D%3A%20%7Bstr(error)%5B%3A80%5D%7D%22%7D)%0A%20%20%20%20return%20pd.DataFrame(rows)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(group_name)%3A%0A%20%20%20%20_slug%20%3D%20group_slug(group_name.value)%0A%20%20%20%20mo.stop(not%20_slug%2C%20mo.callout(mo.md(%22Type%20your%20group's%20name%20at%20the%20top%2C%20and%20this%20table%20reads%20your%20script's%20outputs.%22)%2C%20kind%3D%22neutral%22))%0A%20%20%20%20_out%20%3D%20CAPSTONE%20%2F%20_slug%20%2F%20%22out%22%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**Acceptance%3A%20the%20files%20in%20%60capstone%2F%7B_slug%7D%2Fout%2F%60**%20(run%20this%20cell%20again%20after%20each%20run%20of%20your%20script)%3A%22)%2C%0A%20%20%20%20%20%20%20%20acceptance(_out)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Own%20data%20tier%3A%20your%20institution's%20tables%0A%0A%20%20%20%20The%20same%20pipeline%20on%20a%20survey%20your%20own%20institution%20holds%20is%20the%20real%20test%20of%20it%2C%20and%20the%20one%20the%20course%20cannot%20check%20for%20you.%20It%20is%20done%20**in%20the%20room%20only**%2C%20under%20rules%20that%20keep%20the%20microdata%20where%20it%20may%20be%3A%0A%0A%20%20%20%20-%20The%20tables%20come%20into%20the%20room%20only%20with%20the%20agreement%20of%20whoever%20is%20responsible%20for%20them%2C%20and%20only%20on%20a%20path%20your%20institution%20has%20approved.%20They%20stay%20where%20they%20are%20kept%3A%20read%20them%20from%20their%20own%20folder%20or%20server%2C%20and%20write%20nothing%20of%20them%20into%20the%20course%20folder%20or%20%60capstone%2F%60%2C%20which%20are%20copied%2C%20zipped%20and%20shared.%0A%20%20%20%20-%20No%20AI%20tool%20reads%20them%20unless%20your%20institution%20has%20approved%20that%20path%20for%20them%2C%20as%20%5Bthe%20stretch%20S4%2C%20section%202%5D(%2F%3Ffile%3D86_stretch_agents.py)%20sets%20out%3A%20a%20cloud%20agent%20sends%20what%20it%20reads%20to%20its%20provider.%0A%20%20%20%20-%20What%20leaves%20the%20session%20is%20aggregates%3A%20the%20domain%20table%2C%20counts%20of%20failures%20by%20rule.%20The%20rules%20report%20and%20the%20review%20table%20name%20households%2C%20and%20stay%20on%20your%20institution's%20machine.%0A%0A%20%20%20%20The%20checks%20above%20know%20nothing%20of%20your%20tables%2C%20so%20write%20your%20own%3A%20for%20each%20output%2C%20one%20small%20table%20worked%20out%20by%20hand%2C%20and%20one%20total%20that%20must%20match%20a%20figure%20your%20institution%20already%20publishes.%0A%0A%20%20%20%20The%20course's%20own%20data%20is%20invented%20for%20it%2C%20so%20it%20may%20go%20anywhere.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(group_name)%3A%0A%20%20%20%20_slug%20%3D%20group_slug(group_name.value)%0A%20%20%20%20_out%20%3D%20CAPSTONE%20%2F%20_slug%20%2F%20%22out%22%20if%20_slug%20else%20None%0A%20%20%20%20_rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(%22The%20group's%20name%20typed%20at%20the%20top%22%2C%20bool(_slug)%2C%20group_name.value.strip()%2C%20%22Type%20it%20in%20the%20box%20at%20the%20top.%22)%2C%0A%20%20%20%20%20%20%20%20(%22The%20plan%20saved%22%2C%20bool(_slug)%20and%20(CAPSTONE%20%2F%20f%22%7B_slug%7D-plan.md%22).exists()%2C%20f%22capstone%2F%7B_slug%7D-plan.md%22%20if%20_slug%20else%20%22%22%2C%20%22Section%201%3A%20answer%20the%20questions%2C%20then%20Save%20the%20plan.%22)%2C%0A%20%20%20%20%20%20%20%20(%22The%20script%20written%22%2C%20bool(_slug)%20and%20(CAPSTONE%20%2F%20_slug%20%2F%20%22run.py%22).exists()%2C%20f%22capstone%2F%7B_slug%7D%2Frun.py%22%20if%20_slug%20else%20%22%22%2C%20%22Full%20tier%3A%20move%20your%20functions%20into%20it.%22)%2C%0A%20%20%20%20%20%20%20%20(%22Its%20outputs%20written%22%2C%20bool(_out)%20and%20_out.exists()%20and%20any(_out.iterdir())%2C%20f%22capstone%2F%7B_slug%7D%2Fout%2F%22%20if%20_slug%20else%20%22%22%2C%20%22Run%20the%20script%20on%20data%2Fbatch2.%22)%2C%0A%20%20%20%20%5D%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22**Where%20your%20group%20is**%20(it%20reads%20the%20files%20in%20%60capstone%2F%60%3B%20run%20this%20cell%20again%20to%20refresh)%3A%22)%2C%0A%20%20%20%20%20%20%20%20pd.DataFrame(%5B%7B%22step%22%3A%20s%2C%20%22done%22%3A%20%22yes%22%20if%20ok%20else%20%22not%20yet%22%2C%20%22detail%22%3A%20d%2C%20%22next%22%3A%20%22%22%20if%20ok%20else%20n%7D%20for%20s%2C%20ok%2C%20d%2C%20n%20in%20_rows%5D)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%203%20%C2%B7%20Demos%0A%0A%20%20%20%20Each%20group%20shows%20its%20pipeline%20working%2C%20from%20one%20laptop%2C%20in%20**five%20minutes**%3A%20a%20hard%20stop%2C%20so%20that%20every%20group%20gets%20its%20turn.%0A%0A%20%20%20%20%7C%20minutes%20%7C%20what%20%7C%20say%20or%20show%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%20%7C%20**the%20plan**%20%7C%20your%20order%20of%20outputs%20and%20who%20built%20what%20(your%20plan's%20first%20two%20answers)%20%7C%0A%20%20%20%20%7C%203%20%7C%20**the%20pipeline%20working**%20%7C%20the%20checks%20green%20from%20a%20restart%2C%20or%20the%20script%20run%20from%20the%20terminal%20and%20the%20acceptance%20table%20read%3B%20then%20one%20number%20from%20the%20domain%20table%2C%20and%20how%20you%20know%20it%20is%20right%20%7C%0A%20%20%20%20%7C%201%20%7C%20**what%20the%20batch%20hid**%20%7C%20a%20defect%20you%20found%20that%20no%20module%20planted%3A%20how%20you%20noticed%20it%2C%20which%20output%20it%20changed%2C%20and%20what%20your%20review%20table's%20recall%20says%20your%20ranking%20still%20misses%20%7C%0A%0A%20%20%20%20The%20last%20minute%20is%20the%20one%20people%20remember.%20A%20pipeline%20that%20caught%20something%20nobody%20told%20it%20to%20look%20for%20is%20the%20story%20the%20capstone%20is%20for.%0A%0A%20%20%20%20**Before%20your%20turn%2C%20on%20the%20presenting%20laptop%3A**%0A%0A%20%20%20%201.%20Restart%20this%20notebook%20and%20let%20it%20run%20from%20the%20top%3A%20every%20check%20you%20claim%20must%20be%20green%20without%20you%20fixing%20anything.%0A%20%20%20%202.%20If%20you%20wrote%20the%20script%2C%20run%20it%20once%20more%20and%20refresh%20the%20acceptance%20table.%0A%20%20%20%203.%20Open%20the%20workbook%2C%20%60capstone%2F%3Cyour-group%3E%2Fout%2Ftables.xlsx%60%20or%20the%20Try%20it's%20%60capstone%2Ftry%2Ftables.xlsx%60%2C%20at%20the%20%60Estimates%60%20sheet.%0A%20%20%20%204.%20Open%20your%20plan%20(%60capstone%2F%3Cyour-group%3E-plan.md%60)%3A%20it%20is%20your%20script.%0A%0A%20%20%20%20**While%20the%20others%20present%2C**%20write%20down%20one%20question%20for%20each%20group.%20Good%20questions%20name%20a%20number%3A%20%22Which%20output%20would%20change%20if%20that%20rule%20were%20wrong%3F%22%2C%20%22What%20did%20your%20ranking%20put%20first%2C%20and%20was%20it%20planted%3F%22%2C%20%22What%20happens%20when%20the%20third%20batch%20has%20a%20region%20with%20codes%20as%20text%3F%22%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20What%20you%20can%20do%20now%0A%0A%20%20%20%20%7C%20you%20can%20%7C%20where%20the%20course%20taught%20it%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Read%20a%20survey's%20regional%20databases%20into%20one%20flat%20table%2C%20codes%20whole%20and%20removals%20applied%20%7C%20%5Bmodule%202%5D(%2F%3Ffile%3D10_flat_table.py)%20%7C%0A%20%20%20%20%7C%20Find%20the%20rows%20of%20parent%20and%20child%20tables%20that%20have%20no%20match%2C%20key%20part%20by%20key%20part%20%7C%20%5Bmodule%203%5D(%2F%3Ffile%3D20_key_matching.py)%20%7C%0A%20%20%20%20%7C%20Write%20a%20questionnaire's%20rules%20as%20exact%20masks%20and%20run%20them%20to%20a%20failure%20report%20%7C%20%5Bmodule%204%5D(%2F%3Ffile%3D30_edit_rules.py)%20%7C%0A%20%20%20%20%7C%20Derive%20categories%20from%20codes%20and%20count%20units%20by%20group%20%7C%20%5Bmodule%205%5D(%2F%3Ffile%3D40_recode_and_tabulate.py)%20%7C%0A%20%20%20%20%7C%20Estimate%20totals%20with%20SE%20and%20CV%20under%20a%20stratified%20cluster%20design%2C%20in%20svy%20or%20from%20the%20formula%20%7C%20%5Bmodule%206%5D(%2F%3Ffile%3D50_design_estimates.py)%20%7C%0A%20%20%20%20%7C%20Publish%20tables%20to%20a%20workbook%2C%20and%20run%20the%20whole%20tabulation%20as%20one%20script%20%7C%20%5Bmodule%207%5D(%2F%3Ffile%3D60_publish_tables.py)%20%7C%0A%20%20%20%20%7C%20Rank%20records%20for%20review%20with%20rules%2C%20statistics%20and%20machine%20learning%2C%20and%20judge%20the%20ranking%20by%20recall%20%7C%20%5Bmodules%208%5D(%2F%3Ffile%3D70_anomalies_rules.py)%20and%20%5B9%5D(%2F%3Ffile%3D80_anomalies_ml.py)%20%7C%0A%20%20%20%20%7C%20Brief%20an%20agent%20to%20build%20such%20a%20pipeline%2C%20and%20know%20what%20it%20may%20read%20%7C%20%5Bthe%20stretch%20S4%5D(%2F%3Ffile%3D86_stretch_agents.py)%2C%20a%20showcase%20%7C%0A%0A%20%20%20%20This%20is%20the%20course's%20last%20notebook.%20The%20two%20primers%2C%20%5Bfrom%20R%20to%20Python%5D(%2F%3Ffile%3D95_primer_r_to_python.py)%20and%20%5Bsurvey%20sampling%20in%20one%20page%5D(%2F%3Ffile%3D96_primer_survey_sampling.py)%2C%20and%20the%20%5Bglossary%5D(%2F%3Ffile%3D97_glossary.py)%20stay%20for%20when%20you%20need%20them%3B%20the%20next%20batch%20your%20own%20institution%20receives%20is%20the%20next%20exercise.%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
1640971824580b3f125d76dadf6a1b5a