Skip to content

Commit f7bbaf8

Browse files
authored
MAINT: calculations.py function and dirpar_ deprecations (#124)
* Deprecates the parameter `dirpar_edges` in the following functions: - `calculations.calc_sigma` - `calculations.calc_state_probs_from_diags` - `calculations.calc_net_cycle_flux_from_diags` * Re-introduces (with deprecated status) the `calculations.calc_net_cycle_flux` to allow for KDA paper code to continue working * Updates docstrings for aforementioned functions so it is clear under which circumstances the deprecation warnings will be produced * Updates `Test_Diagram_Generation.test_diagram_counts` and `Test_Diagram_Generation.test_max_connected_diagram_counts` to remove mentions of `dirpars` * Updates to `test_function_inputs`: - Adds 3 test cases to confirm `DeprecationWarning`s are triggered when `dirpar_edges` is used in `calc_sigma`, `calc_state_probs_from_diags`, etc. - Adds 2 test cases to confirm a `TypeError` is raised when no directional diagram edges/diagrams are input - Adds 2 test cases to confirm multiple `DeprecationWarning`s are raised when calling `calc_state_probs_from_diags` or `calc_net_cycle_flux_from_diags` with the deprecated parameter `dirpar_edges`
1 parent 502a920 commit f7bbaf8

File tree

2 files changed

+259
-37
lines changed

2 files changed

+259
-37
lines changed

kda/calculations.py

Lines changed: 205 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def _get_ordered_cycle(G, input_cycle):
9999
)
100100

101101

102-
def calc_sigma(G, dirpar_edges, key="name", output_strings=True):
102+
def calc_sigma(G, dir_edges=None, key="name", output_strings=True, **kwargs):
103103
r"""
104104
Generates the normalization factor expression for state
105105
probabilities and cycle fluxes, which is the sum of directional
@@ -109,12 +109,12 @@ def calc_sigma(G, dirpar_edges, key="name", output_strings=True):
109109
----------
110110
G : ``NetworkX.MultiDiGraph``
111111
A kinetic diagram
112-
dirpar_edges : ndarray
112+
dir_edges : ndarray (optional)
113113
Array of all directional diagram edges (made from 2-tuples)
114114
for the input diagram ``G``. Created using
115115
:meth:`~kda.diagrams.generate_directional_diagrams`
116116
with ``return_edges=True``.
117-
key : str
117+
key : str (optional)
118118
Attribute key used to retrieve edge data from ``G.edges``. The default
119119
``NetworkX`` edge key is ``"weight"``, however the ``kda`` edge keys
120120
are ``"name"`` (for rate constant names, e.g. ``"k12"``) and ``"val"``
@@ -125,6 +125,9 @@ def calc_sigma(G, dirpar_edges, key="name", output_strings=True):
125125
factor using numbers. If ``True``, this will assume the input
126126
``'key'`` will return strings of variable names to join into the
127127
analytic cycle flux function.
128+
kwargs : dict (optional)
129+
Additional keyword arguments. Note that the alias
130+
``dirpar_edges`` is deprecated; please use ``dir_edges``.
128131
129132
Returns
130133
-------
@@ -133,6 +136,18 @@ def calc_sigma(G, dirpar_edges, key="name", output_strings=True):
133136
diagram ``G`` as a float (``output_strings=False``) or a string
134137
(``output_strings=True``).
135138
139+
Warns
140+
-----
141+
DeprecationWarning
142+
The ``dirpar_edges`` parameter is deprecated and will be removed
143+
in a future release. Please use ``dir_edges`` instead.
144+
145+
Raises
146+
------
147+
TypeError
148+
If the required argument ``dir_edges`` is not provided (including
149+
when the deprecated ``dirpar_edges`` parameter is not supplied).
150+
136151
Notes
137152
-----
138153
The expression generated here is important for normalizing
@@ -159,6 +174,15 @@ def calc_sigma(G, dirpar_edges, key="name", output_strings=True):
159174
of all directional diagrams for the kinetic diagram.
160175
161176
"""
177+
if "dirpar_edges" in kwargs:
178+
warn_msg = (
179+
"The `dirpar_edges` parameter is deprecated and will be "
180+
"removed in a future release. Please use `dir_edges` instead."
181+
)
182+
warnings.warn(warn_msg, DeprecationWarning)
183+
dir_edges = kwargs.pop("dirpar_edges", None)
184+
if dir_edges is None:
185+
raise TypeError("`calc_sigma()` missing required argument: `dir_edges`")
162186
edge_is_str = isinstance(G.edges[list(G.edges)[0]][key], str)
163187
if output_strings != edge_is_str:
164188
msg = f"""Inputs `key={key}` and `output_strings={output_strings}`
@@ -167,19 +191,19 @@ def calc_sigma(G, dirpar_edges, key="name", output_strings=True):
167191
variable names for all edges."""
168192
raise TypeError(msg)
169193

