Skip to content

Commit

Permalink
Convert excel files to csv files. Joe's modules (#1508)
Browse files Browse the repository at this point in the history
* convert excel files to csv equivalent - Joe modules

* use new method to read newly converted csv files

* sort imports

* used read_csv_files method

* read csv files using the new read_csv_files method, disable pylint error, update doc string

* edits

* convert Excel to csv file(s), change from read_excel to read_csv_files for newly created csv file(s)

* rename resource files. added underscores and removed some unwanted characters and duplicate extensions

* updated modules to align with updates to read_csv_files method in master

* remove disable pylint as we are now directly reading via pd.read_csv

* remove unused import

* delete unused csv files

---------

Co-authored-by: joehcollins <[email protected]>
  • Loading branch information
mnjowe and joehcollins authored Dec 17, 2024
1 parent 3c407a4 commit 63a06d8
Show file tree
Hide file tree
Showing 34 changed files with 84 additions and 41 deletions.
3 changes: 0 additions & 3 deletions resources/ResourceFile_AntenatalCare.xlsx

This file was deleted.

3 changes: 3 additions & 0 deletions resources/ResourceFile_AntenatalCare/parameter_values.csv
Git LFS file not shown
3 changes: 0 additions & 3 deletions resources/ResourceFile_LabourSkilledBirthAttendance.xlsx

This file was deleted.

Git LFS file not shown
3 changes: 0 additions & 3 deletions resources/ResourceFile_NewbornOutcomes.xlsx

This file was deleted.

3 changes: 3 additions & 0 deletions resources/ResourceFile_NewbornOutcomes/parameter_values.csv
Git LFS file not shown
3 changes: 0 additions & 3 deletions resources/ResourceFile_PostnatalSupervisor.xlsx

This file was deleted.

Git LFS file not shown
3 changes: 0 additions & 3 deletions resources/ResourceFile_PregnancySupervisor.xlsx

This file was deleted.

Git LFS file not shown
3 changes: 0 additions & 3 deletions resources/contraception/ResourceFile_Contraception.xlsx

