Skip to content

Commit 89646f1

Browse files
committed
[Fix] catch and fix a weird bug in sparse arrays -- this is really a mpython_endpoint issue, but let's be robust here as well
1 parent a8402de commit 89646f1

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

mpython/core/mixin_types.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,28 @@ def _from_runtime(cls, dictobj: dict):
8686
raise ValueError("Not a matlab sparse matrix")
8787
size = np.array(dictobj["size__"], dtype=np.uint64).ravel()
8888
size = size.tolist()
89+
ndim = len(size)
8990
if isinstance(dictobj["values__"], float):
9091
dtype = np.double
9192
else:
9293
dtype = _matlab_array_types()[type(dictobj["values__"])]
93-
indices = np.asarray(dictobj["indices__"], dtype=np.long) - 1
94+
indices = np.asarray(dictobj["indices__"], dtype=np.long)
95+
# print('from_matlab', indices.shape)
9496
values = np.asarray(dictobj["values__"], dtype=dtype).ravel()
97+
# indices = indices.reshape([ndim, -1])
98+
indices -= 1
9599
if indices.size == 0:
96-
indices = indices.reshape([0, len(size)])
100+
indices = indices.reshape([0, ndim])
101+
elif indices.shape[0] == 1:
102+
# NOTE: I've encountered this issue while runngin the PEB
103+
# tutorial, but it is difficult to find a minimal example.
104+
# It seems that for some reason, find() has returned row vectors
105+
# instead of column vectors, so [ii, jj] generates a long row
106+
# vector. When this is detected, I properly unfold the data,
107+
# but this should probably be fixed in mpython_endpoint
108+
# as well.
109+
indices = indices.reshape([ndim, -1]).T
110+
# print(indices)
97111
return cls.from_coo(values, indices.T, size)
98112

99113

0 commit comments

Comments
 (0)