Skip to content

Commit

Permalink
rotating leadership calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Giulio Massacci committed Jul 12, 2024
1 parent c8cf08e commit 50eeabe
Show file tree
Hide file tree
Showing 2 changed files with 242 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
"# Quarterly data\n",
"# frequency = \"quarter\"\n",
"\n",
"start_date = \"2022-01\"\n",
"end_date = \"2022-12\"\n",
"start_date = \"2019-08\"\n",
"end_date = \"2020-12\"\n",
"\n",
"# Products Graph EU - Extra EU\n",
"# https://api.terra.istat.it/cls/productsExtra?lang=en\n",
Expand All @@ -58,10 +58,10 @@
"\n",
"# Base payload\n",
"base_payload = {\n",
" \"percentage\": \"80\",\n",
" \"percentage\": \"50\",\n",
" \"transport\": [0, 1, 2, 3, 4, 5, 7, 8, 9], # means of transport\n",
" \"product\": \"TOT\",\n",
" \"flow\": 1, #import = 1, #export = 2\n",
" \"flow\": 2, #import = 1, #export = 2\n",
" \"weight\": True,\n",
" \"position\": None,\n",
" \"edges\": None\n",
Expand Down Expand Up @@ -89,7 +89,7 @@
"output_type": "stream",
"text": [
"Retrieving Intra data\n",
"Endpoint: https://api.terra.istat.it/graph/graphIntraMonth\n",
"Endpoint: http://localhost:5500/graphIntraMonth\n",
"Time points: ['201908', '201909', '201910', '201911', '201912', '202001', '202002', '202003', '202004', '202005', '202006', '202007', '202008', '202009', '202010', '202011', '202012']\n",
"Starting data retrieval...\n",
" Retrieving data for month 201908\n",
Expand Down Expand Up @@ -119,11 +119,245 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 6,
"id": "3d620c17",
"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>country</th>\n",
" <th>density</th>\n",
" <th>degree</th>\n",
" <th>vulnerability</th>\n",
" <th>out_degree</th>\n",
" <th>closeness</th>\n",
" <th>betweenness</th>\n",
" <th>distinctiveness</th>\n",
" <th>period</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>FR</td>\n",
" <td>0.075858</td>\n",
" <td>0.285714</td>\n",
" <td>0.880952</td>\n",
" <td>0.171161</td>\n",
" <td>0.432990</td>\n",
" <td>0.191057</td>\n",
" <td>0.007437</td>\n",
" <td>201908</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>DE</td>\n",
" <td>0.075858</td>\n",
" <td>0.404762</td>\n",
" <td>0.809524</td>\n",
" <td>0.170382</td>\n",
" <td>0.512195</td>\n",
" <td>0.355401</td>\n",
" <td>0.021535</td>\n",
" <td>201908</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>PL</td>\n",
" <td>0.075858</td>\n",
" <td>0.238095</td>\n",
" <td>0.880952</td>\n",
" <td>0.019326</td>\n",
" <td>0.424242</td>\n",
" <td>0.108595</td>\n",
" <td>0.003864</td>\n",
" <td>201908</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>SE</td>\n",
" <td>0.075858</td>\n",
" <td>0.238095</td>\n",
" <td>0.904762</td>\n",
" <td>0.084160</td>\n",
" <td>0.400000</td>\n",
" <td>0.066783</td>\n",
" <td>0.019057</td>\n",
" <td>201908</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>FI</td>\n",
" <td>0.075858</td>\n",
" <td>0.095238</td>\n",
" <td>0.952381</td>\n",
" <td>0.011996</td>\n",
" <td>0.304348</td>\n",
" <td>0.027875</td>\n",
" <td>0.012974</td>\n",
" <td>201908</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",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>40</th>\n",
" <td>AD</td>\n",
" <td>0.070707</td>\n",
" <td>0.045455</td>\n",
" <td>0.954545</td>\n",
" <td>0.000000</td>\n",
" <td>0.314286</td>\n",
" <td>0.000000</td>\n",
" <td>0.000312</td>\n",
" <td>202012</td>\n",
" </tr>\n",
" <tr>\n",
" <th>41</th>\n",
" <td>CY</td>\n",
" <td>0.070707</td>\n",
" <td>0.022727</td>\n",
" <td>0.977273</td>\n",
" <td>0.000000</td>\n",
" <td>0.328358</td>\n",
" <td>0.000000</td>\n",
" <td>0.000052</td>\n",
" <td>202012</td>\n",
" </tr>\n",
" <tr>\n",
" <th>42</th>\n",
" <td>RU</td>\n",
" <td>0.070707</td>\n",
" <td>0.045455</td>\n",
" <td>0.954545</td>\n",
" <td>0.000000</td>\n",
" <td>0.247191</td>\n",
" <td>0.000000</td>\n",
" <td>0.000101</td>\n",
" <td>202012</td>\n",
" </tr>\n",
" <tr>\n",
" <th>43</th>\n",
" <td>XK</td>\n",
" <td>0.070707</td>\n",
" <td>0.022727</td>\n",
" <td>0.977273</td>\n",
" <td>0.000000</td>\n",
" <td>0.282051</td>\n",
" <td>0.000000</td>\n",
" <td>0.000024</td>\n",
" <td>202012</td>\n",
" </tr>\n",
" <tr>\n",
" <th>44</th>\n",
" <td>TR</td>\n",
" <td>0.070707</td>\n",
" <td>0.090909</td>\n",
" <td>0.909091</td>\n",
" <td>0.000000</td>\n",
" <td>0.379310</td>\n",
" <td>0.000000</td>\n",
" <td>0.000018</td>\n",
" <td>202012</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>752 rows × 9 columns</p>\n",
"</div>"
],
"text/plain": [
" country density degree vulnerability out_degree closeness \\\n",
"0 FR 0.075858 0.285714 0.880952 0.171161 0.432990 \n",
"1 DE 0.075858 0.404762 0.809524 0.170382 0.512195 \n",
"2 PL 0.075858 0.238095 0.880952 0.019326 0.424242 \n",
"3 SE 0.075858 0.238095 0.904762 0.084160 0.400000 \n",
"4 FI 0.075858 0.095238 0.952381 0.011996 0.304348 \n",
".. ... ... ... ... ... ... \n",
"40 AD 0.070707 0.045455 0.954545 0.000000 0.314286 \n",
"41 CY 0.070707 0.022727 0.977273 0.000000 0.328358 \n",
"42 RU 0.070707 0.045455 0.954545 0.000000 0.247191 \n",
"43 XK 0.070707 0.022727 0.977273 0.000000 0.282051 \n",
"44 TR 0.070707 0.090909 0.909091 0.000000 0.379310 \n",
"\n",
" betweenness distinctiveness period \n",
"0 0.191057 0.007437 201908 \n",
"1 0.355401 0.021535 201908 \n",
"2 0.108595 0.003864 201908 \n",
"3 0.066783 0.019057 201908 \n",
"4 0.027875 0.012974 201908 \n",
".. ... ... ... \n",
"40 0.000000 0.000312 202012 \n",
"41 0.000000 0.000052 202012 \n",
"42 0.000000 0.000101 202012 \n",
"43 0.000000 0.000024 202012 \n",
"44 0.000000 0.000018 202012 \n",
"\n",
"[752 rows x 9 columns]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_metrics"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8d08e6c6",
"metadata": {},
"outputs": [],
"source": [
"df_pivot = df_metrics.pivot(index='period', columns='country', values='degree_centrality')\n",
"\n",
"# Plotting the time series for each group\n",
"df_pivot.plot(figsize=(10, 6))\n",
"plt.title('Time Series Data by Group')\n",
"plt.xlabel('Time')\n",
"plt.ylabel('Value')\n",
"plt.legend(title='Group')\n",
"plt.grid(True)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"df_metrics.to_csv(\"output/metrics_papaer.csv\", index = False, sep = \";\", decimal= \",\")"
"df_metrics.to_csv(\"output/metrics_paper_exp_new.csv\", index = False, sep = \";\", decimal= \",\")"
]
},
{
Expand Down
29 changes: 1 addition & 28 deletions terra-analysis/terra_modules/terra_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,36 +43,10 @@ def get_quarter(month):
print(f"Time points: {date_list}")
return date_list


## Function that converts the metrics JSON in a dataframe
def convert_to_dataframe(json_data, period):

# Convert each key in the JSON data to a DataFrame
df_degree_centrality = pd.DataFrame(list(json_data["degree_centrality"].items()), columns=['country', 'degree_centrality'])
df_vulnerability = pd.DataFrame(list(json_data["vulnerability"].items()), columns=['country', 'vulnerability'])
df_exportation_strength = pd.DataFrame(list(json_data["exportation strenght"].items()), columns=['country', 'exportation_strength'])
df_hubness = pd.DataFrame(list(json_data["hubness"].items()), columns=['country', 'hubness'])

# Display the DataFrames
# print("Degree Centrality:\n", df_degree_centrality.head())
# print("\nVulnerability:\n", df_vulnerability.head())
# print("\nExportation Strength:\n", df_exportation_strength.head())
# print("\nHubness:\n", df_hubness.head())

# Merging the dataframes on 'country'
metrics = pd.merge(df_degree_centrality, df_vulnerability, on='country', how='outer')
metrics = pd.merge(metrics, df_exportation_strength, on='country', how='outer')
metrics = pd.merge(metrics, df_hubness, on='country', how='outer')

# Round decimals
metrics['degree_centrality'] = metrics['degree_centrality'].round(2)
metrics['vulnerability'] = metrics['vulnerability'].round(2)
metrics['exportation_strength'] = metrics['exportation_strength'].round(2)
metrics['hubness'] = metrics['hubness'].round(2)

metrics = pd.DataFrame(json_data).reset_index(names='country')
metrics['period'] = period # Add the period to the DataFrame

# print(metrics)
return metrics

# Get all countries
Expand Down Expand Up @@ -140,7 +114,6 @@ def get_graph_metrics(dataset, base_payload, start_date, end_date, frequency):
if response.status_code == 200:
# Access 'metriche' in the response
data = response.json().get('metriche', {}) # using .get() to avoid KeyError if 'data' does not exist

if len(data) == 0:
print("Empty payload in period: ", period )
else:
Expand Down

0 comments on commit 50eeabe

Please sign in to comment.