base
Base classes for HPVsim. These classes handle a lot of the boilerplate of the People and Sim classes (e.g. loading, saving, key lookups, etc.), so those classes can be focused on the disease-specific functionality.
Classes
| Name | Description |
|---|---|
| BasePeople | A class to handle all the boilerplate for people – note that as with the |
| BaseSim | The BaseSim class stores various methods useful for the Sim that are not directly |
| Contacts | A simple (for now) class for storing different contact layers. |
| FlexDict | A dict that allows more flexible element access: in addition to obj[‘a’], |
| Layer | A small class holding a single layer of contact edges (connections) between people. |
| ParsObj | A class based around performing operations on a self.pars dict. |
| Person | Class for a single person. Note: this is largely deprecated since sim.people |
| Result | Stores a single result – by default, acts like an array. |
BasePeople
base.BasePeople(pars)A class to handle all the boilerplate for people – note that as with the BaseSim vs Sim classes, everything interesting happens in the People class, whereas this class exists to handle the less interesting implementation details.
Attributes
| Name | Description |
|---|---|
| abnormal | Boolean array of everyone with abnormal cells. Union of episomal, transformed, and cancerous |
| alive_inds | Indices of everyone alive |
| alive_level0 | Indices of everyone alive who is a level 0 agent |
| alive_level0_inds | Indices of everyone alive who is a level 0 agent |
| dt_age | Return ages rounded to the nearest whole timestep |
| f_inds | Indices of everyone female |
| infected | Boolean array of everyone infected. Union of infectious and inactive. |
| int_age | Return ages as an integer |
| is_active | Boolean array of everyone sexually active i.e. past debut |
| is_female | Boolean array of everyone female |
| is_female_adult | Boolean array of everyone eligible for screening |
| is_female_alive | Boolean array of everyone female and alive |
| is_male | Boolean array of everyone male |
| is_male_alive | Boolean array of everyone male and alive |
| is_virgin | Boolean array of everyone not yet sexually active i.e. pre debut |
| latent | Boolean array of everyone with latent infection. By definition, these |
| m_inds | Indices of everyone male |
| n_alive | Number of people alive |
| n_alive_level0 | Number of people alive |
| precin | Boolean array of females with HPV whose disease severity level does not meet the threshold for detectable cell changes |
| round_age | Rounds age up to the next highest integer |
Methods
| Name | Description |
|---|---|
| add_contacts | Add new contacts to the array. See also contacts.add_layer(). |
| addtoself | Combine two people arrays, avoiding dcp |
| count | Count the number of people for a given key |
| count_any | Count the number of people for a given key for a 2D array if any value matches |
| count_by_genotype | Count the number of people for a given key |
| date_keys | Returns keys for different event dates (e.g., date a person became symptomatic) |
| defined | Return indices of people who are not-nan |
| dur_keys | Returns keys for different durations (e.g., the duration from exposed to infectious) |
| false | Return indices not matching the condition |
| false_by_genotype | Return indices not matching genotype-condition |
| filter | Store indices to allow for easy filtering of the People object. |
| filter_inds | Store indices to allow for easy filtering of the People object. |
| from_list | Convert a list of people back into a People object |
| get | Convenience method – key can be string or list of strings |
| imm_keys | Returns keys for different states of a person (e.g., symptomatic) |
| indices | The indices of each people array |
| init_contacts | Initialize the contacts dataframe with the correct columns and data types |
| initialize | Initialize underlying storage and map arrays |
| keys | Returns keys for all non-derived properties of the people object |
| layer_keys | Get the available contact keys – try contacts first, then acts |
| load | Load from disk from a gzipped pickle. |
| lock | Lock the people object to prevent keys from being added |
| make_edgelist | Parse a list of people with a list of contacts per person and turn it |
| person | Method to create person from the people |
| person_keys | Returns keys specific to a person (e.g., their age) |
| remove_duplicates | Sort the dataframe and remove duplicates – note, not extensively tested |
| save | Save to disk as a gzipped pickle. |
| set_pars | Re-link the parameters stored in the people object to the sim containing it, |
| state_keys | Returns keys for different states of a person (e.g., symptomatic) |
| to_arr | Return as numpy array |
| to_df | Convert to a Pandas dataframe |
| to_graph | Convert all people to a networkx MultiDiGraph, including all properties of |
| to_list | Return all people as a list |
| true | Return indices matching the condition |
| true_by_genotype | Return indices matching genotype-condition |
| undefined | Return indices of people who are nan |
| unfilter | Set main simulation attributes to be views of the underlying data |
| unlock | Unlock the people object to allow keys to be added |
| validate | Perform validation on the People object. |
add_contacts
base.BasePeople.add_contacts(contacts, lkey=None, beta=None)Add new contacts to the array. See also contacts.add_layer().
addtoself
base.BasePeople.addtoself(people2)Combine two people arrays, avoiding dcp
count
base.BasePeople.count(key, weighted=True)Count the number of people for a given key
count_any
base.BasePeople.count_any(key, weighted=True)Count the number of people for a given key for a 2D array if any value matches
count_by_genotype
base.BasePeople.count_by_genotype(key, genotype, weighted=True)Count the number of people for a given key
date_keys
base.BasePeople.date_keys()Returns keys for different event dates (e.g., date a person became symptomatic)
defined
base.BasePeople.defined(key)Return indices of people who are not-nan
dur_keys
base.BasePeople.dur_keys()Returns keys for different durations (e.g., the duration from exposed to infectious)
false
base.BasePeople.false(key)Return indices not matching the condition
false_by_genotype
base.BasePeople.false_by_genotype(key, genotype)Return indices not matching genotype-condition
filter
base.BasePeople.filter(criteria)Store indices to allow for easy filtering of the People object.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| criteria | array | a boolean array for the filtering critria | required |
Returns
| Name | Type | Description |
|---|---|---|
| A filtered People object, which works just like a normal People object | ||
| except only operates on a subset of indices. |
filter_inds
base.BasePeople.filter_inds(inds)Store indices to allow for easy filtering of the People object.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| inds | array | filter by these indices | required |
Returns
| Name | Type | Description |
|---|---|---|
| A filtered People object, which works just like a normal People object | ||
| except only operates on a subset of indices. |
from_list
base.BasePeople.from_list(people)Convert a list of people back into a People object
get
base.BasePeople.get(key)Convenience method – key can be string or list of strings
imm_keys
base.BasePeople.imm_keys()Returns keys for different states of a person (e.g., symptomatic)
indices
base.BasePeople.indices()The indices of each people array
init_contacts
base.BasePeople.init_contacts(reset=False)Initialize the contacts dataframe with the correct columns and data types
initialize
base.BasePeople.initialize()Initialize underlying storage and map arrays
keys
base.BasePeople.keys()Returns keys for all non-derived properties of the people object
layer_keys
base.BasePeople.layer_keys()Get the available contact keys – try contacts first, then acts
load
base.BasePeople.load(filename, *args, **kwargs)Load from disk from a gzipped pickle.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| filename | str | the name or path of the file to load from | required |
| args | list | passed to hpv.load() |
() |
| kwargs | dict | passed to hpv.load() |
{} |
Returns
| Name | Type | Description |
|---|---|---|
| people | People | the loaded people object |
Example::
people = hpv.people.load('my-people.ppl')
lock
base.BasePeople.lock()Lock the people object to prevent keys from being added
make_edgelist
base.BasePeople.make_edgelist(contacts)Parse a list of people with a list of contacts per person and turn it into an edge list.
person
base.BasePeople.person(ind)Method to create person from the people
person_keys
base.BasePeople.person_keys()Returns keys specific to a person (e.g., their age)
remove_duplicates
base.BasePeople.remove_duplicates(df)Sort the dataframe and remove duplicates – note, not extensively tested
save
base.BasePeople.save(filename=None, force=False, **kwargs)Save to disk as a gzipped pickle.
Note: by default this function raises an exception if trying to save a run or partially run People object, since the changes that happen during a run are usually irreversible.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| filename | str or None | the name or path of the file to save to; if None, uses stored | None |
| force | bool | whether to allow saving even of a run or partially-run People object | False |
| kwargs | passed to sc.makefilepath() |
{} |
Returns
| Name | Type | Description |
|---|---|---|
| filename | str | the validated absolute path to the saved file |
Example::
sim = hpv.Sim()
sim.initialize()
sim.people.save() # Saves to a .ppl file
set_pars
base.BasePeople.set_pars(pars=None)Re-link the parameters stored in the people object to the sim containing it, and perform some basic validation.
state_keys
base.BasePeople.state_keys()Returns keys for different states of a person (e.g., symptomatic)
to_arr
base.BasePeople.to_arr()Return as numpy array
to_df
base.BasePeople.to_df()Convert to a Pandas dataframe
to_graph
base.BasePeople.to_graph()Convert all people to a networkx MultiDiGraph, including all properties of the people (nodes) and contacts (edges).
Example::
import hpvsim as hpv
import networkx as nx
sim = hpv.Sim(n_agents=50, pop_type='hybrid', contacts=dict(h=3, s=10, w=10, c=5)).run()
G = sim.people.to_graph()
nodes = G.nodes(data=True)
edges = G.edges(keys=True)
node_colors = [n['age'] for i,n in nodes]
layer_map = dict(h='#37b', s='#e11', w='#4a4', c='#a49')
edge_colors = [layer_map[G[i][j][k]['layer']] for i,j,k in edges]
edge_weights = [G[i][j][k]['beta']*5 for i,j,k in edges]
nx.draw(G, node_color=node_colors, edge_color=edge_colors, width=edge_weights, alpha=0.5)
to_list
base.BasePeople.to_list()Return all people as a list
true
base.BasePeople.true(key)Return indices matching the condition
true_by_genotype
base.BasePeople.true_by_genotype(key, genotype)Return indices matching genotype-condition
undefined
base.BasePeople.undefined(key)Return indices of people who are nan
unfilter
base.BasePeople.unfilter()Set main simulation attributes to be views of the underlying data
This method should be called whenever the number of agents required changes (regardless of whether or not the underlying arrays have been resized)
unlock
base.BasePeople.unlock()Unlock the people object to allow keys to be added
validate
base.BasePeople.validate(sim_pars=None, verbose=False)Perform validation on the People object.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| sim_pars | dict | dictionary of parameters from the sim to ensure they match the current People object | None |
| verbose | bool | detail to print | False |
BaseSim
base.BaseSim(*args, **kwargs)The BaseSim class stores various methods useful for the Sim that are not directly related to simulating the epidemic. It is not used outside of the Sim object, so the separation of methods into the BaseSim and Sim classes is purely to keep each one of manageable size.
Attributes
| Name | Description |
|---|---|
| n | Count the number of people – if it fails, assume none |
Methods
| Name | Description |
|---|---|
| copy | Returns a deep copy of the sim |
| export_pars | Return parameters for JSON export – see also to_json(). |
| export_results | Convert results to dict – see also to_json(). |
| get_analyzer | Same as get_intervention(), but for analyzers. |
| get_analyzers | Same as get_interventions(), but for analyzers. |
| get_intervention | Like get_interventions(), find the matching intervention(s) by label, |
| get_interventions | Find the matching intervention(s) by label, index, or type. If None, return |
| get_t | Convert a string, date/datetime object, or int to a timepoint (int). |
| load | Load from disk from a gzipped pickle. |
| result_keys | Get the actual results objects, not other things stored in sim.results. |
| result_types | Figure out what kind of result it is, which determines what plotting style to use |
| save | Save to disk as a gzipped pickle. |
| set_metadata | Set the metadata for the simulation – creation time and filename |
| shrink | “Shrinks” the simulation by removing the people and other memory-intensive |
| to_df | Export results to a pandas dataframe |
| to_excel | Export parameters and results as Excel format |
| to_json | Export results and parameters as JSON. |
| update_pars | Ensure that metaparameters get used properly before being updated |
copy
base.BaseSim.copy()Returns a deep copy of the sim
export_pars
base.BaseSim.export_pars(filename=None, indent=2, *args, **kwargs)Return parameters for JSON export – see also to_json().
This method is required so that interventions can specify their JSON-friendly representation.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| filename | str | filename to save to; if None, do not save | None |
| indent | int | indent (int): if writing to file, how many indents to use per nested level | 2 |
| args | list | passed to savejson() | () |
| kwargs | dict | passed to savejson() | {} |
Returns
| Name | Type | Description |
|---|---|---|
| pardict | dict | a dictionary containing all the parameter values |
export_results
base.BaseSim.export_results(
for_json=True,
filename=None,
indent=2,
*args,
**kwargs,
)Convert results to dict – see also to_json().
The results written to Excel must have a regular table shape, whereas for the JSON output, arbitrary data shapes are supported.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| for_json | bool | if False, only data associated with Result objects will be included in the converted output | True |
| filename | str | filename to save to; if None, do not save | None |
| indent | int | indent (int): if writing to file, how many indents to use per nested level | 2 |
| args | list | passed to savejson() | () |
| kwargs | dict | passed to savejson() | {} |
Returns
| Name | Type | Description |
|---|---|---|
| resdict | dict | dictionary representation of the results |
get_analyzer
base.BaseSim.get_analyzer(label=None, partial=False, first=False, die=True)Same as get_intervention(), but for analyzers.
get_analyzers
base.BaseSim.get_analyzers(label=None, partial=False, as_inds=False)Same as get_interventions(), but for analyzers.
get_intervention
base.BaseSim.get_intervention(label=None, partial=False, first=False, die=True)Like get_interventions(), find the matching intervention(s) by label, index, or type. If more than one intervention matches, return the last by default. If no label is provided, return the last intervention in the list.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| label | (str, int, Intervention, list) | the label, index, or type of intervention to get; if a list, iterate over one of those types | None |
| partial | bool | if true, return partial matches (e.g. ‘beta’ will match all beta interventions) | False |
| first | bool | if true, return first matching intervention (otherwise, return last) | False |
| die | bool | whether to raise an exception if no intervention is found | True |
Examples::
tp = hpv.test_prob(symp_prob=0.1)
cb = hpv.change_beta(days=5, changes=0.3, label='NPI')
sim = hpv.Sim(interventions=[tp, cb])
cb = sim.get_intervention('NPI')
cb = sim.get_intervention('NP', partial=True)
cb = sim.get_intervention(hpv.change_beta)
cb = sim.get_intervention(1)
cb = sim.get_intervention()
tp = sim.get_intervention(first=True)
get_interventions
base.BaseSim.get_interventions(label=None, partial=False, as_inds=False)Find the matching intervention(s) by label, index, or type. If None, return all interventions. If the label provided is “summary”, then print a summary of the interventions (index, label, type).
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| label | (str, int, Intervention, list) | the label, index, or type of intervention to get; if a list, iterate over one of those types | None |
| partial | bool | if true, return partial matches (e.g. ‘beta’ will match all beta interventions) | False |
| as_inds | bool | if true, return matching indices instead of the actual interventions | False |
Examples::
tp = hpv.test_prob(symp_prob=0.1)
cb1 = hpv.change_beta(days=5, changes=0.3, label='NPI')
cb2 = hpv.change_beta(days=10, changes=0.3, label='Masks')
sim = hpv.Sim(interventions=[tp, cb1, cb2])
cb1, cb2 = sim.get_interventions(hpv.change_beta)
tp, cb2 = sim.get_interventions([0,2])
ind = sim.get_interventions(hpv.change_beta, as_inds=True) # Returns [1,2]
sim.get_interventions('summary') # Prints a summary
get_t
base.BaseSim.get_t(dates, exact_match=False, return_date_format=None)Convert a string, date/datetime object, or int to a timepoint (int).
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| date | str, date, int, or list | convert any of these objects to a timepoint relative to the simulation’s start day | required |
| exact_match | bool | whether or not to demand an exact match to the requested date | False |
| return_date_format | (None, str) | if None, do not return dates; otherwise return them as strings or floats as requested | None |
Returns
| Name | Type | Description |
|---|---|---|
| t | int or str | the time point in the simulation cloesst to the requested date |
Examples::
sim.get_t('2015-03-01') # Get the closest timepoint to the specified date
sim.get_t(3) # Will return 3
sim.get_t('2015') # Can use strings
sim.get_t(['2015.5', '2016.5']) # List of strings, will match as close as possible
sim.get_t(['2015.5', '2016.5'], exact_match=True) # Raises an error since these dates aren't directly simulated
load
base.BaseSim.load(filename, *args, **kwargs)Load from disk from a gzipped pickle.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| filename | str | the name or path of the file to load from | required |
| kwargs | passed to hpv.load() | {} |
Returns
| Name | Type | Description |
|---|---|---|
| sim | Sim |
the loaded simulation object |
Example::
sim = hpv.Sim.load('my-simulation.sim')
result_keys
base.BaseSim.result_keys(which='all')Get the actual results objects, not other things stored in sim.results.
If which is ‘total’, return only the main results keys. If ‘genotype’, return only genotype keys. If ‘all’, return all keys.
result_types
base.BaseSim.result_types(reskeys)Figure out what kind of result it is, which determines what plotting style to use
save
base.BaseSim.save(filename=None, keep_people=None, skip_attrs=None, **kwargs)Save to disk as a gzipped pickle.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| filename | str or None | the name or path of the file to save to; if None, uses stored | None |
| kwargs | passed to sc.makefilepath() | {} |
Returns
| Name | Type | Description |
|---|---|---|
| filename | str | the validated absolute path to the saved file |
Example::
sim.save() # Saves to a .sim file
set_metadata
base.BaseSim.set_metadata(simfile)Set the metadata for the simulation – creation time and filename
shrink
base.BaseSim.shrink(skip_attrs=None, in_place=True)“Shrinks” the simulation by removing the people and other memory-intensive attributes (e.g., some interventions and analyzers), and returns a copy of the “shrunken” simulation. Used to reduce the memory required for RAM or for saved files.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| skip_attrs | list | a list of attributes to skip (remove) in order to perform the shrinking; default “people” | None |
| in_palce | bool | whether to perform the shrinking in place (default), or return a shrunken copy instead | required |
Returns
| Name | Type | Description |
|---|---|---|
| shrunken | Sim |
a Sim object with the listed attributes removed |
to_df
base.BaseSim.to_df(date_index=False)Export results to a pandas dataframe
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| date_index | bool | if True, use the date as the index | False |
to_excel
base.BaseSim.to_excel(filename=None, skip_pars=None)Export parameters and results as Excel format
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| filename | str | if None, return string; else, write to file | None |
| skip_pars | list | if provided, a custom list parameters to exclude | None |
Returns
| Name | Type | Description |
|---|---|---|
| An sc.Spreadsheet with an Excel file, or writes the file to disk |
to_json
base.BaseSim.to_json(
filename=None,
keys=None,
tostring=False,
indent=2,
verbose=False,
*args,
**kwargs,
)Export results and parameters as JSON.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| filename | str | if None, return string; else, write to file | None |
| keys | str or list | attributes to write to json (default: results, parameters, and summary) | None |
| tostring | bool | if not writing to file, whether to write to string (alternative is sanitized dictionary) | False |
| indent | int | if writing to file, how many indents to use per nested level | 2 |
| verbose | bool | detail to print | False |
| args | list | passed to savejson() | () |
| kwargs | dict | passed to savejson() | {} |
Returns
| Name | Type | Description |
|---|---|---|
| A unicode string containing a JSON representation of the results, | ||
| or writes the JSON file to disk |
Examples::
json = sim.to_json()
sim.to_json('results.json')
sim.to_json('summary.json', keys='summary')
update_pars
base.BaseSim.update_pars(pars=None, create=False, **kwargs)Ensure that metaparameters get used properly before being updated
Contacts
base.Contacts(data=None, layer_keys=None, **kwargs)A simple (for now) class for storing different contact layers.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| data | dict | a dictionary that looks like a Contacts object | None |
| layer_keys | list | if provided, create an empty Contacts object with these layers | None |
| kwargs | dict | additional layer(s), merged with data | {} |
Methods
| Name | Description |
|---|---|
| add_layer | Small method to add one or more layers to the contacts. Layers should |
| pop_layer | Remove the layer(s) from the contacts. |
| to_graph | Convert all layers to a networkx MultiDiGraph |
add_layer
base.Contacts.add_layer(**kwargs)Small method to add one or more layers to the contacts. Layers should be provided as keyword arguments.
Example::
hospitals_layer = hpv.Layer(label='hosp')
sim.people.contacts.add_layer(hospitals=hospitals_layer)
pop_layer
base.Contacts.pop_layer(*args)Remove the layer(s) from the contacts.
Example::
sim.people.contacts.pop_layer('hospitals')
Note: while included here for convenience, this operation is equivalent to simply popping the key from the contacts dictionary.
to_graph
base.Contacts.to_graph()Convert all layers to a networkx MultiDiGraph
Example::
import networkx as nx
sim = hpv.Sim(n_agents=50, pop_type='hybrid').run()
G = sim.people.contacts.to_graph()
nx.draw(G)
FlexDict
base.FlexDict()A dict that allows more flexible element access: in addition to obj[‘a’], also allow obj[0]. Lightweight implementation of the Sciris odict class.
Layer
base.Layer(*args, label=None, **kwargs)A small class holding a single layer of contact edges (connections) between people.
The input is typically arrays including: person 1 of the connection, person 2 of the connection, the weight of the connection, the duration and start/end times of the connection. Connections are undirected; each person is both a source and sink.
This class is usually not invoked directly by the user, but instead is called as part of the population creation.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| f | array | an array of N connections, representing people on one side of the connection | required |
| m | array | an array of people on the other side of the connection | required |
| acts | array | an array of number of acts per timestep for each connection | required |
| dur | array | duration of the connection | required |
| start | array | start time of the connection | required |
| end | array | end time of the connection | required |
| label | str | the name of the layer (optional) | None |
| kwargs | dict | other keys copied directly into the layer | {} |
Note that all arguments (except for label) must be arrays of the same length, although not all have to be supplied at the time of creation (they must all be the same at the time of initialization, though, or else validation will fail).
Examples::
# Generate an average of 10 contacts for 1000 people
n = 10_000
n_people = 1000
p1 = np.random.randint(n_people, size=n)
p2 = np.random.randint(n_people, size=n)
beta = np.ones(n)
layer = hpv.Layer(p1=p1, p2=p2, beta=beta, label='rand')
layer = hpv.Layer(dict(p1=p1, p2=p2, beta=beta), label='rand') # Alternate method
# Convert one layer to another with extra columns
index = np.arange(n)
self_conn = p1 == p2
layer2 = hpv.Layer(**layer, index=index, self_conn=self_conn, label=layer.label)
Attributes
| Name | Description |
|---|---|
| members | Return sorted array of all members |
Methods
| Name | Description |
|---|---|
| append | Append contacts to the current layer. |
| find_contacts | Find all contacts of the specified people |
| from_df | Convert from a dataframe |
| get_inds | Get the specified indices from the edgelist and return them as a dict. |
| meta_keys | Return the keys for the layer’s meta information – i.e., f, m, beta, any others |
| pop_inds | “Pop” the specified indices from the edgelist and return them as a dict. |
| to_df | Convert to dataframe |
| to_graph | Convert to a networkx DiGraph |
| update | Regenerate contacts on each timestep. |
| validate | Check the integrity of the layer: right types, right lengths. |
append
base.Layer.append(contacts)Append contacts to the current layer.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| contacts | dict | a dictionary of arrays with keys f,m,beta, as returned from layer.pop_inds() | required |
find_contacts
base.Layer.find_contacts(inds, as_array=True)Find all contacts of the specified people
For some purposes (e.g. contact tracing) it’s necessary to find all of the contacts associated with a subset of the people in this layer. Since contacts are bidirectional it’s necessary to check both P1 and P2 for the target indices. The return type is a Set so that there is no duplication of indices (otherwise if the Layer has explicit symmetric interactions, they could appear multiple times). This is also for performance so that the calling code doesn’t need to perform its own unique() operation. Note that this cannot be used for cases where multiple connections count differently than a single infection, e.g. exposure risk.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| inds | array | indices of people whose contacts to return | required |
| as_array | bool | if true, return as sorted array (otherwise, return as unsorted set) | True |
Returns
| Name | Type | Description |
|---|---|---|
| contact_inds | array | a set of indices for pairing partners |
Example: If there were a layer with - P1 = [1,2,3,4] - P2 = [2,3,1,4] Then find_contacts([1,3]) would return {1,2,3}
from_df
base.Layer.from_df(df, keys=None)Convert from a dataframe
get_inds
base.Layer.get_inds(inds, remove=False)Get the specified indices from the edgelist and return them as a dict.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| inds | (int, array, slice) | the indices to be removed | required |
meta_keys
base.Layer.meta_keys()Return the keys for the layer’s meta information – i.e., f, m, beta, any others
pop_inds
base.Layer.pop_inds(inds)“Pop” the specified indices from the edgelist and return them as a dict. Returns in the right format to be used with layer.append().
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| inds | (int, array, slice) | the indices to be removed | required |
to_df
base.Layer.to_df()Convert to dataframe
to_graph
base.Layer.to_graph()Convert to a networkx DiGraph
Example::
import networkx as nx
sim = hpv.Sim(n_agents=20, pop_type='hybrid').run()
G = sim.people.contacts['h'].to_graph()
nx.draw(G)
update
base.Layer.update(people, frac=1.0)Regenerate contacts on each timestep.
This method gets called if the layer appears in sim.pars['dynam_layer']. The Layer implements the update procedure so that derived classes can customize the update e.g. implementing over-dispersion/other distributions, random clusters, etc.
Typically, this method also takes in the people object so that the update can depend on person attributes that may change over time (e.g. changing contacts for people that are severe/critical).
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| people | People | the HPVsim People object, which is usually used to make new contacts | required |
| frac | float | the fraction of contacts to update on each timestep | 1.0 |
validate
base.Layer.validate(force=True)Check the integrity of the layer: right types, right lengths.
If dtype is incorrect, try to convert automatically; if length is incorrect, do not.
ParsObj
base.ParsObj(pars)A class based around performing operations on a self.pars dict.
Methods
| Name | Description |
|---|---|
| update_pars | Update internal dict with new pars. |
update_pars
base.ParsObj.update_pars(pars=None, create=False)Update internal dict with new pars.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| pars | dict | the parameters to update (if None, do nothing) | None |
| create | bool | if create is False, then raise a KeyNotFoundError if the key does not already exist | False |
Person
base.Person(
pars=None,
uid=None,
age=-1,
sex=-1,
debut=-1,
rel_sev=-1,
partners=None,
current_partners=None,
rship_start_dates=None,
rship_end_dates=None,
n_rships=None,
)Class for a single person. Note: this is largely deprecated since sim.people is now based on arrays rather than being a list of people.
Result
base.Result(name=None, npts=None, scale=True, color=None, n_rows=0, n_copies=0)Stores a single result – by default, acts like an array.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| name | str | name of this result, e.g. new_infections | None |
| npts | int | if values is None, precreate it to be of this length | None |
| scale | bool | whether or not the value scales by population scale factor | True |
| color | str / arr |
default color for plotting (hex or RGB notation) | None |
Example::
import hpvsim as hpv
r1 = hpv.Result(name='test1', npts=10)
r1[:5] = 20
print(r1.values)
Methods
| Name | Description |
|---|---|
| mean | To allow result.mean() instead of result.values.mean() |
| median | To allow result.median() instead of result.values.median() |
| sum | To allow result.sum() instead of result.values.sum() |
mean
base.Result.mean()To allow result.mean() instead of result.values.mean()
median
base.Result.median()To allow result.median() instead of result.values.median()
sum
base.Result.sum()To allow result.sum() instead of result.values.sum()