You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is missing, but reasonably expectable, functionality. If you have a zarr array with a compound dtype (i.e., records) and any of the fields are object type (i.e., strings), then you cannot roundtrip the data even though JSON/msgpack/pickle are capable of converting the array.
Minimal, reproducible code sample, a copy-pastable example if possible
(without fill_value, this errors earlier sue to the use of np.zeros to guess a fill; with an appropriate fill_value=("", 0, 0.), it fails at array creation too)
Problem description
JSON and similar codecs store only the dtype.str, which is a "Vxx" in these cases, which means a suitable empty array cannot be made at load time.
The following fixes this for JSON, but looks ugly.
--- a/numcodecs/json.py+++ b/numcodecs/json.py@@ -1,3 +1,4 @@+import ast
import json as _json
import textwrap
@@ -56,14 +57,18 @@ class JSON(Codec):
def encode(self, buf):
buf = np.asarray(buf)
items = buf.tolist()
- items.append(buf.dtype.str)+ items.append(str(buf.dtype))
items.append(buf.shape)
return self._encoder.encode(items).encode(self._text_encoding)
def decode(self, buf, out=None):
items = self._decoder.decode(ensure_text(buf, self._text_encoding))
- dec = np.empty(items[-1], dtype=items[-2])- dec[:] = items[:-2]+ if "[" in items[-2]:+ dec = np.empty(items[-1], dtype=ast.literal_eval(items[-2]))+ dec[:] = [tuple(_) for _ in items[:-2]]+ else:+ dec = np.empty(items[-1], dtype=items[-2])+ dec[:] = items[:-2]
if out is not None:
np.copyto(out, dec)
Unfortunately not. I tried to apply the patch just now. The encode part is already changed from your diff, but the failing np.asarray(buf) is before the changed lines.
Activity
bnavigator commentedon Jan 12, 2023
Any update here?
martindurant commentedon Jan 12, 2023
None from me. I have a plausible fix, above, which could be added now, but I was hoping someone would come up with something cleaner.
bnavigator commentedon Jan 12, 2023
I am getting this from the test suite:
martindurant commentedon Jan 12, 2023
I don't know if my diff would fix that. If yes, let's include it! I wonder what changed that this is showing up now but not before.
bnavigator commentedon Jan 12, 2023
Unfortunately not. I tried to apply the patch just now. The
encodepart is already changed from your diff, but the failingnp.asarray(buf)is before the changed lines.bnavigator commentedon Jan 12, 2023
The difference is an updated numpy 1.24.1