{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mixed signals (first 5 rows):\n", " sensor_1 sensor_2 sensor_3 sensor_4\n", "0 0.0586 0.1214 -0.4462 0.3054\n", "1 1.1785 -0.8927 0.3363 0.4952\n", "2 0.7778 -0.1440 -0.1868 -0.2078\n", "3 0.6653 0.0528 -0.3182 -0.3825\n", "4 0.3337 0.6693 -0.7475 -0.9589\n", "\n", "Original mixed values: [-0.6944 2.0534 -1.4544 -1.8907]\n", "Separated ICs: [-1.9177 1.1616 0.4506 -1.0057]\n" ] } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "from sklearn.decomposition import FastICA\n", "from sklearn.preprocessing import StandardScaler\n", "\n", "np.random.seed(42)\n", "n = 1000\n", "t = np.linspace(0, 8 * np.pi, n)\n", "\n", "# 4 independent source signals\n", "s1 = np.sin(t) # sine wave\n", "s2 = np.sign(np.sin(2.3 * t)) # square wave\n", "s3 = (t % (2 * np.pi) / np.pi) - 1 # sawtooth\n", "s4 = np.random.laplace(0, 0.5, n) # sparse noise\n", "S = np.column_stack([s1, s2, s3, s4])\n", "\n", "# Random mixing matrix (simulates sensor cross-talk)\n", "np.random.seed(7)\n", "A = np.random.uniform(-1, 1, (4, 4))\n", "X_mixed = S @ A.T # shape (n, 4): mixed sensor observations\n", "\n", "feature_names = ['sensor_1', 'sensor_2', 'sensor_3', 'sensor_4']\n", "df = pd.DataFrame(X_mixed, columns=feature_names)\n", "\n", "print('Mixed signals (first 5 rows):')\n", "print(df.head().round(4))\n", "\n", "scaler = StandardScaler()\n", "X_scaled = scaler.fit_transform(X_mixed)\n", "\n", "ica = FastICA(n_components=4, random_state=42, max_iter=1000, tol=0.01)\n", "ica.fit(X_scaled)\n", "\n", "# Transform a sample point\n", "sample_raw = X_mixed[42:43] # pick a real observation as example\n", "sample_scaled = scaler.transform(sample_raw)\n", "components = ica.transform(sample_scaled)[0]\n", "print('\\nOriginal mixed values:', sample_raw[0].round(4))\n", "print('Separated ICs:', components.round(4))" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/usr/local/lib/python3.12/dist-packages/huggingface_hub/utils/_auth.py:122: UserWarning: \n", "Error while fetching `HF_TOKEN` secret value from your vault: 'Requesting secret HF_TOKEN timed out. Secrets can only be fetched when running from the Colab UI.'.\n", "You are not authenticated with the Hugging Face Hub in this notebook.\n", "If the error persists, please let us know by opening an issue on GitHub (https://github.com/huggingface/huggingface_hub/issues/new).\n", " warnings.warn(\n" ] } ], "source": [ "from huggingface_hub import notebook_login\n", "notebook_login()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model saved locally: /content/ML_ICA_SensorSignals.joblib\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "1c83515b033c4dd48dd87910cf7d73a6", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Processing Files (0 / 0) : | | 0.00B / 0.00B " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a0f5884226974ceb90987d4298142250", "version_major": 2, "version_minor": 0 }, "text/plain": [ "New Data Upload : | | 0.00B / 0.00B " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "0d1e710c11d54738ad4199d9939d8e63", "version_major": 2, "version_minor": 0 }, "text/plain": [ " ML_ICA_SensorSignals.joblib : 100%|##########| 1.94kB / 1.94kB " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Uploaded to https://huggingface.co/looh2/model\n", "Default sensor values for UI: [-0.694, 2.053, -1.454, -1.891]\n" ] } ], "source": [ "import joblib\n", "from pathlib import Path\n", "from huggingface_hub import HfApi\n", "\n", "repo_id = \"looh2/model\"\n", "model_path = Path(\"ML_ICA_SensorSignals.joblib\")\n", "\n", "bundle = {\n", " 'scaler': scaler,\n", " 'ica': ica,\n", " 'feature_names': feature_names,\n", " # Keep a representative sample so the UI can show realistic default values\n", " 'sample_input': X_mixed[42].tolist(),\n", "}\n", "joblib.dump(bundle, model_path)\n", "print(f\"Model saved locally: {model_path.resolve()}\")\n", "\n", "api = HfApi()\n", "api.create_repo(repo_id=repo_id, repo_type=\"model\", exist_ok=True)\n", "api.upload_file(\n", " path_or_fileobj=str(model_path),\n", " path_in_repo=model_path.name,\n", " repo_id=repo_id,\n", " repo_type=\"model\",\n", ")\n", "print(f\"Uploaded to https://huggingface.co/{repo_id}\")\n", "print(f\"Default sensor values for UI: {X_mixed[42].round(3).tolist()}\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "5d48c04be4a043779d9add83764f51d1", "version_major": 2, "version_minor": 0 }, "text/plain": [ "ML_ICA_SensorSignals.joblib: 0%| | 0.00/1.94k [00:00