Skip to content

Commit ebc6938

Browse files
committed
Get rid of cruft from FastAI.jl
1 parent 950e111 commit ebc6938

File tree

5 files changed

+18
-44
lines changed

5 files changed

+18
-44
lines changed

src/containers/cacheddataset.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ make_cache(source, cacheidx) = getobs(source, cacheidx)
88

99
"""
1010
CachedDataset(source, cachesize = numbobs(source))
11+
CachedDataset(source, cacheidx = 1:numbobs(source))
1112
CachedDataset(source, cacheidx, cache)
1213
1314
Wrap a `source` data container and cache `cachesize` samples in memory.
1415
This can be useful for improving read speeds when `source` is a lazy data container,
1516
but your system memory is large enough to store a sizeable chunk of it.
1617
1718
By default the observation indices `1:cachesize` are cached.
18-
You can manually pass in a `cache` and set of `cacheidx` as well.
19+
You can manually pass in a set of `cacheidx` as well.
1920
2021
See also [`make_cache`](@ref) for customizing the default cache creation for `source`.
2122
"""
@@ -25,11 +26,9 @@ struct CachedDataset{T, S}
2526
cache::S
2627
end
2728

28-
function CachedDataset(source, cachesize::Int = numobs(source))
29-
cacheidx = 1:cachesize
30-
29+
CachedDataset(source, cacheidx::AbstractVector{<:Integer} = 1:numobs(source)) =
3130
CachedDataset(source, collect(cacheidx), make_cache(source, cacheidx))
32-
end
31+
CachedDataset(source, cachesize::Int = numobs(source)) = CachedDataset(source, 1:cachesize)
3332

3433
function Base.getindex(dataset::CachedDataset, i::Integer)
3534
_i = findfirst(==(i), dataset.cacheidx)

src/containers/filedataset.jl

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
matches(re::Regex) = f -> matches(re, f)
2-
matches(re::Regex, f) = !isnothing(match(re, f))
3-
const RE_IMAGEFILE = r".*\.(gif|jpe?g|tiff?|png|webp|bmp)$"i
4-
isimagefile(f) = matches(RE_IMAGEFILE, f)
5-
61
"""
72
rglob(filepattern, dir = pwd(), depth = 4)
83
@@ -15,43 +10,26 @@ function rglob(filepattern = "*", dir = pwd(), depth = 4)
1510
end
1611

1712
"""
18-
loadfile(file)
19-
20-
Load a file from disk into the appropriate format.
21-
"""
22-
function loadfile(file::String)
23-
if isimagefile(file)
24-
# faster image loading
25-
return FileIO.load(file, view = true)
26-
elseif endswith(file, ".csv")
27-
return DataFrame(CSV.File(file))
28-
else
29-
return FileIO.load(file)
30-
end
31-
end
32-
loadfile(file::AbstractPath) = loadfile(string(file))
33-
34-
"""
35-
FileDataset([loadfn = loadfile,] paths)
36-
FileDataset([loadfn = loadfile,] dir, pattern = "*", depth = 4)
13+
FileDataset([loadfn = FileIO.load,] paths)
14+
FileDataset([loadfn = FileIO.load,] dir, pattern = "*", depth = 4)
3715
3816
Wrap a set of file `paths` as a dataset (traversed in the same order as `paths`).
3917
Alternatively, specify a `dir` and collect all paths that match a glob `pattern`
4018
(recursively globbing by `depth`). The glob order determines the traversal order.
4119
"""
42-
struct FileDataset{F, T<:Union{AbstractPath, AbstractString}} <: AbstractDataContainer
20+
struct FileDataset{F, T<:AbstractString} <: AbstractDataContainer
4321
loadfn::F
4422
paths::Vector{T}
4523
end
4624

47-
FileDataset(paths) = FileDataset(loadfile, paths)
25+
FileDataset(paths) = FileDataset(FileIO.load, paths)
4826
FileDataset(loadfn,
49-
dir::Union{AbstractPath, AbstractString},
27+
dir::AbstractString,
5028
pattern::AbstractString = "*",
5129
depth = 4) = FileDataset(loadfn, rglob(pattern, string(dir), depth))
52-
FileDataset(dir::Union{AbstractPath, AbstractString}, pattern::AbstractString = "*", depth = 4) =
53-
FileDataset(loadfile, dir, pattern, depth)
30+
FileDataset(dir::AbstractString, pattern::AbstractString = "*", depth = 4) =
31+
FileDataset(FileIO.load, dir, pattern, depth)
5432

55-
Base.getindex(dataset::FileDataset, i::Integer) = loadfile(dataset.paths[i])
33+
Base.getindex(dataset::FileDataset, i::Integer) = dataset.loadfn(dataset.paths[i])
5634
Base.getindex(dataset::FileDataset, is::AbstractVector) = map(Base.Fix1(getobs, dataset), is)
5735
Base.length(dataset::FileDataset) = length(dataset.paths)

src/containers/hdf5dataset.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function _check_hdf5_shapes(shapes)
55
end
66

77
"""
8-
HDF5Dataset(file::Union{AbstractString, AbstractPath}, paths)
8+
HDF5Dataset(file::AbstractString, paths)
99
HDF5Dataset(fid::HDF5.File, paths::Union{HDF5.Dataset, Vector{HDF5.Dataset}})
1010
HDF5Dataset(fid::HDF5.File, paths::Union{AbstractString, Vector{<:AbstractString}})
1111
HDF5Dataset(fid::HDF5.File, paths::Union{HDF5.Dataset, Vector{HDF5.Dataset}}, shapes)
@@ -38,8 +38,7 @@ HDF5Dataset(fid::HDF5.File, paths::Vector{HDF5.Dataset}) =
3838
HDF5Dataset(fid::HDF5.File, path::AbstractString) = HDF5Dataset(fid, fid[path])
3939
HDF5Dataset(fid::HDF5.File, paths::Vector{<:AbstractString}) =
4040
HDF5Dataset(fid, map(p -> fid[p], paths))
41-
HDF5Dataset(file::Union{AbstractString, AbstractPath}, paths) =
42-
HDF5Dataset(h5open(file, "r"), paths)
41+
HDF5Dataset(file::AbstractString, paths) = HDF5Dataset(h5open(file, "r"), paths)
4342

