From d13751d37e4fcd13bff3e69e490475d7a02c32e8 Mon Sep 17 00:00:00 2001 From: David Neuhauss Date: Wed, 22 Oct 2025 19:46:35 +0200 Subject: [PATCH] add feature generation code --- .idea/copilotDiffState.xml | 18 -- .vscode/extensions.json | 3 +- .../com/example/sumoc_demo/LocationWorker.kt | 75 ------ ml_stuff/data/location_history_1.csv | 151 ++++++++++++ ml_stuff/environment.yml | 29 +++ ml_stuff/feature_vectors.ipynb | 226 ++++++++++++++++++ 6 files changed, 408 insertions(+), 94 deletions(-) delete mode 100644 .idea/copilotDiffState.xml delete mode 100644 app/src/main/java/com/example/sumoc_demo/LocationWorker.kt create mode 100644 ml_stuff/data/location_history_1.csv create mode 100644 ml_stuff/feature_vectors.ipynb diff --git a/.idea/copilotDiffState.xml b/.idea/copilotDiffState.xml deleted file mode 100644 index 61b67bd..0000000 --- a/.idea/copilotDiffState.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 3885144..4c45dd7 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -1,5 +1,6 @@ { "recommendations": [ - "spencerwmiles.vscode-task-buttons" + "spencerwmiles.vscode-task-buttons", + "ms-toolsai.jupyter" ] } \ No newline at end of file diff --git a/app/src/main/java/com/example/sumoc_demo/LocationWorker.kt b/app/src/main/java/com/example/sumoc_demo/LocationWorker.kt deleted file mode 100644 index 664a5a6..0000000 --- a/app/src/main/java/com/example/sumoc_demo/LocationWorker.kt +++ /dev/null @@ -1,75 +0,0 @@ -package com.example.sumoc_demo - - -import android.Manifest -import android.content.Context -import android.content.pm.PackageManager -import android.util.Log -import androidx.core.content.ContextCompat -import androidx.work.CoroutineWorker -import androidx.work.WorkerParameters -import com.google.android.gms.location.LocationServices -import com.google.android.gms.location.Priority -import kotlinx.coroutines.tasks.await -import java.text.SimpleDateFormat -import java.util.Date -import java.util.Locale - -class LocationWorker( - private val context: Context, - workerParameters: WorkerParameters -) : CoroutineWorker(context, workerParameters) { - // Get an instance of the Room database - private val locationDao = LocationDatabase.getDatabase(context).locationDao() - - companion object { - const val TAG = "LocationWorker" - } - - override suspend fun doWork(): Result { - Log.d(TAG, "Work starting...") - - // Check for permissions before proceeding - if (ContextCompat.checkSelfPermission( - context, - Manifest.permission.ACCESS_FINE_LOCATION - ) != PackageManager.PERMISSION_GRANTED - ) { - Log.e(TAG, "Location permission not granted. Cannot perform work.") - return Result.failure() - } - - val fusedLocationClient = LocationServices.getFusedLocationProviderClient(context) - - return try { - // Use the modern getCurrentLocation() for a single, fresh location update - val location = fusedLocationClient.getCurrentLocation( - Priority.PRIORITY_BALANCED_POWER_ACCURACY, - null - ).await() // This requires the kotlinx-coroutines-play-services library - - location?.let { - val simpleDate = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", - Locale.getDefault()) - val formattedDate = simpleDate.format(Date(it.time)) - Log.d(TAG, "Location found: Lat ${it.latitude}, Lon ${it.longitude} at $formattedDate") - - // Save the location to the Room database - val locationEntity = LocationEntity( - latitude = it.latitude, - longitude = it.longitude, - timestamp = it.time - ) - locationDao.insert(locationEntity) - Log.d(TAG, "Location saved to database.") - - } ?: Log.d(TAG, "Location was null.") - - Log.d(TAG, "Work finished successfully.") - Result.success() - } catch (e: Exception) { - Log.e(TAG, "Error getting location", e) - Result.failure() - } - } -} \ No newline at end of file diff --git a/ml_stuff/data/location_history_1.csv b/ml_stuff/data/location_history_1.csv new file mode 100644 index 0000000..e6b1eb7 --- /dev/null +++ b/ml_stuff/data/location_history_1.csv @@ -0,0 +1,151 @@ +id;latitude;longitude;timestamp +1;37.4219983;-122.084;1760718082155 +2;37.4219983;-122.084;1760718097158 +3;37.4219983;-122.084;1760718112158 +4;37.4219983;-122.084;1760718172171 +5;37.4219983;-122.084;1760718187169 +6;37.4219983;-122.084;1760718266185 +7;51.4406667;6.9842633;1760718281199 +8;51.441455;6.984175;1760718297096 +9;51.442145;6.9840933;1760718312194 +10;51.4420033;6.9851983;1760718327196 +11;51.4417933;6.9863517;1760718342199 +12;51.4415617;6.9875667;1760718357200 +13;51.442135;6.9875883;1760718372198 +14;51.4429783;6.9872;1760718387205 +15;51.4438683;6.9869733;1760718402204 +16;51.44474;6.98663;1760718417208 +17;51.4455883;6.9862433;1760718432209 +18;51.44646;6.98578;1760718447213 +19;51.4473683;6.985425;1760718462215 +20;51.4482483;6.9850267;1760718477219 +21;51.4491883;6.9846567;1760718492221 +22;51.4498633;6.9852167;1760718507224 +23;51.4505667;6.9860917;1760718522226 +24;51.45122;6.9869;1760718537230 +25;51.45193;6.98767;1760718552226 +26;51.4526617;6.9882717;1760718567227 +27;51.45341;6.98886;1760718582231 +28;51.453975;6.989805;1760718597236 +29;51.45415;6.99112;1760718612231 +30;51.4544633;6.9923817;1760718627240 +31;51.454645;6.993645;1760718642237 +32;51.45478;6.9950267;1760718657243 +33;51.45487;6.99637;1760718672243 +34;51.4549567;6.9977183;1760718687240 +35;51.45579;6.99793;1760718702244 +36;51.456915;6.99767;1760718717249 +37;51.458115;6.99743;1760718732245 +38;51.45932;6.9972;1760718747247 +39;51.46045;6.997;1760718762249 +40;51.4616783;6.9968933;1760718777255 +41;51.4628;6.9972;1760718792255 +42;51.46379;6.99813;1760718807256 +43;51.46445;6.9996;1760718822261 +44;51.46501;7.00136;1760718837260 +45;51.46583;7.00258;1760718852264 +46;51.4667117;7.003845;1760718867267 +47;51.46733;7.0054783;1760718882270 +48;51.4679033;7.0070717;1760718897265 +49;51.46879;7.00754;1760718912274 +50;51.4699517;7.007045;1760718927273 +51;51.47114;7.00678;1760718942276 +52;51.4723;7.00697;1760718957278 +53;51.47285;7.0071;1760718972283 +54;51.47285;7.0071;1760718987278 +55;51.47285;7.0071;1760719002291 +56;51.47285;7.0071;1760719017286 +57;51.47285;7.0071;1760719032291 +58;51.47285;7.0071;1760719047292 +59;51.47285;7.0071;1760719062298 +60;51.47285;7.0071;1760719077294 +61;51.47285;7.0071;1760719092303 +62;51.47285;7.0071;1760719107297 +63;51.47285;7.0071;1760719122306 +64;51.47285;7.0071;1760719137304 +65;51.47285;7.0071;1760719152310 +66;51.47285;7.0071;1760719167311 +67;51.47285;7.0071;1760719182315 +68;51.47285;7.0071;1760719197320 +69;51.47285;7.0071;1760719213322 +70;51.47285;7.0071;1760719228320 +71;51.47285;7.0071;1760719243326 +72;51.47285;7.0071;1760719258328 +73;51.47285;7.0071;1760719273332 +74;51.47285;7.0071;1760719288342 +75;51.47285;7.0071;1760719303340 +76;51.47285;7.0071;1760719319336 +77;51.47285;7.0071;1760719334341 +78;51.47285;7.0071;1760719349355 +79;51.47285;7.0071;1760719364345 +80;51.47285;7.0071;1761128464211 +81;51.47285;7.0071;1761128479199 +82;51.47285;7.0071;1761128494203 +83;51.47285;7.0071;1761128510205 +84;51.46368;7.00981;1761128525203 +85;51.463165;7.0106217;1761128540213 +86;51.46263;7.0114;1761128555213 +87;51.4621;7.01219;1761128570218 +88;51.46165;7.01225;1761128585222 +89;51.46105;7.0123517;1761128600224 +90;51.4603433;7.0125733;1761128615227 +91;51.4596617;7.012275;1761128630237 +92;51.45893;7.0120883;1761128645236 +93;51.45826;7.01239;1761128660242 +94;51.4576617;7.0127917;1761128675240 +95;51.45693;7.01262;1761128690242 +96;51.4562183;7.0127967;1761128705246 +97;51.455425;7.0128967;1761128720250 +98;51.454675;7.01293;1761128735250 +99;51.45393;7.01287;1761128750250 +100;51.45316;7.01298;1761128765255 +101;51.45238;7.01318;1761128780255 +102;51.4521117;7.012995;1761128795258 +103;51.451845;7.013065;1761128810259 +104;51.4511617;7.0130933;1761128825260 +105;51.45048;7.01338;1761128840264 +106;51.45048;7.0137;1761128855269 +107;51.45048;7.0137;1761128870271 +108;51.45048;7.0137;1761128885275 +109;51.45048;7.0137;1761128900277 +110;51.45048;7.0137;1761128915283 +111;51.45048;7.0137;1761128930285 +112;51.45048;7.0137;1761128945284 +113;51.45048;7.0137;1761128960286 +114;51.45048;7.0137;1761128975290 +115;51.45048;7.0137;1761128990293 +116;51.45048;7.0137;1761129005300 +117;51.45048;7.0137;1761129020298 +118;51.45048;7.0137;1761129035308 +119;51.45048;7.0137;1761129050309 +120;51.45048;7.0137;1761129065313 +121;51.45048;7.0137;1761129080315 +122;51.45048;7.0137;1761129095321 +123;51.45048;7.0137;1761129110319 +124;51.45048;7.0137;1761129125326 +125;51.45048;7.0137;1761129140329 +126;51.45048;7.0137;1761129155336 +127;51.45048;7.0137;1761129170333 +128;51.45048;7.0137;1761129185340 +129;51.45048;7.0137;1761129200340 +130;51.45048;7.0137;1761129215347 +131;51.45048;7.0137;1761129230343 +132;51.45048;7.0137;1761129245345 +133;51.45048;7.0137;1761129260352 +134;51.45048;7.0137;1761129275354 +135;51.45048;7.0137;1761129290355 +136;51.45048;7.0137;1761129305358 +137;51.45048;7.0137;1761129320359 +138;51.45048;7.0137;1761129335364 +139;51.45048;7.0137;1761129350368 +140;51.45048;7.0137;1761129365367 +141;51.45048;7.0137;1761129380372 +142;51.45048;7.0137;1761129395378 +143;51.45048;7.0137;1761129410378 +144;51.45048;7.0137;1761129425378 +145;51.45048;7.0137;1761129440382 +146;51.45048;7.0137;1761129455383 +147;51.45048;7.0137;1761129470385 +148;51.45048;7.0137;1761129485387 +149;51.45048;7.0137;1761129500392 +150;51.45048;7.0137;1761129515393 diff --git a/ml_stuff/environment.yml b/ml_stuff/environment.yml index d5c72db..60b4a73 100644 --- a/ml_stuff/environment.yml +++ b/ml_stuff/environment.yml @@ -33,14 +33,43 @@ dependencies: - xz=5.6.4=h5eee18b_1 - zlib=1.3.1=hb25bd0a_0 - pip: + - asttokens==3.0.0 + - comm==0.2.3 + - debugpy==1.8.17 + - decorator==5.2.1 + - executing==2.2.1 + - haversine==2.9.0 + - ipykernel==7.0.1 + - ipython==9.6.0 + - ipython-pygments-lexers==1.1.1 + - jedi==0.19.2 - joblib==1.5.2 + - jupyter-client==8.6.3 + - jupyter-core==5.9.1 + - matplotlib-inline==0.1.7 + - nest-asyncio==1.6.0 - numpy==2.3.4 + - packaging==25.0 - pandas==2.3.3 + - parso==0.8.5 + - pexpect==4.9.0 + - platformdirs==4.5.0 + - prompt-toolkit==3.0.52 + - psutil==7.1.1 + - ptyprocess==0.7.0 + - pure-eval==0.2.3 + - pygments==2.19.2 - python-dateutil==2.9.0.post0 - pytz==2025.2 + - pyzmq==27.1.0 - scikit-learn==1.7.2 - scipy==1.16.2 - six==1.17.0 + - stack-data==0.6.3 - threadpoolctl==3.6.0 + - tornado==6.5.2 + - traitlets==5.14.3 + - typing-extensions==4.15.0 - tzdata==2025.2 + - wcwidth==0.2.14 prefix: /home/david/AndroidStudioProjects/SuMoC_Demo/ml_stuff/env diff --git a/ml_stuff/feature_vectors.ipynb b/ml_stuff/feature_vectors.ipynb new file mode 100644 index 0000000..ebbb9a8 --- /dev/null +++ b/ml_stuff/feature_vectors.ipynb @@ -0,0 +1,226 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 61, + "id": "c5a83572", + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "from haversine import haversine, Unit" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "66e03da2", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "DATA_PATH = \"data/location_history_1.csv\" # Path format for jupyter notebook\n", + "df_raw = pd.read_csv(DATA_PATH, sep=\";\")" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "029361d3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " id latitude longitude timestamp\n", + "0 1 37.421998 -122.0840 1760718082155\n", + "1 2 37.421998 -122.0840 1760718097158\n", + "2 3 37.421998 -122.0840 1760718112158\n", + "3 4 37.421998 -122.0840 1760718172171\n", + "4 5 37.421998 -122.0840 1760718187169\n", + ".. ... ... ... ...\n", + "145 146 51.450480 7.0137 1761129455383\n", + "146 147 51.450480 7.0137 1761129470385\n", + "147 148 51.450480 7.0137 1761129485387\n", + "148 149 51.450480 7.0137 1761129500392\n", + "149 150 51.450480 7.0137 1761129515393\n", + "\n", + "[150 rows x 4 columns]\n" + ] + } + ], + "source": [ + "print(df_raw)" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "id": "5294e9db", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 NaN\n", + "1 37.421998\n", + "2 37.421998\n", + "3 37.421998\n", + "4 37.421998\n", + " ... \n", + "145 51.450480\n", + "146 51.450480\n", + "147 51.450480\n", + "148 51.450480\n", + "149 51.450480\n", + "Name: latitude, Length: 150, dtype: float64\n" + ] + } + ], + "source": [ + "print(df_raw['latitude'].shift(1))" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "id": "6200d505", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " lat_prev lat_curr lon_prev lon_curr timestamp_prev timestamp\n", + "0 37.421998 37.421998 -122.0840 -122.0840 1.760718e+12 1760718097158\n", + "1 37.421998 37.421998 -122.0840 -122.0840 1.760718e+12 1760718112158\n", + "2 37.421998 37.421998 -122.0840 -122.0840 1.760718e+12 1760718172171\n", + "3 37.421998 37.421998 -122.0840 -122.0840 1.760718e+12 1760718187169\n", + "4 37.421998 37.421998 -122.0840 -122.0840 1.760718e+12 1760718266185\n", + ".. ... ... ... ... ... ...\n", + "144 51.450480 51.450480 7.0137 7.0137 1.761129e+12 1761129455383\n", + "145 51.450480 51.450480 7.0137 7.0137 1.761129e+12 1761129470385\n", + "146 51.450480 51.450480 7.0137 7.0137 1.761129e+12 1761129485387\n", + "147 51.450480 51.450480 7.0137 7.0137 1.761129e+12 1761129500392\n", + "148 51.450480 51.450480 7.0137 7.0137 1.761130e+12 1761129515393\n", + "\n", + "[149 rows x 6 columns]\n" + ] + } + ], + "source": [ + "df_feature = pd.DataFrame({\n", + " 'lat_prev': df_raw['latitude'].shift(1),\n", + " 'lat_curr': df_raw['latitude'],\n", + " 'lon_prev': df_raw['longitude'].shift(1),\n", + " 'lon_curr': df_raw['longitude'],\n", + " 'timestamp_prev': df_raw['timestamp'].shift(1),\n", + " 'timestamp': df_raw['timestamp']\n", + "})\n", + "df_feature = df_feature.dropna().reset_index(drop=True) # Dropping first row that has nan because of the shift\n", + "\n", + "print(df_feature)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "57cd55f2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Index(['lat_prev', 'lat_curr', 'lon_prev', 'lon_curr', 'timestamp_prev',\n", + " 'timestamp', 'distance'],\n", + " dtype='object')\n" + ] + } + ], + "source": [ + "# Calculating distance in meters\n", + "df_feature['distance'] = df_feature.apply(\n", + " lambda r: haversine(\n", + " (r['lat_prev'], r['lon_prev']),\n", + " (r['lat_curr'], r['lon_curr']),\n", + " unit=Unit.METERS\n", + " ),\n", + " axis=1\n", + ")\n", + "\n", + "# Calculating speed in m/s\n", + "df_feature['speed'] = df_feature.apply(\n", + " lambda r: r['distance'] / ((r['timestamp'] - r['timestamp_prev']) / 1000), axis=1\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "id": "c21b02e9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " lat_prev lat_curr lon_prev lon_curr timestamp_prev timestamp \\\n", + "0 37.421998 37.421998 -122.0840 -122.0840 1.760718e+12 1760718097158 \n", + "1 37.421998 37.421998 -122.0840 -122.0840 1.760718e+12 1760718112158 \n", + "2 37.421998 37.421998 -122.0840 -122.0840 1.760718e+12 1760718172171 \n", + "3 37.421998 37.421998 -122.0840 -122.0840 1.760718e+12 1760718187169 \n", + "4 37.421998 37.421998 -122.0840 -122.0840 1.760718e+12 1760718266185 \n", + ".. ... ... ... ... ... ... \n", + "144 51.450480 51.450480 7.0137 7.0137 1.761129e+12 1761129455383 \n", + "145 51.450480 51.450480 7.0137 7.0137 1.761129e+12 1761129470385 \n", + "146 51.450480 51.450480 7.0137 7.0137 1.761129e+12 1761129485387 \n", + "147 51.450480 51.450480 7.0137 7.0137 1.761129e+12 1761129500392 \n", + "148 51.450480 51.450480 7.0137 7.0137 1.761130e+12 1761129515393 \n", + "\n", + " distance speed \n", + "0 0.0 0.0 \n", + "1 0.0 0.0 \n", + "2 0.0 0.0 \n", + "3 0.0 0.0 \n", + "4 0.0 0.0 \n", + ".. ... ... \n", + "144 0.0 0.0 \n", + "145 0.0 0.0 \n", + "146 0.0 0.0 \n", + "147 0.0 0.0 \n", + "148 0.0 0.0 \n", + "\n", + "[149 rows x 8 columns]\n" + ] + } + ], + "source": [ + "print(df_feature)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "env", + "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.14" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}