Skip to content

Commit

Permalink
v0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
davidvlaminck committed Dec 14, 2023
1 parent 7c9355e commit 35b796f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
10 changes: 5 additions & 5 deletions SampleCode/pyvis_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from otlmow_model.Classes.Onderdeel.Bevestiging import Bevestiging
from otlmow_model.Classes.Onderdeel.Verkeersregelaar import Verkeersregelaar
from otlmow_model.Classes.Onderdeel.Wegkantkast import Wegkantkast
from otlmow_model.Helpers.RelationCreator import create_relation
from otlmow_model.OtlmowModel.Classes.Onderdeel.Bevestiging import Bevestiging
from otlmow_model.OtlmowModel.Classes.Onderdeel.Verkeersregelaar import Verkeersregelaar
from otlmow_model.OtlmowModel.Classes.Onderdeel.Wegkantkast import Wegkantkast
from otlmow_model.OtlmowModel.Helpers.RelationCreator import create_relation

from otlmow_visuals.PyVisWrapper import PyVisWrapper

Expand All @@ -13,4 +13,4 @@
bevestiging = create_relation(source=kast, target=vr, relation_type=Bevestiging)
assets = (vr, kast, bevestiging)

PyVisWrapper().show(assets)
PyVisWrapper().show(assets, launch_html=False)
29 changes: 17 additions & 12 deletions otlmow_visuals/PyVisWrapper.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import webbrowser
from pathlib import Path
from random import choice

from IPython.display import display, HTML
from otlmow_model.OtlmowModel.BaseClasses.OTLObject import OTLObject
from otlmow_model.OtlmowModel.Classes.ImplementatieElement.NietDirectioneleRelatie import NietDirectioneleRelatie
from otlmow_model.OtlmowModel.Classes.ImplementatieElement.RelatieObject import RelatieObject
from otlmow_model.OtlmowModel.Helpers.OTLObjectHelper import is_relation, is_directional_relation
from pyvis import network as networkx


Expand Down Expand Up @@ -143,22 +143,29 @@ def __init__(self, notebook_mode: bool = False):

def show(self, list_of_objects: [OTLObject], html_path: Path = Path('example.html'), launch_html: bool = True):
g = networkx.Network(directed=True)
nodes_created = self.create_nodes(g, list_of_objects)
self.create_edges(g, list_of_objects=list_of_objects, nodes=nodes_created)

assets = []
relations = []
for o in list_of_objects:
if is_relation(o):
relations.append(o)
else:
assets.append(o)

nodes_created = self.create_nodes(g, assets)
self.create_edges(g, list_of_objects=relations, nodes=nodes_created)
options = ('options = {"nodes": {"font":{"bold":{"size": 18}}}, "interaction": {"dragView": true}, "physics": '
'{"solver": "barnesHut", "stabilization": true, "barnesHut" : {"centralGravity" : 0, '
'"springLength" : 100, "avoidOverlap" : 0.05,"gravitationalConstant" : -2500}}}')
# see https://visjs.github.io/vis-network/docs/network/#options => {"configure":{"showButton":true}}
g.set_options(options)

g.show(str(html_path), notebook=self.notebook_mode)
g.write_html(str(html_path), notebook=self.notebook_mode)
self.modify_html(Path(html_path))
if not self.notebook_mode and launch_html:
display(HTML(str(html_path)))
webbrowser.open(str(html_path))

def create_nodes(self, g, list_of_objects: [OTLObject]) -> [OTLObject]:

list_of_objects = filter(lambda o: not isinstance(o, RelatieObject), list_of_objects)
list_of_objects = remove_duplicates_in_iterable_based_on_asset_id(list_of_objects)

nodes = []
Expand Down Expand Up @@ -186,10 +193,8 @@ def create_nodes(self, g, list_of_objects: [OTLObject]) -> [OTLObject]:
return nodes

def create_edges(self, g, list_of_objects: [OTLObject], nodes) -> None:
relaties = filter(lambda o: isinstance(o, RelatieObject), list_of_objects)
asset_ids = list(map(lambda x: x.assetId.identificator, nodes))

relaties = remove_duplicates_in_iterable_based_on_asset_id(relaties)
relaties = remove_duplicates_in_iterable_based_on_asset_id(list_of_objects)

for relatie in relaties:
if relatie.bronAssetId.identificator in asset_ids and relatie.doelAssetId.identificator in asset_ids:
Expand All @@ -198,7 +203,7 @@ def create_edges(self, g, list_of_objects: [OTLObject], nodes) -> None:
to=relatie.doelAssetId.identificator,
color=self.map_relation_to_color(relatie),
width=2)
if isinstance(relatie, NietDirectioneleRelatie):
if not is_directional_relation(relatie):
g.add_edge(to=relatie.bronAssetId.identificator,
source=relatie.doelAssetId.identificator,
color=self.map_relation_to_color(relatie),
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "otlmow_visuals"
version = "0.3"
version = "0.4"
authors = [{name = "David Vlaminck", email = "david.vlaminck@mow.vlaanderen.be"}]
readme = "README.md"
license = {file = "LICENSE"}
Expand All @@ -29,7 +29,7 @@ classifiers = [
requires-python = ">=3.8"
dependencies = [
'pyvis >= 0.3.2',
'otlmow_model >= 2.9.2',
'otlmow_model >= 2.9.5.0',
]

[tool.setuptools.packages.find]
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
otlmow-model>=2.9.2
otlmow-model>=2.9.5.0
pyvis>=0.3.2
pytest~=7.3.1
ipython>=8.0.0

0 comments on commit 35b796f

Please sign in to comment.