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%22Edit%20rules%20and%20duplicates%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20the%20tables%20every%20other%20cell%20uses.%0A%20%20%20%20import%20re%0A%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%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20the%20survey's%20tables%2C%20file%20locations%20and%20exercise%20checks%0A%20%20%20%20from%20course%20import%20NOT_YET%2C%20PATHS%2C%20UNIT_KEY%2C%20attempt%2C%20check_cases%2C%20try_it%2C%20waiting_for%0A%0A%20%20%20%20HOUSEHOLD%20%3D%20%5B%22region_code%22%2C%20%22hh_id%22%5D%20%20%23%20a%20household%3A%20each%20regional%20database%20numbers%20its%20own%2C%20so%20the%20region%20is%20part%20of%20the%20key%0A%20%20%20%20MEMBERS%2C%20HOUSEHOLDS%2C%20UNITS%20%3D%20%22member%22%2C%20%22household%22%2C%20%22fishing_unit%22%0A%20%20%20%20CREW%2C%20BOATS%2C%20CATCH%20%3D%20%22crew%22%2C%20%22boat%22%2C%20%22catch%22%0A%20%20%20%20ROW_ID%20%3D%20%22row_id%22%20%20%23%20the%20capture%20system's%20row%20id%2C%20in%20every%20table%3B%20each%20regional%20database%20numbers%20its%20own%0A%0A%20%20%20%20%23%20The%20survey%2C%20every%20region%20in%20one%20table%2C%20the%20removed%20households%20and%20voided%20rows%20left%20out%20(module%202)%3B%20the%20child%20tables'%0A%20%20%20%20%23%20key%20parts%20renamed%20to%20the%20unit's%20(module%203)%0A%20%20%20%20members%20%3D%20course.members()%0A%20%20%20%20households%20%3D%20course.table(HOUSEHOLDS)%0A%20%20%20%20units%20%3D%20course.units()%0A%20%20%20%20crew%20%3D%20course.child(CREW)%0A%20%20%20%20boats%20%3D%20course.child(BOATS)%0A%20%20%20%20catch%20%3D%20course.child(CATCH)%0A%20%20%20%20tables%20%3D%20%7BMEMBERS%3A%20members%2C%20HOUSEHOLDS%3A%20households%2C%20UNITS%3A%20units%2C%20CREW%3A%20crew%2C%20BOATS%3A%20boats%2C%20CATCH%3A%20catch%7D%0A%20%20%20%20selected%20%3D%20course.households()%20%20%23%20each%20household%20with%20its%20selection%20row%3A%20stratum%2C%20enumeration%20area%20and%20weights%0A%0A%20%20%20%20codebook%20%3D%20pd.read_csv(PATHS.codebook)%20%20%23%20every%20variable%3A%20table%2C%20type%2C%20label%2C%20and%20its%20codes%20in%20words%0A%20%20%20%20codebook_rules%20%3D%20pd.read_csv(PATHS.rules)%20%20%23%20the%20survey's%20validation%20rules%20in%20words%3A%20M1%20to%20W1%0A%20%20%20%20species%2C%20education%2C%20relationship%20%3D%20course.codes(%22species%22)%2C%20course.codes(%22education%22)%2C%20course.codes(%22relationship%22)%0A%0A%20%20%20%20%23%20The%20values%20the%20rules%20name%20with%20%40%3A%20code%20lists%2C%20and%20the%20tolerance%20for%20a%20stored%20tonnage%20(gross%20tons)%2C%20as%20rule%20B3%20states%20it%0A%20%20%20%20rule_values%20%3D%20%7B%22species%22%3A%20species%5B%22code%22%5D.tolist()%2C%20%22education%22%3A%20education%5B%22code%22%5D.tolist()%2C%20%22tolerance%22%3A%200.01%7D%0A%20%20%20%20REPORT_COLUMNS%20%3D%20%5B%22rule%22%2C%20%22table%22%2C%20%22region_code%22%2C%20%22hh_id%22%2C%20%22rows%22%2C%20%22action%22%2C%20%22message%22%5D%20%20%23%20the%20failure%20report%0A%20%20%20%20EDIT_LOG_COLUMNS%20%3D%20%5B%22rule%22%2C%20%22table%22%2C%20%22region_code%22%2C%20%22hh_id%22%2C%20%22row%22%2C%20%22variable%22%2C%20%22before%22%2C%20%22after%22%2C%20%22how%22%5D%20%20%23%20the%20edit%20log%0A%20%20%20%20SEED%20%3D%2041593%20%20%23%20the%20seed%20of%20every%20random%20edit%3A%20the%20same%20seed%20and%20the%20same%20data%20give%20the%20same%20draws%0A%20%20%20%20planted%20%3D%20course.planted_defects()%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%20Edit%20rules%20and%20duplicates%0A%0A%20%20%20%20**Where%20this%20is%20in%20processing%20a%20survey.**%20The%20tables%20are%20in%20one%20place%20and%20their%20keys%20match%20(modules%202%20and%203).%20This%20module%20does%20the%20next%20two%20steps%3A%20it%20**reviews%20and%20validates**%20the%20data%20within%20each%20table%2C%20then%20**edits**%20what%20a%20rule%20can%20settle%20and%20sends%20the%20rest%20to%20a%20person.%0A%0A%20%20%20%20%7C%20step%20%7C%20what%20it%20asks%20%7C%20here%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20review%20and%20validate%2C%20within%20each%20table%20%7C%20every%20coded%20item%20counted%20against%20its%20valid%20codes%3B%20ages%20in%20range%3B%20items%20that%20must%20agree%20with%20each%20other%20or%20with%20another%20table%20%7C%20sections%201%20and%202%20%7C%0A%20%20%20%20%7C%20%7C%20the%20survey's%20validation%20rules%20run%20as%20one%20specification%2C%20to%20one%20failure%20report%20%7C%20section%203%20%7C%0A%20%20%20%20%7C%20edit%20%7C%20the%20edits%20the%20specification%20carries%3A%20a%20blank%20answered%20from%20another%20table%2C%20a%20code%20drawn%20at%20random%20with%20a%20seed%2C%20an%20engine%20set%20by%20the%20boat's%20size%3B%20every%20change%20in%20an%20edit%20log%20%7C%20section%204%20%7C%0A%20%20%20%20%7C%20review%2C%20for%20duplicates%20%7C%20rows%20entered%20twice%2C%20found%20by%20their%20key%2C%20and%20two%20definitions%20the%20survey%20states%3B%20what%20no%20program%20can%20settle%20goes%20to%20a%20person%20%7C%20section%205%20%7C%0A%0A%20%20%20%20Module%203%20matched%20the%20survey's%20keys%2C%20so%20every%20catch%20row%20now%20finds%20its%20unit.%20A%20matched%20row%20can%20still%20be%20wrong.%20A%20member's%20sex%20is%20a%20code%20the%20codebook%20does%20not%20have.%20A%20member%20is%20older%20than%20anyone%20alive.%20A%20unit's%20answer%20to%20%22did%20you%20use%20a%20boat%3F%22%20is%20blank.%20A%20boat%20with%20no%20engine%20is%20a%205-ton%20vessel.%20One%20catch%20row%20is%20there%20twice.%20This%20module%20finds%20such%20rows%20with%20rules%2C%20writes%20the%20rules%20as%20data%20that%20an%20editor%20and%20a%20program%20both%20read%2C%20lets%20a%20rule%20carry%20its%20edit%2C%20and%20sends%20what%20no%20rule%20can%20settle%20to%20a%20person.%0A%0A%20%20%20%20%7C%20section%20%7C%20what%20you%20do%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%20%C2%B7%20Frequency%20tables%20against%20the%20codebook's%20valid%20codes%20%7C%20count%20every%20coded%20item%20against%20its%20code%20list%2C%20with%20%22Invalid%20value%22%20and%20%22No%20answer%22%20rows%20%7C%0A%20%20%20%20%7C%202%20%C2%B7%20Range%20and%20cross-item%20consistency%20rules%20%7C%20ages%20in%20range%2C%20operators%2016%20or%20older%2C%20a%20boat's%20tonnage%20against%20its%20dimensions%2C%20a%20unit's%20scale%20against%20its%20boats%2C%20a%20household's%20count%20against%20its%20member%20rows%20%7C%0A%20%20%20%20%7C%203%20%C2%B7%20Rules%20as%20data%20%7C%20the%20codebook's%20rules%20as%20a%20specification%20table%2C%20run%20to%20one%20failure%20report%20%7C%0A%20%20%20%20%7C%204%20%C2%B7%20Specifications%20that%20edit%20%7C%20a%20rule%20that%20carries%20its%20edit%3A%20a%20blank%20answered%20from%20the%20boat%20table%2C%20a%20crew%20member's%20sex%20drawn%20from%201%20and%202%20with%20a%20seed%2C%20an%20engine%20set%20by%20the%20boat's%20tonnage%2C%20a%20unit%20with%20no%20hired%20crew%20referred%3B%20every%20change%20in%20an%20edit%20log%20%7C%0A%20%20%20%20%7C%205%20%C2%B7%20Duplicates%20by%20key%20and%20by%20a%20stated%20definition%20%7C%20copies%20found%20by%20the%20key%20that%20names%20a%20row%2C%20a%20registered%20boat%20under%20two%20units%20and%20an%20area%20in%20two%20strata%2C%20then%20every%20edit%20and%20every%20rule%20again%3A%20what%20still%20fails%20goes%20to%20a%20person%20%7C%0A%0A%20%20%20%20**The%20tables.**%20The%20setup%20cell%20reads%20six%20tables%20of%20the%20survey%20with%20the%20course's%20helpers%2C%20every%20region's%20rows%20in%20one%20table%20and%20the%20removed%20households%20and%20voided%20rows%20left%20out%2C%20as%20module%202%20taught%3A%20%60members%60%20(%60member%60%2C%20one%20row%20per%20household%20member)%2C%20%60households%60%20(%60household%60%2C%20one%20row%20per%20household)%2C%20%60units%60%20(%60fishing_unit%60%2C%20one%20row%20per%20fishing%20unit)%2C%20and%20%60crew%60%2C%20%60boats%60%20and%20%60catch%60%20(%60crew%60%2C%20%60boat%60%20and%20%60catch%60)%20with%20their%20key%20parts%20renamed%20to%20the%20unit's%3A%20%60catch_operator_line%60%20is%20%60operator_line%60%20here.%20The%20key%20parts%20are%20as%20the%20survey%20stores%20them%2C%20so%20a%20row%20module%203%20found%20keyed%20to%20the%20wrong%20unit%20is%20still%20keyed%20to%20it%20here.%20%60selected%60%20is%20each%20household%20with%20its%20selection%20row%20(stratum%2C%20enumeration%20area%20and%20weights)%2C%20which%20section%205%20needs.%20The%20setup%20cell%20also%20reads%20the%20codebook%20(%60data%2Fcodebook.csv%60)%2C%20the%20survey's%20validation%20rules%20in%20words%20(%60data%2Fcodebook_rules.csv%60%2C%20M1%20to%20W1)%20and%20the%20code%20lists%20in%20%60data%2Fcodes%2F%60.%0A%0A%20%20%20%20**A%20household%20is%20a%20region%20and%20an%20%60hh_id%60.**%20Each%20regional%20database%20numbers%20its%20own%20households%2C%20so%20the%20same%20%60hh_id%60%20can%20be%20one%20household%20in%20region%2001%20and%20another%20in%20region%2004.%20Every%20key%20in%20this%20module%20starts%20with%20the%20region%2C%20%60region_code%60%3B%20the%20setup%20cell%20names%20the%20pair%20%60HOUSEHOLD%60%2C%20and%20a%20unit%20is%20%60UNIT_KEY%60%3A%20the%20household%2C%20%60operator_line%60%20and%20%60unit_no%60.%0A%0A%20%20%20%20Exercises%20work%20as%20in%20the%20modules%20before%3A%20write%20the%20function%2C%20delete%20the%20%60raise%60%20line%2C%20use%20the%20**Try%20it**%20cell%2C%20read%20the%20check.%20When%20something%20goes%20red%2C%20read%20the%20error's%20**last%20line**%20first%3A%20it%20names%20what%20went%20wrong.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%224.0%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201%20%C2%B7%20Frequency%20tables%20against%20the%20codebook's%20valid%20codes%0A%0A%20%20%20%20**The%20problem.**%20The%20first%20look%20at%20a%20coded%20item%20is%20a%20count%20of%20its%20codes.%20Count%20the%20members'%20sex%2C%20%60mem_sex%60%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Every%20code%20of%20mem_sex%20and%20how%20many%20members%20have%20it%2C%20blanks%20included%0A%20%20%20%20_counts%20%3D%20members%5B%22mem_sex%22%5D.value_counts(dropna%3DFalse).sort_index()%0A%20%20%20%20_strange%20%3D%20%5Bint(code)%20for%20code%20in%20_counts.index%20if%20code%20not%20in%20(1%2C%202)%5D%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20_counts%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22The%20codebook%20allows%20%601%20%3D%20Male%3B%202%20%3D%20Female%60.%20The%20count%20also%20has%20**%7B'%2C%20'.join(map(str%2C%20_strange))%7D**%2C%20side%20by%20side%20with%20the%20valid%20codes%20and%20no%20different%20from%20them.%20With%20two%20codes%20that%20is%20easy%20to%20see%3B%20with%20the%20species%20codes%20of%20%60catch_species%60%2C%20or%20the%20education%20codes%20of%20%60mem_education%60%2C%20it%20is%20not.%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20A%20count%20of%20codes%20answers%20%22how%20many%20of%20each%3F%22.%20An%20editor%20asks%20a%20second%20question%3A%20**which%20of%20these%20codes%20are%20allowed%3F**%20The%20answer%20is%20in%20the%20codebook%2C%20so%20the%20frequency%20table%20should%20be%20read%20against%20it%3A%20every%20valid%20code%20with%20its%20label%2C%20then%20the%20codes%20the%20codebook%20does%20not%20have%2C%20then%20the%20blanks.%20In%20R%20a%20recode%20with%20%60case_when%60%20does%20this%2C%20putting%20codes%20outside%20the%20list%20in%20an%20**%22Invalid%20value%22**%20row%20and%20missing%20values%20in%20a%20**%22No%20answer%22**%20row.%20This%20section%20builds%20the%20same%20table%20in%20pandas%2C%20and%20keeps%20the%20two%20apart%3A%20a%20missing%20answer%20and%20a%20wrong%20one%20call%20for%20different%20work.%0A%0A%20%20%20%20%23%23%23%20Where%20the%20valid%20codes%20are%0A%0A%20%20%20%20The%20codebook%20keeps%20short%20code%20lists%20in%20its%20%60values%60%20column%2C%20as%20text%3A%20%601%20%3D%20Male%3B%202%20%3D%20Female%60.%20Long%20lists%20have%20a%20file%20of%20their%20own%20in%20%60data%2Fcodes%2F%60%2C%20and%20the%20%60values%60%20column%20points%20to%20it%3A%20%60see%20codes%2Feducation.csv%60.%20Here%20are%20the%20members'%20coded%20items%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20members'%20items%20with%20codes%3A%20short%20lists%20in%20the%20values%20text%2C%20long%20ones%20in%20a%20file%20of%20data%2Fcodes%2F%0A%20%20%20%20codebook.loc%5B(codebook%5B%22table%22%5D%20%3D%3D%20MEMBERS)%20%26%20codebook%5B%22values%22%5D.str.contains(%22%3D%7Ccodes%2F%22%2C%20na%3DFalse)%2C%20%5B%22variable%22%2C%20%22label%22%2C%20%22values%22%5D%5D%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20text%20is%20written%20for%20people.%20Three%20things%20in%20it%20trip%20a%20program%3A%20a%20note%20can%20follow%20the%20codes%20(%601%20%3D%20Yes%3B%200%20%3D%20No%3B%20asked%20of%20members%2010%20years%20old%20and%20over%60)%2C%20a%20line%20can%20contain%20%60%3D%60%20and%20hold%20no%20code%20at%20all%20(%60blank%20if%20unit_uses_boat%20%3D%200%60)%2C%20and%20a%20variable%20with%20no%20codes%20has%20an%20empty%20cell%2C%20which%20pandas%20reads%20as%20a%20missing%20value%2C%20not%20as%20text.%0A%0A%20%20%20%20%23%23%23%20Exercise%204.1.%20The%20codes%20in%20a%20codebook%20entry%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20codebook%20is%20where%20the%20valid%20codes%20are%20written%20once.%20A%20frequency%20table%20that%20reads%20them%20from%20it%20stays%20right%20when%20the%20codebook%20changes%3B%20one%20that%20types%20them%20again%20drifts.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20one%20cell%20of%20the%20codebook's%20%60values%60%20column%20%E2%86%92%20a%20dictionary%20%60%7Bcode%3A%20label%7D%60%20with%20the%20codes%20as%20**integers**%2C%20in%20the%20codebook's%20order%3B%20%60%7B%7D%60%20when%20the%20text%20lists%20no%20codes%20or%20the%20cell%20is%20empty%3A%0A%0A%20%20%20%20%7C%20%60values%60%20%7C%20result%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%601%20%3D%20Male%3B%202%20%3D%20Female%60%20%7C%20%60%7B1%3A%20%22Male%22%2C%202%3A%20%22Female%22%7D%60%20%7C%0A%20%20%20%20%7C%20%601%20%3D%20Yes%3B%200%20%3D%20No%3B%20asked%20of%20members%2010%20years%20old%20and%20over%60%20%7C%20%60%7B1%3A%20%22Yes%22%2C%200%3A%20%22No%22%7D%60%20%7C%0A%20%20%20%20%7C%20%60blank%20if%20unit_uses_boat%20%3D%200%60%20%7C%20%60%7B%7D%60%20%7C%0A%20%20%20%20%7C%20*(empty%3A%20a%20missing%20value)*%20%7C%20%60%7B%7D%60%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60isinstance(values%2C%20str)%60%2C%20%60text.split(%22%3B%22)%60%2C%20%60part.partition(%22%3D%22)%60%20(splits%20at%20the%20first%20%60%3D%60%20into%20the%20left%20side%2C%20the%20%60%3D%60%20and%20the%20rest%2C%20and%20gives%20an%20empty%20%60%3D%60%20when%20there%20is%20none)%2C%20%60.strip()%60%2C%20%60.isdigit()%60%2C%20%60int()%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20codebook_codes(values)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22The%20codes%20a%20codebook%20%60values%60%20entry%20lists%2C%20as%20%7Bcode%3A%20label%7D%20with%20integer%20codes%2C%20in%20its%20order%3B%20%7B%7D%20if%20it%20lists%20none.%22%22%22%0A%20%20%20%20%23%20Exercise%204.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20start%20an%20empty%20dict%3B%20if%20values%20is%20not%20text%20(an%20empty%20cell%20is%20a%20missing%20value)%2C%20return%20it%20as%20it%20is%0A%20%20%20%20%23%20Step%202%3A%20split%20the%20text%20on%20%22%3B%22%20and%20split%20each%20part%20at%20its%20first%20%22%3D%22%20with%20.partition(%22%3D%22)%0A%20%20%20%20%23%20Step%203%3A%20keep%20a%20part%20only%20if%20it%20had%20an%20%22%3D%22%20and%20its%20left%20side%2C%20stripped%2C%20is%20all%20digits%3A%20int(code)%20-%3E%20label.strip()%0A%20%20%20%20%23%20Step%204%3A%20return%20the%20dict%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.1%22)%0A%0A%0A%40app.function%0Adef%20as_pairs(codes%3A%20dict)%20-%3E%20list%3A%0A%20%20%20%20%22%22%22A%20dictionary%20as%20a%20list%20of%20%5Bkey%2C%20value%5D%20pairs%2C%20for%20the%20checks%3A%20the%20order%20counts%2C%20as%20the%20codebook's%20order%20does.%22%22%22%0A%20%20%20%20return%20%5B%5Bkey%2C%20value%5D%20for%20key%2C%20value%20in%20codes.items()%5D%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%204.1%22%2C%20codebook_codes%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22sex%3A%201%20%3D%20Male%3B%202%20%3D%20Female%22%2C%20(%221%20%3D%20Male%3B%202%20%3D%20Female%22%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22mem_fished%3A%20two%20codes%2C%20one%20of%20them%200%2C%20then%20a%20note%22%2C%20(%221%20%3D%20Yes%3B%200%20%3D%20No%3B%20asked%20of%20members%2010%20years%20old%20and%20over%22%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22unit_boats%3A%20an%20'%3D'%20that%20is%20not%20a%20code%22%2C%20(%22blank%20if%20unit_uses_boat%20%3D%200%22%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22mem_education%3A%20a%20pointer%20to%20a%20code%20list%20and%20a%20note%22%2C%20(%22see%20codes%2Feducation.csv%3B%20asked%20of%20members%206%20years%20old%20and%20over%22%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22an%20empty%20cell%2C%20read%20as%20a%20missing%20value%22%2C%20(float(%22nan%22)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22unit_scale%3A%20labels%20with%20numbers%20and%20a%20colon%20in%20them%22%2C%20(%221%20%3D%20Small-scale%3A%20no%20boat%2C%20or%20every%20boat%20under%205.0%20gross%20tons%3B%202%20%3D%20Large-scale%3A%20a%20boat%20of%205.0%20gross%20tons%20or%20more%22%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22codebook%20entries%22%2C%20summarize%3Das_pairs%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Split%20on%20'%3B'%2C%20cut%20each%20part%20at%20its%20first%20'%3D'%2C%20keep%20it%20only%20if%20the%20left%20side%20is%20digits%2C%20and%20make%20the%20code%20an%20int.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20codes%20of%20a%20boat's%20engine%2C%20from%20the%20codebook%0A%20%20%20%20try_it(codebook_codes%2C%20codebook.loc%5B(codebook%5B%22table%22%5D%20%3D%3D%20BOATS)%20%26%20(codebook%5B%22variable%22%5D%20%3D%3D%20%22boat_engine%22)%2C%20%22values%22%5D.iloc%5B0%5D)%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%204.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20*AttributeError%3A%20'float'%20object%20has%20no%20attribute%20'split'*%20%7C%20The%20empty%20cell%20is%20a%20missing%20value%2C%20not%20text.%20%7C%20%60isinstance(values%2C%20str)%60%20first%20%7C%0A%20%20%20%20%7C%20%60%5B%5B'1'%2C%20'Male'%5D%2C%20...%5D%60%2C%20with%20quotes%20%7C%20The%20codes%20are%20kept%20as%20text.%20%7C%20%60int(code)%60%20%7C%0A%20%20%20%20%7C%20%60%5B%5B'%201'%2C%20'Male'%5D%2C%20...%5D%60%20or%20labels%20starting%20with%20a%20space%20%7C%20The%20parts%20were%20not%20stripped.%20%7C%20%60.strip()%60%20on%20both%20sides%20%7C%0A%20%20%20%20%7C%20A%20code%20from%20%60blank%20if%20unit_uses_boat%20%3D%200%60%20%7C%20Every%20part%20with%20%60%3D%60%20was%20kept.%20%7C%20keep%20a%20part%20only%20if%20the%20left%20side%20%60.isdigit()%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20invalid%20literal%20for%20int()*%20%7C%20%60int()%60%20was%20tried%20on%20words.%20%7C%20test%20%60.isdigit()%60%20before%20%60int()%60%20%7C%0A%20%20%20%20%7C%20No%20%600%3A%20'No'%60%20%7C%20The%20test%20was%20%60if%20int(code)%60%2C%20and%200%20counts%20as%20false.%20%7C%20test%20%60.isdigit()%60%2C%20not%20the%20number%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%20Blanks%20and%20invalid%20codes%20are%20not%20the%20same%20thing%0A%0A%20%20%20%20With%20the%20codes%20as%20a%20dictionary%2C%20%60map%60%20puts%20a%20label%20on%20each%20value.%20It%20also%20hides%20the%20difference%20this%20section%20is%20about%3A%20a%20code%20the%20dictionary%20does%20not%20have%20and%20a%20blank%20both%20come%20back%20as%20a%20missing%20label.%20A%20member's%20education%2C%20%60mem_education%60%2C%20has%20both%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20map()%20gives%20a%20missing%20label%20to%20a%20blank%20and%20to%20a%20code%20not%20in%20the%20list%20alike%0A%20%20%20%20_education_labels%20%3D%20dict(zip(education%5B%22code%22%5D%2C%20education%5B%22label%22%5D))%0A%20%20%20%20_labels%20%3D%20members%5B%22mem_education%22%5D.map(_education_labels)%0A%20%20%20%20_blanks%20%3D%20int(members%5B%22mem_education%22%5D.isna().sum())%0A%20%20%20%20_invalid%20%3D%20int((members%5B%22mem_education%22%5D.notna()%20%26%20~members%5B%22mem_education%22%5D.isin(_education_labels)).sum())%0A%20%20%20%20mo.md(f%22%60members%5B'mem_education'%5D.map(labels)%60%20gives%20**%7Bint(_labels.isna().sum())%3A%2C%7D%20missing%20labels**%3A%20%7B_blanks%3A%2C%7D%20blanks%20(members%20under%206%2C%20who%20are%20not%20asked)%20and%20**%7B_invalid%7D**%20codes%20the%20education%20list%20does%20not%20have.%20In%20one%20count%20they%20look%20alike%3B%20an%20editor%20needs%20them%20apart%2C%20because%20the%20blanks%20are%20expected%20and%20the%20invalid%20codes%20are%20errors.%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%20In%20R%2C%20%60case_when%60%20puts%20each%20value%20in%20a%20category%20by%20the%20first%20condition%20it%20meets%2C%20and%20a%20condition%20that%20is%20%60NA%60%20counts%20as%20not%20met.%20pandas%20has%20%60Series.case_when%60%20too%2C%20with%20two%20differences%20an%20R%20user%20trips%20on%3A%0A%0A%20%20%20%20%7C%20%7C%20dplyr%20%60case_when%60%20%7C%20pandas%20%60Series.case_when%60%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20a%20value%20no%20condition%20meets%20%7C%20%60NA%60%2C%20or%20%60.default%60%20%7C%20**the%20value%20of%20the%20Series%20you%20call%20it%20on**%3A%20start%20from%20a%20Series%20that%20holds%20the%20default%20%7C%0A%20%20%20%20%7C%20a%20condition%20that%20is%20missing%20(%60NA%60)%20%7C%20not%20met%20%7C%20**met**%20(pandas%203.1%2C%20measured%20on%20this%20course's%20lock)%20%7C%0A%0A%20%20%20%20The%20second%20difference%20matters%20here.%20The%20survey's%20codes%20are%20nullable%20integers%2C%20so%20%60codes%20%3D%3D%201%60%20is%20%60%3CNA%3E%60%20for%20a%20blank%2C%20and%20%60case_when%60%20puts%20the%20blank%20in%20category%201.%20Build%20conditions%20that%20are%20never%20missing%3A%20%60codes.isin(%5B1%5D)%60%20is%20%60False%60%20for%20a%20blank%2C%20and%20so%20is%20%60(codes%20%3D%3D%201).fillna(False)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20One%20blank%2C%20labeled%20%22Male%22%20by%20case_when%2C%20because%20its%20condition%20is%20%3CNA%3E%3B%20isin()%20gives%20False%20instead%0A%20%20%20%20_codes%20%3D%20pd.Series(%5B1%2C%202%2C%20None%2C%209%5D%2C%20dtype%3D%22Int32%22)%0A%20%20%20%20_start%20%3D%20pd.Series(%5B%22Invalid%20value%22%5D%20*%204)%0A%20%20%20%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22code%22%3A%20_codes%2C%0A%20%20%20%20%20%20%20%20%22codes%20%3D%3D%201%22%3A%20_codes%20%3D%3D%201%2C%0A%20%20%20%20%20%20%20%20%22case_when%20with%20%3D%3D%22%3A%20_start.case_when(%5B(_codes%20%3D%3D%201%2C%20%22Male%22)%2C%20(_codes%20%3D%3D%202%2C%20%22Female%22)%5D)%2C%0A%20%20%20%20%20%20%20%20%22case_when%20with%20isin%22%3A%20_start.case_when(%5B(_codes.isin(%5B1%5D)%2C%20%22Male%22)%2C%20(_codes.isin(%5B2%5D)%2C%20%22Female%22)%2C%20(_codes.isna()%2C%20%22No%20answer%22)%5D)%2C%0A%20%20%20%20%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%204.2.%20A%20frequency%20table%20against%20the%20valid%20codes%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20table%20an%20editor%20reads%20first%20for%20every%20coded%20item.%20Codes%20outside%20the%20list%20are%20the%20errors%3B%20valid%20codes%20with%20no%20answers%20are%20worth%20a%20look%20(a%20skip%20pattern%20that%20never%20fires%2C%20a%20code%20nobody%20was%20trained%20on)%3B%20blanks%20are%20counted%20so%20they%20can%20be%20explained.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20column%20of%20codes%20(with%20blanks)%20and%20the%20valid%20codes%20as%20%60%7Bcode%3A%20label%7D%60%20%E2%86%92%20a%20table%20with%20the%20columns%20%60code%60%2C%20%60label%60%20and%20%60count%60%2C%20and%20these%20rows%2C%20in%20this%20order%3A%0A%0A%20%20%20%201.%20every%20valid%20code%2C%20in%20the%20dictionary's%20order%2C%20with%20its%20label%20and%20its%20count%2C%20**0%20included**%3B%0A%20%20%20%202.%20every%20code%20in%20the%20column%20that%20the%20dictionary%20does%20not%20have%2C%20smallest%20first%2C%20each%20with%20the%20label%20%60Invalid%20value%60%3B%0A%20%20%20%203.%20one%20last%20row%2C%20code%20missing%20(%60None%60)%2C%20label%20%60No%20answer%60%2C%20the%20number%20of%20blanks%20(0%20included).%0A%0A%20%20%20%20%7C%20code%20%7C%20label%20%7C%20count%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%201%20%7C%20Yes%20%7C%201%20%7C%0A%20%20%20%20%7C%200%20%7C%20No%20%7C%202%20%7C%0A%20%20%20%20%7C%208%20%7C%20Don't%20know%20%7C%200%20%7C%0A%20%20%20%20%7C%203%20%7C%20Invalid%20value%20%7C%202%20%7C%0A%20%20%20%20%7C%207%20%7C%20Invalid%20value%20%7C%201%20%7C%0A%20%20%20%20%7C%20*None*%20%7C%20No%20answer%20%7C%201%20%7C%0A%0A%20%20%20%20That%20table%20is%20for%20the%20codes%20%600%2C%201%2C%20blank%2C%207%2C%203%2C%200%2C%203%60%20and%20the%20valid%20codes%20%60%7B1%3A%20%22Yes%22%2C%200%3A%20%22No%22%2C%208%3A%20%22Don't%20know%22%7D%60.%0A%0A%20%20%20%20**Tools%3A**%20%60value_counts()%60%20(it%20leaves%20blanks%20out%20by%20default)%2C%20%60counts.get(code%2C%200)%60%2C%20%60sort_index()%60%2C%20%60isna().sum()%60%2C%20a%20list%20of%20rows%20and%20%60pd.DataFrame(rows%2C%20columns%3D%5B...%5D)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20code_frequency(values%3A%20pd.Series%2C%20valid%3A%20dict)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20frequency%20table%20of%20a%20coded%20column%20against%20its%20valid%20codes%3A%20code%2C%20label%2C%20count%3B%20invalid%20codes%2C%20then%20blanks.%22%22%22%0A%20%20%20%20%23%20Exercise%204.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20counts%20%3D%20values.value_counts()%20(the%20blanks%20are%20left%20out%20of%20it)%0A%20%20%20%20%23%20Step%202%3A%20one%20row%20(code%2C%20label%2C%20count)%20per%20valid%20code%2C%20in%20the%20dict's%20order%3A%20counts.get(code%2C%200)%0A%20%20%20%20%23%20Step%203%3A%20one%20row%20per%20code%20in%20counts%20that%20is%20not%20in%20valid%2C%20smallest%20first%3A%20(code%2C%20%22Invalid%20value%22%2C%20count)%0A%20%20%20%20%23%20Step%204%3A%20a%20last%20row%20(None%2C%20%22No%20answer%22%2C%20the%20number%20of%20missing%20values)%3B%20return%20the%20rows%20as%20a%20DataFrame%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.2%22)%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%204.2%22%2C%20code_frequency%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20codes%200%2C%201%2C%20blank%2C%207%2C%203%2C%200%2C%203%20against%20Yes%2C%20No%20and%20Don't%20know%22%2C%20(pd.Series(%5B0%2C%201%2C%20None%2C%207%2C%203%2C%200%2C%203%5D%2C%20dtype%3D%22Int64%22)%2C%20%7B1%3A%20%22Yes%22%2C%200%3A%20%22No%22%2C%208%3A%20%22Don't%20know%22%7D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22every%20member's%20sex%22%2C%20(members%5B%22mem_sex%22%5D%2C%20%7B1%3A%20%22Male%22%2C%202%3A%20%22Female%22%7D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22every%20member's%20education%2C%20blanks%20included%22%2C%20(members%5B%22mem_education%22%5D%2C%20dict(zip(education%5B%22code%22%5D%2C%20education%5B%22label%22%5D))))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22every%20catch%20row's%20species%22%2C%20(catch%5B%22catch_species%22%5D%2C%20dict(zip(species%5B%22code%22%5D%2C%20species%5B%22name%22%5D))))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22columns%20of%20codes%22%2C%20summarize%3Dlambda%20table%3A%20table%5B%5B%22code%22%2C%20%22label%22%2C%20%22count%22%5D%5D.reset_index(drop%3DTrue)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Every%20valid%20code%20in%20the%20dict's%20order%20(0%20if%20unused)%2C%20then%20the%20codes%20not%20in%20the%20dict%20smallest%20first%2C%20then%20one%20No%20answer%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%20the%20members'%20sex%20against%20the%20codebook's%20two%20codes%0A%20%20%20%20try_it(code_frequency%2C%20members%5B%22mem_sex%22%5D%2C%20%7B1%3A%20%22Male%22%2C%202%3A%20%22Female%22%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%204.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%20No%20row%20for%20%608%20Don't%20know%60%20%7C%20The%20rows%20were%20built%20from%20the%20counts%2C%20so%20an%20unused%20code%20has%20none.%20%7C%20loop%20over%20%60valid%60%20and%20use%20%60counts.get(code%2C%200)%60%20%7C%0A%20%20%20%20%7C%20One%20%60Invalid%20value%60%20row%20with%20the%20total%20%7C%20The%20invalid%20codes%20were%20added%20up.%20%7C%20one%20row%20per%20invalid%20code%20%7C%0A%20%20%20%20%7C%20No%20%60No%20answer%60%20row%2C%20or%20a%20blank%20listed%20as%20an%20invalid%20code%20%7C%20%60value_counts(dropna%3DFalse)%60%20put%20the%20blanks%20among%20the%20codes.%20%7C%20%60value_counts()%60%2C%20and%20blanks%20counted%20with%20%60isna().sum()%60%20%7C%0A%20%20%20%20%7C%20The%20invalid%20codes%20as%203%2C%207%20in%20the%20wrong%20order%2C%20or%207%20first%20%7C%20They%20are%20in%20the%20order%20of%20their%20counts.%20%7C%20%60counts.sort_index()%60%20%7C%0A%20%20%20%20%7C%20%60count%60%20of%200%20for%20every%20valid%20code%20%7C%20The%20dictionary's%20keys%20are%20text%20and%20the%20codes%20numbers.%20%7C%20the%20keys%20must%20be%20integers%2C%20as%20Exercise%204.1%20gives%20them%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%20Every%20coded%20item%20at%20once%0A%0A%20%20%20%20The%20two%20functions%20together%20read%20the%20whole%20codebook.%20The%20cell%20below%20takes%20every%20item%20of%20the%20six%20tables%20whose%20codes%20the%20codebook%20lists%2C%20and%20the%20three%20whose%20codes%20are%20in%20a%20file%20(relationship%2C%20education%2C%20species)%2C%20makes%20each%20one's%20frequency%20table%20and%20keeps%20three%20numbers%3A%20answers%20with%20a%20valid%20code%2C%20invalid%20codes%20and%20blanks.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Each%20coded%20item%20of%20the%20six%20tables%3A%20its%20frequency%20table%20against%20the%20codebook%2C%20summed%20up%20in%20one%20row%0A%20%20%20%20def%20_summary()%3A%0A%20%20%20%20%20%20%20%20if%20attempt(codebook_codes%2C%20%221%20%3D%20a%22)%20is%20NOT_YET%20or%20attempt(code_frequency%2C%20pd.Series(%5B1%5D)%2C%20%7B1%3A%20%22a%22%7D)%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercises%204.1%20and%204.2%22)%0A%20%20%20%20%20%20%20%20items%20%3D%20%5B(MEMBERS%2C%20%22mem_relation%22%2C%20dict(zip(relationship%5B%22code%22%5D%2C%20relationship%5B%22label%22%5D)))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(MEMBERS%2C%20%22mem_education%22%2C%20dict(zip(education%5B%22code%22%5D%2C%20education%5B%22label%22%5D)))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(CATCH%2C%20%22catch_species%22%2C%20dict(zip(species%5B%22code%22%5D%2C%20species%5B%22name%22%5D)))%5D%0A%20%20%20%20%20%20%20%20for%20entry%20in%20codebook%5Bcodebook%5B%22table%22%5D.isin(list(tables))%5D.itertuples(index%3DFalse)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20valid%20%3D%20codebook_codes(entry.values)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20valid%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20items.append((entry.table%2C%20entry.variable%2C%20valid))%0A%20%20%20%20%20%20%20%20rows%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20table_name%2C%20variable%2C%20valid%20in%20items%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20freq%20%3D%20code_frequency(tables%5Btable_name%5D%5Bvariable%5D%2C%20valid)%0A%20%20%20%20%20%20%20%20%20%20%20%20rows.append(%7B%22table%22%3A%20table_name%2C%20%22variable%22%3A%20variable%2C%20%22valid%20codes%22%3A%20len(valid)%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%22valid%20answers%22%3A%20int(freq.loc%5Bfreq%5B%22code%22%5D.notna()%20%26%20freq%5B%22code%22%5D.isin(list(valid))%2C%20%22count%22%5D.sum())%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%22Invalid%20value%22%3A%20int(freq.loc%5Bfreq%5B%22label%22%5D%20%3D%3D%20%22Invalid%20value%22%2C%20%22count%22%5D.sum())%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%22No%20answer%22%3A%20int(freq.loc%5Bfreq%5B%22label%22%5D%20%3D%3D%20%22No%20answer%22%2C%20%22count%22%5D.iloc%5B0%5D)%7D)%0A%20%20%20%20%20%20%20%20out%20%3D%20pd.DataFrame(rows).sort_values(%5B%22Invalid%20value%22%2C%20%22No%20answer%22%5D%2C%20ascending%3DFalse).reset_index(drop%3DTrue)%0A%20%20%20%20%20%20%20%20wrong%20%3D%20out%5Bout%5B%22Invalid%20value%22%5D%20%3E%200%5D%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(out)%7D%20coded%20items**%3B%20**%7Blen(wrong)%7D**%20have%20codes%20the%20codebook%20does%20not%20have%3A%20%7B'%2C%20'.join('%60'%20%2B%20v%20%2B%20'%60'%20for%20v%20in%20wrong%5B'variable'%5D)%7D.%20The%20items%20with%20blanks%20follow%20them.%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20out%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_summary()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20invalid%20codes%20are%20errors%20whoever%20answered.%20The%20blanks%20are%20not%20all%20errors%3A%20%60mem_education%60%20is%20asked%20of%20members%206%20years%20old%20and%20over%2C%20%60mem_fished%60%20of%20members%2010%20and%20over%2C%20and%20the%20four%20roles%20only%20of%20those%20who%20fished%2C%20so%20a%20child's%20blank%20there%20is%20the%20questionnaire%20working.%20A%20frequency%20table%20cannot%20tell%20a%20skip%20from%20a%20missed%20answer%2C%20because%20it%20does%20not%20know%20who%20was%20asked.%20A%20rule%20can%3A%20section%202%20writes%20rules%20that%20say%20which%20rows%20they%20are%20about.%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.accordion(%7B%22The%20R%20way%2C%20and%20what%20changes%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20In%20R%2C%20a%20recode%20and%20a%20count%20are%20one%20pipe%3A%0A%0A%20%20%20%20%60%60%60r%0A%20%20%20%20members%20%7C%3E%0A%20%20%20%20%20%20mutate(sex%20%3D%20case_when(is.na(mem_sex)%20~%20%22No%20answer%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%20mem_sex%20%3D%3D%201%20~%20%22Male%22%2C%20mem_sex%20%3D%3D%202%20~%20%22Female%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%20TRUE%20~%20%22Invalid%20value%22))%20%7C%3E%0A%20%20%20%20%20%20count(sex)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20-%20**The%20valid%20codes%20are%20typed%20into%20the%20script.**%20Here%20they%20come%20from%20the%20codebook%20(Exercise%204.1)%2C%20so%20a%20code%20added%20to%20the%20codebook%20reaches%20every%20table%20at%20once.%0A%20%20%20%20-%20**Each%20invalid%20code%20keeps%20its%20row.**%20%60TRUE%20~%20%22Invalid%20value%22%60%20folds%200%20and%208%20into%20one%20count%3B%20Exercise%204.2%20lists%20them%20apart%2C%20so%20the%20editor%20knows%20what%20to%20look%20for.%0A%20%20%20%20-%20**%60case_when%60%20differs.**%20In%20dplyr%20a%20missing%20condition%20is%20not%20met%3B%20in%20pandas%20it%20is.%20Write%20%60isin(%5B1%5D)%60%2C%20or%20test%20%60isna()%60%20first%20as%20the%20R%20code%20does%2C%20and%20the%20difference%20never%20bites.%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%202%20%C2%B7%20Range%20and%20cross-item%20consistency%20rules%0A%0A%20%20%20%20**The%20problem.**%20Every%20code%20in%20a%20row%20can%20be%20valid%20while%20the%20row%20is%20wrong.%20An%20age%20of%20130%20is%20a%20number%2C%20but%20no%20one%20is%20130%20years%20old.%20A%207-year-old%20who%20runs%20a%20fishing%20unit%20has%20a%20valid%20age%20and%20a%20valid%20flag%2C%20and%20the%20survey%20says%20a%20member%20who%20runs%20a%20unit%20is%2016%20or%20older.%20A%20unit's%20scale%20(%60unit_scale%60%2C%202%20for%20large-scale)%20can%20be%20a%20valid%20code%20that%20its%20own%20boats%20contradict.%20Each%20of%20these%20is%20a%20**rule**%3A%20a%20statement%20a%20correct%20row%20makes%20true.%20Three%20kinds%20cover%20most%20of%20the%20codebook's%3A%0A%0A%20%20%20%20%7C%20kind%20%7C%20what%20it%20says%20%7C%20in%20this%20survey%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20**range**%20%7C%20a%20value%20lies%20between%20two%20bounds%20%7C%20an%20age%20is%20between%200%20and%20110%20(M2)%20%7C%0A%20%20%20%20%7C%20**scope**%20(skip)%20%7C%20an%20item%20is%20answered%20by%20the%20members%20it%20is%20asked%20of%2C%20and%20only%20by%20them%20%7C%20the%20fishing%20items%20only%20from%2010%20years%20old%20(M3)%3B%20education%20from%206%20(M5)%20%7C%0A%20%20%20%20%7C%20**cross-item**%20%7C%20two%20items%2C%20in%20one%20row%20or%20in%20two%20tables%2C%20agree%20%7C%20a%20member%20who%20runs%20a%20unit%20is%2016%20or%20older%20(M4)%3B%20a%20boat%20with%20no%20engine%20is%20under%205.0%20gross%20tons%20(B2)%3B%20a%20household's%20count%20of%20members%20equals%20its%20member%20rows%20(H1)%20%7C%0A%0A%20%20%20%20%23%23%23%20Range%20rules%2C%20and%20what%20a%20blank%20does%20to%20them%0A%0A%20%20%20%20%60between(0%2C%20110)%60%20is%20%60True%60%20inside%20the%20bounds%2C%20both%20ends%20included.%20On%20a%20nullable%20column%20it%20is%20%60%3CNA%3E%60%20for%20a%20blank%2C%20and%20a%20filter%20treats%20%60%3CNA%3E%60%20as%20%22leave%20the%20row%20out%22.%20So%20the%20rule's%20failures%2C%20written%20the%20obvious%20way%2C%20lose%20the%20blank%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20A%20blank%20age%3A%20between()%20says%20%3CNA%3E%2C%20and%20the%20filter%20of%20failures%20leaves%20it%20out%0A%20%20%20%20_ages%20%3D%20pd.Series(%5B30%2C%20None%2C%20130%5D%2C%20dtype%3D%22Int64%22%2C%20name%3D%22mem_age%22)%0A%20%20%20%20_ok%20%3D%20_ages.between(0%2C%20110)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20pd.DataFrame(%7B%22mem_age%22%3A%20_ages%2C%20%22between(0%2C%20110)%22%3A%20_ok%2C%20%22fillna(False)%22%3A%20_ok.fillna(False)%7D)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22%60ages%5B~ok%5D%60%20gives%20**%7B_ages%5B~_ok%5D.tolist()%7D**%3A%20the%20blank%20is%20not%20a%20failure.%20%60ages%5B~ok.fillna(False)%5D%60%20gives%20**%7B_ages%5B~_ok.fillna(False)%5D.tolist()%7D**%3A%20an%20age%20is%20asked%20of%20every%20member%2C%20so%20a%20blank%20one%20fails%20the%20rule%20too.%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20dplyr's%20%60filter%60%20does%20the%20same%3A%20it%20drops%20a%20row%20whose%20condition%20is%20%60NA%60.%20Whether%20a%20blank%20passes%20or%20fails%20is%20the%20rule's%20decision%2C%20and%20%60fillna(False)%60%20writes%20it%20down%3A%20**a%20value%20the%20rule%20needs%20and%20does%20not%20have%20fails.**%20This%20module's%20rules%20all%20decide%20that%20way.%0A%0A%20%20%20%20The%20survey's%20range%20rule%20on%20age%2C%20and%20its%20rule%20on%20the%20age%20of%20a%20member%20who%20runs%20a%20unit%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20M2%3A%20an%20age%20between%200%20and%20110.%20M4%3A%20a%20member%20who%20runs%20a%20unit%20(mem_role_operator%20%3D%201)%20is%2016%20or%20older.%0A%20%20%20%20_age_ok%20%3D%20members%5B%22mem_age%22%5D.between(0%2C%20110).fillna(False)%0A%20%20%20%20_operators%20%3D%20members%5Bmembers%5B%22mem_role_operator%22%5D.eq(1).fillna(False)%5D%0A%20%20%20%20_young%20%3D%20_operators%5B~_operators%5B%22mem_age%22%5D.ge(16).fillna(False)%5D%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**M2%3A**%20%7Bint((~_age_ok).sum())%7D%20members%20fail%2C%20ages%20%7Bsorted(members.loc%5B~_age_ok%2C%20'mem_age'%5D.tolist())%7D.%20**M4%3A**%20%7Blen(_operators)%3A%2C%7D%20members%20run%20a%20unit%3B%20%7Blen(_young)%7D%20of%20them%20are%20under%2016%3A%22)%2C%0A%20%20%20%20%20%20%20%20_young%5B%5B*HOUSEHOLD%2C%20%22mem_line%22%2C%20%22mem_age%22%2C%20%22mem_fished%22%2C%20%22mem_role_operator%22%2C%20%22mem_units_run%22%5D%5D%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Who%20was%20asked%3A%20a%20rule's%20scope%0A%0A%20%20%20%20M4%20is%20not%20about%20every%20member%3A%20it%20is%20about%20**members%20who%20run%20a%20unit**.%20That%20part%2C%20*the%20rows%20the%20rule%20is%20about*%2C%20is%20its%20**scope**%3B%20the%20rest%2C%20*what%20must%20be%20true%20of%20them*%2C%20is%20its%20**condition**.%20Writing%20the%20two%20apart%20is%20what%20lets%20a%20rule%20respect%20the%20questionnaire's%20skips%3A%0A%0A%20%20%20%20%7C%20rule%20%7C%20scope%3A%20the%20rows%20it%20is%20about%20%7C%20condition%3A%20what%20must%20hold%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20M3%20%7C%20members%20under%2010%20%7C%20the%20fishing%20items%20%60mem_fished%60%20and%20the%20four%20%60mem_role_%60%20flags%20are%20all%20blank%20%7C%0A%20%20%20%20%7C%20M4%20%7C%20members%20who%20run%20a%20unit%3A%20%60mem_role_operator%60%20is%201%20%7C%20%60mem_age%60%20is%2016%20or%20more%20%7C%0A%20%20%20%20%7C%20M5%20%7C%20members%206%20years%20old%20and%20over%20%7C%20%60mem_education%60%20is%20a%20code%20in%20the%20education%20list%20%7C%0A%0A%20%20%20%20A%20blank%20education%20for%20a%203-year-old%20is%20outside%20M5's%20scope%2C%20so%20it%20is%20no%20failure%3B%20a%20blank%20education%20for%20a%2030-year-old%20is%20inside%20it%2C%20and%20fails%2C%20by%20the%20decision%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20M5%20with%20its%20scope%3A%20the%20blanks%20of%20under-6s%20are%20not%20failures%3B%20a%20blank%20or%20an%20unlisted%20code%20from%206%20up%20is%0A%20%20%20%20_scope%20%3D%20members%5Bmembers%5B%22mem_age%22%5D%20%3E%3D%206%5D%0A%20%20%20%20_fails%20%3D%20_scope%5B~_scope%5B%22mem_education%22%5D.isin(rule_values%5B%22education%22%5D)%5D%0A%20%20%20%20_under_6%20%3D%20members%5Bmembers%5B%22mem_age%22%5D%20%3C%206%5D%0A%20%20%20%20mo.md(f%22M5's%20scope%20holds%20%7Blen(_scope)%3A%2C%7D%20members%3B%20**%7Blen(_fails)%7D%20fail**%3A%20%7Bint(_fails%5B'mem_education'%5D.notna().sum())%7D%20with%20a%20code%20the%20education%20list%20does%20not%20have%2C%20%7Bint(_fails%5B'mem_education'%5D.isna().sum())%7D%20with%20a%20blank.%20Outside%20the%20scope%2C%20%7Bint(_under_6%5B'mem_education'%5D.isna().sum())%3A%2C%7D%20children%20under%206%20have%20a%20blank%20education%2C%20as%20the%20questionnaire%20asks.%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%20One%20row%2C%20several%20items%3A%20a%20boat's%20tonnage%0A%0A%20%20%20%20A%20boat's%20gross%20tonnage%2C%20%60boat_tonnage%60%2C%20is%20what%20the%20capture%20program%20stored%3A%20computed%20from%20the%20boat's%20length%2C%20beam%20and%20depth%20in%20the%20unit%20it%20was%20measured%20in%2C%20or%20copied%20from%20the%20registration%20papers%2C%20by%20%60boat_measured_in%60.%20B2%20reads%20it%20beside%20a%20second%20item%20of%20the%20same%20row%2C%20the%20engine%3A%20a%20boat%20with%20no%20engine%20(%60boat_engine%60%20%3D%200)%20is%20under%205.0%20gross%20tons.%20Either%20item%20can%20be%20the%20wrong%20one%3B%20section%204%20decides%20which%20the%20edit%20changes.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20B2%20reads%20two%20items%20of%20one%20row%3A%20the%20engine%20and%20the%20stored%20tonnage%0A%20%20%20%20_b2%20%3D%20boats%5Bboats%5B%22boat_engine%22%5D.eq(0)%20%26%20~boats%5B%22boat_tonnage%22%5D.lt(5).fillna(False)%5D%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**B2%2C%20a%20boat%20with%20no%20engine%20(%60boat_engine%60%20%3D%200)%20is%20under%205.0%20gross%20tons%3A**%20%7Bint(boats%5B'boat_engine'%5D.eq(0).sum())%3A%2C%7D%20boats%20have%20no%20engine%3B%20**%7Blen(_b2)%7D**%20are%205.0%20gross%20tons%20or%20more.%22)%2C%0A%20%20%20%20%20%20%20%20_b2%5B%5B*UNIT_KEY%2C%20%22boat_hull%22%2C%20%22boat_engine%22%2C%20%22boat_engine_hp%22%2C%20%22boat_measured_in%22%2C%20%22boat_length%22%2C%20%22boat_beam%22%2C%20%22boat_depth%22%2C%20%22boat_tonnage%22%5D%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%20B3%20asks%20whether%20the%20stored%20tonnage%20is%20right.%20The%20survey's%20manual%20sets%20it%20as%20length%20%C3%97%20beam%20%C3%97%20depth%20%C3%97%200.212%20for%20a%20boat%20measured%20in%20meters%20(%60boat_measured_in%60%20%3D%202)%2C%20%C3%97%200.006%20for%20one%20measured%20in%20feet%20(%60boat_measured_in%60%20%3D%203)%2C%20and%2C%20for%20a%20boat%20whose%20size%20comes%20from%20its%20registration%20papers%20(%60boat_measured_in%60%20%3D%201)%2C%20the%20papers'%20figure%2C%20%60boat_tonnage_reported%60.%20Such%20a%20boat%20has%20no%20dimensions%20to%20compute%20from.%0A%0A%20%20%20%20%23%23%23%20Exercise%204.3.%20The%20tonnage%20B3%20expects%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20stored%20computation%20is%20only%20as%20right%20as%20the%20program%20that%20wrote%20it.%20Computing%20it%20again%20from%20the%20answers%20it%20came%20from%20is%20the%20check%2C%20and%2C%20when%20they%20disagree%2C%20the%20correction%20(section%205).%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20boats%20table%20%E2%86%92%20a%20Series%2C%20one%20value%20per%20boat%20in%20the%20boats'%20order%3A%20the%20tonnage%20B3%20expects%2C%20by%20how%20the%20boat's%20size%20is%20known%2C%20and%20**missing**%20for%20a%20boat%20with%20a%20dimension%20missing%20or%20with%20no%20%60boat_measured_in%60%3A%0A%0A%20%20%20%20%7C%20boat_measured_in%20%7C%20boat_length%2C%20beam%2C%20depth%20%7C%20boat_tonnage_reported%20%7C%20expected%20tonnage%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%202%20(meters)%20%7C%2010.0%2C%201.2%2C%200.6%20%7C%20%7C%2010.0%20%C3%97%201.2%20%C3%97%200.6%20%C3%97%200.212%20%3D%201.5264%20%7C%0A%20%20%20%20%7C%203%20(feet)%20%7C%2030%2C%204%2C%202%20%7C%20%7C%2030%20%C3%97%204%20%C3%97%202%20%C3%97%200.006%20%3D%201.44%20%7C%0A%20%20%20%20%7C%201%20(papers)%20%7C%20%7C%204.2%20%7C%204.2%20%7C%0A%20%20%20%20%7C%202%20(meters)%20%7C%208.0%2C%201.1%2C%20*blank*%20%7C%20%7C%20*missing*%20%7C%0A%20%20%20%20%7C%20*blank*%20%7C%209.0%2C%201.2%2C%200.6%20%7C%20%7C%20*missing*%20%7C%0A%0A%20%20%20%20**Tools%3A**%20column%20arithmetic%2C%20and%20%60case_when%60%20started%20from%20a%20Series%20of%20missing%20values%20(%60pd.Series(np.nan%2C%20index%3Dboats.index)%60)%2C%20with%20conditions%20built%20by%20%60isin%60%2C%20which%20section%201%20showed%20never%20to%20be%20missing.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20tonnage_expected(boats%3A%20pd.DataFrame)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Each%20boat's%20gross%20tonnage%20as%20rule%20B3%20states%20it%3A%20L%20%C3%97%20B%20%C3%97%20D%20%C3%97%200.212%20in%20meters%2C%20%C3%97%200.006%20in%20feet%2C%20the%20papers'%20figure%0A%20%20%20%20for%20a%20boat%20whose%20size%20comes%20from%20its%20papers%3B%20missing%20otherwise.%22%22%22%0A%20%20%20%20%23%20Exercise%204.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20volume%20%3D%20boat_length%20*%20boat_beam%20*%20boat_depth%2C%20for%20every%20boat%0A%20%20%20%20%23%20Step%202%3A%20start%20from%20a%20Series%20of%20missing%20values%20(np.nan%2C%20index%3Dboats.index)%20and%20case_when%3A%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20volume%20*%200.212%20where%20boat_measured_in%20is%202%2C%20volume%20*%200.006%20where%20it%20is%203%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20boat_tonnage_reported%20where%20it%20is%201%20(build%20each%20condition%20with%20isin)%3B%20return%20it%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.3%22)%0A%0A%0A%40app.function%0Adef%20boats_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Five%20boats%3A%20in%20meters%2C%20in%20feet%2C%20sized%20from%20papers%2C%20in%20meters%20with%20a%20depth%20missing%2C%20and%20with%20no%20unit%20of%20measure.%0A%20%20%20%20The%20rows%20of%20Exercise%204.3's%20example%20table%2C%20used%20by%20its%20check.%22%22%22%0A%20%20%20%20columns%20%3D%20%5B%22boat_measured_in%22%2C%20%22boat_length%22%2C%20%22boat_beam%22%2C%20%22boat_depth%22%2C%20%22boat_tonnage_reported%22%5D%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(2%2C%2010.0%2C%201.2%2C%200.6%2C%20None)%2C%0A%20%20%20%20%20%20%20%20(3%2C%2030.0%2C%204.0%2C%202.0%2C%20None)%2C%0A%20%20%20%20%20%20%20%20(1%2C%20None%2C%20None%2C%20None%2C%204.2)%2C%0A%20%20%20%20%20%20%20%20(2%2C%208.0%2C%201.1%2C%20None%2C%20None)%2C%0A%20%20%20%20%20%20%20%20(None%2C%209.0%2C%201.2%2C%200.6%2C%20None)%2C%0A%20%20%20%20%5D%0A%20%20%20%20return%20pd.DataFrame(rows%2C%20columns%3Dcolumns).astype(%7B%22boat_measured_in%22%3A%20%22Int32%22%7D%20%7C%20%7Bc%3A%20%22float64%22%20for%20c%20in%20columns%5B1%3A%5D%7D)%0A%0A%0A%40app.function%0Adef%20as_values(result%2C%20digits%3A%20int%20%3D%204)%20-%3E%20list%3A%0A%20%20%20%20%22%22%22A%20column's%20values%20in%20order%2C%20rounded%2C%20for%20the%20checks%3A%20its%20name%20and%20index%20don't%20count.%22%22%22%0A%20%20%20%20return%20%5BNone%20if%20pd.isna(value)%20else%20round(float(value)%2C%20digits)%20for%20value%20in%20pd.Series(result).tolist()%5D%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%204.3%22%2C%20tonnage_expected%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22five%20boats%3A%20meters%2C%20feet%2C%20papers%2C%20a%20depth%20missing%2C%20no%20unit%20of%20measure%22%2C%20(boats_fixture()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22every%20boat%20of%20the%20survey%22%2C%20(boats%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20boats%20of%20region%2004%22%2C%20(boats%5Bboats%5B%22region_code%22%5D%20%3D%3D%20%2204%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%20of%20boats%22%2C%20summarize%3Das_values%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Volume%20%C3%97%200.212%20where%20boat_measured_in%20is%202%2C%20%C3%97%200.006%20where%20it%20is%203%2C%20the%20papers'%20figure%20where%20it%20is%201%2C%20missing%20otherwise%3B%20build%20the%20conditions%20with%20isin.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20expected%20tonnage%20of%20the%20first%20boats%0A%20%20%20%20try_it(tonnage_expected%2C%20boats.head(8))%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%204.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%20Missing%20for%20the%20boat%20sized%20from%20its%20papers%20%7C%20The%20papers'%20case%20is%20not%20there%3B%20B3%20expects%20%60boat_tonnage_reported%60%20for%20it.%20%7C%20a%20third%20condition%2C%20%60isin(%5B1%5D)%60%20%7C%0A%20%20%20%20%7C%20A%20value%20for%20the%20boat%20measured%20in%20feet%20that%20is%20about%2035%20times%20too%20large%20%7C%20The%20meters%20factor%20used%20on%20feet%2C%20or%20the%20other%20way%20round.%20%7C%200.212%20for%20meters%2C%200.006%20for%20feet%20%7C%0A%20%20%20%20%7C%20A%20value%20for%20the%20boat%20with%20no%20%60boat_measured_in%60%20%7C%20%60case_when%60%20was%20started%20from%20the%20meters%20value%2C%20or%20a%20condition%20used%20%60%3D%3D%60%2C%20whose%20%60%3CNA%3E%60%20counts%20as%20met.%20%7C%20start%20from%20%60np.nan%60%3B%20build%20conditions%20with%20%60isin%60%20%7C%0A%20%20%20%20%7C%20Every%20value%20missing%20%7C%20The%20conditions%20test%201%2C%202%2C%203%20against%20the%20wrong%20codes%3A%201%20is%20the%20papers%2C%202%20meters%2C%203%20feet.%20%7C%20the%20codebook's%20%60boat_measured_in%60%20%7C%0A%20%20%20%20%7C%20*ValueError*%20about%20lengths%20%7C%20A%20list%20was%20given%20to%20%60case_when%60%20instead%20of%20%60(condition%2C%20value)%60%20pairs.%20%7C%20%60%5B(condition%2C%20value)%2C%20...%5D%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%20Equal%2C%20within%20a%20tolerance%0A%0A%20%20%20%20The%20capture%20program%20stored%20each%20tonnage%20rounded%20to%20hundredths%2C%20so%20a%20correct%20value%20and%20its%20recomputation%20differ%20in%20the%20third%20decimal.%20Testing%20them%20with%20%60%3D%3D%60%20calls%20those%20boats%20wrong.%20A%20rule%20on%20computed%20numbers%20compares%20them%20**within%20a%20tolerance**%3A%20%60abs(boat_tonnage%20-%20expected)%20%3C%3D%200.01%60%2C%20a%20hundredth%20of%20a%20gross%20ton%2C%20as%20B3's%20own%20words%20set%20it%3A%20far%20below%20any%20difference%20that%20matters%20and%20at%20least%20as%20large%20as%20any%20rounding%20to%20hundredths.%20The%20setup%20cell%20keeps%20the%20tolerance%20in%20%60rule_values%60%2C%20beside%20the%20code%20lists%2C%20so%20a%20rule%20and%20an%20edit%20read%20the%20same%20number.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20B3%20with%20%3D%3D%20and%20with%20a%20tolerance%2C%20on%20every%20boat%0A%20%20%20%20def%20_tolerance()%3A%0A%20%20%20%20%20%20%20%20expected%20%3D%20attempt(tonnage_expected%2C%20boats)%0A%20%20%20%20%20%20%20%20if%20expected%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercise%204.3%22)%0A%20%20%20%20%20%20%20%20exact%20%3D%20int((~(boats%5B%22boat_tonnage%22%5D%20%3D%3D%20expected).fillna(False)).sum())%0A%20%20%20%20%20%20%20%20close%20%3D%20int((~((boats%5B%22boat_tonnage%22%5D%20-%20expected).abs()%20%3C%3D%20rule_values%5B%22tolerance%22%5D).fillna(False)).sum())%0A%20%20%20%20%20%20%20%20return%20mo.md(f%22Of%20%7Blen(boats)%3A%2C%7D%20boats%2C%20**%7Bexact%3A%2C%7D**%20fail%20B3%20with%20%60%3D%3D%60%3A%20rounding%20to%20hundredths.%20Within%20the%20tolerance%2C%20**%7Bclose%7D**%20fail%3A%20their%20stored%20tonnage%20is%20not%20what%20their%20dimensions%20give.%22)%0A%0A%20%20%20%20_tolerance()%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%20Across%20tables%3A%20bring%20the%20other%20table's%20fact%20in%20as%20a%20column%0A%0A%20%20%20%20H1%20compares%20a%20household's%20count%20of%20members%20(%60hh_roster_members%60%2C%20in%20the%20household%20table)%20with%20its%20rows%20in%20the%20member%20table.%20U3%20compares%20a%20unit's%20scale%20(%60unit_scale%60)%20with%20its%20boats'%20tonnage%2C%20in%20the%20boat%20table.%20A%20rule%20of%20this%20kind%20needs%20two%20tables%2C%20and%20a%20rule%20that%20reads%20two%20tables%20cannot%20be%20written%20as%20a%20condition%20on%20one.%0A%0A%20%20%20%20The%20way%20out%20is%20a%20step%20before%20the%20rule%3A%20**compute%20the%20other%20table's%20fact%20and%20add%20it%20to%20the%20rule's%20table%20as%20a%20column**.%20The%20household%20table%20gets%20%60member_rows%60%2C%20the%20unit%20table%20gets%20%60largest_tonnage%60%2C%20and%20each%20rule%20is%20again%20a%20condition%20on%20one%20row%20of%20one%20table.%20This%20is%20what%20lets%20section%203%20write%20every%20rule%20as%20one%20line%20of%20data.%0A%0A%20%20%20%20The%20household's%20count%20comes%20from%20%60member_rows%60%2C%20given%20in%20section%203%20beside%20the%20rules%3A%20it%20counts%20each%20household's%20rows%20in%20the%20member%20table%2C%20by%20region%20and%20household%2C%20and%20joins%20the%20count%20to%20the%20households%20with%20a%20left%20join%2C%20so%20a%20household%20with%20no%20member%20rows%20keeps%20its%20row%20and%20gets%200.%20The%20stretch%20S1%2C%20%5Bprocessing%5D(%2F%3Ffile%3D36_stretch_processing.py)%2C%20has%20it%20as%20an%20exercise.%20The%20unit's%20largest%20boat%20is%20the%20exercise%20below.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20rows_in_order(table%3A%20pd.DataFrame%2C%20columns%3A%20list%20%7C%20None%20%3D%20None)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20table%20in%20one%20shape%20for%20the%20checks%3A%20these%20columns%20(all%20if%20None)%2C%20rows%20sorted%20by%20every%20column%2C%20numbered%20from%200.%0A%20%20%20%20So%20a%20right%20answer%20passes%20whatever%20order%20its%20rows%20came%20in.%22%22%22%0A%20%20%20%20table%20%3D%20table%20if%20columns%20is%20None%20else%20table%5Bcolumns%5D%0A%20%20%20%20return%20table.sort_values(list(table.columns)%2C%20na_position%3D%22last%22).reset_index(drop%3DTrue)%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%204.4.%20A%20unit's%20scale%20from%20its%20boats%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20unit's%20scale%20(%60unit_scale%60)%20decides%20whether%20it%20is%20counted%20with%20the%20small-scale%20or%20the%20large-scale%20units%2C%20the%20first%20split%20of%20every%20fisheries%20table.%20U3%20says%20the%20scale%20follows%20the%20boats%3A%202%20if%20any%20boat%20is%205.0%20gross%20tons%20or%20more%2C%201%20otherwise.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20unit%20table%20and%20the%20boat%20table%20%E2%86%92%20the%20unit%20table%2C%20its%20rows%20unchanged%2C%20with%20two%20new%20columns%3A%0A%0A%20%20%20%20-%20**%60largest_tonnage%60**%3A%20the%20largest%20%60boat_tonnage%60%20among%20the%20unit's%20boats%3B%20missing%20for%20a%20unit%20with%20no%20boat%3B%0A%20%20%20%20-%20**%60scale_by_boats%60**%3A%202%20where%20%60largest_tonnage%60%20is%205.0%20**or%20more**%2C%201%20otherwise%2C%20including%20a%20unit%20with%20no%20boat.%0A%0A%20%20%20%20A%20unit%20is%20named%20by%20%60UNIT_KEY%60%2C%20its%20region%2C%20household%20and%20two%20key%20parts%3A%20%60region_code%60%2C%20%60hh_id%60%2C%20%60operator_line%60%2C%20%60unit_no%60.%0A%0A%20%20%20%20%7C%20region_code%20%7C%20hh_id%20%7C%20%E2%80%A6%20%7C%20unit_scale%20%7C%20boats'%20tonnage%20%7C%20largest_tonnage%20%7C%20scale_by_boats%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%3A--%7C--%3A%7C%3A--%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%2001%20%7C%205300%20%7C%20%E2%80%A6%20%7C%201%20%7C%201.2%2C%200.8%20%7C%201.2%20%7C%201%20%7C%0A%20%20%20%20%7C%2004%20%7C%205300%20%7C%20%E2%80%A6%20%7C%201%20%7C%205.5%20%7C%205.5%20%7C%202%20%7C%0A%20%20%20%20%7C%2001%20%7C%205301%20%7C%20%E2%80%A6%20%7C%201%20%7C%20*no%20boat*%20%7C%20*missing*%20%7C%201%20%7C%0A%20%20%20%20%7C%2001%20%7C%205302%20%7C%20%E2%80%A6%20%7C%202%20%7C%205.0%20%7C%205.0%20%7C%202%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60groupby(UNIT_KEY)%5B%22boat_tonnage%22%5D.max()%60%2C%20a%20left%20%60merge%60%20with%20%60validate%3D%22many_to_one%22%60%2C%20and%20%60np.where(condition%2C%202%2C%201)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20scale_from_boats(units%3A%20pd.DataFrame%2C%20boats%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20unit%20table%20with%20largest_tonnage%20(its%20boats'%20largest%20boat_tonnage%3B%20missing%20if%20none)%20and%20scale_by_boats%20(2%20if%0A%20%20%20%205.0%20or%20more%2C%20else%201).%22%22%22%0A%20%20%20%20%23%20Exercise%204.4%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20largest%20tonnage%20of%20each%20unit's%20boats%3A%20group%20boats%20by%20UNIT_KEY%2C%20%5B%22boat_tonnage%22%5D.max()%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20name%20it%20%22largest_tonnage%22%20and%20reset%20the%20index%0A%20%20%20%20%23%20Step%202%3A%20left-join%20it%20onto%20the%20units%20on%20UNIT_KEY%2C%20with%20validate%3D%22many_to_one%22%0A%20%20%20%20%23%20Step%203%3A%20scale_by_boats%20%3D%202%20where%20largest_tonnage%20%3E%3D%205%2C%20else%201%20(np.where)%3B%20return%20the%20table%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.4%22)%0A%0A%0A%40app.function%0Adef%20units_fixture()%20-%3E%20tuple%5Bpd.DataFrame%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22Four%20units%2C%20two%20with%20the%20same%20key%20in%20two%20regions%2C%20one%20with%20no%20boat%2C%20one%20with%20a%20boat%20of%20exactly%205.0%20gross%20tons%2C%0A%20%20%20%20and%20their%20boats%3A%20the%20rows%20of%20Exercise%204.4's%20example%20table%2C%20used%20by%20its%20check.%22%22%22%0A%20%20%20%20key%20%3D%20%7B%22operator_line%22%3A%20%5B1%2C%201%2C%201%2C%201%5D%2C%20%22unit_no%22%3A%20%5B1%2C%201%2C%201%2C%201%5D%7D%0A%20%20%20%20units%20%3D%20pd.DataFrame(%7B%22region_code%22%3A%20%5B%2201%22%2C%20%2204%22%2C%20%2201%22%2C%20%2201%22%5D%2C%20%22hh_id%22%3A%20%5B5300%2C%205300%2C%205301%2C%205302%5D%2C%20**key%2C%20%22unit_scale%22%3A%20%5B1%2C%201%2C%201%2C%202%5D%7D)%0A%20%20%20%20hulls%20%3D%20pd.DataFrame(%7B%22region_code%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2204%22%2C%20%2201%22%5D%2C%20%22hh_id%22%3A%20%5B5300%2C%205300%2C%205300%2C%205302%5D%2C%20**key%2C%20%22boat_tonnage%22%3A%20%5B1.2%2C%200.8%2C%205.5%2C%205.0%5D%7D)%0A%20%20%20%20return%20units%2C%20hulls%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%204.4%22%2C%20scale_from_boats%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22four%20units%3A%20one%20key%20in%20two%20regions%2C%20one%20with%20no%20boat%2C%20one%20with%20a%20boat%20of%205.0%22%2C%20units_fixture())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22every%20unit%20and%20boat%20of%20the%20survey%22%2C%20(units%2C%20boats))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20units%20of%20region%2007%20and%20every%20boat%22%2C%20(units%5Bunits%5B%22region_code%22%5D%20%3D%3D%20%2207%22%5D%2C%20boats))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20tables%22%2C%20summarize%3Dlambda%20table%3A%20rows_in_order(table%2C%20%5B*UNIT_KEY%2C%20%22unit_scale%22%2C%20%22largest_tonnage%22%2C%20%22scale_by_boats%22%5D)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Take%20each%20unit's%20largest%20boat_tonnage%20by%20all%20of%20UNIT_KEY%2C%20left-join%20it%2C%20and%20set%202%20where%20it%20is%205%20or%20more%2C%20else%201.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20units%20whose%20scale%20disagrees%20with%20their%20boats%0A%20%20%20%20def%20_try_scale()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20scaled%20%3D%20scale_from_boats(units%2C%20boats)%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20apart%20%3D%20scaled%5Bscaled%5B%22unit_scale%22%5D%20!%3D%20scaled%5B%22scale_by_boats%22%5D%5D%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5Bmo.md(f%22**%7Blen(apart)%7D%20units**%20fail%20U3%3A%22)%2C%20apart%5B%5B*UNIT_KEY%2C%20%22unit_scale%22%2C%20%22largest_tonnage%22%2C%20%22scale_by_boats%22%5D%5D%5D)%0A%0A%20%20%20%20_try_scale()%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%204.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%20Region%2004's%20unit%20of%20household%205300%20has%20%60largest_tonnage%60%201.2%2C%20or%20both%20have%205.5%20%7C%20The%20region%20is%20not%20in%20the%20key.%20%7C%20group%20and%20join%20on%20all%20of%20%60UNIT_KEY%60%20%7C%0A%20%20%20%20%7C%20The%20unit%20with%20no%20boat%20is%20missing%20%7C%20An%20inner%20join.%20%7C%20%60how%3D%22left%22%60%20%7C%0A%20%20%20%20%7C%20The%20unit%20with%20no%20boat%20is%20scale%202%20%7C%20The%20condition%20was%20written%20as%20%60largest_tonnage%20%3C%205%60%20for%201%3A%20a%20missing%20value%20makes%20it%20%60False%60.%20%7C%20%60np.where(largest_tonnage%20%3E%3D%205%2C%202%2C%201)%60%20%7C%0A%20%20%20%20%7C%20The%20boat%20of%20exactly%205.0%20makes%20its%20unit%20scale%201%20%7C%20%60%3E%60%20used%3B%20the%20code%20says%205.0%20*or%20more*.%20%7C%20%60%3E%3D%205%60%20%7C%0A%20%20%20%20%7C%20More%20rows%20than%20units%20%7C%20The%20boats%20were%20joined%20without%20the%20%60max%60%2C%20one%20row%20per%20boat.%20%7C%20reduce%20the%20boats%20to%20one%20row%20per%20unit%20first%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.accordion(%7B%22The%20R%20way%2C%20and%20what%20changes%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20-%20**Ranges%3A**%20%60between(mem_age%2C%200%2C%20110)%60%20in%20dplyr%2C%20%60Series.between(0%2C%20110)%60%20in%20pandas%3B%20both%20include%20the%20ends%2C%20and%20both%20give%20%60NA%60%20for%20a%20blank.%20In%20both%2C%20a%20filter%20drops%20the%20%60NA%60%20rows%2C%20so%20decide%20what%20a%20blank%20means%20with%20%60fillna(False)%60%20(R%3A%20%60coalesce(x%2C%20FALSE)%60).%0A%20%20%20%20-%20**A%20fact%20from%20another%20table%3A**%20%60members%20%7C%3E%20count(region_code%2C%20hh_id%2C%20name%20%3D%20%22member_rows%22)%60%20then%20%60left_join%60%20is%20%60groupby(HOUSEHOLD).size()%60%20then%20%60merge(how%3D%22left%22)%60%3B%20%60add_count()%60%20puts%20a%20group's%20count%20on%20each%20row%2C%20as%20%60groupby(...).transform(%22size%22)%60%20does.%0A%20%20%20%20-%20**%60validate%3D%60**%20is%20dplyr's%20%60relationship%3D%60%20(version%201.1%20and%20later)%3A%20the%20line%20that%20stops%20a%20join%20that%20would%20copy%20rows.%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%203%20%C2%B7%20Rules%20as%20data%3A%20a%20specification%20table%20run%20to%20a%20failure%20report%0A%0A%20%20%20%20**The%20problem.**%20Section%202%20wrote%20each%20rule%20as%20its%20own%20lines%20of%20code.%20Twenty%20rules%20make%20twenty%20blocks%2C%20and%20a%20script%20with%20one%20block%20per%20rule%2C%20each%20writing%20its%20failures%20to%20a%20sheet%20of%20its%20own%2C%20is%20a%20common%20way%20to%20run%20them.%20Two%20things%20go%20wrong.%20The%20codebook%20says%20the%20rule%20in%20words%2C%20and%20the%20code%20says%20it%20again%2C%20so%20when%20the%20codebook%20changes%2C%20someone%20must%20find%20the%20block%20and%20change%20it%20too.%20And%20the%20person%20who%20decides%20the%20rules%2C%20a%20subject-matter%20specialist%2C%20cannot%20read%20or%20check%20the%20blocks.%0A%0A%20%20%20%20**The%20tool.**%20Write%20the%20rules%20as%20**data**%3A%20one%20row%20per%20rule%20in%20a%20**specification%20table**%2C%20with%20the%20codebook's%20words%20beside%20a%20condition%20a%20program%20runs.%20The%20runner%2C%20a%20short%20function%2C%20reads%20the%20table%20and%20runs%20every%20row%20the%20same%20way.%20Adding%20a%20rule%20is%20adding%20a%20row%2C%20and%20a%20reader%20can%20check%20the%20conditions%20against%20the%20words%20line%20by%20line.%0A%0A%20%20%20%20%7C%20column%20%7C%20what%20it%20holds%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60rule%60%20%7C%20the%20rule's%20id%2C%20as%20the%20codebook%20names%20it%20%7C%0A%20%20%20%20%7C%20%60table%60%20%7C%20the%20one%20table%20the%20rule%20reads%20%7C%0A%20%20%20%20%7C%20%60applies_to%60%20%7C%20the%20scope%3A%20a%20condition%20picking%20the%20rows%20the%20rule%20is%20about%3B%20empty%20for%20every%20row%20%7C%0A%20%20%20%20%7C%20%60must_hold%60%20%7C%20the%20condition%3A%20what%20must%20be%20true%20of%20those%20rows%20%7C%0A%20%20%20%20%7C%20%60action%60%20%7C%20%60edit%60%20or%20%60refer%60%3A%20what%20happens%20to%20a%20failure%20(section%204%20decides%20it)%20%7C%0A%20%20%20%20%7C%20%60message%60%20%7C%20the%20rule%20in%20words%2C%20for%20the%20editor%20%7C%0A%0A%20%20%20%20The%20conditions%20are%20pandas%20**query%20expressions**%2C%20the%20language%20of%20%60table.query(%22...%22)%60%3A%20column%20names%2C%20comparisons%20(%60%3C%3D%60%20and%20chained%20%600%20%3C%3D%20mem_age%20%3C%3D%20110%60)%2C%20%60in%20%5B1%2C%202%5D%60%2C%20%60and%60%2C%20%60or%60%2C%20%60not%60%2C%20%60abs()%60%2C%20methods%20such%20as%20%60.isna()%60%20and%20%60.notna()%60%2C%20and%20%60%40name%60%20for%20a%20value%20from%20%60rule_values%60%20(%60%40education%60%2C%20the%20education%20codes%3B%20%60%40tolerance%60).%20The%20table%20starts%20from%20the%20codebook's%20own%20rules%20file%2C%20joined%20by%20rule%20id%2C%20so%20the%20words%20are%20the%20codebook's%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20conditions%20of%20the%20rules%20that%20read%20one%20table%2C%20after%20section%202's%20columns%20are%20added.%20U1%2C%20B5%20and%20C4%20(duplicates)%0A%20%20%20%20%23%20come%20in%20section%205%3B%20U2%2C%20C2%20and%20C3%20match%20keys%20across%20tables%2C%20which%20module%203%20did.%0A%20%20%20%20conditions%20%3D%20pd.DataFrame(%5B%0A%20%20%20%20%20%20%20%20(%22M1%22%2C%20%22%22%2C%20%22mem_sex%20in%20%5B1%2C%202%5D%22%2C%20%22refer%22)%2C%0A%20%20%20%20%20%20%20%20(%22M2%22%2C%20%22%22%2C%20%220%20%3C%3D%20mem_age%20%3C%3D%20110%22%2C%20%22refer%22)%2C%0A%20%20%20%20%20%20%20%20(%22M3%22%2C%20%22mem_age%20%3C%2010%22%2C%20%22mem_fished.isna()%20and%20mem_role_operator.isna()%20and%20mem_role_crew.isna()%20and%20mem_role_hired.isna()%20and%20mem_role_shore.isna()%22%2C%20%22refer%22)%2C%0A%20%20%20%20%20%20%20%20(%22M4%22%2C%20%22mem_role_operator%20%3D%3D%201%22%2C%20%22mem_age%20%3E%3D%2016%22%2C%20%22refer%22)%2C%0A%20%20%20%20%20%20%20%20(%22M5%22%2C%20%22mem_age%20%3E%3D%206%22%2C%20%22mem_education%20in%20%40education%22%2C%20%22refer%22)%2C%0A%20%20%20%20%20%20%20%20(%22M6%22%2C%20%22mem_fished%20%3D%3D%201%22%2C%20%22mem_role_operator%20%3D%3D%201%20or%20mem_role_crew%20%3D%3D%201%20or%20mem_role_hired%20%3D%3D%201%20or%20mem_role_shore%20%3D%3D%201%22%2C%20%22refer%22)%2C%0A%20%20%20%20%20%20%20%20(%22H1%22%2C%20%22%22%2C%20%22hh_roster_members%20%3D%3D%20member_rows%22%2C%20%22edit%22)%2C%0A%20%20%20%20%20%20%20%20(%22U3%22%2C%20%22%22%2C%20%22unit_scale%20%3D%3D%20scale_by_boats%22%2C%20%22refer%22)%2C%0A%20%20%20%20%20%20%20%20(%22U4%22%2C%20%22%22%2C%20%22unit_uses_boat%20in%20%5B0%2C%201%5D%22%2C%20%22edit%22)%2C%0A%20%20%20%20%20%20%20%20(%22U5%22%2C%20%22%22%2C%20%22unit_uses_boat%20%3D%3D%20(boat_rows%20%3E%200)%22%2C%20%22refer%22)%2C%0A%20%20%20%20%20%20%20%20(%22U6%22%2C%20%22unit_hired_crew%20%3D%3D%201%22%2C%20%22hired_rows%20%3E%200%22%2C%20%22refer%22)%2C%0A%20%20%20%20%20%20%20%20(%22U7%22%2C%20%22unit_uses_boat%20%3D%3D%201%20and%20boat_rows%20%3E%200%22%2C%20%22unit_boats%20%3D%3D%20boat_rows%22%2C%20%22edit%22)%2C%0A%20%20%20%20%20%20%20%20(%22U8%22%2C%20%22%22%2C%20%221%20%3C%3D%20unit_months_active%20%3C%3D%2012%22%2C%20%22refer%22)%2C%0A%20%20%20%20%20%20%20%20(%22B1%22%2C%20%22%22%2C%20%22boat_hull%20in%20%5B1%2C%202%2C%203%5D%20and%20boat_engine%20in%20%5B0%2C%201%2C%202%5D%22%2C%20%22refer%22)%2C%0A%20%20%20%20%20%20%20%20(%22B2%22%2C%20%22boat_engine%20%3D%3D%200%22%2C%20%22boat_tonnage%20%3C%205%22%2C%20%22edit%22)%2C%0A%20%20%20%20%20%20%20%20(%22B3%22%2C%20%22%22%2C%20%22abs(boat_tonnage%20-%20tonnage_expected)%20%3C%3D%20%40tolerance%22%2C%20%22edit%22)%2C%0A%20%20%20%20%20%20%20%20(%22B4%22%2C%20%22%22%2C%20%22boat_engine_hp.isna()%20%3D%3D%20(boat_engine%20%3D%3D%200)%22%2C%20%22refer%22)%2C%0A%20%20%20%20%20%20%20%20(%22C1%22%2C%20%22%22%2C%20%22catch_species%20in%20%40species%22%2C%20%22refer%22)%2C%0A%20%20%20%20%20%20%20%20(%22W1%22%2C%20%22%22%2C%20%22crew_sex%20in%20%5B1%2C%202%5D%22%2C%20%22edit%22)%2C%0A%20%20%20%20%5D%2C%20columns%3D%5B%22rule%22%2C%20%22applies_to%22%2C%20%22must_hold%22%2C%20%22action%22%5D)%0A%20%20%20%20spec%20%3D%20(codebook_rules.merge(conditions%2C%20on%3D%22rule%22%2C%20how%3D%22inner%22%2C%20validate%3D%22one_to_one%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20.rename(columns%3D%7B%22rule_in_words%22%3A%20%22message%22%7D)%5B%5B%22rule%22%2C%20%22table%22%2C%20%22applies_to%22%2C%20%22must_hold%22%2C%20%22action%22%2C%20%22message%22%5D%5D)%0A%20%20%20%20spec%0A%20%20%20%20return%20(spec%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%20Read%20a%20row%20aloud%20and%20it%20is%20the%20rule%3A%20*M4%3A%20in%20%60member%60%2C%20for%20the%20rows%20where%20%60mem_role_operator%20%3D%3D%201%60%2C%20%60mem_age%20%3E%3D%2016%60%20must%20hold%3B%20failures%20are%20referred.*%20Two%20rows%20read%20a%20fact%20as%20a%20comparison%3A%20U5's%20%60unit_uses_boat%20%3D%3D%20(boat_rows%20%3E%200)%60%20compares%20the%20answer%20with%20whether%20the%20unit%20has%20boat%20rows%2C%201%20with%20%60True%60%20and%200%20with%20%60False%60%2C%20and%20B4's%20%60boat_engine_hp.isna()%20%3D%3D%20(boat_engine%20%3D%3D%200)%60%20says%20the%20power%20is%20blank%20exactly%20when%20there%20is%20no%20engine.%20The%20table%20can%20live%20in%20a%20CSV%20beside%20the%20codebook%20(%60spec.to_csv(...)%60%2C%20%60pd.read_csv(...)%60)%2C%20where%20a%20specialist%20edits%20it%20in%20a%20spreadsheet%20and%20the%20runner%20reads%20it%20unchanged%3B%20an%20empty%20%60applies_to%60%20then%20comes%20back%20as%20a%20missing%20value%2C%20which%20the%20runner%20treats%20as%20%22every%20row%22.%0A%0A%20%20%20%20**A%20specification%20table%20is%20code.**%20%60query%60%20and%20%60eval%60%20run%20what%20the%20table%20says%2C%20so%20a%20row%20can%20call%20methods%20as%20well%20as%20compare%20values.%20Keep%20the%20file%20with%20the%20survey's%20processing%20code%2C%20under%20version%20control%2C%20and%20review%20a%20change%20to%20it%20as%20you%20would%20a%20change%20to%20a%20script.%0A%0A%20%20%20%20**Rules%20as%20data%2C%20the%20pipeline%20form.**%20Python%20for%20Data%20Management%20wrote%20a%20survey%20round's%20rules%20once%20as%20a%20pandera%20schema%2C%20the%20round's%20**contract**%2C%20and%20its%20pipeline%20refused%20to%20store%20a%20round%20that%20broke%20it%3A%20contract%2C%20cleaning%2C%20contract%20again.%20That%20is%20the%20form%20for%20a%20table%20that%20must%20pass%20before%20it%20moves%20on.%20The%20specification%20table%20here%20serves%20the%20step%20before%20it%2C%20editing%3A%20its%20rules%20cross%20tables%2C%20each%20failure%20is%20kept%20as%20a%20row%20for%20an%20editor%2C%20and%20each%20rule%20says%20whether%20its%20failures%20are%20corrected%20or%20referred.%20A%20rule%20such%20as%20M2%20is%20the%20same%20fact%20in%20both%3A%20%60Check.in_range(0%2C%20110)%60%20in%20a%20pandera%20schema%2C%20%600%20%3C%3D%20mem_age%20%3C%3D%20110%60%20here.%0A%0A%20%20%20%20%23%23%23%20The%20tables%20the%20rules%20read%0A%0A%20%20%20%20Every%20rule%20reads%20one%20table%2C%20so%20the%20columns%20section%202%20brought%20from%20other%20tables%20are%20added%20first.%20%60rule_frames%60%20does%20it%20with%20your%20Exercises%204.3%20and%204.4%2C%20%60member_rows%60%20and%20%60unit_rows%60.%20%60unit_rows%60%20gives%20each%20unit%20its%20number%20of%20boat%20rows%20(%60boat_rows%60)%2C%20which%20U5%20and%20U7%20read%2C%20and%20its%20number%20of%20hired%20crew%20rows%20(%60hired_rows%60)%2C%20which%20U6%20reads%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20member_rows(households%3A%20pd.DataFrame%2C%20members%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20household%20table%20with%20member_rows%3A%20the%20household's%20rows%20in%20the%20member%20table%20(0%20if%20none).%22%22%22%0A%20%20%20%20counts%20%3D%20members.groupby(HOUSEHOLD).size().rename(%22member_rows%22).reset_index()%0A%20%20%20%20out%20%3D%20households.merge(counts%2C%20on%3DHOUSEHOLD%2C%20how%3D%22left%22%2C%20validate%3D%22one_to_one%22)%0A%20%20%20%20out%5B%22member_rows%22%5D%20%3D%20out%5B%22member_rows%22%5D.fillna(0).astype(%22int64%22)%0A%20%20%20%20return%20out%0A%0A%0A%40app.function%0Adef%20unit_rows(units%3A%20pd.DataFrame%2C%20boats%3A%20pd.DataFrame%2C%20crew%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20unit%20table%20with%20boat_rows%20(its%20rows%20in%20boat)%20and%20hired_rows%20(its%20rows%20in%20crew%20with%20crew_hired%20%3D%201)%2C%20by%0A%20%20%20%20UNIT_KEY%3B%200%20if%20none.%22%22%22%0A%20%20%20%20out%20%3D%20units%0A%20%20%20%20for%20counts%20in%20(boats.groupby(UNIT_KEY).size().rename(%22boat_rows%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20crew%5Bcrew%5B%22crew_hired%22%5D%20%3D%3D%201%5D.groupby(UNIT_KEY).size().rename(%22hired_rows%22))%3A%0A%20%20%20%20%20%20%20%20out%20%3D%20out.merge(counts.reset_index()%2C%20on%3DUNIT_KEY%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20%20%20%20%20out%5Bcounts.name%5D%20%3D%20out%5Bcounts.name%5D.fillna(0).astype(%22int64%22)%0A%20%20%20%20return%20out%0A%0A%0A%40app.function%0Adef%20rule_frames(tables%3A%20dict)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22The%20tables%20the%20rules%20read%2C%20each%20with%20the%20columns%20its%20rules%20need%20from%20other%20tables%3A%20member_rows%20on%20the%20households%0A%20%20%20%20(member_rows)%2C%20tonnage_expected%20on%20the%20boats%20(Exercise%204.3)%2C%20largest_tonnage%20and%20scale_by_boats%20(4.4)%20and%0A%20%20%20%20boat_rows%20and%20hired_rows%20(unit_rows)%20on%20the%20units.%22%22%22%0A%20%20%20%20hulls%20%3D%20tables%5BBOATS%5D.assign(tonnage_expected%3Dtonnage_expected(tables%5BBOATS%5D))%0A%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20MEMBERS%3A%20tables%5BMEMBERS%5D%2C%0A%20%20%20%20%20%20%20%20HOUSEHOLDS%3A%20member_rows(tables%5BHOUSEHOLDS%5D%2C%20tables%5BMEMBERS%5D)%2C%0A%20%20%20%20%20%20%20%20UNITS%3A%20unit_rows(scale_from_boats(tables%5BUNITS%5D%2C%20hulls)%2C%20hulls%2C%20tables%5BCREW%5D)%2C%0A%20%20%20%20%20%20%20%20CREW%3A%20tables%5BCREW%5D%2C%0A%20%20%20%20%20%20%20%20BOATS%3A%20hulls%2C%0A%20%20%20%20%20%20%20%20CATCH%3A%20tables%5BCATCH%5D%2C%0A%20%20%20%20%7D%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20six%20tables%2C%20each%20with%20the%20columns%20its%20rules%20need%20(NOT_YET%20until%20Exercises%204.3%20and%204.4%20are%20written)%0A%20%20%20%20frames%20%3D%20attempt(rule_frames%2C%20tables)%0A%20%20%20%20waiting_for(%22Exercises%204.3%20and%204.4%22)%20if%20frames%20is%20NOT_YET%20else%20mo.md(%22%60frames%60%3A%20%22%20%2B%20%22%2C%20%22.join(f%22%60%7Bname%7D%60%20(%7Blen(table)%3A%2C%7D%20rows)%22%20for%20name%2C%20table%20in%20frames.items()))%0A%20%20%20%20return%20(frames%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20The%20failure%20report%3A%20its%20shape%2C%20which%20module%208%20reuses%0A%0A%20%20%20%20The%20runner's%20output%20is%20**one%20failure%20report**%20for%20every%20rule%2C%20a%20long%20table%20with%20**one%20row%20per%20rule%20and%20household**.%20A%20household%20that%20fails%20three%20rules%20has%20three%20rows%3B%20a%20rule%20that%20fails%20on%20two%20of%20a%20household's%20member%20rows%20has%20one%20row%2C%20whose%20%60rows%60%20is%202.%20Module%208%20adds%20its%20own%20flags%20to%20the%20same%20shape%2C%20so%20design%20it%20once%3A%0A%0A%20%20%20%20%7C%20column%20%7C%20type%20%7C%20what%20it%20holds%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60rule%60%20%7C%20text%20%7C%20the%20rule's%20id%20in%20the%20specification%20table%20%7C%0A%20%20%20%20%7C%20%60table%60%20%7C%20text%20%7C%20the%20table%20the%20rule%20reads%20%7C%0A%20%20%20%20%7C%20%60region_code%60%20%7C%20text%2C%20two%20digits%20%7C%20the%20region%20%7C%0A%20%20%20%20%7C%20%60hh_id%60%20%7C%20integer%20%7C%20the%20household%2C%20within%20its%20region%20%7C%0A%20%20%20%20%7C%20%60rows%60%20%7C%20integer%20%7C%20how%20many%20of%20the%20household's%20rows%20in%20that%20table%20fail%20the%20rule%20%7C%0A%20%20%20%20%7C%20%60action%60%20%7C%20text%20%7C%20%60edit%60%20or%20%60refer%60%2C%20from%20the%20specification%20table%20%7C%0A%20%20%20%20%7C%20%60message%60%20%7C%20text%20%7C%20the%20rule%20in%20words%2C%20from%20the%20specification%20table%20%7C%0A%0A%20%20%20%20Rows%20are%20sorted%20by%20%60rule%60%2C%20%60region_code%60%2C%20%60hh_id%60%20and%20numbered%20from%200%3B%20%60rule%60%2C%20%60region_code%60%20and%20%60hh_id%60%20together%20name%20a%20row%20once.%20The%20household%20is%20the%20grain%20because%20it%20is%20what%20an%20editor%20opens%3A%20one%20household's%20questionnaire%2C%20all%20its%20tables%20at%20once.%20It%20is%20also%20the%20grain%20of%20the%20list%20of%20planted%20defects%2C%20so%20the%20report%20can%20be%20judged%20against%20it.%20The%20setup%20cell%20lists%20the%20columns%20in%20%60REPORT_COLUMNS%60.%0A%0A%20%20%20%20%23%23%23%20Exercise%204.5.%20The%20runner%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20function%20is%20the%20whole%20of%20the%20rule%20machinery.%20Every%20rule%20written%20as%20a%20row%20of%20the%20table%20goes%20through%20it%2C%20so%20it%20is%20written%20once%20and%20tested%20once%3B%20the%20rules%20themselves%20are%20only%20data.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20specification%20table%2C%20the%20tables%20by%20name%20(%60frames%60)%2C%20and%20the%20named%20values%20(%60rule_values%60)%20%E2%86%92%20the%20failure%20report%20above.%20For%20each%20rule%3A%0A%0A%20%20%20%201.%20take%20its%20table%20from%20%60frames%60%3B%20keep%20the%20rows%20%60applies_to%60%20picks%20(%60table.query(applies_to%2C%20local_dict%3Drule_values)%60)%2C%20or%20every%20row%20when%20%60applies_to%60%20is%20empty%20text%20or%20missing%3B%0A%20%20%20%202.%20evaluate%20%60must_hold%60%20on%20them%20(%60.eval(must_hold%2C%20local_dict%3Drule_values)%60)%3B%20a%20row%20**fails**%20where%20the%20result%20is%20%60False%60%20or%20missing%3B%0A%20%20%20%203.%20count%20the%20failing%20rows%20per%20household%2C%20and%20add%20the%20rule's%20%60rule%60%2C%20%60table%60%2C%20%60action%60%20and%20%60message%60.%0A%0A%20%20%20%20%7C%20rule%20%7C%20table%20%7C%20region_code%20%7C%20hh_id%20%7C%20rows%20%7C%20action%20%7C%20message%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C--%3A%7C--%3A%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20A1%20%7C%20people%20%7C%2001%20%7C%205300%20%7C%202%20%7C%20refer%20%7C%20sex%20is%201%20or%202%20%7C%0A%20%20%20%20%7C%20A2%20%7C%20people%20%7C%2004%20%7C%205300%20%7C%201%20%7C%20refer%20%7C%20a%20member%20who%20runs%20a%20unit%20is%2016%20or%20older%20%7C%0A%0A%20%20%20%20A%20rule%20with%20no%20failures%20adds%20no%20row%3B%20a%20spec%20with%20no%20failures%20at%20all%20gives%20an%20empty%20report%20with%20the%20seven%20columns.%0A%0A%20%20%20%20**Tools%3A**%20%60spec.itertuples(index%3DFalse)%60%20(each%20row%20as%20an%20object%3A%20%60rule.must_hold%60)%2C%20%60query%60%20and%20%60eval%60%20with%20%60local_dict%3D%60%2C%20%60fillna(False)%60%2C%20%60groupby(HOUSEHOLD).size()%60%2C%20%60assign%60%2C%20%60pd.concat%60%2C%20%60sort_values%60%2C%20%60reset_index(drop%3DTrue)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20run_rules(spec%3A%20pd.DataFrame%2C%20frames%3A%20dict%2C%20values%3A%20dict)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Run%20every%20rule%20of%20the%20specification%20table%20on%20its%20table%3A%20one%20row%20per%20rule%20and%20household%20that%20fails%2C%20REPORT_COLUMNS.%22%22%22%0A%20%20%20%20%23%20Exercise%204.5%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20found%20%3D%20%5B%5D%3B%20for%20each%20rule%20in%20spec.itertuples(index%3DFalse)%2C%20take%20its%20table%3A%20frames%5Brule.table%5D%0A%20%20%20%20%23%20Step%202%3A%20the%20rows%20it%20applies%20to%3A%20table.query(rule.applies_to%2C%20local_dict%3Dvalues)%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20or%20the%20whole%20table%20if%20applies_to%20is%20not%20text%20or%20is%20empty%0A%20%20%20%20%23%20Step%203%3A%20holds%20%3D%20those%20rows.eval(rule.must_hold%2C%20local_dict%3Dvalues)%3B%20failing%20%3D%20rows%20where%20holds.fillna(False)%20is%20False%0A%20%20%20%20%23%20Step%204%3A%20count%20failing%20rows%20per%20household%3A%20groupby(HOUSEHOLD).size()%2C%20named%20%22rows%22%2C%20reset_index%3B%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20assign%20rule%2C%20table%2C%20action%20and%20message%20from%20the%20rule%2C%20and%20append%20to%20found%0A%20%20%20%20%23%20Step%205%3A%20concat%20found%2C%20keep%20REPORT_COLUMNS%2C%20sort%20by%20rule%2C%20region_code%2C%20hh_id%2C%20reset_index(drop%3DTrue)%3B%20return%20it%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.5%22)%0A%0A%0A%40app.function%0Adef%20rules_fixture()%20-%3E%20tuple%5Bpd.DataFrame%2C%20dict%2C%20dict%5D%3A%0A%20%20%20%20%22%22%22A%20specification%20of%20four%20rules%20on%20two%20small%20tables%2C%20with%20the%20traps%20the%20runner%20must%20handle%3A%20a%20scope%2C%20a%20blank%20age%2C%0A%20%20%20%20a%20scope%20missing%20as%20from%20a%20CSV%2C%20%40-values%2C%20an%20hh_id%20in%20two%20regions.%20Used%20by%20the%20check%20of%20Exercise%204.5.%22%22%22%0A%20%20%20%20people%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2204%22%2C%20%2204%22%5D%2C%20%22hh_id%22%3A%20%5B5300%2C%205300%2C%205300%2C%205300%2C%205301%5D%2C%0A%20%20%20%20%20%20%20%20%22sex%22%3A%20pd.array(%5B0%2C%208%2C%201%2C%202%2C%201%5D%2C%20dtype%3D%22Int32%22)%2C%20%22age%22%3A%20pd.array(%5B40%2C%2012%2C%20None%2C%2013%2C%2030%5D%2C%20dtype%3D%22Int32%22)%2C%0A%20%20%20%20%20%20%20%20%22runs_unit%22%3A%20pd.array(%5B1%2C%20None%2C%20None%2C%201%2C%200%5D%2C%20dtype%3D%22Int32%22)%2C%0A%20%20%20%20%7D)%0A%20%20%20%20places%20%3D%20pd.DataFrame(%7B%22region_code%22%3A%20%5B%2201%22%2C%20%2207%22%5D%2C%20%22hh_id%22%3A%20%5B5300%2C%205087%5D%2C%20%22code%22%3A%20pd.array(%5B10%2C%2030%5D%2C%20dtype%3D%22Int32%22)%7D)%0A%20%20%20%20spec%20%3D%20pd.DataFrame(%5B%0A%20%20%20%20%20%20%20%20(%22A1%22%2C%20%22people%22%2C%20%22%22%2C%20%22sex%20in%20%5B1%2C%202%5D%22%2C%20%22refer%22%2C%20%22sex%20is%201%20or%202%22)%2C%0A%20%20%20%20%20%20%20%20(%22A2%22%2C%20%22people%22%2C%20%22runs_unit%20%3D%3D%201%22%2C%20%22age%20%3E%3D%2016%22%2C%20%22refer%22%2C%20%22a%20member%20who%20runs%20a%20unit%20is%2016%20or%20older%22)%2C%0A%20%20%20%20%20%20%20%20(%22A3%22%2C%20%22people%22%2C%20float(%22nan%22)%2C%20%220%20%3C%3D%20age%20%3C%3D%20110%22%2C%20%22refer%22%2C%20%22an%20age%20is%20between%200%20and%20110%22)%2C%0A%20%20%20%20%20%20%20%20(%22B1%22%2C%20%22places%22%2C%20%22%22%2C%20%22code%20in%20%40valid%22%2C%20%22edit%22%2C%20%22a%20code%20is%20in%20the%20list%22)%2C%0A%20%20%20%20%5D%2C%20columns%3D%5B%22rule%22%2C%20%22table%22%2C%20%22applies_to%22%2C%20%22must_hold%22%2C%20%22action%22%2C%20%22message%22%5D)%0A%20%20%20%20return%20spec%2C%20%7B%22people%22%3A%20people%2C%20%22places%22%3A%20places%7D%2C%20%7B%22valid%22%3A%20%5B10%2C%2020%5D%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(spec)%3A%0A%20%20%20%20_fixture%20%3D%20rules_fixture()%0A%20%20%20%20_one_table%20%3D%20spec%5Bspec%5B%22rule%22%5D.isin(%5B%22M1%22%2C%20%22M2%22%2C%20%22M3%22%2C%20%22M4%22%2C%20%22M5%22%2C%20%22M6%22%2C%20%22U8%22%2C%20%22B1%22%2C%20%22C1%22%2C%20%22W1%22%5D)%5D%0A%20%20%20%20_passing%20%3D%20_fixture%5B0%5D.assign(must_hold%3D%5B%22sex%20in%20%5B0%2C%201%2C%202%2C%208%5D%22%2C%20%22age%20%3E%3D%200%22%2C%20%22age.notna()%20or%20age.isna()%22%2C%20%22code%20%3E%200%22%5D)%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%204.5%22%2C%20run_rules%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22four%20rules%20on%20two%20small%20tables%3A%20a%20scope%2C%20a%20blank%20age%2C%20a%20missing%20scope%2C%20%40valid%2C%20one%20hh_id%20in%20two%20regions%22%2C%20_fixture)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20rules%20that%20read%20the%20members%2C%20units%2C%20boats%2C%20catch%20and%20crew%20as%20they%20are%22%2C%20(_one_table%2C%20%7BMEMBERS%3A%20members%2C%20UNITS%3A%20units%2C%20BOATS%3A%20boats%2C%20CATCH%3A%20catch%2C%20CREW%3A%20crew%7D%2C%20rule_values))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22four%20rules%20that%20every%20row%20passes%22%2C%20(_passing%2C%20_fixture%5B1%5D%2C%20_fixture%5B2%5D))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22specifications%22%2C%20summarize%3Dlambda%20report%3A%20rows_in_order(report%2C%20REPORT_COLUMNS)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Scope%20with%20query%20(every%20row%20if%20applies_to%20is%20empty%20or%20missing)%2C%20eval%20must_hold%2C%20fillna(False)%2C%20count%20per%20region%20and%20household.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frames%2C%20spec)%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20runner%20on%20the%20survey%2C%20every%20rule%20of%20the%20specification%20table%0A%20%20%20%20try_it(run_rules%2C%20spec%2C%20frames%2C%20rule_values)%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%204.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%20No%20A3%20row%20for%20the%20member%20with%20a%20blank%20age%20%7C%20%60~holds%60%20kept%20%60%3CNA%3E%60%2C%20and%20the%20filter%20dropped%20it.%20%7C%20%60holds.fillna(False)%60%20%7C%0A%20%20%20%20%7C%20A2%20fails%20the%2012-year-old%20who%20does%20not%20run%20a%20unit%20%7C%20The%20scope%20was%20not%20applied.%20%7C%20%60table.query(rule.applies_to%2C%20...)%60%20first%20%7C%0A%20%20%20%20%7C%20*ValueError*%20or%20*AttributeError*%20on%20A3's%20scope%20%7C%20A%20missing%20%60applies_to%60%20(as%20from%20a%20CSV)%20was%20given%20to%20%60query%60.%20%7C%20use%20every%20row%20unless%20it%20is%20non-empty%20text%20%7C%0A%20%20%20%20%7C%20*UndefinedVariableError%3A%20local%20variable%20'valid'%20is%20not%20defined*%20%7C%20%60%40valid%60%20has%20nothing%20to%20look%20up.%20%7C%20%60local_dict%3Dvalues%60%20in%20both%20%60query%60%20and%20%60eval%60%20%7C%0A%20%20%20%20%7C%20One%20row%20for%20household%205300%20with%20%60rows%60%203%20%7C%20Grouped%20by%20%60hh_id%60%20alone%3A%20regions%2001%20and%2004%20together.%20%7C%20%60groupby(HOUSEHOLD)%60%20%7C%0A%20%20%20%20%7C%20One%20row%20per%20failing%20member%2C%20not%20per%20household%20%7C%20The%20failing%20rows%20were%20not%20counted%20per%20household.%20%7C%20%60groupby(HOUSEHOLD).size()%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20No%20objects%20to%20concatenate*%20%7C%20%60found%60%20is%20empty%3A%20the%20loop%20never%20appended.%20%7C%20append%20inside%20the%20loop%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frames%2C%20spec)%3A%0A%20%20%20%20%23%20The%20survey's%20failure%20report%2C%20and%20its%20counts%20by%20rule%20and%20region%0A%20%20%20%20report%20%3D%20NOT_YET%20if%20frames%20is%20NOT_YET%20else%20attempt(run_rules%2C%20spec%2C%20frames%2C%20rule_values)%0A%0A%20%20%20%20def%20_show()%3A%0A%20%20%20%20%20%20%20%20if%20report%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercises%204.3%20to%204.5%22)%0A%20%20%20%20%20%20%20%20by_region%20%3D%20pd.crosstab(report%5B%22rule%22%5D%2C%20report%5B%22region_code%22%5D%2C%20margins%3DTrue%2C%20margins_name%3D%22all%22)%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(report)%7D%20rows**%3A%20%7Breport%5B'rule'%5D.nunique()%7D%20of%20the%20%7Blen(spec)%7D%20rules%20failed%2C%20on%20%7Breport%5BHOUSEHOLD%5D.drop_duplicates().shape%5B0%5D%7D%20households.%20Households%20by%20rule%20and%20region%3A%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20by_region%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(%22The%20report%20itself%2C%20its%20first%20rows%3A%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20report.head(10)%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_show()%0A%20%20%20%20return%20(report%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%20The%20table%20by%20rule%20and%20region%20replaces%20the%20sheet%20per%20rule%3A%20one%20look%20says%20which%20rules%20fail%20where%2C%20and%20the%20report%20beneath%20it%20holds%20every%20household.%20An%20editor%20works%20through%20it%20household%20by%20household%3B%20a%20manager%20watches%20the%20table%20shrink%20as%20the%20editing%20goes%20on.%0A%0A%20%20%20%20%23%23%23%20From%20a%20report%20row%20to%20the%20rows%20behind%20it%0A%0A%20%20%20%20A%20report%20row%20names%20a%20household%3B%20an%20editor%20opens%20its%20rows.%20The%20rows%20are%20the%20runner's%20first%20two%20steps%3A%20the%20rows%20the%20rule%20is%20about%2C%20and%20those%20of%20them%20that%20fail.%20%60failures%60%20is%20those%20two%20steps%20of%20Exercise%204.5%20as%20a%20function%2C%20so%20the%20cell%20below%20and%20section%204's%20edits%20pick%20exactly%20the%20rows%20the%20report%20counts.%20Pick%20a%20rule%20to%20see%20its%20failing%20rows%2C%20with%20the%20columns%20the%20rule%20reads%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20failures(table%3A%20pd.DataFrame%2C%20applies_to%2C%20must_hold%3A%20str%2C%20values%3A%20dict)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20rows%20of%20the%20table%20a%20rule%20is%20about%20(applies_to%3B%20every%20row%20if%20it%20is%20empty%20or%20missing)%20where%20must_hold%20is%20False%0A%20%20%20%20or%20missing%3A%20steps%201%20and%202%20of%20the%20runner%2C%20Exercise%204.5.%22%22%22%0A%20%20%20%20scope%20%3D%20table.query(applies_to%2C%20local_dict%3Dvalues)%20if%20isinstance(applies_to%2C%20str)%20and%20applies_to.strip()%20else%20table%0A%20%20%20%20return%20scope%5B~scope.eval(must_hold%2C%20local_dict%3Dvalues).fillna(False).astype(bool)%5D%0A%0A%0A%40app.cell%0Adef%20_(spec)%3A%0A%20%20%20%20rule_pick%20%3D%20mo.ui.dropdown(options%3Dspec%5B%22rule%22%5D.tolist()%2C%20value%3D%22U3%22%2C%20label%3D%22rule%22)%0A%20%20%20%20rule_pick%0A%20%20%20%20return%20(rule_pick%2C)%0A%0A%0A%40app.cell%0Adef%20_(frames%2C%20report%2C%20rule_pick%2C%20spec)%3A%0A%20%20%20%20%23%20The%20failing%20rows%20of%20one%20rule%3A%20its%20scope%2C%20its%20condition%20false%20or%20missing%2C%20and%20the%20columns%20its%20expressions%20name%0A%20%20%20%20def%20_rows_behind()%3A%0A%20%20%20%20%20%20%20%20if%20report%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercises%204.3%20to%204.5%22)%0A%20%20%20%20%20%20%20%20rule%20%3D%20spec.set_index(%22rule%22).loc%5Brule_pick.value%5D%0A%20%20%20%20%20%20%20%20table%20%3D%20frames%5Brule%5B%22table%22%5D%5D%0A%20%20%20%20%20%20%20%20failing%20%3D%20failures(table%2C%20rule%5B%22applies_to%22%5D%2C%20rule%5B%22must_hold%22%5D%2C%20rule_values)%0A%20%20%20%20%20%20%20%20named%20%3D%20set(re.findall(r%22%5BA-Za-z_%5D%5Cw*%22%2C%20rule%5B%22applies_to%22%5D%20%2B%20%22%20%22%20%2B%20rule%5B%22must_hold%22%5D))%0A%20%20%20%20%20%20%20%20columns%20%3D%20%5B*HOUSEHOLD%2C%20*%5Bc%20for%20c%20in%20table.columns%20if%20c%20in%20named%20and%20c%20not%20in%20HOUSEHOLD%5D%5D%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5Bmo.md(f%22**%7Brule_pick.value%7D%3A**%20%7Brule%5B'message'%5D%7D%20%7Blen(failing)%7D%20rows%20fail.%22)%2C%20failing%5Bcolumns%5D%5D)%0A%0A%20%20%20%20_rows_behind()%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%20Judged%20against%20the%20planted%20defects%0A%0A%20%20%20%20The%20course%20planted%20every%20defect%20in%20this%20survey%20and%20listed%20it%20in%20%60data%2Fplanted_defects.csv%60%3B%20a%20real%20survey%20has%20no%20such%20list.%20Here%20the%20list%20tells%20us%20whether%20the%20rules%20find%20what%20they%20should.%20The%20kinds%20this%20module's%20rules%20are%20written%20for%2C%20and%20the%20rules%20that%20should%20find%20each%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Each%20kind%20of%20planted%20defect%2C%20and%20the%20rules%20that%20should%20find%20it%0A%20%20%20%20EXPECTED_RULES%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22code_not_in_list%22%3A%20%7B%22M1%22%2C%20%22M5%22%2C%20%22B1%22%2C%20%22C1%22%7D%2C%20%22age_out_of_range%22%3A%20%7B%22M2%22%7D%2C%20%22operator_too_young%22%3A%20%7B%22M4%22%7D%2C%0A%20%20%20%20%20%20%20%20%22roster_count_differs%22%3A%20%7B%22H1%22%7D%2C%20%22scale_contradicts_boats%22%3A%20%7B%22U3%22%7D%2C%20%22no_engine_large_boat%22%3A%20%7B%22B2%22%7D%2C%0A%20%20%20%20%20%20%20%20%22stored_tonnage_wrong%22%3A%20%7B%22B3%22%7D%2C%20%22uses_boat_blank%22%3A%20%7B%22U4%22%7D%2C%20%22crew_sex_invalid%22%3A%20%7B%22W1%22%7D%2C%20%22hired_crew_no_rows%22%3A%20%7B%22U6%22%7D%2C%0A%20%20%20%20%20%20%20%20%22unit_entered_twice%22%3A%20%7B%22U1%22%7D%2C%20%22catch_row_twice%22%3A%20%7B%22C4%22%7D%2C%20%22boat_under_two_units%22%3A%20%7B%22B5%22%7D%2C%20%20%23%20section%205's%0A%20%20%20%20%7D%0A%20%20%20%20pd.DataFrame(%5B(kind%2C%20%22%2C%20%22.join(sorted(rules)))%20for%20kind%2C%20rules%20in%20EXPECTED_RULES.items()%5D%2C%20columns%3D%5B%22kind%22%2C%20%22found%20by%22%5D)%0A%20%20%20%20return%20(EXPECTED_RULES%2C)%0A%0A%0A%40app.function%0Adef%20judged(report%3A%20pd.DataFrame%2C%20defects%3A%20pd.DataFrame%2C%20expected%3A%20dict)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Each%20planted%20defect%20of%20the%20expected%20kinds%2C%20with%20the%20rules%20that%20flagged%20its%20household%20and%20whether%20one%20of%20them%20is%0A%20%20%20%20a%20rule%20expected%20for%20its%20kind.%22%22%22%0A%20%20%20%20kept%20%3D%20defects%5Bdefects%5B%22kind%22%5D.isin(list(expected))%5D%5B%5B*HOUSEHOLD%2C%20%22table%22%2C%20%22kind%22%2C%20%22detail%22%5D%5D%0A%20%20%20%20flagged%20%3D%20report.groupby(HOUSEHOLD)%5B%22rule%22%5D.agg(lambda%20rules%3A%20sorted(set(rules))).rename(%22rules%22).reset_index()%0A%20%20%20%20out%20%3D%20kept.merge(flagged%2C%20on%3DHOUSEHOLD%2C%20how%3D%22left%22)%0A%20%20%20%20out%5B%22rules%22%5D%20%3D%20out%5B%22rules%22%5D.apply(lambda%20rules%3A%20rules%20if%20isinstance(rules%2C%20list)%20else%20%5B%5D)%0A%20%20%20%20out%5B%22found%22%5D%20%3D%20%5Bbool(expected%5Bkind%5D%20%26%20set(rules))%20for%20kind%2C%20rules%20in%20zip(out%5B%22kind%22%5D%2C%20out%5B%22rules%22%5D)%5D%0A%20%20%20%20out%5B%22rules%22%5D%20%3D%20out%5B%22rules%22%5D.str.join(%22%2C%20%22)%0A%20%20%20%20return%20out%0A%0A%0A%40app.cell%0Adef%20_(EXPECTED_RULES%2C%20report%2C%20spec)%3A%0A%20%20%20%20%23%20The%20planted%20defects%20of%20this%20section's%20kinds%2C%20and%20whether%20the%20report%20found%20each%0A%20%20%20%20def%20_judge()%3A%0A%20%20%20%20%20%20%20%20if%20report%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercises%204.3%20to%204.5%22)%0A%20%20%20%20%20%20%20%20kinds%20%3D%20%7Bkind%3A%20rules%20for%20kind%2C%20rules%20in%20EXPECTED_RULES.items()%20if%20rules%20%26%20set(spec%5B%22rule%22%5D)%7D%0A%20%20%20%20%20%20%20%20cases%20%3D%20judged(report%2C%20planted%2C%20kinds)%0A%20%20%20%20%20%20%20%20summary%20%3D%20cases.groupby(%22kind%22).agg(planted%3D(%22found%22%2C%20%22size%22)%2C%20found%3D(%22found%22%2C%20%22sum%22)).reset_index()%0A%20%20%20%20%20%20%20%20missed%20%3D%20cases%5B~cases%5B%22found%22%5D%5D%0A%20%20%20%20%20%20%20%20verdict%20%3D%20(%22Every%20planted%20defect%20of%20these%20kinds%20is%20in%20the%20report.%22%20if%20missed.empty%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%20f%22**%7Blen(missed)%7D%20are%20missed**%2C%20each%20a%20unit%20whose%20%60unit_ownership%60%20holds%20a%20code%20its%20list%20does%20not%20have.%20No%20rule%20of%20the%20codebook%20reads%20%60unit_ownership%60%2C%20so%20only%20section%201's%20frequency%20table%20finds%20them.%20A%20rule%20for%20it%20is%20one%20more%20row%20of%20the%20specification%2C%20%60unit_ownership%20in%20%5B10%2C%2020%2C%2030%2C%2040%2C%2090%5D%60.%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20missed%5B%22detail%22%5D.str.contains(%22unit_ownership%22).all()%20else%20f%22**%7Blen(missed)%7D%20are%20missed**%3B%20the%20rules%20for%20their%20kinds%20need%20a%20second%20look.%22)%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22**%7Bint(cases%5B'found'%5D.sum())%7D%20of%20%7Blen(cases)%7D**%20planted%20defects%20found.%20%7Bverdict%7D%20Sections%204%20and%205%20add%20the%20edits%20and%20the%20rules%20on%20duplicates%2C%20then%20look%20at%20the%20other%20side%3A%20what%20the%20report%20finds%20that%20the%20list%20did%20not%20plant%20for%20these%20rules.%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20summary%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20missed%5B%5B*HOUSEHOLD%2C%20%22kind%22%2C%20%22detail%22%2C%20%22rules%22%5D%5D%20if%20len(missed)%20else%20mo.md(%22%22)%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_judge()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22The%20R%20way%2C%20and%20what%20changes%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20-%20**One%20script%20per%20rule**%20runs%20one%20%60filter%60%20per%20rule%20and%20writes%20each%20to%20a%20sheet%20with%20%60writexl%60.%20The%20specification%20table%20puts%20the%20conditions%20in%20one%20place%20and%20the%20runner%20writes%20one%20long%20report%3B%20a%20pivot%20of%20it%20is%20the%20sheet-per-rule%20overview%2C%20and%20%60report%5Breport%5B%22rule%22%5D%20%3D%3D%20%22M4%22%5D%60%20is%20one%20sheet.%0A%20%20%20%20-%20**R%20has%20rules%20as%20data%20too%3A**%20the%20%60validate%60%20package%20reads%20rules%20from%20a%20file%20(%60validator(.file%20%3D%20%22rules.yaml%22)%60)%20and%20%60confront(data%2C%20rules)%60%20runs%20them.%20A%20pandas%20user%20can%20reach%20for%20pandera%2C%20which%20Python%20for%20Data%20Management%20used.%20This%20module%20writes%20its%20own%20runner%20because%20the%20survey's%20rules%20cross%20tables%20and%20its%20report%20has%20the%20household%20as%20its%20grain%3B%20it%20is%20short%20enough%20to%20read%20whole.%0A%20%20%20%20-%20**%60query%60%20is%20pandas'%20%60filter%60%3A**%20%60members.query(%22mem_age%20%3E%3D%2016%22)%60%20is%20%60members%20%7C%3E%20filter(mem_age%20%3E%3D%2016)%60%2C%20with%20%60%40name%60%20where%20dplyr%20would%20use%20%60!!name%60%20or%20%60.env%24name%60.%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%204%20%C2%B7%20Specifications%20that%20edit%3A%20a%20value%20set%2C%20a%20code%20drawn%20at%20random%2C%20a%20case%20referred%0A%0A%20%20%20%20**The%20problem.**%20Section%203's%20report%20says%20which%20households%20fail.%20The%20survey's%20edit%20specification%20asks%20for%20more%3A%20for%20each%20invalid%20or%20inconsistent%20value%20it%20can%20settle%2C%20it%20says%20what%20the%20value%20becomes%2C%20and%20a%20program%20makes%20the%20change.%20Two%20items%20show%20the%20kinds%20of%20edit.%20Whether%20a%20unit%20used%20a%20boat%2C%20%60unit_uses_boat%60%2C%20has%20blanks%3B%20a%20crew%20member's%20sex%2C%20%60crew_sex%60%2C%20has%20blanks%20and%20a%20code%20the%20list%20does%20not%20have.%20Here%20are%20both%20items'%20frequency%20tables%2C%20made%20by%20your%20Exercises%204.1%20and%204.2%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20unit_uses_boat%20and%20crew_sex%20against%20their%20codebook%20entries%3A%20the%20frequency%20tables%20of%20Exercise%204.2%0A%20%20%20%20def%20_two_items()%3A%0A%20%20%20%20%20%20%20%20shown%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20table%2C%20frame%2C%20variable%20in%20%5B(UNITS%2C%20units%2C%20%22unit_uses_boat%22)%2C%20(CREW%2C%20crew%2C%20%22crew_sex%22)%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20entry%20%3D%20codebook.loc%5B(codebook%5B%22table%22%5D%20%3D%3D%20table)%20%26%20(codebook%5B%22variable%22%5D%20%3D%3D%20variable)%2C%20%22values%22%5D.iloc%5B0%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20valid%20%3D%20attempt(codebook_codes%2C%20entry)%0A%20%20%20%20%20%20%20%20%20%20%20%20freq%20%3D%20NOT_YET%20if%20valid%20is%20NOT_YET%20else%20attempt(code_frequency%2C%20frame%5Bvariable%5D%2C%20valid)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20freq%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercises%204.1%20and%204.2%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20invalid%20%3D%20freq%5Bfreq%5B%22label%22%5D%20%3D%3D%20%22Invalid%20value%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20blanks%20%3D%20int(freq.loc%5Bfreq%5B%22label%22%5D%20%3D%3D%20%22No%20answer%22%2C%20%22count%22%5D.iloc%5B0%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20codes%20%3D%20%22%2C%20%22.join(f%22%7Bint(code)%7D%20(%7Bint(count)%7D)%22%20for%20code%2C%20count%20in%20zip(invalid%5B%22code%22%5D%2C%20invalid%5B%22count%22%5D))%20or%20%22none%22%0A%20%20%20%20%20%20%20%20%20%20%20%20shown%20%2B%3D%20%5Bmo.md(f%22**%60%7Bvariable%7D%60**%3A%20**%7Bblanks%7D**%20blanks%3B%20codes%20the%20list%20does%20not%20have%3A%20%7Bcodes%7D.%22)%2C%20freq%5D%0A%20%20%20%20%20%20%20%20return%20mo.vstack(shown)%0A%0A%20%20%20%20_two_items()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20survey's%20edit%20specification%20settles%20four%20rules%20by%20an%20edit%3A%0A%0A%20%20%20%20%7C%20rule%20%7C%20the%20value%20that%20fails%20%7C%20the%20edit%20%7C%20why%20the%20data%20settles%20it%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20U4%20%7C%20%60unit_uses_boat%60%20blank%2C%20or%20not%200%20or%201%20%7C%201%20if%20the%20unit%20has%20rows%20in%20%60boat%60%2C%200%20if%20it%20has%20none%20%7C%20the%20boat%20table%20answers%20the%20question%20%7C%0A%20%20%20%20%7C%20U7%20%7C%20%60unit_boats%60%20not%20the%20unit's%20rows%20in%20%60boat%60%20%7C%20the%20rows%20%7C%20the%20boat%20list%20is%20counted%20line%20by%20line%3B%20the%20count%20follows%20the%20list%20%7C%0A%20%20%20%20%7C%20B2%20%7C%20%60boat_engine%60%200%20on%20a%20boat%20of%205.0%20gross%20tons%20or%20more%20%7C%202%2C%20an%20inboard%20engine%20%7C%20the%20engine%20of%20nearly%20every%20boat%20that%20size%20%7C%0A%20%20%20%20%7C%20W1%20%7C%20%60crew_sex%60%20blank%2C%20or%20not%201%20or%202%20%7C%201%20or%202%2C%20**at%20random**%20%7C%20nothing%20in%20the%20data%20says%20which%3B%20the%20specification%20draws%20%7C%0A%0A%20%20%20%20%23%23%23%20Two%20columns%20that%20carry%20the%20edit%0A%0A%20%20%20%20A%20row%20of%20section%203's%20table%20says%20when%20a%20row%20of%20the%20survey%20fails.%20To%20edit%2C%20it%20must%20also%20say%20what%20the%20failing%20value%20becomes.%20Two%20columns%20do%20that%3A%0A%0A%20%20%20%20%7C%20column%20%7C%20what%20it%20holds%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60variable%60%20%7C%20the%20column%20the%20edit%20writes%20%7C%0A%20%20%20%20%7C%20%60edit_to%60%20%7C%20the%20new%20value%3A%20an%20expression%20on%20the%20failing%20row%2C%20in%20the%20same%20language%20as%20%60must_hold%60%20(%602%60%2C%20%60boat_rows%60%2C%20%60(boat_rows%20%3E%200)%20*%201%60)%2C%20or%20%60random(1%2C%202)%60%2C%20one%20of%20the%20listed%20codes%20drawn%20for%20each%20failing%20row%20%7C%0A%0A%20%20%20%20%60(boat_rows%20%3E%200)%20*%201%60%20is%20the%20comparison%20as%20a%20number%3A%20%60True%60%20times%201%20is%201%2C%20%60False%60%20times%201%20is%200.%20A%20rule%20with%20an%20empty%20%60edit_to%60%20changes%20nothing.%20Its%20failures%20are%20referred%20to%20a%20person%2C%20with%20two%20exceptions%20that%20section%205%20edits%20with%20code%20of%20its%20own%3A%20H1%20corrects%20a%20count%20only%20where%20the%20listing%20agrees%20with%20the%20roster%2C%20and%20B3%20recomputes%20a%20tonnage%20only%20where%20the%20dimensions%20give%20one.%20Each%20edits%20only%20some%20of%20its%20rule's%20failures%2C%20and%20one%20row%20of%20the%20table%20holds%20no%20condition%20for%20that.%0A%0A%20%20%20%20The%20cell%20below%20gives%20section%203's%20table%20the%20two%20columns%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(spec)%3A%0A%20%20%20%20%23%20The%20specification%20table%20with%20its%20edits%3A%20U4%20answers%20unit_uses_boat%20from%20the%20boat%20rows%2C%20U7%20sets%20the%20count%20of%20boats%2C%0A%20%20%20%20%23%20B2%20sets%20the%20engine%2C%20W1%20draws%20the%20sex%0A%20%20%20%20edit_spec%20%3D%20spec.assign(variable%3D%22%22%2C%20edit_to%3D%22%22)%0A%20%20%20%20for%20_rule%2C%20_variable%2C%20_edit_to%20in%20%5B(%22U4%22%2C%20%22unit_uses_boat%22%2C%20%22(boat_rows%20%3E%200)%20*%201%22)%2C%20(%22U7%22%2C%20%22unit_boats%22%2C%20%22boat_rows%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%20%20%20%20%20%20%20%20%20%20%20(%22B2%22%2C%20%22boat_engine%22%2C%20%222%22)%2C%20(%22W1%22%2C%20%22crew_sex%22%2C%20%22random(1%2C%202)%22)%5D%3A%0A%20%20%20%20%20%20%20%20edit_spec.loc%5Bedit_spec%5B%22rule%22%5D%20%3D%3D%20_rule%2C%20%5B%22variable%22%2C%20%22edit_to%22%5D%5D%20%3D%20%5B_variable%2C%20_edit_to%5D%0A%20%20%20%20edit_spec%5Bedit_spec%5B%22edit_to%22%5D%20!%3D%20%22%22%5D%0A%20%20%20%20return%20(edit_spec%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**Each%20rule%20reads%20the%20tables%20as%20the%20rules%20before%20it%20left%20them**%2C%20in%20the%20specification's%20order.%20U4%20runs%20before%20U7%2C%20and%20U7's%20scope%20is%20the%20units%20that%20use%20a%20boat%3A%20a%20unit%20whose%20blank%20U4%20answers%20with%201%20is%20then%20in%20U7's%20scope%2C%20and%20its%20count%20is%20checked%20against%20its%20boats.%0A%0A%20%20%20%20%23%23%23%20An%20engine%20set%20by%20the%20boat's%20size%0A%0A%20%20%20%20B2's%20edit%20changes%20the%20engine%2C%20not%20the%20tonnage.%20The%20scope%20keeps%20the%20edit%20to%20boats%20with%20no%20engine%2C%20and%20only%20them%3B%20the%20counts%20below%20show%20why%20the%20tonnage%20is%20not%20the%20item%20to%20change%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20boats%20of%205.0%20gross%20tons%20or%20more%2C%20by%20engine%3B%20and%20what%20the%20planted%20list%20says%20of%20those%20with%20an%20outboard%20motor%0A%20%20%20%20_large%20%3D%20boats%5Bboats%5B%22boat_tonnage%22%5D.ge(5).fillna(False)%5D%0A%20%20%20%20_engines%20%3D%20_large%5B%22boat_engine%22%5D.value_counts().reindex(%5B0%2C%201%2C%202%5D%2C%20fill_value%3D0)%0A%20%20%20%20_kinds%20%3D%20planted%5Bplanted%5B%22kind%22%5D%20!%3D%20%22large_unit_in_area_stratum%22%5D.groupby(HOUSEHOLD)%5B%22kind%22%5D.agg(set).rename(%22kinds%22).reset_index()%0A%20%20%20%20_outboard%20%3D%20_large%5B_large%5B%22boat_engine%22%5D.eq(1)%5D%5BHOUSEHOLD%5D.merge(_kinds%2C%20on%3DHOUSEHOLD%2C%20how%3D%22left%22)%0A%20%20%20%20_feet%20%3D%20int(_outboard%5B%22kinds%22%5D.apply(lambda%20k%3A%20isinstance(k%2C%20set)%20and%20%22feet_recorded_as_meters%22%20in%20k).sum())%0A%20%20%20%20_stored%20%3D%20int(_outboard%5B%22kinds%22%5D.apply(lambda%20k%3A%20isinstance(k%2C%20set)%20and%20%22stored_tonnage_wrong%22%20in%20k).sum())%0A%20%20%20%20mo.md(f%22Of%20the%20**%7Blen(_large)%7D**%20boats%20of%205.0%20gross%20tons%20or%20more%2C%20**%7B_engines%5B2%5D%7D**%20have%20an%20inboard%20engine%2C%20%7B_engines%5B1%5D%7D%20an%20outboard%20motor%20and%20%7B_engines%5B0%5D%7D%20none.%20An%20inboard%20engine%20is%20the%20engine%20of%20nearly%20every%20boat%20this%20size%2C%20so%20it%20is%20the%20edit.%20Of%20the%20%7B_engines%5B1%5D%7D%20with%20an%20outboard%20motor%2C%20the%20planted%20list%20names%20**%7B_feet%7D**%20as%20boats%20measured%20in%20feet%20whose%20size%20was%20recorded%20in%20meters%2C%20which%20module%208%20finds%2C%20and%20**%7B_stored%7D**%20as%20a%20boat%20whose%20stored%20tonnage%20is%20wrong%2C%20which%20B3%20corrects.%20On%20those%20boats%20the%20size%20is%20wrong%2C%20not%20the%20engine%2C%20and%20a%20rule%20that%20set%20the%20engine%20of%20every%20boat%20over%205%20tons%20would%20have%20edited%20the%20wrong%20item.%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**An%20edit%20that%20makes%20another%20rule%20fail.**%20B4%20says%20the%20engine's%20power%2C%20%60boat_engine_hp%60%2C%20is%20blank%20exactly%20when%20the%20boat%20has%20no%20engine.%20A%20boat%20that%20B2%20gives%20an%20inboard%20engine%20still%20has%20a%20blank%20power%2C%20so%20after%20the%20edit%20it%20fails%20B4.%20The%20power%20is%20not%20in%20the%20data%2C%20and%20no%20rule%20says%20what%20it%20is%2C%20so%20the%20specification%20does%20not%20edit%20it%3A%20B4%20refers.%20The%20order%20allows%20for%20it%3A%20every%20rule%20runs%20again%20after%20the%20edits%20(section%205)%2C%20and%20the%20boat%20reaches%20a%20person%20with%20one%20question%20left%2C%20its%20engine's%20power.%20The%20edited%20engine%20stays%20in%20the%20log%2C%20so%20the%20person%20who%20finds%20the%20power%20also%20sees%20where%20the%20engine%20came%20from.%0A%0A%20%20%20%20%23%23%23%20A%20unit%20with%20hired%20crew%20and%20no%20hired%20crew%20rows%3A%20referred%2C%20not%20edited%0A%0A%20%20%20%20A%20unit%20that%20took%20on%20crew%20from%20outside%20the%20household%20(%60unit_hired_crew%60%20%3D%201)%20lists%20them%20in%20%60crew%60%2C%20with%20%60crew_hired%60%20%3D%201.%20A%20unit%20with%20no%20such%20row%20fails%20U6.%20No%20edit%20follows%20from%20the%20data%2C%20because%20three%20different%20errors%20look%20the%20same%3A%20hired%20crew%20who%20were%20never%20entered%2C%20a%20%60unit_hired_crew%60%20keyed%20wrong%2C%20or%20hired%20crew%20rows%20keyed%20to%20another%20unit.%20Only%20the%20questionnaire%2C%20or%20a%20callback%2C%20says%20which.%20U6%20therefore%20refers.%20Its%20fact%20from%20another%20table%20is%20%60hired_rows%60%2C%20given%20in%20section%203%20beside%20%60boat_rows%60%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(edit_spec)%3A%0A%20%20%20%20%23%20U6's%20failures%2C%20with%20what%20the%20planted%20list%20holds%20for%20each%20household%0A%20%20%20%20def%20_no_hired_crew()%3A%0A%20%20%20%20%20%20%20%20rule%20%3D%20edit_spec.set_index(%22rule%22).loc%5B%22U6%22%5D%0A%20%20%20%20%20%20%20%20failing%20%3D%20failures(unit_rows(units%2C%20boats%2C%20crew)%2C%20rule%5B%22applies_to%22%5D%2C%20rule%5B%22must_hold%22%5D%2C%20rule_values)%0A%20%20%20%20%20%20%20%20kinds%20%3D%20planted.groupby(HOUSEHOLD)%5B%22kind%22%5D.agg(lambda%20k%3A%20%22%2C%20%22.join(sorted(set(k)))).rename(%22kinds%20in%20the%20list%22).reset_index()%0A%20%20%20%20%20%20%20%20shown%20%3D%20failing%5B%5B*UNIT_KEY%2C%20%22unit_hired_crew%22%2C%20%22hired_rows%22%5D%5D.merge(kinds%2C%20on%3DHOUSEHOLD%2C%20how%3D%22left%22)%0A%20%20%20%20%20%20%20%20planted_here%20%3D%20int(shown%5B%22kinds%20in%20the%20list%22%5D.str.contains(%22hired_crew_no_rows%22%2C%20na%3DFalse).sum())%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(shown)%7D%20units**%20say%20they%20took%20on%20hired%20crew%20and%20have%20no%20hired%20crew%20row.%20The%20planted%20list%20holds%20**%7Bplanted_here%7D**%20of%20them%20as%20units%20whose%20hired%20crew%20rows%20were%20left%20out.%20Each%20is%20a%20true%20failure%2C%20and%20each%20needs%20a%20person.%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20shown%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_no_hired_crew()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20A%20draw%20that%20a%20rerun%20repeats%0A%0A%20%20%20%20%22At%20random%22%20needs%20care%20in%20a%20program.%20Python's%20random%20numbers%20come%20from%20a%20**generator**%2C%20and%20a%20generator%20started%20from%20the%20same%20**seed**%20gives%20the%20same%20numbers%20every%20time%3A%20%60np.random.default_rng(41593).choice(%5B1%2C%202%5D%2C%20size%3D6)%60%20gives%20the%20same%20six%20codes%20on%20any%20computer%2C%20in%20any%20run.%20The%20setup%20cell%20keeps%20the%20seed%20in%20%60SEED%60.%20The%20draws%20also%20depend%20on%20the%20order%20of%20the%20rows%20they%20are%20given.%20A%20program%20that%20draws%20in%20the%20order%20the%20rows%20happen%20to%20arrive%20gives%20other%20codes%20when%20a%20query%20returns%20them%20in%20another%20order.%20So%20the%20rows%20are%20sorted%20first%2C%20by%20region%20and%20row%20id%2C%20and%20the%20draws%20follow%20that%20order.%0A%0A%20%20%20%20A%20survey%20wants%20a%20draw%20that%20repeats%20for%20three%20reasons%3A%0A%0A%20%20%20%20-%20**The%20edited%20data%20can%20be%20made%20again.**%20From%20the%20raw%20tables%2C%20the%20specification%2C%20the%20program%20and%20the%20seed%2C%20a%20rerun%20gives%20the%20same%20edited%20values%2C%20so%20every%20published%20total%20can%20be%20traced%20back%20to%20them.%0A%20%20%20%20-%20**A%20reviewer%20sees%20what%20the%20program%20did.**%20A%20supervisor%20who%20reruns%20the%20program%20to%20check%20one%20edit%20gets%20the%20value%20in%20the%20log%2C%20not%20a%20new%20one.%0A%20%20%20%20-%20**A%20rerun%20changes%20only%20what%20changed.**%20When%20the%20specification%20or%20the%20data%20changes%2C%20a%20comparison%20of%20two%20logs%20shows%20the%20edits%20that%20moved.%20With%20a%20new%20draw%20on%20every%20run%2C%20every%20random%20edit%20would%20move%20each%20time.%0A%0A%20%20%20%20A%20draw%20follows%20from%20the%20seed%20and%20the%20rows%20that%20fail.%20If%20a%20correction%20changes%20which%20rows%20fail%20a%20random%20rule%2C%20the%20draws%20for%20that%20rule%20can%20change%20too%3B%20the%20log%20shows%20every%20one.%20W1%20draws%201%20and%202%20with%20equal%20chances%2C%20as%20the%20specification%20says%3B%20a%20draw%20in%20proportion%20to%20the%20crews'%20own%20shares%20of%20men%20and%20women%2C%20%60rng.choice(codes%2C%20p%3Dshares)%60%2C%20is%20the%20other%20common%20choice%2C%20and%20the%20specification%20is%20where%20it%20would%20be%20written.%0A%0A%20%20%20%20%23%23%23%20The%20edit%20log%0A%0A%20%20%20%20Every%20change%20a%20program%20makes%20is%20written%20to%20an%20**edit%20log**%2C%20one%20row%20per%20changed%20value%3A%20the%20rule%2C%20the%20row%20it%20changed%2C%20the%20value%20before%20and%20after%2C%20and%20how%20the%20new%20value%20was%20found.%20The%20log%20is%20what%20makes%20an%20edit%20reviewable%2C%20and%20what%20lets%20a%20supervisor%20undo%20one.%20Its%20columns%20are%20in%20%60EDIT_LOG_COLUMNS%60.%20Two%20small%20helpers%20read%20the%20specification%20for%20you%3A%20%60drawn_from(%22random(1%2C%202)%22)%60%20gives%20the%20codes%20of%20a%20random%20edit%2C%20%60%5B1%2C%202%5D%60%2C%20and%20%60None%60%20for%20any%20other%20edit%3B%20%60as_text(value)%60%20writes%20a%20value%20for%20the%20log%2C%20a%20blank%20as%20empty%20text.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20drawn_from(edit_to)%20-%3E%20list%20%7C%20None%3A%0A%20%20%20%20%22%22%22The%20codes%20of%20a%20random%20edit%3A%20%22random(1%2C%202)%22%20gives%20%5B1%2C%202%5D.%20None%20for%20any%20other%20edit%2C%20or%20no%20edit.%22%22%22%0A%20%20%20%20match%20%3D%20re.fullmatch(r%22%5Cs*random%5C((.*)%5C)%5Cs*%22%2C%20edit_to)%20if%20isinstance(edit_to%2C%20str)%20else%20None%0A%20%20%20%20return%20%5Bint(code)%20for%20code%20in%20match.group(1).split(%22%2C%22)%5D%20if%20match%20else%20None%0A%0A%0A%40app.function%0Adef%20as_text(value)%20-%3E%20str%3A%0A%20%20%20%20%22%22%22A%20value%20as%20the%20edit%20log%20writes%20it%3A%20a%20blank%20is%20empty%20text%2C%20anything%20else%20its%20text.%22%22%22%0A%20%20%20%20return%20%22%22%20if%20pd.isna(value)%20else%20str(value)%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%204.6.%20Edits%20by%20specification%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20edit%20step%20as%20a%20program.%20The%20specification%20says%20what%20to%20edit%20and%20how%2C%20one%20function%20makes%20every%20edit%20the%20same%20way%2C%20and%20its%20log%20is%20what%20a%20supervisor%20reads%20and%20what%20the%20next%20run%20is%20compared%20with.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20specification%20table%20with%20%60variable%60%20and%20%60edit_to%60%2C%20the%20tables%20by%20name%2C%20the%20named%20values%20(%60rule_values%60)%20and%20a%20seed%20%E2%86%92%20a%20pair%3A%20the%20tables%20with%20the%20edits%20made%2C%20and%20the%20edit%20log.%20Make%20one%20generator%20at%20the%20start%2C%20%60rng%20%3D%20np.random.default_rng(seed)%60.%20Then%2C%20for%20each%20rule%20whose%20%60edit_to%60%20is%20text%20that%20is%20not%20empty%2C%20in%20the%20specification's%20order%3A%0A%0A%20%20%20%201.%20take%20its%20failing%20rows%20from%20the%20**edited**%20tables%20with%20%60failures%60%2C%20sorted%20by%20%60region_code%60%20and%20the%20row%20id%2C%20%60row_id%60%3B%0A%20%20%20%202.%20find%20the%20new%20values%3A%20if%20%60drawn_from(rule.edit_to)%60%20gives%20codes%2C%20draw%20one%20per%20failing%20row%20with%20%60rng.choice(codes%2C%20size%3Dlen(failing))%60%3B%20otherwise%20evaluate%20%60failing.eval(rule.edit_to%2C%20local_dict%3Dvalues)%60%3B%0A%20%20%20%203.%20write%20one%20log%20row%20per%20failing%20row%2C%20then%20write%20the%20new%20values%20into%20the%20rule's%20%60variable%60%20on%20those%20rows%2C%20in%20the%20column's%20own%20type%20(%60.astype(table%5Brule.variable%5D.dtype)%60).%0A%0A%20%20%20%20The%20log%20has%20the%20columns%20%60EDIT_LOG_COLUMNS%60%3A%20%60rule%60%2C%20%60table%60%2C%20%60region_code%60%2C%20%60hh_id%60%2C%20%60row%60%20(the%20row%20id)%2C%20%60variable%60%2C%20%60before%60%20and%20%60after%60%20written%20with%20%60as_text%60%2C%20and%20%60how%60%3A%20the%20rule's%20%60edit_to%60%2C%20with%20%60%2C%20seed%2041593%60%20added%20after%20a%20draw%20(the%20seed%20you%20were%20given).%20The%20tables%20you%20are%20given%20stay%20as%20they%20are%3A%20edit%20copies.%0A%0A%20%20%20%20%7C%20rule%20%7C%20table%20%7C%20region_code%20%7C%20hh_id%20%7C%20row%20%7C%20variable%20%7C%20before%20%7C%20after%20%7C%20how%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%0A%20%20%20%20%7C%20U4%20%7C%20fishing_unit%20%7C%2001%20%7C%205300%20%7C%201%20%7C%20unit_uses_boat%20%7C%20%7C%201%20%7C%20(boat_rows%20%3E%200)%20*%201%20%7C%0A%20%20%20%20%7C%20U7%20%7C%20fishing_unit%20%7C%2007%20%7C%205087%20%7C%201%20%7C%20unit_boats%20%7C%201%20%7C%202%20%7C%20boat_rows%20%7C%0A%20%20%20%20%7C%20W1%20%7C%20crew%20%7C%2001%20%7C%205300%20%7C%201%20%7C%20crew_sex%20%7C%203%20%7C%201%20or%202%2C%20as%20drawn%20%7C%20random(1%2C%202)%2C%20seed%2041593%20%7C%0A%20%20%20%20%7C%20B2%20%7C%20boat%20%7C%2001%20%7C%205300%20%7C%201%20%7C%20boat_engine%20%7C%200%20%7C%202%20%7C%202%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60%7Bname%3A%20table.copy()%20for%20name%2C%20table%20in%20frames.items()%7D%60%2C%20%60np.random.default_rng(seed)%60%2C%20%60rng.choice%60%2C%20%60DataFrame.eval%60%2C%20%60pd.Series(new%2C%20index%3Dfailing.index)%60%20(a%20single%20value%20or%20one%20value%20per%20row%2C%20lined%20up%20with%20the%20failing%20rows)%2C%20%60failing.iterrows()%60%2C%20%60.astype(dtype)%60%2C%20%60.loc%5Brows%2C%20column%5D%20%3D%20values%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20apply_edits(spec%3A%20pd.DataFrame%2C%20frames%3A%20dict%2C%20values%3A%20dict%2C%20seed%3A%20int)%20-%3E%20tuple%5Bdict%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22Make%20every%20edit%20the%20specification%20carries%2C%20in%20its%20order%3A%20the%20edited%20tables%20and%20the%20edit%20log%20(EDIT_LOG_COLUMNS).%22%22%22%0A%20%20%20%20%23%20Exercise%204.6%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20edited%20%3D%20a%20copy%20of%20every%20table%20in%20frames%3B%20log%20%3D%20%5B%5D%3B%20rng%20%3D%20np.random.default_rng(seed)%2C%20made%20once%0A%20%20%20%20%23%20Step%202%3A%20for%20each%20rule%20in%20spec.itertuples(index%3DFalse)%20whose%20edit_to%20is%20text%20that%20is%20not%20empty%3A%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20table%20%3D%20edited%5Brule.table%5D%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20failing%20%3D%20failures(table%2C%20rule.applies_to%2C%20rule.must_hold%2C%20values)%2C%20sorted%20by%20%5B%22region_code%22%2C%20ROW_ID%5D%0A%20%20%20%20%23%20Step%203%3A%20codes%20%3D%20drawn_from(rule.edit_to)%3B%20if%20codes%3A%20new%20%3D%20rng.choice(codes%2C%20size%3Dlen(failing))%20and%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20how%20%3D%20f%22%7Brule.edit_to%7D%2C%20seed%20%7Bseed%7D%22%3B%20otherwise%20new%20%3D%20failing.eval(rule.edit_to%2C%20local_dict%3Dvalues)%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20and%20how%20%3D%20rule.edit_to%3B%20then%20new%20%3D%20pd.Series(new%2C%20index%3Dfailing.index)%0A%20%20%20%20%23%20Step%204%3A%20for%20each%20failing%20row%2C%20append%20a%20dict%20to%20log%3A%20rule%2C%20table%2C%20region_code%2C%20hh_id%2C%20row%20(its%20row%20id)%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20variable%2C%20before%20%3D%20as_text(its%20value%20of%20rule.variable)%2C%20after%20%3D%20as_text(its%20new%20value)%2C%20how%0A%20%20%20%20%23%20Step%205%3A%20write%20them%20in%20the%20column's%20own%20type%3A%20table.loc%5Bfailing.index%2C%20rule.variable%5D%20%3D%20new.astype(table%5Brule.variable%5D.dtype)%0A%20%20%20%20%23%20Step%206%3A%20return%20edited%20and%20pd.DataFrame(log%2C%20columns%3DEDIT_LOG_COLUMNS)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.6%22)%0A%0A%0A%40app.function%0Adef%20edits_fixture()%20-%3E%20tuple%5Bpd.DataFrame%2C%20dict%2C%20dict%5D%3A%0A%20%20%20%20%22%22%22Six%20units%2C%20six%20crew%20rows%20and%20four%20boats%20in%20three%20regions%2C%20not%20in%20key%20order%2C%20with%20row%20ids%20that%20repeat%20across%0A%20%20%20%20regions%3A%20blank%20answers%20with%20and%20without%20boat%20rows%2C%20a%20count%20that%20U4's%20edit%20brings%20into%20U7's%20scope%2C%20a%20blank%20sex%20and%20two%0A%20%20%20%20codes%20outside%20the%20list%2C%20a%20boat%20with%20no%20engine%20of%205.54%20tons%2C%20and%20five%20rules%3A%20a%20value%20from%20the%20boat%20rows%2C%20a%20count%2C%20a%0A%20%20%20%20draw%2C%20a%20rule%20that%20only%20refers%2C%20and%20an%20engine.%20Used%20by%20the%20check%20of%20Exercise%204.6.%22%22%22%0A%20%20%20%20fleet%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2207%22%2C%20%2201%22%2C%20%2201%22%2C%20%2204%22%2C%20%2201%22%2C%20%2204%22%5D%2C%20ROW_ID%3A%20%5B1%2C%201%2C%202%2C%201%2C%203%2C%202%5D%2C%20%22hh_id%22%3A%20%5B5087%2C%205300%2C%205301%2C%205300%2C%205302%2C%205301%5D%2C%0A%20%20%20%20%20%20%20%20%22unit_uses_boat%22%3A%20pd.array(%5BNone%2C%20None%2C%201%2C%20None%2C%200%2C%201%5D%2C%20dtype%3D%22Int32%22)%2C%20%22boat_rows%22%3A%20%5B2%2C%201%2C%201%2C%200%2C%200%2C%203%5D%2C%0A%20%20%20%20%20%20%20%20%22unit_boats%22%3A%20pd.array(%5B1%2C%201%2C%201%2C%20None%2C%20None%2C%202%5D%2C%20dtype%3D%22Int32%22)%2C%0A%20%20%20%20%7D)%0A%20%20%20%20hands%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2204%22%2C%20%2201%22%2C%20%2201%22%2C%20%2207%22%2C%20%2201%22%2C%20%2204%22%5D%2C%20ROW_ID%3A%20%5B1%2C%201%2C%202%2C%201%2C%203%2C%202%5D%2C%20%22hh_id%22%3A%20%5B5300%2C%205300%2C%205300%2C%205087%2C%205301%2C%205301%5D%2C%0A%20%20%20%20%20%20%20%20%22crew_sex%22%3A%20pd.array(%5BNone%2C%203%2C%201%2C%209%2C%202%2C%20None%5D%2C%20dtype%3D%22Int32%22)%2C%0A%20%20%20%20%7D)%0A%20%20%20%20hulls%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2201%22%2C%20%2204%22%2C%20%2207%22%2C%20%2207%22%5D%2C%20ROW_ID%3A%20%5B1%2C%201%2C%201%2C%202%5D%2C%20%22hh_id%22%3A%20%5B5300%2C%205300%2C%205087%2C%205088%5D%2C%0A%20%20%20%20%20%20%20%20%22boat_hull%22%3A%20%5B2%2C%201%2C%203%2C%202%5D%2C%20%22boat_engine%22%3A%20pd.array(%5B0%2C%200%2C%202%2C%207%5D%2C%20dtype%3D%22int32%22)%2C%20%22boat_tonnage%22%3A%20%5B5.54%2C%201.2%2C%206.1%2C%201.0%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20spec%20%3D%20pd.DataFrame(%5B%0A%20%20%20%20%20%20%20%20(%22U4%22%2C%20UNITS%2C%20%22%22%2C%20%22unit_uses_boat%20in%20%5B0%2C%201%5D%22%2C%20%22edit%22%2C%20%22unit_uses_boat%22%2C%20%22(boat_rows%20%3E%200)%20*%201%22%2C%20%22answered%20from%20the%20boat%20rows%22)%2C%0A%20%20%20%20%20%20%20%20(%22U7%22%2C%20UNITS%2C%20%22unit_uses_boat%20%3D%3D%201%20and%20boat_rows%20%3E%200%22%2C%20%22unit_boats%20%3D%3D%20boat_rows%22%2C%20%22edit%22%2C%20%22unit_boats%22%2C%20%22boat_rows%22%2C%20%22the%20count%20is%20the%20rows%22)%2C%0A%20%20%20%20%20%20%20%20(%22W1%22%2C%20CREW%2C%20%22%22%2C%20%22crew_sex%20in%20%5B1%2C%202%5D%22%2C%20%22edit%22%2C%20%22crew_sex%22%2C%20%22random(1%2C%202)%22%2C%20%22a%20sex%20outside%201%20and%202%20is%20drawn%22)%2C%0A%20%20%20%20%20%20%20%20(%22B1%22%2C%20BOATS%2C%20%22%22%2C%20%22boat_engine%20in%20%5B0%2C%201%2C%202%5D%22%2C%20%22refer%22%2C%20%22%22%2C%20%22%22%2C%20%22an%20engine%20is%200%2C%201%20or%202%22)%2C%0A%20%20%20%20%20%20%20%20(%22B2%22%2C%20BOATS%2C%20%22boat_engine%20%3D%3D%200%22%2C%20%22boat_tonnage%20%3C%205%22%2C%20%22edit%22%2C%20%22boat_engine%22%2C%20%222%22%2C%20%22a%20boat%20with%20no%20engine%20is%20under%205%20tons%22)%2C%0A%20%20%20%20%5D%2C%20columns%3D%5B%22rule%22%2C%20%22table%22%2C%20%22applies_to%22%2C%20%22must_hold%22%2C%20%22action%22%2C%20%22variable%22%2C%20%22edit_to%22%2C%20%22message%22%5D)%0A%20%20%20%20return%20spec%2C%20%7BUNITS%3A%20fleet%2C%20CREW%3A%20hands%2C%20BOATS%3A%20hulls%7D%2C%20%7B%7D%0A%0A%0A%40app.function%0Adef%20shuffled(frames%3A%20dict)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22The%20same%20tables%20with%20their%20rows%20in%20reverse%20order%20and%20numbered%20again%3A%20a%20query%20that%20returns%20them%20in%20another%20order.%22%22%22%0A%20%20%20%20return%20%7Bname%3A%20table.iloc%5B%3A%3A-1%5D.reset_index(drop%3DTrue)%20for%20name%2C%20table%20in%20frames.items()%7D%0A%0A%0A%40app.function%0Adef%20edits_checked(spec%3A%20pd.DataFrame%2C%20frames%3A%20dict%2C%20values%3A%20dict%2C%20seed%3A%20int)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22What%20the%20check%20of%20Exercise%204.6%20compares%3A%20the%20log%20and%20the%20edited%20columns%2C%20rows%20sorted%2C%20and%20whether%20the%20tables%20it%0A%20%20%20%20was%20given%20are%20unchanged.%22%22%22%0A%20%20%20%20given%20%3D%20%7Bname%3A%20table.copy()%20for%20name%2C%20table%20in%20frames.items()%7D%0A%20%20%20%20edited%2C%20log%20%3D%20apply_edits(spec%2C%20frames%2C%20values%2C%20seed)%0A%20%20%20%20out%20%3D%20%7B%22log%22%3A%20rows_in_order(log%2C%20EDIT_LOG_COLUMNS)%2C%20%22the%20tables%20given%20are%20unchanged%22%3A%20all(frames%5Bname%5D.equals(given%5Bname%5D)%20for%20name%20in%20frames)%7D%0A%20%20%20%20for%20name%20in%20sorted(set(log%5B%22table%22%5D))%3A%0A%20%20%20%20%20%20%20%20written%20%3D%20sorted(set(log.loc%5Blog%5B%22table%22%5D%20%3D%3D%20name%2C%20%22variable%22%5D))%0A%20%20%20%20%20%20%20%20out%5Bname%5D%20%3D%20rows_in_order(edited%5Bname%5D%5B%5B%22region_code%22%2C%20ROW_ID%2C%20*written%5D%5D)%0A%20%20%20%20return%20out%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(edit_spec)%3A%0A%20%20%20%20_spec%2C%20_frames%2C%20_values%20%3D%20edits_fixture()%0A%20%20%20%20_survey%20%3D%20edit_spec%5Bedit_spec%5B%22rule%22%5D.isin(%5B%22U4%22%2C%20%22U7%22%2C%20%22B1%22%2C%20%22B2%22%2C%20%22W1%22%5D)%5D%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%204.6%22%2C%20edits_checked%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22six%20units%2C%20six%20crew%20rows%20and%20four%20boats%3A%20blanks%2C%20codes%20outside%20the%20list%2C%20a%20count%2C%20a%20rule%20that%20only%20refers%2C%20an%20engine%22%2C%20(_spec%2C%20_frames%2C%20_values%2C%20SEED))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20same%20tables%2C%20their%20rows%20in%20another%20order%22%2C%20(_spec%2C%20shuffled(_frames)%2C%20_values%2C%20SEED))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20same%20tables%2C%20another%20seed%22%2C%20(_spec%2C%20_frames%2C%20_values%2C%207))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20units%2C%20boats%20and%20crew%3A%20U4%2C%20U7%2C%20B1%2C%20B2%20and%20W1%22%2C%20(_survey%2C%20%7BUNITS%3A%20unit_rows(units%2C%20boats%2C%20crew)%2C%20BOATS%3A%20boats%2C%20CREW%3A%20crew%7D%2C%20rule_values%2C%20SEED))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22specifications%22%2C%20summarize%3DNone%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Copy%20the%20tables%3B%20one%20default_rng(seed)%3B%20for%20each%20rule%20with%20an%20edit_to%3A%20failures%20on%20the%20edited%20table%2C%20sorted%20by%20region%20and%20row%20id%2C%20then%20draw%20or%20eval%2C%20log%2C%20write.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(edit_spec)%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20edits%20on%20the%20survey's%20units%2C%20boats%20and%20crew%2C%20and%20the%20log%20they%20write%0A%20%20%20%20def%20_try_edits()%3A%0A%20%20%20%20%20%20%20%20done%20%3D%20attempt(apply_edits%2C%20edit_spec%2C%20%7BUNITS%3A%20unit_rows(units%2C%20boats%2C%20crew)%2C%20BOATS%3A%20boats%2C%20CREW%3A%20crew%7D%2C%20rule_values%2C%20SEED)%0A%20%20%20%20%20%20%20%20if%20done%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20edited%2C%20log%20%3D%20done%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5Bmo.md(f%22%60apply_edits%60%20wrote%20**%7Blen(log)%7D%20changes**%3A%20%7B'%2C%20'.join(f'%7Brule%7D%20%7Bn%7D'%20for%20rule%2C%20n%20in%20log%5B'rule'%5D.value_counts().sort_index().items())%7D.%22)%2C%20log%5D)%0A%0A%20%20%20%20_try_edits()%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%204.6%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Red%20only%20for%20%22rows%20in%20another%20order%22%20%7C%20The%20draws%20follow%20the%20rows'%20order%20as%20they%20arrived.%20%7C%20sort%20the%20failing%20rows%20by%20%60region_code%60%20and%20the%20row%20id%20before%20drawing%20%7C%0A%20%20%20%20%7C%20Red%20for%20the%20first%20case%3B%20region%2004's%20row%201%20and%20region%2001's%20row%201%20swap%20codes%20%7C%20Sorted%20by%20the%20row%20id%20alone%3A%20row%20ids%20repeat%20across%20regions.%20%7C%20sort%20by%20%60%5B%22region_code%22%2C%20ROW_ID%5D%60%20%7C%0A%20%20%20%20%7C%20No%20U7%20row%20for%20region%2007's%20unit%201%20%7C%20U7's%20failures%20were%20taken%20from%20the%20tables%20you%20were%20given%2C%20where%20that%20unit's%20%60unit_uses_boat%60%20is%20still%20blank.%20%7C%20%60failures%60%20on%20%60edited%5Brule.table%5D%60%20%7C%0A%20%20%20%20%7C%20%60the%20tables%20given%20are%20unchanged%60%20is%20%60False%60%20%7C%20The%20edits%20were%20written%20into%20the%20tables%20you%20were%20given.%20%7C%20%60table.copy()%60%20for%20every%20table%20first%20%7C%0A%20%20%20%20%7C%20A%20different%20code%20on%20every%20run%20%7C%20The%20generator%20has%20no%20seed%2C%20or%20%60np.random.choice%60%20was%20used.%20%7C%20%60np.random.default_rng(seed)%60%20%7C%0A%20%20%20%20%7C%20Red%20for%20%22another%20seed%22%20only%20%7C%20The%20seed%20is%20typed%20in%2C%20not%20the%20one%20passed.%20%7C%20use%20the%20%60seed%60%20argument%2C%20in%20the%20generator%20and%20in%20%60how%60%20%7C%0A%20%20%20%20%7C%20%60before%60%20is%20%60%3CNA%3E%60%2C%20or%20%60before%60%20equals%20%60after%60%20%7C%20%60str()%60%20on%20a%20blank%2C%20or%20the%20value%20read%20after%20it%20was%20written.%20%7C%20%60as_text(row%5Brule.variable%5D)%60%20before%20Step%205%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20expr%20cannot%20be%20an%20empty%20string*%2C%20or%20a%20log%20row%20for%20B1%20%7C%20A%20rule%20with%20no%20edit%20was%20applied%3A%20B1%20only%20refers.%20%7C%20skip%20a%20rule%20whose%20%60edit_to%60%20is%20empty%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20Length%20of%20values*%20%7C%20%60rng.choice%60%20drew%20a%20number%20of%20codes%20other%20than%20the%20number%20of%20failing%20rows.%20%7C%20%60size%3Dlen(failing)%60%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20Invalid%20value%20...%20for%20dtype%20'int32'*%2C%20on%20the%20survey%20only%20%7C%20The%20new%20values%20are%2064-bit%20integers%20and%20the%20survey's%20%60boat_engine%60%20is%2032-bit%3B%20pandas%20will%20not%20change%20a%20column's%20type%20in%20a%20partial%20write.%20%7C%20%60new.astype(table%5Brule.variable%5D.dtype)%60%20in%20Step%205%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(edit_spec)%3A%0A%20%20%20%20%23%20W1's%20draws%2C%20run%20four%20ways%3A%20again%2C%20on%20the%20rows%20in%20another%20order%2C%20and%20with%20another%20seed%0A%20%20%20%20def%20_repeat()%3A%0A%20%20%20%20%20%20%20%20drawn_rule%20%3D%20edit_spec%5Bedit_spec%5B%22rule%22%5D%20%3D%3D%20%22W1%22%5D%0A%20%20%20%20%20%20%20%20runs%20%3D%20%7Bf%22seed%20%7BSEED%7D%22%3A%20(%7BCREW%3A%20crew%7D%2C%20SEED)%2C%20%22again%22%3A%20(%7BCREW%3A%20crew%7D%2C%20SEED)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22rows%20in%20another%20order%22%3A%20(shuffled(%7BCREW%3A%20crew%7D)%2C%20SEED)%2C%20f%22seed%20%7BSEED%20%2B%201%7D%22%3A%20(%7BCREW%3A%20crew%7D%2C%20SEED%20%2B%201)%7D%0A%20%20%20%20%20%20%20%20logs%20%3D%20%7B%7D%0A%20%20%20%20%20%20%20%20for%20name%2C%20(frames%2C%20seed)%20in%20runs.items()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20done%20%3D%20attempt(apply_edits%2C%20drawn_rule%2C%20frames%2C%20rule_values%2C%20seed)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20done%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercise%204.6%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20logs%5Bname%5D%20%3D%20done%5B1%5D.set_index(%5B%22rule%22%2C%20%22region_code%22%2C%20%22row%22%2C%20%22before%22%5D)%5B%22after%22%5D%0A%20%20%20%20%20%20%20%20side%20%3D%20pd.concat(logs%2C%20axis%3D1).reset_index()%0A%20%20%20%20%20%20%20%20first%20%3D%20side%5Bf%22seed%20%7BSEED%7D%22%5D%0A%20%20%20%20%20%20%20%20same%20%3D%20all(first.equals(side%5Bname%5D)%20for%20name%20in%20(%22again%22%2C%20%22rows%20in%20another%20order%22))%0A%20%20%20%20%20%20%20%20moved%20%3D%20int((first%20!%3D%20side%5Bf%22seed%20%7BSEED%20%2B%201%7D%22%5D).sum())%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22W1%20draws%20**%7Blen(side)%7D**%20codes%2C%20and%20they%20are%20**%7B'the%20same'%20if%20same%20else%20'not%20the%20same'%7D**%20in%20a%20rerun%20and%20with%20the%20rows%20in%20another%20order.%20With%20another%20seed%2C%20%7Bmoved%7D%20of%20them%20come%20out%20differently.%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20side%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_repeat()%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%20Edited%20by%20rule%20or%20referred%0A%0A%20%20%20%20A%20failure%20ends%20one%20of%20two%20ways.%20**Edited%20by%20rule**%3A%20a%20program%20corrects%20it%2C%20because%20the%20specification%20says%20what%20the%20value%20becomes.%20**Referred**%3A%20a%20person%20decides%2C%20from%20the%20questionnaire%2C%20a%20callback%20or%20the%20field%20supervisor%2C%20because%20the%20data%20allows%20more%20than%20one%20correction.%20The%20specification%20table's%20%60action%60%20column%20records%20the%20decision%20for%20each%20rule%3A%0A%0A%20%20%20%20%7C%20rule%20%7C%20action%20%7C%20why%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20U4%20a%20blank%20%60unit_uses_boat%60%20%7C%20**edit**%3A%201%20if%20the%20unit%20has%20boat%20rows%2C%200%20if%20not%20%7C%20the%20boat%20table%20answers%20it%20%7C%0A%20%20%20%20%7C%20U7%20a%20count%20of%20boats%20that%20is%20not%20the%20boat%20rows%20%7C%20**edit**%3A%20the%20rows%20%7C%20the%20list%20is%20the%20answer%3B%20the%20count%20is%20its%20sum%20%7C%0A%20%20%20%20%7C%20B2%20a%20boat%20with%20no%20engine%20of%205%20tons%20or%20more%20%7C%20**edit**%3A%20engine%202%20%7C%20the%20engine%20of%20nearly%20every%20boat%20that%20size%3B%20the%20scope%20keeps%20the%20edit%20to%20boats%20with%20no%20engine%20%7C%0A%20%20%20%20%7C%20W1%20a%20crew%20member's%20sex%20outside%201%20and%202%20%7C%20**edit**%3A%201%20or%202%2C%20drawn%20with%20the%20seed%20%7C%20the%20specification's%20choice%3B%20the%20log%20keeps%20each%20draw%20%7C%0A%20%20%20%20%7C%20U1%2C%20C4%20a%20unit%20or%20a%20species%20entered%20twice%20%7C%20**edit**%3A%20drop%20the%20copy%2C%20if%20the%20copy%20is%20exact%20(section%205)%20%7C%20a%20row%20equal%20in%20every%20column%20but%20its%20row%20id%20is%20the%20same%20answer%20stored%20twice%3B%20dropping%20it%20loses%20nothing.%20A%20copy%20that%20differs%20is%20two%20answers%2C%20and%20a%20person%20picks%20one%20%7C%0A%20%20%20%20%7C%20H1%20member%20count%20%7C%20**edit**%3A%20set%20the%20count%20to%20the%20member%20rows%2C%20when%20the%20listing%20(%60hh_listed_members%60)%20agrees%20with%20the%20rows%20%7C%20the%20roster%20lists%20the%20members%20line%20by%20line%2C%20and%20the%20count%20is%20the%20sum%20of%20the%20lines%3B%20the%20roster%20prevails.%20If%20the%20listing%20agrees%20with%20the%20count%20instead%2C%20a%20member%20may%20be%20missing%20from%20the%20roster%2C%20and%20that%20is%20referred%20%7C%0A%20%20%20%20%7C%20B3%20stored%20tonnage%20%7C%20**edit**%3A%20recompute%20it%20from%20the%20dimensions%20%7C%20the%20tonnage%20is%20computed%2C%20not%20answered%3B%20the%20dimensions%20are%20the%20answer%20%7C%0A%20%20%20%20%7C%20U6%20hired%20crew%20with%20no%20hired%20crew%20row%20%7C%20refer%20%7C%20hired%20crew%20never%20entered%2C%20a%20wrong%20answer%20and%20rows%20keyed%20astray%20look%20the%20same%20%7C%0A%20%20%20%20%7C%20B4%20an%20engine's%20power%20%7C%20refer%20%7C%20the%20power%20is%20not%20in%20the%20data%3B%20after%20B2's%20edit%2C%20this%20is%20the%20question%20left%20for%20a%20person%20%7C%0A%20%20%20%20%7C%20M1%2C%20M2%2C%20M5%2C%20B1%2C%20C1%2C%20U8%20invalid%20codes%20and%20ranges%20%7C%20refer%20%7C%20the%20right%20code%20is%20not%20in%20the%20data%3A%20sex%208%20could%20be%20either%20sex%2C%20an%20age%20of%20130%20could%20be%2013%20or%2030%20%7C%0A%20%20%20%20%7C%20M3%2C%20M4%20a%20young%20member%20who%20runs%20a%20unit%20%7C%20refer%20%7C%20the%20age%20may%20be%20wrong%2C%20or%20the%20flag%3A%20only%20the%20questionnaire%20says%20which%20%7C%0A%20%20%20%20%7C%20M6%20a%20member%20who%20fished%20with%20no%20role%20%7C%20refer%20%7C%20which%20role%20was%20left%20out%20is%20not%20in%20the%20data%20%7C%0A%20%20%20%20%7C%20U3%20a%20unit's%20scale%20against%20its%20boats%20%7C%20refer%20%7C%20the%20boat's%20measurement%20or%20the%20unit's%20scale%20may%20be%20wrong%20%7C%0A%20%20%20%20%7C%20U5%20%60unit_uses_boat%60%20against%20the%20boat%20rows%20%7C%20refer%20%7C%20the%20answer%20may%20be%20wrong%2C%20or%20the%20boat%20rows%20%7C%0A%20%20%20%20%7C%20B5%20one%20registration%20on%20two%20boat%20rows%20%7C%20refer%20%7C%20the%20household's%20two%20units%20may%20share%20a%20boat%2C%20or%20the%20boat%20was%20entered%20under%20the%20wrong%20unit%20%7C%0A%0A%20%20%20%20Corrections%20run%20in%20a%20fixed%20order%3A%20first%20the%20edits%2C%20then%20every%20rule%20again.%20An%20edit%20can%20make%20another%20rule%20fail%2C%20as%20B2%20does%20with%20B4.%20Section%205%20runs%20the%20whole%20of%20it%2C%20and%20what%20fails%20the%20second%20run%20is%20the%20**referral%20list**.%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.accordion(%7B%22The%20R%20way%2C%20and%20what%20changes%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20In%20R%2C%20the%20draw%20for%20W1%20is%20one%20%60mutate%60%3A%0A%0A%20%20%20%20%60%60%60r%0A%20%20%20%20set.seed(41593)%0A%20%20%20%20crew%20%3C-%20crew%20%7C%3E%0A%20%20%20%20%20%20mutate(crew_sex%20%3D%20case_when(!crew_sex%20%25in%25%20c(1%2C%202)%20~%20sample(c(1%2C%202)%2C%20n()%2C%20replace%20%3D%20TRUE)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20TRUE%20~%20crew_sex))%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20-%20**The%20seed%3A**%20%60set.seed()%60%20before%20%60sample()%60%20is%20what%20%60np.random.default_rng(seed)%60%20is%20here.%20Without%20it%2C%20a%20rerun%20draws%20other%20codes.%0A%20%20%20%20-%20**The%20order%3A**%20%60sample()%60%20draws%20one%20code%20for%20every%20row%20of%20the%20table%20and%20%60case_when%60%20keeps%20those%20of%20the%20rows%20it%20edits%2C%20so%20the%20codes%20depend%20on%20the%20rows'%20order.%20%60arrange()%60%20by%20region%20and%20row%20id%20first%2C%20as%20Exercise%204.6%20sorts.%0A%20%20%20%20-%20**The%20edit%20in%20the%20script%2C%20or%20in%20the%20specification%3A**%20the%20R%20code%20types%20the%20codes%20and%20the%20edit%20into%20the%20script.%20Here%20they%20are%20a%20row%20of%20the%20specification%20table%20that%20a%20specialist%20reads%2C%20and%20the%20log%20records%20every%20value%20the%20program%20changed.%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%205%20%C2%B7%20Duplicates%20by%20key%20and%20by%20a%20stated%20definition%3B%20the%20referral%20list%0A%0A%20%20%20%20**The%20problem.**%20Before%20the%20tables%20are%20tabulated%2C%20each%20of%20their%20rows%20must%20be%20there%20once.%20A%20unit%20entered%20twice%20counts%20twice%20in%20every%20total%20of%20units%2C%20and%20its%20catch%20twice%20in%20every%20total%20of%20catch.%20But%20%22the%20same%20row%22%20has%20several%20meanings%2C%20and%20the%20obvious%20test%20finds%20nothing%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Rows%20equal%20in%20every%20column%3A%20none%2C%20because%20the%20system's%20row%20id%20is%20different%20on%20every%20row%0A%20%20%20%20_rows%20%3D%20%5B%5D%0A%20%20%20%20for%20_name%20in%20(UNITS%2C%20CATCH%2C%20MEMBERS)%3A%0A%20%20%20%20%20%20%20%20_table%20%3D%20tables%5B_name%5D%0A%20%20%20%20%20%20%20%20_rows.append(%7B%22table%22%3A%20_name%2C%20%22rows%22%3A%20len(_table)%2C%20%22duplicated()%22%3A%20int(_table.duplicated().sum())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22duplicated()%2C%20the%20row%20id%20left%20out%22%3A%20int(_table.drop(columns%3DROW_ID).duplicated().sum())%7D)%0A%20%20%20%20pd.DataFrame(_rows)%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%20Every%20table%20has%20a%20**row%20id**%20the%20capture%20system%20gives%20each%20row%20it%20stores%20(%60row_id%60).%20A%20row%20entered%20twice%20is%20stored%20twice%2C%20with%20two%20ids%2C%20so%20no%20two%20rows%20are%20ever%20equal%20in%20every%20column.%20Leave%20the%20row%20id%20out%20and%20the%20copies%20appear.%20Like%20the%20household%20id%2C%20a%20row%20id%20is%20numbered%20by%20each%20regional%20database%2C%20so%20it%20names%20a%20row%20only%20together%20with%20%60region_code%60.%0A%0A%20%20%20%20%23%23%23%20By%20key%3A%20the%20columns%20that%20should%20name%20a%20row%20once%0A%0A%20%20%20%20A%20copy%20that%20differs%20in%20some%20column%20is%20still%20a%20duplicate%20if%20it%20describes%20the%20same%20thing.%20What%20names%20a%20row%20is%20its%20**key**%3A%20a%20unit's%20is%20%60UNIT_KEY%60.%20Test%20the%20key%2C%20and%20remember%20the%20region%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20unit%20key%20with%20and%20without%20the%20region%3A%20hh_id%20repeats%20across%20regions%2C%20so%20the%20key%20needs%20region_code%0A%20%20%20%20_without%20%3D%20int(units.duplicated(UNIT_KEY%5B1%3A%5D).sum())%0A%20%20%20%20_with%20%3D%20int(units.duplicated(UNIT_KEY).sum())%0A%20%20%20%20mo.md(f%22%60duplicated%60%20on%20the%20three%20key%20parts%20without%20the%20region%3A%20**%7B_without%3A%2C%7D**%20rows%20repeat%20a%20key.%20With%20%60region_code%60%3A%20**%7B_with%7D**.%20The%20other%20%7B_without%20-%20_with%3A%2C%7D%20are%20units%20of%20different%20regions%20whose%20household%20numbers%20happen%20to%20be%20equal%2C%20not%20duplicates.%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%20Key%20subsets%3A%20a%20duplicate%20is%20a%20duplicate%20of%20something%0A%0A%20%20%20%20A%20table%20can%20have%20more%20than%20one%20key%2C%20each%20a%20**subset**%20of%20its%20columns%20that%20should%20name%20a%20row%20once%2C%20and%20each%20finds%20a%20different%20kind%20of%20duplicate%3A%0A%0A%20%20%20%20%7C%20table%20%7C%20key%20subset%20%7C%20a%20repeat%20means%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20units%20%7C%20%60UNIT_KEY%60%20%7C%20the%20unit%20is%20entered%20twice%20(U1)%20%7C%0A%20%20%20%20%7C%20catch%20%7C%20%60UNIT_KEY%60%20%2B%20%60catch_species%60%20%7C%20a%20species%20is%20listed%20twice%20for%20one%20unit%20(C4)%20%7C%0A%20%20%20%20%7C%20members%20%7C%20%60HOUSEHOLD%60%20%2B%20%60mem_line%60%20%7C%20a%20member%20is%20listed%20twice%20%7C%0A%20%20%20%20%7C%20boats%20%7C%20%60HOUSEHOLD%60%20%2B%20%60boat_reg_no%60%20(the%20registration%20number)%20%7C%20one%20registered%20boat%20is%20on%20two%20boat%20rows%20of%20the%20household%20(B5)%20%7C%0A%0A%20%20%20%20The%20last%20key%20has%20a%20trap.%20Most%20boats%20are%20not%20registered%2C%20and%20their%20%60boat_reg_no%60%20is%20blank.%20%60duplicated%60%20treats%20two%20blanks%20as%20equal%2C%20so%20every%20two%20unregistered%20boats%20of%20a%20household%20are%20%22duplicates%22%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20registration%20key%20with%20and%20without%20the%20blanks%3A%20two%20blank%20registrations%20are%20equal%20to%20duplicated()%2C%20and%20mean%20nothing%0A%20%20%20%20_all%20%3D%20int(boats.duplicated(%5B*HOUSEHOLD%2C%20%22boat_reg_no%22%5D%2C%20keep%3DFalse).sum())%0A%20%20%20%20_registered%20%3D%20boats.dropna(subset%3D%5B%22boat_reg_no%22%5D)%0A%20%20%20%20_real%20%3D%20int(_registered.duplicated(%5B*HOUSEHOLD%2C%20%22boat_reg_no%22%5D%2C%20keep%3DFalse).sum())%0A%20%20%20%20mo.md(f%22Boats%20whose%20region%2C%20household%20and%20registration%20repeat%3A%20**%7B_all%3A%2C%7D**%20as%20%60duplicated%60%20counts%20them%2C%20most%20of%20them%20unregistered%20boats%20with%20a%20blank%20%60boat_reg_no%60.%20Among%20the%20registered%20boats%3A%20**%7B_real%7D**%2C%20the%20rows%20to%20look%20at.%20A%20key%20with%20a%20blank%20part%20names%20nothing%2C%20so%20its%20rows%20are%20left%20out%20first.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%204.7.%20Every%20copy%20of%20a%20repeated%20key%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20to%20decide%20what%20to%20do%20with%20a%20duplicate%2C%20an%20editor%20sees%20all%20its%20copies%20side%20by%20side.%20%60duplicated()%60%20marks%20the%20second%20copy%20only%3B%20%60keep%3DFalse%60%20marks%20every%20copy.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20table%20and%20a%20key%20(a%20list%20of%20columns)%20%E2%86%92%20the%20rows%20whose%20key%2C%20**with%20no%20part%20blank**%2C%20appears%20more%20than%20once%2C%20every%20copy%20kept%2C%20sorted%20by%20the%20key%3A%0A%0A%20%20%20%20%7C%20region_code%20%7C%20hh_id%20%7C%20boat_no%20%7C%20boat_reg_no%20%7C%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%2001%20%7C%205300%20%7C%201%20%7C%20R01-00101%20%7C%20kept%3A%20the%20registration%20is%20on%20two%20rows%20%7C%0A%20%20%20%20%7C%2001%20%7C%205300%20%7C%203%20%7C%20R01-00101%20%7C%20kept%20%7C%0A%20%20%20%20%7C%2001%20%7C%205300%20%7C%202%20%7C%20*blank*%20%7C%20left%20out%3A%20not%20registered%20%7C%0A%20%20%20%20%7C%2001%20%7C%205300%20%7C%204%20%7C%20*blank*%20%7C%20left%20out%20%7C%0A%20%20%20%20%7C%2001%20%7C%205301%20%7C%201%20%7C%20R01-00101%20%7C%20left%20out%3A%20another%20household%2C%20the%20boat%20sold%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60dropna(subset%3Dkey)%60%2C%20%60duplicated(subset%3Dkey%2C%20keep%3DFalse)%60%2C%20%60sort_values(key)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20duplicate_rows(table%3A%20pd.DataFrame%2C%20key%3A%20list)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Every%20row%20whose%20key%20(no%20part%20blank)%20appears%20more%20than%20once%20in%20the%20table%2C%20all%20copies%20kept%2C%20sorted%20by%20the%20key.%22%22%22%0A%20%20%20%20%23%20Exercise%204.7%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20leave%20out%20the%20rows%20with%20a%20blank%20in%20any%20key%20column%20(dropna%20with%20subset%3Dkey)%0A%20%20%20%20%23%20Step%202%3A%20keep%20the%20rows%20whose%20key%20is%20duplicated%2C%20every%20copy%20(duplicated(subset%3Dkey%2C%20keep%3DFalse))%0A%20%20%20%20%23%20Step%203%3A%20sort%20them%20by%20the%20key%20and%20return%20them%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.7%22)%0A%0A%0A%40app.function%0Adef%20registrations_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Five%20boats%20of%20two%20households%3A%20one%20registration%20on%20two%20rows%2C%20two%20boats%20with%20no%20registration%2C%20and%20the%20same%0A%20%20%20%20registration%20in%20another%20household.%20The%20rows%20of%20Exercise%204.7's%20example%20table%2C%20used%20by%20its%20check.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2201%22%5D%20*%205%2C%20%22hh_id%22%3A%20%5B5300%2C%205300%2C%205300%2C%205300%2C%205301%5D%2C%20%22boat_no%22%3A%20%5B1%2C%203%2C%202%2C%204%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%22boat_tonnage%22%3A%20%5B1.1%2C%201.1%2C%200.4%2C%200.6%2C%201.1%5D%2C%0A%20%20%20%20%20%20%20%20%22boat_reg_no%22%3A%20pd.array(%5B%22R01-00101%22%2C%20%22R01-00101%22%2C%20None%2C%20None%2C%20%22R01-00101%22%5D%2C%20dtype%3D%22string%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%204.7%22%2C%20duplicate_rows%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22five%20boats%3A%20a%20registration%20on%20two%20rows%2C%20two%20blanks%2C%20the%20registration%20again%20in%20another%20household%22%2C%20(registrations_fixture()%2C%20%5B*HOUSEHOLD%2C%20%22boat_reg_no%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20boats%20by%20registration%22%2C%20(boats%2C%20%5B*HOUSEHOLD%2C%20%22boat_reg_no%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20units%20by%20UNIT_KEY%22%2C%20(units%2C%20UNIT_KEY))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20catch%20by%20unit%20and%20species%22%2C%20(catch%2C%20%5B*UNIT_KEY%2C%20%22catch_species%22%5D))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%20and%20keys%22%2C%20summarize%3Drows_in_order%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Drop%20the%20rows%20with%20a%20blank%20key%20part%2C%20keep%20every%20copy%20(keep%3DFalse)%20of%20the%20keys%20that%20repeat%2C%20sort%20by%20the%20key.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20units%20entered%20twice%2C%20every%20copy%0A%20%20%20%20try_it(duplicate_rows%2C%20units%2C%20UNIT_KEY)%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%204.7%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20The%20two%20boats%20with%20no%20registration%20are%20listed%20%7C%20Blank%20key%20parts%20count%20as%20equal%20in%20%60duplicated%60.%20%7C%20%60dropna(subset%3Dkey)%60%20first%20%7C%0A%20%20%20%20%7C%20One%20row%20per%20repeated%20key%2C%20not%20two%20%7C%20%60duplicated()%60%20marks%20the%20second%20copy%20only.%20%7C%20%60keep%3DFalse%60%20%7C%0A%20%20%20%20%7C%20Household%205301's%20boat%20is%20listed%20%7C%20The%20key%20passed%20had%20no%20household%2C%20or%20the%20household%20was%20dropped%20from%20it.%20%7C%20use%20the%20key%20as%20given%3A%20it%20starts%20with%20%60region_code%60%20and%20%60hh_id%60%20%7C%0A%20%20%20%20%7C%20Hundreds%20of%20boats%20%7C%20Both%20of%20the%20above%3A%20blanks%20kept%20and%20every%20copy%20marked.%20%7C%20%60dropna%60%2C%20then%20%60duplicated(keep%3DFalse)%60%20%7C%0A%20%20%20%20%7C%20*KeyError*%20with%20a%20column%20name%20%7C%20The%20key%20was%20given%20as%20one%20text%2C%20not%20a%20list.%20%7C%20%60subset%3Dkey%60%2C%20where%20key%20is%20a%20list%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%20Duplicates%20by%20a%20stated%20definition%0A%0A%20%20%20%20A%20key%20that%20repeats%20is%20one%20row%20stored%20twice.%20Some%20duplicates%20are%20not%20stored%20twice%3A%20they%20are%20one%20thing%20written%20under%20two%20names.%20The%20survey's%20edit%20specification%20states%20two%20definitions%20for%20them%2C%20each%20counting%20**distinct%20values**%20within%20a%20group%2C%20where%20a%20key%20subset%20counts%20**rows**%3A%0A%0A%20%20%20%20-%20**a%20registered%20boat%20under%20more%20than%20one%20unit%3A**%20more%20than%20one%20distinct%20%60operator_line%60%20or%20%60unit_no%60%20among%20a%20household's%20boat%20rows%20with%20one%20%60boat_reg_no%60%3B%0A%20%20%20%20-%20**an%20area%20in%20more%20than%20one%20stratum%3A**%20more%20than%20one%20distinct%20%60sel_stratum%60%20or%20%60sel_design_weight%60%20among%20a%20province's%20households%20with%20one%20%60sel_ea%60%20(the%20enumeration%20area%2C%20numbered%20within%20the%20province).%0A%0A%20%20%20%20A%20group%20with%20more%20than%20one%20distinct%20value%20is%20a%20boat%20that%20two%20units%20of%20the%20household%20use%2C%20or%20a%20boat%20entered%20under%20the%20wrong%20unit%3B%20an%20area%20whose%20households%20were%20selected%20in%20two%20strata%2C%20or%20a%20stratum%20keyed%20wrong.%20The%20data%20cannot%20tell%20them%20apart%2C%20so%20the%20definitions%20send%20the%20groups%20to%20a%20person%2C%20who%20checks%20and%20edits%20them%20by%20hand.%0A%0A%20%20%20%20One%20reading%20is%20needed%20before%20the%20boat%20definition%20can%20run.%20A%20blank%20registration%20is%20not%20one%20registration%3A%20counted%20with%20the%20blanks%20as%20a%20group%20of%20their%20own%2C%20the%20definition%20flags%20households%20whose%20two%20units%20each%20have%20an%20unregistered%20boat%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20boat%20definition%20with%20the%20blank%20registrations%20grouped%2C%20and%20without%20them%20(groupby's%20default%20drops%20a%20blank%20key)%0A%20%20%20%20_counted%20%3D%20%5B%22operator_line%22%2C%20%22unit_no%22%5D%0A%20%20%20%20_with_blanks%20%3D%20boats.groupby(%5B*HOUSEHOLD%2C%20%22boat_reg_no%22%5D%2C%20dropna%3DFalse)%5B_counted%5D.nunique()%0A%20%20%20%20_registered%20%3D%20boats.groupby(%5B*HOUSEHOLD%2C%20%22boat_reg_no%22%5D)%5B_counted%5D.nunique()%0A%20%20%20%20_flag%20%3D%20lambda%20counts%3A%20counts%5B(counts%20%3E%201).any(axis%3D1)%5D.reset_index()%5BHOUSEHOLD%5D.drop_duplicates().shape%5B0%5D%20%20%23%20noqa%3A%20E731%0A%20%20%20%20mo.md(f%22With%20a%20blank%20registration%20as%20a%20group%20of%20its%20own%20(%60groupby(...%2C%20dropna%3DFalse)%60)%3A%20**%7B_flag(_with_blanks)%7D**%20households%20flagged.%20Over%20the%20registered%20boats%20(%60groupby%60's%20default%2C%20which%20leaves%20a%20blank%20key%20out)%3A%20**%7B_flag(_registered)%7D**.%20The%20definition%20names%20a%20registration%2C%20so%20this%20course%20counts%20the%20registered%20boats%3B%20that%20is%20its%20reading%20of%20the%20definition's%20words%2C%20and%20the%20reason%20the%20exercise%20below%20groups%20with%20%60groupby%60's%20default.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%204.8.%20More%20than%20one%20distinct%20value%20within%20a%20group%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20both%20definitions%20are%20one%20question%20asked%20of%20other%20columns%3A%20which%20groups%20hold%20more%20than%20one%20distinct%20value%20of%20an%20item.%20One%20function%20answers%20both%2C%20and%20what%20it%20returns%20is%20the%20list%20a%20person%20checks.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20table%2C%20the%20columns%20that%20make%20a%20group%20(%60within%60)%20and%20the%20columns%20to%20count%20(%60counted%60)%20%E2%86%92%20one%20row%20per%20group%20in%20which%20**any**%20counted%20column%20has%20more%20than%20one%20distinct%20value%3A%20the%20%60within%60%20columns%2C%20then%20each%20counted%20column%20holding%20its%20number%20of%20distinct%20values.%20Rows%20sorted%20by%20%60within%60%2C%20numbered%20from%200.%20A%20blank%20is%20not%20a%20value%3A%20%60nunique()%60%20leaves%20it%20out%2C%20and%20%60groupby%60%20leaves%20out%20a%20group%20whose%20key%20is%20blank.%20For%20the%20registered%20boats%20of%20four%20households%2C%20%60within%60%20%3D%20%60%5B*HOUSEHOLD%2C%20%22boat_reg_no%22%5D%60%20and%20%60counted%60%20%3D%20%60%5B%22operator_line%22%2C%20%22unit_no%22%5D%60%3A%0A%0A%20%20%20%20%7C%20region_code%20%7C%20hh_id%20%7C%20boat_reg_no%20%7C%20operator_line%20%7C%20unit_no%20%7C%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%2001%20%7C%205300%20%7C%20R01-00101%20%7C%201%20%7C%201%20%7C%20one%20operator's%20two%20units%20%7C%0A%20%20%20%20%7C%2001%20%7C%205300%20%7C%20R01-00101%20%7C%201%20%7C%202%20%7C%20%7C%0A%20%20%20%20%7C%2001%20%7C%205301%20%7C%20R01-00102%20%7C%201%20%7C%201%20%7C%20two%20operators'%20units%20%7C%0A%20%20%20%20%7C%2001%20%7C%205301%20%7C%20R01-00102%20%7C%202%20%7C%201%20%7C%20%7C%0A%20%20%20%20%7C%2001%20%7C%205302%20%7C%20R01-00103%20%7C%201%20%7C%201%20%7C%20one%20row%2C%20stored%20twice%20%7C%0A%20%20%20%20%7C%2001%20%7C%205302%20%7C%20R01-00103%20%7C%201%20%7C%201%20%7C%20%7C%0A%20%20%20%20%7C%2004%20%7C%205300%20%7C%20R04-00101%20%7C%201%20%7C%201%20%7C%20one%20boat%2C%20in%20another%20region%20%7C%0A%0A%20%20%20%20gives%0A%0A%20%20%20%20%7C%20region_code%20%7C%20hh_id%20%7C%20boat_reg_no%20%7C%20operator_line%20%7C%20unit_no%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%3A--%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%2001%20%7C%205300%20%7C%20R01-00101%20%7C%201%20%7C%202%20%7C%0A%20%20%20%20%7C%2001%20%7C%205301%20%7C%20R01-00102%20%7C%202%20%7C%201%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60groupby(within)%5Bcounted%5D.nunique()%60%2C%20%60reset_index()%60%2C%20%60(counts%5Bcounted%5D%20%3E%201).any(axis%3D1)%60%2C%20%60sort_values(within)%60%2C%20%60reset_index(drop%3DTrue)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20more_than_one_distinct(table%3A%20pd.DataFrame%2C%20within%3A%20list%2C%20counted%3A%20list)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20groups%20(by%20%60within%60)%20in%20which%20any%20%60counted%60%20column%20has%20more%20than%20one%20distinct%20value%2C%20with%20those%20numbers.%22%22%22%0A%20%20%20%20%23%20Exercise%204.8%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20counts%20%3D%20the%20number%20of%20distinct%20values%20of%20each%20counted%20column%20per%20group%3A%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20table.groupby(within)%5Bcounted%5D.nunique()%2C%20then%20reset_index()%0A%20%20%20%20%23%20Step%202%3A%20keep%20the%20groups%20where%20any%20counted%20column%20is%20over%201%3A%20(counts%5Bcounted%5D%20%3E%201).any(axis%3D1)%0A%20%20%20%20%23%20Step%203%3A%20sort%20by%20within%2C%20reset_index(drop%3DTrue)%2C%20and%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%204.8%22)%0A%0A%0A%40app.function%0Adef%20shared_boats_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Seven%20boat%20rows%20of%20four%20households%3A%20one%20operator's%20two%20units%2C%20two%20operators'%20units%2C%20a%20row%20stored%20twice%2C%20and%20a%0A%20%20%20%20boat%20in%20another%20region%20with%20the%20same%20household%20number.%20Used%20by%20the%20text%20and%20the%20check%20of%20Exercise%204.8.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2204%22%5D%2C%20%22hh_id%22%3A%20%5B5300%2C%205300%2C%205301%2C%205301%2C%205302%2C%205302%2C%205300%5D%2C%0A%20%20%20%20%20%20%20%20%22boat_reg_no%22%3A%20pd.array(%5B%22R01-00101%22%2C%20%22R01-00101%22%2C%20%22R01-00102%22%2C%20%22R01-00102%22%2C%20%22R01-00103%22%2C%20%22R01-00103%22%2C%20%22R04-00101%22%5D%2C%20dtype%3D%22string%22)%2C%0A%20%20%20%20%20%20%20%20%22operator_line%22%3A%20%5B1%2C%201%2C%201%2C%202%2C%201%2C%201%2C%201%5D%2C%20%22unit_no%22%3A%20%5B1%2C%202%2C%201%2C%201%2C%201%2C%201%2C%201%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.function%0Adef%20areas_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Seven%20households%20of%20two%20provinces%3A%20an%20area%20with%20two%20strata%2C%20an%20area%20with%20two%20design%20weights%2C%20a%20household%20of%0A%20%20%20%20stratum%20F%20with%20no%20area%2C%20and%20an%20area%20of%20the%20other%20province%20with%20the%20first%20area's%20number.%20Used%20by%20the%20check%20of%0A%20%20%20%20Exercise%204.8.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2201%22%5D%20*%205%20%2B%20%5B%2204%22%5D%20*%202%2C%20%22province_code%22%3A%20%5B%22033%22%5D%20*%205%20%2B%20%5B%22010%22%5D%20*%202%2C%0A%20%20%20%20%20%20%20%20%22sel_ea%22%3A%20pd.array(%5B1003%2C%201003%2C%202004%2C%202004%2C%20None%2C%201003%2C%201003%5D%2C%20dtype%3D%22Int32%22)%2C%0A%20%20%20%20%20%20%20%20%22sel_stratum%22%3A%20%5B%22A%22%2C%20%22B%22%2C%20%22B%22%2C%20%22B%22%2C%20%22F%22%2C%20%22A%22%2C%20%22A%22%5D%2C%20%22sel_design_weight%22%3A%20%5B2.5%2C%202.5%2C%205.0%2C%205.5%2C%201.0%2C%202.5%2C%202.5%5D%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%204.8%22%2C%20more_than_one_distinct%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22seven%20boat%20rows%3A%20one%20operator's%20two%20units%2C%20two%20operators'%20units%2C%20a%20row%20stored%20twice%2C%20region%2004%22%2C%20(shared_boats_fixture()%2C%20%5B*HOUSEHOLD%2C%20%22boat_reg_no%22%5D%2C%20%5B%22operator_line%22%2C%20%22unit_no%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22seven%20households%3A%20an%20area%20in%20two%20strata%2C%20two%20weights%20in%20one%20area%2C%20stratum%20F%2C%20another%20province%22%2C%20(areas_fixture()%2C%20%5B%22region_code%22%2C%20%22province_code%22%2C%20%22sel_ea%22%5D%2C%20%5B%22sel_stratum%22%2C%20%22sel_design_weight%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20boats%2C%20by%20the%20definition%22%2C%20(boats%2C%20%5B*HOUSEHOLD%2C%20%22boat_reg_no%22%5D%2C%20%5B%22operator_line%22%2C%20%22unit_no%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20areas%2C%20by%20the%20definition%22%2C%20(selected%2C%20%5B%22region_code%22%2C%20%22province_code%22%2C%20%22sel_ea%22%5D%2C%20%5B%22sel_stratum%22%2C%20%22sel_design_weight%22%5D))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%20and%20groups%22%2C%20summarize%3Drows_in_order%2C%0A%20%20%20%20%20%20%20%20hint%3D%22groupby(within)%5Bcounted%5D.nunique()%2C%20keep%20the%20groups%20where%20any%20count%20is%20over%201%2C%20sort%20by%20within.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20areas%20whose%20households%20were%20selected%20in%20more%20than%20one%20stratum%2C%20by%20the%20definition%0A%20%20%20%20try_it(more_than_one_distinct%2C%20selected%2C%20%5B%22region_code%22%2C%20%22province_code%22%2C%20%22sel_ea%22%5D%2C%20%5B%22sel_stratum%22%2C%20%22sel_design_weight%22%5D)%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%204.8%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Household%205302%20is%20listed%2C%20with%202%20and%202%20%7C%20Rows%20were%20counted%2C%20not%20distinct%20values%3A%20%60size()%60%20or%20%60count()%60.%20%7C%20%60nunique()%60%20%7C%0A%20%20%20%20%7C%20Household%205300%20of%20region%2001%20is%20missing%20%7C%20Both%20counts%20had%20to%20be%20over%201%3A%20%60.all%60%2C%20or%20%60%26%60.%20%7C%20the%20definition%20says%20*or*%3A%20%60.any(axis%3D1)%60%20%7C%0A%20%20%20%20%7C%20Household%205300%20has%203%20units%2C%20or%20region%2004%20is%20missing%20%7C%20Grouped%20by%20%60hh_id%60%20alone%2C%20so%20two%20regions%20became%20one%20household.%20%7C%20group%20by%20%60within%60%20as%20given%3B%20it%20starts%20with%20%60region_code%60%20%7C%0A%20%20%20%20%7C%20A%20row%20for%20the%20stratum%20F%20household%2C%20with%20a%20blank%20%60sel_ea%60%20%7C%20%60groupby(...%2C%20dropna%3DFalse)%60%20kept%20the%20blank%20area%20as%20a%20group.%20%7C%20%60groupby%60's%20default%20leaves%20a%20blank%20key%20out%20%7C%0A%20%20%20%20%7C%20Every%20group%20is%20listed%2C%20with%20its%20counts%20%7C%20The%20groups%20were%20not%20filtered.%20%7C%20keep%20the%20rows%20where%20a%20count%20is%20over%201%20%7C%0A%20%20%20%20%7C%20*KeyError*%20with%20a%20column%20name%20%7C%20A%20column%20name%20typed%20instead%20of%20the%20%60within%60%20and%20%60counted%60%20lists.%20%7C%20use%20the%20arguments%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%20What%20each%20definition%20flags%20in%20the%20survey%0A%0A%20%20%20%20The%20cells%20below%20run%20your%20function%20with%20each%20definition%2C%20and%20set%20what%20it%20flags%20beside%20the%20planted%20list.%20The%20list%20planted%20two%20kinds%20these%20definitions%20are%20for%3A%20a%20registered%20boat%20entered%20under%20a%20second%20unit%20of%20its%20household%20(%60boat_under_two_units%60)%2C%20and%20one%20household's%20stratum%20keyed%20as%20another%20stratum%20of%20its%20area%20(%60area_in_two_strata%60).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Registered%20boats%20under%20more%20than%20one%20unit%3A%20which%20households%2C%20and%20what%20the%20planted%20list%20says%20of%20them%0A%20%20%20%20def%20_shared_boats()%3A%0A%20%20%20%20%20%20%20%20flagged%20%3D%20attempt(more_than_one_distinct%2C%20boats%2C%20%5B*HOUSEHOLD%2C%20%22boat_reg_no%22%5D%2C%20%5B%22operator_line%22%2C%20%22unit_no%22%5D)%0A%20%20%20%20%20%20%20%20if%20flagged%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercise%204.8%22)%0A%20%20%20%20%20%20%20%20kind%20%3D%20planted.loc%5Bplanted%5B%22kind%22%5D%20%3D%3D%20%22boat_under_two_units%22%2C%20HOUSEHOLD%5D.drop_duplicates()%0A%20%20%20%20%20%20%20%20households_flagged%20%3D%20flagged%5BHOUSEHOLD%5D.drop_duplicates()%0A%20%20%20%20%20%20%20%20caught%20%3D%20households_flagged.merge(kind%2C%20on%3DHOUSEHOLD%2C%20how%3D%22inner%22)%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22**A%20registered%20boat%20under%20more%20than%20one%20unit%3A**%20the%20definition%20flags%20**%7Blen(flagged)%7D%20registrations**%20in%20**%7Blen(households_flagged)%7D%20households**.%20**%7Blen(caught)%7D**%20of%20them%20are%20the%20%7Blen(kind)%7D%20households%20the%20planted%20list%20names%3A%20a%20boat%20entered%20a%20second%20time%20under%20another%20unit.%20The%20other%20**%7Blen(households_flagged)%20-%20len(caught)%7D**%20are%20households%20whose%20two%20units%20use%20one%20boat%2C%20and%20list%20it%20twice%2C%20honestly.%20The%20planted%20rows%20look%20exactly%20like%20the%20honest%20ones%3A%20the%20same%20registration%2C%20the%20same%20boat%2C%20a%20second%20unit.%20The%20definition%20cannot%20tell%20them%20apart%2C%20which%20is%20why%20a%20person%20checks%20every%20one.%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20flagged.head(10)%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_shared_boats()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Areas%20in%20more%20than%20one%20stratum%3A%20which%20areas%2C%20and%20the%20planted%20ones%0A%20%20%20%20def%20_areas()%3A%0A%20%20%20%20%20%20%20%20flagged%20%3D%20attempt(more_than_one_distinct%2C%20selected%2C%20%5B%22region_code%22%2C%20%22province_code%22%2C%20%22sel_ea%22%5D%2C%20%5B%22sel_stratum%22%2C%20%22sel_design_weight%22%5D)%0A%20%20%20%20%20%20%20%20if%20flagged%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercise%204.8%22)%0A%20%20%20%20%20%20%20%20kind%20%3D%20planted.loc%5Bplanted%5B%22kind%22%5D%20%3D%3D%20%22area_in_two_strata%22%2C%20HOUSEHOLD%5D%0A%20%20%20%20%20%20%20%20areas_planted%20%3D%20selected.merge(kind%2C%20on%3DHOUSEHOLD%2C%20how%3D%22inner%22)%5B%5B%22region_code%22%2C%20%22province_code%22%2C%20%22sel_ea%22%5D%5D.drop_duplicates()%0A%20%20%20%20%20%20%20%20caught%20%3D%20flagged.merge(areas_planted%2C%20on%3D%5B%22region_code%22%2C%20%22province_code%22%2C%20%22sel_ea%22%5D%2C%20how%3D%22inner%22)%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22**An%20area%20in%20more%20than%20one%20stratum%3A**%20the%20definition%20flags%20**%7Blen(flagged)%7D%20areas**%3B%20**%7Blen(caught)%7D**%20of%20them%20hold%20one%20of%20the%20%7Blen(kind)%7D%20households%20the%20planted%20list%20names.%20Each%20flagged%20area%20has%20two%20strata%20and%20one%20design%20weight%3A%20the%20household's%20stratum%20letter%20was%20keyed%20as%20another%20stratum's%2C%20and%20its%20weight%20kept%20its%20area's.%20Here%20the%20definition%20flags%20only%20planted%20errors%2C%20because%20the%20survey%20numbers%20each%20stratum's%20areas%20apart%20(from%201001%20in%20A%2C%202001%20in%20B%2C%203001%20in%20C).%20A%20person%20still%20decides%20each%3A%20whether%20the%20letter%20is%20wrong%2C%20or%20the%20area%20number.%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20flagged%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_areas()%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%20For%20a%20person%3A%20the%20review%20list%0A%0A%20%20%20%20The%20definitions'%20groups%20are%20checked%20and%20edited%20by%20hand%2C%20so%20the%20program's%20part%20ends%20with%20a%20list.%20The%20cell%20below%20sets%20out%20every%20row%20behind%20a%20flagged%20group%2C%20the%20boats%20under%20their%20units%20and%20the%20households%20of%20each%20flagged%20area%2C%20with%20an%20empty%20%60decision%60%20column%20for%20the%20editor%3A%20*one%20boat%2C%20both%20units%20use%20it*%2C%20*entered%20under%20the%20wrong%20unit%3A%20drop%20boat%203*%2C%20*stratum%20keyed%20wrong%3A%20A*%2C%20and%20so%20on.%20A%20decision%20written%20there%20is%20an%20edit%20like%20any%20other%2C%20and%20goes%20into%20the%20edit%20log%20when%20it%20is%20made.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Every%20row%20behind%20a%20flagged%20group%2C%20for%20the%20editor%20to%20decide%0A%20%20%20%20def%20_review()%3A%0A%20%20%20%20%20%20%20%20by_boat%20%3D%20attempt(more_than_one_distinct%2C%20boats%2C%20%5B*HOUSEHOLD%2C%20%22boat_reg_no%22%5D%2C%20%5B%22operator_line%22%2C%20%22unit_no%22%5D)%0A%20%20%20%20%20%20%20%20by_area%20%3D%20attempt(more_than_one_distinct%2C%20selected%2C%20%5B%22region_code%22%2C%20%22province_code%22%2C%20%22sel_ea%22%5D%2C%20%5B%22sel_stratum%22%2C%20%22sel_design_weight%22%5D)%0A%20%20%20%20%20%20%20%20if%20by_boat%20is%20NOT_YET%20or%20by_area%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercise%204.8%22)%0A%20%20%20%20%20%20%20%20hulls%20%3D%20boats.merge(by_boat%5B%5B*HOUSEHOLD%2C%20%22boat_reg_no%22%5D%5D%2C%20on%3D%5B*HOUSEHOLD%2C%20%22boat_reg_no%22%5D%2C%20how%3D%22inner%22)%5B%5B*UNIT_KEY%2C%20%22boat_no%22%2C%20%22boat_reg_no%22%2C%20%22boat_hull%22%2C%20%22boat_engine%22%2C%20%22boat_tonnage%22%5D%5D%0A%20%20%20%20%20%20%20%20homes%20%3D%20selected.merge(by_area%5B%5B%22region_code%22%2C%20%22province_code%22%2C%20%22sel_ea%22%5D%5D%2C%20on%3D%5B%22region_code%22%2C%20%22province_code%22%2C%20%22sel_ea%22%5D%2C%20how%3D%22inner%22)%5B%5B*HOUSEHOLD%2C%20%22province_code%22%2C%20%22sel_ea%22%2C%20%22sel_stratum%22%2C%20%22sel_design_weight%22%2C%20%22sel_final_weight%22%5D%5D%0A%20%20%20%20%20%20%20%20review%20%3D%20%7B%22boats%20under%20two%20units%22%3A%20hulls.assign(decision%3D%22%22).sort_values(%5B*HOUSEHOLD%2C%20%22boat_reg_no%22%2C%20%22operator_line%22%2C%20%22unit_no%22%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22areas%20in%20two%20strata%22%3A%20homes.assign(decision%3D%22%22).sort_values(%5B%22region_code%22%2C%20%22province_code%22%2C%20%22sel_ea%22%2C%20%22hh_id%22%5D)%7D%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(hulls)%7D**%20boat%20rows%20of%20%7Blen(by_boat)%7D%20registrations%2C%20and%20**%7Blen(homes)%7D**%20households%20of%20%7Blen(by_area)%7D%20areas%2C%20for%20an%20editor.%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.ui.tabs(%7Bname%3A%20table.reset_index(drop%3DTrue)%20for%20name%2C%20table%20in%20review.items()%7D)%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_review()%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%20Duplicate%20rules%20in%20the%20specification%20table%0A%0A%20%20%20%20A%20duplicate%20fits%20the%20specification%20table%20as%20any%20rule%20does%20once%20its%20fact%20is%20a%20column%3A%20each%20row%20gets%20the%20number%20of%20rows%20that%20share%20its%20key%2C%20with%20%60groupby(key).transform(%22size%22)%60%2C%20and%20the%20rule%20says%20that%20number%20must%20be%201.%20U1%2C%20C4%20and%20B5%20are%20the%20codebook's%20rules%20for%20the%20three%20key%20subsets%3A%20a%20unit's%20key%20is%20unique%2C%20a%20species%20is%20listed%20once%20per%20unit%2C%20and%20a%20registration%20is%20on%20one%20boat%20row%20of%20the%20household.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20with_copies(frames%3A%20dict)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22The%20rule%20tables%20with%20each%20row's%20number%20of%20copies%20by%20key%3A%20key_copies%20on%20the%20units%20(U1)%2C%20copies%20on%20the%20catch%20(C4%2C%0A%20%20%20%20by%20unit%20and%20species)%2C%20reg_copies%20on%20the%20boats%20(B5%2C%20by%20household%20and%20registration).%22%22%22%0A%20%20%20%20out%20%3D%20dict(frames)%0A%20%20%20%20fleet%2C%20fish%2C%20hulls%20%3D%20frames%5BUNITS%5D%2C%20frames%5BCATCH%5D%2C%20frames%5BBOATS%5D%0A%20%20%20%20out%5BUNITS%5D%20%3D%20fleet.assign(key_copies%3Dfleet.groupby(UNIT_KEY)%5B%22unit_no%22%5D.transform(%22size%22))%0A%20%20%20%20out%5BCATCH%5D%20%3D%20fish.assign(copies%3Dfish.groupby(%5B*UNIT_KEY%2C%20%22catch_species%22%5D)%5B%22catch_line%22%5D.transform(%22size%22))%0A%20%20%20%20%23%20a%20blank%20registration%20is%20in%20no%20group%2C%20so%20its%20count%20is%20missing%3B%20B5's%20scope%20leaves%20those%20boats%20out%0A%20%20%20%20out%5BBOATS%5D%20%3D%20hulls.assign(reg_copies%3Dhulls.groupby(%5B*HOUSEHOLD%2C%20%22boat_reg_no%22%5D)%5B%22boat_no%22%5D.transform(%22size%22))%0A%20%20%20%20return%20out%0A%0A%0A%40app.cell%0Adef%20_(edit_spec%2C%20frames)%3A%0A%20%20%20%20%23%20The%20specification%20table%20of%20section%204%20with%20the%20duplicate%20rules%20added%2C%20in%20the%20codebook's%20order%2C%20and%20the%20report%20of%0A%20%20%20%20%23%20every%20rule.%20The%20edits%20of%20U1%20and%20C4%20drop%20rows%2C%20which%20section%204's%20two%20columns%20do%20not%20describe%3B%20the%20cells%20below%20make%20them.%0A%20%20%20%20_conditions%20%3D%20pd.DataFrame(%5B(%22U1%22%2C%20%22%22%2C%20%22key_copies%20%3D%3D%201%22%2C%20%22edit%22)%2C%20(%22C4%22%2C%20%22%22%2C%20%22copies%20%3D%3D%201%22%2C%20%22edit%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%20%20%20%20(%22B5%22%2C%20%22boat_reg_no.notna()%22%2C%20%22reg_copies%20%3D%3D%201%22%2C%20%22refer%22)%5D%2C%20columns%3D%5B%22rule%22%2C%20%22applies_to%22%2C%20%22must_hold%22%2C%20%22action%22%5D)%0A%20%20%20%20_duplicates%20%3D%20(codebook_rules.merge(_conditions%2C%20on%3D%22rule%22%2C%20how%3D%22inner%22%2C%20validate%3D%22one_to_one%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.rename(columns%3D%7B%22rule_in_words%22%3A%20%22message%22%7D).assign(variable%3D%22%22%2C%20edit_to%3D%22%22)%5Bedit_spec.columns%5D)%0A%20%20%20%20_order%20%3D%20%7Brule%3A%20place%20for%20place%2C%20rule%20in%20enumerate(codebook_rules%5B%22rule%22%5D)%7D%0A%20%20%20%20full_spec%20%3D%20pd.concat(%5Bedit_spec%2C%20_duplicates%5D%2C%20ignore_index%3DTrue)%0A%20%20%20%20full_spec%20%3D%20full_spec.iloc%5Bfull_spec%5B%22rule%22%5D.map(_order).argsort()%5D.reset_index(drop%3DTrue)%0A%20%20%20%20full_frames%20%3D%20NOT_YET%20if%20frames%20is%20NOT_YET%20else%20with_copies(frames)%0A%20%20%20%20full_report%20%3D%20NOT_YET%20if%20full_frames%20is%20NOT_YET%20else%20attempt(run_rules%2C%20full_spec%2C%20full_frames%2C%20rule_values)%0A%20%20%20%20full_spec%5Bfull_spec%5B%22rule%22%5D.isin(%5B%22U1%22%2C%20%22C4%22%2C%20%22B5%22%5D)%5D%0A%20%20%20%20return%20full_frames%2C%20full_report%2C%20full_spec%0A%0A%0A%40app.cell%0Adef%20_(EXPECTED_RULES%2C%20full_report)%3A%0A%20%20%20%20%23%20Every%20planted%20kind%20the%20rules%20are%20written%20for%2C%20found%20or%20not%0A%20%20%20%20def%20_judge_all()%3A%0A%20%20%20%20%20%20%20%20if%20full_report%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercises%204.3%20to%204.5%22)%0A%20%20%20%20%20%20%20%20cases%20%3D%20judged(full_report%2C%20planted%2C%20EXPECTED_RULES)%0A%20%20%20%20%20%20%20%20summary%20%3D%20cases.groupby(%22kind%22).agg(planted%3D(%22found%22%2C%20%22size%22)%2C%20found%3D(%22found%22%2C%20%22sum%22)).reset_index()%0A%20%20%20%20%20%20%20%20missed%20%3D%20cases%5B~cases%5B%22found%22%5D%5D%0A%20%20%20%20%20%20%20%20note%20%3D%20(%22%22%20if%20missed.empty%20else%20f%22%3B%20the%20%7Blen(missed)%7D%20missed%20are%20the%20ownership%20codes%20section%203%20named%2C%20which%20no%20rule%20reads%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20missed%5B%22detail%22%5D.str.contains(%22unit_ownership%22).all()%20else%20f%22%3B%20%7Blen(missed)%7D%20are%20missed%22)%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5Bmo.md(f%22The%20full%20report%3A%20**%7Blen(full_report)%7D%20rows**.%20Planted%20defects%20found%3A%20**%7Bint(cases%5B'found'%5D.sum())%7D%20of%20%7Blen(cases)%7D**%7Bnote%7D.%22)%2C%20summary%5D)%0A%0A%20%20%20%20_judge_all()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20What%20the%20report%20finds%20besides%0A%0A%20%20%20%20A%20check%20that%20finds%20every%20planted%20defect%20has%20done%20half%20its%20job.%20The%20other%20half%20is%20what%20else%20it%20flags%3A%20a%20rule%20that%20fires%20on%20correct%20rows%20wastes%20an%20editor's%20time%20and%20teaches%20them%20to%20ignore%20it.%20The%20cell%20below%20lists%20every%20report%20row%20that%20the%20planted%20list%20does%20not%20explain%2C%20a%20rule%20firing%20on%20a%20household%20where%20the%20list%20planted%20nothing%20for%20that%20rule%2C%20with%20what%20the%20list%20says%20of%20the%20household%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(EXPECTED_RULES%2C%20full_report)%3A%0A%20%20%20%20%23%20Report%20rows%20the%20planted%20list%20does%20not%20explain%3A%20a%20rule%20on%20a%20household%20where%20no%20kind%20it%20is%20written%20for%20was%20planted%0A%20%20%20%20def%20_besides()%3A%0A%20%20%20%20%20%20%20%20if%20full_report%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercises%204.3%20to%204.5%22)%0A%20%20%20%20%20%20%20%20kinds%20%3D%20planted.groupby(HOUSEHOLD)%5B%22kind%22%5D.agg(lambda%20k%3A%20sorted(set(k))).rename(%22kinds%20in%20the%20list%22).reset_index()%0A%20%20%20%20%20%20%20%20rows%20%3D%20full_report.merge(kinds%2C%20on%3DHOUSEHOLD%2C%20how%3D%22left%22)%0A%20%20%20%20%20%20%20%20rows%5B%22kinds%20in%20the%20list%22%5D%20%3D%20rows%5B%22kinds%20in%20the%20list%22%5D.apply(lambda%20k%3A%20k%20if%20isinstance(k%2C%20list)%20else%20%5B%5D)%0A%20%20%20%20%20%20%20%20explained%20%3D%20%5Bany(rule%20in%20EXPECTED_RULES.get(kind%2C%20set())%20for%20kind%20in%20k)%20for%20rule%2C%20k%20in%20zip(rows%5B%22rule%22%5D%2C%20rows%5B%22kinds%20in%20the%20list%22%5D)%5D%0A%20%20%20%20%20%20%20%20extra%20%3D%20rows%5B%5Bnot%20e%20for%20e%20in%20explained%5D%5D.copy()%0A%20%20%20%20%20%20%20%20planted_for_rules%20%3D%20%5Bany(kind%20in%20EXPECTED_RULES%20for%20kind%20in%20k)%20for%20k%20in%20extra%5B%22kinds%20in%20the%20list%22%5D%5D%0A%20%20%20%20%20%20%20%20extra%5B%22what%20it%20is%22%5D%20%3D%20np.select(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5Bnp.array(planted_for_rules%2C%20dtype%3Dbool)%2C%20extra%5B%22kinds%20in%20the%20list%22%5D.str.len().to_numpy()%20%3E%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B%22a%20second%20rule%20on%20a%20planted%20defect%22%2C%20%22a%20defect%20of%20another%20kind%22%5D%2C%20%22nothing%20planted%3A%20a%20false%20alarm%22)%0A%20%20%20%20%20%20%20%20extra%5B%22kinds%20in%20the%20list%22%5D%20%3D%20extra%5B%22kinds%20in%20the%20list%22%5D.str.join(%22%2C%20%22)%0A%20%20%20%20%20%20%20%20tally%20%3D%20extra.groupby(%5B%22what%20it%20is%22%2C%20%22rule%22%2C%20%22kinds%20in%20the%20list%22%5D).size().rename(%22households%22).reset_index()%0A%20%20%20%20%20%20%20%20alarms%20%3D%20int((extra%5B%22what%20it%20is%22%5D%20%3D%3D%20%22nothing%20planted%3A%20a%20false%20alarm%22).sum())%0A%20%20%20%20%20%20%20%20%23%20Why%20each%20pair%20of%20rule%20and%20planted%20kind%20shows%20up%2C%20for%20the%20pairs%20this%20survey%20has%0A%20%20%20%20%20%20%20%20why%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22M3%22%2C%20%22operator_too_young%22)%3A%20%22**M3%20on%20a%20member%20who%20runs%20a%20unit%20and%20is%20too%20young.**%20The%20member%20made%20younger%20than%2010%20still%20answers%20the%20fishing%20items%2C%20which%20M3%20allows%20only%20from%2010%2C%20so%20the%20household%20fails%20M3%20beside%20M4.%20One%20defect%2C%20two%20rules.%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22U3%22%2C%20%22stored_tonnage_wrong%22)%3A%20%22**U3%20on%20a%20wrong%20stored%20tonnage.**%20A%20tonnage%20stored%20with%20its%20factor%20left%20out%20puts%20a%20small%20boat%20at%205%20tons%20or%20more%2C%20so%20its%20unit's%20scale%20fails%20U3%20beside%20B3.%20When%20B3's%20edit%20recomputes%20the%20tonnage%2C%20the%20U3%20flag%20goes%20too%2C%20as%20the%20end%20of%20this%20section%20shows.%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22U5%22%2C%20%22uses_boat_blank%22)%3A%20%22**U5%20on%20a%20blank%20%60unit_uses_boat%60.**%20A%20blank%20answer%20cannot%20agree%20with%20the%20boat%20rows%2C%20so%20the%20unit%20fails%20U5%20beside%20U4.%20When%20U4's%20edit%20answers%20it%20from%20the%20boat%20rows%2C%20the%20U5%20flag%20goes%20too.%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22B5%22%2C%20%22household_copied%22)%3A%20%22**B5%20on%20a%20copied%20household.**%20The%20household%20copied%20under%20another%20id%20carries%20its%20original's%20boats%2C%20and%20the%20original's%20two%20units%20share%20one%20registered%20boat%2C%20so%20the%20copy%20fails%20B5%20as%20the%20original%20does.%20The%20copy%20itself%20is%20for%20module%206%20and%20module%208%20to%20find.%22%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%23%20Why%20a%20rule%20fires%20where%20nothing%20was%20planted%2C%20for%20the%20rules%20that%20do%20so%20in%20this%20survey%0A%20%20%20%20%20%20%20%20alarm_why%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22B5%22%3A%20%22B5's%20words%20say%20a%20registration%20is%20on%20one%20boat%20row%20of%20the%20household%2C%20and%20a%20household%20whose%20two%20units%20both%20use%20one%20boat%20lists%20it%20under%20each%20unit.%20The%20rows%20are%20right%2C%20and%20the%20rule%20fires%20on%20them.%20Section%205's%20definition%20shows%20why%20the%20rule%20cannot%20be%20written%20more%20sharply%3A%20the%20planted%20boat%20under%20a%20second%20unit%20looks%20exactly%20like%20a%20shared%20one.%20B5%20therefore%20refers%2C%20and%20an%20editor%20confirms%20each%20shared%20boat%3B%20a%20rule%20that%20fires%20this%20often%20on%20correct%20rows%20is%20worth%20telling%20the%20editors%20about%2C%20so%20they%20read%20its%20referrals%20as%20questions%20and%20not%20as%20errors.%22%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20pairs%20%3D%20sorted(%7B(rule%2C%20kind)%20for%20rule%2C%20kinds%20in%20zip(extra%5B%22rule%22%5D%2C%20extra%5B%22kinds%20in%20the%20list%22%5D.str.split(%22%2C%20%22))%20for%20kind%20in%20kinds%20if%20kind%7D)%0A%20%20%20%20%20%20%20%20lines%20%3D%20%5Bwhy.get(pair%2C%20f%22**%7Bpair%5B0%5D%7D%20on%20a%20household%20planted%20as%20%7Bpair%5B1%5D%7D.**%20Not%20explained%20here%3A%20open%20the%20household%20and%20read%20its%20rows.%22)%20for%20pair%20in%20pairs%5D%0A%20%20%20%20%20%20%20%20by_rule%20%3D%20extra.loc%5Bextra%5B%22what%20it%20is%22%5D%20%3D%3D%20%22nothing%20planted%3A%20a%20false%20alarm%22%2C%20%22rule%22%5D.value_counts()%0A%20%20%20%20%20%20%20%20lines%20%2B%3D%20%5Bf%22**%7Brule%7D%2C%20%7Bn%7D%20false%20alarms.**%20%22%20%2B%20alarm_why.get(rule%2C%20%22Not%20explained%20here%3A%20open%20the%20households%20and%20read%20their%20rows.%22)%20for%20rule%2C%20n%20in%20by_rule.items()%5D%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(extra)%7D%20report%20rows**%20the%20list%20does%20not%20explain%3B%20**%7Balarms%7D**%20of%20them%20are%20false%20alarms%2C%20flags%20on%20a%20household%20where%20nothing%20was%20planted.%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20tally%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(%22%5Cn%5Cn%22.join(f%22-%20%7Bline%7D%22%20for%20line%20in%20lines))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20extra%5B%5B%22rule%22%2C%20*HOUSEHOLD%2C%20%22rows%22%2C%20%22kinds%20in%20the%20list%22%2C%20%22what%20it%20is%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_besides()%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%20A%20rule%20that%20fires%20beside%20another%20on%20the%20same%20defect%20is%20no%20false%20alarm%3A%20the%20household%20is%20wrong%2C%20and%20the%20second%20flag%20tells%20the%20editor%20where%20the%20error%20shows.%20A%20rule%20that%20fires%20on%20a%20defect%20planted%20for%20another%20module%20is%20a%20true%20finding%20too.%20A%20false%20alarm%20is%20a%20flag%20on%20a%20household%20where%20nothing%20is%20wrong%2C%20and%20the%20count%20above%20is%20the%20number%20of%20them.%20A%20rule%20with%20many%20is%20worth%20a%20second%20look%3A%20it%20may%20be%20written%20too%20strictly%2C%20or%2C%20as%20with%20a%20registration%20that%20two%20units%20share%2C%20the%20data%20may%20hold%20nothing%20that%20would%20let%20it%20be%20written%20better.%0A%0A%20%20%20%20%23%23%23%20The%20edits%20that%20drop%20a%20row%0A%0A%20%20%20%20Section%204's%20table%20of%20decisions%20edits%20U1%20and%20C4%20by%20dropping%20a%20copy%2C%20and%20only%20an%20exact%20one.%20A%20failure%20that%20an%20edit%20should%20have%20cleared%20and%20did%20not%2C%20such%20as%20a%20copy%20that%20is%20not%20exact%2C%20stays%20in%20the%20second%20report%20and%20is%20referred%20with%20the%20rest.%20The%20one%20edit%20a%20program%20makes%20on%20duplicates%20is%20%60drop_exact_copies%60%2C%20below%3A%20it%20drops%20a%20row%20equal%20to%20an%20earlier%20row%20in%20every%20column%20but%20its%20row%20id%2C%20and%20keeps%20the%20first.%20Every%20other%20column%20is%20compared%2C%20the%20region%20included%2C%20so%20a%20row%20that%20shares%20a%20key%20but%20differs%20in%20a%20value%20stays%20for%20an%20editor.%20The%20stretch%20S1%2C%20%5Bprocessing%5D(%2F%3Ffile%3D36_stretch_processing.py)%2C%20has%20it%20as%20an%20exercise.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20drop_exact_copies(table%3A%20pd.DataFrame%2C%20id_column%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20table%20without%20rows%20equal%20to%20an%20earlier%20row%20in%20every%20column%20but%20the%20row%20id%3B%20the%20first%20copy%20is%20kept.%22%22%22%0A%20%20%20%20compared%20%3D%20%5Bcolumn%20for%20column%20in%20table.columns%20if%20column%20!%3D%20id_column%5D%0A%20%20%20%20return%20table.drop_duplicates(subset%3Dcompared%2C%20keep%3D%22first%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20The%20edits%2C%20then%20every%20rule%20again%0A%0A%20%20%20%20The%20cell%20below%20makes%20every%20edit%20on%20copies%20of%20the%20tables%2C%20in%20a%20fixed%20order%3A%20the%20exact%20copies%20dropped%20(U1%2C%20C4)%3B%20the%20two%20edits%20written%20as%20code%2C%20H1%20and%20B3%3B%20then%20every%20edit%20the%20specification%20carries%2C%20with%20your%20Exercise%204.6%20(U4%2C%20U7%2C%20B2%2C%20W1).%20The%20tonnage%20is%20recomputed%20before%20B2%20reads%20it%2C%20so%20a%20boat%20whose%20stored%20tonnage%20was%20wrong%20is%20not%20given%20an%20engine%20for%20it.%20Every%20change%20goes%20into%20one%20edit%20log%2C%20with%20the%20columns%20of%20Exercise%204.6.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(full_frames%2C%20full_spec)%3A%0A%20%20%20%20%23%20Every%20edit%2C%20in%20order%3A%20exact%20copies%20dropped%20(U1%2C%20C4)%3B%20counts%20set%20to%20the%20roster%20(H1)%20and%20tonnage%20recomputed%20(B3)%2C%20as%0A%20%20%20%20%23%20code%3B%20then%20the%20specification's%20edits%20(U4%2C%20U7%2C%20B2%2C%20W1)%2C%20with%20Exercise%204.6.%20One%20log%2C%20EDIT_LOG_COLUMNS.%0A%20%20%20%20def%20_edit()%3A%0A%20%20%20%20%20%20%20%20if%20full_frames%20is%20NOT_YET%20or%20attempt(apply_edits%2C%20full_spec.head(0)%2C%20%7B%7D%2C%20rule_values%2C%20SEED)%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20NOT_YET%2C%20NOT_YET%0A%20%20%20%20%20%20%20%20edited%2C%20log%20%3D%20dict(tables)%2C%20%5B%5D%0A%0A%20%20%20%20%20%20%20%20def%20entry(rule%2C%20name%2C%20row%2C%20variable%2C%20before%2C%20after%2C%20how)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%7B%22rule%22%3A%20rule%2C%20%22table%22%3A%20name%2C%20%22region_code%22%3A%20row%5B%22region_code%22%5D%2C%20%22hh_id%22%3A%20int(row%5B%22hh_id%22%5D)%2C%20%22row%22%3A%20int(row%5BROW_ID%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22variable%22%3A%20variable%2C%20%22before%22%3A%20before%2C%20%22after%22%3A%20after%2C%20%22how%22%3A%20how%7D%0A%0A%20%20%20%20%20%20%20%20for%20name%2C%20rule%20in%20%5B(UNITS%2C%20%22U1%22)%2C%20(CATCH%2C%20%22C4%22)%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20kept%20%3D%20drop_exact_copies(edited%5Bname%5D%2C%20ROW_ID)%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20a%20row%20id%20is%20unique%20within%20its%20region's%20database%20only%2C%20so%20a%20row%20is%20named%20by%20the%20region%20and%20its%20id%0A%20%20%20%20%20%20%20%20%20%20%20%20stored%20%3D%20pd.MultiIndex.from_frame(edited%5Bname%5D%5B%5B%22region_code%22%2C%20ROW_ID%5D%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20gone%20%3D%20edited%5Bname%5D%5B~stored.isin(pd.MultiIndex.from_frame(kept%5B%5B%22region_code%22%2C%20ROW_ID%5D%5D))%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20log%20%2B%3D%20%5Bentry(rule%2C%20name%2C%20r%2C%20%22%22%2C%20%22%22%2C%20%22dropped%22%2C%20%22an%20exact%20copy%20of%20an%20earlier%20row%22)%20for%20_%2C%20r%20in%20gone.iterrows()%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20edited%5Bname%5D%20%3D%20kept%0A%20%20%20%20%20%20%20%20now%20%3D%20rule_frames(edited)%0A%20%20%20%20%20%20%20%20%23%20H1%3A%20the%20roster%20prevails%20where%20the%20listing%20(hh_listed_members)%20agrees%20with%20it%3B%20otherwise%20the%20count%20is%20left%20for%20an%20editor%0A%20%20%20%20%20%20%20%20homes%20%3D%20now%5BHOUSEHOLDS%5D%0A%20%20%20%20%20%20%20%20fix%20%3D%20(homes%5B%22hh_roster_members%22%5D%20!%3D%20homes%5B%22member_rows%22%5D)%20%26%20(homes%5B%22hh_listed_members%22%5D%20%3D%3D%20homes%5B%22member_rows%22%5D)%0A%20%20%20%20%20%20%20%20log%20%2B%3D%20%5Bentry(%22H1%22%2C%20HOUSEHOLDS%2C%20r%2C%20%22hh_roster_members%22%2C%20as_text(r%5B%22hh_roster_members%22%5D)%2C%20as_text(r%5B%22member_rows%22%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22member_rows%2C%20where%20hh_listed_members%20agrees%22)%20for%20_%2C%20r%20in%20homes%5Bfix%5D.iterrows()%5D%0A%20%20%20%20%20%20%20%20edited%5BHOUSEHOLDS%5D%20%3D%20homes.assign(hh_roster_members%3Dhomes%5B%22hh_roster_members%22%5D.where(~fix%2C%20homes%5B%22member_rows%22%5D))%5Btables%5BHOUSEHOLDS%5D.columns%5D%0A%20%20%20%20%20%20%20%20%23%20B3%3A%20a%20stored%20tonnage%20outside%20the%20tolerance%20is%20recomputed%2C%20where%20the%20dimensions%20or%20the%20papers%20give%20one%0A%20%20%20%20%20%20%20%20hulls%20%3D%20now%5BBOATS%5D%0A%20%20%20%20%20%20%20%20wrong%20%3D%20~((hulls%5B%22boat_tonnage%22%5D%20-%20hulls%5B%22tonnage_expected%22%5D).abs()%20%3C%3D%20rule_values%5B%22tolerance%22%5D).fillna(False)%20%26%20hulls%5B%22tonnage_expected%22%5D.notna()%0A%20%20%20%20%20%20%20%20log%20%2B%3D%20%5Bentry(%22B3%22%2C%20BOATS%2C%20r%2C%20%22boat_tonnage%22%2C%20f%22%7Br%5B'boat_tonnage'%5D%3A.2f%7D%22%2C%20f%22%7Br%5B'tonnage_expected'%5D%3A.2f%7D%22%2C%20%22tonnage_expected%2C%20rounded%20to%20hundredths%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20_%2C%20r%20in%20hulls%5Bwrong%5D.iterrows()%5D%0A%20%20%20%20%20%20%20%20fixed%20%3D%20edited%5BBOATS%5D.copy()%0A%20%20%20%20%20%20%20%20fixed.loc%5Bwrong%2C%20%22boat_tonnage%22%5D%20%3D%20hulls.loc%5Bwrong%2C%20%22tonnage_expected%22%5D.round(2)%0A%20%20%20%20%20%20%20%20edited%5BBOATS%5D%20%3D%20fixed%0A%20%20%20%20%20%20%20%20%23%20The%20specification's%20edits%2C%20on%20the%20tables%20as%20the%20edits%20above%20left%20them%3B%20back%20to%20the%20survey's%20own%20columns%0A%20%20%20%20%20%20%20%20by_spec%2C%20spec_log%20%3D%20apply_edits(full_spec%2C%20rule_frames(edited)%2C%20rule_values%2C%20SEED)%0A%20%20%20%20%20%20%20%20edited%20%3D%20%7Bname%3A%20by_spec%5Bname%5D%5Btables%5Bname%5D.columns%5D%20for%20name%20in%20tables%7D%0A%20%20%20%20%20%20%20%20return%20edited%2C%20pd.concat(%5Bpd.DataFrame(log%2C%20columns%3DEDIT_LOG_COLUMNS)%2C%20spec_log%5D%2C%20ignore_index%3DTrue)%0A%0A%20%20%20%20edited_tables%2C%20edit_log%20%3D%20_edit()%0A%20%20%20%20edit_log%20if%20edit_log%20is%20not%20NOT_YET%20else%20waiting_for(%22Exercises%204.3%20to%204.6%22)%0A%20%20%20%20return%20edit_log%2C%20edited_tables%0A%0A%0A%40app.cell%0Adef%20_(edit_log%2C%20edited_tables%2C%20full_report%2C%20full_spec)%3A%0A%20%20%20%20%23%20Every%20rule%20again%2C%20on%20the%20edited%20tables%3A%20what%20still%20fails%20is%20referred%0A%20%20%20%20referred%20%3D%20NOT_YET%20if%20edited_tables%20is%20NOT_YET%20else%20attempt(run_rules%2C%20full_spec%2C%20with_copies(rule_frames(edited_tables))%2C%20rule_values)%0A%0A%20%20%20%20def%20_compare()%3A%0A%20%20%20%20%20%20%20%20if%20referred%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercises%204.3%20to%204.6%22)%0A%20%20%20%20%20%20%20%20counts%20%3D%20pd.concat(%7B%22before%20the%20edits%22%3A%20full_report.groupby(%22rule%22).size()%2C%20%22after%20the%20edits%22%3A%20referred.groupby(%22rule%22).size()%7D%2C%20axis%3D1)%0A%20%20%20%20%20%20%20%20counts%20%3D%20counts.reindex(full_spec%5B%22rule%22%5D).fillna(0).astype(int)%0A%20%20%20%20%20%20%20%20households_of%20%3D%20lambda%20frame%2C%20rule%3A%20set(map(tuple%2C%20frame.loc%5Bframe%5B%22rule%22%5D%20%3D%3D%20rule%2C%20HOUSEHOLD%5D.to_numpy()))%20%20%23%20noqa%3A%20E731%0A%20%20%20%20%20%20%20%20still_edit%20%3D%20referred%5Breferred%5B%22action%22%5D%20%3D%3D%20%22edit%22%5D%0A%20%20%20%20%20%20%20%20u3_gone%20%3D%20households_of(full_report%2C%20%22U3%22)%20-%20households_of(referred%2C%20%22U3%22)%0A%20%20%20%20%20%20%20%20b3_households%20%3D%20households_of(edit_log%2C%20%22B3%22)%0A%20%20%20%20%20%20%20%20b4_after%2C%20b2_edited%20%3D%20households_of(referred%2C%20%22B4%22)%2C%20households_of(edit_log%2C%20%22B2%22)%0A%20%20%20%20%20%20%20%20by_rule%20%3D%20%22%2C%20%22.join(f%22%7Brule%7D%20%7Bn%7D%22%20for%20rule%2C%20n%20in%20edit_log%5B%22rule%22%5D.value_counts().sort_index().items())%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22The%20edit%20log%20has%20**%7Blen(edit_log)%7D%20changes**%20(%7Bby_rule%7D).%20Households%20failing%20each%20rule%2C%20before%20and%20after%3A%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20counts%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22The%20rules%20with%20%60edit%60%20still%20fail%20on%20**%7Blen(still_edit)%7D**%20households%2C%20all%20of%20them%20%7B'%2C%20'.join(sorted(set(still_edit%5B'rule'%5D)))%20or%20'none'%7D%3A%20the%20counts%20H1's%20edit%20leaves%20to%20an%20editor%2C%20where%20the%20listing%20agrees%20with%20the%20count%20and%20not%20with%20the%20roster.%20U3%2C%20a%20rule%20no%20edit%20touches%2C%20lost%20**%7Blen(u3_gone)%7D**%20households%2C%20%7Blen(u3_gone%20%26%20b3_households)%7D%20of%20them%20households%20whose%20stored%20tonnage%20B3%20recomputed%3A%20the%20cause%20corrected%2C%20the%20second%20flag%20went%20with%20it.%20An%20edit%20can%20also%20make%20a%20rule%20fail%3A%20B4%20fails%20on%20**%7Blen(b4_after)%7D**%20households%20after%20the%20edits%2C%20**%7Blen(b4_after%20%26%20b2_edited)%7D**%20of%20them%20boats%20B2%20gave%20an%20engine.%20Their%20power%20is%20the%20one%20answer%20left%20to%20find.%20The%20referral%20list%20is%20the%20second%20report%3A%20**%7Blen(referred)%7D%20rows**%20on%20**%7Breferred%5BHOUSEHOLD%5D.drop_duplicates().shape%5B0%5D%7D%20households**%2C%20every%20one%20for%20an%20editor.%22)%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_compare()%0A%20%20%20%20return%20(referred%2C)%0A%0A%0A%40app.cell%0Adef%20_(referred)%3A%0A%20%20%20%20%23%20The%20referral%20list%3A%20the%20second%20report%2C%20one%20row%20per%20rule%20and%20household%2C%20for%20the%20editors%0A%20%20%20%20referred%20if%20referred%20is%20not%20NOT_YET%20else%20waiting_for(%22Exercises%204.3%20to%204.6%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22The%20R%20way%2C%20and%20what%20changes%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20-%20**Exact%20copies%3A**%20%60distinct(select(table%2C%20-row_id))%60%20in%20dplyr%20drops%20the%20id%20along%20with%20the%20copies%3B%20%60drop_duplicates(subset%3D...)%60%20compares%20without%20the%20id%20and%20keeps%20it%2C%20which%20the%20edit%20log%20needs.%0A%20%20%20%20-%20**Every%20copy%20of%20a%20key%3A**%20%60group_by(across(all_of(key)))%20%7C%3E%20filter(n()%20%3E%201)%60%20is%20%60duplicated(subset%3Dkey%2C%20keep%3DFalse)%60.%20dplyr's%20%60group_by%60%20keeps%20%60NA%60%20as%20a%20group%2C%20as%20pandas'%20%60duplicated%60%20does%2C%20so%20drop%20the%20blank%20keys%20first%20in%20both.%0A%20%20%20%20-%20**%60n_distinct%60%20by%20group**%20counts%20distinct%20values%3B%20%60nunique()%60%20is%20pandas'%20name.%20The%20boat%20definition%2C%20%60group_by(region_code%2C%20hh_id%2C%20boat_reg_no)%20%7C%3E%20summarise(across(c(operator_line%2C%20unit_no)%2C%20n_distinct))%20%7C%3E%20filter(operator_line%20%3E%201%20%7C%20unit_no%20%3E%201)%60%2C%20is%20Exercise%204.8's%20%60groupby(within)%5Bcounted%5D.nunique()%60.%20dplyr's%20%60group_by%60%20keeps%20a%20blank%20registration%20as%20a%20group%2C%20as%20%60groupby(...%2C%20dropna%3DFalse)%60%20does%3B%20filter%20it%20out%20first.%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%20What%20you%20can%20do%20now%0A%0A%20%20%20%20%7C%20to%20%7C%20use%20%7C%20and%20check%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20count%20a%20coded%20item%20against%20the%20codebook%20%7C%20%60codebook_codes%60%2C%20%60code_frequency%60%3A%20valid%20codes%2C%20%22Invalid%20value%22%2C%20%22No%20answer%22%20%7C%20%60case_when%60%20conditions%20built%20with%20%60isin%60%3A%20a%20missing%20condition%20counts%20as%20met%20%7C%0A%20%20%20%20%7C%20write%20a%20range%20or%20skip%20rule%20%7C%20a%20scope%20(%60applies_to%60)%20and%20a%20condition%20(%60must_hold%60)%20%7C%20%60fillna(False)%60%3A%20a%20needed%20value%20that%20is%20blank%20fails%20%7C%0A%20%20%20%20%7C%20write%20a%20rule%20across%20tables%20%7C%20the%20other%20table's%20fact%20as%20a%20column%20first%3A%20%60groupby(...).size()%60%2C%20%60.max()%60%2C%20a%20left%20%60merge%60%20%7C%20the%20region%20in%20every%20key%3B%20%60validate%3D%60%20on%20the%20join%20%7C%0A%20%20%20%20%7C%20compare%20computed%20numbers%20%7C%20%60abs(a%20-%20b)%20%3C%3D%20tolerance%60%20%7C%20never%20%60%3D%3D%60%20on%20a%20rounded%20computation%20%7C%0A%20%20%20%20%7C%20run%20rules%20as%20data%20%7C%20a%20specification%20table%20and%20%60run_rules%60%2C%20to%20one%20failure%20report%20%7C%20one%20row%20per%20rule%20and%20household%3B%20%60REPORT_COLUMNS%60%20%7C%0A%20%20%20%20%7C%20let%20a%20rule%20carry%20its%20edit%20%7C%20%60variable%60%20and%20%60edit_to%60%20in%20the%20specification%2C%20%60apply_edits%60%2C%20an%20edit%20log%20%7C%20a%20draw%20seeded%2C%20in%20key%20order%2C%20and%20logged%20with%20its%20seed%3B%20each%20rule%20reads%20the%20tables%20as%20the%20rules%20before%20it%20left%20them%20%7C%0A%20%20%20%20%7C%20find%20duplicates%20by%20key%20%7C%20%60duplicated(subset%3Dkey%2C%20keep%3DFalse)%60%20on%20key%20subsets%20%7C%20the%20row%20id%20left%20out%3B%20blank%20key%20parts%20dropped%20first%20%7C%0A%20%20%20%20%7C%20find%20duplicates%20by%20a%20stated%20definition%20%7C%20%60groupby(within)%5Bcounted%5D.nunique()%60%2C%20groups%20over%201%20%7C%20distinct%20values%2C%20not%20rows%3B%20the%20list%20goes%20to%20a%20person%20%7C%0A%20%20%20%20%7C%20edit%20or%20refer%20%7C%20every%20edit%20in%20a%20fixed%20order%20with%20one%20log%2C%20then%20every%20rule%20again%20%7C%20what%20fails%20the%20second%20run%20is%20the%20referral%20list%20%7C%0A%0A%20%20%20%20Next%2C%20the%20drill%20D1%20(%6035_debugging_drill.py%60)%3A%20a%20flat%20table%20that%20lies%2C%20with%20six%20bugs%20to%20find%20and%20fix.%20The%20stretch%20S1%20(%5B%6036_stretch_processing.py%60%5D(%2F%3Ffile%3D36_stretch_processing.py))%20has%20five%20more%20exercises%20beside%20modules%201%2C%202%20and%204.%20Then%20module%205%20(%6040_recode_and_tabulate.py%60)%20turns%20the%20edited%20codes%20into%20the%20categories%20a%20table%20is%20made%20of.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%224.5%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
bd0cf1c5ed994fd4d4374cf67429e636