From a8e60b3aecbf6b208d35cb4fe9e285b117408ed2 Mon Sep 17 00:00:00 2001 From: Dhirendra Singh Date: Tue, 16 Mar 2021 11:04:11 +1100 Subject: [PATCH] Write out map of PlanId > AgentId > GroupId; #4 --- R/plan.R | 34 ++ tests/expected/4.plan/plan2agent2group.csv | 428 +++++++++++++++++++++ tests/testthat/testPlan.R | 12 +- 3 files changed, 473 insertions(+), 1 deletion(-) create mode 100644 tests/expected/4.plan/plan2agent2group.csv diff --git a/R/plan.R b/R/plan.R index 733f67c..f620f05 100644 --- a/R/plan.R +++ b/R/plan.R @@ -514,4 +514,38 @@ combinePlans <- function(groupIds, nextId <- nextId + length(unique(all$PlanId)) } write.table(allplans, out_csv, row.names=FALSE, col.names=TRUE, quote=TRUE, sep=",", append=FALSE) +} + + +writePlan2Agent2GroupMap <- function(groupIds, + matched_persons_csv_prefix, + plans_csv, + out_csv) { + # example inputs + # groupIds <- getGroupIds('../data/vistaCohorts.csv.gz') + # matched_persons_csv_prefix <- '../output/3.match/match_' + # plans_csv <- '../output/4.plan/plan.csv' + # out_csv <- '../output/4.plan/plan2agent2group.csv' + + echo(paste0('Creating map of PlanID to AgentId in ', out_csv, '\n')) + plans<-read.csv(plans_csv, header=T, stringsAsFactors=F, strip.white=T) + plan2agent <- data.frame() + for (gid in groupIds) { + cohort<-read.csv(paste0(matched_persons_csv_prefix, gid, ".csv"), header=T, stringsAsFactors=F, strip.white=T) + cohortAgentIds <- unique(cohort$AgentId) + cohortPlans <- plans %>% filter(GroupId == gid) + cohortPlanIds <- unique(cohortPlans$PlanId) + if (length(cohortAgentIds) != length(cohortPlanIds)) { + echo(paste0("For Group ", gid, + ", the number of generated vista-like plans (", length(cohortPlanIds), + ") is not the same as the number of sampled census agents (", length(cohortAgentIds), + "). Skipping this group." )) + next + } + df <- data.frame(PlanId=cohortPlanIds, AgentId=cohortAgentIds) + df$GroupId <- gid + plan2agent <- rbind(plan2agent, df) + } + write.table(plan2agent, out_csv, row.names=FALSE, col.names=TRUE, quote=TRUE, sep=",", append=FALSE) + } \ No newline at end of file diff --git a/tests/expected/4.plan/plan2agent2group.csv b/tests/expected/4.plan/plan2agent2group.csv new file mode 100644 index 0000000..2480674 --- /dev/null +++ b/tests/expected/4.plan/plan2agent2group.csv @@ -0,0 +1,428 @@ +"PlanId","AgentId","GroupId" +1,"213021342P2856225",1 +2,"213021342P2856544",1 +3,"213021342P2856532",1 +4,"213021342P2856080",1 +5,"213021342P2856288",1 +6,"213021342P2856540",1 +7,"213021342P2855370",1 +8,"213021342P2855433",1 +9,"213021342P2856174",1 +10,"213021342P2857306",1 +11,"213021342P2856518",1 +12,"213021342P2856082",1 +13,"213021342P2855368",1 +14,"213021342P2862001",1 +15,"213021342P2855660",1 +16,"213021342P2857475",1 +17,"213021342P2856204",1 +18,"213021342P2861650",1 +19,"213021342P2861920",1 +20,"213031351P3698884",1 +21,"213031351P3696688",1 +22,"213031351P3698281",1 +23,"213031351P3698969",1 +24,"213031351P3698758",1 +25,"213031351P3698574",1 +26,"213031352P897951",1 +27,"213031352P897879",1 +28,"213031352P898786",1 +29,"213031352P899064",1 +30,"213031352P898218",1 +31,"213031352P897996",1 +32,"213031352P898050",1 +33,"213031352P899113",1 +34,"213031352P900119",1 +35,"213031352P898527",1 +36,"213031352P898369",1 +37,"213031352P899067",1 +38,"213031352P897897",1 +39,"213031352P902780",1 +40,"213021342P2854997",1 +41,"213021342P2857938",1 +42,"213021342P2855019",1 +43,"213021342P2854099",1 +44,"213021342P2854952",1 +45,"213021342P2854639",1 +46,"213021342P2854744",1 +47,"213021342P2854251",1 +48,"213021342P2854515",1 +49,"213021342P2861655",1 +50,"213021342P2853789",1 +51,"213021342P2854134",1 +52,"213021342P2861951",1 +53,"213021342P2861768",1 +54,"213021342P2854140",1 +55,"213021342P2854473",1 +56,"213021342P2853966",1 +57,"213021342P2858160",1 +58,"213021342P2854173",1 +59,"213021342P2854704",1 +60,"213021342P2853848",1 +61,"213021342P2854567",1 +62,"213021342P2861914",1 +63,"213021342P2855021",1 +64,"213031351P3698130",1 +65,"213031351P3697709",1 +66,"213031351P3697994",1 +67,"213031351P3697562",1 +68,"213031351P3697759",1 +69,"213031351P3697847",1 +70,"213031351P3697454",1 +71,"213031351P3702532",1 +72,"213031351P3697573",1 +73,"213031351P3702442",1 +74,"213031352P899424",1 +75,"213031352P897426",1 +76,"213031352P896900",1 +77,"213031352P897787",1 +78,"213031352P897063",1 +79,"213031352P897627",1 +80,"213031352P896660",1 +81,"213031352P899745",1 +82,"213031352P899422",1 +83,"213031352P897095",1 +84,"213031352P896818",1 +85,"213031352P899773",1 +86,"213031352P897480",1 +87,"213031352P896797",1 +88,"213031352P896617",1 +89,"213031352P897474",1 +90,"213031352P899314",1 +91,"213031352P897802",1 +92,"213031352P902876",1 +93,"213031352P897304",1 +94,"213031352P899313",1 +95,"213031352P896755",1 +96,"213021342P2860113",2 +97,"213021342P2857603",2 +98,"213021342P2852537",2 +99,"213021342P2852538",2 +100,"213021342P2848185",2 +101,"213021342P2861804",2 +102,"213021342P2857761",2 +103,"213021342P2857333",2 +104,"213021342P2858919",2 +105,"213021342P2859001",2 +106,"213031351P3701983",2 +107,"213031351P3699831",2 +108,"213031352P902243",2 +109,"213031352P899464",2 +110,"213031352P900187",2 +111,"213021342P2857974",2 +112,"213021342P2856830",2 +113,"213021342P2857095",2 +114,"213021342P2857961",2 +115,"213021342P2857233",2 +116,"213021342P2857109",2 +117,"213031351P3701359",2 +118,"213031351P3701377",2 +119,"213031351P3699155",2 +120,"213031351P3699232",2 +121,"213031351P3699554",2 +122,"213031352P901759",2 +123,"213031352P889088",2 +124,"213021342P2849361",3 +125,"213021342P2848267",3 +126,"213021342P2849392",3 +127,"213021342P2859208",3 +128,"213021342P2849557",3 +129,"213021342P2848428",3 +130,"213021342P2859221",3 +131,"213021342P2849384",3 +132,"213021342P2848740",3 +133,"213021342P2848806",3 +134,"213021342P2849270",3 +135,"213031351P3702034",3 +136,"213031351P3702087",3 +137,"213031351P3702142",3 +138,"213031351P3694306",3 +139,"213031351P3694639",3 +140,"213031351P3694875",3 +141,"213031351P3694499",3 +142,"213031351P3694329",3 +143,"213031351P3694751",3 +144,"213031351P3694764",3 +145,"213031351P3694584",3 +146,"213031352P902450",3 +147,"213031352P893322",3 +148,"213031352P896152",3 +149,"213031352P892917",3 +150,"213031352P893383",3 +151,"213021342P2859513",3 +152,"213021342P2859592",3 +153,"213021342P2859801",3 +154,"213021342P2859893",3 +155,"213021342P2860005",3 +156,"213021342P2861209",3 +157,"213021342P2845347",3 +158,"213021342P2845019",3 +159,"213021342P2845728",3 +160,"213021342P2845382",3 +161,"213021342P2847513",3 +162,"213021342P2845236",3 +163,"213021342P2845661",3 +164,"213021342P2846276",3 +165,"213021342P2846460",3 +166,"213021342P2845370",3 +167,"213021342P2845752",3 +168,"213021342P2844836",3 +169,"213021342P2847123",3 +170,"213021342P2844941",3 +171,"213021342P2844331",3 +172,"213021342P2844985",3 +173,"213021342P2846917",3 +174,"213021342P2844964",3 +175,"213021342P2858618",3 +176,"213021342P2845415",3 +177,"213021342P2845085",3 +178,"213021342P2852819",3 +179,"213021342P2845741",3 +180,"213021342P2844213",3 +181,"213021342P2845710",3 +182,"213021342P2847221",3 +183,"213021342P2846120",3 +184,"213021342P2846573",3 +185,"213021342P2845410",3 +186,"213021342P2846068",3 +187,"213021342P2852908",3 +188,"213021342P2858780",3 +189,"213021342P2844763",3 +190,"213021342P2858601",3 +191,"213031351P3700124",3 +192,"213031351P3700267",3 +193,"213031351P3700301",3 +194,"213031351P3700317",3 +195,"213031351P3700357",3 +196,"213031351P3700386",3 +197,"213031351P3700430",3 +198,"213031351P3701716",3 +199,"213031351P3701720",3 +200,"213031351P3701735",3 +201,"213031351P3701840",3 +202,"213031351P3692673",3 +203,"213031351P3693257",3 +204,"213031351P3693244",3 +205,"213031351P3693328",3 +206,"213031351P3692694",3 +207,"213031351P3692479",3 +208,"213031351P3693073",3 +209,"213031351P3693004",3 +210,"213031351P3699721",3 +211,"213031351P3696934",3 +212,"213031351P3693283",3 +213,"213031351P3693657",3 +214,"213031351P3699805",3 +215,"213031351P3693790",3 +216,"213031351P3692856",3 +217,"213031351P3692097",3 +218,"213031351P3692838",3 +219,"213031351P3693476",3 +220,"213031351P3699795",3 +221,"213031351P3692436",3 +222,"213031351P3693540",3 +223,"213031351P3692067",3 +224,"213031352P900606",3 +225,"213031352P900634",3 +226,"213031352P901794",3 +227,"213031352P901966",3 +228,"213031352P902025",3 +229,"213031352P902163",3 +230,"213031352P890833",3 +231,"213031352P889666",3 +232,"213031352P891976",3 +233,"213031352P890509",3 +234,"213031352P889583",3 +235,"213031352P896045",3 +236,"213031352P895754",3 +237,"213031352P895785",3 +238,"213031352P890723",3 +239,"213031352P891538",3 +240,"213031352P891619",3 +241,"213031352P891361",3 +242,"213031352P891952",3 +243,"213031352P890654",3 +244,"213031352P899975",3 +245,"213031352P895740",3 +246,"213031352P890872",3 +247,"213031352P889385",3 +248,"213031352P891996",3 +249,"213031352P889944",3 +250,"213031352P889192",3 +251,"213031352P889748",3 +252,"213031352P890771",3 +253,"213031352P889987",3 +254,"213031352P889883",3 +255,"213031352P891535",3 +256,"213031352P891557",3 +257,"213031352P889327",3 +258,"213031352P900024",3 +259,"213031352P890946",3 +260,"213031352P902650",3 +261,"213031352P889334",3 +262,"213021342P2860311",4 +263,"213021342P2860346",4 +264,"213021342P2860381",4 +265,"213021342P2860385",4 +266,"213021342P2860416",4 +267,"213021342P2860456",4 +268,"213021342P2860524",4 +269,"213021342P2860659",4 +270,"213021342P2860694",4 +271,"213021342P2861366",4 +272,"213021342P2850427",4 +273,"213021342P2850448",4 +274,"213021342P2851892",4 +275,"213021342P2849441",4 +276,"213021342P2850204",4 +277,"213021342P2848530",4 +278,"213021342P2853079",4 +279,"213021342P2853153",4 +280,"213021342P2848677",4 +281,"213021342P2851028",4 +282,"213021342P2850174",4 +283,"213021342P2853312",4 +284,"213021342P2852670",4 +285,"213021342P2852633",4 +286,"213021342P2848986",4 +287,"213021342P2850084",4 +288,"213021342P2850551",4 +289,"213021342P2848431",4 +290,"213021342P2850758",4 +291,"213021342P2849503",4 +292,"213021342P2852574",4 +293,"213021342P2849146",4 +294,"213021342P2849651",4 +295,"213021342P2853559",4 +296,"213021342P2849587",4 +297,"213021342P2848742",4 +298,"213021342P2861526",4 +299,"213021342P2861984",4 +300,"213031351P3700728",4 +301,"213031351P3700760",4 +302,"213031351P3700979",4 +303,"213031351P3700995",4 +304,"213031351P3702057",4 +305,"213031351P3695622",4 +306,"213031351P3695614",4 +307,"213031351P3695435",4 +308,"213031351P3694951",4 +309,"213031351P3694663",4 +310,"213031351P3694690",4 +311,"213031351P3697213",4 +312,"213031351P3697165",4 +313,"213031351P3695210",4 +314,"213031351P3694446",4 +315,"213031351P3699931",4 +316,"213031351P3695184",4 +317,"213031351P3695470",4 +318,"213031351P3695458",4 +319,"213031351P3695679",4 +320,"213031351P3694932",4 +321,"213031351P3699989",4 +322,"213031351P3695575",4 +323,"213031351P3694651",4 +324,"213031351P3695396",4 +325,"213031352P900986",4 +326,"213031352P901060",4 +327,"213031352P901126",4 +328,"213031352P901218",4 +329,"213031352P901310",4 +330,"213031352P901402",4 +331,"213031352P901442",4 +332,"213031352P902515",4 +333,"213031352P892510",4 +334,"213031352P892743",4 +335,"213031352P894919",4 +336,"213031352P894858",4 +337,"213031352P892696",4 +338,"213031352P892497",4 +339,"213031352P894406",4 +340,"213031352P895087",4 +341,"213031352P893999",4 +342,"213031352P894340",4 +343,"213031352P894206",4 +344,"213031352P896268",4 +345,"213031352P893652",4 +346,"213031352P895873",4 +347,"213031352P894205",4 +348,"213031352P894195",4 +349,"213031352P894589",4 +350,"213031352P896362",4 +351,"213031352P893914",4 +352,"213031352P892945",4 +353,"213031352P894085",4 +354,"213031352P900372",4 +355,"213031352P900342",4 +356,"213031352P893804",4 +357,"213031352P894237",4 +358,"213031352P893157",4 +359,"213031352P893741",4 +360,"213031352P900336",4 +361,"213021342P2859831",4 +362,"213021342P2852897",4 +363,"213021342P2846832",4 +364,"213031351P3693822",4 +365,"213031351P3693639",4 +366,"213031352P891668",4 +367,"213031352P891754",4 +368,"213021342P2860479",5 +369,"213021342P2860543",5 +370,"213021342P2860696",5 +371,"213021342P2860839",5 +372,"213021342P2860866",5 +373,"213021342P2852132",5 +374,"213021342P2851878",5 +375,"213021342P2852049",5 +376,"213021342P2851917",5 +377,"213021342P2851325",5 +378,"213021342P2851086",5 +379,"213021342P2850963",5 +380,"213021342P2851061",5 +381,"213021342P2852041",5 +382,"213021342P2852755",5 +383,"213021342P2852012",5 +384,"213021342P2861637",5 +385,"213021342P2852704",5 +386,"213021342P2852124",5 +387,"213031351P3701113",5 +388,"213031351P3701177",5 +389,"213031351P3702255",5 +390,"213031351P3696145",5 +391,"213031351P3696398",5 +392,"213031351P3697239",5 +393,"213031351P3696373",5 +394,"213031351P3696173",5 +395,"213031352P901322",5 +396,"213031352P901376",5 +397,"213031352P901411",5 +398,"213031352P901468",5 +399,"213031352P901469",5 +400,"213031352P901511",5 +401,"213031352P901664",5 +402,"213031352P896401",5 +403,"213021342P2859853",5 +404,"213021342P2860043",5 +405,"213021342P2860059",5 +406,"213021342P2847827",5 +407,"213021342P2847688",5 +408,"213021342P2848138",5 +409,"213021342P2847278",5 +410,"213021342P2848015",5 +411,"213021342P2852480",5 +412,"213021342P2846836",5 +413,"213021342P2847856",5 +414,"213031351P3693683",5 +415,"213031351P3693940",5 +416,"213031351P3693769",5 +417,"213031351P3694077",5 +418,"213031351P3694112",5 +419,"213031352P900764",5 +420,"213031352P900781",5 +421,"213031352P900861",5 +422,"213031352P900889",5 +423,"213031352P892190",5 +424,"213031352P892223",5 +425,"213031352P896097",5 +426,"213031352P891915",5 +427,"213031352P892352",5 diff --git a/tests/testthat/testPlan.R b/tests/testthat/testPlan.R index 641baad..cbbf4e9 100644 --- a/tests/testthat/testPlan.R +++ b/tests/testthat/testPlan.R @@ -25,7 +25,13 @@ test_that("VISTA-like plans generation works", { '../actual/4.plan/plan.csv' ) ) - + capture_output( + writePlan2AgentMap(getGroupIds('../data/vistaCohorts.csv.gz'), + '../expected/3.match/match_', + '../expected/4.plan/plan.csv', + '../actual/4.plan/plan2agent2group.csv' + ) + ) files<-c( 'analysis-start-times-by-activity-qq.pdf', 'analysis-end-times-by-activity-qq.pdf', @@ -46,5 +52,9 @@ test_that("VISTA-like plans generation works", { expect_true(file.exists('../actual/4.plan/plan.csv')) expect_true(md5sum('../actual/4.plan/plan.csv') == md5sum('../expected/4.plan/plan.csv')) + + expect_true(file.exists('../actual/4.plan/plan2agent2group.csv')) + expect_true(md5sum('../actual/4.plan/plan2agent2group.csv') == md5sum('../expected/4.plan/plan2agent2group.csv')) + }) \ No newline at end of file