-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from cgarling/ast_util_ext
Implement helper function `process_ASTs` to process artificial star tests. I wanted to support `DataFrames.DataFrame` and `TypedTables.Table` but couldn't figure out a single implementation that would work on both, so I wrote an implementation for both types in separate package extensions. I don't really like this solution as there is a lot of code duplication but I cannot currently figure out how to iterate a `DataFrame` and a `TypedTable` in the same way. Due to this design, there is *no default implementation* in the base package. One of the above packages must be loaded before `process_ASTs` can be called. This solution uses package extensions which are only supported on Julia versions 1.9 or higher. I am not backporting these to earlier versions, though it could be done with https://github.com/cjdoris/PackageExtensionCompat.jl. Ideally in the long term we could transition this to a single implementation that would work with both types, without having to depend directly on either in the base package.
- Loading branch information
Showing
11 changed files
with
339 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module DataFramesExt | ||
|
||
import StarFormationHistories: process_ASTs | ||
using Printf: @sprintf | ||
using DataFrames: DataFrame | ||
using StatsBase: median, mean | ||
|
||
function process_ASTs(ASTs::DataFrame, inmag::Symbol, outmag::Symbol, | ||
bins::AbstractVector{<:Real}, selectfunc; | ||
statistic=median) | ||
@assert length(bins) > 1 | ||
!issorted(bins) && sort!(bins) | ||
|
||
completeness = Vector{Float64}(undef, length(bins)-1) | ||
bias = similar(completeness) | ||
error = similar(completeness) | ||
bin_centers = similar(completeness) | ||
|
||
input_mags = getproperty(ASTs, inmag) | ||
|
||
Threads.@threads for i in eachindex(completeness) | ||
# Get the stars in the current bin | ||
inbin = findall((input_mags .>= bins[i]) .& | ||
(input_mags .< bins[i+1])) | ||
tmp_asts = ASTs[inbin,:] | ||
if size(tmp_asts,1) == 0 | ||
@warn(@sprintf("No input magnitudes found in bin ranging from %.6f => %.6f \ | ||
in `ASTs.inmag`, please revise `bins` argument.", bins[i], | ||
bins[i+1])) | ||
completeness[i] = NaN | ||
bias[i] = NaN | ||
error[i] = NaN | ||
bin_centers[i] = bins[i] + (bins[i+1] - bins[i])/2 | ||
continue | ||
end | ||
# Let selectfunc determine which ASTs are properly detected | ||
good = [selectfunc(row) for row in eachrow(tmp_asts)] | ||
completeness[i] = count(good) / size(tmp_asts,1) | ||
if count(good) > 0 | ||
inmags = getproperty(tmp_asts, inmag)[good] | ||
outmags = getproperty(tmp_asts, outmag)[good] | ||
diff = outmags .- inmags # This makes bias relative to input | ||
bias[i] = statistic(diff) | ||
error[i] = statistic(abs.(diff)) | ||
bin_centers[i] = mean(inmags) | ||
else | ||
@warn(@sprintf("Completeness measured to be 0 in bin ranging from \ | ||
%.6f => %.6f. The error and bias values for this bin \ | ||
will be returned as NaN.", bins[i], bins[i+1])) | ||
bias[i] = NaN | ||
error[i] = NaN | ||
bin_centers[i] = bins[i] + (bins[i+1] - bins[i])/2 | ||
end | ||
end | ||
return bin_centers, completeness, bias, error | ||
end | ||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module TypedTablesExt | ||
|
||
import StarFormationHistories: process_ASTs | ||
using Printf: @sprintf | ||
using TypedTables: Table | ||
using StatsBase: median, mean | ||
|
||
function process_ASTs(ASTs::Table, inmag::Symbol, outmag::Symbol, | ||
bins::AbstractVector{<:Real}, selectfunc; | ||
statistic=median) | ||
@assert length(bins) > 1 | ||
!issorted(bins) && sort!(bins) | ||
|
||
completeness = Vector{Float64}(undef, length(bins)-1) | ||
bias = similar(completeness) | ||
error = similar(completeness) | ||
bin_centers = similar(completeness) | ||
|
||
input_mags = getproperty(ASTs, inmag) | ||
|
||
Threads.@threads for i in eachindex(completeness) | ||
# Get the stars in the current bin | ||
inbin = findall((input_mags .>= bins[i]) .& | ||
(input_mags .< bins[i+1])) | ||
tmp_asts = ASTs[inbin] | ||
if length(tmp_asts) == 0 | ||
@warn(@sprintf("No input magnitudes found in bin ranging from %.6f => %.6f \ | ||
in `ASTs.inmag`, please revise `bins` argument.", bins[i], | ||
bins[i+1])) | ||
completeness[i] = NaN | ||
bias[i] = NaN | ||
error[i] = NaN | ||
bin_centers[i] = bins[i] + (bins[i+1] - bins[i])/2 | ||
continue | ||
end | ||
# Let selectfunc determine which ASTs are properly detected | ||
good = selectfunc.(tmp_asts) | ||
completeness[i] = count(good) / length(tmp_asts) | ||
if count(good) > 0 | ||
inmags = getproperty(tmp_asts, inmag)[good] | ||
outmags = getproperty(tmp_asts, outmag)[good] | ||
diff = outmags .- inmags # This makes bias relative to input | ||
bias[i] = statistic(diff) | ||
error[i] = statistic(abs.(diff)) | ||
bin_centers[i] = mean(inmags) | ||
else | ||
@warn(@sprintf("Completeness measured to be 0 in bin ranging from \ | ||
%.6f => %.6f. The error and bias values for this bin \ | ||
will be returned as NaN.", bins[i], bins[i+1])) | ||
bias[i] = NaN | ||
error[i] = NaN | ||
bin_centers[i] = bins[i] + (bins[i+1] - bins[i])/2 | ||
end | ||
end | ||
return bin_centers, completeness, bias, error | ||
end | ||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.