Library Tutorial

These tutorials shows how to use PyBioNetGen’s library to run and plot a simple model, as well as create a BNG model object.

Getting Started

Make sure you have PyBioNetGen properly installed by running

bionetgen -h

If this command doesn’t print out help information, install PyBioNetGen with

pip install bionetgen

Finally, make sure to import the PyBioNetGen library.

import bionetgen

Running and Plotting a Model

Use the run method to run a BNGL model. Optionally, you can save the results in a new or existing folder.

result = bionetgen.run("SIR.bngl")
# OR
result = bionetgen.run("SIR.bngl", out = "SIR_folder")

To view the resulting gdat record array, you can either use the gdats attribute or the index of the result object:

result.gdats["SIR"][:10]
# OR
result[0][:10]

Similarly, to view the resulting cdat record array, use the cdats attribute:

result.cdats["SIR"][:10]

To plot the gdat record array, we’ll need matplotlib.

import matplotlib.pyplot as plt

Save the gdat record array as its own object. Then, the values can be plotted.

r = result[0]

for name in r.dtype.names:
    if name != "time":
        plt.plot(r['time'], r[name], label = name)
plt.xlabel("time")
plt.ylabel("species (counts)")
_ = plt.legend(frameon = False)

Using the bngmodel object

Use the bngmodel method to create a Python representation of a BNGL model.

model = bionetgen.bngmodel("SIR.bngl")

To view the model, you can print() the entire BNGL model or just certain blocks of the model.

print(model)
print(model.parameters)

Jupyter Notebooks

Interactive Jupyter notebooks versions of these tutorials can be found here: