Skip to content

Commit

Permalink
final ruff format
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianoF committed Jun 20, 2024
1 parent 306cca6 commit b4900b6
Show file tree
Hide file tree
Showing 74 changed files with 2,548 additions and 1,255 deletions.
1 change: 1 addition & 0 deletions docs/examples/0_sample_load_cargo_movements.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"""

from datetime import datetime

from vortexasdk import CargoMovements
Expand Down
1 change: 1 addition & 0 deletions docs/examples/1_china.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"""

from datetime import datetime

from vortexasdk import CargoMovements, Geographies, Vessels
Expand Down
1 change: 1 addition & 0 deletions docs/examples/2_crude_from_saudi_arabia_to_india.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
filtering on `Products: Crude` with `Origin: Saudi Arabia`, `Destination: India` and `Date Range: Departures in the last Month`.
"""

from datetime import datetime

from dateutil.relativedelta import relativedelta
Expand Down
1 change: 1 addition & 0 deletions docs/examples/3_chinese_daily_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
...
"""

from datetime import datetime

from vortexasdk import CargoTimeSeries, Geographies, Products
Expand Down
1 change: 1 addition & 0 deletions docs/examples/4_medium_sour_floating_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
...
"""

from datetime import datetime

from docs.utils import to_markdown
Expand Down
344 changes: 223 additions & 121 deletions docs/examples/China_Flows.ipynb

Large diffs are not rendered by default.

175 changes: 133 additions & 42 deletions docs/examples/Crude_Floating_Storage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"outputs": [],
"source": [
"# Defining date range for historical analysis\n",
"# NB: Vortexa data is currently available from 2016-01-01 with a maximum date range of 4 years per query \n",
"# NB: Vortexa data is currently available from 2016-01-01 with a maximum date range of 4 years per query\n",
"START_DATE = datetime(2016, 1, 31)\n",
"END_DATE = datetime(2020, 1, 31)\n",
"DATE_RANGE = pd.date_range(START_DATE, END_DATE)\n",
Expand All @@ -107,30 +107,45 @@
"metadata": {},
"outputs": [],
"source": [
"def fetch_global_crude_floating_storage_timeseries(start_date, end_date, unit=\"t\"):\n",
" \n",
"def fetch_global_crude_floating_storage_timeseries(\n",
" start_date, end_date, unit=\"t\"\n",
"):\n",
" # Find Crude/Condensates ID, ensuring its uniqueness\n",
" crude_and_condensates = [p.id for p in Products().search(\"crude\").to_list() if p.name==\"Crude/Condensates\"]\n",
" crude_and_condensates = [\n",
" p.id\n",
" for p in Products().search(\"crude\").to_list()\n",
" if p.name == \"Crude/Condensates\"\n",
" ]\n",
" assert len(crude_and_condensates) == 1\n",
" \n",
"\n",
" # make Vortexa API query\n",
" # NB: disable_geographic_exclusion_rules is set to True to include intra-movements\n",
" df_fs = CargoTimeSeries().search(timeseries_frequency=\"day\",\n",
" timeseries_unit=unit,\n",
" disable_geographic_exclusion_rules=True,\n",
" filter_products=crude_and_condensates,\n",
" filter_activity=\"storing_state\",\n",
" filter_time_min=start_date,\n",
" filter_time_max=end_date).to_df()\n",
" df_fs = (\n",
" CargoTimeSeries()\n",
" .search(\n",
" timeseries_frequency=\"day\",\n",
" timeseries_unit=unit,\n",
" disable_geographic_exclusion_rules=True,\n",
" filter_products=crude_and_condensates,\n",
" filter_activity=\"storing_state\",\n",
" filter_time_min=start_date,\n",
" filter_time_max=end_date,\n",
" )\n",
" .to_df()\n",
" )\n",
"\n",
" # rename columns\n",
" df_fs = df_fs.rename(columns={\"key\": \"date\",\n",
" \"value\": unit,\n",
" \"count\": \"number_of_cargo_movements\"})\n",
" \n",
" df_fs = df_fs.rename(\n",
" columns={\n",
" \"key\": \"date\",\n",
" \"value\": unit,\n",
" \"count\": \"number_of_cargo_movements\",\n",
" }\n",
" )\n",
"\n",
" # remove time zone from timestamp\n",
" df_fs[\"date\"] = pd.to_datetime(df_fs[\"date\"]).dt.tz_localize(None)\n",
" \n",
"\n",
" return df_fs"
]
},
Expand Down Expand Up @@ -277,7 +292,9 @@
}
],
"source": [
"df_fs = fetch_global_crude_floating_storage_timeseries(START_DATE, END_DATE, UNIT)\n",
"df_fs = fetch_global_crude_floating_storage_timeseries(\n",
" START_DATE, END_DATE, UNIT\n",
")\n",
"df_fs"
]
},
Expand Down Expand Up @@ -461,8 +478,16 @@
}
],
"source": [
"spot_prices = pd.read_excel(\"https://www.eia.gov/dnav/pet/xls/PET_PRI_SPT_S1_D.xls\", sheet_name=\"Data 1\", skiprows=[0,1])\n",
"spot_prices = spot_prices.set_index(\"Date\").fillna(method=\"ffill\").reindex(DATE_RANGE, method=\"ffill\")\n",
"spot_prices = pd.read_excel(\n",
" \"https://www.eia.gov/dnav/pet/xls/PET_PRI_SPT_S1_D.xls\",\n",
" sheet_name=\"Data 1\",\n",
" skiprows=[0, 1],\n",
")\n",
"spot_prices = (\n",
" spot_prices.set_index(\"Date\")\n",
" .fillna(method=\"ffill\")\n",
" .reindex(DATE_RANGE, method=\"ffill\")\n",
")\n",
"spot_prices"
]
},
Expand Down Expand Up @@ -697,8 +722,16 @@
}
],
"source": [
"future_prices = pd.read_excel(\"https://www.eia.gov/dnav/pet/xls/PET_PRI_FUT_S1_D.xls\", sheet_name=\"Data 1\", skiprows=[0,1])\n",
"future_prices = future_prices.set_index(\"Date\").fillna(method=\"ffill\").reindex(DATE_RANGE, method=\"ffill\")\n",
"future_prices = pd.read_excel(\n",
" \"https://www.eia.gov/dnav/pet/xls/PET_PRI_FUT_S1_D.xls\",\n",
" sheet_name=\"Data 1\",\n",
" skiprows=[0, 1],\n",
")\n",
"future_prices = (\n",
" future_prices.set_index(\"Date\")\n",
" .fillna(method=\"ffill\")\n",
" .reindex(DATE_RANGE, method=\"ffill\")\n",
")\n",
"future_prices"
]
},
Expand Down Expand Up @@ -947,7 +980,9 @@
}
],
"source": [
"calendar_spread.plot(title=\"Spread between Future and Spot crude oil prices\", grid=True)\n",
"calendar_spread.plot(\n",
" title=\"Spread between Future and Spot crude oil prices\", grid=True\n",
")\n",
"plt.xlabel(\"date\")\n",
"plt.ylabel(\"USD\");"
]
Expand Down Expand Up @@ -1000,7 +1035,9 @@
"outputs": [],
"source": [
"def crosscorr(series_x, series_y, lags):\n",
" return pd.Series([series_y.corr(series_x.shift(lag)) for lag in lags], index=lags)"
" return pd.Series(\n",
" [series_y.corr(series_x.shift(lag)) for lag in lags], index=lags\n",
" )"
]
},
{
Expand Down Expand Up @@ -1048,7 +1085,7 @@
"outputs": [],
"source": [
"def plot_crosscorr(series_x, series_y, maxlag, label_x, label_y):\n",
" lags = np.arange(0, maxlag+1)\n",
" lags = np.arange(0, maxlag + 1)\n",
"\n",
" plt.subplot(\"211\")\n",
" xcorr_x_y = crosscorr(series_x, series_y, lags)\n",
Expand Down Expand Up @@ -1088,7 +1125,13 @@
}
],
"source": [
"plot_crosscorr(calendar_spread, floating_storage, maxlag=MAXLAG, label_x=\"calendar spread\", label_y=\"floating storage\")"
"plot_crosscorr(\n",
" calendar_spread,\n",
" floating_storage,\n",
" maxlag=MAXLAG,\n",
" label_x=\"calendar spread\",\n",
" label_y=\"floating storage\",\n",
")"
]
},
{
Expand Down Expand Up @@ -1119,7 +1162,13 @@
}
],
"source": [
"plot_crosscorr(spot_prices, floating_storage, maxlag=MAXLAG, label_x=\"spot prices\", label_y=\"floating storage\")"
"plot_crosscorr(\n",
" spot_prices,\n",
" floating_storage,\n",
" maxlag=MAXLAG,\n",
" label_x=\"spot prices\",\n",
" label_y=\"floating storage\",\n",
")"
]
},
{
Expand Down Expand Up @@ -1152,7 +1201,7 @@
}
],
"source": [
"lags = np.arange(0, MAXLAG+1)\n",
"lags = np.arange(0, MAXLAG + 1)\n",
"\n",
"crosscorr(floating_storage, floating_storage, lags).plot()\n",
"crosscorr(calendar_spread, calendar_spread, lags).plot()\n",
Expand Down Expand Up @@ -1214,7 +1263,11 @@
}
],
"source": [
"gct = grangercausalitytests(pd.concat([floating_storage, calendar_spread], axis=1), maxlag=2, verbose=True)"
"gct = grangercausalitytests(\n",
" pd.concat([floating_storage, calendar_spread], axis=1),\n",
" maxlag=2,\n",
" verbose=True,\n",
")"
]
},
{
Expand All @@ -1233,21 +1286,37 @@
"metadata": {},
"outputs": [],
"source": [
"def plot_granger_pvalues(series_x, series_y, maxlag, label_x, label_y, test=\"ssr_ftest\", confidence_level=0.05):\n",
" lags = np.arange(1, maxlag+1)\n",
"def plot_granger_pvalues(\n",
" series_x,\n",
" series_y,\n",
" maxlag,\n",
" label_x,\n",
" label_y,\n",
" test=\"ssr_ftest\",\n",
" confidence_level=0.05,\n",
"):\n",
" lags = np.arange(1, maxlag + 1)\n",
"\n",
" plt.subplot(\"211\")\n",
" gct_x_y = grangercausalitytests(pd.concat([series_y, series_x], axis=1), maxlag=maxlag, verbose=False)\n",
" pvalue_x_y = pd.Series([gct_x_y[lag][0][test][1] for lag in lags], index=lags)\n",
" gct_x_y = grangercausalitytests(\n",
" pd.concat([series_y, series_x], axis=1), maxlag=maxlag, verbose=False\n",
" )\n",
" pvalue_x_y = pd.Series(\n",
" [gct_x_y[lag][0][test][1] for lag in lags], index=lags\n",
" )\n",
" pvalue_x_y.plot(title=f\"{label_x} -> {label_y}\", grid=True)\n",
" plt.plot((0, maxlag),(confidence_level, confidence_level),\"--r\")\n",
" plt.plot((0, maxlag), (confidence_level, confidence_level), \"--r\")\n",
" plt.ylabel(\"p-value\")\n",
"\n",
" plt.subplot(\"210\")\n",
" gct_y_x = grangercausalitytests(pd.concat([series_x, series_y], axis=1), maxlag=maxlag, verbose=False)\n",
" pvalue_y_x = pd.Series([gct_y_x[lag][0][test][1] for lag in lags], index=lags)\n",
" gct_y_x = grangercausalitytests(\n",
" pd.concat([series_x, series_y], axis=1), maxlag=maxlag, verbose=False\n",
" )\n",
" pvalue_y_x = pd.Series(\n",
" [gct_y_x[lag][0][test][1] for lag in lags], index=lags\n",
" )\n",
" pvalue_y_x.plot(title=f\"{label_y} -> {label_x}\", grid=True)\n",
" plt.plot((0, maxlag),(confidence_level, confidence_level),\"--r\")\n",
" plt.plot((0, maxlag), (confidence_level, confidence_level), \"--r\")\n",
" plt.xlabel(\"lag [days]\")\n",
" plt.ylabel(\"p-value\")"
]
Expand Down Expand Up @@ -1278,7 +1347,13 @@
}
],
"source": [
"plot_granger_pvalues(calendar_spread, floating_storage, maxlag=MAXLAG, label_x=\"calendar spread\", label_y=\"floating storage\")"
"plot_granger_pvalues(\n",
" calendar_spread,\n",
" floating_storage,\n",
" maxlag=MAXLAG,\n",
" label_x=\"calendar spread\",\n",
" label_y=\"floating storage\",\n",
")"
]
},
{
Expand Down Expand Up @@ -1311,7 +1386,13 @@
}
],
"source": [
"plot_granger_pvalues(spot_prices, floating_storage, maxlag=MAXLAG, label_x=\"spot prices\", label_y=\"floating storage\")"
"plot_granger_pvalues(\n",
" spot_prices,\n",
" floating_storage,\n",
" maxlag=MAXLAG,\n",
" label_x=\"spot prices\",\n",
" label_y=\"floating storage\",\n",
")"
]
},
{
Expand Down Expand Up @@ -1342,7 +1423,13 @@
}
],
"source": [
"plot_granger_pvalues(spot_prices, calendar_spread, maxlag=MAXLAG, label_x=\"spot prices\", label_y=\"calendar spread\")"
"plot_granger_pvalues(\n",
" spot_prices,\n",
" calendar_spread,\n",
" maxlag=MAXLAG,\n",
" label_x=\"spot prices\",\n",
" label_y=\"calendar spread\",\n",
")"
]
},
{
Expand Down Expand Up @@ -1399,10 +1486,14 @@
"end_ts = pd.Timestamp(\"now\")\n",
"start_ts = end_ts - pd.Timedelta(\"100 d\")\n",
"\n",
"df_fs_now = fetch_global_crude_floating_storage_timeseries(start_ts.date(), end_ts.date(), UNIT)\n",
"df_fs_now = fetch_global_crude_floating_storage_timeseries(\n",
" start_ts.date(), end_ts.date(), UNIT\n",
")\n",
"\n",
"floating_storage_now = df_fs_now.set_index(\"date\")[UNIT] / 1000\n",
"floating_storage_now.plot(title=\"Global crude oil floating storage - last 100 days\", grid=True)\n",
"floating_storage_now.plot(\n",
" title=\"Global crude oil floating storage - last 100 days\", grid=True\n",
")\n",
"plt.xlabel(\"date\")\n",
"plt.ylabel(\"k\" + UNIT);"
]
Expand Down
Loading

0 comments on commit b4900b6

Please sign in to comment.