import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')
from scipy.spatial.distance import pdist, squareform
import networkx as nx
stars=pd.read_csv('assets/stars.csv')
stars.sort_values('dist', ascending=True)
stars['dist'].sort_values()
fig, ax = plt.subplots()
plotstarshist=stars['dist'].plot.hist(color='k', alpha=0.5, bins=75, range=[0, 75], ax=ax)
squareform( pdist(stars[['x-pos','y-pos','z-pos']].values, metric='euclidean') ).flatten()
dist_mat = pd.DataFrame(
pd.DataFrame(
squareform(
pdist(stars[['x-pos','y-pos','z-pos']].values, metric='euclidean')
).flatten()
).values.flatten()
)
dist_mat.plot.hist(color='k', alpha=0.5, bins=150)
print(
dist_mat.values.flatten().shape[0] / stars['#Name'].values.shape[0], stars['#Name'].values.shape[0]
)
sourceCol = np.transpose(\
np.array( [ stars['#Name'].values for __ in range( stars['#Name'].values.shape[0] )\
])\
).flatten()
targetCol = np.array( [ stars['#Name'].values for __ in range( stars['#Name'].values.shape[0] )\
]).flatten()
print(
sourceCol.shape, targetCol.shape, dist_mat.values.shape
)
dist_mat.insert(1,'source',sourceCol)
dist_mat.insert(2,'target',targetCol)
dist_mat.columns = ['distance', 'source', 'target']
dist_mat
#StarGraph = nx.from_pandas_dataframe(dist_mat, 'source', 'target', 'distance')
StarGraph = nx.from_pandas_edgelist(dist_mat, 'source', 'target', 'distance')
StarGraph['Sun']['Sun']['distance']
print( [p for p in nx.all_shortest_paths(StarGraph, source='Sun', target='GJ-2108', weight='distance')] )
dist10df = dist_mat[dist_mat['distance']<10]
dist10df
dist10df.to_csv('dist10df.csv', sep=',',index=False)
StarGraph10 = nx.from_pandas_edgelist(dist10df, 'source', 'target', 'distance')
nx.shortest_path(StarGraph10, source='Sun', target='Gl-305', weight='distance')
print( [p for p in nx.all_shortest_paths(StarGraph10, source='Sun', target='Gl-305', weight='distance')] )
print(
nx.dijkstra_path(StarGraph10,'Sun','Gl-305'), '\n' , nx.dijkstra_path(StarGraph10,'Sun','Gl-305',weight='distance')
)
dist10df['source'].shape
dist10dfSC = dist10df['source'].value_counts()
dist10dfSC
for i in range( dist10dfSC[dist10dfSC > 15].values.shape[0] ):
print(
dist_mat[ (dist_mat['target'] == dist10dfSC.index[i]) & (dist_mat['source'] == 'Sun') ]['distance'].values, dist10dfSC[dist10dfSC > 15].values[i]
)
def DistanceMatrix(stars_dataframe):
return pd.DataFrame( \
pd.DataFrame( \
squareform( \
pdist(stars_dataframe[['x-pos','y-pos','z-pos']].values, metric='euclidean') \
).flatten() \
).values.flatten() \
)
def SourceColumn(stars_dataframe):
return np.transpose(\
np.array( [ stars_dataframe['#Name'].values for __ in range( stars_dataframe['#Name'].values.shape[0] )\
])\
).flatten()
def TargetColumn(stars_dataframe):
return np.array( [ stars_dataframe['#Name'].values for __ in range( stars_dataframe['#Name'].values.shape[0] )\
]).flatten()
stars_30 = stars[stars['dist']<30]
stars_30.sort_values('dist', ascending=True)
dist_mat_30 = DistanceMatrix(stars_30)
dist_mat_30
print(
dist_mat_30.values.flatten().shape[0] / stars_30['#Name'].values.shape[0], stars_30['#Name'].values.shape[0]
)
dist_mat_30.insert(1,'source',SourceColumn(stars_30))
dist_mat_30.insert(2,'target',TargetColumn(stars_30))
dist_mat_30.columns = ['distance', 'source', 'target']
dist_mat_30
dist10df_30 = dist_mat_30[dist_mat_30['distance']<10]
dist10df_30.to_csv('dist10df_30.csv', sep=',',index=False)
dist10df_30_st = dist10df_30[['source','target']]
dist10df_30_st.to_csv('dist10df_30_st.csv', sep=',',index=False) # only source and target column
def FindTargets_dist10df_30_st(source_name):
return dist10df_30_st[ dist10df_30_st['source']==source_name ]['target'].values
FindTargets_dist10df_30_st('Sun')
flarejsondf = pd.DataFrame(columns=['source','targets', 'bright', 'temp'])
dist10df_30_st['source'].value_counts().index
stars[stars['#Name']=='Sun']['bright']
i_flag = 0
for source in dist10df_30_st['source'].value_counts().index:
flarejsondf.loc[i] = [source, FindTargets_dist10df_30_st(source), stars[stars['#Name']==source]['bright'].values[0], stars[stars['#Name']==source]['temp'].values[0]]
i = i + 1
flarejsondf
flarejsondf.reset_index().to_json('dist10df_30.json', orient='records')