Skip to content

Commit 62105e0

Browse files
lazy_import and require_import
1 parent 163aab0 commit 62105e0

File tree

11 files changed

+51
-46
lines changed

11 files changed

+51
-46
lines changed

src/MLDatasets.jl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module MLDatasets
33
using FixedPointNumbers
44
using SparseArrays
55
using Tables
6-
using Glob
6+
# using Glob
77
# using DataFrames
88
# import ImageCore
99
using DataDeps
@@ -23,10 +23,7 @@ using FileIO
2323
using DelimitedFiles: readdlm
2424
##########
2525

26-
export getobs, numobs
27-
export convert2image
28-
29-
26+
export getobs, numobs # From MLUtils.jl
3027

3128
include("abstract_datasets.jl")
3229
# export AbstractDataset,

src/datasets/misc/boston_housing.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function BostonHousing(; as_df = true, dir = nothing)
7878
@assert dir === nothing "custom `dir` is not supported at the moment."
7979
path = joinpath(@__DIR__, "..", "..", "..", "data", "boston_housing.csv")
8080
df = read_csv(path)
81-
DFs = checked_import(idDataFrames)
81+
DFs = require_import(:DataFrames)
8282
features = df[!, DFs.Not(:MEDV)]
8383
targets = df[!, [:MEDV]]
8484

src/datasets/misc/iris.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ end
8080
function Iris(; dir = nothing, as_df = true)
8181
path = datafile("Iris", "iris.data", dir)
8282
df = read_csv(path, header=0)
83-
DFs = checked_import(idDataFrames)
83+
DFs = require_import(:DataFrames)
8484
DFs.rename!(df, ["sepallength", "sepalwidth", "petallength", "petalwidth", "class"])
8585

8686
features = df[!, DFs.Not(:class)]

src/datasets/misc/mutagenesis.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function Mutagenesis(split::Symbol; dir=nothing)
8585
metadata = read_json(metadata_path)
8686
labelkey = metadata["label"]
8787
targets = map(i -> i[labelkey], samples)
88-
features = map(x->delete!(copy(x), Symbol(labelkey)), samples)
88+
features = map(x -> delete!(copy(x), Symbol(labelkey)), samples)
8989
val_num = metadata["val_samples"]
9090
test_num = metadata["test_samples"]
9191
train_idxs = 1:length(samples)-val_num-test_num

src/datasets/misc/titanic.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function Titanic(; as_df = true, dir = nothing)
6262
@assert dir === nothing "custom `dir` is not supported at the moment."
6363
path = joinpath(@__DIR__, "..", "..", "..", "data", "titanic.csv")
6464
df = read_csv(path)
65-
DFs = checked_import(idDataFrames)
65+
DFs = require_import(:DataFrames)
6666

6767
features = df[!, DFs.Not(:Survived)]
6868
targets = df[!, [:Survived]]

src/datasets/vision/cifar10.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ convert2image(::Type{<:CIFAR10}, x::AbstractArray{<:Integer}) =
165165
function convert2image(::Type{<:CIFAR10}, x::AbstractArray{T,N}) where {T,N}
166166
@assert N == 3 || N == 4
167167
x = permutedims(x, (3, 2, 1, 4:N...))
168-
checked_import(idImageCore).colorview(RGB, x)
169-
# return ImageCore.colorview(RGB, x)
168+
ImageCore = lazy_import(:ImageCore)
169+
return ImageCore.colorview(RGB, x)
170170
end
171171

172172

src/datasets/vision/mnist.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ convert2image(::Type{<:MNIST}, x::AbstractArray{<:Integer}) =
141141
function convert2image(::Type{<:MNIST}, x::AbstractArray{T,N}) where {T,N}
142142
@assert N == 2 || N == 3
143143
x = permutedims(x, (2, 1, 3:N...))
144-
checked_import(idImageCore).colorview(Gray, x)
144+
ImageCore = lazy_import(:ImageCore)
145+
return ImageCore.colorview(Gray, x)
145146
end
146147

147148
# DEPRECATED INTERFACE, REMOVE IN v0.7 (or 0.6.x)

src/imports.jl

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1+
# Check that packages like JSON3 are already imported by the user.
2+
3+
function require_import(m::Symbol)
4+
pkgid = Base.identify_package(string(m))
5+
if Base.root_module_exists(pkgid)
6+
return Base.root_module(pkgid)
7+
else
8+
error("Add `import $m` or `using $m` to your code to unlock this functionality.")
9+
end
10+
end
111

