@@ -85,6 +85,10 @@ def __getattr__(self, attr: str) -> np.ndarray:
8585 # pylint: disable=raise-missing-from
8686 raise AttributeError (* e .args )
8787
88+ def __getstate__ (self ) -> dict :
89+ self ._assemble_generated_quantities ()
90+ return self .__dict__
91+
8892 def _validate_csv_files (self ) -> Dict [str , Any ]:
8993 """
9094 Checks that Stan CSV output files for all chains are consistent
@@ -189,8 +193,7 @@ def draws(
189193 CmdStanGQ.draws_xr
190194 CmdStanMCMC.draws
191195 """
192- if self ._draws .shape == (0 ,):
193- self ._assemble_generated_quantities ()
196+ self ._assemble_generated_quantities ()
194197 if (
195198 inc_warmup
196199 and not self .mcmc_sample .metadata .cmdstan_config ['save_warmup' ]
@@ -277,8 +280,7 @@ def draws_pd(
277280 'Draws from warmup iterations not available,'
278281 ' must run sampler with "save_warmup=True".'
279282 )
280- if self ._draws .shape == (0 ,):
281- self ._assemble_generated_quantities ()
283+ self ._assemble_generated_quantities ()
282284
283285 gq_cols = []
284286 mcmc_vars = []
@@ -400,8 +402,7 @@ def draws_xr(
400402 for var in dup_vars :
401403 vars_list .remove (var )
402404
403- if self ._draws .shape == (0 ,):
404- self ._assemble_generated_quantities ()
405+ self ._assemble_generated_quantities ()
405406
406407 num_draws = self .mcmc_sample .num_draws_sampling
407408 sample_config = self .mcmc_sample .metadata .cmdstan_config
@@ -505,8 +506,7 @@ def stan_variable(
505506 if var not in gq_var_names :
506507 return self .mcmc_sample .stan_variable (var , inc_warmup = inc_warmup )
507508 else : # is gq variable
508- if self ._draws .shape == (0 ,):
509- self ._assemble_generated_quantities ()
509+ self ._assemble_generated_quantities ()
510510 draw1 = 0
511511 if (
512512 not inc_warmup
@@ -561,6 +561,8 @@ def stan_variables(self, inc_warmup: bool = False) -> Dict[str, np.ndarray]:
561561 return result
562562
563563 def _assemble_generated_quantities (self ) -> None :
564+ if self ._draws .shape != (0 ,):
565+ return
564566 # use numpy loadtxt
565567 warmup = self .mcmc_sample .metadata .cmdstan_config ['save_warmup' ]
566568 num_draws = self .mcmc_sample .draws (inc_warmup = warmup ).shape [0 ]
0 commit comments