Dash Cytoscape⑥(ノードの配置方法)

ノードの配置方法

Cytoscapeクラスでは、インスタンス生成時の引数layoutに辞書型のデータを設定することにより、ノードの配置方法を指定できます。

レイアウトに指定できる配置方法の種類は以下の通りです。

配置方法 説明
preset 各ノードの座標を手動で指定する。
 position:X,Y座標
grid 格子状にノードを配置する。
 row:格子の行数
 cols:格子の列数
circle 円状にノードを配置する。
 radius:円の半径
 startAngle:ノードの開始位置の角度(弧度法)
 sweep:最初のノードと最後のノードの間の角度(弧度法)
concentric 同心円状にノードを配置する。
 startAngle:ノードの開始位置の角度(弧度法)
 sweep:最初のノードと死後のノードの間の角度(弧度法)
breadthfirst 階層的にノードを配置する。
 roots:ルートとなるノードのID
 circle:描画モード ( top-down / circle )
cose 物理シミュレーションに基づいてノードを配置する。複合グラフが考慮される。
 gravity:物理シミュレーション時に使われる重力の値
random ランダムな位置にノードを配置する。

次回から、それぞれの配置方法を使ってネットワーク図を表示していきます。

Dash Cytoscape⑤(複合ノード)

複合ノード

複合ノード(Compoundノード)を使うと、ノード間に親子関係を作ることができます。

子ノードの “parent”キーに親ノードのIDを指定して親子関係を定義します。

[ソースコード]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import dash
import dash_cytoscape as cyto
import dash_html_components as html

app = dash.Dash(__name__)

app.layout = html.Div([
cyto.Cytoscape(
id='cytoscape-compound',
layout={'name': 'preset'},
style={'width': '100%', 'height': '450px'},
stylesheet=[
{
'selector': 'node',
'style': {'content': 'data(label)'}
},
{
'selector': '.countries',
'style': {'width': 5}
},
{
'selector': '.cities',
'style': {'line-style': 'dashed'}
}
],
elements=[
# Parent Nodes
{
'data': {'id': 'us', 'label': 'United States'}
},
{
'data': {'id': 'can', 'label': 'Canada'}
},

# Children Nodes
{
'data': {'id': 'nyc', 'label': 'New York', 'parent': 'us'},
'position': {'x': 100, 'y': 100}
},
{
'data': {'id': 'sf', 'label': 'San Francisco', 'parent': 'us'},
'position': {'x': 100, 'y': 200}
},
{
'data': {'id': 'mtl', 'label': 'Montreal', 'parent': 'can'},
'position': {'x': 400, 'y': 100}
},

# Edges
{
'data': {'source': 'can', 'target': 'us'},
'classes': 'countries'
},
{
'data': {'source': 'nyc', 'target': 'sf'},
'classes': 'cities'
},
{
'data': {'source': 'sf', 'target': 'mtl'},
'classes': 'cities'
}
]
)
])

if __name__ == '__main__':
app.run_server(debug=True)

[ブラウザで表示]

親ノード(United States / Canada)のサイズは、子ノードに合わせて自動的に計算されます。

子ノードを移動させると、その親ノードの範囲も自動で変わります。

また親ノードを移動させるとその親ノードに属する子ノードもまとめて移動します。

Dash Cytoscape④(スタイル設定)

スタイル設定

ネットワークの関係を把握したいとき、ノート間の重みによってエッジの重さ(大きさ)を変えたり、同じ属性を持つノードの色や形を統一したりすることで、ネットワーク構造をより把握しやすくなります。

スタイル設定(セレクタ文字列)

Cytoscapeクラスでは、インスタンス生成時に引数stylesheetにスタイルを記述することで、ノードやエッジにスタイル(色・幅・形など)を適用できます。

スタイルの設定手順は下記の通りです。

  1. 引数stylesheetに、スタイル設定のリストを記述する。
    スタイル設定は複数指定でき、個々のスタイル設定は “selector”キーで指定します。
  2. “selector”キーには、スタイルの適用範囲を文字列で指定する。
  3. 各要素の “classes”キーに、2で定義した “selector”キーを設定する。
    複数の “selector”を設定する場合は、半角スペースで区切って設定する。

[ソースコード]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import dash
import dash_cytoscape as cyto
import dash_html_components as html

app = dash.Dash(__name__)

my_stylesheet = [
# Group selectors
{
'selector': 'node',
'style': {
'content': 'data(label)'
}
},
# Class selectors
{
'selector': '.red',
'style': {
'background-color': 'red',
'line-color': 'red'
}
},
{
'selector': '.triangle',
'style': {
'shape': 'triangle'
}
}
]

