Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/TagBot.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
name: TagBot
on:
schedule:
- cron: 0 0 * * *
issue_comment:
types:
- created
workflow_dispatch:
jobs:
TagBot:
if: github.event_name == 'workflow_dispatch' || github.actor == 'JuliaTagBot'
runs-on: ubuntu-latest
steps:
- uses: JuliaRegistries/TagBot@v1
Expand Down
47 changes: 47 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: CI
on:
pull_request:
branches:
- master
push:
branches:
- master
tags: '*'
jobs:
test:
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
version:
- '0.7'
- '1.0'
- '1'
Comment thread
jishnub marked this conversation as resolved.
- 'nightly'
os:
- ubuntu-latest
Comment thread
jishnub marked this conversation as resolved.
Outdated
arch:
- x64
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: actions/cache@v1
env:
cache-name: cache-artifacts
with:
path: ~/.julia/artifacts
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
restore-keys: |
${{ runner.os }}-test-${{ env.cache-name }}-
${{ runner.os }}-test-
${{ runner.os }}-
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v1
with:
file: lcov.info
24 changes: 24 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Documentation

on:
push:
branches:
- master
tags: '*'
pull_request:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@latest
with:
version: '1.4'
- name: Install dependencies
run: julia --project=docs/ -e 'using Pkg; Pkg.develop(PackageSpec(path=pwd())); Pkg.instantiate()'
- name: Build and deploy
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # For authentication with GitHub Actions token
DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }} # For authentication with SSH deploy key
run: julia --project=docs/ docs/make.jl
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
docs/build
Manifest.toml
docs/Manifest.toml
18 changes: 0 additions & 18 deletions .travis.yml

This file was deleted.

6 changes: 6 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
NPZ = "15e1cf62-19b3-5cfa-8e77-841668bca605"

[compat]
Documenter = "0.25"
Comment thread
jishnub marked this conversation as resolved.
Outdated
22 changes: 22 additions & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using NPZ
using Documenter

DocMeta.setdocmeta!(NPZ, :DocTestSetup, :(using NPZ); recursive=true)

makedocs(;
modules=[NPZ],
repo="https://github.com/fhs/NPZ.jl/blob/{commit}{path}#L{line}",
sitename="NPZ",
format=Documenter.HTML(;
prettyurls=get(ENV, "CI", "false") == "true",
canonical="https://fhs.github.io/NPZ.jl",
assets=String[],
),
pages=[
"Reference" => "index.md",
],
)

deploydocs(;
repo="github.com/fhs/NPZ.jl",
)
9 changes: 9 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
```@meta
CurrentModule = NPZ
```

# NPZ.jl

```@autodocs
Modules = [NPZ]
```
113 changes: 90 additions & 23 deletions src/NPZ.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,36 +235,64 @@ function samestart(a::AbstractVector, b::AbstractVector)
length(a) >= nb && view(a, 1:nb) == b
end

function npzread(filename::AbstractString)
function _maybetrimext(name::AbstractString)
fname, ext = splitext(name)
if ext == ".npy"
name = fname
end
name
end

"""
npzread(filename::AbstractString, [vars])

Read an variable or a collection of variables from `filename`.
Comment thread
jishnub marked this conversation as resolved.
Outdated
The input needs to be either an `npy` or an `npz` file.
The optional argument `vars` is used only for `npz` files.
If it is specified, only the matching variables are read in from the file.

# Example

```julia
julia> npzwrite("temp.npz", Dict("x" => ones(3), "y" => 3))

julia> npzread("temp.npz") # Reads all variables
Dict{String,Any} with 2 entries:
"x" => [1.0, 1.0, 1.0]
"y" => 3

julia> npzread("temp.npz", ["x"]) # Reads only "x"
Dict{String,Array{Float64,1}} with 1 entry:
"x" => [1.0, 1.0, 1.0]
```
"""
function npzread(filename::AbstractString, vars...)
# Detect if the file is a numpy npy array file or a npz/zip file.
f = open(filename)
@compat b = read!(f, Vector{UInt8}(undef, MaxMagicLen))

if samestart(b, ZIPMagic)
close(f)
f = ZipFile.Reader(filename)
data = npzread(f)
close(f)
return data
end
if samestart(b, NPYMagic)
fz = ZipFile.Reader(filename)
data = npzread(fz, vars...)
close(fz)
elseif samestart(b, NPYMagic)
seekstart(f)
data = npzreadarray(f)
else
close(f)
return data
error("not a NPY or NPZ/Zip file: $filename")
end
error("not a NPY or NPZ/Zip file: $filename")

close(f)
return data
end

function npzread(dir::ZipFile.Reader)
vars = Dict{String,Any}()
for f in dir.files
name = f.name
if endswith(name, ".npy")
name = name[1:end-4]
end
vars[name] = npzreadarray(f)
end
vars
function npzread(dir::ZipFile.Reader,
vars = map(f -> _maybetrimext(f.name), dir.files))

Dict(_maybetrimext(f.name) => npzreadarray(f)
for f in dir.files
if f.name in vars || _maybetrimext(f.name) in vars)
end

function npzwritearray(
Expand Down Expand Up @@ -301,12 +329,51 @@ end
function npzwritearray(f::IO, x::T) where T<:Number
npzwritearray(f, reinterpret(UInt8, [x]), T, Int[])
end

"""
npzwrite(filename::AbstractString, x)

Write the variable `x` to the `npy` file `filename`.
Unlike `numpy`, the extension `.npy` is not appened to `filename`.

!!! warn "Warning"
Any existing file with the same name will be overwritten.

# Examples
```julia
julia> npzwrite("abc.npy", zeros(3))

julia> npzread("abc.npy")
3-element Array{Float64,1}:
0.0
0.0
0.0
```
"""
function npzwrite(filename::AbstractString, x)
f = open(filename, "w")
npzwritearray(f, x)
close(f)
open(filename, "w") do f
npzwritearray(f, x)
end
end

"""
npzwrite(filename::AbstractString, vars::Dict{<:AbstractString})

Write the variables in `vars` to an `npz` file named `filename`.
Unlike `numpy`, the extension `.npz` is not appened to `filename`.

!!! warn "Warning"
Any existing file with the same name will be overwritten.

```julia
julia> npzwrite("temp.npz", Dict("x" => ones(3), "y" => 3))

julia> npzread("temp.npz")
Dict{String,Any} with 2 entries:
"x" => [1.0, 1.0, 1.0]
"y" => 3
```
"""
function npzwrite(filename::AbstractString, vars::Dict{<:AbstractString})
dir = ZipFile.Writer(filename)
for (k, v) in vars
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ npzwrite(dictfilename, testdict)
dictoutput = npzread(dictfilename)
for (k, v) in testdict
@test v == dictoutput[k]
d = npzread(dictfilename, [k])
@test typeof(d) == Dict{String,typeof(v)}
val_read = d[k]
@test typeof(val_read) == typeof(v)
@test val_read == v
end
# Write and then read NPY files for each array
for x in TestArrays
Expand Down