Skip to content

Instantly share code, notes, and snippets.

@aloncohen1
Last active October 31, 2022 18:02
Show Gist options
  • Save aloncohen1/6cc7165b060eef2ba27a85743db3afc5 to your computer and use it in GitHub Desktop.
Save aloncohen1/6cc7165b060eef2ba27a85743db3afc5 to your computer and use it in GitHub Desktop.
timeline_generator.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"id": "SBiOHJSOrjK3"
},
"outputs": [],
"source": [
"home_info = residence_df.sample(1).to_dict(orient='records')[0] # pick a random home location\n",
"work_info = pois_df.sample(1).to_dict(orient='records')[0] # pick a random work location\n",
"start_date = '2022-10-01' # timeline start time\n",
"end_date = '2022-11-01' # timeline end time\n",
"max_residence = 3 # max residence stay in a day\n",
"max_pois = 3 # max poi stay in a day"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 206
},
"id": "PhhAH-KvqpKc",
"outputId": "2a6c0f89-f495-413c-a461-d1a10f3382a2"
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <div id=\"df-72afc921-01be-489f-bf4c-07067eda149b\">\n",
" <div class=\"colab-df-container\">\n",
" <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>stay_id</th>\n",
" <th>start_time</th>\n",
" <th>end_time</th>\n",
" <th>entity_id</th>\n",
" <th>entity_name</th>\n",
" <th>entity_type</th>\n",
" <th>lat</th>\n",
" <th>lng</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>2022-10-01 00:00:00</td>\n",
" <td>2022-10-01 08:00:00</td>\n",
" <td>home</td>\n",
" <td>home</td>\n",
" <td>home</td>\n",
" <td>45.410639</td>\n",
" <td>-122.552044</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>2022-10-01 08:00:00</td>\n",
" <td>2022-10-01 10:00:00</td>\n",
" <td>1290</td>\n",
" <td>1290</td>\n",
" <td>building</td>\n",
" <td>45.449074</td>\n",
" <td>-122.758561</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2</td>\n",
" <td>2022-10-01 10:00:00</td>\n",
" <td>2022-10-01 12:00:00</td>\n",
" <td>249212</td>\n",
" <td>Al's Garden Center</td>\n",
" <td>poi</td>\n",
" <td>45.458765</td>\n",
" <td>-122.710870</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>3</td>\n",
" <td>2022-10-01 12:00:00</td>\n",
" <td>2022-10-01 14:00:00</td>\n",
" <td>502973</td>\n",
" <td>Lloyd Center</td>\n",
" <td>poi</td>\n",
" <td>45.532621</td>\n",
" <td>-122.653473</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>4</td>\n",
" <td>2022-10-01 14:00:00</td>\n",
" <td>2022-10-01 16:00:00</td>\n",
" <td>225659</td>\n",
" <td>B&amp;B Barbershop</td>\n",
" <td>poi</td>\n",
" <td>45.483497</td>\n",
" <td>-122.581378</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>\n",
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-72afc921-01be-489f-bf4c-07067eda149b')\"\n",
" title=\"Convert this dataframe to an interactive table.\"\n",
" style=\"display:none;\">\n",
" \n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
" width=\"24px\">\n",
" <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
" <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
" </svg>\n",
" </button>\n",
" \n",
" <style>\n",
" .colab-df-container {\n",
" display:flex;\n",
" flex-wrap:wrap;\n",
" gap: 12px;\n",
" }\n",
"\n",
" .colab-df-convert {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-convert:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
"\n",
" <script>\n",
" const buttonEl =\n",
" document.querySelector('#df-72afc921-01be-489f-bf4c-07067eda149b button.colab-df-convert');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" async function convertToInteractive(key) {\n",
" const element = document.querySelector('#df-72afc921-01be-489f-bf4c-07067eda149b');\n",
" const dataTable =\n",
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
" [key], {});\n",
" if (!dataTable) return;\n",
"\n",
" const docLinkHtml = 'Like what you see? stay the ' +\n",
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
" + ' to learn more about interactive tables.';\n",
" element.innerHTML = '';\n",
" dataTable['output_type'] = 'display_data';\n",
" await google.colab.output.renderOutput(dataTable, element);\n",
" const docLink = document.createElement('div');\n",
" docLink.innerHTML = docLinkHtml;\n",
" element.appendChild(docLink);\n",
" }\n",
" </script>\n",
" </div>\n",
" </div>\n",
" "
],
"text/plain": [
" stay_id start_time end_time entity_id \\\n",
"0 0 2022-10-01 00:00:00 2022-10-01 08:00:00 home \n",
"1 1 2022-10-01 08:00:00 2022-10-01 10:00:00 1290 \n",
"2 2 2022-10-01 10:00:00 2022-10-01 12:00:00 249212 \n",
"3 3 2022-10-01 12:00:00 2022-10-01 14:00:00 502973 \n",
"4 4 2022-10-01 14:00:00 2022-10-01 16:00:00 225659 \n",
"\n",
" entity_name entity_type lat lng \n",
"0 home home 45.410639 -122.552044 \n",
"1 1290 building 45.449074 -122.758561 \n",
"2 Al's Garden Center poi 45.458765 -122.710870 \n",
"3 Lloyd Center poi 45.532621 -122.653473 \n",
"4 B&B Barbershop poi 45.483497 -122.581378 "
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from datetime import timedelta\n",
"import numpy as np\n",
"\n",
"phone_timeline = []\n",
"\n",
"stays_counter = 0\n",
"for day in pd.date_range(start_date, end_date):\n",
" \n",
" n_residence = np.random.choice(range(1, max_residence))\n",
" n_pois = np.random.choice(range(1, max_pois))\n",
"\n",
" levaing_hour = np.random.choice([6,7,8])\n",
" start_time = day\n",
" end_time = day + timedelta(hours=int(levaing_hour))\n",
"\n",
" phone_timeline.append({'stay_id': stays_counter,\n",
" \"start_time\": start_time,\n",
" \"end_time\": end_time,\n",
" 'entity_id': 'home',\n",
" 'entity_name': 'home',\n",
" 'entity_type': 'home',\n",
" 'lat': home_info['lat'],\n",
" 'lng': home_info['lng']})\n",
" stays_counter+=1\n",
"\n",
" \n",
" if day.weekday() not in [5,6]: # if working day\n",
" working_hours = np.random.choice([7,8,9])\n",
" start_time = end_time\n",
" end_time += timedelta(hours=int(working_hours))\n",
" \n",
" phone_timeline.append({'stay_id': stays_counter,\n",
" \"start_time\": start_time,\n",
" \"end_time\": end_time,\n",
" \"entity_id\": 'work',\n",
" 'entity_name': 'work',\n",
" 'entity_type': 'work',\n",
" 'lat': work_info['lat'],\n",
" 'lng': work_info['lng']})\n",
" stays_counter+=1\n",
" \n",
" time_left = 24 - end_time.hour\n",
" if n_residence !=0 or n_pois !=0:\n",
" time_left -= 1 # leave 1 hour to stay at home at the end of the timeline\n",
" time_per_stay = np.floor(time_left/(n_residence + n_pois))\n",
"\n",
" if n_residence !=0:\n",
" for row in residence_df.sample(n_residence).itertuples():\n",
" \n",
" start_time = end_time\n",
" end_time += timedelta(hours=int(time_per_stay))\n",
" time_left -= time_per_stay\n",
"\n",
" phone_timeline.append({'stay_id': stays_counter,\n",
" \"start_time\": start_time,\n",
" \"end_time\": end_time,\n",
" 'entity_name': row.Index,\n",
" 'entity_type': 'building',\n",
" 'entity_id': row.Index,\n",
" 'lat': row.lat,\n",
" 'lng': row.lng})\n",
" stays_counter+=1\n",
" \n",
" if n_pois !=0:\n",
" for row in pois_df.sample(n_pois).itertuples():\n",
" \n",
" start_time = end_time\n",
" end_time += timedelta(hours=int(time_per_stay))\n",
" time_left -= time_per_stay\n",
"\n",
" phone_timeline.append({'stay_id': stays_counter,\n",
" \"start_time\": start_time,\n",
" \"end_time\": end_time,\n",
" 'entity_name': row.poi_name,\n",
" 'entity_type': 'venue',\n",
" 'entity_id': row.Index,\n",
" 'lat': row.lat,\n",
" 'lng': row.lng})\n",
" stays_counter+=1\n",
"\n",
" start_time = end_time\n",
" end_time += timedelta(hours=int(time_left))\n",
" phone_timeline.append({'stay_id': stays_counter,\n",
" \"start_time\": start_time,\n",
" \"end_time\": end_time + timedelta(hours=abs(end_time.hour-24)),\n",
" 'entity_id': 'home',\n",
" 'entity_name': 'home',\n",
" 'entity_type': 'home',\n",
" 'lat': home_info['lat'],\n",
" 'lng': home_info['lng']})\n",
" stays_counter+=1\n",
"phone_timeline = pd.DataFrame(phone_timeline)\n",
"phone_timeline.head()"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"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.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment