public static double getPrevValue(int lineNum, string inputData, Person.diseaseType myDisease) { // Parse input record into fields by looking for comma delimiter string[] myFields = Regex.Split(inputData, fieldDelimiter); // Get offset for prevalence corresponding to myDisease if (Person.getPrevOffset(myDisease) == 6) { Console.WriteLine(); } PrevValue_offset = Person.getPrevOffset(myDisease); double PrevValue; if (!Double.TryParse((myFields[PrevValue_offset]), out PrevValue)) { Console.WriteLine("Error - Data line: {0} - Field {1} (prevalence) is not numeric.", lineNum, PrevValue_offset + 1); Console.WriteLine("Data: {0}", inputData); return(-1); } if (PrevValue < 0 || PrevValue > 1) { Console.WriteLine("Error - Data line: {0} - Field {1} (relative risk) not from 0 to 1.", lineNum, PrevValue_offset + 1); Console.WriteLine("Data: {0}", inputData); return(-1); } return(PrevValue); }
public static Person DiseasePrevTablePerson(int lineNum, string inputData, Person.diseaseType myDisease) { DiseasePrevPersonInitializer myDiseasePrevPersonInitializer = new DiseasePrevPersonInitializer(lineNum, inputData, myDisease); if (!myDiseasePrevPersonInitializer.initializeOK) { return(new Person(false)); } return(new Person(myDiseasePrevPersonInitializer)); }
public DiseasePrevPersonInitializer(int lineNum, string inputData, Person.diseaseType myDisease) { // Parse input record into fields by looking for comma delimiter string[] myFields = Regex.Split(inputData, fieldDelimiter); initializeOK = true; // (1) Read in country (string - convertable to Person countrytype). // Valid values are: "ETH, IND, GHA, BGD, NPL, NGA, UGA, [or] TZA." if (!Person.countrytypelist.ContainsKey(((myFields[country_offset]).Trim()).ToUpper())) { initializeOK = false; Console.WriteLine("Error - Data line: {0} - Field {1} is not a valid country.", lineNum, country_offset + 1); Console.WriteLine("Data: {0}", inputData); return; } this.country = (Person.countrytype) Person.countrytypelist[((myFields[country_offset]).Trim()).ToUpper()]; // (2) Read in year (int) if (!int.TryParse((myFields[yearnow_offset]), out this.yearNow)) { initializeOK = false; Console.WriteLine("Error - Data line: {0} - Field {1} (yearnow) is not an integer.", lineNum, yearnow_offset + 1); Console.WriteLine("Data: {0}", inputData); return; } // (3) Read in age (double). if (!Double.TryParse((myFields[agenow_offset]), out this.ageNow)) { initializeOK = false; Console.WriteLine("Error - Data line: {0} - Field {1} (agenow) is not a double.", lineNum, agenow_offset + 1); Console.WriteLine("Data: {0}", inputData); } // (4) Get disease type disease = myDisease; }
public static double DiseasePrevTableVALUE(int lineNum, string inputData, Person.diseaseType myDisease) { return(DiseasePrevVALUEInitializer.getPrevValue(lineNum, inputData, myDisease)); }
/// <summary> ////////////////////////////////////////////////////////////////////////// /// readFile01_diseasePrevalence /// Open and read in File01 - diseasePrevalence file. Create lookup table of disease /// prevalence as a function of country, year, & age. Report out to console any problems. /// </summary> ///////////////////////////////////////////////////////////////////////// static private bool readFile01_diseasePrevalence(string fileName) { FileStream myFile = new FileStream(fileName, FileMode.Open, FileAccess.Read); StreamReader fileReader = new StreamReader(myFile); int lineNum = 2; // Keep track of where we are in the file in case there is a problem. Person myPersonStunting; Person.diseaseType stunting = Person.diseaseType.STUNTING; Person myPersonDiarrhea; Person.diseaseType diarrhea = Person.diseaseType.DIARRHEA; Person myPersonAnemia; Person.diseaseType anemia = Person.diseaseType.ANEMIA; // Read in first two lines of file (because first line is column headings to be ignored). string inputRecord = fileReader.ReadLine(); inputRecord = fileReader.ReadLine(); // Read until reaching the end of file. Report errors and exit if any line has bad data while (inputRecord != null) { if (inputRecord != "") { //Get prevalence state fields, create a Person object initialized with //probability of each disease //if (lineNum == 52) { int foo = 2; } myPersonStunting = DiseasePrev.DiseasePrevTablePerson(lineNum, inputRecord, stunting); if (!myPersonStunting.getInitalizeOK()) { Console.WriteLine("File name was: {0}.", fileName); Console.WriteLine("Hit any key"); Console.ReadKey(); return(false); } myPersonDiarrhea = DiseasePrev.DiseasePrevTablePerson(lineNum, inputRecord, diarrhea); if (!myPersonDiarrhea.getInitalizeOK()) { Console.WriteLine("File name was: {0}.", fileName); Console.WriteLine("Hit any key"); Console.ReadKey(); return(false); } myPersonAnemia = DiseasePrev.DiseasePrevTablePerson(lineNum, inputRecord, anemia); if (!myPersonAnemia.getInitalizeOK()) { Console.WriteLine("File name was: {0}.", fileName); Console.WriteLine("Hit any key"); Console.ReadKey(); return(false); } // Get prevalence for each disease, create a double initialized with this value. double stuntingPREV = DiseasePrev.DiseasePrevTableVALUE(lineNum, inputRecord, stunting); if (stuntingPREV < 0) { Console.WriteLine("Hit any key"); Console.ReadKey(); return(false); } if (DiseasePrev.DiseasePrevTable.ContainsKey(myPersonStunting)) { Console.WriteLine("Error - Prevalence for disease combination (stunting) entered twice."); Console.WriteLine("File name was: {0}.", fileName); Console.WriteLine("Entry: {0}", inputRecord); Console.WriteLine("Line number is: {0}", lineNum); Console.WriteLine(""); Console.ReadKey(); return(false); } double diarrheaPREV = DiseasePrev.DiseasePrevTableVALUE(lineNum, inputRecord, diarrhea); if (diarrheaPREV < 0) { Console.WriteLine("Hit any key"); Console.ReadKey(); return(false); } if (DiseasePrev.DiseasePrevTable.ContainsKey(myPersonDiarrhea)) { Console.WriteLine("Error - Prevalence for disease combination (diarrhea) entered twice."); Console.WriteLine("File name was: {0}.", fileName); Console.WriteLine("Entry: {0}", inputRecord); Console.WriteLine(""); Console.ReadKey(); return(false); } double anemiaPREV = DiseasePrev.DiseasePrevTableVALUE(lineNum, inputRecord, anemia); if (anemiaPREV < 0) { Console.WriteLine("Hit any key"); Console.ReadKey(); return(false); } if (DiseasePrev.DiseasePrevTable.ContainsKey(myPersonAnemia)) { Console.WriteLine("Error - Prevalence for disease combination (anemia) entered twice."); Console.WriteLine("File name was: {0}.", fileName); Console.WriteLine("Entry: {0}", inputRecord); Console.WriteLine(""); Console.ReadKey(); return(false); } // Add the Person object (key) and prevalence (lookup value) //to the dictionary. DiseasePrev.DiseasePrevTable.Add(myPersonStunting, stuntingPREV); DiseasePrev.DiseasePrevTable.Add(myPersonDiarrhea, diarrheaPREV); DiseasePrev.DiseasePrevTable.Add(myPersonAnemia, anemiaPREV); } inputRecord = fileReader.ReadLine(); lineNum++; } // If DiseasePrevTable is empty, return with error if (DiseasePrev.DiseasePrevTable.Count() == 0) { Console.WriteLine("Error - File contains no data."); Console.WriteLine("File name was: {0}.", file01_DiseasePrevalence_name); Console.WriteLine(""); Console.ReadKey(); return(false); } // No errors encountered - so return true return(true); }