From 0b8aec277517eb940954cd84634c9adf8bd37707 Mon Sep 17 00:00:00 2001 From: Marcel Stimberg Date: Wed, 4 May 2022 17:06:27 +0200 Subject: [PATCH] Handle .ndim and [:, np.newaxis] on VariableView These are used when handing a VariableView directly to matplotlib, e.g. for spike_mon.i (an expression like spike_mon.t/ms directly results in an ndarray and is therefore not concerned). This problem was introduced with matplotlib 3.5.2, previous versions converted VariableView along the way with a call to np.asanyarray. Fixes #1409 --- brian2/core/variables.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/brian2/core/variables.py b/brian2/core/variables.py index 841d49c0c..da57977d0 100644 --- a/brian2/core/variables.py +++ b/brian2/core/variables.py @@ -1,4 +1,3 @@ - """ Classes used to specify the type of a function, variable or common sub-expression. @@ -1091,6 +1090,16 @@ def get_with_index_array(self, item): elif (isinstance(item, slice) and item == slice(None) and self.index_var == '_idx'): indices = slice(None) + # Quick fix for matplotlib calling 1-d variables as var[:, np.newaxis] + # The test is a bit verbose, but we need to avoid comparisons that raise errors + # (e.g. comparing an array to slice(None)) + elif (isinstance(item, tuple) and + len(item) == 2 and + isinstance(item[0], slice) and + item[0] == slice(None) and + item[1] is None and + self.index_var == '_idx'): + return variable.get_value()[item] else: indices = self.indexing(item, self.index_var) @@ -1400,6 +1409,10 @@ def __repr__(self): def shape(self): return self.get_item(slice(None), level=1).shape + @property + def ndim(self): + return self.get_item(slice(None), level=1).ndim + @property def dtype(self): return self.get_item(slice(None), level=1).dtype