Commit c55b3be2 authored by felix's avatar felix
Browse files

adding drawing methods with plotly

parent 53698764
......@@ -11,11 +11,17 @@ import numpy as np
import scipy.linalg as lina
import sys
# custom embeddings/architectures for mono networkx
from kirchhoff.circuit_init import *
from kirchhoff.circuit_flow import *
from kirchhoff.circuit_flux import *
# custom primer
import kirchhoff.init_dual as init_dual
# custom output functions
import kirchhoff.draw_networkx as dx
def initialize_dual_circuit_from_networkx(input_graph1,input_graph2,e_adj):
kirchhoff_dual_graph=dual_circuit()
......@@ -119,3 +125,11 @@ class dual_circuit():
def clipp_graph(self):
for i in range(2):
self.layer[i].clipp_graph()
# output
def plot_circuit(self):
fig=dx.plot_networkx_dual(self)
return fig
......@@ -13,8 +13,10 @@ import pandas as pd
import sys
# custom embeddings/architectures
import kirchhoff.init_crystal
import kirchhoff.init_random
import kirchhoff.init_crystal as init_crystal
import kirchhoff.init_random as init_random
# custom output functions
import kirchhoff.draw_networkx as dx
def initialize_circuit_from_networkx(input_graph):
......@@ -23,7 +25,6 @@ def initialize_circuit_from_networkx(input_graph):
return kirchhoff_graph
def initialize_circuit_from_crystal(crystal_type='default',periods=1):
kirchhoff_graph=circuit()
......@@ -251,3 +252,10 @@ class circuit:
def set_graph_pars(self, new_parameters):
self.graph=new_parameters
# output
def plot_circuit(self):
fig=dx.plot_networkx(self.G)
return fig
# @Author: Felix Kramer <kramer>
# @Date: 2021-05-08T20:34:30+02:00
# @Email: kramer@mpi-cbg.de
# @Project: go-with-the-flow
# @Last modified by: Felix Kramer
# @Last modified time: 2021-05-23T23:17:48+02:00
# @License: MIT
# standard types
import networkx as nx
import numpy as np
import plotly.graph_objects as go
#generate interactive plots with plotly and return the respective figures
def plot_networkx(input_graph,**kwargs):
options={
'network_id':0,
'color_nodes':['#a845b5'],
'color_edges':['#c762d4']
}
for k,v in kwargs.items():
if k in options:
options[k]=v
fig = go.Figure()
plot_nodes_edges(fig,options,input_graph)
fig.update_layout(showlegend=False)
return fig
def plot_networkx_dual(dual_graph,**kwargs):
options={
'network_id':0,
'color_nodes':['#6aa84f','#a845b5'],
'color_edges':['#2BDF94','#c762d4']
}
for k,v in kwargs.items():
if k in options:
options[k]=v
fig = go.Figure()
for i,K in enumerate(dual_graph.layer):
options['network_id']=i
plot_nodes_edges(fig,options,K.G)
fig.update_layout(showlegend=False)
return fig
#auxillary functions generating traces for nodes and edges
def get_edge_coords(input_graph,options):
pos=nx.get_node_attributes(input_graph,'pos')
if len(list(pos.values())[0]) != options['dim']:
options['dim']=len(list(pos.values())[0])
edge_xyz = [[] for i in range(options['dim'])]
for edge in input_graph.edges():
xyz_0= pos[edge[0]]
xyz_1 = pos[edge[1]]
for i in range(options['dim']):
edge_xyz[i].append(xyz_0[i])
edge_xyz[i].append(xyz_1[i])
edge_xyz[i].append(None)
return edge_xyz
def get_edge_scatter(edge_xyz,options):
if options['dim']==3:
edge_trace = go.Scatter3d(
x=edge_xyz[0], y=edge_xyz[1],z=edge_xyz[2],
line=dict(width=5, color=options['color']),
hoverinfo='none',
mode='lines')
else:
edge_trace = go.Scatter(
x=edge_xyz[0], y=edge_xyz[1],
line=dict(width=5, color=options['color']),
hoverinfo='none',
mode='lines')
return edge_trace
def get_edge_trace(input_graph, **kwargs):
options={
'color':'#888',
'dim':3
}
for k,v in kwargs.items():
if k in options:
options[k]=v
edge_xyz=get_edge_coords(input_graph,options)
edge_trace=get_edge_scatter(edge_xyz,options)
return edge_trace
def get_node_coords(input_graph,options):
pos=nx.get_node_attributes(input_graph,'pos')
if len(list(pos.values())[0])!=options['dim']:
options['dim']=len(list(pos.values())[0])
node_xyz = [[] for i in range(options['dim'])]
for node in input_graph.nodes():
xyz_0= pos[node]
for i in range(options['dim']):
node_xyz[i].append(xyz_0[i])
return node_xyz
def get_node_scatter(node_xyz,options):
if options['dim']==3:
node_trace = go.Scatter3d(
x=node_xyz[0], y=node_xyz[1],z=node_xyz[2],
mode='markers',
hoverinfo='none',
marker=dict(
size=2,
line_width=2,
color=options['color'])
)
else:
node_trace = go.Scatter(
x=node_xyz[0], y=node_xyz[1],
mode='markers',
hoverinfo='none',
marker=dict(
size=2,
line_width=2,
color=options['color'])
)
return node_trace
def get_node_trace(input_graph,**kwargs):
options={
'color':'#888',
'dim':3
}
for k,v in kwargs.items():
if k in options:
options[k]=v
node_xyz=get_node_coords(input_graph,options)
node_trace = get_node_scatter(node_xyz,options)
return node_trace
# integrate traces into the figure
def plot_nodes_edges(fig,options,input_graph):
idx=options['network_id']
edge_trace=(get_edge_trace(input_graph,color=options['color_edges'][idx]))
node_trace=(get_node_trace(input_graph,color=options['color_nodes'][idx]))
fig.add_trace( edge_trace)
fig.add_trace( node_trace)
......@@ -12,7 +12,19 @@ import numpy as np
# construct a non-trivial, periodic 3d embedding
def init_graph_from_crystal(crystal_type,periods):
choose_constructor_option={ 'default': networkx_simple, 'simple': networkx_simple, 'chain': networkx_chain,'bcc': networkx_bcc,'fcc': networkx_fcc,'diamond': networkx_diamond,'laves':networkx_laves}
choose_constructor_option={
'default': networkx_simple,
'simple': networkx_simple,
'chain': networkx_chain,
'bcc': networkx_bcc,
'fcc': networkx_fcc,
'diamond': networkx_diamond,
'laves':networkx_laves,
'trigonal_stack': networkx_trigonal_stack,
'square': networkx_square,
'hexagonal':networkx_hexagonal,
'trigonal_planar':networkx_trigonal_planar
}
if crystal_type in choose_constructor_option:
crystal=choose_constructor_option[crystal_type](periods)
......@@ -394,6 +406,7 @@ class networkx_hexagonal(networkx_crystal,object):
def __init__(self,tiling_factor,periodic=False):
super(networkx_hexagonal,self).__init__()
self.hexagonal_grid(tiling_factor,periodic)
def hexagonal_grid(self, *args):
tiling_factor,periodic_bool=args
......
......@@ -148,7 +148,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
"version": "3.7.3"
}
},
"nbformat": 4,
......
This diff is collapsed.
......@@ -15,7 +15,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
setuptools.setup(
name="kirchhoff", # Replace with your own username
version="0.0.6",
version="0.1.0",
author="felixk1990",
author_email="felixuwekramer@protonmail.com",
description="Collecton of routines for creation and manipulation of Kirchhoff circuits based on resistor-only networks, together with 2D/3D spatial embeddings. ",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment