diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f267f5c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.venv +debug.jsonl +raiconfig.toml +relationalai-vscode.toml \ No newline at end of file diff --git a/samples/tastybytes-communities/README.md b/samples/tastybytes-communities/README.md new file mode 100644 index 0000000..63ed11d --- /dev/null +++ b/samples/tastybytes-communities/README.md @@ -0,0 +1,25 @@ + +# TastyBytes: Community Detection + +## Setup + +1. From this directory, do + + ```sh + python -m venv .venv + source .venv/bin/activate + ``` + +2. Then install dependencies with: + + ```sh + pip install -r requirements.txt + ``` + +3. Lastly, do `rai init` to create a local `raiconfig.toml` file. + +## Running the notebook + +4. Do `jupyter` from the command line to start the Jupyter notebook server and open a browser. + +5. Do shift-enter to run each cell in the notebook. \ No newline at end of file diff --git a/samples/tastybytes-communities/community_detection_RelationalAI_V1.ipynb b/samples/tastybytes-communities/community_detection_RelationalAI_V1.ipynb index dd0b1dd..60daf47 100644 --- a/samples/tastybytes-communities/community_detection_RelationalAI_V1.ipynb +++ b/samples/tastybytes-communities/community_detection_RelationalAI_V1.ipynb @@ -43,10 +43,8 @@ "\n", "import relationalai as rai\n", "from relationalai.clients.snowflake import Snowflake\n", - "from relationalai.std import aggregates, alias, rel\n", + "from relationalai.std import aggregates, rel\n", "from relationalai.std.graphs import Graph\n", - "from relationalai.std import Vars\n", - "\n", "\n", "random.seed(123)" ] @@ -65,7 +63,8 @@ "metadata": {}, "outputs": [], "source": [ - "model = rai.Model(\"LOYALTY_ORDERS_REGION_CALIFORNIA\")" + "model = rai.Model(\"LOYALTY_ORDERS_REGION_CALIFORNIA\")\n", + "model._config.set(\"compiler.use_multi_valued\", True)" ] }, { @@ -145,7 +144,7 @@ "outputs": [], "source": [ "# Define Customer Type\n", - "with model.rule(dynamic=True):\n", + "with model.rule():\n", " r = Record()\n", " Customer.add(customer_id=r.customer_id)\n", "\n", @@ -236,7 +235,7 @@ " t1.customer_id != t2.customer_id\n", " rel.abs(t1.order_ts_seconds - t2.order_ts_seconds) <= 1200\n", "\n", - " t1.connected.add(t2) " + " t1.connected.add(t2)" ] }, { @@ -286,7 +285,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Get the total occurrences where pairs of customers coexisted together more than once\n", + "# Get the number of occurrences where pairs of customers coexisted together more than once\n", "with model.query() as select:\n", " record = RelevantConnection()\n", " num_records = aggregates.count(record)\n", @@ -311,7 +310,9 @@ "source": [ "community_graph = Graph(model, undirected=True)\n", "\n", - "# Add edges to the graph between customers / Nodes will be added automatically\n", + "community_graph.Node.extend(Customer, customer_id=Customer.customer_id)\n", + "\n", + "# Add edges to the graph between customers\n", "with model.rule():\n", " connection = RelevantConnection()\n", " community_graph.Edge.add(\n", @@ -336,15 +337,9 @@ "outputs": [], "source": [ "with model.rule():\n", - " customer = Customer()\n", + " customer = community_graph.Node()\n", " community_id = community_graph.compute.louvain(customer)\n", - " customer.set(community_id=community_id)\n", - "\n", - " community_graph.Node.add(\n", - " customer,\n", - " community_id=community_id,\n", - " customer_id=customer.customer_id\n", - " )" + " customer.set(community_id=community_id)" ] }, { @@ -390,7 +385,8 @@ "num_communities = len(community_set)\n", "print(f\"Number of communities: {num_communities}\")\n", "\n", - "random_colors = generate_random_colors(num_communities)\n", + "random_colors = generate_random_colors(num_communities + 1)\n", + "none_color = random_colors[-1]\n", "\n", "community_colors = {}\n", "for i, community in enumerate(community_set):\n", @@ -410,7 +406,7 @@ " node_hover_neighborhood=True,\n", " style={\n", " \"node\": {\n", - " \"color\": lambda x : community_colors[x['community_id']],\n", + " \"color\": lambda n : community_colors[n['community_id']] if 'community_id' in n else none_color,\n", " \"hover\": lambda x : f\"{x['customer_id']}\"\n", " },\n", " \"edge\": {\n", @@ -434,7 +430,7 @@ "metadata": {}, "outputs": [], "source": [ - "def get_community_id_of_customer( customer_id ):\n", + "def get_community_id_of_customer(customer_id):\n", " mycommunity = None\n", " for i in data.values():\n", " for p in i.values():\n", @@ -471,15 +467,15 @@ " node_hover_neighborhood=True,\n", " style={\n", " \"node\": {\n", - " \"color\": lambda x : community_colors_focus[x['community_id']],\n", - " \"size\" : lambda x : 20 if x['community_id'] == community_id_of_interest else 2,\n", - " \"hover\": lambda x : f\"{x['customer_id']}\"\n", + " \"color\": lambda n : community_colors_focus[n['community_id']] if 'community_id' in n else none_color,\n", + " \"size\" : lambda n : 20 if n.get('community_id', None) == community_id_of_interest else 2,\n", + " \"hover\": lambda n : f\"{n['customer_id']}\"\n", " },\n", " \"edge\": {\n", " \"opacity\": 0.8,\n", " \"color\": \"#ccc\",\n", " \n", - " \"hover\": lambda x : x['weight']\n", + " \"hover\": lambda e : e['weight']\n", " }\n", "})" ] @@ -508,7 +504,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.11.6" } }, "nbformat": 4, diff --git a/samples/tastybytes-communities/requirements.txt b/samples/tastybytes-communities/requirements.txt new file mode 100644 index 0000000..9a9a1d2 --- /dev/null +++ b/samples/tastybytes-communities/requirements.txt @@ -0,0 +1,2 @@ +relationalai +jupyter \ No newline at end of file diff --git a/samples/telecom_demo/.gitignore b/samples/telecom_demo/.gitignore index e9ed65b..0e0f111 100644 --- a/samples/telecom_demo/.gitignore +++ b/samples/telecom_demo/.gitignore @@ -1,2 +1,3 @@ app/.streamlit -app/__pycache__ \ No newline at end of file +app/__pycache__ +raiconfig.toml \ No newline at end of file