Skip to content

Commit

Permalink
Refactor latest_comparable_paths
Browse files Browse the repository at this point in the history
  • Loading branch information
charleskawczynski committed Nov 18, 2024
1 parent a9f4ebf commit 8b08e01
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 50 deletions.
66 changes: 19 additions & 47 deletions reproducibility_tests/reproducibility_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,17 @@ end
n = 5,
root_path = "/central/scratch/esm/slurm-buildkite/climaatmos-main",
ref_counter_PR = read_ref_counter(joinpath(@__DIR__, "ref_counter.jl"))
skip = get(ENV, "BUILDKITE_PIPELINE_SLUG", nothing) != "climaatmos-ci"
)
Returns a vector of strings, containing the `n`
latest comparable paths. The assumed folder structure
is:
latest comparable paths in the `root_path` directory.
Only paths that match the `ref_counter_PR` are
returned, and an empty vector is retuned if
`skip = true`. By default, `skip` is set to
`get(ENV, "BUILDKITE_PIPELINE_SLUG", nothing) != "climaatmos-ci"`.
The assumed folder structure is:
```
root_path/some_folder_1/ref_counter.jl
Expand All @@ -110,53 +116,19 @@ function latest_comparable_paths(;
n = 5,
root_path = "/central/scratch/esm/slurm-buildkite/climaatmos-main",
ref_counter_PR = read_ref_counter(joinpath(@__DIR__, "ref_counter.jl")),
skip = get(ENV, "BUILDKITE_PIPELINE_SLUG", nothing) != "climaatmos-ci",
)
@info "---Finding the latest comparable paths"
# Note: root_path is also defined in move_output.jl
# Get (sorted) array of paths, `pop!(sorted_paths)`
# is the most recent merged folder.
sorted_paths = sorted_dataset_folder(; dir = root_path)
if isempty(sorted_paths)
@warn "No paths found in $root_path"
return String[]
end
# Find oldest path in main with the same reference
# counter as the one in the PR. If none exists,
# then assume no comparable references.

# Short circuit if we don't find anything:
found_ref_counters =
filter(p -> isfile(joinpath(p, "ref_counter.jl")), sorted_paths)
if isempty(found_ref_counters)
@warn "No reference counters found in paths: $sorted_paths"
return String[]
end

# Find comparable paths
comparable_paths = String[]
@info "Reference counters found:"
for (i, path) in enumerate(sorted_paths)
ref_counter_file = joinpath(path, "ref_counter.jl")
!isfile(ref_counter_file) && continue
rc = read_ref_counter(ref_counter_file)
comparable = ref_counter_PR == rc
suffix = comparable ? ", comparable" : ""
@info " $path: $rc$suffix"
comparable && push!(comparable_paths, path)
skip && return String[]
bins = compute_bins(root_path)
isempty(bins) && return String[]
ref_counter_bins = filter(bins) do bin
f = joinpath(first(bin), "ref_counter.jl")
isfile(f) && ref_counter_PR == read_ref_counter(f)
end

if isempty(comparable_paths)
@warn "No comparable paths found in any of the paths:$sorted_paths"
return String[]
end

comparable_paths = reverse(comparable_paths) # sort so that

if length(comparable_paths) > n # limit to n comparable paths
comparable_paths = comparable_paths[1:min(n, length(comparable_paths))]
end

return comparable_paths
isnothing(ref_counter_bins) && return String[]
isempty(ref_counter_bins) && return String[]
comparable_paths = ref_counter_bins[1]
return comparable_paths[1:min(n, length(comparable_paths))]
end

"""
Expand Down
12 changes: 9 additions & 3 deletions test/unit_reproducibility_infra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ end
paths = quiet_latest_comparable_paths(;
root_path = path,
ref_counter_PR = 2,
skip = false,
)
@test paths == []
end
Expand All @@ -42,6 +43,7 @@ end
paths = quiet_latest_comparable_paths(;
root_path = path,
ref_counter_PR = 2,
skip = false,
)
@test paths == []
end
Expand All @@ -54,6 +56,7 @@ end
paths = quiet_latest_comparable_paths(;
root_path = path,
ref_counter_PR = 2,
skip = false,
)
@test paths == []
end
Expand All @@ -68,6 +71,7 @@ end
paths = quiet_latest_comparable_paths(;
root_path = path,
ref_counter_PR = 2,
skip = false,
)
@test paths == [p2]
end
Expand All @@ -85,6 +89,7 @@ end
paths = quiet_latest_comparable_paths(;
root_path = path,
ref_counter_PR = 3,
skip = false,
)
@test paths == [p6, p5, p4, p3] # p6 is most recent
end
Expand All @@ -103,6 +108,7 @@ end
n = 2,
root_path = path,
ref_counter_PR = 3,
skip = false,
)
@test paths == [p6, p5] # p6 is most recent
end
Expand All @@ -121,10 +127,9 @@ end
n = 2,
root_path = path,
ref_counter_PR = 3,
skip = false,
)
# TODO: do we want to compare against p3 here?
# This could be problematic for reverted commits
@test paths == [p6, p3]
@test paths == [p6]
end
end

Expand All @@ -142,6 +147,7 @@ end
n = 2,
root_path = path,
ref_counter_PR = 3,
skip = false,
)
@test paths == [p7, p6]
end
Expand Down

0 comments on commit 8b08e01

Please sign in to comment.