From 016cc2806d4693a51489e82ed804e712eae1d36f Mon Sep 17 00:00:00 2001 From: Nils Gudat Date: Wed, 17 Jan 2024 11:04:59 +0000 Subject: [PATCH] v0.4 - add Aqua, fix tests, limit Julia compat --- .gitignore | 2 ++ Project.toml | 6 ++++-- README.md | 4 +++- src/TreatmentPanel.jl | 23 +++++++++++++++++++++-- src/TreatmentPanels.jl | 5 +++-- test/Aqua.jl | 12 ++++++++++++ test/Project.toml | 14 ++++++++++++++ test/runtests.jl | 2 ++ 8 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 test/Aqua.jl create mode 100644 test/Project.toml diff --git a/.gitignore b/.gitignore index b067edd..8a945dd 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /Manifest.toml +.vscode/settings.json +test/Manifest.toml diff --git a/Project.toml b/Project.toml index c73092f..71438af 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "TreatmentPanels" uuid = "7885c543-3ac4-48a3-abed-7a36d7ddb69f" authors = ["Nils and contributors"] -version = "0.3.0" +version = "0.4.0" [deps] DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" @@ -11,9 +11,11 @@ RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01" [compat] DataFrames = "1" +Dates = "1" RecipesBase = "1" Parameters = "0.12" -julia = "1" +Test = "1" +julia = "1.6" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/README.md b/README.md index d43e394..e8fa95a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ [![CI](https://github.com/nilshg/TreatmentPanels.jl/actions/workflows/ci.yml/badge.svg)](https://github.com/nilshg/TreatmentPanels.jl/actions/workflows/ci.yml) +[![Aqua QA](https://raw.githubusercontent.com/JuliaTesting/Aqua.jl/master/badge.svg)](https://github.com/JuliaTesting/Aqua.jl) + # TreatmentPanels @@ -17,7 +19,7 @@ julia> Pkg.add("TreatmentPanels") or in the Pkg REPL ``` -(@v1.7) add TreatmentPanels +(@v1.10) add TreatmentPanels ``` ## Quickstart diff --git a/src/TreatmentPanel.jl b/src/TreatmentPanel.jl index 7d438f5..db920d8 100644 --- a/src/TreatmentPanel.jl +++ b/src/TreatmentPanel.jl @@ -185,7 +185,7 @@ function treated_ids(x::BalancedPanel{SingleUnitTreatment{T}}) where T end function treated_ids(x::BalancedPanel{MultiUnitTreatment{T}}) where T - findall(>(0), vec(sum(Y, dims = 2))) + any.(eachrow(x.W)) end """ @@ -197,17 +197,27 @@ function treated_labels(x::BalancedPanel{SingleUnitTreatment{T}}) where T x.is[treated_ids(x)] end +function treated_labels(x::BalancedPanel{MultiUnitTreatment{Simultaneous{Continuous}}}) + x.is[treated_ids(x)] +end + """ first_treated_period_ids(x <: BalancedPanel) Returns the indices of the first treated period for each treated units, that is, a Vector{Int} of length Nₜᵣ, where each element is the index of the first 1 in the row of treatment matrix W corresonding to the treatment unit. + + For a single treated unit, returns only the index of the first treated period. For multiple """ function first_treated_period_ids(x::BalancedPanel{SingleUnitTreatment{T}}) where T findfirst(vec(x.W[treated_ids(x), :])) end +function first_treated_period_ids(x::BalancedPanel{MultiUnitTreatment{T}}) where T + [x for x ∈ findfirst.(eachrow(x.W)) if !isnothing(x)] +end + """ first_treated_period_labels(x <: BalancedPanel) @@ -227,6 +237,11 @@ function length_T₀(x::BalancedPanel{SingleUnitTreatment{Continuous}}) first_treated_period_ids(x) - 1 end +function length_T₀(x::BalancedPanel{MultiUnitTreatment{Simultaneous{Continuous}}}) + first_treated_period_ids(x) .- 1 +end + + """ length_T₁(x <: BalancedPanel) @@ -236,6 +251,9 @@ function length_T₁(x::BalancedPanel{SingleUnitTreatment{Continuous}}) size(x.Y, 2) .- first_treated_period_ids(x) + 1 end +function length_T₁(x::BalancedPanel{MultiUnitTreatment{Simultaneous{Continuous}}}) + size(x.Y[treated_ids(x), :], 2) .- first_treated_period_ids(x) .+ 1 +end """ get_y₁₀(x <: BalancedPanel) @@ -247,6 +265,7 @@ function get_y₁₀(x::BalancedPanel{SingleUnitTreatment{Continuous}}) x.Y[treated_ids(x), 1:first_treated_period_ids(x)-1] end + """ get_y₁₁(x <: BalancedPanel) @@ -340,7 +359,7 @@ end # Fallback method - if the length of treatment assignment is one use single treatment method above function BalancedPanel(df::DataFrame, treatment_assignment; id_var = nothing, t_var = nothing, outcome_var = nothing, - sort_inplace = false) where NType where TType + sort_inplace = false) if length(treatment_assignment) == 1 return BalancedPanel(df, only(treatment_assignment); id_var = id_var, t_var = t_var, diff --git a/src/TreatmentPanels.jl b/src/TreatmentPanels.jl index 1b1fe0a..c8d2b31 100644 --- a/src/TreatmentPanels.jl +++ b/src/TreatmentPanels.jl @@ -5,10 +5,11 @@ include("show_and_plot.jl") # Export main types export TreatmentPanel -export BalancedPanel, UnbalancedPanel +export BalancedPanel +# export UnbalancedPanel - Not yet implemented # Export treatment description types -export UnitTreatmentType +export TreatmentType export SingleUnitTreatment, MultiUnitTreatment export TreatmentTimingType export Staggered, Simultaneous diff --git a/test/Aqua.jl b/test/Aqua.jl new file mode 100644 index 0000000..02f3128 --- /dev/null +++ b/test/Aqua.jl @@ -0,0 +1,12 @@ +using Aqua + +@testset "Aqua.jl" begin + Aqua.test_ambiguities(TreatmentPanels) + Aqua.test_unbound_args(TreatmentPanels) + Aqua.test_undefined_exports(TreatmentPanels) + Aqua.test_project_extras(TreatmentPanels) + Aqua.test_stale_deps(TreatmentPanels) + Aqua.test_deps_compat(TreatmentPanels) + Aqua.test_piracies(TreatmentPanels) + Aqua.test_persistent_tasks(TreatmentPanels) +end \ No newline at end of file diff --git a/test/Project.toml b/test/Project.toml new file mode 100644 index 0000000..195bf62 --- /dev/null +++ b/test/Project.toml @@ -0,0 +1,14 @@ +[deps] +Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" +DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" +Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" +Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[compat] +Aqua = "0.8" +DataFrames = "1" +Dates = "1" +Documenter = "1" +Test = "1" + diff --git a/test/runtests.jl b/test/runtests.jl index 4e8a996..e26b56b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,6 +2,8 @@ using TreatmentPanels using Test using DataFrames, Dates +include("Aqua.jl") + @testset "Input tests" begin test_df = DataFrame(id = ["a", "a", "b", "b"], period = [1, 2, 1, 2], value = 1.0:4.0)