Skip to content

Commit

Permalink
Added AST visualization functionality based on d3 (#178)
Browse files Browse the repository at this point in the history
  - nmodl.ast now has ast_view function that visualize in memory AST
    representation using d3.js based radial tree
  - necessary html, css and js files are added
  - get_nmodl_example_dir is removed and taken case by python modules

Todo: utility tools should be outside the main repo (see #190)
  • Loading branch information
pramodk authored May 7, 2019
1 parent f378285 commit 42058f5
Show file tree
Hide file tree
Showing 12 changed files with 413 additions and 32 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ set(PROJECT_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR})
# generate file with version number from git and nrnunits.lib file path
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/config/config.cpp.in
${CMAKE_CURRENT_BINARY_DIR}/config.cpp @ONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/nmodl/config.py.in ${PROJECT_SOURCE_DIR}/nmodl/config.py
@ONLY)

# generate Doxyfile with correct source paths
configure_file(${PROJECT_SOURCE_DIR}/docs/Doxyfile.in ${PROJECT_SOURCE_DIR}/docs/Doxyfile)
Expand Down
32 changes: 32 additions & 0 deletions nmodl/ast.py
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
from ._nmodl.ast import * # noqa
from .config import *

def ast_view(nmodl_ast):
"""Visualize given NMODL AST by converting to JSON form"""
from ._nmodl import to_json
from distutils.dir_util import copy_tree
import getpass
import json
import os
import tempfile
import webbrowser

installed_viz_tool = os.path.join(PROJECT_INSTALL_DIR, "share", "viz")
if os.path.exists(installed_viz_tool):
viz_tool_dir = installed_viz_tool
else:
viz_tool_dir = os.path.join(PROJECT_SOURCE_DIR, "share", "viz")

work_dir = os.path.join(tempfile.gettempdir(), getpass.getuser(), "nmodl")

# first copy necessary files to temp work directory
copy_tree(viz_tool_dir, work_dir)

# prepare json data
with open(os.path.join(work_dir, 'ast.js'), 'w') as outfile:
json_data = json.loads(to_json(nmodl_ast, True, True, True))
outfile.write('var astRoot = %s;' % json.dumps(json_data))

# open browser with ast
url = 'file://' + os.path.join(work_dir, "index.html")
webbrowser.open(url, new=1, autoraise=True)

2 changes: 2 additions & 0 deletions nmodl/config.py.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PROJECT_SOURCE_DIR="@PROJECT_SOURCE_DIR@"
PROJECT_INSTALL_DIR="@CMAKE_INSTALL_PREFIX@"
12 changes: 12 additions & 0 deletions nmodl/dsl.py
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
from ._nmodl import *
from .config import *


def example_dir():
import os
"""Return directory containing NMODL examples"""
installed_example = os.path.join(PROJECT_INSTALL_DIR, "share", "example")
if os.path.exists(installed_example):
return installed_example
else:
return os.path.join(PROJECT_SOURCE_DIR, "share", "example")

51 changes: 51 additions & 0 deletions share/viz/css/tree.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#ast {
position: fixed;
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
}

.node {
cursor: pointer;
}

.node circle {
fill: #ffc8c8 !important;
stroke: #c57bc4;
stroke-width: 2.5px;
}

.node text {
font-size: 12px !important;
font-family: sans-serif;
fill: #1f1f92;
}

.link {
fill: none;
stroke: #DCA5DB;
stroke-width: 3px;
}

.templink {
fill: none;
stroke: red;
stroke-width: 3px;
}

.dots {
cursor: pointer;
}

.tooltip {
background: #23298aeb;
white-space: pre;
padding: 10px;
border-radius: 3px;
color: white;
font-size: 13px;
font-family: "Monaco";
position: "absolute";
z-index: 10;
}
18 changes: 18 additions & 0 deletions share/viz/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<title>NMODL AST Visualization</title>
<link rel="stylesheet" href="css/tree.css">
</head>

<body>
<div id="ast"></div>

<script src="js/d3.min.js"></script>
<script src="ast.js"></script>
<script src="js/tree.js"></script>
</body>

</html>
5 changes: 5 additions & 0 deletions share/viz/js/d3.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 42058f5

Please sign in to comment.