This file was deleted.

Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
2 changes: 1 addition & 1 deletion src/scripts/contraception/f_steril_use_2010vs2020.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
A helper script to see the numbers of women of reproductive age having female sterilisation per 5-years age categories +
total, and the number of all women in the population in 2010 and 2020, to help to calibrate the intervention multipliers
(saved in ResourceFile_Contraception.xlsx in the sheets Interventions_Pop & Interventions_PPFP).
(saved in ResourceFile_Contraception folder in the Interventions_Pop & Interventions_PPFP CSV files).
"""
from pathlib import Path

Expand Down
5 changes: 3 additions & 2 deletions src/tlo/methods/care_of_women_during_pregnancy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from tlo.methods.labour import LabourOnsetEvent
from tlo.methods.malaria import HSI_MalariaIPTp
from tlo.methods.tb import HSI_Tb_ScreeningAndRefer
from tlo.util import read_csv_files

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
Expand Down Expand Up @@ -176,8 +177,8 @@ def __init__(self, name=None, resourcefilepath=None):
}

def read_parameters(self, data_folder):
parameter_dataframe = pd.read_excel(Path(self.resourcefilepath) / 'ResourceFile_AntenatalCare.xlsx',
sheet_name='parameter_values')
parameter_dataframe = read_csv_files(Path(self.resourcefilepath) / 'ResourceFile_AntenatalCare',
files='parameter_values')
self.load_parameters_from_dataframe(parameter_dataframe)

def initialise_population(self, population):
Expand Down
11 changes: 6 additions & 5 deletions src/tlo/methods/contraception.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from tlo.analysis.utils import flatten_multi_index_series_into_dict_for_logging
from tlo.events import Event, IndividualScopeEventMixin, PopulationScopeEventMixin, RegularEvent
from tlo.methods.hsi_event import HSI_Event
from tlo.util import random_date, sample_outcome, transition_states
from tlo.util import random_date, read_csv_files, sample_outcome, transition_states

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
Expand Down Expand Up @@ -164,7 +164,8 @@ def read_parameters(self, data_folder):
"""Import the relevant sheets from the ResourceFile (excel workbook) and declare values for other parameters
(CSV ResourceFile).
"""
workbook = pd.read_excel(Path(self.resourcefilepath) / 'contraception' / 'ResourceFile_Contraception.xlsx', sheet_name=None)
workbook = read_csv_files(Path(self.resourcefilepath) / 'contraception' / 'ResourceFile_Contraception',
files=None)

# Import selected sheets from the workbook as the parameters
sheet_names = [
Expand Down Expand Up @@ -1350,10 +1351,10 @@ def __init__(self, *args):
super().__init__(name='Labour')

def read_parameters(self, *args):
parameter_dataframe = pd.read_excel(self.sim.modules['Contraception'].resourcefilepath /
parameter_dataframe = read_csv_files(self.sim.modules['Contraception'].resourcefilepath /
'contraception' /
'ResourceFile_Contraception.xlsx',
sheet_name='simplified_labour_parameters')
'ResourceFile_Contraception',
files='simplified_labour_parameters')
self.load_parameters_from_dataframe(parameter_dataframe)

def initialise_population(self, population):
Expand Down
8 changes: 4 additions & 4 deletions src/tlo/methods/labour.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from tlo.methods.hsi_event import HSI_Event
from tlo.methods.hsi_generic_first_appts import GenericFirstAppointmentsMixin
from tlo.methods.postnatal_supervisor import PostnatalWeekOneMaternalEvent
from tlo.util import BitsetHandler
from tlo.util import BitsetHandler, read_csv_files

if TYPE_CHECKING:
from tlo.methods.hsi_generic_first_appts import HSIEventScheduler
Expand Down Expand Up @@ -620,9 +620,9 @@ def __init__(self, name=None, resourcefilepath=None):
}

def read_parameters(self, data_folder):
parameter_dataframe = pd.read_excel(Path(self.resourcefilepath) / 'ResourceFile_LabourSkilledBirth'
'Attendance.xlsx',
sheet_name='parameter_values')
parameter_dataframe = read_csv_files(Path(self.resourcefilepath) / 'ResourceFile_LabourSkilledBirth'
'Attendance',
files='parameter_values')
self.load_parameters_from_dataframe(parameter_dataframe)

def initialise_population(self, population):
Expand Down
6 changes: 3 additions & 3 deletions src/tlo/methods/newborn_outcomes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from tlo.methods.causes import Cause
from tlo.methods.hsi_event import HSI_Event
from tlo.methods.postnatal_supervisor import PostnatalWeekOneNeonatalEvent
from tlo.util import BitsetHandler
from tlo.util import BitsetHandler, read_csv_files

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
Expand Down Expand Up @@ -311,8 +311,8 @@ def __init__(self, name=None, resourcefilepath=None):

def read_parameters(self, data_folder):

parameter_dataframe = pd.read_excel(Path(self.resourcefilepath) / 'ResourceFile_NewbornOutcomes.xlsx',
sheet_name='parameter_values')
parameter_dataframe = read_csv_files(Path(self.resourcefilepath) / 'ResourceFile_NewbornOutcomes',
files='parameter_values')
self.load_parameters_from_dataframe(parameter_dataframe)

# Here we map 'disability' parameters to associated DALY weights to be passed to the health burden module
Expand Down
5 changes: 3 additions & 2 deletions src/tlo/methods/postnatal_supervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from tlo.methods import Metadata, postnatal_supervisor_lm, pregnancy_helper_functions
from tlo.methods.causes import Cause
from tlo.methods.hsi_event import HSI_Event
from tlo.util import read_csv_files

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
Expand Down Expand Up @@ -230,8 +231,8 @@ def __init__(self, name=None, resourcefilepath=None):
}

def read_parameters(self, data_folder):
parameter_dataframe = pd.read_excel(Path(self.resourcefilepath) / 'ResourceFile_PostnatalSupervisor.xlsx',
sheet_name='parameter_values')
parameter_dataframe = read_csv_files(Path(self.resourcefilepath) / 'ResourceFile_PostnatalSupervisor',
files='parameter_values')
self.load_parameters_from_dataframe(parameter_dataframe)

def initialise_population(self, population):
Expand Down
6 changes: 3 additions & 3 deletions src/tlo/methods/pregnancy_supervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
)
from tlo.methods.causes import Cause
from tlo.methods.hsi_generic_first_appts import GenericFirstAppointmentsMixin
from tlo.util import BitsetHandler
from tlo.util import BitsetHandler, read_csv_files

if TYPE_CHECKING:
from tlo.methods.hsi_generic_first_appts import HSIEventScheduler
Expand Down Expand Up @@ -434,8 +434,8 @@ def __init__(self, name=None, resourcefilepath=None):

def read_parameters(self, data_folder):
# load parameters from the resource file
parameter_dataframe = pd.read_excel(Path(self.resourcefilepath) / 'ResourceFile_PregnancySupervisor.xlsx',
sheet_name='parameter_values')
parameter_dataframe = read_csv_files(Path(self.resourcefilepath) / 'ResourceFile_PregnancySupervisor',
files='parameter_values')
self.load_parameters_from_dataframe(parameter_dataframe)

# Here we map 'disability' parameters to associated DALY weights to be passed to the health burden module.
Expand Down
2 changes: 1 addition & 1 deletion src/tlo/methods/simplified_births.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def read_parameters(self, data_folder):
self.parameters['months_between_pregnancy_and_delivery'] = 9

# Breastfeeding status for newborns (importing from the Newborn resourcefile)
rf = pd.read_excel(Path(self.resourcefilepath) / 'ResourceFile_NewbornOutcomes.xlsx')
rf = pd.read_csv(Path(self.resourcefilepath) / 'ResourceFile_NewbornOutcomes/parameter_values.csv')
param_as_string = rf.loc[rf.parameter_name == 'prob_breastfeeding_type']['value'].iloc[0]
parameter = json.loads(param_as_string)[0]
self.parameters['prob_breastfeeding_type'] = parameter
Expand Down
5 changes: 3 additions & 2 deletions tests/test_contraception.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from tlo.methods import contraception, demography, enhanced_lifestyle, healthsystem, symptommanager
from tlo.methods.contraception import HSI_Contraception_FamilyPlanningAppt
from tlo.methods.hiv import DummyHivModule
from tlo.util import read_csv_files


def run_sim(tmpdir,
Expand Down Expand Up @@ -918,8 +919,8 @@ def test_input_probs_sum():

# Import relevant sheets from the workbook
resourcefilepath = Path(os.path.dirname(__file__)) / '../resources'
workbook = pd.read_excel(Path(resourcefilepath) / 'contraception' / 'ResourceFile_Contraception.xlsx',
sheet_name=None)
workbook = read_csv_files(Path(resourcefilepath) / 'contraception' / 'ResourceFile_Contraception',
files=None)
sheet_names = [
'Initiation_ByMethod',
'Interventions_Pop',
Expand Down

0 comments on commit 63a06d8

Please sign in to comment.