Matrix Processing in Cube with Python

Hi


I want to include python script in cube for matrix processing.
How to define input matrix (1 matrix file with 15 tabs) in python script?
Is there any simple example about matrix processing using python in Cube?

Thanks

  • Hi.

    We suggest to undertake matrix processing with Voyager Matrix programs in CUBE 6 directly, since it is very efficient when working with native CUBE MAT files and Voyager scripting allows to undertake any computation you need with flexibility.

    If moving to Python is needed, for some reasons, then it might be more appropriate with CUBE 6 to convert the MAT files to OMX in CUBE before processing them in Python.

  • Thank you for your reply.

    Yes, actually I need to run this in Python since it will run very complex calculation and I need to redirect that input and output (matrix) in that python script. We have developed this python script for other software and we want to try to apply it on cube, but having difficulties defining the input and output matrix as I mentioned before.

  • Hi Sahrial. OK, I understand that the Python script is already written, so you would like to make use of it with the CUBE matrix. The answer obviously might depend on the specific Python script, but in my opinion, with CUBE 6, you can convert your MAT files in OMX as explained here and then use openmatrix and NumPy in Python directly very conveniently.

    A possible alternative is to use our API, but again a clearer answer might depend on the specific Python process.

  • Hi, thank you again for your explanation.

    Yes, I want to utilize cube matrix and python script that we developed, then run that python in cube application manager.
    Do you have simple example about this?

    Perhaps similar like this one, but for processing matrix
    communities.bentley.com/.../how-to-include-python-in-your-cube-application-manager-workflow

  • Hello. 

    You can either use a command line in a Pilot program, in a single line like the example below (passing OMX matrices and using catalog keys also for the definition of the python exe to be used):

    ;                       sys.argv[0]                              sys.argv[1]     sys.argv[2]      sys.argv[3]       sys.argv[4]                            sys.argv[5]                            sys.argv[6]                         sys.argv[7]                                                        
    *"{Python_Interpreter}" "{catalog_dir}/scripts/python_script.py" "{catalog_dir}" "{scenario_dir}" "{SCENARIO_CODE}" "{SCENARIO_DIR}/HW_LoopSkims01_OP.omx" "{SCENARIO_DIR}/HW_LoopSkims01_PK.omx" "{SCENARIO_DIR}/PT_LoopSk01_OP.omx" "{SCENARIO_DIR}/PT_LoopSk01_PK.omx"

    or using a token, like in the example below:

    MaxString = 999
    ; statement whose first field begins with a *, and is more than 2 characters long, is considered as a command for the operating system.
    ; **command appropriate if the command requires some interaction with the user, or if it displays some progress which the user always wants to be able to view.
    ; defining a string-token first, so that I can specify the sys.argv in separate lines (just for readability of inputs)
    sargv_run = '"{catalog_dir}"'                             +  ; sys.argv[1]
               ' "{scenario_dir}"'                            +  ; sys.argv[2]
               ' "{SCENARIO_CODE}"'                           +  ; sys.argv[3]
               ' "{SCENARIO_DIR}/HW_LoopSkims01_OP.omx"'      +  ; sys.argv[4]
               ' "{SCENARIO_DIR}/HW_LoopSkims01_PK.omx"'      +  ; sys.argv[5]
               ' "{SCENARIO_DIR}/PT_LoopSk01_OP.omx"'         +  ; sys.argv[6]
               ' "{SCENARIO_DIR}/PT_LoopSk01_PK.omx"'            ; sys.argv[7]
    
    
    ;                        sys.argv[0]                              All other sys.argv from the string-token
    **"{Python_Interpreter}" "{catalog_dir}/scripts/python_script.py" @sargv_run@

    or you can enter a user program as explained at that link that you mentioned. That one at that link is just a general example, and it really makes no difference what data type you use in input, in terms of the process to include the script in Application Manager. It is then your Python script that needs to be modified to appropriately read the inputs from the command line as sys.argv[#].

    Answer Verified By: Sahrial Sahrial