fhs / NPZ.jl

A Julia package that provides support for reading and writing Numpy .npy and .npz files
Other
117 stars 16 forks source link

Suggest to add string support #33

Open ascetic168 opened 5 years ago

ascetic168 commented 5 years ago

In Numpy, string array to npz file is legal. "np.savez_compressed" in Python works fine. ex: something like ["name1" "name2" "name3"]

So far, NPZ.jl can not read/write such kind of data. Would you like to add the support?

bjarthur commented 5 years ago

how can i help with adding string support?

bjarthur commented 4 years ago

seems non-trivial as numpy's dtype includes the length of the string (e.g. U18 for 18 characters of unicode) and so the dictionary used by NPZ.jl to convert to a julia type would have to be reworked. ugh.

bjarthur commented 4 years ago

in the meantime, reading what is supported helps my use case:

diff --git a/src/NPZ.jl b/src/NPZ.jl
index 39ccbc1..6babc7e 100644
--- a/src/NPZ.jl
+++ b/src/NPZ.jl
@@ -262,7 +262,11 @@ function npzread(dir::ZipFile.Reader)
         if endswith(name, ".npy")
             name = name[1:end-4]
         end
-        vars[name] = npzreadarray(f)
+        try
+            vars[name] = npzreadarray(f)
+        catch
+            @warn string("unable to read variable ",name)
+        end
     end
     vars
 end

if you like, i could work this up to include proper exceptions and submit a PR.