In [40]:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from scipy.stats import norm
from sklearn.preprocessing import StandardScaler
from scipy import stats
from mpl_toolkits.mplot3d import Axes3D
from sklearn.model_selection import train_test_split
import warnings
import math
warnings.filterwarnings('ignore')
%matplotlib inline
In [41]:
df_data = pd.read_csv('./data/encirclement/AW_RPV_lassy.csv')
In [ ]:
In [ ]:
In [ ]:
In [42]:
df_data.describe()
Out[42]:
In [43]:
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(df_data['X'], df_data['Y'], df_data['Z'])
plt.show()
In [44]:
df_data_scalars = pd.concat([df_data['MainVertexSeq'], df_data['MeshScalar']], axis=1)
In [45]:
ax = sns.boxplot(x="MainVertexSeq", y="MeshScalar", data=df_data_scalars)
In [46]:
g1 = df_data.groupby(['MainVertexSeq']).median()
g1 = g1.reset_index()
In [47]:
g1.head(2)
Out[47]:
In [48]:
ax = sns.tsplot(data=g1['MeshScalar'])
In [49]:
center_data = [df_data['X'].mean(), df_data['Y'].mean(), df_data['Z'].mean()]
In [50]:
center_data
Out[50]:
In [51]:
df_data_scalars = pd.concat([df_data['MainVertexSeq'], df_data['MeshScalar']], axis=1)
In [52]:
g1 = df_data.groupby(['MainVertexSeq']).median()
g1 = g1.reset_index()
In [53]:
ax = sns.tsplot(data=g1['MeshScalar'])
In [54]:
def unit_vector(vector):
""" Returns the unit vector of the vector. """
return vector / np.linalg.norm(vector)
def angle_between(v1, v2):
""" Returns the angle in radians between vectors 'v1' and 'v2'::
>>> angle_between((1, 0, 0), (0, 1, 0))
1.5707963267948966
>>> angle_between((1, 0, 0), (1, 0, 0))
0.0
>>> angle_between((1, 0, 0), (-1, 0, 0))
3.141592653589793
"""
v1_u = unit_vector(v1)
v2_u = unit_vector(v2)
return np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0))
In [55]:
def point_theta(center, A):
thetas = [];
Ref_pt = [A.iloc[0]['X'], A.iloc[0]['Y'], A.iloc[0]['Z']]
for index, row in A.iterrows():
pt = [row['X'], row['Y'], row['Z']]
theta = angle_between(Ref_pt, pt)
thetas.append(theta)
return thetas
In [56]:
thetas = point_theta(center_data, df_data)
In [57]:
thetas_df = pd.Series(thetas, name='theta');
In [58]:
df_data_with_theta = pd.concat([df_data, thetas_df], axis=1)
In [59]:
sns.tsplot(df_data_with_theta['theta'])
Out[59]:
In [60]:
df_data_with_thetas_sorted = df_data_with_theta.sort_values(by='theta')
In [61]:
df_data_with_thetas_sorted.head(10)
Out[61]:
In [62]:
sns.tsplot(df_data_with_thetas_sorted['theta'])
Out[62]:
In [63]:
sns.tsplot(df_data_with_thetas_sorted['MeshScalar'])
Out[63]:
In [ ]:
In [64]:
df_data_with_thetas_sorted.head(15)
Out[64]:
In [65]:
df_data_with_thetas_sorted['MeshScalar'].std()
Out[65]:
In [66]:
df_data_with_thetas_sorted.head()
Out[66]:
In [67]:
df_data_with_thetas_sorted_nodupes = df_data_with_thetas_sorted.drop_duplicates(subset='VertexID', keep='last');
In [68]:
sns.tsplot(pd.rolling_mean(df_data_with_thetas_sorted_nodupes['MeshScalar'],50))
Out[68]:
In [69]:
df_data_with_thetas_sorted_nodupes.describe()
Out[69]:
In [70]:
'''
This function is not complete yet, it tries to compute the distance between each point in the list to its
point on the line (vertex depth = v for all points within the same neighbourhood)
'''
def scar_width(df, threshold):
width = [];
is_scar = [];
#Ref_pt = [A.iloc[0]['X'], A.iloc[0]['Y'], A.iloc[0]['Z']]
for index, row in df.iterrows():
if row['VertexDepth'] == 0:
pt_on_line = (row['X'], row['Y'], row['Z'])
pt = (row['X'], row['Y'], row['Z'])
#distance = math.hypot(pt[0]-pt_on_line[0], pt[1]-pt_on_line[1], pt[1]-pt_on_line[1])
distance = math.sqrt((pt[0]-pt_on_line[0])**2 + (pt[1]-pt_on_line[1])**2 + (pt[2]-pt_on_line[2])**2)
if row['MeshScalar'] > threshold:
is_scar.append(1)
width.append(distance)
else:
is_scar.append(0)
width.append(0)
width_df = pd.Series(width, name='scar_width');
is_scar_df = pd.Series(is_scar, name='scar_bin');
df = pd.concat([df, width_df], axis=1)
df = pd.concat([df, is_scar_df], axis=1)
return df
In [ ]:
In [71]:
df_data_with_thetas_sorted = scar_width(df_data_with_thetas_sorted, 135)
In [72]:
df_data_with_thetas_sorted.head(20)
Out[72]:
In [80]:
sns.tsplot(pd.rolling_mean(df_data_with_thetas_sorted['scar_bin'],50))
Out[80]:
In [74]:
sns.tsplot(pd.rolling_mean(df_data_with_thetas_sorted['scar_width'],200)) # Uses mean windowing=100, so 0 widths are smoothed
Out[74]: