[docs]classCensus:def__init__(self,model)->None:self.model=modelasserthasattr(model,"patches"),"Census: model needs to have a 'patches' attribute."model.patches.add_vector_property("N",length=model.params.nticks+1,dtype=np.int32,default=0)asserthasattr(self.model,"params"),"Census: model needs to have a 'params' attribute."return
[docs]defcheck(self):self(self.model,-1)# a little hacky, but we need to set the initial population sizereturn
def__call__(self,model,tick:int)->None:forcompartmentin["S","E","Isym","Iasym","R","V1","V2"]:ifhasattr(model.agents,compartment):model.patches.N[tick+1]+=getattr(model.agents,compartment)[tick+1]assertnp.all(model.patches.N[tick+1]>=0),"N' should not go negative"return
[docs]defplot(self,fig:Figure=None):# pragma: no cover_fig=plt.figure(figsize=(12,9),dpi=128,num="Census (Total Population)")iffigisNoneelsefigforipatchinnp.argsort(self.model.params.S_j_initial)[-10:]:plt.plot(self.model.patches.N[:,ipatch],label=f"{self.model.params.location_name[ipatch]}")plt.xlabel("Tick")plt.ylabel("Total Population")plt.legend()yield"Census (Total Population)"return