Skip to content

Instantly share code, notes, and snippets.

@x1001000
Last active May 4, 2023 05:34
Show Gist options
  • Save x1001000/795c4c1da426c290cbca64367e3065ea to your computer and use it in GitHub Desktop.
Save x1001000/795c4c1da426c290cbca64367e3065ea to your computer and use it in GitHub Desktop.
hack scipy.io.loadmat
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"name": "hack scipy.io.loadmat",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/x1001000/795c4c1da426c290cbca64367e3065ea/hack-scipy-io-loadmat.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"from google.colab import files\n",
"assert files.upload()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 73
},
"id": "8qdNWd9hRxJB",
"outputId": "a57f13e6-d599-4cf7-bcd2-dab96c86e054"
},
"execution_count": 1,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<IPython.core.display.HTML object>"
],
"text/html": [
"\n",
" <input type=\"file\" id=\"files-63324d27-42e7-45ad-9dd8-1ef6ce64df5b\" name=\"files[]\" multiple disabled\n",
" style=\"border:none\" />\n",
" <output id=\"result-63324d27-42e7-45ad-9dd8-1ef6ce64df5b\">\n",
" Upload widget is only available when the cell has been executed in the\n",
" current browser session. Please rerun this cell to enable.\n",
" </output>\n",
" <script>// Copyright 2017 Google LLC\n",
"//\n",
"// Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"// you may not use this file except in compliance with the License.\n",
"// You may obtain a copy of the License at\n",
"//\n",
"// http://www.apache.org/licenses/LICENSE-2.0\n",
"//\n",
"// Unless required by applicable law or agreed to in writing, software\n",
"// distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"// See the License for the specific language governing permissions and\n",
"// limitations under the License.\n",
"\n",
"/**\n",
" * @fileoverview Helpers for google.colab Python module.\n",
" */\n",
"(function(scope) {\n",
"function span(text, styleAttributes = {}) {\n",
" const element = document.createElement('span');\n",
" element.textContent = text;\n",
" for (const key of Object.keys(styleAttributes)) {\n",
" element.style[key] = styleAttributes[key];\n",
" }\n",
" return element;\n",
"}\n",
"\n",
"// Max number of bytes which will be uploaded at a time.\n",
"const MAX_PAYLOAD_SIZE = 100 * 1024;\n",
"\n",
"function _uploadFiles(inputId, outputId) {\n",
" const steps = uploadFilesStep(inputId, outputId);\n",
" const outputElement = document.getElementById(outputId);\n",
" // Cache steps on the outputElement to make it available for the next call\n",
" // to uploadFilesContinue from Python.\n",
" outputElement.steps = steps;\n",
"\n",
" return _uploadFilesContinue(outputId);\n",
"}\n",
"\n",
"// This is roughly an async generator (not supported in the browser yet),\n",
"// where there are multiple asynchronous steps and the Python side is going\n",
"// to poll for completion of each step.\n",
"// This uses a Promise to block the python side on completion of each step,\n",
"// then passes the result of the previous step as the input to the next step.\n",
"function _uploadFilesContinue(outputId) {\n",
" const outputElement = document.getElementById(outputId);\n",
" const steps = outputElement.steps;\n",
"\n",
" const next = steps.next(outputElement.lastPromiseValue);\n",
" return Promise.resolve(next.value.promise).then((value) => {\n",
" // Cache the last promise value to make it available to the next\n",
" // step of the generator.\n",
" outputElement.lastPromiseValue = value;\n",
" return next.value.response;\n",
" });\n",
"}\n",
"\n",
"/**\n",
" * Generator function which is called between each async step of the upload\n",
" * process.\n",
" * @param {string} inputId Element ID of the input file picker element.\n",
" * @param {string} outputId Element ID of the output display.\n",
" * @return {!Iterable<!Object>} Iterable of next steps.\n",
" */\n",
"function* uploadFilesStep(inputId, outputId) {\n",
" const inputElement = document.getElementById(inputId);\n",
" inputElement.disabled = false;\n",
"\n",
" const outputElement = document.getElementById(outputId);\n",
" outputElement.innerHTML = '';\n",
"\n",
" const pickedPromise = new Promise((resolve) => {\n",
" inputElement.addEventListener('change', (e) => {\n",
" resolve(e.target.files);\n",
" });\n",
" });\n",
"\n",
" const cancel = document.createElement('button');\n",
" inputElement.parentElement.appendChild(cancel);\n",
" cancel.textContent = 'Cancel upload';\n",
" const cancelPromise = new Promise((resolve) => {\n",
" cancel.onclick = () => {\n",
" resolve(null);\n",
" };\n",
" });\n",
"\n",
" // Wait for the user to pick the files.\n",
" const files = yield {\n",
" promise: Promise.race([pickedPromise, cancelPromise]),\n",
" response: {\n",
" action: 'starting',\n",
" }\n",
" };\n",
"\n",
" cancel.remove();\n",
"\n",
" // Disable the input element since further picks are not allowed.\n",
" inputElement.disabled = true;\n",
"\n",
" if (!files) {\n",
" return {\n",
" response: {\n",
" action: 'complete',\n",
" }\n",
" };\n",
" }\n",
"\n",
" for (const file of files) {\n",
" const li = document.createElement('li');\n",
" li.append(span(file.name, {fontWeight: 'bold'}));\n",
" li.append(span(\n",
" `(${file.type || 'n/a'}) - ${file.size} bytes, ` +\n",
" `last modified: ${\n",
" file.lastModifiedDate ? file.lastModifiedDate.toLocaleDateString() :\n",
" 'n/a'} - `));\n",
" const percent = span('0% done');\n",
" li.appendChild(percent);\n",
"\n",
" outputElement.appendChild(li);\n",
"\n",
" const fileDataPromise = new Promise((resolve) => {\n",
" const reader = new FileReader();\n",
" reader.onload = (e) => {\n",
" resolve(e.target.result);\n",
" };\n",
" reader.readAsArrayBuffer(file);\n",
" });\n",
" // Wait for the data to be ready.\n",
" let fileData = yield {\n",
" promise: fileDataPromise,\n",
" response: {\n",
" action: 'continue',\n",
" }\n",
" };\n",
"\n",
" // Use a chunked sending to avoid message size limits. See b/62115660.\n",
" let position = 0;\n",
" do {\n",
" const length = Math.min(fileData.byteLength - position, MAX_PAYLOAD_SIZE);\n",
" const chunk = new Uint8Array(fileData, position, length);\n",
" position += length;\n",
"\n",
" const base64 = btoa(String.fromCharCode.apply(null, chunk));\n",
" yield {\n",
" response: {\n",
" action: 'append',\n",
" file: file.name,\n",
" data: base64,\n",
" },\n",
" };\n",
"\n",
" let percentDone = fileData.byteLength === 0 ?\n",
" 100 :\n",
" Math.round((position / fileData.byteLength) * 100);\n",
" percent.textContent = `${percentDone}% done`;\n",
"\n",
" } while (position < fileData.byteLength);\n",
" }\n",
"\n",
" // All done.\n",
" yield {\n",
" response: {\n",
" action: 'complete',\n",
" }\n",
" };\n",
"}\n",
"\n",
"scope.google = scope.google || {};\n",
"scope.google.colab = scope.google.colab || {};\n",
"scope.google.colab._files = {\n",
" _uploadFiles,\n",
" _uploadFilesContinue,\n",
"};\n",
"})(self);\n",
"</script> "
]
},
"metadata": {}
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"Saving p00001s002e020c001t004.mat to p00001s002e020c001t004.mat\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"%cd /usr/local/lib/python3.10/dist-packages/scipy/io/matlab\n",
"!sed -i \"125a \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ pass\" _mio4.py\n",
"!sed -i \"125d\" _mio4.py"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Lb_YHv5-QTUn",
"outputId": "5b543340-b60f-46c5-9f80-758c61302dc4"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"/usr/local/lib/python3.10/dist-packages/scipy/io/matlab\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"import scipy.io\n",
"scipy.io.loadmat('/content/p00001s002e020c001t004.mat')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "vSVFJfijuYug",
"outputId": "f2c1ee29-97b6-4933-b970-3b844c29b97b"
},
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'Examinator': array([' '], dtype='<U1'),\n",
" 'Subject': array(['B123630179 B123630179'], dtype='<U21'),\n",
" 'RegNr': array(['B123630179'], dtype='<U10'),\n",
" 'Birthday': array(['2020-08-28'], dtype='<U10'),\n",
" 'Diagnosis': array([' '], dtype='<U1'),\n",
" 'Comment': array([' '], dtype='<U1'),\n",
" 'OriginalFile': array(['C:/IABASE2000/EyeSeeCam/p00001s002e020c001t004.mat'], dtype='<U50'),\n",
" 'RecordingDevice': array(['ESCUSBVOG:1.2.0.001'], dtype='<U19'),\n",
" 'AcquisitionProgram': array(['C:\\\\Program Files (x86)\\\\Interacoustics\\\\EyeSeeCam 1.2.0\\\\eyeseecam.exe'],\n",
" dtype='<U67'),\n",
" 'Date': array(['2020-08-28 15:47:50.319108'], dtype='<U26'),\n",
" 'MonitorWidth': array([' '], dtype='<U1'),\n",
" 'MonitorDist': array([' '], dtype='<U1'),\n",
" 'Examination': array(['Nystagmus'], dtype='<U9'),\n",
" 'Session': array(['2'], dtype='<U1'),\n",
" 'Condition': array(['Standard'], dtype='<U8'),\n",
" 'PixelSize': array([[0.024, 0. ],\n",
" [0.024, 0. ],\n",
" [0. , 0. ],\n",
" [0. , 0. ],\n",
" [0. , 0. ],\n",
" [0. , 0. ],\n",
" [0. , 0. ],\n",
" [0. , 0. ]]),\n",
" 'CalHeadIMUGyroOff': array([[2046.111 ],\n",
" [2043.1865],\n",
" [2052.824 ]], dtype=float32),\n",
" 'CalHeadIMUGyroGain': array([[-5.4996889e-03, -1.5389581e-03, -2.4470359e-01],\n",
" [-5.8925076e-04, -2.4438748e-01, -2.0833896e-04],\n",
" [ 2.4691427e-01, -3.9392780e-03, 4.7326861e-03]], dtype=float32),\n",
" 'CalHeadIMUAccelOff': array([[2030.8529],\n",
" [2053.414 ],\n",
" [2048.0632]], dtype=float32),\n",
" 'CalHeadIMUAccelGain': array([[-7.8641024e-06, 1.5780195e-05, 1.9555683e-03],\n",
" [ 3.6712991e-05, 1.9508923e-03, 2.3158684e-06],\n",
" [-1.9331446e-03, 2.0944261e-05, 1.2481664e-06]], dtype=float32),\n",
" 'SamplingRate': array([[220.80318559]]),\n",
" 'StartTime': array([[956.462636]]),\n",
" 'DataNames': array(['T xine a ef vMtlXmpS', 'i aea m Vt YaEL Xah',\n",
" 'm lrd e eEL Yrye gi', 'e TtIH lye kef ef',\n",
" ' L iine Xef eLt Xt', ' e maea Vt reEL X',\n",
" ' f elrd ePL Tfye ', ' t VtIL lue otef ',\n",
" ' SL eine Zpf rMRt ', ' ye laef it saiRL ',\n",
" ' sf Ylrt lPLirgee ', ' tt AtEL Rueokhff ',\n",
" ' eSH ciye opfnetlt ', ' mye caef wit rMeRL ',\n",
" ' Tna elTt lPLRaxee ', ' icd lAoEL CueorCff ',\n",
" ' mFIHXcrye opfwkelt ', ' elne c ef vit eneSL',\n",
" ' aea e Ht XlELrtxle', ' grd l oEL YMyeReRif',\n",
" ' tIHZ rye eefoript', 'L ine ef tLtwYmpS',\n",
" 'e aea Vt heEL Yah', 'f lrd ePL ofye gi',\n",
" 't VtIH lue dtef ef', 'TL eine Ypf MRt Yt',\n",
" 'ie laea it aiRL Y', 'mf Xlrd lPL rgee ',\n",
" 'et VtIL Cue khff ', ' FH eine opf etlt ',\n",
" ' re laef lit rMeRL ', ' aa Zlrt lPLCaxee ',\n",
" ' md AtEL CueorCff ', ' eIH ciye opflkelt ',\n",
" ' Ine caef vit eneSL ', ' nea elVt XlELrtxle ',\n",
" ' drd lFeEL XCyeCeRif ', ' etIHYrrye oefoript '], dtype='<U21'),\n",
" 'DataUnits': array(['ss / u mi', ' d s m d', ' e R t', ' g o p h',\n",
" ' / t i p ', ' s x i i', ' b e x m', ' o g l e a',\n",
" ' o ds l g', ' l e^ds e', ' g2e mh', 's / g me',\n",
" ' d s i', ' e R g', ' g o p h', ' / t i p t',\n",
" ' s x i i', ' s e x m', ' C l e a', ' o ps l g',\n",
" ' u i^ps e', ' n x2i mw', 's t e x mi', ' g l e d',\n",
" ' ds l t', ' e ps h', ' g i m ', ' / x m i',\n",
" ' d s e m', ' e R l a', ' g o ps g', ' / t i^p e',\n",
" ' s x2i ih', 'f e x me', 'r g l e ai', 'a ds l gg',\n",
" 'm e es eh', 'e g n mwt'], dtype='<U11'),\n",
" 'Data': array([[ 9.9999997e-06, -9.9261355e-01, -1.3856964e+01, ...,\n",
" 0.0000000e+00, 4.2894733e-01, nan],\n",
" [ 9.9999997e-06, 4.0670000e+03, 1.0568102e+02, ...,\n",
" -3.0580115e-01, -1.3031392e+01, nan],\n",
" [ 0.0000000e+00, 1.7533397e-19, 5.0000000e+00, ...,\n",
" 5.3182232e-01, -9.2669353e+00, nan],\n",
" ...,\n",
" [-2.8220987e-01, 6.8284111e+00, -3.2726181e-01, ...,\n",
" 5.9289840e-19, 5.0000000e+00, 0.0000000e+00],\n",
" [-1.5122776e-01, 1.6548311e+01, -3.4669340e-01, ...,\n",
" -3.3387076e-02, nan, 0.0000000e+00],\n",
" [-3.0914770e-02, 1.3967102e+02, nan, ...,\n",
" 9.1741113e-03, nan, 0.0000000e+00]], dtype=float32),\n",
" 'EvalDataColumns': array(['col.Time = 1;\\ncol.LeftTime = 2;\\ncol.LeftSystemTime = 3;\\ncol.LeftFrameIndex = 4;\\ncol.LeftSyncFlag = 5;\\ncol.HeadInertialTime = 6;\\ncol.HeadInertialVelX = 7;\\ncol.HeadInertialVelY = 8;\\ncol.HeadInertialVelZ = 9;\\ncol.HeadInertialAccelX = 10;\\ncol.HeadInertialAccelY = 11;\\ncol.HeadInertialAccelZ = 12;\\ncol.HeadInertialFrame = 13;\\ncol.LeftEyeTor = 14;\\ncol.LeftEyeVer = 15;\\ncol.LeftEyeHor = 16;\\ncol.LeftEyeVelX = 17;\\ncol.LeftEyeVelY = 18;\\ncol.LeftEyeVelZ = 19;\\ncol.LeftPupilCol = 20;\\ncol.LeftPupilRow = 21;\\ncol.LeftPupilCovXX = 22;\\ncol.LeftPupilCovXY = 23;\\ncol.LeftPupilCovYY = 24;\\ncol.LeftPupilMethod = 25;\\ncol.LeftEyeMarkerTorsion = 26;\\ncol.LeftEyeLeftMarkerCol = 27;\\ncol.LeftEyeLeftMarkerRow = 28;\\ncol.LeftEyeRightMarkerCol = 29;\\ncol.LeftEyeRightMarkerRow = 30;\\ncol.LeftReflexCenterX = 31;\\ncol.LeftReflexCenterY = 32;\\ncol.LeftReflexRimX = 33;\\ncol.LeftReflexRimY = 34;\\ncol.LeftSlippageX = 35;\\ncol.LeftSlippageY = 36;\\ncol.LeftShiftX = 37;\\ncol.LeftShiftY = 38;\\n'],\n",
" dtype='<U959'),\n",
" 'EvalEZPlot': array([\"if ~exist('f','var') f=1; end;\\neval(EvalDataColumns);\\nfp = [col.Time col.LeftPupilCol col.Time col.LeftPupilRow col.Time col.LeftEyeMarkerTorsion; ...\\ncol.Time col.LeftEyeTor col.Time col.LeftEyeVer col.Time col.LeftEyeHor; ...\\ncol.Time col.LeftEyeVelX col.Time col.LeftEyeVelY col.Time col.LeftEyeVelZ; ...\\ncol.LeftEyeHor col.LeftEyeVer col.LeftEyeTor col.LeftEyeVer col.LeftEyeTor col.LeftEyeHor; ...\\n];\\nif f>size(fp,1) f=f-size(fp,1); end;\\nsp = length(find(fp(f,:)>0))/2; pm=sp; pn=1; ti=1;\\nif mod(f-1,4)>=3 pn=pm; pm=1; ti=2; end;\\nax=[];\\nfor p = 1:sp\\n ix = fp(f,2*(p-1)+1);\\n iy = fp(f,2*p);\\n if ~isnan(iy)\\n ax(p)=subplot(pm,pn,p); cla(ax(p));\\n plot(Data(:,ix),Data(:,iy),'k-');\\n if p==ti title(sprintf('%s %s %s',Subject,Birthday,Date)); end;\\n u = DataUnits(iy,:); n = DataNames(iy,:);\\n ylabel(sprintf('%s [%s]',n(n~=' '),u(u~=' ')));\\n u = DataUnits(ix,:); n = DataNames(ix,:);\\n if p~=sp\\n if pm<pn xlabel(sprintf('%s [%s]',n(n~=' '),u(u~=' ')));\\n else set(gca,'XTickLabel',[]);\\n end;\\n else\\n xlabel(sprintf('%s [%s]',n(n~=' '),u(u~=' ')));\\n end;\\n end;\\nend;\\nif pm<pn linkaxes (ax(1:2),'y');\\nelse linkaxes (ax, 'x');\\nend;\\n\"],\n",
" dtype='<U1248')}"
]
},
"metadata": {},
"execution_count": 3
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment