master-thesis/RecipeAnalysis/AdjacencyMatrixRefinement.i...

339 lines
10 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Further Refinement of raw Adjacency Matrices"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"sys.path.append(\"../\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/jonas/.local/lib/python3.7/site-packages/ipykernel_launcher.py:5: TqdmExperimentalWarning: Using `tqdm.autonotebook.tqdm` in notebook mode. Use `tqdm.tqdm` instead to force console mode (e.g. in jupyter console)\n",
" \"\"\"\n"
]
},
{
"data": {
"text/html": [
" <script type=\"text/javascript\">\n",
" window.PlotlyConfig = {MathJaxConfig: 'local'};\n",
" if (window.MathJax) {MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}\n",
" if (typeof require !== 'undefined') {\n",
" require.undef(\"plotly\");\n",
" requirejs.config({\n",
" paths: {\n",
" 'plotly': ['https://cdn.plot.ly/plotly-latest.min']\n",
" }\n",
" });\n",
" require(['plotly'], function(Plotly) {\n",
" window._Plotly = Plotly;\n",
" });\n",
" }\n",
" </script>\n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import dill\n",
"import numpy as np\n",
"import settings\n",
"import AdjacencyMatrix\n",
"from tqdm.autonotebook import tqdm\n",
"from Recipe import Ingredient\n",
"from ActionGroups import groups"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"m_act = dill.load(open(\"m_act_raw.dill\", \"rb\"))\n",
"m_mix = dill.load(open(\"m_mix_raw.dill\", \"rb\"))\n",
"m_base_act = dill.load(open(\"m_base_act_raw.dill\", \"rb\"))\n",
"m_base_mix = dill.load(open(\"m_base_mix_raw.dill\", \"rb\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Grouping Actions"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"groups = {\n",
" #'place':None,\n",
" 'heat':'heat',\n",
" 'cook':'heat',\n",
" 'bake':'heat',\n",
" 'grill':'heat',\n",
" 'melt':'heat',\n",
" 'blend':None,\n",
" 'beat':'prepare',\n",
" 'cool':'cool',\n",
" 'brown':'heat',\n",
" 'cut':'prepare',\n",
" 'chill':'cool',\n",
" 'drain':None,\n",
" 'boil':'heat',\n",
" 'simmer':'heat',\n",
" 'pour':None,\n",
" 'freeze':'cool',\n",
" 'saute':'heat',\n",
" 'rinse':'prepare',\n",
" 'warm':'heat',\n",
" 'wash':'prepare',\n",
" 'knead':'prepare',\n",
" 'peel':'prepare',\n",
" 'parboil':'heat',\n",
" 'break':'prepare',\n",
" 'broil':'heat',\n",
" 'scorch':'heat',\n",
" 'skim':None,\n",
" 'fry':'heat',\n",
" 'refrigerate':'cool',\n",
" 'burn':'heat',\n",
" 'thicken':None,\n",
" 'grate':'prepare',\n",
" 'brush':'prepare',\n",
" 'open':'prepare',\n",
" 'crack':'prepare',\n",
" 'poach':'heat',\n",
" 'slice':'prepare',\n",
" 'whisk':None,\n",
" 'dice':'prepare',\n",
" 'marinate':None,\n",
" 'whip':None,\n",
" 'sour':None,\n",
" 'soak':None,\n",
" 'steam':'heat',\n",
" 'chop':'prepare',\n",
" 'mince':None,\n",
" 'mash':'prepare',\n",
" 'squeeze':'prepare',\n",
" 'wipe':'prepare',\n",
" 'thaw':'prepare',\n",
" 'curdle':'heat',\n",
" 'sweeten':None,\n",
" 'baste':None,\n",
" 'carve':None,\n",
" 'grind':'prepare',\n",
" 'debone':'prepare',\n",
" 'steep':None,\n",
" 'clarify':None,\n",
" 'macerate':'prepare',\n",
" #'spread':None,\n",
" 'crumple':'prepare',\n",
" 'braise':'heat',\n",
" 'gut':None,\n",
" 'bury':None\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* now refactor the matrices to new versions that only contain those groups"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# create new matrices:\n",
"m_grouped_act = AdjacencyMatrix.adj_matrix()\n",
"m_grouped_mix = AdjacencyMatrix.adj_matrix(True)\n",
"m_grouped_base_act = AdjacencyMatrix.adj_matrix()\n",
"#m_grouped_base_mix = AdjacencyMatrix.adj_matrix(True)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "77d9643f1116425eb40c8664edca0bf9",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(FloatProgress(value=0.0, max=467050.0), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"ename": "KeyError",
"evalue": "'spread'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-6-8156ba150c10>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mgrouped_ing\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mIngredient\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ming\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_base_ingredient\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0ma\u001b[0m \u001b[0;32min\u001b[0m \u001b[0ming\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_action_set\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0mgrouped_ing\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply_action\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgroups\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 12\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0mgrouped_act\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgroups\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mact\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyError\u001b[0m: 'spread'"
]
}
],
"source": [
"c = m_act.get_csr()\n",
"\n",
"label_acts, labels_ings = m_act.get_labels()\n",
"acts, ings = c.nonzero()\n",
"for i_act,j_ing in tqdm(zip(acts,ings), total=len(acts)):\n",
" ing = Ingredient.from_json(labels_ings[j_ing])\n",
" act = label_acts[i_act]\n",
" \n",
" grouped_ing = Ingredient(ing._base_ingredient)\n",
" for a in ing._action_set:\n",
" grouped_ing.apply_action(groups[a])\n",
" \n",
" grouped_act = groups[act]\n",
" \n",
" m_grouped_act.add_entry(grouped_act, grouped_ing.to_json(),c[i_act, j_ing])\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c = m_mix.get_csr()\n",
"\n",
"labels_ings = m_mix.get_labels()\n",
"ings_a, ings_b = c.nonzero()\n",
"for i_ing,j_ing in tqdm(zip(ings_a,ings_b), total=len(ings_a)):\n",
" ing_a = Ingredient.from_json(labels_ings[i_ing])\n",
" ing_b = Ingredient.from_json(labels_ings[j_ing])\n",
" \n",
" grouped_ing_a = Ingredient(ing_a._base_ingredient)\n",
" for a in ing_a._action_set:\n",
" grouped_ing_a.apply_action(groups[a])\n",
" \n",
" grouped_ing_b = Ingredient(ing_b._base_ingredient)\n",
" for a in ing_b._action_set:\n",
" grouped_ing_b.apply_action(groups[a])\n",
" \n",
" m_grouped_mix.add_entry(grouped_ing_a.to_json(), grouped_ing_b.to_json(),c[i_ing, j_ing])"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d4064d730bd34f49946f54b845738585",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(FloatProgress(value=0.0, max=78714.0), HTML(value='')))"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"c = m_base_act.get_csr()\n",
"\n",
"label_acts, labels_ings = m_base_act.get_labels()\n",
"acts, ings = c.nonzero()\n",
"for i_act,j_ing in tqdm(zip(acts,ings), total=len(acts)):\n",
" base_ing = labels_ings[j_ing]\n",
" act = label_acts[i_act]\n",
" \n",
" grouped_act = groups[act]\n",
" \n",
" m_grouped_base_act.add_entry(grouped_act, base_ing,c[i_act,j_ing])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"dill.dump(m_grouped_act, file=open(\"m_grouped_act_raw.dill\", 'wb'))\n",
"dill.dump(m_grouped_mix, file=open(\"m_grouped_mix_raw.dill\", 'wb'))\n",
"dill.dump(m_grouped_base_act, file=open(\"m_grouped_base_act_raw.dill\", 'wb'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}