I want to write Python function cis.read_data() equivalent in Julia.
def read_data(data_list, read_function):
"""
Wrapper for calling an HDF reading function for each dataset, and then concatenating the result.
:param list data_list: A list of data objects to read
:param callable or str read_function: A function for reading the data, or 'SD' or 'VD' for default reading routines.
:return: A single numpy array of concatenated data values.
"""
if callable(read_function):
out = utils.concatenate([read_function(i) for i in data_list])
elif read_function == 'VD':
out = utils.concatenate([hdf_vd.get_data(i) for i in data_list])
elif read_function == 'SD':
out = utils.concatenate([hdf_sd.get_data(i) for i in data_list])
else:
raise ValueError("Invalid read-function: {}, please supply a callable read "
"function, 'VD' or 'SD' only".format(read_function))
return out
I have to read a part of data from a hdf5 file and i will use a config file for selecting that part of data.
"""
read_data(read_function, data_list)
Wrapper for calling an HDF reading function for each dataset, and then concatenating the result.
- data_list: A list of data objects to read
- read_function: A function for reading the data, or 'SD' or 'VD' for default reading routines.
Return single array of concatenated data values.
"""
function read_data(read_function::Function, data_list)
# note I change the order of args because it's more Julian this way
return stack(read_function, data_list)
end
# if you really want that string argument as well you could define something like
function read_data(functionname::String, data_list)
# now determine the function to call and just call
return read_data(myfun, data_list)
end
read_function in my code is as well defined as it is in your code - it is simply the function’s argument. I don’t know what function your are referring to and just rewrote the function that you gave in your post as a Julia function. The connect to HDF5 below is unclear.
See cis.read_data()
I have to convert following python code to julia.
from pyfiles import *
runname='./' #define start dir and file, uncomment below
sane = 1#0 for MAD98, 1 for SANE00
if sane == 1:
inputfile=runname+'sane00.athinput'
datafile='sane00.prim.01800.athdf'
data=read_data(datafile,athinput=inputfile)
so i think read_function in my case is config file named sane00.athinput
You link the very same code you posted here that I translated to Julia for you.
The script you posted contains the call read_data(datafile,athinput=inputfile) where both arguments are strings which is not compatible with the function definition you gave me.
If you want to replicate the functionality of that Python project, then the much easier route is to just use PythonCall.jl and call it directly.
I have to write efficient code as it will be used for plotting astrophysical numerical simulations outputs of Athena++ so it would be better if i use julia instead of using PythonCall.jl.
I have to agree with @abraemer here – from the snippet you posted, his code is the translation of that.
Also this thread sounds quite like “Please program this for me” – I think you will not really find someone doing that for you for free.
PythonCall.jl would also be a good idea to do this step by step. Inside the functions provided above as a start you could first call the corresponding python functions and then turn that into Julia code step by step. That way you could always check whether it still works.
It also looks like the python package you link has a certain maturity already, so if you “just want to reprogram it in Julia” – I doubt you will hit their efficiency “on first try”. Then PythonCall might still be a better start there.
In addition to @kellertuer’s questions I have and additional one:
Are you sure this effort is even necessary/worth it? Plotting is usually not the limiting factor in large-scale numerical simulations.
function read_data(data_list::Vector{Any}, read_function::Union{Function, String})
if typeof(read_function) == Function
out = vcat([read_function(i) for i in data_list]...)
elseif read_function == "VD"
out = vcat([hdf_vd_get_data(i) for i in data_list]...)
elseif read_function == "SD"
out = vcat([hdf_sd_get_data(i) for i in data_list]...)
else
throw(ArgumentError("Invalid read-function: $read_function, please supply a callable read function, 'VD' or 'SD' only"))
end
return out
end
I am unable to find alternative of “VD” and “SD” in Julia.
Honestly i don’t know how much speed will change by doing it but i think it would be better if i keep everything in Julia. Don’t Julia have some alternative of that cis.read_data() function ?
To me that seems require a lot of effort for uncertain gains and it is not even clear whether these gains translate to something noticeable in the real world. I’d recommend you focus your effort elsewhere for now and only if it becomes clear that this part is a bottleneck, then you come back and try to improve it.
Not even Python has this function. It belongs to CIS which is a command-line tool that can be used as a Python library. It is a multi-year open source project. You won’t be able to replicate all of this just on your own in any reasonable amount of time. You really should consider just using this tool as is and not try to replicate its functionality.