app.layout = html.Div([
cyto.Cytoscape(
id='cytoscape-elements-classes',
layout={'name': 'preset'},
style={'width': '100%', 'height': '400px'},
stylesheet=my_stylesheet,
elements=[
{
'data': {'id': 'one', 'label': 'Modified Color'},
'position': {'x': 75, 'y': 75},
'classes': 'red' # Single class
},
{
'data': {'id': 'two', 'label': 'Modified Shape'},
'position': {'x': 75, 'y': 200},
'classes': 'triangle' # Single class
},
{
'data': {'id': 'three', 'label': 'Both Modified'},
'position': {'x': 200, 'y': 75},
'classes': 'red triangle' # Multiple classes
},
{
'data': {'id': 'four', 'label': 'Regular'},
'position': {'x': 200, 'y': 200}
},
{'data': {'source': 'one', 'target': 'two'}, 'classes': 'red'},
{'data': {'source': 'two', 'target': 'three'}},
{'data': {'source': 'three', 'target': 'four'}, 'classes': 'red'},
{'data': {'source': 'two', 'target': 'four'}},
]
)
])

if __name__ == '__main__':
app.run_server(debug=True)

[ブラウザで表示]

各要素に対して、色や形を指定することができました。

Dash Cytoscape③(ブール型属性)

ブール型属性

ブール型属性を設定すると、ネットワーク図のノードを選択できなくしたり、移動できなくしたりすることができます。

  • locked属性
    Trueを設定すると、移動できなくなる。選択は可能。
  • selected属性
    Trueを設定すると、初期状態で選択された状態になる。他のノードを選択すると非選択状態になる。移動は可能。
  • selectable属性
    Falseを設定すると、選択状態にできなくなる。移動は可能。
  • grabbable属性
    Falseを設定すると、移動できなくなる。選択状態にはできる(?)

[ソースコード]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import dash
import dash_cytoscape as cyto
import dash_html_components as html

app = dash.Dash(__name__)

app.layout = html.Div([
cyto.Cytoscape(
id='cytoscape-elements-boolean',
layout={'name': 'preset'},
style={'width': '100%', 'height': '400px'},
elements=[
{
'data': {'id': 'one', 'label': 'Locked'},
'position': {'x': 75, 'y': 75},
'locked': True
},
{
'data': {'id': 'two', 'label': 'Selected'},
'position': {'x': 75, 'y': 200},
'selected': True
},
{
'data': {'id': 'three', 'label': 'Not Selectable'},
'position': {'x': 200, 'y': 75},
'selectable': False
},
{
'data': {'id': 'four', 'label': 'Not grabbable'},
'position': {'x': 200, 'y': 200},
'grabbable': False
},
{'data': {'source': 'one', 'target': 'two'}},
{'data': {'source': 'two', 'target': 'three'}},
{'data': {'source': 'three', 'target': 'four'}},
{'data': {'source': 'two', 'target': 'four'}},
]
)
])

if __name__ == '__main__':
app.run_server(debug=True)

[ブラウザで表示]

設定した属性値通りに、ノードによって選択できなくなったり、移動できなくなったりしていることが確認できます。

Dash Cytoscape②(基本的なネットワーク図)

基本的なネットワーク図

Dash Cytoscapeを使って、基本的なネットワーク図を描いてみます。

[ソースコード]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import dash
import dash_cytoscape as cyto
import dash_html_components as html

app = dash.Dash(__name__)

app.layout = html.Div([
cyto.Cytoscape(
id='cytoscape-two-nodes',
layout={'name': 'preset'},
style={'width': '100%', 'height': '400px'},
elements=[
{'data': {'id': 'one', 'label': 'Node 1'}, 'position': {'x': 75, 'y': 75}},
{'data': {'id': 'two', 'label': 'Node 2'}, 'position': {'x': 200, 'y': 200}},
{'data': {'source': 'one', 'target': 'two'}}
]
)
])

if __name__ == '__main__':
app.run_server(debug=True)

[ブラウザで表示]

ノードが2つと各ノードをでつないだ基本的なネットワーク図が表示されました。

ノードやノード間の線は選択することができ、ドラッグするとそれぞれの位置を移動することができます。

また、スクロールを行うと拡大・縮小して表示することができます。

Dash Cytoscape①(説明/インストール)

Dash Cytoscape

Dash Cytoscapeとは、ネットワークの可視化を実現するためのDashコンポーネントです。

Dash Cytoscape - https://dash.plotly.com/cytoscape

以下のようなインタラクティブな可視化や操作を行うことができます

  • ネットワーク図を拡大・縮小することによって、ネットワークの全体と部分を行き来しやすくする。
  • ノードをクリックすることで、隣接するノードを強調する。
  • ボタンやドロップダウンなどのUIを使って、ノードの配置やスタイルを動的に変更する。
  • ノードやエッジを移動させることによって、ネットワークの構造を確認しやする。

Dash Cytoscapeのインストール

Dash Cytoscapeを使うためには、下記のコマンドを実行しインストールを行います。

[インストール]

1
pip install dash-cytoscape==0.2.0

次回は、Dash Cytoscapeを使って基本的なネットワーク図の描画を行います。

Dash DAQ⑯(ToggleSwitchコンポーネント)

