Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added save compatible version of filenames, arrays and images function #30

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3121308
added process function
siddharthlal25 Jul 7, 2020
b2a69d4
minor fix to signatures
siddharthlal25 Jul 7, 2020
022f9b7
minor doc fix
siddharthlal25 Jul 7, 2020
1b2c4a0
added basic tests
siddharthlal25 Jul 7, 2020
b4fde15
added saving tests
siddharthlal25 Jul 7, 2020
2c4a4aa
removed generated_data folder from gitignore
siddharthlal25 Jul 7, 2020
fff6af7
fixed test issue
siddharthlal25 Jul 7, 2020
404656c
fixed file naming bug and added name checking tests
siddharthlal25 Jul 7, 2020
28d732f
modified function to close file after operations
siddharthlal25 Jul 8, 2020
255efa2
modified docs
siddharthlal25 Jul 8, 2020
0ed0b73
modified constructors and added tests
siddharthlal25 Jul 12, 2020
27b614d
added save compatible version of filenames
siddharthlal25 Jul 12, 2020
811fb14
fixed docs of `fitscollection`
siddharthlal25 Jul 13, 2020
7b0a157
switched to `write_fits` and `generate_filename`
siddharthlal25 Jul 13, 2020
cb2fa43
switched from df to collection
siddharthlal25 Jul 13, 2020
ef93510
changed names of internal variables
siddharthlal25 Jul 13, 2020
3e862d5
Merge branch 'devel' of https://github.com/siddharthlal25/CCDReductio…
siddharthlal25 Jul 13, 2020
aeb6090
modified API of mapping version and minor change in tests
siddharthlal25 Jul 15, 2020
1d34a34
switched from `write_fits` to `write_data`
siddharthlal25 Jul 15, 2020
4066765
modified `generate_filename`
siddharthlal25 Jul 15, 2020
37c000f
added a minor test
siddharthlal25 Jul 15, 2020
ca21271
modified docs
siddharthlal25 Jul 15, 2020
ef1881f
minor change in tests
siddharthlal25 Jul 15, 2020
c095b6e
remove data generated after testing
siddharthlal25 Jul 16, 2020
8be44c8
fixed issue of closing files in iterative version of `images`
siddharthlal25 Jul 16, 2020
6064f26
minor test fix
siddharthlal25 Jul 16, 2020
79e97ac
closed all open handles during testing
siddharthlal25 Jul 16, 2020
ef0b18d
added documentation for mapping version of functions
siddharthlal25 Jul 16, 2020
ab98374
added docs for iterative version of functions
siddharthlal25 Jul 17, 2020
0207990
doc fix
siddharthlal25 Jul 17, 2020
a7dd75f
added docs and minor fix
siddharthlal25 Jul 17, 2020
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
3 changes: 2 additions & 1 deletion src/CCDReduction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export subtract_bias,
fitscollection,
arrays,
filenames,
images
images,
process

include("methods.jl")
include("fits.jl")
Expand Down
53 changes: 53 additions & 0 deletions src/collection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ end

parse_name(filename, ext, ::Val{true}) = filename


# transposes the data and saves it, the reason is same as that for getdata
function setdata(file_path, data)
siddharthlal25 marked this conversation as resolved.
Show resolved Hide resolved
d = ndims(data)
transposed_data = permutedims(data, d:-1:1)
FITS(file_path, "w") do fh
write(fh, transposed_data)
end
end

#---------------------------------------------------------------------------------------
@doc raw"""
fitscollection(dir; recursive=true, abspath=true, keepext=true, ext=r"fits(\.tar\.gz)?", exclude=nothing, exclude_dir=nothing, exclude_key = ("", "HISTORY"))
siddharthlal25 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -142,3 +152,46 @@ function images end
@yield FITS(row.path)[row.hdu]
end
end


"""
process(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.

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`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will definitely want to show some examples with this, I'm not sure whether it makes more sense to copy-paste three nearly identical examples in the docstrings or whether to relagate that to the online documentation. Perhaps both?

Another way to get around this is to put all of the filename-changing stuff into the docstring for make_file and then show the CCDReduction.make_file docstring in the docs and reference it. This removes the repitition and keeps all the relevant info in docstrings, but at the cost of having to search around a little more to get the information you need.

"""
function process(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(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
siddharthlal25 marked this conversation as resolved.
Show resolved Hide resolved
return final_value
siddharthlal25 marked this conversation as resolved.
Show resolved Hide resolved
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)
siddharthlal25 marked this conversation as resolved.
Show resolved Hide resolved
# removing the extension from filename
modified_name = parse_name(filename, "." * ext, Val(false))

if !(save_prefix isa Nothing)
modified_name = string(save_prefix, save_delim, modified_name)
end
if !(save_suffix isa Nothing)
modified_name = string(modified_name, save_delim, save_suffix)
end

file_path = joinpath(save_location, modified_name * ".fits")

# writing file
setdata(file_path, data)
end
25 changes: 25 additions & 0 deletions test/collection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,31 @@ end
end
end

@testset "process" begin
# setting initial data
dir = joinpath(@__DIR__, "data")
savedir = @__DIR__
df = fitscollection(dir)

final = process(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 "helper" begin
@test parse_name("abc.fits", "."*"fits", Val(true)) == "abc.fits"
@test parse_name("abc.fits.tar.gz", "."*"fits.tar.gz", Val(false)) == "abc"
Expand Down