Skip to content

Commit

Permalink
Merge pull request #12 from UNGlobalPulse/dev_contactmatrices
Browse files Browse the repository at this point in the history
Dev contactmatrices
  • Loading branch information
JosephPB committed Sep 22, 2022
2 parents 6e8f51b + 4029092 commit 222f374
Show file tree
Hide file tree
Showing 9 changed files with 1,466 additions and 1,302 deletions.
2,644 changes: 1,420 additions & 1,224 deletions Notebooks/quickstart camp.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion camp_scripts/full_run_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def set_seed_numba(seed):
learning_centers_sorted = learning_centers[np.argsort(enrolled)]

# find top k most filled learning centers
top_k = learning_centers_sorted[-int(args.extra_learning_centers):]
top_k = learning_centers_sorted[-int(args.extra_learning_centers) :]
for learning_center in top_k:
extra_lc = LearningCenter(
coordinates=learning_center.super_area.coordinates
Expand Down
2 changes: 1 addition & 1 deletion camp_scripts/full_run_parse_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def set_seed_numba(seed):
learning_centers_sorted = learning_centers[np.argsort(enrolled)]

# find top k most filled learning centers
top_k = learning_centers_sorted[-int(args.extra_learning_centers):]
top_k = learning_centers_sorted[-int(args.extra_learning_centers) :]
for learning_center in top_k:
extra_lc = LearningCenter(
coordinates=learning_center.super_area.coordinates
Expand Down
47 changes: 3 additions & 44 deletions camp_scripts/full_run_parse_newJoe.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,12 @@ def set_seed_numba(seed):
else:
world = generate_empty_world({"region": args.region_only})

print("Now populate")
# populate empty world
populate_world(world)

# distribute people to households
print("Now Distribute")
distribute_people_to_households(world)

# medical facilities
Expand Down Expand Up @@ -489,7 +491,7 @@ def set_seed_numba(seed):
learning_centers_sorted = learning_centers[np.argsort(enrolled)]

# find top k most filled learning centers
top_k = learning_centers_sorted[-int(args.extra_learning_centers):]
top_k = learning_centers_sorted[-int(args.extra_learning_centers) :]
for learning_center in top_k:
extra_lc = LearningCenter(
coordinates=learning_center.super_area.coordinates
Expand Down Expand Up @@ -715,10 +717,6 @@ def set_seed_numba(seed):
# df = pd.DataFrame(index=mi, columns=["CXB-207"])
df[:] = args.n_seeding_case_per_day / len(world.people)

print("#### seeding df:")
print(df)
print("####")

infection_seed = InfectionSeed(
world=world,
infection_selector=selector,
Expand Down Expand Up @@ -768,7 +766,6 @@ def set_seed_numba(seed):
group_types=group_types,
load_interactions_path=args.parameters,
contact_sexes=["unisex", "male", "female"],
Tracker_Contact_Type=["1D", "All"],
)
else:
tracker = None
Expand Down Expand Up @@ -821,41 +818,3 @@ def set_seed_numba(seed):
simulator.tracker.contract_matrices("AC", np.array([0, 18, 60]))
simulator.tracker.contract_matrices("All", np.array([0, 100]))
simulator.tracker.post_process_simulation(save=True)

# Make Plots
Plots = PlotClass(record_path=args.save_path / "Tracker", Tracker_Contact_Type="1D")
Plots.make_plots(
plot_BBC=True,
plot_thumbprints=True,
SameCMAP="Log",
plot_INPUTOUTPUT=True,
plot_AvContactsLocation=True,
plot_dTLocationPopulation=True,
plot_InteractionMatrices=True,
plot_ContactMatrices=True,
plot_CompareSexMatrices=True,
plot_AgeBinning=True,
plot_Distances=True,

MaxAgeBin=60
)

# Make Plots
Plots = PlotClass(
record_path=args.save_path / "Tracker", Tracker_Contact_Type="All"
)
Plots.make_plots(
plot_BBC=True,
plot_thumbprints=True,
SameCMAP="Log",
plot_INPUTOUTPUT=False,
plot_AvContactsLocation=False,
plot_dTLocationPopulation=False,
plot_InteractionMatrices=True,
plot_ContactMatrices=True,
plot_CompareSexMatrices=True,
plot_AgeBinning=False,
plot_Distances=False,

MaxAgeBin=60
)
37 changes: 20 additions & 17 deletions camps/camp_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,29 @@ def read_yaml(config_filename):
# area_household_structure_params_exists = False


def GenerateDiscretePDF(Type="Gaussian", datarange=[0, 100], Mean=0, SD=1, stretch=False):
def GenerateDiscretePDF(
Type="Gaussian", datarange=[0, 100], Mean=0, SD=1, stretch=False
):
def PValue(Mean, SD, Xmin, Xmax, stretch=False):
return integrate.quad(lambda x: Gaussian(Mean, SD, x, stretch), Xmin, Xmax)


def Gaussian(Mean, SD, x,stretch):
def Gaussian(Mean, SD, x, stretch):
if stretch:
if abs(x - Mean) < 2:
return 4*np.exp(-0.5 * ((x - Mean) / SD) ** 2.0) / (SD * np.sqrt(2 * np.pi))
return (
4
* np.exp(-0.5 * ((x - Mean) / SD) ** 2.0)
/ (SD * np.sqrt(2 * np.pi))
)
else:
return 1*np.exp(-0.5 * ((x - Mean) / SD) ** 2.0) / (SD * np.sqrt(2 * np.pi))
return (
1
* np.exp(-0.5 * ((x - Mean) / SD) ** 2.0)
/ (SD * np.sqrt(2 * np.pi))
)
else:
return np.exp(-0.5 * ((x - Mean) / SD) ** 2.0) / (SD * np.sqrt(2 * np.pi))


Vals = np.arange(datarange[0], datarange[1] + 1, 1)
dist = {}
for a_i in range(len(Vals) - 1):
Expand Down Expand Up @@ -252,9 +260,7 @@ def distribute_people_to_households(world: CampWorld):
]["min_age_gap_between_childen"],
)
else:
household_distributor = CampHouseholdDistributor(
max_household_size=12,
)
household_distributor = CampHouseholdDistributor(max_household_size=12)

households_total = []
for area in world.areas:
Expand Down Expand Up @@ -300,20 +306,17 @@ def distribute_people_to_households(world: CampWorld):
mother_firstchild_gap_generator, dist = GenerateDiscretePDF(
datarange=[14, 60],
Mean=mother_firstchild_gap_mean + 0.5 + (9.0 / 12.0),
SD=mother_firstchild_gap_STD

SD=mother_firstchild_gap_STD,
)
partner_age_gap_generator, dist = GenerateDiscretePDF(
datarange=[-20, 20],
Mean=partner_age_gap_mean + 0.5,
datarange=[-20, 20],
Mean=partner_age_gap_mean + 0.5,
SD=partner_age_gap_mean_STD,
stretch=stretch
stretch=stretch,
)

nchildren_generator, dist = GenerateDiscretePDF(
datarange=[0, 8],
Mean=n_children,
SD=n_children_STD,
datarange=[0, 8], Mean=n_children, SD=n_children_STD
)

n_families = int(area_data["families"])
Expand Down
2 changes: 1 addition & 1 deletion camps/distributors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#from .camp_household_distributor_legacy import CampHouseholdDistributor
# from .camp_household_distributor_legacy import CampHouseholdDistributor
from .camp_household_distributor import CampHouseholdDistributor
from .learning_center_distributor import LearningCenterDistributor
28 changes: 18 additions & 10 deletions camps/distributors/camp_household_distributor.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,7 @@ def not_in(list_A, list_B, permute=True):

Houses_Single = list(
np.random.choice(
np.array(Houses_W_Children)[indexes],
size=n_families_singleparent,
np.array(Houses_W_Children)[indexes], size=n_families_singleparent
)
)
household_Single_sizes = np.array(
Expand Down Expand Up @@ -451,6 +450,7 @@ def not_in(list_A, list_B, permute=True):

# put adults households with kids start
Intersection = intersection(Houses_W_Children, households_with_space)
# print(f"Point A: {len(Intersection)},Nkids:{n_kids},Nmen:{n_men},Nkids:{n_women}")
for household in Intersection:
# Single House
if household in Houses_Single:
Expand Down Expand Up @@ -487,7 +487,7 @@ def not_in(list_A, list_B, permute=True):
# House now full?
if household.size >= household.max_size:
households_with_space.remove(household)
#print("Parents done")
# print("Parents done")

# Distribute all the children
Loop_1 = True
Expand All @@ -499,13 +499,16 @@ def not_in(list_A, list_B, permute=True):
if Loop_1:
Intersection = intersection(Houses_W_Children, households_with_space)
else:
Intersection = intersection(not_in(Houses_W_Children,Houses_Single), households_with_space)
Intersection = intersection(
not_in(Houses_W_Children, Houses_Single), households_with_space
)

if len(Intersection) == 0:
# Need to find space for final children we sqeeze them into Houses_W_Children even if full
squeeze = True
Intersection = intersection(Houses_W_Children, Houses_W_Children)

# print(f"Point B: {len(Intersection)},Nkids:{n_kids},Nmen:{n_men},Nkids:{n_women}")
for household in Intersection:
NKids = len(household.kids)
NAdults = len(household.adults)
Expand Down Expand Up @@ -575,6 +578,7 @@ def not_in(list_A, list_B, permute=True):
if len(Intersection) == 0:
break

# print(f"Point C: {len(Intersection)},Nkids:{n_kids},Nmen:{n_men},Nkids:{n_women}")
for household in Intersection:
NKids = len(household.kids)
NAdults = len(household.adults)
Expand Down Expand Up @@ -620,7 +624,7 @@ def not_in(list_A, list_B, permute=True):
# Check if we finished up adults
if not men_by_age and not women_by_age:
break
#print("All multigen adults done")
# print("All multigen adults done")

while True:
sqeeze = False
Expand All @@ -635,19 +639,22 @@ def not_in(list_A, list_B, permute=True):
sqeeze = True
Intersection = intersection(Houses_Multigen, Houses_Multigen)

#Find houses with space
# Find houses with space
if len(households_with_space) > 0:
Intersection = intersection(households_with_space, households_with_space)
Intersection = intersection(
households_with_space, households_with_space
)
else:
# Sqeeze in the final adults
sqeeze = True
Intersection = intersection(Houses_WO_Children, Houses_WO_Children)

# print(f"Point D: {len(Intersection)},Nkids:{n_kids},Nmen:{n_men},Nkids:{n_women}")
if len(Intersection) == 0:
sqeeze = True
intersection(households, households)

Intersection = intersection(households, households)

# print(f"Point E: {len(Intersection)},Nkids:{n_kids},Nmen:{n_men},Nkids:{n_women}")
for household in Intersection:
NAdults = len(household.adults)

Expand Down Expand Up @@ -708,8 +715,9 @@ def not_in(list_A, list_B, permute=True):
# Check if we finished up adults
if not men_by_age and not women_by_age:
break
#print("All adults only houses done")
# print("All adults only houses done")

# print(f"END")
# check everyone has a house
people_in_households = len(
[person for household in households for person in household.people]
Expand Down
4 changes: 1 addition & 3 deletions camps/distributors/camp_household_distributor_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,7 @@ def get_closest_person_of_age(self, men_by_age, women_by_age, age, sex):
return None

def distribute_people_to_households(
self,
area: Area,
n_families: int,
self, area: Area, n_families: int
) -> Households:
"""
Distributes people to household given an area with a given number of families
Expand Down
2 changes: 1 addition & 1 deletion test_camps/test_shelter_visits.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def setup_shelter_visits(camps_world):
daytypes={
"weekday": ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"],
"weekend": ["Friday", "Saturday"],
},
}
)
shelter_visits_distributor.link_shelters_to_shelters(camps_world.super_areas)
return camps_world
Expand Down

0 comments on commit 222f374

Please sign in to comment.