Dash DAQ

Dash DAQでは、データの収集・管理を行うコンポーネントを提供しています。

スイッチや計器に似た可視化ツールが用意されており、データを変化させる管理UIを作成できます。

Dash Enterprise - https://dash.plotly.com/dash-daq

ToggleSwitchコンポーネント

ToggleSwitchコンポーネントを使うと、ブール型のトグルスイッチを表示することができます。

初期値はvalueで設定します。(11行目)

[ソースコード]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import dash
from dash.dependencies import Input, Output
import dash_daq as daq
import dash_html_components as html

app = dash.Dash(__name__)

app.layout = html.Div([
daq.ToggleSwitch(
id='my-toggle-switch',
value=False
),
html.Br(),
html.Div(id='my-toggle-switch-output')
])

@app.callback(
Output('my-toggle-switch-output', 'children'),
Input('my-toggle-switch', 'value')
)
def update_output(value):
return 'The switch is {}.'.format(value)

if __name__ == '__main__':
app.run_server(debug=True)

[ブラウザで表示]

トグルスイッチが表示されました。

トグルスイッチをクリックすると、オンオフが切り替わることを確認できます。

Dash DAQ⑮(Thermometerコンポーネント)

Dash DAQ

Dash DAQでは、データの収集・管理を行うコンポーネントを提供しています。

スイッチや計器に似た可視化ツールが用意されており、データを変化させる管理UIを作成できます。

Dash Enterprise - https://dash.plotly.com/dash-daq

Thermometerコンポーネント

Thermometerコンポーネントを使うと、温度計型の計器を表示することができます。

初期値をvalue、最小値をmin、最大値をmaxでそれぞれ設定します。(12~14行目)

[ソースコード]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import dash
from dash.dependencies import Input, Output
import dash_daq as daq
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash(__name__)

app.layout = html.Div([
daq.Thermometer(
id='my-thermometer-1',
value=5,
min=0,
max=10,
style={
'margin-bottom': '5%'
}
),
html.Br(),
dcc.Slider(
id='thermometer-slider-1',
value=5,
min=0,
max=10,

),
])

@app.callback(
Output('my-thermometer-1', 'value'),
Input('thermometer-slider-1', 'value')
)
def update_thermometer(value):
return value

if __name__ == '__main__':
app.run_server(debug=True)

[ブラウザで表示]

温度計型の計器が表示されました。

スライドバーで数値を変更すると、温度計内の水色のエリアが変わることを確認できます。

Dash DAQ⑭(Tankコンポーネント)

Dash DAQ

Dash DAQでは、データの収集・管理を行うコンポーネントを提供しています。

スイッチや計器に似た可視化ツールが用意されており、データを変化させる管理UIを作成できます。

Dash Enterprise - https://dash.plotly.com/dash-daq

Tankコンポーネント

Tankコンポーネントを使うと、タンク型の計器を表示することができます。

初期値をvalue、最小値をmin、最大値をmaxでそれぞれ設定します。(13~15行目)

[ソースコード]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import dash
from dash.dependencies import Input, Output
import dash_daq as daq
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash(__name__)

app.layout = html.Div([
html.Br(),
daq.Tank(
id='my-tank-1',
value=5,
min=0,
max=10,
style={'margin-left': '50px'}
),
html.Br(),
dcc.Slider(
id='tank-slider-1',
value=5,
min=0,
max=10,
),
])

@app.callback(Output('my-tank-1', 'value'), Input('tank-slider-1', 'value'))
def update_tank(value):
return value

if __name__ == '__main__':
app.run_server(debug=True)

[ブラウザで表示]

タンク型の計器が表示されました。

スライドバーで数値を変更すると、タンク内の水色のエリアが変更されることを確認できます。

Dash DAQ⑬(StopButtonコンポーネント)

Dash DAQ

Dash DAQでは、データの収集・管理を行うコンポーネントを提供しています。

スイッチや計器に似た可視化ツールが用意されており、データを変化させる管理UIを作成できます。

Dash Enterprise - https://dash.plotly.com/dash-daq

StopButtonコンポーネント

StopButtonコンポーネントを使うと、ストップボタンを表示することができます。

[ソースコード]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import dash
from dash.dependencies import Input, Output
import dash_daq as daq
import dash_html_components as html

app = dash.Dash(__name__)

app.layout = html.Div([
daq.StopButton(
id='my-stop-button-1',
label='Default',
n_clicks=0
),
html.Br(),
html.Div(id='stop-button-output-1')
])

@app.callback(
Output('stop-button-output-1', 'children'),
Input('my-stop-button-1', 'n_clicks')
)
def update_output(n_clicks):
return 'The stop button has been clicked {} times.'.format(n_clicks)

if __name__ == '__main__':
app.run_server(debug=True)

[ブラウザで表示]

ストップボタンが表示されました。

ストップボタンをクリックするたびに、ボタン下にあるラベルのクリック回数が更新されることを確認できます。