Skip to content

Commit 7f38287

Browse files
committed
newer extract func
1 parent 664967b commit 7f38287

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

pkg/VoltoMapSim/src/plot.jl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ function plot_detection_rates(
121121
return fig, ax
122122
end
123123

124-
"""
125-
Create an array of the same shape as the one given, but with just
126-
the values stored under `name` in each element of the given array.
127-
"""
128-
function extract(name::Symbol, arr #=an array of NamedTuples or structs =#)
129-
getval(index) = getproperty(arr[index], name)
130-
out = similar(arr, typeof(getval(firstindex(arr))))
131-
for index in eachindex(arr)
132-
out[index] = getval(index)
124+
function extract(name::Symbol, arr)
125+
# `arr` is an array of NamedTuples or structs.
126+
# Creates an array of the same shape as the one given, but
127+
# with just the values stored under `name` in each element of the given array.
128+
getval(el) = getproperty(el, name)
129+
valtype = arr |> first |> getval |> typeof
130+
out = similar(arr, valtype)
131+
for i in eachindex(arr)
132+
out[i] = getval(arr[i])
133133
end
134134
return out
135135
end
@@ -145,6 +145,7 @@ function plot_samples_and_means(
145145
plot(x, data, ".", ax; alpha=0.5, c, clip_on, kw...)
146146
end
147147

148+
148149
add_refline(ax, y = 0; c = "0.2", lw = 1, ls = "solid", zorder = 1, kw...) =
149150
ax.axhline(y; c, lw, ls, zorder, kw...)
150151

0 commit comments

Comments
 (0)