Python map_coordinates() equivalent in Julia

No, I will not debug a 300+ lines of code for you. You basically ask me to finish you task. And You have not yet posted whether you have tried changing (r_grid_stream_pix, theta_grid_stream_pix)to [r_grid_stream_pix, theta_grid_stream_pix] in you function call, which I suspect is the fix. if it is, I would be a bit angry at this point.

edit: Are you kidding me? The linked file does contain datafile ="./home/raman/Pictures/sane00.athinput/" that In no way runs on my machine. How much work do you expect me to put in to get that to run on my machine? And do I then get your salary?

2 Likes

Hi @kellertuer Earlier i have tried using Vector(r_grid_stream_pix, theta_grid_stream_pix) but that didn’t work. Now on doing [r_grid_stream_pix, theta_grid_stream_pix] i am getting error:

ERROR: LoadError: MethodError: no method matching (::var"#map_coordinates#3"{var"#map_coordinates#1#4"})(::Matrix{Float64}, ::Vector{Matrix{Float64}})
Stacktrace:
 [1] main()
   @ Main ~/Fluxes.jl:278
 [2] top-level scope
   @ ~/Fluxes.jl:358
in expression starting at /home/raman/Fluxes.jl:358

You have to specify the location where you have downloaded that folder.

Tha is not how this works.
Please read (already linked, but I am not sure you read it)

point 4.

I will especially not download source code, files and do a full setup of stuff to maybe reproduce your error locally – because it might also just be that our setups are different or so.

For the error you currently get: Yes, that tells you that the two elements of your vector are matrices and the function we already wrote for you expects a vector of two SVectors.

So maybe think about how you can turn your two matrices into SVectors. That is probably domain specific.

In general, this interpretation of the error message is no magic, please learn how to read them, then you can infer that yourself.

4 Likes

I tried using theta_grid_stream_pix = [SVector(theta_grid_stream_pixar[i, :])(zeros(1000,50)) for i in 1:size(theta_grid_stream_pixar, 1)]
to convert matrix to SVector but i got

Error Message
ERROR: LoadError: The size of type `SVector` is not known.

If you were trying to construct (or `convert` to) a `StaticArray` you
may need to add the size explicitly as a type parameter so its size is
inferrable to the Julia compiler (or performance would be terrible). For
example, you might try

    m = zeros(3,3)
    SMatrix(m)            # this error
    SMatrix{3,3}(m)       # correct - size is inferrable
    SArray{Tuple{3,3}}(m) # correct, note Tuple{3,3}

Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] missing_size_error(::Type{SVector})
    @ StaticArraysCore ~/.julia/packages/StaticArraysCore/7xxEJ/src/StaticArraysCore.jl:500
  [3] Size(::Type{SVector})
    @ StaticArraysCore ~/.julia/packages/StaticArraysCore/7xxEJ/src/StaticArraysCore.jl:517
  [4] length(a::Type{SVector})
    @ StaticArrays ~/.julia/packages/StaticArrays/MSJcA/src/abstractarray.jl:2
  [5] convert
    @ ~/.julia/packages/StaticArrays/MSJcA/src/convert.jl:208 [inlined]
  [6] StaticArray
    @ ~/.julia/packages/StaticArrays/MSJcA/src/convert.jl:182 [inlined]
  [7] (::var"#1#5"{Matrix{Float64}})(i::Int64)
    @ Main ./none:0
  [8] iterate
    @ ./generator.jl:47 [inlined]
  [9] collect(itr::Base.Generator{UnitRange{Int64}, var"#1#5"{Matrix{Float64}}})
    @ Base ./array.jl:834
 [10] main()
    @ Main ~/Fluxes.jl:235
 [11] top-level scope
    @ ~/Fluxes.jl:358
in expression starting at /home/raman/Fluxes.jl:358

I am using zeros(1000,50) to provide m mentioned in error output.

Sorry, but that error message even has an example where the error comes from and how to fix that?!

You can not just call SVector(X) you have to provide the size explicitly, so if n= length(X) you can do SVector{n}(X). This is exactly even with an example the error message tells you.
So please read your error messages and start working on fixing those yourself – especially when the error message explicitly tells you how to fix that.

1 Like

All of your issues ultimately stem from the fact that you still appear to lack a basic understanding of how Julia works. It would probably be useful to go through the Julia manual and some tutorials. There’s a lot of resources here:

or watch a video maybe:

If you insist on continuing with your approach of just blindly changing things around and then asking for help with error messages, I’d suggest that an AI assistant like ChatGPT or Claude might be a better discussion partner for that than this forum.

3 Likes

I have already watched videos of Doggo channel.

Now i am getting error due to SVector definition. theta_grid_stream_pix = [SVector{n}(theta_grid_stream_pixar[i, :]) for i in 1:size(theta_grid_stream_pixar, 1)]

("lina, T[lina], T[ntheta-1]=", 1, <py np.float64(0.03681553890925539)>, <py np.float64(3.129320807286708)>)
("phic, P[phic], P[nphi-1]=", 1, <py np.float64(0.14726215563702155)>, <py np.float64(6.234097921967246)>)
zsh: killed     julia Fluxes.jl

I changed SVector part to segments or chunks to overcome memory issue. But still my programme gets killed.

# Define the chunk size
    chunk_size = 100
# Initialize the resulting array
    theta_grid_stream_pix = Vector{SVector{n, Float64}}(undef, size(theta_grid_stream_pixar, 1))

# Process in chunks
	for i in 1:chunk_size:size(theta_grid_stream_pixar, 1)
  	  chunk_end = min(i + chunk_size - 1, size(theta_grid_stream_pixar, 1))
  	   for j in i:chunk_end
           theta_grid_stream_pix[j] = SVector{n}(theta_grid_stream_pixar[j, :])
       end
	end

In Julia, you can use the Interpolations package to achieve functionality similar to Python’s map_coordinates from the scipy.ndimage module. The Interpolations package allows for interpolation of multidimensional data, which is what map_coordinates does.

thanks for sharing...