Skip to content
Snippets Groups Projects
Commit b360a1e5 authored by adip00's avatar adip00
Browse files

update noob.ipynb

parent 8da43221
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
from CONWAY_table_data import evo_table from CONWAY_table_data import evo_table
import numpy as np import numpy as np
import pandas as pd import pandas as pd
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
``` ```
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
evo_mapping_dict = {row[1]: row[2].split() for row in evo_table} evo_mapping_dict = {row[1]: row[2].split() for row in evo_table}
column_names = ['atomic_num', 'element', 'evolution', 'string', 'string_evolution'] column_names = ['atomic_num', 'element', 'evolution', 'string', 'string_evolution']
df = pd.DataFrame(evo_table, columns=column_names) df = pd.DataFrame(evo_table, columns=column_names)
``` ```
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
evo_mapping_dict['He'] evo_mapping_dict['He']
``` ```
   
%% Output %% Output
   
['Hf', 'Pa', 'H', 'Ca', 'Li'] ['Hf', 'Pa', 'H', 'Ca', 'Li']
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
df.head() df.head()
``` ```
   
%% Output %% Output
   
atomic_num element evolution \ atomic_num element evolution \
0 1 H H 0 1 H H
1 2 He Hf Pa H Ca Li 1 2 He Hf Pa H Ca Li
2 3 Li He 2 3 Li He
3 4 Be Ge Ca Li 3 4 Be Ge Ca Li
4 5 B Be 4 5 B Be
string \ string \
0 22 0 22
1 13112221133211322112211213322112 1 13112221133211322112211213322112
2 312211322212221121123222112 2 312211322212221121123222112
3 111312211312113221133211322112211213322112 3 111312211312113221133211322112211213322112
4 1321132122211322212221121123222112 4 1321132122211322212221121123222112
string_evolution string_evolution
0 22 0 22
1 11132132212312211322212221121123222112 1 11132132212312211322212221121123222112
2 13112221133211322112211213322112 2 13112221133211322112211213322112
3 3113112221131112211322212312211322212221121123... 3 3113112221131112211322212312211322212221121123...
4 111312211312113221133211322112211213322112 4 111312211312113221133211322112211213322112
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
def get_encoding(e): def get_encoding(e):
#print(e) #print(e)
one_hot = np.zeros((92)) one_hot = np.zeros((92))
elements = evo_mapping_dict[e] elements = evo_mapping_dict[e]
#print(elements) #print(elements)
indices = (df.loc[df['element'].isin(elements), 'atomic_num']-1).tolist() indices = (df.loc[df['element'].isin(elements), 'atomic_num']-1).tolist()
one_hot[indices] = 1 one_hot[indices] = 1
return one_hot return one_hot
``` ```
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
onehot = [] onehot = []
for e in df['element']: for e in df['element']:
onehot.append(get_encoding(e)) onehot.append(get_encoding(e))
onehot = np.array(onehot) onehot = np.array(onehot)
print(onehot.shape) print(onehot.shape)
``` ```
   
%% Output %% Output
   
(92, 92) (92, 92)
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
# Create a DataFrame with the connection data # Create a DataFrame with the connection data
connections_df = pd.DataFrame(onehot, columns=range(1, 93), index=range(1, 93)) connections_df = pd.DataFrame(onehot, columns=range(1, 93), index=range(1, 93))
connections_df.head() connections_df.head()
``` ```
   
%% Output %% Output
   
