Final Assessment¶
Your university is collaborating with a company to develop a tool for processing reports of cases of a particular disease. The company has a very specific way of doing things, and has asked you to design and build a JavaScript object to interact with their proprietary code. Your supervisor wants you to submit an example of your code working within a Jupyter notebook (her preferred environment!) so she can review it before sending it off to the company.
There are three input datasets, so you can play with them to make sure your code is robust.
Remember – your code will be tested against other input data, so make sure that you are being careful in your assumptions!
We also give you an example of sample_output.txt, which would be useful for Task 3. Remember, you will need to overwrite this file.
Also remember – don’t use any package/library including but not limited to numpy/scipy/csv. You should finish these tasks without these packages.
Task 1¶
You have been provided with a sample of the case incidence records to help you test your code in the file “sampleCases.tsv”. The company has done a questionable job of digitising these records, however, and you will need to clean them up a bit before you process them. Remember, .tsv files are “tab separated values”, meaning that the entries are separated by tabs rather than commas (see here for more information).
Read in the file and store each record as an element within a list.
In [ ]:
Task 2¶
If you look at the records, you find that the latitudes and longitudes of cases are sometimes formatted strangely – apparently the automatic text recognitition software used by the company has a few bugs in it! Go through the records and prompt the user to input the corrected values in cases where the elements cannot be processed as numbers.
In [ ]:
Task 3¶
It’s time to format the data so that you can transfer it to the JavaScript portion of your work. Please write the code out to another file called sample_output.txt using the tab separator (\t), and put the file in the same directory as this Jupyter notebook.
In [ ]:
Task 4¶
The company wants you to create a kind of object called a CaseStudy to help them deal with all of the records associated with this file. The CaseStudy object will allow them to associate each lat/lon pair with the count of disease instances in that location; it will also allow the object to calculate higher-level properties of that set of locations.
The CaseStudy object should have attributes as follows:
• an attribute called name: the name of the case study (a string).
• an attribute called lat: all of the latitudes from the file (an array).
• an attribute called lon: all of the longitudes from the file (an array).
• an attribute called count: all of the cases of the disease from the file (an array).
The CaseStudy object should be able to:
• return the geographic extent of the area (the minimum and maximum of both latitude and longitude) as a list (e.g. [min_lat, max_lat, min_lon, max_lon]). Please call this method getExtent.
• return the centroid of the points as a list (e.g. [cent_lat, cent_lon]). Please call this method getCentroid.
Please make sure that a CaseStudy object can be created by calling the following:
var cs = new CaseStudy(name, lats, lons, cases)
In [ ]:
%%javascript
Task 5¶
As a final step, you’re going to copy over the JavaScript object you created in Task 4 and use it to calculate the geographic extent and centroid of the data you wrote out to a file in Task 3. Please take the JavaScript code you wrote in Task 4 and add it to the section where the code says < ADD JS HERE >.
In [ ]:
%%html
****************************************************** ****************** < ADD JS HERE > ******************* ******************************************************
Barchart of Results
Summary Statistics
var myBar = function(x, title){ var element = document.getElementById('barchart'); var div = document.createElement("DIV"); div.setAttribute("class", "mybar"); div.style.width = (x * 10) + "px"; div.innerHTML = title; element.append(div); };
var myStats = function(cs){
var element = document.getElementById('summaryStats');
element.innerHTML += "The summary statistics for Case Study " + cs.name + " are:
";
element.innerHTML += "
GEOGRAPHIC EXTENT: " + cs.getExtent();
element.innerHTML += "
CENTROID: " + cs.getCentroid(); };
var summariseCaseStudies = function(data, filename){
var cs = new CaseStudy(filename, [], [], []);
var i; var rawData = data.split("\n"); for(i = 0; i < rawData.length; i++){ var line = rawData[i].split('\t'); if(line.length == 1) continue; cs.lat.push(Number(line[0])); cs.lon.push(Number(line[1])); cs.count.push(Number(line[2])); myBar(line[2], [line[0], line[1]]); } myStats(cs); }; var filename = "sample_output.txt"; readInFile(filename).then(response => summariseCaseStudies(response, filename));