Skip to content

Commit 502a920

Browse files
authored
ENH: Enable one-way cycle flux calculations (#123)
* Changes `calc_net_cycle_flux` to `calc_cycle_flux`, and adds parameter `net` to toggle between one-way and net cycle fluxes * Updates `calc_pi_difference` to allow for retrieving the forward cycle rate product by adding the same `net` parameter (`net=True` returns the cycle product difference) * Updates call signatures throughout `calculations.py` and `test_kda.py` * Related to issue #57
1 parent a0315af commit 502a920

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

kda/calculations.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
probabilities and fluxes from a user-defined kinetic diagram.
1111
1212
.. autofunction:: calc_state_probs
13-
.. autofunction:: calc_net_cycle_flux
13+
.. autofunction:: calc_cycle_flux
1414
.. autofunction:: calc_sigma
1515
.. autofunction:: calc_sigma_K
1616
.. autofunction:: calc_pi_difference
@@ -282,7 +282,8 @@ def calc_sigma_K(G, cycle, flux_diags, key="name", output_strings=True):
282282
return sigma_K
283283

284284

285-
def calc_pi_difference(G, cycle, order, key="name", output_strings=True):
285+
def calc_pi_difference(G, cycle, order, key="name",
286+
output_strings=True, net=True):
286287
r"""
287288
Generates the expression for the cycle-based componenet of the
288289
sum of flux diagrams for some ``cycle`` in kinetic diagram ``G``.
@@ -305,13 +306,16 @@ def calc_pi_difference(G, cycle, order, key="name", output_strings=True):
305306
``NetworkX`` edge key is ``"weight"``, however the ``kda`` edge keys
306307
are ``"name"`` (for rate constant names, e.g. ``"k12"``) and ``"val"``
307308
(for the rate constant values, e.g. ``100``). Default is ``"name"``.
308-
Default is ``"name"``.
309309
output_strings : bool (optional)
310310
Used to denote whether values or strings will be combined. Default
311311
is ``False``, which tells the function to calculate the difference
312312
using numbers. If ``True``, this will assume the input ``'key'``
313313
will return strings of variable names to join into the analytic
314314
function.
315+
net : bool (optional)
316+
Used to determine whether to return the forward cycle product
317+
(i.e., ``net=False``) or the difference of the forward and reverse
318+
cycle products (i.e., ``net=True``). Default is ``True``.
315319
316320
Returns
317321
-------
@@ -357,14 +361,20 @@ def calc_pi_difference(G, cycle, order, key="name", output_strings=True):
357361
for edge in cycle_edges:
358362
ccw_rates.append(G.edges[edge[0], edge[1], edge[2]][key])
359363
cw_rates.append(G.edges[edge[1], edge[0], edge[2]][key])
360-
pi_difference = "-".join(["*".join(ccw_rates), "*".join(cw_rates)])
364+
if net:
365+
pi_difference = "-".join(["*".join(ccw_rates), "*".join(cw_rates)])
366+
else:
367+
pi_difference = "*".join(ccw_rates)
361368
else:
362369
ccw_rates = 1
363370
cw_rates = 1
364371
for edge in cycle_edges:
365372
ccw_rates *= G.edges[edge[0], edge[1], edge[2]][key]
366373
cw_rates *= G.edges[edge[1], edge[0], edge[2]][key]
367-
pi_difference = ccw_rates - cw_rates
374+
if net:
375+
pi_difference = ccw_rates - cw_rates
376+
else:
377+
pi_difference = ccw_rates
368378
return pi_difference
369379

370380

@@ -563,10 +573,10 @@ def calc_state_probs(G, key="name", output_strings=True, dir_edges=None):
563573
return state_probs
564574

565575

566-
def calc_net_cycle_flux(G, cycle, order, key="name",
567-
output_strings=True, dir_edges=None):
568-
r"""Generates the expression for the net cycle flux for
569-
some ``cycle`` in kinetic diagram ``G``.
576+
def calc_cycle_flux(G, cycle, order, key="name",
577+
output_strings=True, dir_edges=None, net=True):
578+
r"""Generates the expression for the one-way or net cycle
579+
flux for a ``cycle`` in the kinetic diagram ``G``.
570580
571581
Parameters
572582
----------
@@ -584,7 +594,6 @@ def calc_net_cycle_flux(G, cycle, order, key="name",
584594
``NetworkX`` edge key is ``"weight"``, however the ``kda`` edge keys
585595
are ``"name"`` (for rate constant names, e.g. ``"k12"``) and ``"val"``
586596
(for the rate constant values, e.g. ``100``). Default is ``"name"``.
587-
Default is ``"name"``.
588597
output_strings : bool (optional)
589598
Used to denote whether values or strings will be combined. Default
590599
is ``False``, which tells the function to calculate the cycle flux
@@ -598,11 +607,14 @@ def calc_net_cycle_flux(G, cycle, order, key="name",
598607
generate the directional diagram edges up front and provide them).
599608
Created using :meth:`~kda.diagrams.generate_directional_diagrams`
600609
with ``return_edges=True``.
610+
net : bool (optional)
611+
Used to determine whether to return the one-way or net cycle flux.
612+
Default is ``True`` (i.e., to generate the net cycle flux).
601613
602614
Returns
603615
-------
604-
net_cycle_flux : float or ``SymPy`` expression
605-
Net cycle flux for the input ``cycle``.
616+
cycle_flux : float or ``SymPy`` expression
617+
The one-way or net cycle flux for the input ``cycle``.
606618
607619
Notes
608620
-----
@@ -628,18 +640,19 @@ def calc_net_cycle_flux(G, cycle, order, key="name",
628640
# construct the expressions for (Pi+ - Pi-), sigma, and sigma_k
629641
# from the directional diagram edges
630642
pi_diff = calc_pi_difference(
631-
G=G, cycle=cycle, order=order, key=key, output_strings=output_strings)
643+
G=G, cycle=cycle, order=order, key=key,
644+
output_strings=output_strings, net=net)
632645
sigma_K = calc_sigma_K(
633646
G=G, cycle=cycle, flux_diags=flux_diags,
634647
key=key, output_strings=output_strings)
635648
sigma = calc_sigma(
636649
G=G, dirpar_edges=dir_edges, key=key, output_strings=output_strings)
637650
if output_strings:
638-
net_cycle_flux = expressions.construct_sympy_net_cycle_flux_func(
651+
cycle_flux = expressions.construct_sympy_net_cycle_flux_func(
639652
pi_diff_str=pi_diff, sigma_K_str=sigma_K, sigma_str=sigma)
640653
else:
641-
net_cycle_flux = pi_diff * sigma_K / sigma
642-
return net_cycle_flux
654+
cycle_flux = pi_diff * sigma_K / sigma
655+
return cycle_flux
643656

644657

645658
def calc_state_probs_from_diags(G, dirpar_edges, key="name", output_strings=True):
@@ -697,7 +710,7 @@ def calc_net_cycle_flux_from_diags(
697710
"""Generates the expression for the net cycle flux for some ``cycle``
698711
in kinetic diagram ``G``. If directional diagram edges are already
699712
generated this offers better performance than
700-
:meth:`~kda.calculations.calc_net_cycle_flux`.
713+
:meth:`~kda.calculations.calc_cycle_flux`.
701714
702715
Parameters
703716
----------
@@ -734,13 +747,14 @@ def calc_net_cycle_flux_from_diags(
734747
735748
"""
736749
msg = """`kda.calculations.calc_net_cycle_flux_from_diags` will be deprecated.
737-
Use `kda.calculations.calc_net_cycle_flux` with parameter `dir_edges`."""
750+
Use `kda.calculations.calc_cycle_flux` with parameter `dir_edges`."""
738751
warnings.warn(msg, DeprecationWarning)
739-
return calc_net_cycle_flux(
752+
return calc_cycle_flux(
740753
G=G,
741754
cycle=cycle,
742755
order=order,
743756
key=key,
744757
output_strings=output_strings,
745758
dir_edges=dirpar_edges,
759+
net=True,
746760
)

kda/tests/test_kda.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ def test_calc_net_cycle_flux_4WL(self, k_vals):
467467
cycle = [0, 1, 3]
468468
order = [0, 1]
469469
# calculate the net cycle flux
470-
net_cycle_flux = calculations.calc_net_cycle_flux(
470+
net_cycle_flux = calculations.calc_cycle_flux(
471471
G,
472472
dir_edges=dir_edges,
473473
cycle=cycle,
@@ -476,7 +476,7 @@ def test_calc_net_cycle_flux_4WL(self, k_vals):
476476
output_strings=False,
477477
)
478478
# generate the net cycle flux function
479-
net_cycle_flux_sympy_func = calculations.calc_net_cycle_flux(
479+
net_cycle_flux_sympy_func = calculations.calc_cycle_flux(
480480
G,
481481
dir_edges=dir_edges,
482482
cycle=cycle,
@@ -522,11 +522,11 @@ def test_calc_net_cycle_flux_3(self, k_vals):
522522
cycle = [0, 2, 1]
523523
order = [0, 1]
524524
# calculate the net cycle flux
525-
net_cycle_flux = calculations.calc_net_cycle_flux(
525+
net_cycle_flux = calculations.calc_cycle_flux(
526526
G, cycle=cycle, order=order, key="val", output_strings=False
527527
)
528528
# generate the sympy net cycle flux function
529-
sympy_net_cycle_flux_func = calculations.calc_net_cycle_flux(
529+
sympy_net_cycle_flux_func = calculations.calc_cycle_flux(
530530
G, cycle=cycle, order=order, key="name", output_strings=True
531531
)
532532
# make sure the sympy function agrees with the known solution

0 commit comments

Comments
 (0)