Skip to content
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

Calling gs.Gaussian() with dim=3 leads to TypeError: 'float' object cannot be interpreted as an integer #183

Closed
MatusGasparik opened this issue Jul 2, 2021 · 2 comments · Fixed by #184
Assignees
Labels
bug Something isn't working external problem
Milestone

Comments

@MatusGasparik
Copy link

This works:

model = gs.Gaussian(dim=2, var=1)
srf = gs.SRF(model, seed=20170519)

But this doesn't:

model = gs.Gaussian(dim=3, var=1)
srf = gs.SRF(model, seed=20170519)

Traceback:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-76-0274372fd2dd> in <module>
      3 
      4 model = gs.Gaussian(dim=3, var=1) #, len_scale=10)
----> 5 srf = gs.SRF(model, seed=20170519)
      6 
      7 #x = y = z = np.arange(0, 10)

~/miniconda3/envs/indust39/lib/python3.9/site-packages/gstools/field/srf.py in __init__(self, model, mean, normalizer, trend, upscaling, generator, **generator_kwargs)
     98         # initialize attributes
     99         self.upscaling = upscaling
--> 100         self.set_generator(generator, **generator_kwargs)
    101 
    102     def __call__(

~/miniconda3/envs/indust39/lib/python3.9/site-packages/gstools/field/srf.py in set_generator(self, generator, **generator_kwargs)
    159         if generator in GENERATOR:
    160             gen = GENERATOR[generator]
--> 161             self._generator = gen(self.model, **generator_kwargs)
    162             self.value_type = self._generator.value_type
    163         else:

~/miniconda3/envs/indust39/lib/python3.9/site-packages/gstools/field/generator.py in __init__(self, model, mode_no, seed, verbose, sampling, **kwargs)
    106         self.sampling = sampling
    107         # set model and seed
--> 108         self.update(model, seed)
    109 
    110     def __call__(self, pos, add_nugget=True):

~/miniconda3/envs/indust39/lib/python3.9/site-packages/gstools/field/generator.py in update(self, model, seed)
    172                 self._model = dcp(model)
    173                 if seed is None or not np.isnan(seed):
--> 174                     self.reset_seed(seed)
    175                 else:
    176                     self.reset_seed(self._seed)

~/miniconda3/envs/indust39/lib/python3.9/site-packages/gstools/field/generator.py in reset_seed(self, seed)
    240             )
    241         else:
--> 242             rad = self._rng.sample_ln_pdf(
    243                 ln_pdf=self.model.ln_spectral_rad_pdf,
    244                 size=self._mode_no,

~/miniconda3/envs/indust39/lib/python3.9/site-packages/gstools/random/rng.py in sample_ln_pdf(self, ln_pdf, size, sample_around, nwalkers, burn_in, oversampling_factor)
     96         initial_state = State(burn_in_state, copy=True)
     97         initial_state.random_state = self.random.get_state()
---> 98         sampler.run_mcmc(initial_state=initial_state, nsteps=sample_size)
     99         samples = sampler.get_chain(flat=True)[:, 0]
    100 

~/miniconda3/envs/indust39/lib/python3.9/site-packages/emcee/ensemble.py in run_mcmc(self, initial_state, nsteps, **kwargs)
    436 
    437         results = None
--> 438         for results in self.sample(initial_state, iterations=nsteps, **kwargs):
    439             pass
    440 

~/miniconda3/envs/indust39/lib/python3.9/site-packages/emcee/ensemble.py in sample(self, initial_state, log_prob0, rstate0, blobs0, iterations, tune, skip_initial_state_check, thin_by, thin, store, progress)
    374             checkpoint_step = thin_by
    375             if store:
--> 376                 self.backend.grow(iterations, state.blobs)
    377 
    378         # Set up a wrapper around the relevant model functions

~/miniconda3/envs/indust39/lib/python3.9/site-packages/emcee/backends/backend.py in grow(self, ngrow, blobs)
    173         self._check_blobs(blobs)
    174         i = ngrow - (len(self.chain) - self.iteration)
--> 175         a = np.empty((i, self.nwalkers, self.ndim), dtype=self.dtype)
    176         self.chain = np.concatenate((self.chain, a), axis=0)
    177         a = np.empty((i, self.nwalkers), dtype=self.dtype)

TypeError: 'float' object cannot be interpreted as an integer

The problem seems to originate in gs.random.rng.RNG.sample_ln_pdf():

        #.....
        if size is None:  # pragma: no cover
            sample_size = burn_in
        else:
            sample_size = max(burn_in, (size / nwalkers) * oversampling_factor)

...which later calls sampler.run_mcmc() with nsteps=sample_size but with sample_size=200.0 being a float:

sampler.run_mcmc(initial_state=initial_state, nsteps=sample_size)
@MuellerSeb
Copy link
Member

Hey there!

Good catch. This should have been converted to integer in the past already. I guess the problem never ocured, since it was not a problem with older versions of emcee, that got a new release last week: https://github.com/dfm/emcee/releases/tag/v3.1.0

I will come up with a bugfix. Sorry for the inconvenience and thank you very much for the investigation of this error.

For now you could downgrade your emcee version to 3.0.2.

Cheers,
Sebastian

@MuellerSeb
Copy link
Member

This is now fixed in the latest release 1.3.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working external problem
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants