Skip to content
Open
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.venv
debug.jsonl
raiconfig.toml
relationalai-vscode.toml
25 changes: 25 additions & 0 deletions samples/tastybytes-communities/README.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
]
Expand All @@ -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)"
]
},
{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)"
]
},
{
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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)"
]
},
{
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
"})"
]
Expand Down Expand Up @@ -508,7 +504,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.6"
}
},
"nbformat": 4,
Expand Down
2 changes: 2 additions & 0 deletions samples/tastybytes-communities/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
relationalai
jupyter
3 changes: 2 additions & 1 deletion samples/telecom_demo/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
app/.streamlit
app/__pycache__
app/__pycache__
raiconfig.toml