Skip to content

Commit

Permalink
Neat session.show.data model (#673)
Browse files Browse the repository at this point in the history
* updated docs

* implement show
  • Loading branch information
krista-t authored Oct 25, 2024
1 parent 6923f58 commit dd40c6e
Show file tree
Hide file tree
Showing 5 changed files with 903 additions and 23 deletions.
2 changes: 2 additions & 0 deletions cognite/neat/_session/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from ._prepare import PrepareAPI
from ._read import ReadAPI
from ._show import ShowAPI
from ._state import SessionState
from ._to import ToAPI
from .exceptions import intercept_session_exceptions
Expand All @@ -33,6 +34,7 @@ def __init__(
self.read = ReadAPI(self._state, client, verbose)
self.to = ToAPI(self._state, client, verbose)
self.prepare = PrepareAPI(self._state, verbose)
self.show = ShowAPI(self._state)

def verify(self) -> IssueList:
output = VerifyAnyRules("continue").try_transform(self._state.input_rule)
Expand Down
96 changes: 96 additions & 0 deletions cognite/neat/_session/_show.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from typing import Any, cast

import networkx as nx
from ipycytoscape import CytoscapeWidget # type: ignore
from IPython.display import display

from cognite.neat._rules.models.dms._rules import DMSRules
from cognite.neat._rules.models.entities._single_value import ViewEntity

from ._state import SessionState


class ShowAPI:
def __init__(self, state: SessionState) -> None:
self._state = state
self.data_model = ShowDataModelAPI(self._state)


class ShowDataModelAPI:
def __init__(self, state: SessionState) -> None:
self._state = state

def __call__(self) -> Any:
if self._state.last_verified_dms_rules:
digraph = self._generate_dms_di_graph()
widget = self._generate_widget()
widget.graph.add_graph_from_networkx(digraph)
return display(widget)

def _generate_dms_di_graph(self) -> nx.DiGraph:
"""Generate a DiGraph from the last verified DMS rules."""
G = nx.DiGraph()

nodes, edges = self._generate_dms_rules_nodes_and_edges()
G.add_nodes_from(nodes)
G.add_edges_from(edges)
for node in G.nodes:
G.nodes[node]["label"] = node

return G

def _generate_dms_rules_nodes_and_edges(self) -> tuple[list[str], list[tuple[str, str, dict]]]:
"""Generate nodes and edges for the last verified DMS rules for DiGraph."""

nodes = []
edges = []

for prop_ in cast(DMSRules, self._state.last_verified_dms_rules).properties:
nodes.append(prop_.view.suffix)

if prop_.connection and isinstance(prop_.value_type, ViewEntity):
label = f"{prop_.property_} [{0 if prop_.nullable else 1}..{ '' if prop_.is_list else 1}]"
edges.append((prop_.view.suffix, prop_.value_type.suffix, {"label": label}))

for view in cast(DMSRules, self._state.last_verified_dms_rules).views:
nodes.append(view.view.suffix)

if view.implements:
for implement in view.implements:
edges.append((view.view.suffix, implement.suffix, {"label": "implements"}))

return nodes, edges

def _generate_widget(self):
"""Generates an empty a CytoscapeWidget."""
widget = CytoscapeWidget()
widget.layout.height = "500px"

widget.set_style(
[
{
"selector": "node",
"css": {
"content": "data(label)",
"text-valign": "center",
"color": "black",
"background-color": "#33C4FF",
"font-size": "10px",
"width": "mapData(score, 0, 1, 10, 50)",
"height": "mapData(score, 0, 1, 10, 50)",
},
},
{
"selector": "edge",
"style": {
"width": 1,
"target-arrow-shape": "triangle",
"curve-style": "bezier",
"label": "data(label)",
"font-size": "8px",
},
},
]
)

return widget
2 changes: 1 addition & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Changes are grouped as follows:
- Support for `xsd:decimal` which is now mapped to `float64` in DMS rules
- Added RDF based readers for `NeatSession`
- `NeatSession.read.rdf.examples.nordic44`

- `NeatSession.show.data_model` show data model in UI

### Removed
- State on DataType stored in `_dms_loaded` attribute
Expand Down
Loading

0 comments on commit dd40c6e

Please sign in to comment.