Patients Fever¶
Let’s consider files that record the body temperatures for a number of respiratory diseases patients (which b.t.w. has been this year’s Marató de TV theme). We are receiveing the info in one format (let’s call it FormatA) and we need to put it in a different format (let’s call it FormatB):
- Format A: Each line of the file corresponds to a patient and contains the following data fields separated by coma (
','):The file
fev1.csvandfev2.csvare examples:Blanca Nieves Frias,37.6,38.2,38.8,38.9,38.9,39.0 Pere Gil Julivert,36.5,36.6,36.5,36.8,36.5,39.5 Mar Tellez Berlat,36.7,36.5,36.9,37.5,36.2 Lola Marturi San,39.4,38.9,39.0,39.1,38.3,37.1 Enric Marco Gol,37.0,37.3,38.4,39.0,36.6,35.9Jordi Torres Pipes,38.2,38.1,39.0,36.5,36.8 Dolores Fuertes deCabeza,38.2,38.1,39.0,36.5,36.8 Ricard Lamata Feliz,39.4,38.9,39.0,39.1,38.3,37.1 Inmaculada Delano Moreno,36.9,36.5,36.8,37.5,38.5,39.0 Aitor Menta Ita,39.0,38.2,38.1,36.5,36.8,35.9Format B: Each line of the file corresponds to a patient and contains the following data fields separated by coma (
','):The file
fev1+2.csvis an example:38.6,Nieves,Frias,Blanca,37.6,38.2,38.8,38.9,38.9,39.0 37.1,Gil,Julivert,Pere,36.5,36.6,36.5,36.8,36.5,39.5 36.8,Tellez,Berlat,Mar,36.7,36.5,36.9,37.5,36.2 38.6,Marturi,San,Lola,39.4,38.9,39.0,39.1,38.3,37.1 37.4,Marco,Gol,Enric,37.0,37.3,38.4,39.0,36.6,35.9 37.7,Torres,Pipes,Jordi,38.2,38.1,39.0,36.5,36.8 37.7,Fuertes,deCabeza,Dolores,38.2,38.1,39.0,36.5,36.8 38.6,Lamata,Feliz,Ricard,39.4,38.9,39.0,39.1,38.3,37.1 37.5,Delano,Moreno,Inmaculada,36.9,36.5,36.8,37.5,38.5,39.0 37.4,Menta,Ita,Aitor,39.0,38.2,38.1,36.5,36.8,35.9
Implement the following Python functions in the module fever (file fever.py):
The first function is:
- fever_merge(fn1, fn2, fnout):
given
fn1,fn2,fnout, threestrwith textfile names, wherefn1,fn2are names of files with patients data in Format A.does two things:
writes a fille named
fnoutwith the information offn1andfn2together in Format B. The order of the patients is: first the patients offn1followed by those infn2, all apearing in the same order as in the input files.returns an
intwith the number of patients processed i.e. the number of patients in the output file.
For example the call fever_merge('fev1.csv', 'fev2.csv', 'fev1+2.csv) should return 10 and create the file fev1+2.csv above.
Doctests are available at the fever_merge.test file.
The second function is:
- fever_merge_sort(fn1, fn2, fnout):
that does exactly the same except the the patients must be ordered as follows:
Criteria #1: Decreasing order of the average temperature.
Criteria #2: Alphabetical order.
For example the call fever_merge_sort('fev1.csv', 'fev2.csv', 'fev1+2ord.csv) should return 10 and create the following file fev1+2ord.csv above.
38.6,Lamata,Feliz,Ricard,39.4,38.9,39.0,39.1,38.3,37.1 38.6,Marturi,San,Lola,39.4,38.9,39.0,39.1,38.3,37.1 38.6,Nieves,Frias,Blanca,37.6,38.2,38.8,38.9,38.9,39.0 37.7,Fuertes,deCabeza,Dolores,38.2,38.1,39.0,36.5,36.8 37.7,Torres,Pipes,Jordi,38.2,38.1,39.0,36.5,36.8 37.5,Delano,Moreno,Inmaculada,36.9,36.5,36.8,37.5,38.5,39.0 37.4,Marco,Gol,Enric,37.0,37.3,38.4,39.0,36.6,35.9 37.4,Menta,Ita,Aitor,39.0,38.2,38.1,36.5,36.8,35.9 37.1,Gil,Julivert,Pere,36.5,36.6,36.5,36.8,36.5,39.5 36.8,Tellez,Berlat,Mar,36.7,36.5,36.9,37.5,36.2
Doctests are available at the fever_merge_sort.test file.
Note
To implement these functions is recommended (but not mandatory) to previosly implement, test and call the following auxiliar functions:
The first function:
The second function:
- pat_write(L, fnout):
given
a
listof lists, each with the patient data in Format B.
fn,strwith the name of file to write patients data in Format A.does 2 things:
writes a file called fnout with the info in
L.returns an
intwith the number of patients processed i.e. the number of patients in the output file.