text: {
   "cells": [
    {
     "cell_type": "code",
     "execution_count": 1,
     "metadata": {
      "collapsed": true
     },
     "outputs": [],
     "source": [
      "import subprocess\n",
      "import os\n",
      "import pandas as pd"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": 2,
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "text": [
        "AGE        20\n",
        "DCAPS       2\n",
        "GLEASON     1\n",
        "RACE        2\n",
        "VOL         0\n",
        "dtype: object\n",
        "{\"AGE\":\"20\",\"DCAPS\":\"2\",\"GLEASON\":\"1\",\"RACE\":\"2\",\"VOL\":\"0\"}\n"
       ]
      }
     ],
     "source": [
      "df = pd.Series({\"AGE\":\"20\", \"RACE\":\"2\", \"DCAPS\":\"2\", \"VOL\":\"0\", \"GLEASON\":\"1\"})\n",
      "print(df)\n",
      "df_json = df.to_json()\n",
      "print(df_json)"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": 3,
     "metadata": {
      "collapsed": true
     },
     "outputs": [],
     "source": [
      "gen_model_file_name = \"h2o-genmodel.jar\"\n",
      "h2o_predictor_class = \"water.util.H2OPredictor\""
     ]
    },
    {
     "cell_type": "code",
     "execution_count": 4,
     "metadata": {
      "collapsed": true
     },
     "outputs": [],
     "source": [
      "def find_file(name, path):\n",
      "    for root, dirs, files in os.walk(path):\n",
      "        if name in files:\n",
      "            return os.path.join(root, name)"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": 5,
     "metadata": {},
     "outputs": [],
     "source": [
      "def check_java_process_and_version():\n",
      "    result = subprocess.call([\"java\", \"-version\"])\n",
      "    if result is not 0:\n",
      "        raise RuntimeError(\"Java process not found, exiting!!\")\n",
      "        \n",
      "    output = subprocess.check_output([\"java\", \"-version\"], stderr=subprocess.STDOUT)\n",
      "    #output = subprocess.Popen([\"java\", \"-version\"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n",
      "    #print output.communicate()\n",
      "    #print output.wait()\n",
      "    result = \"0.0\"\n",
      "    supported = False\n",
      "    if output.find(\"1.6.\") is not -1:\n",
      "        result = \"1.6\"\n",
      "    elif output.find(\"1.7.\") is not -1:\n",
      "        result = \"1.7\"\n",
      "        supported = True\n",
      "    elif output.find(\"1.8.\") is not -1:\n",
      "        result = \"1.8\"\n",
      "        supported = True\n",
      "    return supported"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": 6,
     "metadata": {},
     "outputs": [],
     "source": [
      "def predict_json(dir_path_to_genmodel_and_mojo, mojo_model_name, json_data, show_debug=False):\n",
      "    \n",
      "    # Checking java\n",
      "    if not check_java_process_and_version():\n",
      "        raise RuntimeError(\"Error: Java version is not supported!!\") \n",
      "    \n",
      "    # Check both genmodel and pojo is availalbe in the same path\n",
      "    mojo_test = find_file(mojo_model_name, dir_path_to_genmodel_and_mojo)\n",
      "    if mojo_test is None:\n",
      "        raise RuntimeError(\"Error: MOJO model is not available in \" + dir_path_to_genmodel_and_mojo + \" folder.\")\n",
      "        \n",
      "\n",
      "    genmodel_test = find_file(gen_model_file_name, dir_path_to_genmodel_and_mojo)\n",
      "    if genmodel_test is None:\n",
      "        raise RuntimeError(\"Error:\" + gen_model_file_name +  \" is not available in \" + dir_path_to_genmodel_and_mojo + \" folder.\") \n",
      "    \n",
      "    # dir_path_to_genmodel_and_mojo > must start and ends with \"/\" or add to it \n",
      "    gen_model_arg = \".:\"\n",
      "\n",
      "    if (dir_path_to_genmodel_and_mojo.endswith(\"/\")):\n",
      "        gen_model_arg = gen_model_arg + dir_path_to_genmodel_and_mojo\n",
      "        temp_dir_path = dir_path_to_genmodel_and_mojo[:-1]\n",
      "    else:\n",
      "        gen_model_arg = gen_model_arg + dir_path_to_genmodel_and_mojo + \"/\"\n",
      "        \n",
      "    gen_model_arg = (gen_model_arg\n",
      "                     + gen_model_file_name + \":\"\n",
      "                     + temp_dir_path\n",
      "                     + \":genmodel.jar:/\")\n",
      "    if (show_debug):\n",
      "        print(gen_model_arg)\n",
      "\n",
      "    if (dir_path_to_genmodel_and_mojo.endswith(\"/\")):\n",
      "        mojo_model_args = dir_path_to_genmodel_and_mojo + mojo_model_name\n",
      "    else:\n",
      "        mojo_model_args = dir_path_to_genmodel_and_mojo +  \"/\" + mojo_model_name\n",
      "        \n",
      "    if show_debug:\n",
      "        print(mojo_model_args)\n",
      "    if show_debug:\n",
      "        print(json_data)\n",
      "    return subprocess.check_output([\"java\" , \"-Xmx4g\", \"-cp\", gen_model_arg, h2o_predictor_class, \n",
      "                                    mojo_model_args, json_data], shell=False).decode()\n",
      "\n"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": 7,
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "text": [
        "{\"labelIndex\":0,\"label\":\"0\",\"classProbabilities\":[0.8751848361290144,0.12481516387098562]}\n",
        "\n"
       ]
      }
     ],
     "source": [
      "result = predict_json(\"/Users/avkashchauhan/src/github.com/h2oai/h2o-tutorials/tutorials/python_mojo_scoring/\", \n",
      "             \"gbm_prostate_new.zip\",\n",
      "             df_json,\n",
      "             show_debug=False)\n",
      "\n",
      "print(result)"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": true
     },
     "outputs": [],
     "source": []
    },
    {
     "cell_type": "code",
     "execution_count": null,
     "metadata": {
      "collapsed": true
     },
     "outputs": [],
     "source": []
    }
   ],
   "metadata": {
    "kernelspec": {
     "display_name": "Python 2",
     "language": "python",
     "name": "python2"
    },
    "language_info": {
     "codemirror_mode": {
      "name": "ipython",
      "version": 2
     },
     "file_extension": ".py",
     "mimetype": "text/x-python",
     "name": "python",
     "nbconvert_exporter": "python",
     "pygments_lexer": "ipython2",
     "version": "2.7.13"
    }
   },
   "nbformat": 4,
   "nbformat_minor": 2
  }
  