mirror of
https://github.com/fhswf/aki_prj23_transparenzregister.git
synced 2025-08-12 03:48:34 +02:00
Added test
This commit is contained in:
2126
Jupyter/NetworkX/archive/Dev.ipynb
Normal file
2126
Jupyter/NetworkX/archive/Dev.ipynb
Normal file
File diff suppressed because one or more lines are too long
919
Jupyter/NetworkX/archive/networkX_with_sql.ipynb
Normal file
919
Jupyter/NetworkX/archive/networkX_with_sql.ipynb
Normal file
@@ -0,0 +1,919 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os.path\n",
|
||||
"\n",
|
||||
"import pandas as pd\n",
|
||||
"\n",
|
||||
"# if not os.path.exists(\"src\"):\n",
|
||||
"# %cd \"../\"\n",
|
||||
"# os.path.abspath(\".\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from aki_prj23_transparenzregister.utils.sql import entities\n",
|
||||
"from sqlalchemy.orm import aliased\n",
|
||||
"from sqlalchemy import func, text\n",
|
||||
"\n",
|
||||
"# Alias for Company table for the base company\n",
|
||||
"base_company = aliased(entities.Company, name=\"base_company\")\n",
|
||||
"\n",
|
||||
"# Alias for Company table for the head company\n",
|
||||
"head_company = aliased(entities.Company, name=\"head_company\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from aki_prj23_transparenzregister.config.config_providers import JsonFileConfigProvider\n",
|
||||
"from aki_prj23_transparenzregister.utils.sql.connector import get_session\n",
|
||||
"\n",
|
||||
"session = get_session(JsonFileConfigProvider(\"../secrets.json\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'SELECT base_company.name AS name_company_base, relation.relation AS relation_type, head_company.name AS name_company_head \\nFROM company AS base_company JOIN (relation JOIN company_relation ON relation.id = company_relation.id) ON relation.company_id = base_company.id JOIN company AS head_company ON company_relation.company2_id = head_company.id'"
|
||||
]
|
||||
},
|
||||
"execution_count": 21,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Query to fetch relations between companies\n",
|
||||
"relations_query = (\n",
|
||||
" session.query(\n",
|
||||
" base_company.name.label(\"name_company_base\"),\n",
|
||||
" entities.CompanyRelation.relation.label(\"relation_type\"),\n",
|
||||
" head_company.name.label(\"name_company_head\"),\n",
|
||||
" )\n",
|
||||
" .join(\n",
|
||||
" entities.CompanyRelation,\n",
|
||||
" entities.CompanyRelation.company_id == base_company.id,\n",
|
||||
" )\n",
|
||||
" .join(\n",
|
||||
" head_company,\n",
|
||||
" entities.CompanyRelation.company2_id == head_company.id,\n",
|
||||
" )\n",
|
||||
")\n",
|
||||
"str(relations_query)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"121 ms ± 9.27 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%timeit pd.read_sql_query(str(relations_query), session.bind)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<div>\n",
|
||||
"<style scoped>\n",
|
||||
" .dataframe tbody tr th:only-of-type {\n",
|
||||
" vertical-align: middle;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe tbody tr th {\n",
|
||||
" vertical-align: top;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe thead th {\n",
|
||||
" text-align: right;\n",
|
||||
" }\n",
|
||||
"</style>\n",
|
||||
"<table border=\"1\" class=\"dataframe\">\n",
|
||||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>name_company_base</th>\n",
|
||||
" <th>relation_type</th>\n",
|
||||
" <th>name_company_head</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2. Schaper Objekt GmbH & Co. Kiel KG</td>\n",
|
||||
" <td>KOMMANDITIST</td>\n",
|
||||
" <td>Multi-Center Warenvertriebs GmbH</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>Alb-Windkraft GmbH & Co. KG</td>\n",
|
||||
" <td>KOMMANDITIST</td>\n",
|
||||
" <td>EnBW Windkraftprojekte GmbH</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>Anneliese Köster GmbH & Co. KG</td>\n",
|
||||
" <td>KOMMANDITIST</td>\n",
|
||||
" <td>INDUS Holding Aktiengesellschaft</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>AURELIUS Equity Opportunities SE & Co. KGaA</td>\n",
|
||||
" <td>HAFTENDER_GESELLSCHAFTER</td>\n",
|
||||
" <td>AURELIUS Management SE</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>Aurelius KG</td>\n",
|
||||
" <td>HAFTENDER_GESELLSCHAFTER</td>\n",
|
||||
" <td>Aurelius Verwaltungs GmbH</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>...</th>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>573</th>\n",
|
||||
" <td>Zalando BTD 011 SE & Co. KG</td>\n",
|
||||
" <td>HAFTENDER_GESELLSCHAFTER</td>\n",
|
||||
" <td>Zalando SE</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>574</th>\n",
|
||||
" <td>Zalando BTD 011 SE & Co. KG</td>\n",
|
||||
" <td>KOMMANDITIST</td>\n",
|
||||
" <td>Zalando Operations GmbH</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>575</th>\n",
|
||||
" <td>zLabels Creation & Sales GmbH & Co. KG</td>\n",
|
||||
" <td>HAFTENDER_GESELLSCHAFTER</td>\n",
|
||||
" <td>zLabels GmbH</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>576</th>\n",
|
||||
" <td>Zalando Customer Care International SE & Co. KG</td>\n",
|
||||
" <td>HAFTENDER_GESELLSCHAFTER</td>\n",
|
||||
" <td>Zalando SE</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>577</th>\n",
|
||||
" <td>Zalando Customer Care International SE & Co. KG</td>\n",
|
||||
" <td>KOMMANDITIST</td>\n",
|
||||
" <td>Zalando Operations GmbH</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"<p>578 rows × 3 columns</p>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" name_company_base \\\n",
|
||||
"0 2. Schaper Objekt GmbH & Co. Kiel KG \n",
|
||||
"1 Alb-Windkraft GmbH & Co. KG \n",
|
||||
"2 Anneliese Köster GmbH & Co. KG \n",
|
||||
"3 AURELIUS Equity Opportunities SE & Co. KGaA \n",
|
||||
"4 Aurelius KG \n",
|
||||
".. ... \n",
|
||||
"573 Zalando BTD 011 SE & Co. KG \n",
|
||||
"574 Zalando BTD 011 SE & Co. KG \n",
|
||||
"575 zLabels Creation & Sales GmbH & Co. KG \n",
|
||||
"576 Zalando Customer Care International SE & Co. KG \n",
|
||||
"577 Zalando Customer Care International SE & Co. KG \n",
|
||||
"\n",
|
||||
" relation_type name_company_head \n",
|
||||
"0 KOMMANDITIST Multi-Center Warenvertriebs GmbH \n",
|
||||
"1 KOMMANDITIST EnBW Windkraftprojekte GmbH \n",
|
||||
"2 KOMMANDITIST INDUS Holding Aktiengesellschaft \n",
|
||||
"3 HAFTENDER_GESELLSCHAFTER AURELIUS Management SE \n",
|
||||
"4 HAFTENDER_GESELLSCHAFTER Aurelius Verwaltungs GmbH \n",
|
||||
".. ... ... \n",
|
||||
"573 HAFTENDER_GESELLSCHAFTER Zalando SE \n",
|
||||
"574 KOMMANDITIST Zalando Operations GmbH \n",
|
||||
"575 HAFTENDER_GESELLSCHAFTER zLabels GmbH \n",
|
||||
"576 HAFTENDER_GESELLSCHAFTER Zalando SE \n",
|
||||
"577 KOMMANDITIST Zalando Operations GmbH \n",
|
||||
"\n",
|
||||
"[578 rows x 3 columns]"
|
||||
]
|
||||
},
|
||||
"execution_count": 23,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"company_relations = pd.read_sql_query(str(relations_query), session.bind)\n",
|
||||
"company_relations"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"relations_query = (\n",
|
||||
" session.query(\n",
|
||||
" entities.Company.name.label(\"name_company\"),\n",
|
||||
" entities.PersonRelation.relation.label(\"relation_type\"),\n",
|
||||
" entities.Person.lastname.label(\"lastname\"),\n",
|
||||
" entities.Person.firstname.label(\"firstname\"),\n",
|
||||
" entities.Person.date_of_birth.label(\"date_of_birth\"),\n",
|
||||
" )\n",
|
||||
" .join(\n",
|
||||
" entities.PersonRelation,\n",
|
||||
" entities.PersonRelation.company_id == entities.Company.id,\n",
|
||||
" )\n",
|
||||
" .join(\n",
|
||||
" entities.Person,\n",
|
||||
" entities.PersonRelation.person_id == entities.Person.id,\n",
|
||||
" )\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"373 ms ± 25.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%timeit pd.read_sql_query(str(relations_query), session.bind)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 26,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<div>\n",
|
||||
"<style scoped>\n",
|
||||
" .dataframe tbody tr th:only-of-type {\n",
|
||||
" vertical-align: middle;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe tbody tr th {\n",
|
||||
" vertical-align: top;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe thead th {\n",
|
||||
" text-align: right;\n",
|
||||
" }\n",
|
||||
"</style>\n",
|
||||
"<table border=\"1\" class=\"dataframe\">\n",
|
||||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>name_company</th>\n",
|
||||
" <th>relation_type</th>\n",
|
||||
" <th>lastname</th>\n",
|
||||
" <th>firstname</th>\n",
|
||||
" <th>date_of_birth</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0 10 24 Telefondienste GmbH</td>\n",
|
||||
" <td>GESCHAEFTSFUEHRER</td>\n",
|
||||
" <td>Tetau</td>\n",
|
||||
" <td>Nicolas</td>\n",
|
||||
" <td>1971-01-02</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0 10 24 Telefondienste GmbH</td>\n",
|
||||
" <td>PROKURIST</td>\n",
|
||||
" <td>Dammast</td>\n",
|
||||
" <td>Lutz</td>\n",
|
||||
" <td>1966-12-06</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>1. Staiger Grundstücksverwaltung GmbH & Co. KG</td>\n",
|
||||
" <td>KOMMANDITIST</td>\n",
|
||||
" <td>Tutsch</td>\n",
|
||||
" <td>Rosemarie</td>\n",
|
||||
" <td>1941-10-09</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>1. Staiger Grundstücksverwaltung GmbH & Co. KG</td>\n",
|
||||
" <td>KOMMANDITIST</td>\n",
|
||||
" <td>Staiger</td>\n",
|
||||
" <td>Marc</td>\n",
|
||||
" <td>1969-10-22</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>1. Staiger Grundstücksverwaltung GmbH & Co. KG</td>\n",
|
||||
" <td>KOMMANDITIST</td>\n",
|
||||
" <td>Staiger</td>\n",
|
||||
" <td>Michaela</td>\n",
|
||||
" <td>1971-03-03</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>...</th>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>14891</th>\n",
|
||||
" <td>Wohnungsbaugesellschaft mit beschränkter Haftu...</td>\n",
|
||||
" <td>GESCHAEFTSFUEHRER</td>\n",
|
||||
" <td>Weirich</td>\n",
|
||||
" <td>Torsten</td>\n",
|
||||
" <td>1975-07-21</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>14892</th>\n",
|
||||
" <td>Wohnungsbaugesellschaft mit beschränkter Haftu...</td>\n",
|
||||
" <td>GESCHAEFTSFUEHRER</td>\n",
|
||||
" <td>Brusinski</td>\n",
|
||||
" <td>Bastian</td>\n",
|
||||
" <td>1980-10-29</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>14893</th>\n",
|
||||
" <td>Zalando Customer Care International SE & Co. KG</td>\n",
|
||||
" <td>PROKURIST</td>\n",
|
||||
" <td>Pape</td>\n",
|
||||
" <td>Ute</td>\n",
|
||||
" <td>1978-12-13</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>14894</th>\n",
|
||||
" <td>zebotec GmbH</td>\n",
|
||||
" <td>GESCHAEFTSFUEHRER</td>\n",
|
||||
" <td>Neff</td>\n",
|
||||
" <td>Werner</td>\n",
|
||||
" <td>1981-11-24</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>14895</th>\n",
|
||||
" <td>zebotec GmbH</td>\n",
|
||||
" <td>GESCHAEFTSFUEHRER</td>\n",
|
||||
" <td>Morris</td>\n",
|
||||
" <td>Richard</td>\n",
|
||||
" <td>1971-01-02</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"<p>14896 rows × 5 columns</p>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" name_company relation_type \\\n",
|
||||
"0 0 10 24 Telefondienste GmbH GESCHAEFTSFUEHRER \n",
|
||||
"1 0 10 24 Telefondienste GmbH PROKURIST \n",
|
||||
"2 1. Staiger Grundstücksverwaltung GmbH & Co. KG KOMMANDITIST \n",
|
||||
"3 1. Staiger Grundstücksverwaltung GmbH & Co. KG KOMMANDITIST \n",
|
||||
"4 1. Staiger Grundstücksverwaltung GmbH & Co. KG KOMMANDITIST \n",
|
||||
"... ... ... \n",
|
||||
"14891 Wohnungsbaugesellschaft mit beschränkter Haftu... GESCHAEFTSFUEHRER \n",
|
||||
"14892 Wohnungsbaugesellschaft mit beschränkter Haftu... GESCHAEFTSFUEHRER \n",
|
||||
"14893 Zalando Customer Care International SE & Co. KG PROKURIST \n",
|
||||
"14894 zebotec GmbH GESCHAEFTSFUEHRER \n",
|
||||
"14895 zebotec GmbH GESCHAEFTSFUEHRER \n",
|
||||
"\n",
|
||||
" lastname firstname date_of_birth \n",
|
||||
"0 Tetau Nicolas 1971-01-02 \n",
|
||||
"1 Dammast Lutz 1966-12-06 \n",
|
||||
"2 Tutsch Rosemarie 1941-10-09 \n",
|
||||
"3 Staiger Marc 1969-10-22 \n",
|
||||
"4 Staiger Michaela 1971-03-03 \n",
|
||||
"... ... ... ... \n",
|
||||
"14891 Weirich Torsten 1975-07-21 \n",
|
||||
"14892 Brusinski Bastian 1980-10-29 \n",
|
||||
"14893 Pape Ute 1978-12-13 \n",
|
||||
"14894 Neff Werner 1981-11-24 \n",
|
||||
"14895 Morris Richard 1971-01-02 \n",
|
||||
"\n",
|
||||
"[14896 rows x 5 columns]"
|
||||
]
|
||||
},
|
||||
"execution_count": 26,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_sql_query(str(relations_query), session.bind)\n",
|
||||
"df"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 27,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<div>\n",
|
||||
"<style scoped>\n",
|
||||
" .dataframe tbody tr th:only-of-type {\n",
|
||||
" vertical-align: middle;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe tbody tr th {\n",
|
||||
" vertical-align: top;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe thead th {\n",
|
||||
" text-align: right;\n",
|
||||
" }\n",
|
||||
"</style>\n",
|
||||
"<table border=\"1\" class=\"dataframe\">\n",
|
||||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>person_id</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2520</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>4993</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>3202</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>4611</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>4095</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>...</th>\n",
|
||||
" <td>...</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1804</th>\n",
|
||||
" <td>3565</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1805</th>\n",
|
||||
" <td>3510</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1806</th>\n",
|
||||
" <td>530</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1807</th>\n",
|
||||
" <td>536</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1808</th>\n",
|
||||
" <td>4617</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"<p>1809 rows × 1 columns</p>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" person_id\n",
|
||||
"0 2520\n",
|
||||
"1 4993\n",
|
||||
"2 3202\n",
|
||||
"3 4611\n",
|
||||
"4 4095\n",
|
||||
"... ...\n",
|
||||
"1804 3565\n",
|
||||
"1805 3510\n",
|
||||
"1806 530\n",
|
||||
"1807 536\n",
|
||||
"1808 4617\n",
|
||||
"\n",
|
||||
"[1809 rows x 1 columns]"
|
||||
]
|
||||
},
|
||||
"execution_count": 27,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from sqlalchemy import func, text\n",
|
||||
"\n",
|
||||
"# Subquery to group and count the relations without joins\n",
|
||||
"grouped_relations_subquery = (\n",
|
||||
" session.query(\n",
|
||||
" entities.PersonRelation.person_id,\n",
|
||||
" )\n",
|
||||
" .group_by(entities.PersonRelation.person_id)\n",
|
||||
" .having(func.count() > 1)\n",
|
||||
")\n",
|
||||
"pd.DataFrame(grouped_relations_subquery.all())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 28,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"relations_query = (\n",
|
||||
" session.query(\n",
|
||||
" entities.Company.name.label(\"name_company\"),\n",
|
||||
" entities.PersonRelation.relation.label(\"relation_type\"),\n",
|
||||
" entities.Person.lastname.label(\"lastname\"),\n",
|
||||
" entities.Person.firstname.label(\"firstname\"),\n",
|
||||
" entities.Person.date_of_birth.label(\"date_of_birth\"),\n",
|
||||
" )\n",
|
||||
" .join(\n",
|
||||
" entities.PersonRelation,\n",
|
||||
" entities.PersonRelation.company_id == entities.Company.id,\n",
|
||||
" )\n",
|
||||
" .join(\n",
|
||||
" entities.Person,\n",
|
||||
" entities.PersonRelation.person_id == entities.Person.id,\n",
|
||||
" )\n",
|
||||
" .filter(entities.PersonRelation.person_id.in_(grouped_relations_subquery))\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 30,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<div>\n",
|
||||
"<style scoped>\n",
|
||||
" .dataframe tbody tr th:only-of-type {\n",
|
||||
" vertical-align: middle;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe tbody tr th {\n",
|
||||
" vertical-align: top;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe thead th {\n",
|
||||
" text-align: right;\n",
|
||||
" }\n",
|
||||
"</style>\n",
|
||||
"<table border=\"1\" class=\"dataframe\">\n",
|
||||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>name_company</th>\n",
|
||||
" <th>relation_type</th>\n",
|
||||
" <th>lastname</th>\n",
|
||||
" <th>firstname</th>\n",
|
||||
" <th>date_of_birth</th>\n",
|
||||
" <th>person_name</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0 10 24 Telefondienste GmbH</td>\n",
|
||||
" <td>RelationshipRoleEnum.GESCHAEFTSFUEHRER</td>\n",
|
||||
" <td>Tetau</td>\n",
|
||||
" <td>Nicolas</td>\n",
|
||||
" <td>1971-01-02</td>\n",
|
||||
" <td>TetauNicolas</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0 10 24 Telefondienste GmbH</td>\n",
|
||||
" <td>RelationshipRoleEnum.PROKURIST</td>\n",
|
||||
" <td>Dammast</td>\n",
|
||||
" <td>Lutz</td>\n",
|
||||
" <td>1966-12-06</td>\n",
|
||||
" <td>DammastLutz</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>01050.com GmbH</td>\n",
|
||||
" <td>RelationshipRoleEnum.GESCHAEFTSFUEHRER</td>\n",
|
||||
" <td>Tetau</td>\n",
|
||||
" <td>Nicolas</td>\n",
|
||||
" <td>1971-01-02</td>\n",
|
||||
" <td>TetauNicolas</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>01050.com GmbH</td>\n",
|
||||
" <td>RelationshipRoleEnum.PROKURIST</td>\n",
|
||||
" <td>Dammast</td>\n",
|
||||
" <td>Lutz</td>\n",
|
||||
" <td>1966-12-06</td>\n",
|
||||
" <td>DammastLutz</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>AASP Filmproduktionsgesellschaft mbH & Co. Leo...</td>\n",
|
||||
" <td>RelationshipRoleEnum.KOMMANDITIST</td>\n",
|
||||
" <td>Dellhofen</td>\n",
|
||||
" <td>Jens</td>\n",
|
||||
" <td>1977-04-19</td>\n",
|
||||
" <td>DellhofenJens</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>...</th>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>7071</th>\n",
|
||||
" <td>Wohnungsbaugesellschaft mit beschränkter Haftu...</td>\n",
|
||||
" <td>RelationshipRoleEnum.GESCHAEFTSFUEHRER</td>\n",
|
||||
" <td>Karounos</td>\n",
|
||||
" <td>Marita</td>\n",
|
||||
" <td>1971-03-30</td>\n",
|
||||
" <td>KarounosMarita</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>7072</th>\n",
|
||||
" <td>Wohnungsbaugesellschaft mit beschränkter Haftu...</td>\n",
|
||||
" <td>RelationshipRoleEnum.PROKURIST</td>\n",
|
||||
" <td>Groll</td>\n",
|
||||
" <td>Michael</td>\n",
|
||||
" <td>1967-12-24</td>\n",
|
||||
" <td>GrollMichael</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>7073</th>\n",
|
||||
" <td>Wohnungsbaugesellschaft mit beschränkter Haftu...</td>\n",
|
||||
" <td>RelationshipRoleEnum.GESCHAEFTSFUEHRER</td>\n",
|
||||
" <td>Weirich</td>\n",
|
||||
" <td>Torsten</td>\n",
|
||||
" <td>1975-07-21</td>\n",
|
||||
" <td>WeirichTorsten</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>7074</th>\n",
|
||||
" <td>Wohnungsbaugesellschaft mit beschränkter Haftu...</td>\n",
|
||||
" <td>RelationshipRoleEnum.GESCHAEFTSFUEHRER</td>\n",
|
||||
" <td>Brusinski</td>\n",
|
||||
" <td>Bastian</td>\n",
|
||||
" <td>1980-10-29</td>\n",
|
||||
" <td>BrusinskiBastian</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>7075</th>\n",
|
||||
" <td>Zalando Customer Care International SE & Co. KG</td>\n",
|
||||
" <td>RelationshipRoleEnum.PROKURIST</td>\n",
|
||||
" <td>Pape</td>\n",
|
||||
" <td>Ute</td>\n",
|
||||
" <td>1978-12-13</td>\n",
|
||||
" <td>PapeUte</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"<p>7076 rows × 6 columns</p>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" name_company \\\n",
|
||||
"0 0 10 24 Telefondienste GmbH \n",
|
||||
"1 0 10 24 Telefondienste GmbH \n",
|
||||
"2 01050.com GmbH \n",
|
||||
"3 01050.com GmbH \n",
|
||||
"4 AASP Filmproduktionsgesellschaft mbH & Co. Leo... \n",
|
||||
"... ... \n",
|
||||
"7071 Wohnungsbaugesellschaft mit beschränkter Haftu... \n",
|
||||
"7072 Wohnungsbaugesellschaft mit beschränkter Haftu... \n",
|
||||
"7073 Wohnungsbaugesellschaft mit beschränkter Haftu... \n",
|
||||
"7074 Wohnungsbaugesellschaft mit beschränkter Haftu... \n",
|
||||
"7075 Zalando Customer Care International SE & Co. KG \n",
|
||||
"\n",
|
||||
" relation_type lastname firstname \\\n",
|
||||
"0 RelationshipRoleEnum.GESCHAEFTSFUEHRER Tetau Nicolas \n",
|
||||
"1 RelationshipRoleEnum.PROKURIST Dammast Lutz \n",
|
||||
"2 RelationshipRoleEnum.GESCHAEFTSFUEHRER Tetau Nicolas \n",
|
||||
"3 RelationshipRoleEnum.PROKURIST Dammast Lutz \n",
|
||||
"4 RelationshipRoleEnum.KOMMANDITIST Dellhofen Jens \n",
|
||||
"... ... ... ... \n",
|
||||
"7071 RelationshipRoleEnum.GESCHAEFTSFUEHRER Karounos Marita \n",
|
||||
"7072 RelationshipRoleEnum.PROKURIST Groll Michael \n",
|
||||
"7073 RelationshipRoleEnum.GESCHAEFTSFUEHRER Weirich Torsten \n",
|
||||
"7074 RelationshipRoleEnum.GESCHAEFTSFUEHRER Brusinski Bastian \n",
|
||||
"7075 RelationshipRoleEnum.PROKURIST Pape Ute \n",
|
||||
"\n",
|
||||
" date_of_birth person_name \n",
|
||||
"0 1971-01-02 TetauNicolas \n",
|
||||
"1 1966-12-06 DammastLutz \n",
|
||||
"2 1971-01-02 TetauNicolas \n",
|
||||
"3 1966-12-06 DammastLutz \n",
|
||||
"4 1977-04-19 DellhofenJens \n",
|
||||
"... ... ... \n",
|
||||
"7071 1971-03-30 KarounosMarita \n",
|
||||
"7072 1967-12-24 GrollMichael \n",
|
||||
"7073 1975-07-21 WeirichTorsten \n",
|
||||
"7074 1980-10-29 BrusinskiBastian \n",
|
||||
"7075 1978-12-13 PapeUte \n",
|
||||
"\n",
|
||||
"[7076 rows x 6 columns]"
|
||||
]
|
||||
},
|
||||
"execution_count": 30,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"relations_df = pd.DataFrame(relations_query.all())\n",
|
||||
"relations_df[\"person_name\"] = relations_df[\"lastname\"] + relations_df[\"firstname\"]\n",
|
||||
"relations_df.rename(\n",
|
||||
" columns={\"oldName1\": \"newName1\", \"oldName2\": \"newName2\"}, inplace=True\n",
|
||||
")\n",
|
||||
"relations_df"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"node_template = {\n",
|
||||
" \"id\": \"(company|person)_\\d\",\n",
|
||||
" \"label\": \"Name from entries\",\n",
|
||||
" \"type\": \"Company|Person\",\n",
|
||||
" \"shape\": \"dot\",\n",
|
||||
" \"color\": \"#729b79ff\",\n",
|
||||
" # TODO add title for hover effect in graph \"title\": \"\"\n",
|
||||
"}\n",
|
||||
"nodes = relations_df\n",
|
||||
"for index in relations_df.index:\n",
|
||||
" nodes[\"index\"] = {\n",
|
||||
" \"label\": company_2.name,\n",
|
||||
" \"type\": \"Company\",\n",
|
||||
" \"shape\": \"dot\",\n",
|
||||
" \"color\": \"#729b79ff\",\n",
|
||||
" }"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import networkx as nx\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"# relations_df[\"person_name\"] = relations_df[\"lastname\"] + relations_df[\"firstname\"]\n",
|
||||
"\n",
|
||||
"nodes = \n",
|
||||
"# create edges from dataframe\n",
|
||||
"graph = nx.from_pandas_edgelist(relations_df, source=\"name_company\", target=\"person_name\", edge_attr=\"relation_type\")\n",
|
||||
"\n",
|
||||
"# update node attributes from dataframe\n",
|
||||
"nodes_attr = nodes.set_index(\"index\").to_dict(orient=\"index\")\n",
|
||||
"nx.set_node_attributes(graph, nodes_attr)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from pyvis.network import Network\n",
|
||||
"\n",
|
||||
"net = Network(\n",
|
||||
" directed=False, neighborhood_highlight=True, bgcolor=\"white\", font_color=\"black\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# pass networkx graph to pyvis\n",
|
||||
"net.from_nx(graph)\n",
|
||||
"\n",
|
||||
"net.inherit_edge_colors(False)\n",
|
||||
"net.set_edge_smooth(\"dynamic\")\n",
|
||||
"adj_list = net.get_adj_list()\n",
|
||||
"\n",
|
||||
"measure_type = \"degree\"\n",
|
||||
"measure_vector = {}\n",
|
||||
"\n",
|
||||
"if measure_type == \"eigenvector\":\n",
|
||||
" measure_vector = nx.eigenvector_centrality(graph)\n",
|
||||
" df[\"eigenvector\"] = measure_vector.values()\n",
|
||||
"if measure_type == \"degree\":\n",
|
||||
" measure_vector = nx.degree_centrality(graph)\n",
|
||||
" df[\"degree\"] = measure_vector.values()\n",
|
||||
"if measure_type == \"betweeness\":\n",
|
||||
" measure_vector = nx.betweenness_centrality(graph)\n",
|
||||
" df[\"betweeness\"] = measure_vector.values()\n",
|
||||
"if measure_type == \"closeness\":\n",
|
||||
" measure_vector = nx.closeness_centrality(graph)\n",
|
||||
" df[\"closeness\"] = measure_vector.values()\n",
|
||||
"if measure_type == \"pagerank\":\n",
|
||||
" measure_vector = nx.pagerank(graph)\n",
|
||||
" df[\"pagerank\"] = measure_vector.values()\n",
|
||||
"if measure_type == \"average_degree\":\n",
|
||||
" measure_vector = nx.average_degree_connectivity(graph)\n",
|
||||
" # df[\"average_degree\"] = measure_vector.values()\n",
|
||||
" print(measure_vector.values())\n",
|
||||
"\n",
|
||||
"# calculate and update size of the nodes depending on their number of edges\n",
|
||||
"for node_id, neighbors in adj_list.items():\n",
|
||||
" # df[\"edges\"] = measure_vector.values()\n",
|
||||
"\n",
|
||||
" if measure_type == \"edges\":\n",
|
||||
" size = 10 # len(neighbors)*5\n",
|
||||
" else:\n",
|
||||
" size = measure_vector[node_id] * 50\n",
|
||||
" next(\n",
|
||||
" (node.update({\"size\": size}) for node in net.nodes if node[\"id\"] == node_id),\n",
|
||||
" None,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"net.repulsion()\n",
|
||||
"net.show_buttons(filter_=[\"physics\"])\n",
|
||||
"\n",
|
||||
"# net.show_buttons()\n",
|
||||
"\n",
|
||||
"# save graph as HTML\n",
|
||||
"net.save_graph(\"./tmp.html\")"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "aki-prj23-transparenzregister-IY2hcXvW-py3.11",
|
||||
"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.11.4"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
272
Jupyter/NetworkX/archive/network_graph.html
Normal file
272
Jupyter/NetworkX/archive/network_graph.html
Normal file
File diff suppressed because one or more lines are too long
129790
Jupyter/NetworkX/archive/sql_alchemy_to_networkx.ipynb
Normal file
129790
Jupyter/NetworkX/archive/sql_alchemy_to_networkx.ipynb
Normal file
File diff suppressed because it is too large
Load Diff
22114
Jupyter/NetworkX/archive/sql_alchemy_to_networkx_v2.ipynb
Normal file
22114
Jupyter/NetworkX/archive/sql_alchemy_to_networkx_v2.ipynb
Normal file
File diff suppressed because one or more lines are too long
275
Jupyter/NetworkX/archive/tmp.html
Normal file
275
Jupyter/NetworkX/archive/tmp.html
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user