170-
n_dir_diagrams = dirpar_edges.shape[0]
194+
n_dir_diagrams = dir_edges.shape[0]
171195
if output_strings:
172196
rate_products = np.empty(shape=(n_dir_diagrams,), dtype=object)
173197
# iterate over the directional diagrams
174-
for i, edge_list in enumerate(dirpar_edges):
198+
for i, edge_list in enumerate(dir_edges):
175199
rates = [G.edges[edge][key] for edge in edge_list]
176200
rate_products[i] = "*".join(rates)
177201
# sum all terms to get normalization factor
178202
sigma = "+".join(rate_products)
179203
else:
180204
rate_products = np.ones(n_dir_diagrams, dtype=float)
181205
# iterate over the directional diagrams
182-
for i, edge_list in enumerate(dirpar_edges):
206+
for i, edge_list in enumerate(dir_edges):
183207
# iterate over the edges in the given directional diagram i
184208
for edge in edge_list:
185209
# multiply the rate of each edge in edge_list
@@ -646,7 +670,7 @@ def calc_cycle_flux(G, cycle, order, key="name",
646670
G=G, cycle=cycle, flux_diags=flux_diags,
647671
key=key, output_strings=output_strings)
648672
sigma = calc_sigma(
649-
G=G, dirpar_edges=dir_edges, key=key, output_strings=output_strings)
673+
G=G, dir_edges=dir_edges, key=key, output_strings=output_strings)
650674
if output_strings:
651675
cycle_flux = expressions.construct_sympy_net_cycle_flux_func(
652676
pi_diff_str=pi_diff, sigma_K_str=sigma_K, sigma_str=sigma)
@@ -655,23 +679,108 @@ def calc_cycle_flux(G, cycle, order, key="name",
655679
return cycle_flux
656680

657681

658-
def calc_state_probs_from_diags(G, dirpar_edges, key="name", output_strings=True):
682+
def calc_net_cycle_flux(G, cycle, order, key="name",
683+
output_strings=True, dir_edges=None):
684+
r"""Generates the expression for the net cycle flux for
685+
some ``cycle`` in kinetic diagram ``G``.
686+
687+
.. deprecated:: 0.3.0
688+
``calc_net_cycle_flux`` is deprecated and will be removed in
689+
a future release. Use :func:`~kda.calculations.calc_cycle_flux`
690+
with ``net=True`` instead.
691+
692+
Parameters
693+
----------
694+
G : ``NetworkX.MultiDiGraph``
695+
A kinetic diagram
696+
cycle : list of int
697+
List of node indices for cycle of interest, index zero. Order of node
698+
indices does not matter.
699+
order : list of int
700+
List of integers of length 2 (e.g. ``[0, 1]``), where the integers are
701+
nodes in ``cycle``. The pair of nodes should be ordered such that
702+
a counter-clockwise path is followed.
703+
key : str (optional)
704+
Attribute key used to retrieve edge data from ``G.edges``. The default
705+
``NetworkX`` edge key is ``"weight"``, however the ``kda`` edge keys
706+
are ``"name"`` (for rate constant names, e.g. ``"k12"``) and ``"val"``
707+
(for the rate constant values, e.g. ``100``). Default is ``"name"``.
708+
Default is ``"name"``.
709+
output_strings : bool (optional)
710+
Used to denote whether values or strings will be combined. Default
711+
is ``False``, which tells the function to calculate the cycle flux
712+
using numbers. If ``True``, this will assume the input ``'key'``
713+
will return strings of variable names to join into the analytic
714+
cycle flux function.
715+
dir_edges : ndarray (optional)
716+
Array of all directional diagram edges (made from 2-tuples)
717+
for the input diagram ``G``. Given as an option for performance reasons
718+
(when calculating net cycle fluxes for multiple cycles it is best to
719+
generate the directional diagram edges up front and provide them).
720+
Created using :meth:`~kda.diagrams.generate_directional_diagrams`
721+
with ``return_edges=True``.
722+
723+
Returns
724+
-------
725+
net_cycle_flux : float or ``SymPy`` expression
726+
Net cycle flux for the input ``cycle``.
727+
728+
Warns
729+
-----
730+
DeprecationWarning
731+
``calc_net_cycle_flux`` is deprecated. Use
732+
:func:`~kda.calculations.calc_cycle_flux`
733+
with ``net=True`` instead.
734+
735+
Notes
736+
-----
737+
The net cycle flux for some cycle :math:`k` is :footcite:`hill_free_1989`,
738+
739+
.. math::
740+
741+
J_{k} = \frac{(\Pi_{+} - \Pi_{-}) \Sigma_{k}}{\Sigma},
742+
743+
where :math:`(\Pi_{+} - \Pi_{-}) \Sigma_{k}` is the sum of all
744+
flux diagrams for cycle :math:`k` and :math:`\Sigma` is the sum
745+
of all directional diagrams for the kinetic diagram.
746+
:math:`\Pi_{+}` and :math:`\Pi_{-}` are the forward and reverse
747+
rate-products along cycle :math:`k` where the forward
748+
(i.e. positive) direction is counter-clockwise (CCW).
749+
750+
"""
751+
warn_msg = (
752+
"`kda.calculations.calc_net_cycle_flux` is deprecated"
753+
"and will be removed in a future release. Use "
754+
"`kda.calculations.calc_cycle_flux` with `net=True`."
755+
)
756+
warnings.warn(warn_msg, DeprecationWarning)
757+
return calc_cycle_flux(G=G, cycle=cycle, order=order, key=key,
758+
output_strings=output_strings, dir_edges=dir_edges, net=True)
759+
760+
761+
def calc_state_probs_from_diags(
762+
G, dir_edges=None, key="name", output_strings=True, **kwargs):
659763
"""Generates the state probability expressions using the diagram
660764
method developed by King and Altman :footcite:`king_schematic_1956` and
661765
Hill :footcite:`hill_studies_1966`. If directional diagram edges are already
662766
generated this offers better performance than
663767
:meth:`~kda.calculations.calc_state_probs`.
664768
769+
.. deprecated:: 0.3.0
770+
``calc_state_probs_from_diags`` is deprecated and will be removed in
771+
a future release. Use :func:`~kda.calculations.calc_state_probs`
772+
with the parameter ``dir_edges`` instead.
773+
665774
Parameters
666775
----------
667776
G : ``NetworkX.MultiDiGraph``
668777
A kinetic diagram
669-
dirpar_edges : array
778+
dir_edges : array (optional)
670779
Array of all directional diagram edges (made from 2-tuples)
671780
for the input diagram ``G``. Created using
672781
:meth:`~kda.diagrams.generate_directional_diagrams`
673782
with ``return_edges=True``.
674-
key : str
783+
key : str (optional)
675784
Attribute key used to retrieve edge data from ``G.edges``. The default
676785
``NetworkX`` edge key is ``"weight"``, however the ``kda`` edge keys
677786
are ``"name"`` (for rate constant names, e.g. ``"k12"``) and ``"val"``
@@ -682,6 +791,9 @@ def calc_state_probs_from_diags(G, dirpar_edges, key="name", output_strings=True
682791
probabilities using numbers. If ``True``, this will assume the input
683792
``'key'`` will return strings of variable names to join into the
684793
analytic state multplicity and normalization functions.
794+
kwargs : dict (optional)
795+
Additional keyword arguments. Note that the alias
796+
``dirpar_edges`` is deprecated; please use ``dir_edges``.
685797
686798
Returns
687799
-------
@@ -692,43 +804,76 @@ def calc_state_probs_from_diags(G, dirpar_edges, key="name", output_strings=True
692804
list of symbolic state probability expressions
693805
in the same order (``output_strings=True``).
694806
807+
Warns
808+
-----
809+
DeprecationWarning
810+
- The ``dirpar_edges`` parameter is deprecated and will be removed
811+
in a future release. Please use ``dir_edges`` instead.
812+
- ``calc_state_probs_from_diags`` itself is deprecated. Use
813+
:func:`~kda.calculations.calc_state_probs` with ``dir_edges`` instead.
814+
815+
Raises
816+
------
817+
TypeError
818+
If the required argument ``dir_edges`` is not provided (including
819+
when the deprecated ``dirpar_edges`` parameter is not supplied).
820+
695821
"""
696-
msg = """`kda.calculations.calc_state_probs_from_diags` will be deprecated.
697-
Use `kda.calculations.calc_state_probs` with parameter `dir_edges`."""
698-
warnings.warn(msg, DeprecationWarning)
699-
state_probs = calc_state_probs(
700-
G=G, dir_edges=dirpar_edges, key=key, output_strings=output_strings,
822+
if "dirpar_edges" in kwargs:
823+
warn_msg = (
824+
"The `dirpar_edges` parameter is deprecated and will be "
825+
"removed in a future release. Please use `dir_edges` instead."
826+
)
827+
warnings.warn(warn_msg, DeprecationWarning)
828+
dir_edges = kwargs.pop("dirpar_edges", None)
829+
if dir_edges is None:
830+
err_msg = (
831+
"`calc_state_probs_from_diags()` "
832+
"missing required argument: `dir_edges`"
833+
)
834+
raise TypeError(err_msg)
835+
warn_msg = (
836+
"`kda.calculations.calc_state_probs_from_diags` is deprecated"
837+
"and will be removed in a future release. Use "
838+
"`kda.calculations.calc_state_probs` with parameter `dir_edges`."
701839
)
840+
warnings.warn(warn_msg, DeprecationWarning)
841+
state_probs = calc_state_probs(
842+
G=G, dir_edges=dir_edges, key=key, output_strings=output_strings)
702843
if output_strings:
703844
state_probs = expressions.construct_sympy_prob_funcs(state_mult_funcs=state_probs)
704845
return state_probs
705846

706847

707848
def calc_net_cycle_flux_from_diags(
708-
G, dirpar_edges, cycle, order, key="name", output_strings=True
709-
):
849+
G, cycle, order, dir_edges=None, key="name", output_strings=True, **kwargs):
710850
"""Generates the expression for the net cycle flux for some ``cycle``
711851
in kinetic diagram ``G``. If directional diagram edges are already
712852
generated this offers better performance than
713853
:meth:`~kda.calculations.calc_cycle_flux`.
714854
855+
.. deprecated:: 0.3.0
856+
``calc_net_cycle_flux_from_diags`` is deprecated and will be removed
857+
in a future release. Use :func:`~kda.calculations.calc_cycle_flux`
858+
with the parameter ``dir_edges`` instead.
859+
715860
Parameters
716861
----------
717862
G : ``NetworkX.MultiDiGraph``
718863
A kinetic diagram
719-
dirpar_edges : ndarray
720-
Array of all directional diagram edges (made from 2-tuples)
721-
for the input diagram ``G``. Created using
722-
:meth:`~kda.diagrams.generate_directional_diagrams`
723-
with ``return_edges=True``.
724864
cycle : list of int
725865
List of node indices for cycle of interest, index zero. Order of node
726866
indices does not matter.
727867
order : list of int
728868
List of integers of length 2 (e.g. ``[0, 1]``), where the integers are
729869
nodes in ``cycle``. The pair of nodes should be ordered such that
730870
a counter-clockwise path is followed.
731-
key : str
871+
dir_edges : ndarray (optional)
872+
Array of all directional diagram edges (made from 2-tuples)
873+
for the input diagram ``G``. Created using
874+
:meth:`~kda.diagrams.generate_directional_diagrams`
875+
with ``return_edges=True``.
876+
key : str (optional)
732877
Attribute key used to retrieve edge data from ``G.edges``. The default
733878
``NetworkX`` edge key is ``"weight"``, however the ``kda`` edge keys
734879
are ``"name"`` (for rate constant names, e.g. ``"k12"``) and ``"val"``
@@ -739,22 +884,55 @@ def calc_net_cycle_flux_from_diags(
739884
using numbers. If ``True``, this will assume the input ``'key'``
740885
will return strings of variable names to join into the analytic
741886
cycle flux function.
887+
kwargs : dict (optional)
888+
Additional keyword arguments. Note that the alias
889+
``dirpar_edges`` is deprecated; please use ``dir_edges``.
742890
743891
Returns
744892
-------
745893
net_cycle_flux : float or ``SymPy`` expression
746894
Net cycle flux for the input ``cycle``.
747895
896+
Warns
897+
-----
898+
DeprecationWarning
899+
- The ``dirpar_edges`` parameter is deprecated and will be removed
900+
in a future release. Please use ``dir_edges`` instead.
901+
- ``calc_net_cycle_flux_from_diags`` itself is deprecated. Use
902+
:func:`~kda.calculations.calc_cycle_flux` with ``dir_edges`` instead.
903+
904+
Raises
905+
------
906+
TypeError
907+
If the required argument ``dir_edges`` is not provided (including
908+
when the deprecated ``dirpar_edges`` parameter is not supplied).
909+
748910
"""
749-
msg = """`kda.calculations.calc_net_cycle_flux_from_diags` will be deprecated.
750-
Use `kda.calculations.calc_cycle_flux` with parameter `dir_edges`."""
751-
warnings.warn(msg, DeprecationWarning)
911+
if "dirpar_edges" in kwargs:
912+
warn_msg = (
913+
"The `dirpar_edges` parameter is deprecated and will be "
914+
"removed in a future release. Please use `dir_edges` instead."
915+
)
916+
warnings.warn(warn_msg, DeprecationWarning)
917+
dir_edges = kwargs.pop("dirpar_edges", None)
918+
if dir_edges is None:
919+
err_msg = (
920+
"`calc_net_cycle_flux_from_diags()` "
921+
"missing required argument: `dir_edges`"
922+
)
923+
raise TypeError(err_msg)
924+
warn_msg = (
925+
"`kda.calculations.calc_net_cycle_flux_from_diags` is deprecated"
926+
"and will be removed in a future release. Use "
927+
"`kda.calculations.calc_cycle_flux` with parameter `dir_edges`."
928+
)
929+
warnings.warn(warn_msg, DeprecationWarning)
752930
return calc_cycle_flux(
753931
G=G,
754932
cycle=cycle,
755933
order=order,
756934
key=key,
757935
output_strings=output_strings,
758-
dir_edges=dirpar_edges,
936+
dir_edges=dir_edges,
759937
net=True,
760938
)

0 commit comments

Comments
 (0)