コード例 #1
0
void 
ProgrammableOpAttributes::AddPythonScript(const std::string& name, const stringVector& atts, const std::string& code)
{
    JSONNode vars = JSONNode::JSONArray();
    for(int i = 0; i < atts.size(); ++i)
        vars.Append(atts[i]);

    JSONNode node;
    node["vars"] = vars;

    std::ostringstream pwrapper;

    pwrapper << "from visit_internal_funcs import *\n"
             << code
             << "\n";
    std::string escapedCode = pwrapper.str();
    replace(escapedCode, "\n", "\\n");

    node["source"] = escapedCode;

    script["scripts"][name] = node;

    //update scriptmap
    scriptMap = script.ToString();
    Select(ID_scriptMap, (void *)&scriptMap);
}
コード例 #2
0
void
ProgrammableOpAttributes::AddFunction(const std::string& name, const stringVector& atts)
{
    JSONNode vars = JSONNode::JSONArray();
    for(int i = 0; i < atts.size(); ++i)
        vars.Append(atts[i]);

    JSONNode node;
    node["vars"] = vars;

    std::string argstring = "";
    for(size_t i = 0; i < atts.size(); ++i)
        argstring += atts[i] + (i == atts.size()-1 ? "" : ",");

   // char buf[1024];
   // sprintf(buf,"import visit_internal_funcs\nsetout(visit_internal_funcs.%s(%s))",name.c_str(),argstring.c_str());

    std::ostringstream ostr;

    ostr << "import visit_internal_funcs\n"
         << "setout(visit_internal_funcs." << name << "(" << argstring << "))" << std::endl;

    std::string escapedCode = ostr.str();
    //std::cout << escapedCode << std::endl;
    replace(escapedCode, "\n", "\\n");

    node["source"] = escapedCode;

    script["scripts"][name] = node;

    //update scriptmap
    scriptMap = script.ToString();
    Select(ID_scriptMap, (void *)&scriptMap);
}
コード例 #3
0
void
ProgrammableOpAttributes::LoadRKernel(const std::string& name, const JSONNode &atts, const std::string& code)
{
    script = JSONNode();
    scriptMap = "";

    stringVector args;
    SetupPipeline(atts,args,name);

    JSONNode vars = JSONNode::JSONArray();
    for(int i = 0; i < args.size(); ++i)
        vars.Append(args[i]);

    JSONNode node;
    node["vars"] = vars;

    std::string argstring = "";
    for(size_t i = 0; i < args.size(); ++i)
        argstring += args[i] + (i == args.size()-1 ? "" : ",");

    std::ostringstream rwrapper;

    rwrapper << "import rpy2,numpy\n"
             << "import rpy2.robjects as robjects\n"
             << "import rpy2.robjects.numpy2ri\n"
             << "rpy2.robjects.numpy2ri.activate()\n"
             << name << " = robjects.r('''\n"
             << code << "\n"
             << "''')\n"
             << "setout(numpy.asarray(" << name << "(" << argstring << ")))\n";

    std::string escapedCode = rwrapper.str();
    //std::cout << escapedCode << std::endl;
    replace(escapedCode, "\n", "\\n");

    node["source"] = escapedCode;

    script["scripts"][name] = node;

    //update scriptmap
    scriptMap = script.ToString();

    AddNode(name,name);
    AddFinalOutputConnection(name);

}
コード例 #4
0
void
ProgrammableOpAttributes::AddRScript(const std::string& name, const stringVector& atts, const std::string& code)
{
    JSONNode vars = JSONNode::JSONArray();
    for(int i = 0; i < atts.size(); ++i)
        vars.Append(atts[i]);

    JSONNode node;
    node["vars"] = vars;

    std::string argstring = "";
    for(size_t i = 0; i < atts.size(); ++i)
        argstring += atts[i] + (i == atts.size()-1 ? "" : ",");

    std::ostringstream rwrapper;

    rwrapper << "import rpy2,numpy\n"
             << "import rpy2.robjects as robjects\n"
             << "import rpy2.robjects.numpy2ri\n"
             << "rpy2.robjects.numpy2ri.activate()\n"
             << "_r_output = None\n"
             << "def _r_setout(out):\n"
             << "  global _r_output\n"
             << "  _r_output = out\n"
             << "r_setout = rpy2.rinterface.rternalize(_r_setout)\n"
             << "rpy2.robjects.globalenv['setout'] = r_setout\n"
             << "r_f = robjects.r('''\n"
             << "(function(" + argstring + ") { \n"
             << code
             << "})\n"
             << "''')\n"
             << "r=r_f("+ argstring + ")\n"
             << "setout(numpy.asarray(_r_output))\n";

    std::string escapedCode = rwrapper.str();
    replace(escapedCode, "\n", "\\n");

    node["source"] = escapedCode;

    script["scripts"][name] = node;

    //update scriptmap
    scriptMap = script.ToString();
    Select(ID_scriptMap, (void *)&scriptMap);
}
コード例 #5
0
void
ProgrammableOpAttributes::LoadPythonKernel(const std::string& name, const JSONNode& atts, const std::string& code)
{
    script = JSONNode();
    scriptMap = "";

    stringVector args;
    SetupPipeline(atts,args,name);

    std::string arglist = "";

    JSONNode vars = JSONNode::JSONArray();

    for(int i = 0; i < args.size(); ++i)
    {
        vars.Append(args[i]);
        arglist += args[i] + (i == args.size()-1 ? "" : ",");
    }

    JSONNode node;
    node["vars"] = vars;

    std::ostringstream pwrapper;

    pwrapper << "from visit_internal_funcs import *\n"
             //<< "exec('''" << "\n"
             //<< code << "''',globals())\n"
             << code << "\n"
             << "setout(" << name << "(" << arglist << "))\n";

    std::string escapedCode = pwrapper.str();
    //std::cout << escapedCode << std::endl;
    //replace(escapedCode, "'", "\"");
    //replace(escapedCode, "\"", "\\\"");
    //replace(escapedCode, "\n", "\\n");

    node["source"] = escapedCode;

    script["scripts"][name] = node;
    scriptMap = script.ToString();

    AddNode(name,name);
    AddFinalOutputConnection(name);
}
コード例 #6
0
bool
GetJSONVectorFromPyObject(PyObject *obj, JSONNode &vec)
{
    bool retval = true;

    if(obj == 0)
    {
        retval = false;
    }
    else if(PyBool_Check(obj))
    {
        vec = obj == Py_True ? true : false;
    }
    else if(PyTuple_Check(obj))
    {
        // Extract arguments from the tuple.
        vec = JSONNode::JSONArray();

        for(int i = 0; i < PyTuple_Size(obj); ++i)
        {
            PyObject *item = PyTuple_GET_ITEM(obj, i);
            JSONNode node;
            if(!GetJSONVectorFromPyObject(item,node))
                return false;
            vec.Append(node);
        }
    }
    else if(PyList_Check(obj))
    {
        vec = JSONNode::JSONArray();

        // Extract arguments from the list.
        for(int i = 0; i < PyList_Size(obj); ++i)
        {
            PyObject *item = PyList_GET_ITEM(obj, i);
            JSONNode node;
            if(!GetJSONVectorFromPyObject(item,node))
                return false;
            vec.Append(node);
        }
    }
    else if(PyString_Check(obj))
    {
        vec = PyString_AS_STRING(obj);
    }
    else if(PyInt_Check(obj))
    {
        vec = PyInt_AsLong(obj);
    }
    else if(PyFloat_Check(obj))
    {
        vec = PyFloat_AsDouble(obj);
    }
    else if(PyDict_Check(obj))
    {
        vec = JSONNode::JSONObject();

        PyObject* keys = PyDict_Keys(obj);
        for(int i = 0; i < PyList_Size(keys); ++i)
        {
            PyObject *item = PyList_GET_ITEM(keys, i);
            if(!PyString_Check(item))
            {
                std::cerr << "unknown element type, skipping " << std::endl;
                continue;
            }

            JSONNode node;

            std::string key = PyString_AsString(item);

            PyObject *value = PyDict_GetItem(obj,item);
            if(!GetJSONVectorFromPyObject(value,node))
                return false;
            vec[key] = node;
        }
    }
    else
    {
        retval = false;
        VisItErrorFunc("The object could not be converted to a "
                       "vector of strings.");
    }

    return retval;
}