Skip to content

Commit

Permalink
modified constructors and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthlal25 committed Jul 12, 2020
1 parent 255efa2 commit b4e20a1
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 7 deletions.
54 changes: 51 additions & 3 deletions src/collection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,15 @@ end


"""
process(f, df::DataFrame; path = nothing, save_prefix = nothing, save_suffix = nothing, save_delim = "_", ext = r"fits(\\.tar\\.gz)?"i)
images(f, df::DataFrame; path = nothing, save_prefix = nothing, save_suffix = nothing, save_delim = "_", ext = r"fits(\\.tar\\.gz)?"i)
Applies function `f` on all elements of data frame and saves it in FITS file.
Applies function `f` on all ImageHDUs present in data frame and saves it in FITS file.
If `path = nothing`, then save functionality does not execute. It returns an array of arrays which contains final returned values of function.
A suffix and prefix can be added to filename of newly created files by modifying `save_suffix` and `save_prefix`, `save_delim` is used as delimiter.
`ext` is the extension of files to be taken into consideration for applying function, by default it is set to `r"fits(\\.tar\\.gz)?"i`.
"""
function process(f, df::DataFrame; path = nothing, save_prefix = nothing, save_suffix = nothing, save_delim = "_", ext = r"fits(\.tar\.gz)?"i)
function images(f, df::DataFrame; path = nothing, save_prefix = nothing, save_suffix = nothing, save_delim = "_", ext = r"fits(\.tar\.gz)?"i)
final_value = Vector{Array}(undef, first(size(df)))
for (i,x) in enumerate(eachrow(df))
fh = FITS(x.path)
Expand All @@ -178,6 +178,54 @@ function process(f, df::DataFrame; path = nothing, save_prefix = nothing, save_s
return final_value
end

# will uncomment/remove after discussion
# """
# filenames(f, df::DataFrame; path = nothing, save_prefix = nothing, save_suffix = nothing, save_delim = "_", ext = r"fits(\\.tar\\.gz)?"i)
#
# Applies function `f` on all filenames present in data frame and saves it in FITS file.
#
# If `path = nothing`, then save functionality does not execute. It returns an array of arrays which contains final returned values of function.
# A suffix and prefix can be added to filename of newly created files by modifying `save_suffix` and `save_prefix`, `save_delim` is used as delimiter.
# `ext` is the extension of files to be taken into consideration for applying function, by default it is set to `r"fits(\\.tar\\.gz)?"i`.
# """
# function filenames(f, df::DataFrame; path = nothing, save_prefix = nothing, save_suffix = nothing, save_delim = "_", ext = r"fits(\.tar\.gz)?"i)
# final_value = Vector{Array}(undef, first(size(df)))
# for (i,x) in enumerate(eachrow(df))
# processed_value = f(x.path)
# final_value[i] = processed_value
# # if path is not nothing then we save
# if !(path isa Nothing)
# make_file(processed_value, x.name, path, save_prefix, save_suffix, save_delim, ext)
# end
# end
# return final_value
# end


"""
arrays(f, df::DataFrame; path = nothing, save_prefix = nothing, save_suffix = nothing, save_delim = "_", ext = r"fits(\\.tar\\.gz)?"i)
Applies function `f` on all image arrays present in data frame and saves it in FITS file.
If `path = nothing`, then save functionality does not execute. It returns an array of arrays which contains final returned values of function.
A suffix and prefix can be added to filename of newly created files by modifying `save_suffix` and `save_prefix`, `save_delim` is used as delimiter.
`ext` is the extension of files to be taken into consideration for applying function, by default it is set to `r"fits(\\.tar\\.gz)?"i`.
"""
function arrays(f, df::DataFrame; path = nothing, save_prefix = nothing, save_suffix = nothing, save_delim = "_", ext = r"fits(\.tar\.gz)?"i)
final_value = Vector{Array}(undef, first(size(df)))
for (i,x) in enumerate(eachrow(df))
fh = FITS(x.path)
processed_value = f(getdata(fh[x.hdu]))
close(fh)
final_value[i] = processed_value
# if path is not nothing then we save
if !(path isa Nothing)
make_file(processed_value, x.name, path, save_prefix, save_suffix, save_delim, ext)
end
end
return final_value
end

# utility function to generate file name and then save the given data
function make_file(data, filename, save_location, save_prefix, save_suffix, save_delim, ext)
# removing the extension from filename
Expand Down
31 changes: 27 additions & 4 deletions test/collection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,30 @@ end
@test arr1 == arr2
end

@testset "saving-arrays" begin
dir = joinpath(@__DIR__, "data")
savedir = @__DIR__
df = fitscollection(dir; recursive = false)

final = arrays(df; path = savedir, save_prefix = "test1", save_suffix = "test2") do img
trim(img, (:, 1040:1059))
end

# testing function outputs
@test final[1] == trim(M35070V[1], (:, 1040:1059))
@test final[2] == trim(M6707HH[1], (:, 1040:1059))

df1 = fitscollection(savedir; recursive = false)

# testing saved data
@test final[1] == getdata(FITS(df1[1, :path])[df1[1, :hdu]])
@test final[2] == getdata(FITS(df1[2, :path])[df1[2, :hdu]])

# testing saved filenames
@test df1[1, :name] == "test1_M35070V_test2.fits"
@test df1[2, :name] == "test1_M6707HH_test2.fits"
end

@testset "filename-generators" begin
# setting initial data
dir = joinpath(@__DIR__, "data")
Expand Down Expand Up @@ -87,13 +111,12 @@ end
end
end

@testset "process" begin
# setting initial data
@testset "saving-image" begin
dir = joinpath(@__DIR__, "data")
savedir = @__DIR__
df = fitscollection(dir)
df = fitscollection(dir; recursive = false)

final = process(df; path = savedir, save_prefix = "test1", save_suffix = "test2") do img
final = images(df; path = savedir, save_prefix = "test1", save_suffix = "test2") do img
trim(img, (:, 1040:1059))
end

Expand Down

0 comments on commit b4e20a1

Please sign in to comment.