File: mxParser.py

package info (click to toggle)
r-cran-openmx 2.21.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,412 kB
  • sloc: cpp: 36,577; ansic: 13,811; fortran: 2,001; sh: 1,440; python: 350; perl: 21; makefile: 5
file content (257 lines) | stat: -rwxr-xr-x 8,894 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/python3
import sys
import os
import string
import re
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
import mxAlgebraParser

matrices = {}
algebras = {}
defines = {}
title = None
valBuffer = ""

class MxMatrix:
    name = None
    type = None
    free = False
    unique = False
    nrow = None
    ncol = None
    values = None
    specification = None

class MxAlgebra:
	name = None
	expression = None

def parseDefine( mxInput ):
    global defines
    match = re.match("\s*#define\s+(\S+)\s+(\S+)\s*", mxInput)
    defines[match.group(1)] = match.group(2)
    return len(match.group(0))

def parseTitle( mxInput ):
    global title
    match = re.match("\s*Title(.*)$", mxInput, re.MULTILINE | re.IGNORECASE)
    title = match.group(1).strip()
    title = title.replace('.','_')
    if title == "":
       title = "model"
    return len(match.group(0)) + 1


def parseAlgebras( mxInput ):
    global algebras
    block = re.match("\s*Begin Algebra;(.*?)End Algebra;\s*", 
    	mxInput, re.MULTILINE | re.IGNORECASE | re.DOTALL)
    declareLines = block.group(1).strip().split(';')
    for declare in declareLines:
        declare = declare.strip()
        pieces = re.search("(.+)=(.+)", declare, re.DOTALL)
        if pieces != None:
			algebra = MxAlgebra()
			algebra.name = pieces.group(1).strip()
			algebra.expression = pieces.group(2).strip()
			algebras[algebra.name] = algebra
    return len(block.group(0))


def parseMatrices( mxInput ):
    global defines, matrices
    block = re.match("\s*Begin Matrices;(.*?)End Matrices;\s*", 
    	mxInput, re.MULTILINE | re.IGNORECASE | re.DOTALL)
    declareLines = block.group(1).strip().split('\n')
    for declare in declareLines:
        declare = declare.strip()
        pieces = re.search("(\S+)\s+(\S+)\s+(\S+)\s+(\S+)", declare)
        if pieces != None:
            matrix = MxMatrix()
            matrix.name = pieces.group(1)
            matrix.type = pieces.group(2)
            try:
                matrix.nrow = int(pieces.group(3))
            except:
                matrix.nrow = int(defines[pieces.group(3)])
            try:
                matrix.ncol = int(pieces.group(4))
            except:
                matrix.ncol = int(defines[pieces.group(4)])
            if re.search("Free", declare, re.IGNORECASE) != None:
                matrix.free = True
            elif re.search("Unique", declare, re.IGNORECASE) != None:
                matrix.free = True
                matrix.unique = True
            matrices[matrix.name] = matrix
    return len(block.group(0))

def parseSpecification( mxInput ):
    global defines, matrices
    match = re.match("\s*spec\S*\s+(\S+)", mxInput, re.IGNORECASE | re.MULTILINE)
    specification = list()
    matrixName = match.group(1)
    matrix = matrices[matrixName]
    specCount = specLength[matrix.type[:4]](matrix.nrow, matrix.ncol)
    specs = re.search("\s*spec\S*\s+" + matrixName + "((\s*\S+\s*)" + "{" + str(specCount) + "})\s*",
                       mxInput, re.IGNORECASE | re.MULTILINE)
    specIter = re.finditer("\S+", specs.group(1))
    for spec in specIter:
         try:
             specification.append(int(spec.group(0)))
         except:
             specification.append(int(defines[spec.group(0)]))
    matrices[matrixName].specification = specification
    return len(specs.group(0))

def parseStartOrValue( mxInput ):
    global valBuffer
    match = re.match("\s*(Start|Value)\s+(\S+)\s+(All|(\S+[ ]+)+)\s*", mxInput, re.IGNORECASE)
    if (match.group(3) == "All"):
        for matrix in list(matrices.values()):
            mname = "matrix" + matrix.name
            if match.group(2) == "Value":
                direction = "!"
            else:
                direction = ""
            valBuffer += mname + "@values[" + direction + mname + "@free] <- " + match.group(2) + '\n'
    else:
       tripleIter = re.finditer("(\S+)\s*(\S+)\s*(\S+)\s*", match.group(3))
       for triple in tripleIter:
           mname = "matrix" + triple.group(1)
           if triple.group(2) in defines:
               row = defines[triple.group(2)]
           else:
               row = triple.group(2)
           if triple.group(3) in defines:
               col = defines[triple.group(3)]
           else:
               col = triple.group(3)
           valBuffer += mname + "@values[" + row + "," + col + "] <- " + match.group(2) + '\n'
    valBuffer += '\n'
    return len(match.group(0))
        
