Skip to content

Commit b362b5e

Browse files
committed
exposing migration again (issues for Josh who was using that), removing webservice from migration, zhuzhing
1 parent 88eb8d5 commit b362b5e

File tree

5 files changed

+13
-48
lines changed

5 files changed

+13
-48
lines changed

emod_api/migration/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .migration import Migration, from_file, from_params, from_demog_and_param_gravity, examine_file, to_csv, from_csv

emod_api/migration/migration.py

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -716,44 +716,6 @@ def from_params(demographics_file_path=None,
716716
migration.MigrationType = migration_type
717717
return migration
718718

719-
720-
def from_demog_and_param_gravity_webservice(demographics_file_path: str, params: str, id_ref: str, migration_type=Migration.LOCAL) -> Migration:
721-
"""
722-
Calls a webservice (running on a GPU) to calculate the migration patterns quickly.
723-
724-
Args:
725-
demographics_file_path: Path to the demographics file.
726-
params: Path to the json file with parameters for gravity calculation and server url.
727-
id_ref: Metadata tag that needs to match corresponding value in demographics file.
728-
migration_type: Migration type.
729-
730-
Returns:
731-
Migration object
732-
733-
"""
734-
735-
with Path(params).open("r") as f_params:
736-
params_url = json.load(f_params)
737-
738-
# load
739-
rates = client.run(Path(demographics_file_path), params_url)
740-
741-
demog = Demog.from_file(demographics_file_path)
742-
migration = Migration()
743-
nodes = [node.forced_id for node in demog.nodes]
744-
745-
# we need a 0-N index for the NumPy array and the node ID for the migration file
746-
for i, src in enumerate(nodes):
747-
for j, dst in enumerate(nodes):
748-
if dst != src:
749-
migration[dst][src] = rates[i, j]
750-
751-
migration.IdReference = id_ref
752-
migration.MigrationType = migration_type
753-
754-
return migration
755-
756-
757719
def from_demog_and_param_gravity(demographics_file_path, gravity_params, id_ref, migration_type=Migration.LOCAL):
758720
demog = Demog.from_file(demographics_file_path)
759721
return _from_demog_and_param_gravity(demog, gravity_params, id_ref, migration_type)

tests/test_demog.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -774,10 +774,9 @@ def test_overlay_list_of_nodes(self):
774774
'pop': [123, 234, 345, 678],
775775
'lon': [10, 11, 12, 13],
776776
'lat': [21, 22, 23, 24]}
777-
csv_file = pathlib.Path("test_overlay_population.csv")
777+
csv_file = os.path.join(self.out_folder, "test_overlay_population.csv")
778778
pd.DataFrame.from_dict(temp).to_csv(csv_file)
779779
demo = Demographics.from_csv(csv_file)
780-
csv_file.unlink()
781780

782781
overlay_nodes = [] # list of all overlay nodes
783782
overlay_nodes_id_1 = [1, 2] # Change susceptibility of nodes with ids 1 and 2
@@ -799,7 +798,7 @@ def test_overlay_list_of_nodes(self):
799798
overlay_nodes.append(Node.OverlayNode(node_id=node_id, individual_attributes=new_individual_attributes_2))
800799

801800
demo.apply_overlay(overlay_nodes)
802-
demo.generate_file("test_overlay_list_of_nodes.json")
801+
demo.generate_file( os.path.join(self.out_folder, "test_overlay_list_of_nodes.json"))
803802

804803
self.assertDictEqual(demo.nodes[0].individual_attributes.to_dict(), new_individual_attributes_1.to_dict())
805804
self.assertDictEqual(demo.nodes[1].individual_attributes.to_dict(), new_individual_attributes_1.to_dict())

tests/test_migration.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@ class MigrationTests(unittest.TestCase):
2929

3030
@classmethod
3131
def setUpClass(cls):
32-
32+
cls.output_folder = os.path.join(manifest.output_folder, "migration")
3333
cls.user = environ["USERNAME"] if system() == "Windows" else environ["USER"]
3434
filename = os.path.join(manifest.migration_folder, "Kenya_Regional_Migration_from_Census.bin")
3535
cls.kenya_regional_migration = from_file(filename)
3636
cls.guinea_pig = Migration()
3737

38+
@classmethod
39+
def tearDownClass(cls):
40+
# Class level cleanup after all tests in the class
41+
cls.resource = None # Example cleanup
42+
3843
def test_defaults(self):
3944
"""
4045
Changing the defaults is a breaking change.
@@ -534,7 +539,6 @@ def test_to_csv(self):
534539

535540
def test_examine_file(self):
536541
filename = os.path.join(manifest.migration_folder, "Seattle_30arcsec_local_migration.bin")
537-
output = os.path.join(manifest.output_folder, "seattle_csv.csv")
538542

539543
expected_output = ["Author:", "DatavalueCount:", "DateCreated:", "GenderDataType:", "IdReference:",
540544
"InterpolationType:", "MigrationType:", "NodeCount:", "NodeOffsets:", "Tool:", "Nodes:"]
@@ -795,7 +799,7 @@ def verify_distance(migration_rate_list, node_locations):
795799
migration_local = from_demog_and_param_gravity(demographics_file, gravity_params=[1, 1, 1, -1],
796800
id_ref=id_ref, migration_type=Migration.REGIONAL)
797801

798-
migration_local_file = Path(os.path.join(manifest.output_folder, 'gravity_distance.bin'))
802+
migration_local_file = Path(os.path.join(self.output_folder, 'gravity_distance.bin'))
799803
migration_local.to_file(migration_local_file)
800804

801805
f = io.StringIO()
@@ -812,7 +816,7 @@ def test_from_demog_and_param_gravity_with_reference(self):
812816
id_ref='from_demog_and_param_gravity_test',
813817
migration_type=Migration.LOCAL)
814818

815-
migration_file = Path(os.path.join(manifest.output_folder, 'test_from_demog_and_param_gravity_with_reference.bin'))
819+
migration_file = Path(os.path.join(self.output_folder, 'test_from_demog_and_param_gravity_with_reference.bin'))
816820
migration.to_file(migration_file)
817821

818822
reference_file = Path(os.path.join(manifest.migration_folder, 'migration_gravity_model_reference.bin'))
@@ -823,11 +827,11 @@ def test_from_csv(self):
823827
'destination': [2, 3, 4],
824828
'rate': [0.1, 0.2, 0.3]}
825829

826-
csv_file = Path(os.path.join(manifest.output_folder, "test_migration.csv"))
830+
csv_file = Path(os.path.join(self.output_folder, "test_migration.csv"))
827831
pd.DataFrame.from_dict(temp).to_csv(csv_file, index=False)
828832
migration = from_csv(csv_file, id_ref="testing")
829833

830-
migration_file = os.path.join(manifest.output_folder, "test_migration.bin")
834+
migration_file = os.path.join(self.output_folder, "test_migration.bin")
831835
migration.to_file(migration_file)
832836
migration_from_bin = from_file(migration_file)
833837

tests/unittests/test_migration_imports.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ def test_migration_migration_import(self):
2525
'from_file',
2626
'examine_file',
2727
'from_params',
28-
'from_demog_and_param_gravity_webservice',
2928
'from_demog_and_param_gravity',
3029
'to_csv'
3130
]

0 commit comments

Comments
 (0)