Extract data from pandas dataframe in vector direction

Viewed 35

I have a Pandas DataFrame in this form, which represents a 3D simulation's results of temperature values in a closed space.

  X  |  Y  |  Z  |  T 
 ----+-----+-----+---
 x0  |  y0 |  z0 | T0
 x1  |  y1 |  z1 | T1
 ....|.....|.....|...
 xn  |  yn |  zn | Tn

I define a line vector by two points:

vector (x0,y0,z0), (x1,y1,z1)

The idea is to get the temperature distribution over that line. So I want to extract the values of T in that vector.

This is what I have until now:

import pandas as pd

SCALE = 0.001
X = 76 * SCALE
Y = 1744 * SCALE
Z = 260 * SCALE

vector = (0.0, Y / 2, 0.0), (X, Y / 2, 0.0) # center vector

raw_data = pd.read_csv("data.csv")
df = raw_data[['Points_0','Points_1','Points_2', 'T']]
df = df.rename(columns={'Points_0': 'X', 'Points_1': 'Y', 'Points_2': 'Z'})

and data.csv looks like:

Block Name,Point ID,Points_0,Points_1,Points_2,Points_Magnitude,T,U_0,U_1,U_2,U_Magnitude,p,p_rgh
internalMesh,0,0,0,-0.26,0.26,288.161,0,0,0,0,101486,101486
internalMesh,1,0.00217143,0,-0.26,0.260009,288.182,0,0,0,0,101486,101486
internalMesh,2,0.00434286,0,-0.26,0.260036,288.212,0,0,0,0,101486,101486
internalMesh,3,0.00651429,0,-0.26,0.260082,288.24,0,0,0,0,101486,101486
internalMesh,4,0.00868571,0,-0.26,0.260145,288.268,0,0,0,0,101486,101486
internalMesh,5,0.0108571,0,-0.26,0.260227,288.294,0,0,0,0,101486,101486
internalMesh,6,0.0130286,0,-0.26,0.260326,288.32,0,0,0,0,101486,101486
internalMesh,7,0.0152,0,-0.26,0.260444,288.346,0,0,0,0,101486,101486
internalMesh,8,0.0173714,0,-0.26,0.26058,288.372,0,0,0,0,101486,101486
internalMesh,9,0.0195429,0,-0.26,0.260733,288.397,0,0,0,0,101486,101486
internalMesh,10,0.0217143,0,-0.26,0.260905,288.423,0,0,0,0,101486,101486
internalMesh,11,0.0238857,0,-0.26,0.261095,288.448,0,0,0,0,101486,101486
internalMesh,12,0.0260571,0,-0.26,0.261302,288.473,0,0,0,0,101486,101486
internalMesh,13,0.0282286,0,-0.26,0.261528,288.496,0,0,0,0,101486,101486
internalMesh,14,0.0304,0,-0.26,0.261771,288.52,0,0,0,0,101486,101486
....
internalMesh,69695,0.076,1.744,0.26,1.76491,307.722,0,0,0,0,101466,101485

The problem can be visualized as:

enter image description here

The simulation results are points between the two green plates. and the vector is a line from one plate two the other. The final idea is to plot the temperature profile over that vector.

A thing to notice is that the Vector is always normal to the plates.

1 Answers

As I wrote in a comment, my first idea was to do something like

df.loc[df['Y'].eq(vector[0][1]) & df['Z'].eq(vector[0][2])] 

but it might not fall just with your data. Let's use KDTree from scipy.spatial. First let's create dummy data.

# input
SCALE = 0.001
X = 76 * SCALE
Y = 1744 * SCALE
Z = 260 * SCALE
vector = (0.0, Y / 2, 0.0), (X, Y / 2, 0.0) # center vector

# dummy df
x_arr = np.linspace(0, X, 100)
y_arr = np.linspace(0, Y, 100)
z_arr = np.linspace(-Z, Z, 100)
df = (
    pd.MultiIndex.from_product(
        [np.linspace(0, X, 100), np.linspace(0, Y, 100),np.linspace(-Z, Z, 100)], 
        names=['X','Y','Z'])
    .to_frame(index=False)
    .assign(T=lambda x: 293+np.random.random(size=len(x)))
)

Now you can use KDTree on df, then use query on an array build from your vector. To assure unicity of the point selected, I suggest to use np.unique after the query.

from scipy.spatial import KDTree
# create the Tree
tree = KDTree(df[['X','Y','Z']])
# create your vector with enough point
vector_arr = np.linspace(vector[0], vector[1], int(round(1/SCALE)))
# get the result
# note the use of unique to prevent getting several time the same point
print(df.loc[np.unique(tree.query(vector_arr)[1])])
#                X         Y         Z           T
# 4949    0.000000  0.863192 -0.002626  293.700319
# 14949   0.000768  0.863192 -0.002626  293.881855
# 24949   0.001535  0.863192 -0.002626  293.699151
# 34949   0.002303  0.863192 -0.002626  293.546777
# 44949   0.003071  0.863192 -0.002626  293.584802
# 54949   0.003838  0.863192 -0.002626  293.703028
# 64949   0.004606  0.863192 -0.002626  293.759604
# 74949   0.005374  0.863192 -0.002626  293.615089
# 84949   0.006141  0.863192 -0.002626  293.032280
# 94949   0.006909  0.863192 -0.002626  293.294189
# 104949  0.007677  0.863192 -0.002626  293.861959
# 114949  0.008444  0.863192 -0.002626  293.990511
Related