Skip to content

Commit

Permalink
up TargetedEstimation dep
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierlabayle committed Jul 25, 2024
1 parent 9f2103a commit 7b2ffed
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 57 deletions.
4 changes: 2 additions & 2 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2379,8 +2379,8 @@ version = "1.10.0"

[[deps.TargetedEstimation]]
deps = ["ArgParse", "Arrow", "CSV", "CategoricalArrays", "Combinatorics", "Configurations", "DataFrames", "EvoTrees", "GLMNet", "HDF5", "JLD2", "JSON", "MKL", "MLJ", "MLJBase", "MLJLinearModels", "MLJModelInterface", "MLJModels", "MLJXGBoostInterface", "Mmap", "MultipleTesting", "Optim", "PackageCompiler", "Random", "Serialization", "TMLE", "Tables", "YAML"]
git-tree-sha1 = "98ae29ef85f5bbeb088b1003c58ced0e0e6104d3"
repo-rev = "acc1de14e05517b6b5e1fe441526f097edb8eb19"
git-tree-sha1 = "9cf3987b177a22ca374b9b11ab7a89de5bdd56b3"
repo-rev = "f4620ef1565f71019fa539e746ff109727e6671c"
repo-url = "https://github.com/TARGENE/TargetedEstimation.jl"
uuid = "2573d147-4098-46ba-9db2-8608d210ccac"
version = "0.9.0"
Expand Down
6 changes: 3 additions & 3 deletions src/cli.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ function cli_settings()
end

@add_arg_table s["summary-plots"] begin
"results-prefix"
help = "Prefix to result files."
"results-file"
help = "Path to the results file."
required = true

"--outprefix"
Expand Down Expand Up @@ -201,7 +201,7 @@ function julia_main()::Cint
)
elseif cmd == "summary-plots"
summary_plots(
cmd_settings["results-prefix"],
cmd_settings["results-file"],
outprefix=cmd_settings["outprefix"],
verbosity=cmd_settings["verbosity"],
)
Expand Down
10 changes: 7 additions & 3 deletions src/inputs_from_config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,20 @@ function estimands_from_flat_list(estimands_configs, dataset, variants, outcomes
return estimands
end

function treatments_from_variant(variant::Symbol, dataset)
variant_levels = sort(levels(dataset[!, variant], skipmissing=true))
return NamedTuple{(variant,)}([variant_levels])
end

function gwas_estimands(dataset, variants, outcomes, confounders;
outcome_extra_covariates=[],
positivity_constraint=0.,
verbosity=0,
)
estimands = []
verbosity > 0 && @info(string("Generating GWAS estimands."))
for v in variants
variant_levels = sort(levels(dataset[!, v], skipmissing=true))
treatments = NamedTuple{(Symbol(v),)}([variant_levels])
for variant in Symbol.(variants)
treatments = treatments_from_variant(variant, dataset)
try_append_new_estimands!(
estimands,
dataset,
Expand Down
21 changes: 4 additions & 17 deletions src/plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,9 @@ log10_uniform_quantiles(n) = -log10.(collect(LinRange(0., 1., n + 1))[2:end])

log10_beta_quantiles(n, alpha) = -log10.([quantile(Beta(k, n + 1 k), alpha) for k in 1:n])

function read_results_file(file)
jldopen(file) do io
return reduce(vcat, (io[key] for key in keys(io)))
end
end

function read_results_files(prefix)
directory_, prefix_ = splitdir(prefix)
directory = directory_ == "" ? "." : directory_
matching_files = [joinpath(directory_, file) for file in readdir(directory) if startswith(file, prefix_)]
reduce(vcat, (read_results_file(file) for file in matching_files))
end

function load_results(file_or_prefix; verbosity=0)
function load_results(resultsfile; verbosity=0)
verbosity > 0 && @info "Loading results."
results = isfile(file_or_prefix) ? read_results_file(file_or_prefix) : read_results_files(file_or_prefix)
results = jldopen(io -> io["results"], resultsfile)
estimators = collect(key for key keys(first(results)) if key !== :SAMPLE_IDS)
results_df = DataFrame([[r[id] for r in results] for id in 1:length(estimators)], estimators)
for estimator in estimators
Expand Down Expand Up @@ -57,10 +44,10 @@ function qqplot(results, outprefix)
return fig
end

function summary_plots(results_prefix;
function summary_plots(results_file;
outprefix="final",
verbosity=0
)
results = load_results(results_prefix; verbosity=verbosity)
results = load_results(results_file; verbosity=verbosity)
qqplot(results, outprefix)
end
1 change: 1 addition & 0 deletions test/inputs_from_gwas_config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,5 @@ end
end

end

true
17 changes: 9 additions & 8 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ using TargeneCore
using Test

TESTDIR = joinpath(pkgdir(TargeneCore), "test")

@test include(joinpath(TESTDIR, "utils.jl"))
@test include(joinpath(TESTDIR, "dataset.jl"))
@test include(joinpath(TESTDIR, "plots.jl"))
@test include(joinpath(TESTDIR, "confounders.jl"))
@test include(joinpath(TESTDIR, "inputs_from_estimands.jl"))
@test include(joinpath(TESTDIR, "inputs_from_config.jl"))
@test include(joinpath(TESTDIR, "inputs_from_gwas_config.jl"))
@testset "Test TargeneCore" begin
@test include(joinpath(TESTDIR, "utils.jl"))
@test include(joinpath(TESTDIR, "dataset.jl"))
@test include(joinpath(TESTDIR, "plots.jl"))
@test include(joinpath(TESTDIR, "confounders.jl"))
@test include(joinpath(TESTDIR, "inputs_from_estimands.jl"))
@test include(joinpath(TESTDIR, "inputs_from_config.jl"))
@test include(joinpath(TESTDIR, "inputs_from_gwas_config.jl"))
end
28 changes: 4 additions & 24 deletions test/testutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,36 +109,16 @@ end

function save(estimates; prefix="tmle_output")
outputs = TargetedEstimation.Outputs(
json=TargetedEstimation.JSONOutput(filename=prefix*".json"),
jls=TargetedEstimation.JLSOutput(filename=prefix*".jls"),
hdf5=TargetedEstimation.HDF5Output(filename=prefix*".hdf5")
json=prefix*".json",
jls=prefix*".jls",
hdf5=prefix*".hdf5"
)
TargetedEstimation.initialize(outputs)
batches = collect(Iterators.partition(estimates, 2))
nbatches = length(batches)
for (batchid, batch) in enumerate(batches)
# Append JSON Output
TargetedEstimation.update_file(outputs.json, batch; finalize=nbatches==batchid)
# Append JLS Output
TargetedEstimation.update_file(outputs.jls, batch)
# Append HDF5 Output
TargetedEstimation.update_file(outputs.hdf5, batch)
end
TargetedEstimation.write(outputs, estimates)
end

make_fake_outputs(estimates_generator=make_estimates; prefix="tmle_output") =
save(estimates_generator(); prefix=prefix)

function clean(;prefix="tmle_output")
dir_, prefix_ = splitdir(prefix)
dir = dir_ == "" ? "." : dir_
for filename in readdir(dir)
if startswith(filename, prefix_)
rm(joinpath(dir_, filename))
end
end
end

### Fixtures for inputs_from_estimands

function make_estimands_configuration()
Expand Down

0 comments on commit 7b2ffed

Please sign in to comment.