{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "ikUJITDDIp19", "pycharm": { "name": "#%% md\n" } }, "source": [ "# Intro to Python Acceleration and CSV Analysis\n", "\n", "## Introduction\n", "This notebook serves as an introduction to Python for a mechanical engineer looking to plot and analyze some acceleration data in a CSV file. Being a Colab, this tool can freely be used without installing anything.\n", "\n", "For more information on making the switch to Python see [enDAQ's blog, Why and How to Get Started in Python for a MATLAB User](https://blog.endaq.com/why-and-how-to-get-started-in-python-for-a-matlab-user).\n", "\n", "This is part of our webinar series on Python for Mechanical Engineers:\n", "\n", "1. **Get Started with Python**\n", " * [Watch Recording of This](https://info.endaq.com/why-mechanical-engineers-should-use-python-webinar)\n", "2. [Introduction to Numpy & Pandas for Data Analysis](https://colab.research.google.com/drive/1O-VwAdRoSlcrineAk0Jkd_fcw7mFGHa4#scrollTo=ce97q1ZcBiwj)\n", "3. [Introduction to Plotly for Plotting Data](https://colab.research.google.com/drive/1pag2pKQQW5amWgRykAH8uMAPqHA2yUfU)\n", "4. [Introduction of the enDAQ Library](https://colab.research.google.com/drive/1WAtQ8JJC_ny0fki7eUABACMA-isZzKB6)" ] }, { "cell_type": "markdown", "metadata": { "id": "ou9NjNsdJ72B", "pycharm": { "name": "#%% md\n" } }, "source": [ "## Import Data File\n", "We will assume that the first column is time in seconds. Some example files are provided or you can load your own." ] }, { "cell_type": "markdown", "metadata": { "id": "vX2hd0kHKPz4", "pycharm": { "name": "#%% md\n" } }, "source": [ "### Example Files\n", "Here are some example datasets you can use to do some initial testing. If you have uploaded your own data, you'll want to comment this out or not run it!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Ut2acKSEKN1C", "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "filenames = ['https://info.endaq.com/hubfs/data/surgical-instrument.csv',\n", " 'https://info.endaq.com/hubfs/data/blushift.csv',\n", " 'https://info.endaq.com/hubfs/Plots/bearing_data.csv', #used in this dataset: https://blog.endaq.com/top-vibration-metrics-to-monitor-how-to-calculate-them\n", " 'https://info.endaq.com/hubfs/data/Motorcycle-Car-Crash.csv', #used in this blog: https://blog.endaq.com/shock-analysis-response-spectrum-srs-pseudo-velocity-severity\n", " 'https://info.endaq.com/hubfs/data/Calibration-Shake.csv',\n", " 'https://info.endaq.com/hubfs/data/Mining-Hammer.csv'] #largest dataset\n", "filename = filenames[4]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "MaNRqOKcbNsl", "outputId": "1b96883c-ba3d-49c3-d3e8-745a1700de73", "pycharm": { "name": "#%%\n" } }, "outputs": [ { "data": { "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" }, "text/plain": [ "'https://info.endaq.com/hubfs/data/Calibration-Shake.csv'" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "filenames[4]" ] }, { "cell_type": "markdown", "metadata": { "id": "yyguF5C1Ju8l", "pycharm": { "name": "#%% md\n" } }, "source": [ "## Install & Import Libraries\n", "\n", "First we'll install all libraries we'll need, then import them. " ] }, { "cell_type": "markdown", "metadata": { "id": "XQIa2uQNOR_K", "pycharm": { "name": "#%% md\n" } }, "source": [ "Note that if running this locally you'll only need to install one time, then subsequent runs can just do the import. But colab and anaconda will contain all the libraries we'll need anyways so the install isn't necessary. Here is how the install would be done though:\n", "\n", "```\n", "!pip install pandas\n", "!pip install numpy\n", "!pip install matplotlib\n", "!pip install plotly\n", "!pip install scipy\n", "```\n", "\n", "You can always check which libraries you have installed by doing:\n", "\n", "```\n", "!pip freeze\n", "```\n", "\n", "We do need to upgrade plotly though to work in Colab" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "rKONh6lJYtF6", "outputId": "e0c78623-92a5-43e8-d0b7-22fddf826f09", "pycharm": { "name": "#%%\n" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: plotly in /usr/local/lib/python3.7/dist-packages (5.3.1)\n", "Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from plotly) (1.15.0)\n", "Requirement already satisfied: tenacity>=6.2.0 in /usr/local/lib/python3.7/dist-packages (from plotly) (8.0.1)\n" ] } ], "source": [ "!pip install --upgrade plotly" ] }, { "cell_type": "markdown", "metadata": { "id": "N9lHwj3pIozY", "pycharm": { "name": "#%% md\n" } }, "source": [ "Now we'll import our libraries we'll use later." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "4s3mCpAZNAZq", "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import plotly.express as xp\n", "import plotly.io as pio; pio.renderers.default = \"iframe\"\n", "from scipy import signal" ] }, { "cell_type": "markdown", "metadata": { "id": "4HshMSZdObSY", "pycharm": { "name": "#%% md\n" } }, "source": [ "## Load the CSV, Analyze & Plot\n", "We'll load the data into pandas, display it, do some very basic analysis, and plot the time history in a few ways." ] }, { "cell_type": "markdown", "metadata": { "id": "AFbCjAMxPx0G", "pycharm": { "name": "#%% md\n" } }, "source": [ "### Load the CSV File and Prepare\n", "Remember we are expecting the first column to be time and will set it as the index. This is loading a CSV file, but Pandas supports a lot of other file formats, see: [Pandas Input/Output](https://pandas.pydata.org/docs/reference/io.html).\n", "\n", "If you must/need to use .MAT files, scipy can read these: [scipy.io.loadmat](https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.loadmat.html)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 447 }, "id": "sXOwidDEOrS0", "outputId": "21e88975-7eac-4088-8c7f-24599ba1012a", "pycharm": { "name": "#%%\n" } }, "outputs": [ { "data": { "text/html": [ "
| \n", " | \"X (2000g)\" | \n", "\"Y (2000g)\" | \n", "\"Z (2000g)\" | \n", "
|---|---|---|---|
| Time | \n", "\n", " | \n", " | \n", " |
| 0.004394 | \n", "-0.122072 | \n", "-0.122072 | \n", "-0.061036 | \n", "
| 0.004594 | \n", "-0.061036 | \n", "0.488289 | \n", "-0.366217 | \n", "
| 0.004794 | \n", "0.183108 | \n", "0.122072 | \n", "-0.061036 | \n", "
| 0.004994 | \n", "0.122072 | \n", "-0.122072 | \n", "-0.122072 | \n", "
| 0.005194 | \n", "0.122072 | \n", "0.122072 | \n", "-0.244144 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "
| 27.691064 | \n", "-0.427253 | \n", "-0.152590 | \n", "-0.671397 | \n", "
| 27.691264 | \n", "-0.122072 | \n", "-0.335698 | \n", "-0.305180 | \n", "
| 27.691464 | \n", "-0.183108 | \n", "-0.152590 | \n", "-0.122072 | \n", "
| 27.691664 | \n", "-0.305180 | \n", "0.030518 | \n", "-0.244144 | \n", "
| 27.691864 | \n", "-0.305180 | \n", "-0.030518 | \n", "-0.366217 | \n", "
138440 rows × 3 columns
\n", "| \n", " | Peak Acceleration (g) | \n", "RMS (g) | \n", "Crest Factor | \n", "
|---|---|---|---|
| \"X (2000g)\" | \n", "1.709010 | \n", "0.249129 | \n", "6.859939 | \n", "
| \"Y (2000g)\" | \n", "1.647974 | \n", "0.279338 | \n", "5.899566 | \n", "
| \"Z (2000g)\" | \n", "8.850233 | \n", "2.687501 | \n", "3.293109 | \n", "
| \n", " | \"X (2000g)\" | \n", "\"Y (2000g)\" | \n", "\"Z (2000g)\" | \n", "
|---|---|---|---|
| Time | \n", "\n", " | \n", " | \n", " |
| 0.004394 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
| 0.281203 | \n", "0.976577 | \n", "1.159686 | \n", "0.976577 | \n", "
| 0.558025 | \n", "1.159686 | \n", "1.403830 | \n", "1.068132 | \n", "
| 0.834870 | \n", "1.403830 | \n", "1.159686 | \n", "1.129168 | \n", "
| 1.111689 | \n", "1.220722 | \n", "1.281758 | \n", "1.220722 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "
| 26.576863 | \n", "1.159686 | \n", "1.037613 | \n", "0.793469 | \n", "
| 26.853655 | \n", "0.976577 | \n", "1.037613 | \n", "0.854505 | \n", "
| 27.130463 | \n", "1.068132 | \n", "1.068132 | \n", "0.946059 | \n", "
| 27.407260 | \n", "0.976577 | \n", "1.281758 | \n", "0.793469 | \n", "
| 27.684064 | \n", "1.098650 | \n", "1.129168 | \n", "0.915541 | \n", "
101 rows × 3 columns
\n", "| \n", " | \"X (2000g)\" | \n", "\"Y (2000g)\" | \n", "\"Z (2000g)\" | \n", "
|---|---|---|---|
| Frequency (Hz) | \n", "\n", " | \n", " | \n", " |
| 0.000000 | \n", "0.000048 | \n", "0.000049 | \n", "0.000072 | \n", "
| 4.000048 | \n", "0.000294 | \n", "0.000276 | \n", "0.000332 | \n", "
| 8.000095 | \n", "0.000256 | \n", "0.000254 | \n", "0.000287 | \n", "
| 12.000143 | \n", "0.000189 | \n", "0.000206 | \n", "0.000230 | \n", "
| 16.000191 | \n", "0.000170 | \n", "0.000156 | \n", "0.000193 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "
| 2484.029606 | \n", "0.000007 | \n", "0.000006 | \n", "0.000007 | \n", "
| 2488.029654 | \n", "0.000007 | \n", "0.000006 | \n", "0.000006 | \n", "
| 2492.029702 | \n", "0.000007 | \n", "0.000007 | \n", "0.000007 | \n", "
| 2496.029749 | \n", "0.000007 | \n", "0.000007 | \n", "0.000007 | \n", "
| 2500.029797 | \n", "0.000003 | \n", "0.000004 | \n", "0.000004 | \n", "
626 rows × 3 columns
\n", "| \n", " | \"X (2000g)\" | \n", "\"Y (2000g)\" | \n", "\"Z (2000g)\" | \n", "
|---|---|---|---|
| Time | \n", "\n", " | \n", " | \n", " |
| 0.004394 | \n", "-0.122072 | \n", "-0.122072 | \n", "-0.061036 | \n", "
| 0.004594 | \n", "-0.061036 | \n", "0.488289 | \n", "-0.366217 | \n", "
| 0.004794 | \n", "0.183108 | \n", "0.122072 | \n", "-0.061036 | \n", "
| 0.004994 | \n", "0.122072 | \n", "-0.122072 | \n", "-0.122072 | \n", "
| 0.005194 | \n", "0.122072 | \n", "0.122072 | \n", "-0.244144 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "
| 27.691064 | \n", "-0.427253 | \n", "-0.152590 | \n", "-0.671397 | \n", "
| 27.691264 | \n", "-0.122072 | \n", "-0.335698 | \n", "-0.305180 | \n", "
| 27.691464 | \n", "-0.183108 | \n", "-0.152590 | \n", "-0.122072 | \n", "
| 27.691664 | \n", "-0.305180 | \n", "0.030518 | \n", "-0.244144 | \n", "
| 27.691864 | \n", "-0.305180 | \n", "-0.030518 | \n", "-0.366217 | \n", "
138440 rows × 3 columns
\n", "