1 2 3 4 5 6 7 8 9 10 ... 83 84 85 86 \ 1 2 3 4 5 6 7 8 9 10 ... 83 84 85 86 \
1 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 1 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
2 1.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 2 1.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
3 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 3 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
4 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 4 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
5 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 5 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
87 88 89 90 91 92 87 88 89 90 91 92
1 0.0 0.0 0.0 0.0 0.0 0.0 1 0.0 0.0 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 0.0 1.0 0.0 2 0.0 0.0 0.0 0.0 1.0 0.0
3 0.0 0.0 0.0 0.0 0.0 0.0 3 0.0 0.0 0.0 0.0 0.0 0.0
4 0.0 0.0 0.0 0.0 0.0 0.0 4 0.0 0.0 0.0 0.0 0.0 0.0
5 0.0 0.0 0.0 0.0 0.0 0.0 5 0.0 0.0 0.0 0.0 0.0 0.0
[5 rows x 92 columns] [5 rows x 92 columns]
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
import holoviews as hv import holoviews as hv
from holoviews import opts from holoviews import opts
from bokeh.palettes import Blues, Viridis from bokeh.palettes import Blues, Viridis
   
hv.extension('bokeh') hv.extension('bokeh')
``` ```
   
%% Output %% Output
   
   
   
   
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
# Compute the number of connections for each element # Compute the number of connections for each element
num_connections = connections_df.sum(axis=1) num_connections = connections_df.sum(axis=1)
   
# Create the source data for the chord diagram # Create the source data for the chord diagram
data = connections_df.stack().reset_index() data = connections_df.stack().reset_index()
data.columns = ['source', 'target', 'value'] data.columns = ['source', 'target', 'value']
color = [str(num_connections[i]) for i in data['source']] color = [str(num_connections[i]) for i in data['source']]
data['color'] = color data['color'] = color
   
# Create a dictionary to map the atomic numbers to element names # Create a dictionary to map the atomic numbers to element names
atomic_number_to_element = {row[0]: row[1] for row in evo_table} atomic_number_to_element = {row[0]: row[1] for row in evo_table}
   
# Map the atomic numbers to element names in the data DataFrame # Map the atomic numbers to element names in the data DataFrame
data['source_element'] = data['source'].map(atomic_number_to_element) data['source_element'] = data['source'].map(atomic_number_to_element)
data['target_element'] = data['target'].map(atomic_number_to_element) data['target_element'] = data['target'].map(atomic_number_to_element)
   
data = data[data['value'] != 0] data = data[data['value'] != 0]
data = data.sort_values(by=['color'], ascending=False) data = data.sort_values(by=['color'], ascending=False)
print(data.head()) print(data.head())
   
plot_data = data[['source_element', 'target_element']] plot_data = data[['source_element', 'target_element']]
``` ```
   
%% Output %% Output
   
source target value color source_element target_element source target value color source_element target_element
1866 21 27 1.0 5.0 Sc Co 1866 21 27 1.0 5.0 Sc Co
1840 21 1 1.0 5.0 Sc H 1840 21 1 1.0 5.0 Sc H
1906 21 67 1.0 5.0 Sc Ho 1906 21 67 1.0 5.0 Sc Ho
1930 21 91 1.0 5.0 Sc Pa 1930 21 91 1.0 5.0 Sc Pa
92 2 1 1.0 5.0 He H 92 2 1 1.0 5.0 He H
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
labels = list(set(data['source_element'].unique().tolist() + data['target_element'].unique().tolist())) labels = list(set(data['source_element'].unique().tolist() + data['target_element'].unique().tolist()))
labels = hv.Dataset(pd.DataFrame(labels, columns=['element'])) labels = hv.Dataset(pd.DataFrame(labels, columns=['element']))
``` ```
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
%%opts Chord [height=1000 width=1000 labels="element"] %%opts Chord [height=1000 width=1000 labels="element"]
%%opts Chord (node_color="element" node_cmap="Category20" edge_color="source_element" edge_cmap='Category20') %%opts Chord (node_color="element" node_cmap="Category20" edge_color="source_element" edge_cmap='Category20')
chord = hv.Chord((plot_data, labels)) chord = hv.Chord((plot_data, labels))
chord chord
# output not visible here, dunno why, but saved as chord_plot.png
``` ```
   
%% Output %% Output
   
   
:Chord [source_element,target_element] :Chord [source_element,target_element]
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
``` ```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment