{"action":"created","issue":{"url":"https://api.github.com/repos/pymc-devs/pymc3/issues/3179","repository_url":"https://api.github.com/repos/pymc-devs/pymc3","labels_url":"https://api.github.com/repos/pymc-devs/pymc3/issues/3179/labels{/name}","comments_url":"https://api.github.com/repos/pymc-devs/pymc3/issues/3179/comments","events_url":"https://api.github.com/repos/pymc-devs/pymc3/issues/3179/events","html_url":"https://github.com/pymc-devs/pymc3/issues/3179","id":355771412,"node_id":"MDU6SXNzdWUzNTU3NzE0MTI=","number":3179,"title":"Error in trace.report._run_convergence_checks()","user":{"login":"rpgoldman","id":3274,"node_id":"MDQ6VXNlcjMyNzQ=","avatar_url":"https://avatars3.githubusercontent.com/u/3274?v=4","gravatar_id":"","url":"https://api.github.com/users/rpgoldman","html_url":"https://github.com/rpgoldman","followers_url":"https://api.github.com/users/rpgoldman/followers","following_url":"https://api.github.com/users/rpgoldman/following{/other_user}","gists_url":"https://api.github.com/users/rpgoldman/gists{/gist_id}","starred_url":"https://api.github.com/users/rpgoldman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rpgoldman/subscriptions","organizations_url":"https://api.github.com/users/rpgoldman/orgs","repos_url":"https://api.github.com/users/rpgoldman/repos","events_url":"https://api.github.com/users/rpgoldman/events{/privacy}","received_events_url":"https://api.github.com/users/rpgoldman/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-08-30T22:07:02Z","updated_at":"2018-08-31T13:57:49Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"Running sample with `trace=db` for `db` a text backend, causes an error in `trace.report._run_convergence_checks()`.\r\n\r\n## Description of your problem\r\n\r\nIf I create a `pm.backends.Text` instance, and pass it as the `trace` argument to the `pm.sampling.sample()` function, I get an error.  When I remove that argument, the call terminates successfully.\r\n\r\nInvestigating the traceback, I find that when I pass the `trace=db` with multiple chains, the above function receives a trace (return value from `get_values`) that is of type `Array[nchains]` where each entry in the array is of type `numpy.ndarray[nsamples]`.  However, the code **expects** a trace of type `Array[nchains][nsamples]`.  In particular, it expects `shape` to be a 2-tuple, when it's only `(20,)`.\r\n\r\n\r\n**Please provide a minimal, self-contained, and reproducible example.**\r\n```python\r\n    with model.model:\r\n        # db = pm.backends.Text(trace_filename)\r\n        model.trace = pm.sampling.sample(num_draws=10000, chains=20, cores=4, tune=1000) # , trace=db)\r\n```\r\nThe above is not fully reproducible, I am afraid.\r\n\r\n**Please provide the full traceback.**\r\n```python\r\nValueError                                Traceback (most recent call last)\r\n<ipython-input-73-88e4799fa24e> in <module>()\r\n----> 1 do_main('NOR')\r\n\r\n~/projects/xplan/yeast-gates-data/Three_Layer_Analysis.py in do_main(gate)\r\n    237     with model.model:\r\n    238         db = pm.backends.Text(trace_filename)\r\n--> 239         model.trace = pm.sampling.sample(num_draws=10000, chains=20, cores=4, trace=db)\r\n    240     entropy = model.entropy()\r\n    241     return entropy\r\n\r\n~/Library/Python/3.7/lib/python/site-packages/pymc3/sampling.py in sample(draws, step, init, n_init, start, trace, chain_idx, chains, cores, tune, nuts_kwargs, step_kwargs, progressbar, model, random_seed, live_plot, discard_tuned_samples, live_plot_kwargs, compute_convergence_checks, use_mmap, **kwargs)\r\n    467                 warnings.warn(\"The number of samples is too small to check convergence reliably.\")\r\n    468             else:\r\n--> 469                 trace.report._run_convergence_checks(trace, model)\r\n    470 \r\n    471         trace.report._log_summary()\r\n\r\n~/Library/Python/3.7/lib/python/site-packages/pymc3/backends/report.py in _run_convergence_checks(self, trace, model)\r\n     81                 varnames.append(rv_name)\r\n     82 \r\n---> 83         self._effective_n = effective_n = diagnostics.effective_n(trace, varnames)\r\n     84         self._gelman_rubin = gelman_rubin = diagnostics.gelman_rubin(trace, varnames)\r\n     85 \r\n\r\n~/Library/Python/3.7/lib/python/site-packages/pymc3/diagnostics.py in effective_n(mtrace, varnames, include_transformed)\r\n    298 \r\n    299     for var in varnames:\r\n--> 300         n_eff[var] = generate_neff(mtrace.get_values(var, combine=False))\r\n    301 \r\n    302     return n_eff\r\n\r\n~/Library/Python/3.7/lib/python/site-packages/pymc3/diagnostics.py in generate_neff(trace_values)\r\n    276         # Iterate over tuples of indices of the shape of var\r\n    277         for tup in np.ndindex(*list(x.shape[:-2])):\r\n--> 278             _n_eff[tup] = get_neff(x[tup])\r\n    279 \r\n    280         if len(shape) == 2:\r\n\r\n~/Library/Python/3.7/lib/python/site-packages/pymc3/diagnostics.py in get_neff(x)\r\n    217         \"\"\"\r\n    218         trace_value = x.T\r\n--> 219         nchain, n_samples = trace_value.shape\r\n    220 \r\n    221         acov = np.asarray([autocov(trace_value[chain]) for chain in range(nchain)])\r\n\r\nValueError: not enough values to unpack (expected 2, got 1)\r\n```\r\n\r\n**Please provide any additional information below.**\r\n\r\nSee my discussion here: https://discourse.pymc.io/t/chains-parameter-and-multiprocess-sampling/1844\r\n\r\nNote that when I pass a `text` backend to trace, I am passing an instance of `backends.text` which is **not** an instance of `MultiTrace`.  I believe that the root of the problem is the difference between the method for `get_values` on `backends.text`:\r\n\r\n    def get_values(self, varname, burn=0, thin=1):\r\n        \"\"\"Get values from trace.\r\n\r\n        Parameters\r\n        ----------\r\n        varname : str\r\n        burn : int\r\n        thin : int\r\n\r\n        Returns\r\n        -------\r\n        A NumPy array\r\n        \"\"\"\r\n        self._load_df()\r\n        var_df = self.df[self.flat_names[varname]]\r\n        shape = (self.df.shape[0],) + self.var_shapes[varname]\r\n        vals = var_df.values.ravel().reshape(shape)\r\n        return vals[burn::thin]\r\n\r\nand the version for `MultiTrace`:\r\n\r\n    def get_values(self, varname, burn=0, thin=1, combine=True, chains=None,\r\n                   squeeze=True):\r\n        \"\"\"Get values from traces.\r\n\r\n        Parameters\r\n        ----------\r\n        varname : str\r\n        burn : int\r\n        thin : int\r\n        combine : bool\r\n            If True, results from `chains` will be concatenated.\r\n        chains : int or list of ints\r\n            Chains to retrieve. If None, all chains are used. A single\r\n            chain value can also be given.\r\n        squeeze : bool\r\n            Return a single array element if the resulting list of\r\n            values only has one element. If False, the result will\r\n            always be a list of arrays, even if `combine` is True.\r\n\r\n        Returns\r\n        -------\r\n        A list of NumPy arrays or a single NumPy array (depending on\r\n        `squeeze`).\r\n        \"\"\"\r\n        if chains is None:\r\n            chains = self.chains\r\n        varname = str(varname)\r\n        try:\r\n            results = [self._straces[chain].get_values(varname, burn, thin)\r\n                       for chain in chains]\r\n        except TypeError:  # Single chain passed.\r\n            results = [self._straces[chains].get_values(varname, burn, thin)]\r\n        return _squeeze_cat(results, combine, squeeze)\r\n\r\nThese two methods can return values of pretty different types, that need to be indexed differently.\r\n\r\n## Versions and main components\r\n\r\n* PyMC3 Version:  HEAD -- 76c024312fc4d84dc7f56885f895adb09fb74d09\r\n* Theano Version: \r\n* Python Version: 3.7\r\n* Operating system: MacOS\r\n* How did you install PyMC3: (conda/pip) pip from the source\r\n"},"comment":{"url":"https://api.github.com/repos/pymc-devs/pymc3/issues/comments/417672512","html_url":"https://github.com/pymc-devs/pymc3/issues/3179#issuecomment-417672512","issue_url":"https://api.github.com/repos/pymc-devs/pymc3/issues/3179","id":417672512,"node_id":"MDEyOklzc3VlQ29tbWVudDQxNzY3MjUxMg==","user":{"login":"rpgoldman","id":3274,"node_id":"MDQ6VXNlcjMyNzQ=","avatar_url":"https://avatars3.githubusercontent.com/u/3274?v=4","gravatar_id":"","url":"https://api.github.com/users/rpgoldman","html_url":"https://github.com/rpgoldman","followers_url":"https://api.github.com/users/rpgoldman/followers","following_url":"https://api.github.com/users/rpgoldman/following{/other_user}","gists_url":"https://api.github.com/users/rpgoldman/gists{/gist_id}","starred_url":"https://api.github.com/users/rpgoldman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rpgoldman/subscriptions","organizations_url":"https://api.github.com/users/rpgoldman/orgs","repos_url":"https://api.github.com/users/rpgoldman/repos","events_url":"https://api.github.com/users/rpgoldman/events{/privacy}","received_events_url":"https://api.github.com/users/rpgoldman/received_events","type":"User","site_admin":false},"created_at":"2018-08-31T13:57:49Z","updated_at":"2018-08-31T13:57:49Z","author_association":"CONTRIBUTOR","body":"P.S. I will see if a workaround is possible by disabling the convergence checks for now."}}
{"actor":{"display_login":"rpgoldman"}}