Improve the check for whether a state is idiosyncratic
@@ -522,7 +522,8 @@ class AgentType(Model):
for var_name in self.state_now:
# Copy only array-like idiosyncratic states. Aggregates should
# not be set by newborns
- if hasattr(self.state_now[var_name], "shape"):
+ idio = isinstance(self.state_now[var_name], np.ndarray) and len(self.state_now[var_name]) == self.AgentCount
+ if idio:
self.state_now[var_name] = self.newborn_init_history[var_name][0]
self.clear_history()
@@ -615,8 +616,9 @@ class AgentType(Model):
# Record the initial condition of the newborns created by
# initialize_sim -> sim_births
for var_name in self.state_vars:
- # Check whether the state is array-like or an aggregate
- if hasattr(self.state_now[var_name], "shape"):
+ # Check whether the state is idiosyncratic or an aggregate
+ idio = isinstance(self.state_now[var_name], np.ndarray) and len(self.state_now[var_name]) == self.AgentCount
+ if idio:
self.newborn_init_history[var_name][self.t_sim] = self.state_now[
var_name
]
@@ -635,8 +637,9 @@ class AgentType(Model):
# Initial conditions of newborns
if np.sum(self.who_dies) > 0:
for var_name in self.state_vars:
- # Check whether the state is array-like or an aggregate
- if hasattr(self.state_now[var_name], "shape"):
+ # Check whether the state is idiosyncratic or an aggregate
+ idio = isinstance(self.state_now[var_name], np.ndarray) and len(self.state_now[var_name]) == self.AgentCount
+ if idio:
self.newborn_init_history[var_name][
self.t_sim, self.who_dies
] = self.state_now[var_name][self.who_dies]
@@ -683,7 +686,8 @@ class AgentType(Model):
for var_name in self.state_now:
# Copy only array-like idiosyncratic states. Aggregates should
# not be set by newborns
- if hasattr(self.state_now[var_name], "shape"):
+ idio = isinstance(self.state_now[var_name], np.ndarray) and len(self.state_now[var_name]) == self.AgentCount
+ if idio:
self.state_now[var_name][who_dies] = self.newborn_init_history[
var_name
][self.t_sim, who_dies]