4443
_getobs_hdf5(dataset::HDF5.Dataset, ::Tuple{}, i) = read(dataset)
4544
function _getobs_hdf5(dataset::HDF5.Dataset, shape, i)

src/containers/jld2dataset.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
_check_jld2_nobs(nobs) = all(==(first(nobs)), nobs[2:end])
22

33
"""
4-
JLD2Dataset(file::Union{AbstractString, AbstractPath}, paths)
4+
JLD2Dataset(file::AbstractString, paths)
55
JLD2Dataset(fid::JLD2.JLDFile, paths::Union{String, Vector{String}})
66
77
Wrap several JLD2 datasets (`paths`) as a single dataset container.
@@ -25,8 +25,7 @@ struct JLD2Dataset{T<:JLD2.JLDFile, S<:Tuple} <: AbstractDataContainer
2525
end
2626

2727
JLD2Dataset(file::JLD2.JLDFile, path::String) = JLD2Dataset(file, (path,))
28-
JLD2Dataset(file::Union{AbstractString, AbstractPath}, paths) =
29-
JLD2Dataset(jldopen(file, "r"), paths)
28+
JLD2Dataset(file::AbstractString, paths) = JLD2Dataset(jldopen(file, "r"), paths)
3029

3130
Base.getindex(dataset::JLD2Dataset{<:JLD2.JLDFile, <:NTuple{1}}, i) = getobs(only(dataset.paths), i)
3231
Base.getindex(dataset::JLD2Dataset, i) = map(Base.Fix2(getobs, i), dataset.paths)

src/containers/tabledataset.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
TableDataset(table)
3-
TableDataset(path::Union{AbstractPath, AbstractString})
3+
TableDataset(path::AbstractString)
44
55
Wrap a Tables.jl-compatible `table` as a dataset container.
66
Alternatively, specify the `path` to a CSV file directly
@@ -19,8 +19,7 @@ struct TableDataset{T} <: AbstractDataContainer
1919
end
2020

2121
TableDataset(table::T) where {T} = TableDataset{T}(table)
22-
TableDataset(path::Union{AbstractPath, AbstractString}) =
23-
TableDataset(DataFrame(CSV.File(path)))
22+
TableDataset(path::AbstractPath) = TableDataset(DataFrame(CSV.File(path)))
2423

2524
# slow accesses based on Tables.jl
2625
_getobs_row(x, i) = first(Iterators.peel(Iterators.drop(x, i - 1)))

0 commit comments

Comments
 (0)