Skip to content

Commit

Permalink
mdf_amr should return MH in sorted order
Browse files Browse the repository at this point in the history
  • Loading branch information
cgarling committed Sep 28, 2024
1 parent f5a8b4a commit 12e4975
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/fitting/mdf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Calculates the mass-weighted metallicity distribution function given a set of *s
P_j = \\frac{ \\sum_k r_{j,k} \\, [\\text{M} / \\text{H}]_k}{\\sum_{j,k} r_{j,k} \\, [\\text{M} / \\text{H}]_k}
```
where ``r_{j,k}`` are the elements of `coeffs` where ``j`` indexes over unique entries in `logAge` and ``k`` indexes over unique entries in `metallicities.` This is the same nomenclature used in the [the documentation on constrained metallicity evolutions](@ref metal_evo_intro).
where ``r_{j,k}`` are the elements of `coeffs` where ``j`` indexes over unique entries in `logAge` and ``k`` indexes over unique entries in `metallicities.` This is the same nomenclature used in the [the documentation on constrained metallicity evolutions](@ref metal_evo_intro). The return values are sorted so that `unique_MH` is in increasing order.
# Examples
```jldoctest; setup = :(import StarFormationHistories: mdf_amr)
Expand All @@ -30,7 +30,8 @@ function mdf_amr(coeffs::AbstractVector{<:Number}, # Stellar mass coefficients
unique_MH = unique(metallicities)
mass_mdf = [sum(coeffs[idx]) for idx in (findall( ==(i), metallicities) for i in unique_MH)]
mass_mdf ./= sum(mass_mdf) # Normalize to sum probability = 1
return unique_MH, mass_mdf
p = sortperm(unique_MH) # Return in sorted order of unique_MH
return unique_MH[p], mass_mdf[p]
end

# function mdf_amr(stellar_masses::AbstractVector{<:Number},
Expand Down
5 changes: 3 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -619,11 +619,12 @@ const rtols = (1e-3, 1e-7) # Relative tolerance levels to use for the above floa
@test isapprox(result3.mle.μ, result4.mle.μ)

@testset "mdf_amr" begin
# @test SFH.mdf_amr(SFRs, logAge, MH, relweights; relweightsmin=0)[1] == unique_MH
# println(SFH.mdf_amr(SFRs, logAge, MH, relweights; relweightsmin=0)[2])
mdf_result1 = SFH.mdf_amr([1.0,2.0,3.0,4.0],[1.0,2.0,1.0,2.0],[-2.0,-2.0,-1.0,-1.0])
@test mdf_result1[1] [-2.0, -1.0]
@test mdf_result1[2] [0.3, 0.7]
# Test mdf_x is always returned in sorted order
mdf_result2 = SFH.mdf_amr(reverse([1.0,2.0,3.0,4.0]),[1.0,2.0,1.0,2.0],reverse([-2.0,-2.0,-1.0,-1.0]))
@test all(mdf_result1 .== mdf_result2)
end
end
end
Expand Down

0 comments on commit 12e4975

Please sign in to comment.