Becksteinlab / kda

Python package used for the analysis of biochemical kinetic diagrams.
GNU General Public License v3.0
3 stars 1 forks source link

ENH: Optimize partial diagram generation #40

Closed nawtrey closed 2 years ago

nawtrey commented 2 years ago
codecov[bot] commented 2 years ago

Codecov Report

Merging #40 (e6c8c98) into master (1642dcc) will decrease coverage by 0.00%. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #40      +/-   ##
==========================================
- Coverage   98.02%   98.01%   -0.01%     
==========================================
  Files           9        9              
  Lines         759      757       -2     
==========================================
- Hits          744      742       -2     
  Misses         15       15              
Impacted Files Coverage Δ
kda/diagrams.py 100.00% <100.00%> (ø)
nawtrey commented 2 years ago

I don't have proper benchmarking setup, but for a maximally connected 8-state diagram the updated code generates the 262144 partial diagrams in 29.9 seconds, down from 96.2 seconds (on master). So this is roughly 3x faster.

Here is the code I used to compare the performance:

import time
import numpy as np
import networkx as nx

from kda import graph_utils, diagrams

def get_K():
    N = 8
    K = np.ones((N, N), dtype=np.int32)
    np.fill_diagonal(K, 0)
    return K

def main(G):
    return diagrams.generate_partial_diagrams(G, return_edges=True)

if __name__ == "__main__":
    K = get_K()
    G = nx.MultiDiGraph()
    graph_utils.generate_edges(G, K)

    start_time = time.perf_counter()
    pars = main(G=G)
    end_time = time.perf_counter()

    elapsed = end_time - start_time
    print(f"Partial diagrams generated: {pars.shape[0]}")
    print(f"Elapsed time: {elapsed} s")
nawtrey commented 2 years ago

Tests passed and CI was green, merged.