Skip to content

Commit

Permalink
Refs #11418. Added failing test for PyExec
Browse files Browse the repository at this point in the history
Imported code from other branch for parsing.
  • Loading branch information
Michael Wedel committed Mar 24, 2015
1 parent 5f1af07 commit fd01484
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 2 deletions.
Expand Up @@ -3,8 +3,113 @@
from mantid.simpleapi import *
from mantid.api import *

from pyparsing import *

import os


class PoldiCompound(object):
def __init__(self, name, content, tolerance, elements):
self._name = name
self._content = content
self._tolerance = tolerance
self._spacegroup = ""
self._atomString = ""
self._latticeDict = ""

self.assign(elements)

def assign(self, elements):
for c in elements:
if c[0] == "atoms":
self._atomString = c[1][0]
elif c[0] == "lattice":
pNames = ['a', 'b', 'c', 'alpha', 'beta', 'gamma']
self._latticeDict = dict(zip(pNames, c[1:]))
elif c[0] == "spacegroup":
self._spacegroup = c[1]

def getAtoms(self):
return self._atomString

def getLatticeDict(self):
return self._latticeDict

def getSpaceGroup(self):
return self._spacegroup

def getContent(self):
return self._content

def getTolerance(self):
return self._tolerance

def getName(self):
return self._name


class PoldiCrystalFileParser(object):
elementSymbol = Word(alphas, exact=2)
integerNumber = Word(nums)
decimalSeparator = Literal('.')
floatNumber = Combine(
integerNumber +
Optional(decimalSeparator + Optional(integerNumber))
)

atomLine = Combine(
elementSymbol + Suppress(White()) +
delimitedList(floatNumber, delim=White()),
joinString=' '
)

keyValueSeparator = Suppress(Literal(":"))

atomsGroup = Group(CaselessLiteral("atoms") + keyValueSeparator + nestedExpr(
opener="{", closer="}",
content=Combine(
delimitedList(atomLine, delim='\n'),
joinString=";")))

unitCell = Group(CaselessLiteral("lattice") + keyValueSeparator + delimitedList(
floatNumber, delim=White()))

spaceGroup = Group(CaselessLiteral("spacegroup") + keyValueSeparator + Word(
alphanums + "-" + ' '))

compoundContent = Each([atomsGroup, unitCell, spaceGroup])

compoundName = Word(alphanums)

compound = Group(Suppress(CaselessLiteral("compound")) + Suppress(White()) + \
compoundName + Suppress(White()) + floatNumber + \
Suppress(White()) + floatNumber + \
nestedExpr(opener='{', closer='}', content=compoundContent))

comment = Suppress(Literal('#') + restOfLine)

compounds = OneOrMore(compound).ignore(comment)

def __call__(self, contentString):
parsedContent = None

if os.path.isfile(contentString):
parsedContent = self._parseFile(contentString)
else:
parsedContent = self._parseString(contentString)

return [PoldiCompound(*x[:3]) for x in parsedContent]

def _parseFile(self, filename):
return self.compounds.parseFile(filename)

def _parseString(self, stringContent):
return self.compounds.parseString(stringContent)


class PoldiLoadCrystalData(PythonAlgorithm):
_parser = PoldiCrystalFileParser()

def category(self):
return "SINQ\\POLDI"

Expand All @@ -16,10 +121,28 @@ def summary(self):
"with the expected reflections.")

def PyInit(self):
pass
self.declareProperty(
FileProperty(name="InputFile",
defaultValue="",
action=FileAction.Load,
extensions=["dat"]),
doc="A file with POLDI crystal data.")

self.declareProperty("LatticeSpacingMin", 0.0,
direction=Direction.Input,
doc="Lowest allowed lattice spacing.")

self.declareProperty("LatticeSpacingMax", 0.0,
direction=Direction.Input,
doc="Lowest allowed lattice spacing.")

self.declareProperty(
WorkspaceProperty(name="OutputWorkspace",
defaultValue="", direction=Direction.Output),
doc="WorkspaceGroup with reflection tables.")


def PyExec(self):
pass


AlgorithmFactory.subscribe(PoldiLoadCrystalData)
@@ -1,17 +1,39 @@
import unittest
from testhelpers import assertRaisesNothing
from testhelpers.tempfile_wrapper import TemporaryFileHelper

from tempfile import NamedTemporaryFile

from mantid.kernel import *
from mantid.api import *
from mantid.simpleapi import *

import os


class PoldiLoadCrystalDataTest(unittest.TestCase):
testname = None

def __init__(self, *args):
unittest.TestCase.__init__(self, *args)

def test_Init(self):
assertRaisesNothing(self, AlgorithmManager.create, ("PoldiLoadCrystalData"))

def test_FileOneCompoundOneAtom(self):
fileHelper = TemporaryFileHelper("""Compound Silicon 1.0 0.01 {
Lattice: 5.43 5.43 5.43 90.0 90.0 90.0
Spacegroup: F d -3 m
Atoms: {
Si 0 0 0 1.0 0.05
}
}""")

ws = PoldiLoadCrystalData(fileHelper.getName(), 0.75, 10.0)

self.assertEquals(ws.rowCount(), 18)



if __name__ == '__main__':
unittest.main()

0 comments on commit fd01484

Please sign in to comment.