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

npzwrite with a numpy-like signature #41

Closed jishnub closed 3 years ago

jishnub commented 3 years ago

This PR adds a method to npzwrite that follows the signature of np.savez, so now this is allowed

julia> npzwrite("temp.npz", 3, 45, x = ones(3), y = zeros(2,2))

julia> npzread("temp.npz")
Dict{String,Any} with 4 entries:
  "arr_1" => 45
  "arr_0" => 3
  "x"     => [1.0, 1.0, 1.0]
  "y"     => [0.0 0.0; 0.0 0.0]
codecov-io commented 3 years ago

Codecov Report

Merging #41 (8f31d4a) into master (a714e9d) will increase coverage by 0.01%. The diff coverage is 91.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #41      +/-   ##
==========================================
+ Coverage   85.27%   85.29%   +0.01%     
==========================================
  Files           1        1              
  Lines         163      170       +7     
==========================================
+ Hits          139      145       +6     
- Misses         24       25       +1     
Impacted Files Coverage Δ
src/NPZ.jl 85.29% <91.66%> (+0.01%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update a714e9d...8f31d4a. Read the comment docs.

jishnub commented 3 years ago

7aef79ae807e0b2f71a1f75f1aaedf16020433d6 reduces allocations in writing arrays by avoiding a copy.

On master:

julia> a = zeros(1_000, 1_000)

julia> @btime npzwrite("temp.npz", $a);
  808.917 ms (33 allocations: 7.63 MiB)

after this PR:

julia> @btime npzwrite("temp.npz", $a);
  808.263 ms (31 allocations: 1.64 KiB)

This might be useful in working with large arrays.