How to obtain variable values from Python API

32 views
Skip to first unread message

Sakrapee Paisalnun

unread,
Jun 26, 2017, 7:51:42 PM6/26/17
to kappa-users

Dear KaSim developer team,

 

Currently, I am working with the python interface of KaSim.

 

I am wondering how to get the value of a particular variable by

using this interface.

 

When I use the expression $PRINTF <variable_name> via

simulation_perturbation, no value is returned or printed. (attached files)

 

Is it possible to give me any suggestions or examples about how to obtain

variable values with the python interface in KaSim?

 

Best Regards,

 

Sakrapee Paisalnan

simpleBinding.ka
kappa_printf.py

Yarden Katz

unread,
Aug 15, 2019, 10:59:14 AM8/15/19
to kappa-users
You can get this information from the "variables" entry in the object returned by "project_parse". Here's a stripped down example based on your file:


import os
import sys
import getopt
import time
import uuid
import kappy
import kappy.kappa_common as kappa_common
import kappy.kappa_std as kappa_std

def main():
    # make simple Kappa model
    model = '''
%agent: A()
%var: 'rate' 0.001

'rule1' A() -> . @ 'rate'

%init: 100 A()
'''
    with open("./model.ka", "w") as file_out:
        file_out.write(model)
    url = os.path.expanduser("~/Software/Kappapp.app/Contents/Resources/bin")
    try:
        runtime = kappa_std.KappaStd(url)
        plot_period = 0.1
        pause_condition = "[T] > 10"
        file_content = ""
        with open("./model.ka", "r") as file_in:
            file_content = file_in.read()
        runtime.add_model_string(file_content)
        kappa_prog = runtime.project_parse()
        print("variables: ")
        for var in kappa_prog["variables"]:
            name, val = var["first"]["val"], var["second"]["val"]
            print("name = %s, value = %s" %(name, str(val)))
    except kappa_common.KappaError as exception:
        print(exception.errors)
    return None
    None

if __name__ == "__main__":
    main()
Reply all
Reply to author
Forward
0 new messages