master-thesis/RecipeAnalysis/MatrixGeneration.ipynb

1 line
9.3 KiB
Plaintext
Raw Normal View History

{"cells":[{"cell_type":"markdown","metadata":{},"source":"# Matrix Generation"},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[{"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 sys\nsys.path.append(\"../\")\nfrom Recipe import Recipe, Ingredient, RecipeGraph\n\nimport settings\nimport db.db_settings as db_settings\nfrom db.database_connection import DatabaseConnection\n\nimport random\n\nimport itertools\n\nimport numpy as np"},{"cell_type":"code","execution_count":2,"metadata":{},"outputs":[{"data":{"text/plain":"<db.database_connection.DatabaseConnection at 0x7f4ea28288d0>"},"execution_count":2,"metadata":{},"output_type":"execute_result"}],"source":"DatabaseConnection(db_settings.db_host,\n db_settings.db_port,\n db_settings.db_user,\n db_settings.db_pw,\n db_settings.db_db,\n db_settings.db_charset)"},{"cell_type":"code","execution_count":3,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"CPU times: user 8.56 s, sys: 924 ms, total: 9.48 s\nWall time: 9.5 s\n"}],"source":"%time ids = DatabaseConnection.global_single_query(\"select id from recipes\")"},{"cell_type":"code","execution_count":4,"metadata":{},"outputs":[],"source":"import AdjacencyMatrix"},{"cell_type":"markdown","metadata":{},"source":"* create Adjacency Matrix"},{"cell_type":"code","execution_count":11,"metadata":{},"outputs":[],"source":"def add_entries_from_rec_state(rec_state, m_act, m_mix, m_base_act, m_base_mix):\n mix_m, mix_label = rec_state.get_mixing_matrix()\n act_m, act_a, act_i = rec_state.get_action_matrix()\n\n # create list of tuples: [action, ingredient]\n seen_actions = np.array(list(itertools.product(act_a,act_i))).reshape((len(act_a), len(act_i), 2))\n\n # create list of tuples [ingredient, ingredient]\n seen_mixes = np.array(list(itertools.product(mix_label,mix_label))).reshape((len(mix_label), len(mix_label), 2))\n\n seen_actions = seen_actions[act_m == 1]\n seen_mixes = seen_mixes[mix_m == 1]\n\n seen_actions = set([tuple(x) for x in seen_actions.tolist()])\n seen_mixes = set([tuple(x) for x in seen_mixes.tolist()])\n \n seen_base_actions = set()\n seen_base_mixes = set()\n \n for act, ing in seen_actions:\n m_act.add_entry(act, ing.to_json(), 1)\n if (act, ing._base_ingredient) not in seen_base_actions:\n seen_base_actions.add((act, ing._base_ingredient))\n m_base_act.add_entry(act, ing._base_ingredient, 1)\n \n for x,y in seen_mixes:\n xj = x.to_json()\n yj = y.to_json()\n if xj < yj:\n m_mix.add_entry(xj,yj,1)\n if (x._base_ingredient, y._base_ingredient) not in seen_base_mixes:\n seen_base_mixes.add((x._base_ingredient, y._base_ingredient))\n m_base_mix.add_entry(x._base_ingredient, y._base_ingredient, 1)\n"},{"cell_type":"code","execution_count":12,"metadata":{},"outputs":[],"source":"m_act = AdjacencyMatrix.adj_matrix()\nm_mix = AdjacencyMatrix.adj_matrix(True)\nm_base_act = AdjacencyMatrix.adj_matrix()\nm_base_mix = AdjacencyMatrix.adj_matrix(True)"},{"cell_type":"code","execution_count":13,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"warning: recipe b4fc8f359d has no ingredient! skipping it\nwarning: recipe f288592241 has no ingredient! skipping it\nwarning: recipe 4dbdc1d0b5 has no ingredient! skipping it\nwarning: recipe 37af7ba84f has