Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions docs/concepts/decision_tables.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,17 @@
}
],
"source": [
"example_dt\\\n",
" .add(dtable.DTMin, input_v1, [0,1,2,3,4,5,6,7,8,9,10])\\\n",
" .add(dtable.DTMax, input_v1, [1,2,3,4,5,6,7,8,9,10,11])\\\n",
" .add(dtable.DTMin, input_v2, [0,0,0,0,0,0,1,1,1,1,1])\\\n",
" .add(dtable.DTMax, input_v2, [1,1,1,1,1,1,2,2,2,2,2])\\\n",
" .set_default(pd.DataFrame({\"value\": [999], \"description\": [\"NA\"]}))\\\n",
" .output(\"value\", [1,2,0,None,-1,20,1,2,3,4,5])\\\n",
" .output(\"description\", [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\", \"k\"])\n"
"example_dt.add(dtable.DTMin, input_v1, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).add(\n",
" dtable.DTMax, input_v1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]\n",
").add(dtable.DTMin, input_v2, [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1]).add(\n",
" dtable.DTMax, input_v2, [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2]\n",
").set_default(\n",
" pd.DataFrame({\"value\": [999], \"description\": [\"NA\"]})\n",
").output(\n",
" \"value\", [1, 2, 0, None, -1, 20, 1, 2, 3, 4, 5]\n",
").output(\n",
" \"description\", [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\", \"k\"]\n",
")"
]
},
{
Expand Down Expand Up @@ -149,7 +152,7 @@
}
],
"source": [
"input_data = pd.DataFrame({input_v1: [5, 3, 8, 0, 10], input_v2: [0,0,0,0,4]})\n",
"input_data = pd.DataFrame({input_v1: [5, 3, 8, 0, 10], input_v2: [0, 0, 0, 0, 4]})\n",
"result_df = example_dt.execute(inputs=input_data)\n",
"result_df"
]
Expand Down Expand Up @@ -257,7 +260,7 @@
" model_name=\"demo_spock_model\",\n",
" model_version=\"1.0.0\",\n",
" namespace=\"decision_table_config\",\n",
" config=example_dt.model_dump(mode='json')\n",
" config=example_dt.model_dump(mode=\"json\"),\n",
")\n",
"\n",
"# Load configuration\n",
Expand Down
57 changes: 37 additions & 20 deletions docs/concepts/decision_trees.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@
"source": [
"from typing_extensions import TypedDict\n",
"\n",
"\n",
"class Reject(TypedDict):\n",
" code: int\n",
" description: str\n",
"\n",
"\n",
"RejectAction = Action[Reject]"
]
},
Expand Down Expand Up @@ -145,6 +147,8 @@
"@tree.condition(output=RejectAction(code=102, description=\"My first condition\"))\n",
"def first_condition(d: pd.Series, e: pd.Series, f: pd.Series) -> pd.Series:\n",
" return (d > 5) & (e > 5) & (f > 5)\n",
"\n",
"\n",
"tree.visualize(get_value_name=lambda x: x[\"description\"][0])"
]
},
Expand Down Expand Up @@ -284,13 +288,21 @@
"def condition_a(a: pd.Series) -> pd.Series:\n",
" return a > 5\n",
"\n",
"@condition_a.condition(output=RejectAction(code=100, description=\"a and b are out of range\"))\n",
"\n",
"@condition_a.condition(\n",
" output=RejectAction(code=100, description=\"a and b are out of range\")\n",
")\n",
"def condition_b(b: pd.Series) -> pd.Series:\n",
" return b > 5\n",
"\n",
"@condition_a.condition(output=RejectAction(code=101, description=\"a and c are out of range\"))\n",
"\n",
"@condition_a.condition(\n",
" output=RejectAction(code=101, description=\"a and c are out of range\")\n",
")\n",
"def condition_c(c: pd.Series) -> pd.Series:\n",
" return c > 5\n",
"\n",
"\n",
"tree.visualize(get_value_name=lambda x: x[\"description\"][0])"
]
},
Expand Down Expand Up @@ -597,17 +609,19 @@
}
],
"source": [
"test_data = pd.DataFrame({\n",
" \"a\": [5,6,7,8,1,2,3,4],\n",
" \"b\": [1,0,0,0,2,0,0,0],\n",
" \"c\": [0,10,0,0,0,10,0,0],\n",
" \"d\": [0,0,10,0,0,0,0,0],\n",
" \"e\": [0,0,10,0,0,0,0,0],\n",
" \"f\": [0,0,10,0,0,0,0,0],\n",
" # Below used later in the tutorial\n",
" \"nums\": [1,2,3,4,5,6,7,8],\n",
" \"input_condition\": [True, True, False, False, False, False, True, True],\n",
"})\n",
"test_data = pd.DataFrame(\n",
" {\n",
" \"a\": [5, 6, 7, 8, 1, 2, 3, 4],\n",
" \"b\": [1, 0, 0, 0, 2, 0, 0, 0],\n",
" \"c\": [0, 10, 0, 0, 0, 10, 0, 0],\n",
" \"d\": [0, 0, 10, 0, 0, 0, 0, 0],\n",
" \"e\": [0, 0, 10, 0, 0, 0, 0, 0],\n",
" \"f\": [0, 0, 10, 0, 0, 0, 0, 0],\n",
" # Below used later in the tutorial\n",
" \"nums\": [1, 2, 3, 4, 5, 6, 7, 8],\n",
" \"input_condition\": [True, True, False, False, False, False, True, True],\n",
" }\n",
")\n",
"test_data"
]
},
Expand Down Expand Up @@ -874,7 +888,10 @@
}
],
"source": [
"tree.condition(condition=\"input_condition\", output=RejectAction(code=101, description=\"Input condition\"))\n",
"tree.condition(\n",
" condition=\"input_condition\",\n",
" output=RejectAction(code=101, description=\"Input condition\"),\n",
")\n",
"tree.visualize(get_value_name=lambda x: x[\"description\"][0])"
]
},
Expand Down Expand Up @@ -1177,16 +1194,16 @@
],
"source": [
"tree = Tree()\n",
"tree.condition(output=Action(value=10), condition='A')\n",
"tree.condition(output=Action(value=20), condition='B')\n",
"tree.condition(output=Action(value=10), condition=\"A\")\n",
"tree.condition(output=Action(value=20), condition=\"B\")\n",
"\n",
"subtree = Tree()\n",
"subtree.condition(output=Action(value=100), condition='SubA')\n",
"subtree.condition(output=Action(value=200), condition='SubB')\n",
"subtree.condition(output=Action(value=100), condition=\"SubA\")\n",
"subtree.condition(output=Action(value=200), condition=\"SubB\")\n",
"\n",
"cond_subtree = Tree()\n",
"cond_subtree.condition(output=Action(value=1000), condition='SubD')\n",
"cond_subtree.condition(output=Action(value=2000), condition='SubE')\n",
"cond_subtree.condition(output=Action(value=1000), condition=\"SubD\")\n",
"cond_subtree.condition(output=Action(value=2000), condition=\"SubE\")\n",
"\n",
"tree.include_subtree(subtree)\n",
"tree.include_subtree(cond_subtree, condition=\"SubC\")\n",
Expand Down
24 changes: 13 additions & 11 deletions docs/concepts/scorecard.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
"var_2 = \"var_2\"\n",
"\n",
"sc = scorecard.ScoreCard(\n",
" bin_prefix='SCORE_BIN_',\n",
" score_prefix='SCORE_VALUE_',\n",
" description_prefix='SCORE_DESC_'\n",
" bin_prefix=\"SCORE_BIN_\",\n",
" score_prefix=\"SCORE_VALUE_\",\n",
" description_prefix=\"SCORE_DESC_\",\n",
")"
]
},
Expand Down Expand Up @@ -109,8 +109,8 @@
"source": [
"sc.add_criteria(\n",
" scorecard.ScoreCriteria(var_2, \"categorical\", default_behavior=\"regex\")\n",
" .add_discrete_score(['a', 'b', 'c'], 10, \"First pattern var_2\")\n",
" .add_discrete_score(['[b-z]'], 20, \"Second pattern var_2\")\n",
" .add_discrete_score([\"a\", \"b\", \"c\"], 10, \"First pattern var_2\")\n",
" .add_discrete_score([\"[b-z]\"], 20, \"Second pattern var_2\")\n",
")"
]
},
Expand Down Expand Up @@ -241,10 +241,12 @@
}
],
"source": [
"test_data = pd.DataFrame({\n",
" \"var_1\": [ 0, 1, 2, None, 0, 1, 2, None],\n",
" \"var_2\": [ 'a', 'b', 'z', 'a', 'a', 'b', 'z', '9'],\n",
"})\n",
"test_data = pd.DataFrame(\n",
" {\n",
" \"var_1\": [0, 1, 2, None, 0, 1, 2, None],\n",
" \"var_2\": [\"a\", \"b\", \"z\", \"a\", \"a\", \"b\", \"z\", \"9\"],\n",
" }\n",
")\n",
"test_data"
]
},
Expand Down Expand Up @@ -424,7 +426,7 @@
" model_name=\"demo_spock_model\",\n",
" model_version=\"1.0.0\",\n",
" namespace=\"scorecard_config\",\n",
" config=sc.model_dump(mode='json')\n",
" config=sc.model_dump(mode=\"json\"),\n",
")"
]
},
Expand Down Expand Up @@ -458,7 +460,7 @@
}
],
"source": [
"config = conf_manager.get_config(\"demo_spock_model\", \"1.0.0\")['scorecard_config']\n",
"config = conf_manager.get_config(\"demo_spock_model\", \"1.0.0\")[\"scorecard_config\"]\n",
"sc_loaded = scorecard.ScoreCard.from_config(\"\").load(config)\n",
"\n",
"# Retrieve view model and display widget\n",
Expand Down
53 changes: 38 additions & 15 deletions docs/getting_started/quick_start.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@
"from typing_extensions import TypedDict\n",
"import pandas as pd\n",
"\n",
"\n",
"class Reject(TypedDict):\n",
" code: int\n",
" description: str\n",
"\n",
"\n",
"RejectAction = Action[Reject]"
]
},
Expand Down Expand Up @@ -106,11 +108,17 @@
"def condition_a(a: pd.Series) -> pd.Series:\n",
" return a > 5\n",
"\n",
"@condition_a.condition(output=RejectAction(code=100, description=\"a and b is out of range\"))\n",
"\n",
"@condition_a.condition(\n",
" output=RejectAction(code=100, description=\"a and b is out of range\")\n",
")\n",
"def condition_b(b: pd.Series) -> pd.Series:\n",
" return b > 5\n",
"\n",
"@condition_a.condition(output=RejectAction(code=101, description=\"a and c is out of range\"))\n",
"\n",
"@condition_a.condition(\n",
" output=RejectAction(code=101, description=\"a and c is out of range\")\n",
")\n",
"def condition_c(c: pd.Series) -> pd.Series:\n",
" return c > 5"
]
Expand Down Expand Up @@ -286,15 +294,17 @@
}
],
"source": [
"test_data = pd.DataFrame({\n",
" \"num\": [1,2,3,4,5,6,7,8], # Note this is for use later in the tutorial\n",
" \"a\": [5,6,7,8,1,2,3,4],\n",
" \"b\": [1,0,0,0,2,0,0,0],\n",
" \"c\": [0,10,0,0,0,10,0,0],\n",
" \"d\": [0,0,10,0,0,0,0,0],\n",
" \"e\": [0,0,10,0,0,0,0,0],\n",
" \"f\": [0,0,10,0,0,0,0,0],\n",
"})\n",
"test_data = pd.DataFrame(\n",
" {\n",
" \"num\": [1, 2, 3, 4, 5, 6, 7, 8], # Note this is for use later in the tutorial\n",
" \"a\": [5, 6, 7, 8, 1, 2, 3, 4],\n",
" \"b\": [1, 0, 0, 0, 2, 0, 0, 0],\n",
" \"c\": [0, 10, 0, 0, 0, 10, 0, 0],\n",
" \"d\": [0, 0, 10, 0, 0, 0, 0, 0],\n",
" \"e\": [0, 0, 10, 0, 0, 0, 0, 0],\n",
" \"f\": [0, 0, 10, 0, 0, 0, 0, 0],\n",
" }\n",
")\n",
"test_data"
]
},
Expand Down Expand Up @@ -810,6 +820,7 @@
],
"source": [
"from spockflow.core import Driver\n",
"\n",
"dr = Driver({}, demo_tree)\n",
"df = dr.execute(inputs=test_data)\n",
"df"
Expand Down Expand Up @@ -916,6 +927,7 @@
],
"source": [
"from hamilton.driver import Driver as HamiltonDriver\n",
"\n",
"dr_ham = HamiltonDriver({}, demo_tree)\n",
"df = dr_ham.execute(inputs=test_data, final_vars=[\"tree\"])\n",
"df"
Expand Down Expand Up @@ -1171,7 +1183,11 @@
}
],
"source": [
"df = dr.execute(inputs=test_data, final_vars=[\"condition_b\",\"tree\"], overrides={\"b\": test_data[\"b\"]})\n",
"df = dr.execute(\n",
" inputs=test_data,\n",
" final_vars=[\"condition_b\", \"tree\"],\n",
" overrides={\"b\": test_data[\"b\"]},\n",
")\n",
"df"
]
},
Expand Down Expand Up @@ -1422,7 +1438,11 @@
}
],
"source": [
"dr.visualize_execution(inputs=test_data, final_vars=[\"condition_b\",\"tree\"], overrides={\"b\": test_data[\"b\"]})"
"dr.visualize_execution(\n",
" inputs=test_data,\n",
" final_vars=[\"condition_b\", \"tree\"],\n",
" overrides={\"b\": test_data[\"b\"]},\n",
")"
]
},
{
Expand Down Expand Up @@ -1538,6 +1558,7 @@
"import os\n",
"import json\n",
"import requests\n",
"\n",
"os.environ[\"MODEL_PREFIX\"] = os.path.abspath(\".\")\n",
"os.environ[\"MODEL_RELATIVE_PATH\"] = \"source_dir\""
]
Expand Down Expand Up @@ -1645,8 +1666,10 @@
}
],
"source": [
"resp = requests.post(\"http://localhost:8000/invocations\", json=test_data.to_dict(orient='records'))\n",
"pd.DataFrame(resp.json()['tree'])"
"resp = requests.post(\n",
" \"http://localhost:8000/invocations\", json=test_data.to_dict(orient=\"records\")\n",
")\n",
"pd.DataFrame(resp.json()[\"tree\"])"
]
},
{
Expand Down
Loading
Loading