2-
const idDataFrames = Base.PkgId(Base.UUID("a93c6f00-e57d-5684-b7b6-d8193f3e46c0"), "DataFrames")
3-
const idCSV = Base.PkgId(Base.UUID("336ed68f-0bac-5ca0-87d4-7b16caf5d00b"), "CSV")
4-
const idImageCore = Base.PkgId(Base.UUID("a09fc81d-aa75-5fe9-8630-4744c3626534"), "ImageCore")
5-
const idPickle = Base.PkgId(Base.UUID("fbb45041-c46e-462f-888f-7c521cafbc2c"), "Pickle")
6-
const idHDF5 = Base.PkgId(Base.UUID("f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"), "HDF5")
7-
const idMAT = Base.PkgId(Base.UUID("23992714-dd62-5051-b70f-ba57cb901cac"), "MAT")
8-
const idJSON3 = Base.PkgId(Base.UUID("0f8b85d8-7281-11e9-16c2-39a750bddbf1"), "JSON3")
9-
# ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
10-
# JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
1112

1213
const load_locker = Threads.ReentrantLock()
1314

14-
function checked_import(pkgid)
15+
function lazy_import(m::Symbol)
16+
pkgid = Base.identify_package(string(m))
1517
mod = if Base.root_module_exists(pkgid)
16-
Base.root_module(pkgid)
17-
else
18-
lock(load_locker) do
19-
Base.require(pkgid)
20-
end
21-
end
18+
Base.root_module(pkgid)
19+
else
20+
lock(load_locker) do
21+
Base.require(pkgid)
22+
end
23+
end
2224

2325
return ImportedModule(mod)
2426
end
@@ -36,4 +38,4 @@ function Base.getproperty(m::ImportedModule, s::Symbol)
3638
end
3739
return f
3840
end
39-
end
41+
end

src/io.jl

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# IMPORTS
2+
# Use `lazy_import(:SomePkg)` whenever the returned types are "native" types,
3+
# i.e. not defined in SomePkg itself, otherwise use `require_import(:SomePkg)`.
4+
5+
16
function read_csv(path; kws...)
27
return read_csv_asdf(path; kws...)
38
end
@@ -8,34 +13,39 @@ end
813
# end
914

1015
function read_csv(path, sink::Type{A}; kws...) where A <: AbstractMatrix
11-
A(read_csv(path; kws...))
16+
return A(read_csv(path; kws...))
1217
end
1318

1419
function read_csv_asdf(path; kws...)
15-
DF = checked_import(idDataFrames).mod.DataFrame
16-
return checked_import(idCSV).read(path, DF; kws...)
20+
DataFrames = require_import(:DataFrames)
21+
CSV = lazy_import(:CSV)
22+
return CSV.read(path, DataFrames.DataFrame; kws...)
1723
end
1824

1925
function read_npy(path)
20-
return FileIO.load(File{format"NPY"}(path))
26+
return FileIO.load(File{format"NPY"}(path)) # FileIO does lazy import of NPZ.jl
2127
end
2228

2329
function read_npz(path)
24-
return FileIO.load(File{format"NPZ"}(path))
30+
return FileIO.load(File{format"NPZ"}(path)) # FileIO does lazy import of NPZ.jl
2531
end
2632

2733
function read_pytorch(path)
28-
Base.invokelatest(checked_import(idPickle).mod.Torch.THload, path)
34+
Pickle = lazy_import(:Pickle)
35+
return Pickle.Torch.THload(path)
2936
end
3037

3138
function read_pickle(path)
32-
checked_import(idPickle).npyload(path)
39+
Pickle = lazy_import(:Pickle)
40+
return Pickle.npyload(path)
3341
end
3442

3543
function read_mat(path)
36-
checked_import(idMAT).matread(path)
44+
MAT = lazy_import(:MAT)
45+
return MAT.matread(path)
3746
end
3847

3948
function read_json(path)
40-
Base.invokelatest(open, checked_import(idJSON3).mod.read, path)
49+
JSON3 = require_import(:JSON3)
50+
return open(JSON3.read, path)
4151
end

test/datasets/misc.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ end
6363
@test val_x isa Vector{<:Dict}
6464

6565
x, y = dtrain[1:2]
66-
@show typeof(x) typeof(y)
6766
@test x isa Vector{Dict{Symbol, Any}}
6867
@test length(x) == 2
6968
@test y isa Vector{Int}

0 commit comments

Comments
 (0)