Skip to content

Commit ec167a7

Browse files
Collapse values into a dtyped array if possible
This way variables of previously unknown shape have a higher chance of resulting in arrays with well-defined dtype. Resolves a bug where `.to_inferencedata()` failed because of `dtype=object` arrays in sample stats.
1 parent 7a0c6ce commit ec167a7

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

mcbackend/backends/clickhouse.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,19 @@ def _get_rows( # pylint: disable=W0221
189189
assert nshape is not None
190190
buffer = numpy.empty((draws, *nshape), dtype)
191191
else:
192-
buffer = numpy.array([None] * draws)
192+
buffer = [None] * draws
193193
for d, (vals,) in enumerate(data):
194194
buffer[d] = numpy.asarray(vals, dtype)
195-
return buffer
195+
196+
# If the values are "ragged" (have different shapes) the
197+
# numpy array must be dtype=object.
198+
if len({v.shape for v in buffer}) > 1:
199+
# To circumvent NumPy issue #19113
200+
arr = numpy.empty(draws, dtype=object)
201+
arr[:] = buffer
202+
return arr
203+
# Otherwise (identical shapes) we can collapse into one ndarray
204+
return numpy.asarray(buffer, dtype=dtype)
196205

197206
def get_draws(self, var_name: str) -> numpy.ndarray:
198207
var = self.variables[var_name]

0 commit comments

Comments
 (0)