How to define hdf5 file data as a function of some variables?


#1

Hi experts,
I need your kind suggestion. I have a hdf5 file and I can able to read the file in python. The h5 file contains a large number of data and these are the values of potential (depends on position (x, y, z) ). I am also able to interpolate my 3D regular grid data by using RegularGridInterpolator. Now, my question is: how I can define my h5 file data as a function of x, y, z so that it can able to update my a_1 (x,y,z) , a_2(x,y,z)… values in my code. Here is my h5 file: https://drive.google.com/open?id=1OFIg7pjfpfjq3Vnvqaj2uyCFjiTwZ9QK

import numpy as np
from numpy import gradient
import h5py
import matplotlib.pyplot as plt
from scipy.interpolate import RegularGridInterpolator

f = h5py.File('k.h5', 'r') 
list(f.keys())
dset = f[u'data']
dset.shap
dset.value.shape
dset[0:64, 0:64, 0:64]
x = np.linspace(-160, 160, 64)
y = np.linspace(-160, 160, 64)
z = np.linspace(-160, 160, 64)

my_interpolating_function = RegularGridInterpolator((x, y, z), dset.value)
pts = np.array([100, 5, -10])  
my_interpolating_function(pts)

# Apply gradient function
gradx, grady, gradz = np.gradient(dset.value)
gradx.shape

# To find the gradient at any point
gradx_interpol = RegularGridInterpolator((x, y, z), gradx)
grady_interpol = RegularGridInterpolator((x, y, z), grady)
gradz_interpol = RegularGridInterpolator((x, y, z), gradz)


def get_val_and_grads(pts):
    v1, x1, y1, z1 = my_interpolating_function(pts), gradx_interpol(
        pts), grady_interpol(pts), gradz_interpol(pts)
    return v1, x1, y1, z1


##getting_interpolated_values

k1 = my_interpolating_function(pts)
k_dx = gradx_interpol(pts)
k_dy = grady_interpol(pts)
k_dz = gradz_interpol(pts)

def a_1(x,y,z):

    return  -(adot/a**2)*k1

def a_2(x,y,z):

    return (1/a)*k_dx

.
.
.
.


#2

Hello!

I couldn’t download your HDF5 file because the link does not seem to allow public sharing so what follows is untested.

Did you get any error or warning when executing the code you provided? I noticed in the line below:

my_interpolating_function = RegularGridInterpolator((x, y, z), dset.value)

the use of the .value property (dset.value). It is deprecated (see its h5py code) and is also the alias for dset[()] which works only for scalar HDF5 datasets. I think you should use dset[...] instead which will provide all the HDF5 dataset’s values as a NumPy array.

If the above does not answer your question, please explain further what you are trying to achieve. Alternatively, you could consider signing up for our Kita Lab where you could upload the HDF5 file and share your Python code as a Jupyter notebook.

Regards,

    Aleksandar

#3

Thanks for your reply. Please check this link: https://drive.google.com/open?id=14lrChd9nn8M3HqH3GQrCT7OXBxVSYuD8 . But my concern is that my h5 file contains the values of potential which is depends on position (x, y, z). So, I have to define my interpolating function in such a way so that my potential values depend on x, y, z and update the values of a_1 and a_2 as they are depends on x, y, z. Can you please help me how I can define my interpolating function so that it’s depend on x, y, z.


#4

Hello!

I was able to download your HDF5 file this time.

One correction to my previous reply: dset.value and dset[...] return the same NumPy array which holds your potential data.

Why your my_interpolating_function does not work? Below is a function that effectively combines several lines of your code above:

def potential_interpolator(potential, axes_bounds):
    x_bounds, y_bounds, z_bounds = axes_bounds
    x = np.linspace(x_bounds[0], x_bounds[1], potential.shape[0])
    y = np.linspace(y_bounds[0], y_bounds[1], potential.shape[1])
    z = np.linspace(z_bounds[0], z_bounds[1], potential.shape[2])
    return RegularGridInterpolator((x, y, z), potential[...])

interp_potential = potential_interpolator(dset, ([-160, 160], [-160, 160], [-160, 160]))

You would then calculate interpolated potential values with something like: interp_potential(pts).

The a_1 and a_2 functions have some undefined variables: adot and a, so not sure what to do there.

Hope this helps!

   Aleksandar