-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymcaToolBox.py
More file actions
520 lines (422 loc) · 16.5 KB
/
SymcaToolBox.py
File metadata and controls
520 lines (422 loc) · 16.5 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import subprocess
from os import devnull, path, mkdir
import sys
from re import sub
from sympy import Symbol, sympify, nsimplify, fraction, S
from sympy.matrices import Matrix, diag, NonSquareMatrixError
from CCobjects import CCBase, CCoef
import logging
class SymcaToolBox(object):
"""The class with the functions used to populate SymcaData. The project is
structured in this way to abstract the 'work' needed to build the various
matrices away from the SymcaData class. This 'toolbox' does only has
a filename variable used for temp storage of maxima output"""
# @staticmethod
# def make_path(mod,subdir,subsubdir = None):
# base_dir = mod.ModelOutput
# main_dir = base_dir + '/' + subdir
# mod_dir = main_dir + '/' + mod.ModelFile[:-4]
# if not path.exists(main_dir):
# mkdir(main_dir)
# if not path.exists(mod_dir):
# mkdir(mod_dir)
# if subsubdir:
# branch_dir = mod_dir + '/' + subsubdir
# if not path.exists(branch_dir):
# mkdir(branch_dir)
# return branch_dir + '/'
@staticmethod
def get_nmatrix(mod):
"""
Returns a sympy matrix made from the N matrix in a Pysces model where
the elements are in the same order as they appear in the k and l
matrices in pysces.
We need this to make calculations easier later on.
"""
nmatrix = mod.nmatrix
#swap columns around to same order as kmatrix, store in new matrix
nmatrix_cols = nmatrix[:, mod.kmatrix_row]
#swap rows around to same oder as lmatrix, store in a new matrix
nmatrix_cols_rows = nmatrix_cols[mod.lmatrix_row, :]
#create Sympy symbolic matrix from the numpy ndarray
nmat = Matrix(nmatrix_cols_rows)
return nmat
@staticmethod
def get_num_ind_species(mod):
inds = len(mod.lmatrix_col)
return inds
@staticmethod
def get_num_ind_fluxes(mod):
inds = len(mod.kmatrix_col)
return inds
@staticmethod
def get_species_vector(mod):
"""
Returns a vector (sympy matrix) with the species in the correct order
"""
slist = []
#gets the order of the species from the lmatrix rows
for index in mod.lmatrix_row:
slist.append(mod.species[index])
svector = Matrix(sympify(slist))
#inds = len(mod.lmatrix_col)
#Sind = Matrix(svector[:inds])
#Sdep = Matrix(svector[inds:])
return svector
@staticmethod
def get_fluxes_vector(mod):
"""
Gets the dependent and independent fluxes (in the correct order)
"""
jlist = []
#gets the order of the fluxes from the kmatrix rows
for index in mod.kmatrix_row:
jlist.append('J_' + mod.reactions[index])
jvector = Matrix(sympify(jlist))
#inds = len(mod.kmatrix_col)
#Jind = Matrix(jvector[:inds])
#Jdep = Matrix(jvector[inds:])
return jvector
@staticmethod
def substitute_fluxes(all_fluxes, kmatrix):
"""
Substitutes equivalent fluxes in the kmatrix (e.i. dependent fluxes
with independent fluxes or otherwise equal fluxes)
"""
new_fluxes = all_fluxes[:, :]
for row in xrange(kmatrix.rows - 1, -1, -1):
for row_above in xrange(row - 1, -1, -1):
if kmatrix[row, :] == kmatrix[row_above, :]:
new_fluxes[row] = new_fluxes[row_above]
return new_fluxes
@staticmethod
def scale_matrix(all_elements, mat, inds):
"""
Scales the k or l matrix.
The procedure is the same for each matrix:
(D^x)^(-1) * y * D^(x_i)
Inverse diagonal The matrix to be The diagonal of
of the x where scaled. i.e. the the independent x
x is either the k or l matrix where x is the
species or the species or the
fluxes fluxes
"""
d_all_inv = diag(*all_elements).inv()
d_inds = diag(*inds)
scaled_matrix = d_all_inv * mat * d_inds
return scaled_matrix
@staticmethod
def get_es_matrix(mod, nmatrix, fluxes, species):
"""
Gets the esmatrix.
Goes down the columns of the nmatrix (which holds the fluxes)
to get the rows of the esmatrix.
Nested loop goes down the rows of the nmatrix (which holds the species)
to get the columns of the esmatrix
so the format is
ecReationN0_M0 ecReationN0_M1 ecReationN0_M2
ecReationN1_M0 ecReationN1_M1 ecReationN1_M2
ecReationN2_M0 ecReationN2_M1 ecReationN2_M2
"""
nmat = nmatrix
elas = []
for col in range(nmat.cols):
current_reaction = fluxes[col]
elas_row = []
for row in range(nmat.rows):
current_species = species[row]
ec_name = 'ec' + str(current_reaction)[2:] + '_' + str(current_species)
cond1 = getattr(mod, ec_name) != 0
if cond1:
elas_row.append(ec_name)
else:
elas_row.append(0)
elas.append(elas_row)
esmatrix = Matrix(elas)
return esmatrix
@staticmethod
def simplify_matrix(matrix):
"""
Replaces floats with ints and puts elements with fractions
on a single demoninator.
"""
m = matrix[:, :]
for i, e in enumerate(m):
m[i] = nsimplify(e, rational=True).cancel()
return m
@staticmethod
def adjugate_matrix(matrix):
"""
Returns the adjugate matrix which is the transpose of the
cofactor matrix.
Contains code adapted from sympy.
Specifically:
cofactorMatrix()
minorEntry()
minorMatrix()
cofactor()
"""
def cofactor_matrix(mat):
out = Matrix(mat.rows, mat.cols, lambda i, j:
cofactor(mat, i, j))
return out
def minor_entry(mat, i, j):
if not 0 <= i < mat.rows or not 0 <= j < mat.cols:
raise ValueError("`i` and `j` must satisfy 0 <= i < `mat.rows` " +
"(%d)" % mat.rows + "and 0 <= j < `mat.cols` (%d)." % mat.cols)
return SymcaToolBox.det_bareis(minor_matrix(mat, i, j))
def minor_matrix(mat, i, j):
if not 0 <= i < mat.rows or not 0 <= j < mat.cols:
raise ValueError("`i` and `j` must satisfy 0 <= i < `mat.rows` " +
"(%d)" % mat.rows + "and 0 <= j < `mat.cols` (%d)." % mat.cols)
m = mat.as_mutable()
m.row_del(i)
m.col_del(j)
return m[:, :]
def cofactor(mat, i, j):
if (i + j) % 2 == 0:
return minor_entry(mat, i, j)
else:
return -1 * minor_entry(mat, i, j)
return cofactor_matrix(matrix).transpose()
@staticmethod
def det_bareis(matrix):
"""
Adapted from original det_bareis function in Sympy 0.7.3.
cancel() and expand() are removed from function to speed
up calculations. Maxima will be used to simplify the result
Original docstring below:
Compute matrix determinant using Bareis' fraction-free
algorithm which is an extension of the well known Gaussian
elimination method. This approach is best suited for dense
symbolic matrices and will result in a determinant with
minimal number of fractions. It means that less term
rewriting is needed on resulting formulae.
"""
mat = matrix
if not mat.is_square:
raise NonSquareMatrixError()
m, n = mat[:, :], mat.rows
if n == 1:
det = m[0, 0]
elif n == 2:
det = m[0, 0] * m[1, 1] - m[0, 1] * m[1, 0]
else:
sign = 1 # track current sign in case of column swap
for k in range(n - 1):
# look for a pivot in the current column
# and assume det == 0 if none is found
if m[k, k] == 0:
for i in range(k + 1, n):
if m[i, k] != 0:
m.row_swap(i, k)
sign *= -1
break
else:
return S.Zero
# proceed with Bareis' fraction-free (FF)
# form of Gaussian elimination algorithm
for i in range(k + 1, n):
for j in range(k + 1, n):
d = m[k, k] * m[i, j] - m[i, k] * m[k, j]
if k > 0:
d /= m[k - 1, k - 1]
m[i, j] = d
det = sign * m[n - 1, n - 1]
return det
@staticmethod
def invert(matrix,path_to):
"""
Returns the numerators of the inverted martix separately from the
common denominator (the determinant of the matrix)
"""
common_denom = SymcaToolBox.det_bareis(matrix)
adjugate = SymcaToolBox.adjugate_matrix(matrix)
common_denom = SymcaToolBox.maxima_factor(common_denom, path_to)
#adjugate = self._maxima_factor('/home/carl/test.txt',adjugate)
cc_i_sol = adjugate, common_denom
return cc_i_sol
@staticmethod
def maxima_factor(expression,path_to):
"""
This function is equivalent to the sympy.cancel()
function but uses maxima instead
"""
maxima_in_file = path_to + 'in.txt'
maxima_out_file = path_to + 'out.txt'
if expression.is_Matrix:
expr_mat = expression[:, :]
#print expr_mat
print 'Simplifying matrix with ' + str(len(expr_mat)) + ' elements'
for i, e in enumerate(expr_mat):
sys.stdout.write('*')
sys.stdout.flush()
if (i + 1) % 50 == 0:
sys.stdout.write(' ' + str(i + 1) + '\n')
sys.stdout.flush()
#print e
expr_mat[i] = SymcaToolBox.maxima_factor(e,path_to)
sys.stdout.write('\n')
sys.stdout.flush()
return expr_mat
else:
batch_string = (
'stardisp:true;stringout("'
+ maxima_out_file + '",factor(' + str(expression) + '));')
#print batch_string
with open(maxima_in_file, 'w') as f:
f.write(batch_string)
maxima_command = ['maxima', '--batch=' + maxima_in_file]
dn = open(devnull, 'w')
subprocess.call(maxima_command, stdin=dn, stdout=dn, stderr=dn)
simplified_expression = ''
with open(maxima_out_file) as f:
for line in f:
if line != '\n':
simplified_expression = line[:-2]
frac = fraction(sympify(simplified_expression))
#print frac[0].expand()/frac[1].expand()
return frac[0].expand() / frac[1].expand()
@staticmethod
def solve_dep(cc_i_num, scaledk0, scaledl0, num_ind_fluxes,path_to):
"""
Calculates the dependent control matrices from the independent control
matrix CC_i_solution
"""
j_cci_sol = cc_i_num[:num_ind_fluxes, :]
s_cci_sol = cc_i_num[num_ind_fluxes:, :]
j_ccd_sol = scaledk0 * j_cci_sol
s_ccd_sol = scaledl0 * s_cci_sol
tempmatrix = j_cci_sol
for matrix in [j_ccd_sol, s_cci_sol, s_ccd_sol]:
if len(matrix) != 0:
tempmatrix = tempmatrix.col_join(matrix)
cc_sol = tempmatrix
cc_sol = SymcaToolBox.maxima_factor(cc_sol,path_to)
#print len(j_cci_sol)
#print len(j_ccd_sol)
#print len(s_cci_sol)
#print len(s_ccd_sol)
return cc_sol
@staticmethod
def build_cc_matrix(j, jind, sind, jdep, sdep):
"""
Produces the matrices j_cci, j_ccd, s_cci and s_ccd
which holds the symbols for the independent and dependent flux control
coefficients and the independent and dependent species control
coefficients respectively
"""
j_cci = []
j_ccd = []
s_cci = []
s_ccd = []
for Ji in jind:
row = []
for R in j:
row.append('ccJ' + str(Ji)[2:] + '_' + str(R)[2:])
j_cci.append(row)
for Si in sind:
row = []
for R in j:
row.append('cc' + str(Si) + '_' + str(R)[2:])
s_cci.append(row)
for Jd in jdep:
row = []
for R in j:
row.append('ccJ' + str(Jd)[2:] + '_' + str(R)[2:])
j_ccd.append(row)
for Sd in sdep:
row = []
for R in j:
row.append('cc' + str(Sd) + '_' + str(R)[2:])
s_ccd.append(row)
j_cci = Matrix(j_cci)
j_ccd = Matrix(j_ccd)
s_cci = Matrix(s_cci)
s_ccd = Matrix(s_ccd)
#cc_i = j_cci.col_join(s_cci)
tempmatrix = j_cci
for matrix in [j_ccd, s_cci, s_ccd]:
if len(matrix) != 0:
tempmatrix = tempmatrix.col_join(matrix)
cc = tempmatrix
#print len(j_cci)
#print len(j_ccd)
#print len(s_cci)
#print len(s_ccd)
return cc
@staticmethod
def get_fix_denom(lmatrix, species_independent, species_dependent):
num_inds = len(species_independent)
num_deps = len(species_dependent)
if num_deps == 0:
return sympify('1')
else:
dependent_ls = lmatrix[num_inds:, :]
denom = sympify('1')
for row in range(dependent_ls.rows):
for each in dependent_ls[row, :] * species_independent * -1:
denom = denom * each.atoms(Symbol).pop()
denom = denom * species_dependent[row]
return denom.nsimplify()
@staticmethod
def fix_expressions(cc_num, common_denom_expr, lmatrix, species_independent, species_dependent):
fix_denom = SymcaToolBox.get_fix_denom(
lmatrix,
species_independent,
species_dependent
)
#print fix_denom
cd_num, cd_denom = fraction(common_denom_expr)
new_cc_num = cc_num[:, :]
#print type(new_cc_num)
for i, each in enumerate(new_cc_num):
new_cc_num[i] = ((each * cd_denom) / fix_denom).expand()
return new_cc_num, (cd_num / fix_denom).expand()
@staticmethod
def spawn_cc_objects(mod, cc_sol, cc_names, common_denom_expr):
common_denom = CCBase(
mod,
'common_denominator',
common_denom_expr
)
cc_object_list = [common_denom]
for i, each in enumerate(cc_names):
cc_object_list.append(
CCoef(
mod,
str(each),
cc_sol[i],
common_denom
)
)
return cc_object_list
# @staticmethod
# def expression_to_latex(expression):
# #At the moment this function can turn (some) expressions containing
# #elasticities and control coefficients into
# #latex strings. One problem is that I assumed that expressions with
# #fractions will always have the form
# #(x1/y1+x2/y2+x3/y3)/(z1/u1+z2/u2+z3/u3). However when the numerator
# #only has one term the form is: x1/(y1*(z1/u1+z2/u2+z3/u3))
# #and in this case the function does not work correctly.
# if type(expression) != str:
# expression = str(expression)
# #elasticities
# expr = sub(r'ec(\S*?)_(\S*?\b)',r'\\varepsilon^{\1}_{\2}',expression)
# #fluxes
# expr = sub(r'J_(\S*?\b)',r'J_{\1}',expr)
# #controls
# expr = sub(r'cc(\S*?)_(\S*?\b)',r'C^{\1}_{\2}',expr)
# #main fraction division
# expr = sub(r'\)/\(',r' ',expr)
# #remove ( and )
# expr = sub(r'\)',r'',expr)
# expr = sub(r'\(',r'',expr)
# #main fraction
# expr = sub(r'(\S*[^\)])/([^\(]\S*)',r'\\frac{\1}{\2}',expr)
# #sub fractions
# expr = sub(r'(.*})\s\s\s(\\frac{.*)',r'\\frac{\1}{\2}',expr)
# #times
# expr = sub(r'\*',r'\\cdot',expr)
# return expr