<jupyter_start><jupyter_text>Parallelized simulationsIn `medusa`, ensemble Flux Balance Analysis (FBA) can be sped up thanks to the `multiprocessing` Python module. With this approach, each core (or processor) is assigned a subset of ensemble members for which to perform FBA, speeding up the computation in proportion to the number of additional processors allocated.Let's load a test model to demonstrate parallel ensemble FBA. This ensemble has 1000 members, so each FBA step will return fluxes for each reaction in one of the 1000 members.<jupyter_code>from medusa.flux_analysis import flux_balance
from medusa.test import create_test_ensemble
ensemble = create_test_ensemble("Staphylococcus aureus")<jupyter_output><empty_output><jupyter_text>Next, perform the actual simulations. To parallelize, just indicate the number of cores you'd like to use with the `num_processes` argument. `medusa` will _not_ automatically recognize that you have additional cores available. We'll use the time module to keep track of how long the simulation takes given the number of cores allocated.<jupyter_code>import time

runtimes = {}
for num_processes in range(1,5):
    t0 = time.time()
    flux_balance.optimize_ensemble(ensemble, num_processes = num_processes)
    t1 = time.time()
    runtimes[num_processes] = t1-t0
    print(str(num_processes) + ' processors: ' + str(t1-t0) + 's')
import matplotlib.pylab as plt
fig,ax = plt.subplots()
plt.bar(runtimes.keys(), runtimes.values(), align = 'center', alpha = 0.6)
plt.xlabel('Number of processors')
plt.ylabel('Runtime (s)')
ax.set_xlabel('Number of processors',size=16)
ax.set_ylabel('Ensemble Flux Balance \nAnalysis runtime (s)',size=16)
ax.tick_params(axis='both', which='major', labelsize=12)
ax.tick_params(axis='both', which='minor', labelsize=12)
plt.savefig('parallel_fba.svg')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09403546480386889, 'fraction_numerical': 0.013970983342289092, 'mean_word_length': 6.1891891891891895, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '325829', 'license': 'MIT', 'n_tokens_mistral': 596, 'n_tokens_neox': 560, 'n_words': 193, 'path': 'docs/parallel_fba.ipynb', 'repo_name': 'Basie12/Medusa', 'size': '1861'}