What is the best way to loop through data and store it in an array without sacrificing performance? The code below is what I am working on but I am guessing it is not very efficient
start_bounds = []
end_bounds = []
for i in 1:N
start_bounds = map(d -> minimum(getindex.(idx, d)), 1:N)
end_bounds = map(d -> maximum(getindex.(idx, d)), 1:N)
end
If it helps, here is the full function
function generate_spatial_bounding_box(img, select_function)
idx = findall(select_function, img)
N = _ndims(eltype(idx))
start_bounds = []
end_bounds = []
for i in 1:N
start_bounds = map(d -> minimum(getindex.(idx, d)), 1:N)
end_bounds = map(d -> maximum(getindex.(idx, d)), 1:N)
end
return start_bounds, end_bounds
end
Oh wait, I think I can just avoid the empty array container altogether. This seems to work fine.
function generate_spatial_bounding_box(img, select_function)
idx = findall(select_function, img)
N = _ndims(eltype(idx))
start_bounds = map(d -> minimum(getindex.(idx, d)), 1:N)
end_bounds = map(d -> maximum(getindex.(idx, d)), 1:N)
return start_bounds, end_bounds
end
BTW you may take a look at GeoStats.jl if you're interested in bounding boxes of spatial data. Part of this functionality is being moved to Meshes.jl in the next release.
Meshes.jl is extremely interesting but I work with medical data, so 3D voxels. I’m not sure if meshes will apply to my use case but you might be able to convince me otherwise if there is a benefit that I’m not seeing?
Meshes.jl should be N-dimensional, we support 3D models and that is definitely part of the goal.
You can follow the #meshes.jl stream in case the works overlaps.
Will do, thanks!
Last updated: Dec 28 2024 at 04:38 UTC