-
Notifications
You must be signed in to change notification settings - Fork 2
Added type hints #2
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
Merged
Merged
Changes from all commits
Commits
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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -22,7 +22,7 @@ class MetaEpiModel: | |
|
|
||
| Provides a way to implement and numerically integrate | ||
| """ | ||
| def __init__(self, travel_graph, populations, population='Population'): | ||
| def __init__(self, travel_graph: pd.DataFrame, populations: pd.DataFrame, population: str ='Population'): | ||
| """ | ||
| Initialize the EpiModel object | ||
|
|
||
|
|
@@ -51,7 +51,7 @@ def __init__(self, travel_graph, populations, population='Population'): | |
|
|
||
| self.models = models | ||
|
|
||
| def __repr__(self): | ||
| def __repr__(self) -> str: | ||
| """ | ||
| Return a string representation of the EpiModel object | ||
|
|
||
|
|
@@ -65,7 +65,7 @@ def __repr__(self): | |
| text = "Metapopulation model with %u populations\n\nThe disease is defined by an %s" % (self.travel_graph.shape[0], model_text) | ||
| return text | ||
|
|
||
| def add_interaction(self, source, target, agent, rate): | ||
| def add_interaction(self, source: str, target: str, agent: str, rate: float) -> None: | ||
| """ | ||
| Add an interaction between two compartments_ | ||
|
|
||
|
|
@@ -85,7 +85,7 @@ def add_interaction(self, source, target, agent, rate): | |
| for state in self.models: | ||
| self.models[state].add_interaction(source, target, agent, rate) | ||
|
|
||
| def add_spontaneous(self, source, target, rate): | ||
| def add_spontaneous(self, source: str, target: str, rate: float) -> None: | ||
| """ | ||
| Add a spontaneous transition between two compartments_ | ||
|
|
||
|
|
@@ -103,7 +103,7 @@ def add_spontaneous(self, source, target, rate): | |
| for state in self.models: | ||
| self.models[state].add_spontaneous(source, target, rate) | ||
|
|
||
| def add_vaccination(self, source, target, rate, start): | ||
| def add_vaccination(self, source: str, target: str, rate: float, start: int) -> None: | ||
| """ | ||
| Add a vaccination transition between two compartments_ | ||
|
|
||
|
|
@@ -123,11 +123,11 @@ def add_vaccination(self, source, target, rate, start): | |
| for state in self.models: | ||
| self.models[state].add_vaccination(source, target, rate, start) | ||
|
|
||
| def R0(self): | ||
| def R0(self) -> Union[float, None]: | ||
| key = list(self.models.keys())[0] | ||
| return self.models[key].R0() | ||
|
|
||
| def get_state(self, state): | ||
| def get_state(self, state: str) -> EpiModel: | ||
| """ | ||
| Return a reference to a state EpiModel object | ||
|
|
||
|
|
@@ -138,7 +138,7 @@ def get_state(self, state): | |
|
|
||
| return self.models[state] | ||
|
|
||
| def _initialize_populations(self, susceptible, population=None): | ||
| def _initialize_populations(self, susceptible: str, population: Union[pd.DataFrame, None] =None) -> None: | ||
| columns = list(self.transitions.nodes()) | ||
| self.compartments_ = pd.DataFrame(np.zeros((self.travel_graph.shape[0], len(columns)), dtype='int'), columns=columns) | ||
| self.compartments_.index = self.populations.index | ||
|
|
@@ -149,8 +149,8 @@ def _initialize_populations(self, susceptible, population=None): | |
| for state in self.compartments_.index: | ||
| self.compartments_.loc[state, susceptible] = self.populations.loc[state, population] | ||
|
|
||
| def _run_travel(self, compartments_, travel): | ||
| def travel_step(x, populations): | ||
| def _run_travel(self, compartments_: pd.DataFrame, travel: pd.DataFrame) -> pd.DataFrame: | ||
| def travel_step(x, populations: pd.DataFrame) -> pd.Series: | ||
| n = populations.loc[x.name] | ||
| p = travel.loc[x.name].values.tolist() | ||
| output = np.random.multinomial(n, p) | ||
|
|
@@ -163,17 +163,24 @@ def travel_step(x, populations): | |
| # Travel occurs independently for each compartment | ||
| # since we don't allow in-flight transitions | ||
| for comp in compartments_.columns: | ||
| new_compartments[comp] = travel.apply(travel_step, populations=compartments_[comp]).sum(axis=1) | ||
| new_compartments[comp] = travel.apply( | ||
| travel_step, | ||
| populations=compartments_[comp] | ||
| ).sum(axis=1) | ||
|
|
||
| return new_compartments | ||
|
|
||
| def _run_spread(self): | ||
| def _run_spread(self) -> None: | ||
| for state in self.compartments_.index: | ||
| pop = self.compartments_.loc[state].to_dict() | ||
| self.models[state].single_step(**pop) | ||
| self.compartments_.loc[state] = self.models[state].values_.iloc[[-1]].values[0] | ||
|
|
||
| def simulate(self, timestamp, t_min=1, seasonality=None, seed_state=None, susceptible='S', **kwargs): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seed_state should be a string. It essentially the name of the population that is being seeded, and corresponds to the index of the population DataFrame |
||
| def simulate( | ||
| self, timestamp: int, t_min: int = 1, | ||
| seasonality=None, seed_state: [str, None] = None, | ||
| susceptible: str ='S', **kwargs | ||
| ) -> None: | ||
| if seed_state is None: | ||
| raise NotInitialized("You have to specify the seed_state") | ||
|
|
||
|
|
@@ -193,10 +200,10 @@ def simulate(self, timestamp, t_min=1, seasonality=None, seed_state=None, suscep | |
| def integrate(self, **kwargs): | ||
| raise NotImplementedError("MetaEpiModel doesn't support direct integration of the ODE") | ||
|
|
||
| def draw_model(self): | ||
| def draw_model(self) -> None: | ||
| return self.models.iloc[0].draw_model() | ||
|
|
||
| def plot(self, title=None, normed=True, layout=None, **kwargs): | ||
| def plot(self, title: Union[str, None] = None, normed: bool = True, layout=None, **kwargs) -> None: | ||
| if layout is None: | ||
| n_pop = self.travel_graph.shape[0] | ||
| N = int(np.round(np.sqrt(n_pop), 0)+1) | ||
|
|
@@ -270,7 +277,7 @@ def plot(self, title=None, normed=True, layout=None, **kwargs): | |
| fig.patch.set_facecolor('#FFFFFF') | ||
| fig.tight_layout() | ||
|
|
||
| def plot_peaks(self): | ||
| def plot_peaks(self) -> None: | ||
| peaks = None | ||
|
|
||
| for state in self.models.values(): | ||
|
|
@@ -301,4 +308,4 @@ def plot_peaks(self): | |
| ax.set_xticks(np.arange(0, peaks.shape[1], 3)) | ||
| ax.set_xticklabels(np.arange(0, peaks.shape[1], 3), fontsize=10) | ||
| # ax.set_aspect(1) | ||
| fig.patch.set_facecolor('#FFFFFF') | ||
| fig.patch.set_facecolor('#FFFFFF') | ||
This file contains hidden or 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
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.
Copy-paste issue. The descriptions should probably be something like "Add age structure with a contact matrix and age-structured population"
Also, the matrix is 2D, so maybe something like List[List]? (Not sure about how to specify this)