{ "cells": [ { "cell_type": "code", "id": "initial_id", "metadata": { "collapsed": true, "ExecuteTime": { "end_time": "2024-11-05T16:07:15.513969Z", "start_time": "2024-11-05T16:07:15.067921Z" } }, "source": [ "from datetime import datetime\n", "import sqlite3\n", "\n", "import plotly.graph_objects as go\n", "import pandas as pd" ], "outputs": [], "execution_count": 1 }, { "metadata": { "ExecuteTime": { "end_time": "2024-11-05T16:07:50.716467Z", "start_time": "2024-11-05T16:07:15.529909Z" } }, "cell_type": "code", "source": [ "sqlite_file = input(\"Sqlite file: \")\n", "conn = sqlite3.connect(sqlite_file)" ], "id": "2a888e85f7940c39", "outputs": [], "execution_count": 2 }, { "metadata": {}, "cell_type": "code", "source": [ "query = '''\n", "SELECT timestamp, filament_variant_id, available, region\n", " FROM availability\n", " LEFT JOIN measurements ON availability.measurement_id = measurements.id\n", " LEFT JOIN filament_variants ON availability.filament_variant_id = filament_variants.id\n", " LEFT JOIN filaments ON filament_variants.filament_id = filaments.id\n", "'''\n", "df = pd.read_sql(query, conn)\n", "\n", "all_timestamps = list(df['timestamp'].dropna().unique())\n", "all_regions = list(df['region'].dropna().unique())\n", "\n", "available = {region: [] for region in all_regions}\n", "not_available = {region: [] for region in all_regions}\n", "\n", "for timestamp in all_timestamps:\n", " values = df.query(f'timestamp <= {timestamp}').drop(columns=['timestamp']).drop_duplicates(keep='last', subset=['filament_variant_id', 'region'])\n", " \n", " for region in all_regions:\n", " available[region].append(len(values.query(f'(available == 1) and (region == \"{region}\")')))\n", " not_available[region].append(len(values.query(f'(available == 0) and (region == \"{region}\")')))\n", "\n", "fig = go.Figure()\n", "for region in all_regions:\n", " timestamp_datetime = [datetime.fromtimestamp(timestamp) for timestamp in all_timestamps]\n", " ratios = []\n", " ratio_texts = []\n", " for i in range(len(available[region])):\n", " ratios.append(round((available[region][i] / (available[region][i] + not_available[region][i])) * 100, 2))\n", " ratio_texts.append(f'({available[region][i]} / {available[region][i] + not_available[region][i]})')\n", " \n", " fig.add_trace(go.Scatter(x=timestamp_datetime, y=ratios, text=ratio_texts, mode='lines', name=region.upper()))\n", "fig.update_layout(title='Availability', xaxis_title='Time', yaxis_title='Availability in %', yaxis_range=[0, 100], yaxis_ticksuffix = '%', hovermode='x unified')\n", "fig.show()" ], "id": "28dd4fe644bc6a1d", "outputs": [], "execution_count": null }, { "metadata": { "ExecuteTime": { "end_time": "2024-11-05T16:15:04.396443Z", "start_time": "2024-11-05T16:15:04.040184Z" } }, "cell_type": "code", "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "data": [ { "name": "global", "text": "22.88%", "x": [ "global" ], "y": [ 22.883728044845427 ], "type": "bar" }, { "name": "us", "text": "11.00%", "x": [ "us" ], "y": [ 10.997112892532591 ], "type": "bar" }, { "name": "eu", "text": "18.65%", "x": [ "eu" ], "y": [ 18.65437220343727 ], "type": "bar" }, { "name": "uk", "text": "19.59%", "x": [ "uk" ], "y": [ 19.591141813187818 ], "type": "bar" }, { "name": "au", "text": "19.66%", "x": [ "au" ], "y": [ 19.65659002293262 ], "type": "bar" }, { "name": "ca", "text": "17.50%", "x": [ "ca" ], "y": [ 17.503974848392822 ], "type": "bar" }, { "name": "jp", "text": "24.41%", "x": [ "jp" ], "y": [ 24.405606489166075 ], "type": "bar" } ], "layout": { "template": { "data": { "histogram2dcontour": [ { "type": "histogram2dcontour", "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0.0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1.0, "#f0f921" ] ] } ], "choropleth": [ { "type": "choropleth", "colorbar": { "outlinewidth": 0, "ticks": "" } } ], "histogram2d": [ { "type": "histogram2d", "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0.0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1.0, "#f0f921" ] ] } ], "heatmap": [ { "type": "heatmap", "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0.0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1.0, "#f0f921" ] ] } ], "heatmapgl": [ { "type": "heatmapgl", "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0.0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1.0, "#f0f921" ] ] } ], "contourcarpet": [ { "type": "contourcarpet", "colorbar": { "outlinewidth": 0, "ticks": "" } } ], "contour": [ { "type": "contour", "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0.0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1.0, "#f0f921" ] ] } ], "surface": [ { "type": "surface", "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0.0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1.0, "#f0f921" ] ] } ], "mesh3d": [ { "type": "mesh3d", "colorbar": { "outlinewidth": 0, "ticks": "" } } ], "scatter": [ { "marker": { "line": { "color": "#283442" } }, "type": "scatter" } ], "parcoords": [ { "type": "parcoords", "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } } } ], "scatterpolargl": [ { "type": "scatterpolargl", "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } } } ], "bar": [ { "error_x": { "color": "#f2f5fa" }, "error_y": { "color": "#f2f5fa" }, "marker": { "line": { "color": "rgb(17,17,17)", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "bar" } ], "scattergeo": [ { "type": "scattergeo", "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } } } ], "scatterpolar": [ { "type": "scatterpolar", "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } } } ], "histogram": [ { "marker": { "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "histogram" } ], "scattergl": [ { "marker": { "line": { "color": "#283442" } }, "type": "scattergl" } ], "scatter3d": [ { "type": "scatter3d", "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } } } ], "scattermapbox": [ { "type": "scattermapbox", "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } } } ], "scatterternary": [ { "type": "scatterternary", "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } } } ], "scattercarpet": [ { "type": "scattercarpet", "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } } } ], "carpet": [ { "aaxis": { "endlinecolor": "#A2B1C6", "gridcolor": "#506784", "linecolor": "#506784", "minorgridcolor": "#506784", "startlinecolor": "#A2B1C6" }, "baxis": { "endlinecolor": "#A2B1C6", "gridcolor": "#506784", "linecolor": "#506784", "minorgridcolor": "#506784", "startlinecolor": "#A2B1C6" }, "type": "carpet" } ], "table": [ { "cells": { "fill": { "color": "#506784" }, "line": { "color": "rgb(17,17,17)" } }, "header": { "fill": { "color": "#2a3f5f" }, "line": { "color": "rgb(17,17,17)" } }, "type": "table" } ], "barpolar": [ { "marker": { "line": { "color": "rgb(17,17,17)", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "barpolar" } ], "pie": [ { "automargin": true, "type": "pie" } ] }, "layout": { "autotypenumbers": "strict", "colorway": [ "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52" ], "font": { "color": "#f2f5fa" }, "hovermode": "closest", "hoverlabel": { "align": "left" }, "paper_bgcolor": "rgb(17,17,17)", "plot_bgcolor": "rgb(17,17,17)", "polar": { "bgcolor": "rgb(17,17,17)", "angularaxis": { "gridcolor": "#506784", "linecolor": "#506784", "ticks": "" }, "radialaxis": { "gridcolor": "#506784", "linecolor": "#506784", "ticks": "" } }, "ternary": { "bgcolor": "rgb(17,17,17)", "aaxis": { "gridcolor": "#506784", "linecolor": "#506784", "ticks": "" }, "baxis": { "gridcolor": "#506784", "linecolor": "#506784", "ticks": "" }, "caxis": { "gridcolor": "#506784", "linecolor": "#506784", "ticks": "" } }, "coloraxis": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "colorscale": { "sequential": [ [ 0.0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1.0, "#f0f921" ] ], "sequentialminus": [ [ 0.0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1.0, "#f0f921" ] ], "diverging": [ [ 0, "#8e0152" ], [ 0.1, "#c51b7d" ], [ 0.2, "#de77ae" ], [ 0.3, "#f1b6da" ], [ 0.4, "#fde0ef" ], [ 0.5, "#f7f7f7" ], [ 0.6, "#e6f5d0" ], [ 0.7, "#b8e186" ], [ 0.8, "#7fbc41" ], [ 0.9, "#4d9221" ], [ 1, "#276419" ] ] }, "xaxis": { "gridcolor": "#283442", "linecolor": "#506784", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "#283442", "automargin": true, "zerolinewidth": 2 }, "yaxis": { "gridcolor": "#283442", "linecolor": "#506784", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "#283442", "automargin": true, "zerolinewidth": 2 }, "scene": { "xaxis": { "backgroundcolor": "rgb(17,17,17)", "gridcolor": "#506784", "linecolor": "#506784", "showbackground": true, "ticks": "", "zerolinecolor": "#C8D4E3", "gridwidth": 2 }, "yaxis": { "backgroundcolor": "rgb(17,17,17)", "gridcolor": "#506784", "linecolor": "#506784", "showbackground": true, "ticks": "", "zerolinecolor": "#C8D4E3", "gridwidth": 2 }, "zaxis": { "backgroundcolor": "rgb(17,17,17)", "gridcolor": "#506784", "linecolor": "#506784", "showbackground": true, "ticks": "", "zerolinecolor": "#C8D4E3", "gridwidth": 2 } }, "shapedefaults": { "line": { "color": "#f2f5fa" } }, "annotationdefaults": { "arrowcolor": "#f2f5fa", "arrowhead": 0, "arrowwidth": 1 }, "geo": { "bgcolor": "rgb(17,17,17)", "landcolor": "rgb(17,17,17)", "subunitcolor": "#506784", "showland": true, "showlakes": true, "lakecolor": "rgb(17,17,17)" }, "title": { "x": 0.05 }, "updatemenudefaults": { "bgcolor": "#506784", "borderwidth": 0 }, "sliderdefaults": { "bgcolor": "#C8D4E3", "borderwidth": 1, "bordercolor": "rgb(17,17,17)", "tickwidth": 0 }, "mapbox": { "style": "dark" } } }, "yaxis": { "title": { "text": "Average price change in %" }, "range": [ 0, 100 ] }, "title": { "text": "Average price changes" }, "xaxis": { "title": { "text": "Region" } } }, "config": { "plotlyServerURL": "https://plot.ly" } }, "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "execution_count": 6, "source": [ "query = '''\n", "SELECT timestamp, price, filament_variant_id, region\n", " FROM prices\n", " LEFT JOIN measurements ON prices.measurement_id = measurements.id\n", " LEFT JOIN filament_variants ON prices.filament_variant_id = filament_variants.id\n", " LEFT JOIN filaments ON filament_variants.filament_id = filaments.id\n", "'''\n", "df = pd.read_sql(query, conn)\n", "\n", "# average price changes\n", "fig = go.Figure()\n", "for region in list(df['region'].dropna().unique()):\n", " changed = 0\n", " unchanged = 0\n", " price_changes = []\n", " \n", " for _, rows in df.query(f'(price > 0) and (region == \"{region}\")').groupby('filament_variant_id'):\n", " if len(rows) == 1:\n", " unchanged += 1\n", " continue\n", " else:\n", " changed += 1\n", " \n", " last_price = rows.iloc[0].iloc[1]\n", " first_price = rows.iloc[-1].iloc[1]\n", " \n", " price_changes.append((last_price - first_price) / first_price * 100)\n", " \n", " price_changes_percent = sum(price_changes) / len(price_changes)\n", " fig.add_trace(go.Bar(name=region, text=f'{price_changes_percent:.2f}%', x=[region], y=[price_changes_percent]))\n", "fig.update_layout(title='Average price changes', xaxis_title='Region', yaxis_title='Average price changes in %', yaxis_range=[0, 100])\n", "fig.show()" ], "id": "820c18682316ff52" } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }