-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added multi_sweep example for vax and hint #16
Merged
jonathanhhb
merged 5 commits into
InstituteforDiseaseModeling:main
from
shchen-idmod:multisweep_examples
Oct 16, 2023
+627
−1
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7bb9691
Added multi_sweep example for vax and hint
shchen-idmod 81f344b
Fix variable typo
shchen-idmod 882d054
Add __init__.py
shchen-idmod 73d3240
Update requirements.txt for examples in HINTy and vaccination
shchen-idmod 1de3595
Fixed docstring in sweeping.py
shchen-idmod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import numpy as np | ||
from typing import Dict, Any, List | ||
from idmtools.entities.simulation import Simulation | ||
from logging import getLogger, DEBUG | ||
|
||
logger = getLogger() | ||
|
||
|
||
################################################## | ||
# Sweeping utility functions | ||
################################################## | ||
def set_param(simulation: Simulation, param: str, value: Any) -> Dict[str, Any]: | ||
""" | ||
Set specific parameter value. | ||
Args: | ||
simulation: idmtools Simulation | ||
param: parameter | ||
value: new value | ||
|
||
Returns: | ||
dict | ||
""" | ||
# return simulation.task.set_parameter(param, value) | ||
|
||
try: | ||
return simulation.task.set_parameter(param, value) | ||
except ValueError: | ||
if "parameters" in simulation.task.config: | ||
config = simulation.task.config.parameters | ||
else: | ||
config = simulation.task.config | ||
|
||
config[param] = value | ||
return {param: value} | ||
|
||
|
||
def sweep_functions(simulation: Simulation, func_list: List) -> Dict[str, Any]: | ||
""" | ||
Apply funcs on simulation. | ||
Args: | ||
simulation: idmtools Simulation | ||
func_list: a list of functions | ||
|
||
Returns: | ||
dict of parameters | ||
""" | ||
tags_updated = {} | ||
for func in func_list: | ||
tags = func(simulation) | ||
if tags: | ||
tags_updated.update(tags) | ||
return tags_updated | ||
|
||
|
||
class ItvFn: | ||
""" | ||
Sweeping utility: works for sweeping on interventions. | ||
Requirements: | ||
- func is a method that takes campaign as first parameter | ||
- func return a dict | ||
|
||
Returns: | ||
dict | ||
""" | ||
|
||
def __init__(self, func, *args, **kwargs): | ||
self.func = func | ||
self.args = args | ||
self.kwargs = kwargs | ||
|
||
def __call__(self, simulation: Simulation): | ||
import emod_api.campaign as campaign | ||
campaign.reset() | ||
|
||
md = self.func(campaign, *self.args, **self.kwargs) | ||
|
||
# add events | ||
events = campaign.campaign_dict["Events"] | ||
simulation.task.campaign.add_events(events) | ||
|
||
# update config for adhoc events | ||
adhoc_events = campaign.get_adhocs() | ||
if len(adhoc_events) > 0: | ||
if logger.isEnabledFor(DEBUG): | ||
logger.debug("Found adhoc events in campaign. Needs some special processing behind the scenes.") | ||
if "Custom_Individual_Events" in simulation.task.config.parameters: | ||
ev_exist = set(simulation.task.config.parameters.Custom_Individual_Events) | ||
ev_addhoc = set(adhoc_events.keys()) | ||
simulation.task.config.parameters.Custom_Individual_Events = list(ev_exist.union(ev_addhoc)) | ||
else: | ||
simulation.task.config.parameters.Report_Event_Recorder_Events = list(set(adhoc_events.keys())) | ||
#pass | ||
|
||
# Make sure we cast numpy types into normal system types | ||
if md: | ||
for k, v in md.items(): | ||
if isinstance(v, (np.int64, np.float64, np.float32, np.uint32, np.int16, np.int32)): | ||
md[k] = v.item() | ||
|
||
return md | ||
|
||
|
||
class CfgFn: | ||
""" | ||
Sweeping utility: works for sweeping on config parameters. | ||
Requirements: | ||
- func is a method that takes config as first parameter | ||
- func return a dict | ||
|
||
Returns: | ||
dict | ||
""" | ||
|
||
def __init__(self, func, *args, **kwargs): | ||
self.func = func | ||
self.args = args | ||
self.kwargs = kwargs | ||
|
||
def __call__(self, simulation: Simulation): | ||
md = self.func(simulation.task.config, *self.args, **self.kwargs) | ||
|
||
# Make sure we cast numpy types into normal system types | ||
if md: | ||
for k, v in md.items(): | ||
if isinstance(v, (np.int64, np.float64, np.float32, np.uint32, np.int16, np.int32)): | ||
md[k] = v.item() | ||
|
||
return md | ||
|
||
|
||
class SwpFn: | ||
""" | ||
Sweeping utility: works for sweeping on report, demographics, migrations and climate, etc. | ||
Requirements: | ||
- func is a method that takes task as first parameter | ||
- func return a dict | ||
|
||
Returns: | ||
dict | ||
""" | ||
|
||
def __init__(self, func, *args, **kwargs): | ||
self.func = func | ||
self.args = args | ||
self.kwargs = kwargs | ||
|
||
def __call__(self, simulation: Simulation): | ||
md = self.func(simulation.task, *self.args, **self.kwargs) | ||
|
||
# Make sure we cast numpy types into normal system types | ||
if md: | ||
for k, v in md.items(): | ||
if isinstance(v, (np.int64, np.float64, np.float32, np.uint32, np.int16, np.int32)): | ||
md[k] = v.item() | ||
|
||
return md |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should really be handled by emod_api.campaign, and we should be able to get rid of when we get onto a more modern Typhoid_SIM branch, but leave it for now, it's fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering this part too. almost wanted to put just pass to if/else.