Stream: helpdesk (published)

Topic: How to use containers without losing speed


view this post on Zulip Dale Black (Feb 09 2021 at 01:11):

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

view this post on Zulip Dale Black (Feb 09 2021 at 06:05):

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

view this post on Zulip Júlio Hoffimann (Feb 09 2021 at 06:49):

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.

view this post on Zulip Dale Black (Feb 09 2021 at 17:02):

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?

view this post on Zulip Júlio Hoffimann (Feb 09 2021 at 17:19):

Meshes.jl should be N-dimensional, we support 3D models and that is definitely part of the goal.

view this post on Zulip Júlio Hoffimann (Feb 09 2021 at 17:20):

You can follow the #meshes.jl stream in case the works overlaps.

view this post on Zulip Dale Black (Feb 09 2021 at 23:11):

Will do, thanks!


Last updated: Oct 02 2023 at 04:34 UTC