Skip to content

Commit

Permalink
__init__ add for relative import when testing, and starting on writin…
Browse files Browse the repository at this point in the history
…g tests
  • Loading branch information
amyheather committed Jun 7, 2024
1 parent b1bdb28 commit 90626e1
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 16 deletions.
6 changes: 6 additions & 0 deletions evaluation/posts/2024_06_07/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ Started on research compendium...

* Moved `.csv` files (`patients.csv`, `travel_matrix.csv`, `units.csv`) from `sim/` to `data/` and modified file paths in scripts accordingly (`model.py` and `units.py`)
* Moved `.py` files (`sim_replicate.py` and `sim_single.py`) from main folder into `sim/` and modified import in notebook accordingly (`reproduction.ipynb`)
* Created test for successful reproduction using pytest
* Amended `run_replications()` to accept alternative path to save results to
* Copied data from my successful reproduction from `output/` to `test/data`
* Copied parameters from `reproduction.ipynb` into `test_model.py`
* Add `__init__.py` to `sim/` and modified imports in each of the `.py` scripts accordingly
* Started on test to run the model and compare test results against saved results in the test folder

## Suggested changes for protocol/template

Expand Down
2 changes: 1 addition & 1 deletion reproduction/reproduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Running 30 reps of base_3_month => 2, 0, 1, 5, 3, 7, 11, 10, 13, 9, 8, 12, 6, 14, 17, 4, 16, 18, 15, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, "
"Running 30 reps of base_3_month => 0, 1, 2, 7, 4, 8, 6, 3, 5, 11, 12, 10, 15, 9, 16, 17, 14, 13, 19, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, "
]
},
{
Expand Down
8 changes: 4 additions & 4 deletions reproduction/sim/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import pandas as pd
import numpy as np

from sim.patient import Patient
from sim.units import Dialysis_units
from sim.allocation import AllocatePatients
from sim.audit import Audit
from .patient import Patient
from .units import Dialysis_units
from .allocation import AllocatePatients
from .audit import Audit

class Population:
"""
Expand Down
21 changes: 12 additions & 9 deletions reproduction/sim/sim_replicate.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from sim.end_trial_analysis import EndTrialAnalysis
from sim.helper_functions import expand_multi_index
from sim.model import DialysisSim
from sim.parameters import Scenario, Uniform, Normal
from .end_trial_analysis import EndTrialAnalysis
from .helper_functions import expand_multi_index
from .model import DialysisSim
from .parameters import Scenario, Uniform, Normal
from joblib import Parallel, delayed
import pandas as pd


def run_replications(scenarios, number_of_replications=30, base_random_set=0):
def run_replications(scenarios, number_of_replications=30, base_random_set=0,
output_folder='output'):
"""
Main simulation code. Calls multiple runs of prescribed scenarios.
Expand Down Expand Up @@ -34,6 +35,8 @@ def run_replications(scenarios, number_of_replications=30, base_random_set=0):
base_random_set : int, optional
To create the random number set for each replication, the replication
number is added to this value. The default is 0.
output_folder : str, optional
Path to save .csv result files to.
Returns
-------
Expand Down Expand Up @@ -65,13 +68,13 @@ def run_replications(scenarios, number_of_replications=30, base_random_set=0):

# Save as CSV
p_audits.to_csv(
f'output/{name}_reps_{N_REPS}_patient_audit.csv', index=False)
f'{output_folder}/{name}_reps_{N_REPS}_patient_audit.csv', index=False)
u_audits.to_csv(
f'output/{name}_reps_{N_REPS}_unit_audit.csv', index=False)
f'{output_folder}/{name}_reps_{N_REPS}_unit_audit.csv', index=False)
d_audits.to_csv(
f'output/{name}_reps_{N_REPS}_displaced_audit.csv', index=False)
f'{output_folder}/{name}_reps_{N_REPS}_displaced_audit.csv', index=False)
i_audits.to_csv(
f'output/{name}_reps_{N_REPS}_inpatient_audit.csv', index=False)
f'{output_folder}/{name}_reps_{N_REPS}_inpatient_audit.csv', index=False)

# Run analysis after all replicate runs in a scenario
analysis = EndTrialAnalysis(name, p_audits, u_audits, d_audits, i_audits)
Expand Down
4 changes: 2 additions & 2 deletions reproduction/sim/sim_single.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from sim.model import DialysisSim
from sim.parameters import Scenario, Uniform
from .model import DialysisSim
from .parameters import Scenario, Uniform


if __name__ == '__main__':
Expand Down
78 changes: 78 additions & 0 deletions reproduction/tests/test_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'''
Model testing
This module contains tests to confirm whether the simulation model is
producing consistent results.
'''

import pytest
import pandas as pd
import sim.sim_replicate as sim
from sim.parameters import Scenario
from pathlib import Path

DISPLACED = 'base_3_month_reps_30_displaced_audit.csv'
PATH_DISPLACED_1 = Path(__file__).parent.joinpath('data', DISPLACED)
PATH_DISPLACED_2 = Path(__file__).parent.joinpath('data', DISPLACED)

@pytest.mark.parametrize(
'displaced_audit',
[
(PATH_DISPLACED_1),
(PATH_DISPLACED_2),
],
)

def run_model():
'''
Run model with parameters from computational reproducibility assessment
'''
scenarios = {}
scenarios['base_3_month'] = Scenario(
run_length=150,
proportion_pos_requiring_inpatient=0.6)
number_of_replications = 30
base_random_set = 2700
output_folder='test_output'
sim.run_replications(
scenarios, number_of_replications, base_random_set, output_folder)

#def test_displaced(displaced_audit):
# '''
# Test that results are consistent with those from the computational
# reproducibility assessment
# '''
# displaced_data = pd.read_csv(displaced_audit)

'''
# Import model
import sim.sim_replicate as sim
from sim.parameters import Scenario
def test_reproduction():
# Test run model as set-up for computational reproducibility assessment.
# Parameters
scenarios = {}
scenarios['base_3_month'] = Scenario(
run_length=150,
proportion_pos_requiring_inpatient=0.6)
number_of_replications = 30
base_random_set = 2700
output_folder='test_output'
# Import expected results
displaced = pd.read_csv('data/base_3_month_reps_30_displaced_audit.csv')
inpatient = pd.read_csv('data/base_3_month_reps_30_inpatient_audit.csv')
patient = pd.read_csv('data/base_3_month_reps_30_patient_audit.csv')
unit = pd.read_csv('data/base_3_month_reps_30_unit_audit.csv')
# Run the simulation
sim.run_replications(
scenarios, number_of_replications, base_random_set, output_folder)
# Import the test results
test_displaced = pd.read_csv('test_output/base_3_month_reps_30_displaced_audit.csv')
# Compare against actual results
assert pd.testing.assert_frame_equal(displaced, test_displaced)
'''

0 comments on commit 90626e1

Please sign in to comment.