Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
kramer
kirchhoff-circuits
Commits
da04cb7a
Commit
da04cb7a
authored
Jun 23, 2021
by
Felix
Browse files
finalize plotly output methods
parent
bca87766
Changes
6
Expand all
Hide whitespace changes
Inline
Side-by-side
kirchhoff/circuit_dual.py
View file @
da04cb7a
...
...
@@ -3,7 +3,7 @@
# @Email: kramer@mpi-cbg.de
# @Project: go-with-the-flow
# @Last modified by: Felix Kramer
# @Last modified time: 2021-0
5
-2
3
T23:
55:51
+02:00
# @Last modified time: 2021-0
6
-2
2
T23:
01:04
+02:00
# @License: MIT
import
networkx
as
nx
...
...
kirchhoff/circuit_flow.py
View file @
da04cb7a
...
...
@@ -3,7 +3,7 @@
# @Email: kramer@mpi-cbg.de
# @Project: go-with-the-flow
# @Last modified by: Felix Kramer
# @Last modified time: 2021-06-
13T00:35:59
+02:00
# @Last modified time: 2021-06-
22T23:55:48
+02:00
# @License: MIT
import
random
as
rd
...
...
@@ -39,6 +39,14 @@ def initialize_flow_circuit_from_random(random_type='default',periods=10,sidelen
input_graph
=
init_random
.
init_graph_from_random
(
random_type
,
periods
,
sidelength
)
kirchhoff_graph
.
default_init
(
input_graph
)
return
kirchhoff_graph
def
setup_default_flow_circuit
(
dict_pars
):
kirchhoff_graph
=
initialize_flow_circuit_from_networkx
(
dict_pars
[
'plexus'
])
kirchhoff_graph
.
set_source_landscape
()
kirchhoff_graph
.
set_plexus_landscape
()
return
kirchhoff_graph
# class flow_circuit(kirchhoff_init.circuit,object):
class
flow_circuit
(
circuit
,
object
):
...
...
@@ -312,3 +320,26 @@ class flow_circuit(circuit,object):
print
(
'Warning, custom conductance values ill defined, setting default !'
)
self
.
init_plexus_default
()
# output
def
get_nodes_data
(
self
):
dn
=
pd
.
DataFrame
(
self
.
nodes
[
'source'
])
return
dn
def
get_edges_data
(
self
):
de
=
pd
.
DataFrame
(
self
.
edges
[[
'conductivity'
,
'flow_rate'
]])
de
[
'weight'
]
=
np
.
power
(
self
.
edges
[
'conductivity'
].
to_numpy
(),
0.25
)
return
de
def
plot_circuit
(
self
):
E
=
self
.
get_edges_data
()
V
=
self
.
get_nodes_data
()
fig
=
dx
.
plot_networkx
(
self
.
G
,
edge_list
=
self
.
list_graph_edges
,
node_list
=
self
.
list_graph_nodes
,
edge_data
=
E
,
node_data
=
V
)
return
fig
kirchhoff/circuit_flux.py
View file @
da04cb7a
...
...
@@ -3,7 +3,7 @@
# @Email: kramer@mpi-cbg.de
# @Project: go-with-the-flow
# @Last modified by: Felix Kramer
# @Last modified time: 2021-06-
13T17:04
:2
5
+02:00
# @Last modified time: 2021-06-
22T23:58
:2
9
+02:00
# @License: MIT
import
random
as
rd
...
...
@@ -226,3 +226,17 @@ class flux_circuit(flow_circuit,object):
print
(
'Warning, custom absorption values ill defined, setting default !'
)
self
.
init_geom_default
()
def
get_nodes_data
(
self
):
dn
=
pd
.
DataFrame
(
self
.
nodes
[[
'source'
,
'solute'
,
'concentration'
]])
return
dn
def
get_edges_data
(
self
):
de
=
pd
.
DataFrame
(
self
.
edges
[[
'conductivity'
,
'flow_rate'
,
'absorption'
,
'peclet'
,
'length'
]])
de
[
'weight'
]
=
np
.
power
(
self
.
edges
[
'conductivity'
].
to_numpy
(),
0.25
)
return
de
kirchhoff/circuit_init.py
View file @
da04cb7a
...
...
@@ -3,7 +3,7 @@
# @Email: kramer@mpi-cbg.de
# @Project: go-with-the-flow
# @Last modified by: Felix Kramer
# @Last modified time: 2021-0
5
-2
3
T23:
17
:4
8
+02:00
# @Last modified time: 2021-0
6
-2
2
T23:
26
:4
1
+02:00
# @License: MIT
# standard types
...
...
@@ -254,8 +254,9 @@ class circuit:
self
.
graph
=
new_parameters
# output
def
plot_circuit
(
self
):
fig
=
dx
.
plot_networkx
(
self
.
G
,
conductivity
=
self
.
edges
[
'conductivity'
],
flow_rate
=
self
.
edges
[
'flow_rate'
]
)
fig
=
dx
.
plot_networkx
(
self
.
G
,
edge_list
=
self
.
list_graph_edges
,
node_list
=
self
.
list_graph_nodes
)
return
fig
kirchhoff/draw_networkx.py
View file @
da04cb7a
...
...
@@ -3,7 +3,7 @@
# @Email: kramer@mpi-cbg.de
# @Project: go-with-the-flow
# @Last modified by: Felix Kramer
# @Last modified time: 2021-0
5
-2
3
T23:
17:48
+02:00
# @Last modified time: 2021-0
6
-2
2
T23:
55:54
+02:00
# @License: MIT
# standard types
...
...
@@ -20,17 +20,24 @@ def plot_networkx(input_graph,**kwargs):
'color_nodes'
:[
'#a845b5'
],
'color_edges'
:[
'#c762d4'
]
}
extra_data
=
pd
.
DataFrame
()
node_data
=
pd
.
DataFrame
()
edge_data
=
pd
.
DataFrame
()
for
k
,
v
in
kwargs
.
items
():
if
k
in
options
:
options
[
k
]
=
v
else
:
extra_data
[
k
]
=
v
print
(
extra_data
)
if
'node_data'
in
kwargs
:
for
sk
,
sv
in
kwargs
[
'node_data'
].
items
():
node_data
[
sk
]
=
sv
.
to_numpy
()
if
'edge_data'
in
kwargs
:
for
sk
,
sv
in
kwargs
[
'edge_data'
].
items
():
edge_data
[
sk
]
=
sv
.
to_numpy
()
fig
=
go
.
Figure
()
plot_graph_components
(
fig
,
options
,
input_graph
,
extra_data
)
add_traces_nodes
(
fig
,
options
,
input_graph
,
node_data
)
add_traces_edges
(
fig
,
options
,
input_graph
,
edge_data
)
fig
.
update_layout
(
showlegend
=
False
)
return
fig
...
...
@@ -48,63 +55,117 @@ def plot_networkx_dual(dual_graph,**kwargs):
options
[
k
]
=
v
fig
=
go
.
Figure
()
for
i
,
K
in
enumerate
(
dual_graph
.
layer
):
options
[
'network_id'
]
=
i
plot_graph_components
(
fig
,
options
,
K
.
G
)
node_data
=
K
.
get_nodes_data
()
edges_data
=
K
.
get_edges_data
()
add_traces_nodes
(
fig
,
options
,
K
.
G
,
node_data
)
add_traces_edges
(
fig
,
options
,
K
.
G
,
edges_data
)
fig
.
update_layout
(
showlegend
=
False
)
return
fig
# integrate traces into the figure
def
add_traces_edges
(
fig
,
options
,
input_graph
,
extra_data
):
idx
=
options
[
'network_id'
]
edge_mid_trace
=
get_edge_mid_trace
(
input_graph
,
extra_data
,
color
=
options
[
'color_edges'
][
idx
])
edge_invd_traces
=
get_edge_invd_traces
(
input_graph
,
extra_data
,
color
=
options
[
'color_edges'
][
idx
])
for
eit
in
edge_invd_traces
:
fig
.
add_trace
(
eit
)
fig
.
add_trace
(
edge_mid_trace
)
def
add_traces_nodes
(
fig
,
options
,
input_graph
,
extra_data
):
idx
=
options
[
'network_id'
]
node_trace
=
get_node_trace
(
input_graph
,
extra_data
,
color
=
options
[
'color_nodes'
][
idx
])
fig
.
add_trace
(
node_trace
)
#auxillary functions generating traces for nodes and edges
def
get_edge_
coords
(
input_graph
,
option
s
):
def
get_edge_
mid_trace
(
input_graph
,
extra_data
,
**
kwarg
s
):
pos
=
nx
.
get_node_attributes
(
input_graph
,
'pos'
)
options
=
{
'color'
:
'#888'
,
'dim'
:
3
}
for
k
,
v
in
kwargs
.
items
():
if
k
in
options
:
options
[
k
]
=
v
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
():
E
=
input_graph
.
edges
()
if
'edge_list'
in
options
:
E
=
options
[
'edge_list'
]
xyz_0
=
pos
[
edge
[
0
]]
xyz_1
=
pos
[
edge
[
1
]]
middle_node_trace
=
get_hover_scatter_from_template
(
options
)
for
i
in
range
(
options
[
'dim'
]):
XYZ
=
[[]
for
i
in
range
(
options
[
'dim'
])]
for
j
,
edge
in
enumerate
(
E
):
edge_xyz
[
i
].
append
(
xyz_0
[
i
])
edge_xyz
[
i
].
append
(
xyz_1
[
i
])
edge_xyz
[
i
].
append
(
None
)
XYZ_0
=
pos
[
edge
[
0
]]
XYZ_1
=
pos
[
edge
[
1
]]
return
edge_xyz
for
i
,
xi
in
enumerate
(
XYZ
):
xi
.
append
((
XYZ_0
[
i
]
+
XYZ_1
[
i
])
/
2.
)
def
get_edge_scatter
(
edge_xyz
,
extra_data
,
options
):
set_hover_info
(
middle_node_trace
,
XYZ
,
extra_data
)
mode
=
'none'
hover
=
''
return
middle_node_trace
def
set_hover_info
(
trace
,
XYZ
,
extra_data
):
tags
=
[
'x'
,
'y'
,
'z'
]
if
len
(
XYZ
)
<
3
:
tags
=
[
'x'
,
'y'
]
for
i
,
t
in
enumerate
(
tags
):
trace
[
t
]
=
XYZ
[
i
]
if
len
(
extra_data
.
keys
())
!=
0
:
mode
=
'text'
data
=
[
list
(
extra_data
[
c
])
for
c
in
extra_data
.
columns
]
hover
=
[
create_tag
(
vals
,
extra_data
.
columns
)
for
vals
in
list
(
zip
(
*
data
))]
text
=
[
create_tag
(
vals
,
extra_data
.
columns
)
for
vals
in
list
(
zip
(
*
data
))]
trace
[
'text'
]
=
text
else
:
trace
[
'hoverinfo'
]
=
'none'
def
get_hover_scatter_from_template
(
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
=
mode
,
hovertext
=
hover
,
mode
=
'lines'
)
middle_node_trace
=
go
.
Scatter3d
(
x
=
[],
y
=
[],
z
=
[],
text
=
[],
mode
=
'markers'
,
hoverinfo
=
'text'
,
opacity
=
0
,
marker
=
dict
(
color
=
options
[
'color'
])
)
else
:
edge_trace
=
go
.
Scatter
(
x
=
edge_xyz
[
0
],
y
=
edge_xyz
[
1
],
line
=
dict
(
width
=
5
,
color
=
options
[
'color'
]),
hoverinfo
=
mode
,
hovertext
=
hover
,
mode
=
'lines'
)
middle_node_trace
=
go
.
Scatter
(
x
=
[],
y
=
[],
text
=
[],
mode
=
'markers'
,
hoverinfo
=
'text'
,
marker
=
go
.
scatter
.
Marker
(
opacity
=
0
,
color
=
options
[
'color'
]
)
)
return
edg
e_trace
return
middle_nod
e_trace
def
get_edge_trace
(
input_graph
,
extra_data
,
**
kwargs
):
def
get_edge_
invd_
trace
s
(
input_graph
,
extra_data
,
**
kwargs
):
options
=
{
'color'
:
'#888'
,
...
...
@@ -114,10 +175,63 @@ def get_edge_trace(input_graph,extra_data, **kwargs):
if
k
in
options
:
options
[
k
]
=
v
edge_xyz
=
get_edge_coords
(
input_graph
,
options
)
edge_trace
=
get_edge_scatter
(
edge_xyz
,
extra_data
,
options
)
pos
=
nx
.
get_node_attributes
(
input_graph
,
'pos'
)
if
len
(
list
(
pos
.
values
())[
0
])
!=
options
[
'dim'
]:
options
[
'dim'
]
=
len
(
list
(
pos
.
values
())[
0
])
E
=
input_graph
.
edges
()
if
'edge_list'
in
options
:
E
=
options
[
'edge_list'
]
trace_list
=
[]
for
i
,
edge
in
enumerate
(
E
):
options
[
'weight'
]
=
5.
if
'weight'
in
extra_data
:
options
[
'weight'
]
=
extra_data
[
'weight'
][
i
]
trace
=
get_line_from_template
(
options
)
XYZ_0
=
input_graph
.
nodes
[
edge
[
0
]][
'pos'
]
XYZ_1
=
input_graph
.
nodes
[
edge
[
1
]][
'pos'
]
set_edge_info
(
trace
,
XYZ_0
,
XYZ_1
)
trace_list
.
append
(
trace
)
return
trace_list
def
set_edge_info
(
trace
,
XYZ_0
,
XYZ_1
):
tags
=
[
'x'
,
'y'
,
'z'
]
if
len
(
XYZ_0
)
<
3
:
tags
=
[
'x'
,
'y'
]
for
i
,
t
in
enumerate
(
tags
):
trace
[
t
]
=
[
XYZ_0
[
i
],
XYZ_1
[
i
],
None
]
def
get_line_from_template
(
options
):
if
options
[
'dim'
]
==
3
:
trace
=
go
.
Scatter3d
(
x
=
[],
y
=
[],
z
=
[],
mode
=
'lines'
,
line
=
dict
(
color
=
options
[
'color'
],
width
=
options
[
'weight'
]),
hoverinfo
=
'none'
)
return
edge_trace
else
:
trace
=
go
.
Scatter
(
x
=
[],
y
=
[],
mode
=
'lines'
,
line
=
dict
(
color
=
options
[
'color'
],
width
=
options
[
'weight'
]),
hoverinfo
=
'none'
)
return
trace
def
get_node_coords
(
input_graph
,
options
):
...
...
@@ -126,7 +240,12 @@ def get_node_coords(input_graph,options):
options
[
'dim'
]
=
len
(
list
(
pos
.
values
())[
0
])
node_xyz
=
[[]
for
i
in
range
(
options
[
'dim'
])]
for
node
in
input_graph
.
nodes
():
N
=
input_graph
.
nodes
()
if
'node_list'
in
options
:
N
=
options
[
'edge_list'
]
for
node
in
N
:
xyz_0
=
pos
[
node
]
...
...
@@ -138,11 +257,20 @@ def get_node_coords(input_graph,options):
def
get_node_scatter
(
node_xyz
,
extra_data
,
options
):
mode
=
'none'
hover
=
''
if
len
(
extra_data
.
keys
())
!=
0
:
mode
=
'text'
data
=
[
list
(
extra_data
[
c
])
for
c
in
extra_data
.
columns
]
hover
=
[
create_tag
(
vals
,
extra_data
.
columns
)
for
vals
in
list
(
zip
(
*
data
))]
if
options
[
'dim'
]
==
3
:
node_trace
=
go
.
Scatter3d
(
x
=
node_xyz
[
0
],
y
=
node_xyz
[
1
],
z
=
node_xyz
[
2
],
mode
=
'markers'
,
hoverinfo
=
'none'
,
hoverinfo
=
mode
,
hovertext
=
hover
,
marker
=
dict
(
size
=
2
,
line_width
=
2
,
...
...
@@ -152,7 +280,8 @@ def get_node_scatter(node_xyz,extra_data,options):
node_trace
=
go
.
Scatter
(
x
=
node_xyz
[
0
],
y
=
node_xyz
[
1
],
mode
=
'markers'
,
hoverinfo
=
'none'
,
hoverinfo
=
mode
,
hovertext
=
hover
,
marker
=
dict
(
size
=
2
,
line_width
=
2
,
...
...
@@ -184,11 +313,3 @@ def create_tag(vals,columns):
tag
+=
str
(
c
)
+
': '
+
str
(
vals
[
i
])
+
'<br>'
return
tag
# integrate traces into the figure
def
plot_graph_components
(
fig
,
options
,
input_graph
,
extra_data
):
idx
=
options
[
'network_id'
]
edge_trace
=
(
get_edge_trace
(
input_graph
,
extra_data
,
color
=
options
[
'color_edges'
][
idx
]))
node_trace
=
(
get_node_trace
(
input_graph
,
extra_data
,
color
=
options
[
'color_nodes'
][
idx
]))
fig
.
add_trace
(
edge_trace
)
fig
.
add_trace
(
node_trace
)
notebook/test_plot_networkx.ipynb
View file @
da04cb7a
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment