Skip to content

Commit

Permalink
Added code to generate mermaid graphs for plumed inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Gareth Aneurin Tribello authored and Gareth Aneurin Tribello committed May 14, 2024
1 parent 0e7a5cd commit 63a5235
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
29 changes: 28 additions & 1 deletion PlumedToHTML/PlumedToHTML.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,34 @@ def get_html( inpt, name, outloc, tested, broken, plumedexe, usejson=None, actio
if not soup.find("div",{"id": switchval + "_short"} ) : raise Exception("Generated html is invalid as could not find " + switchval + "_short")
else : raise Exception("Could not find toggler command for " + val)
return html


def get_mermaid( inpt, force ) :
"""
Generate the mermaid graph showing how data passes through PLUMED input file
Keyword arguments:
inpt -- A string containing the PLUMED input
force -- Bool that if true ensures we show the graph for the backwards pass through the action list
"""
# Write the plumed input to a file
iff = open( "mermaid_plumed.dat", "w+")
iff.write(inpt+ "\n")
iff.close()
# Now check the input is OK
broken = test_plumed( "plumed", "mermaid_plumed.dat", header="" )
if broken!=0 : raise Exception("invalid plumed input file -- cannot create mermaid graph")
# Run mermaid
cmd = ['plumed', 'show_graph', '--plumed', 'mermaid_plumed.dat', '--out', 'mermaid.md']
if force : cmd.append("--force")
plumed_out = subprocess.run(cmd, capture_output=True, text=True )
mf = open("mermaid.md")
mermaid = mf.read()
mf.close()
# Remove stuff that was created
os.remove("mermaid_plumed.dat")
os.remove("mermaid.md")
return mermaid

def resolve_includes( srcdir, inpt, foundfiles ) :
if not foundfiles or "INCLUDE" not in inpt : return foundfiles, inpt

Expand Down
2 changes: 1 addition & 1 deletion PlumedToHTML/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .PlumedToHTML import test_plumed, test_and_get_html, get_html, get_html_header, compare_to_reference
from .PlumedToHTML import test_plumed, test_and_get_html, get_html, get_html_header, compare_to_reference, get_mermaid
41 changes: 41 additions & 0 deletions PlumedToHTML/tests/test_mermaid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from unittest import TestCase

import os
import subprocess
from PlumedToHTML import get_mermaid

class TestPlumedToHTMLMermaid(TestCase):
def testNormalGraph(self) :
inpt="d1: DISTANCE ATOMS=1,2\n PRINT ARG=d1 FILE=colvar"
mermaid_this = get_mermaid( inpt, False )
iff = open("test_mermaid.dat","w+")
iff.write(inpt)
iff.close()
cmd = ['plumed', 'show_graph', '--plumed', 'test_mermaid.dat', '--out', 'test_mermaid.md']
plumed_out = subprocess.run(cmd, capture_output=True, text=True )
mf = open("test_mermaid.md")
mermaid_pp = mf.read()
mf.close()
mermaid_this = get_mermaid( inpt, False )
os.remove("test_mermaid.dat")
os.remove("test_mermaid.md")
self.assertTrue( mermaid_pp == mermaid_this )

def testForceGraph(self) :
inpt="d1: DISTANCE ATOMS=1,2\n rr: RESTRAINT ARG=d1 KAPPA=10 AT=1"
mermaid_this = get_mermaid( inpt, True )
iff = open("test_mermaid.dat","w+")
iff.write(inpt)
iff.close()
cmd = ['plumed', 'show_graph', '--plumed', 'test_mermaid.dat', '--out', 'test_mermaid.md', '--force']
plumed_out = subprocess.run(cmd, capture_output=True, text=True )
mf = open("test_mermaid.md")
mermaid_pp = mf.read()
mf.close()
mermaid_this = get_mermaid( inpt, True )
os.remove("test_mermaid.dat")
os.remove("test_mermaid.md")
self.assertTrue( mermaid_pp == mermaid_this )



2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setuptools.setup(
name='PlumedToHTML',
version='0.65',
version='0.66',
author="Gareth Tribello",
author_email="[email protected]",
description="A package for creating pretified HTML for PLUMED files",
Expand Down

0 comments on commit 63a5235

Please sign in to comment.