def printAlgebras():
	for algebra in list(algebras.values()):
		outstring = "algebra" + algebra.name + " <- "		
		outstring += "mxAlgebra(" + mxAlgebraParser.parser.parse(algebra.expression) + ", "
		outstring += "name = \"" + algebra.name + "\")"
		print(outstring)
	if len(list(algebras.values())) > 0:
		print()

def printMatrices():
    global matrices
    startCounter = 1
    for matrix in list(matrices.values()):
        outstring = "matrix" + matrix.name + " <- "
        outstring += "mxMatrix(type = \"" + matrix.type + "\", "
        outstring += "nrow = " + str(matrix.nrow) + ", "
        outstring += "ncol = " + str(matrix.ncol) + ", "
        if matrix.specification == None:
            outstring += "free = " + str(matrix.free).upper() + ", "
        else:
            freelist = [x > 0 for x in matrix.specification]
            freestring = str(freelist).upper()
            freestring = string.replace(freestring, "[", "c(")
            freestring = string.replace(freestring, "]", ")")
            outstring += "free = " + freestring + ", "
        nextcounter = None
        if matrix.free and not matrix.unique and matrix.specification == None:
            endCounter = startCounter + specLength[matrix.type[:4]](matrix.nrow, matrix.ncol) - 1
            outstring += "labels = makeLabels(c(" + str(startCounter) + ":"
            outstring += str(endCounter) + ")), "
            startCounter = endCounter + 1
        elif matrix.specification != None:
            specstring = str(matrix.specification)
            specstring = string.replace(specstring, "[", "c(")
            specstring = string.replace(specstring, "]", ")")
            outstring += "labels = " + "makeLabels(" + specstring + "), "
        outstring += "byrow = TRUE, "
        outstring += "name = \"" + matrix.name + "\")"
        print(outstring)
	if len(list(matrices.values())) > 0:
		print()


def printValueBuffer():
	global valBuffer
	if valBuffer != "":
		print(valBuffer)

def printModel():
	global matrices, algebras
	if title == None:
		print("model <- mxModel()")
	else:
		print("model <- mxModel(name = \"" + title + "\")")
	print()
	matrixNames = str(["matrix" + x for x in list(matrices.keys())])
	matrixNames = string.replace(matrixNames, "[", "")
	matrixNames = string.replace(matrixNames, "]", "")
	matrixNames = string.replace(matrixNames, "'", "")
	if len(matrixNames) > 0:
		print("model <- mxModel(model, " + matrixNames + ")")
	algNames = str(["algebra" + x for x in list(algebras.keys())])
	algNames = string.replace(algNames, "[", "")
	algNames = string.replace(algNames, "]", "")
	algNames = string.replace(algNames, "'", "")
	if len(algNames) > 0:
		print("model <- mxModel(model, " + algNames + ")")
	print()


specLength = {   "Diag"  : lambda row, col: row,
                 "SDia" : lambda row, col: row * (row - 1) / 2,
                 "Stan" : lambda row, col: row * (row - 1) / 2,
                 "Symm"  : lambda row, col: row * (row + 1) / 2,
                 "Lowe" : lambda row, col: row * (row + 1) / 2,
                 "Full"  : lambda row, col: row * col }

mxDirectives = {    "\s*title" : parseTitle,
                    "\s*#define" : parseDefine,
                    "\s*Begin Matrices" : parseMatrices,
					"\s*Begin Algebra" : parseAlgebras,
                    "\s*spec\S*" : parseSpecification,
                    "\s*(start|value)" : parseStartOrValue }

def tryDirectives ( mxInput ):
    for directive in list(mxDirectives.keys()):
        match = re.match(directive, mxInput, re.IGNORECASE)
        if (match != None):
            subtract = mxDirectives[directive](mxInput)
            mxInput = mxInput[subtract : ]
            return mxInput
    match = re.match(".*", mxInput)
    if (match != None):
        subtract = len(match.group(0)) + 1
        mxInput = mxInput[subtract : ]
    return mxInput
                
def parseModel( mxInput ):

	# Remove all comments from the file
	mxInput = re.sub(re.compile('!.*$', re.MULTILINE), '', mxInput)
    
	while len(mxInput) > 0:
		mxInput = tryDirectives(mxInput)

	print()
	print("require(OpenMx)")
	print("makeLabels <- function(x) { sapply(x, function(y) { paste('var', y, sep = '') })}")
	print()


	# Print matrix declarations
	printMatrices()

	# Print matrix declarations
	printAlgebras()

	# Print any value assignments
	printValueBuffer()

	# Print model declaration
	printModel()

parseModel(sys.stdin.read())