The main.py script is a complete pipeline designed to process time-series data from specific MATLAB (.mat) files. It performs the following steps for each file:
- Loads raw sensor data from keys ending in 'X', 'Y', and 'Z'.
- Filters the data to remove very low frequencies (0-0.5 Hz).
- Decimates the data to reduce the sampling rate.
- Segments the processed data into 50-second chunks.
- Visualizes each chunk as a line plot and saves it as a
.pngimage.
This entire process is done in a single run, without creating intermediate files.
To run this script, you need a Python environment with the following libraries installed:
scipynumpypandasmatplotlib
These libraries are already present in the lstm_fcn conda environment we have been using.
You can execute the script from your terminal. Make sure you have activated the correct conda environment first.
# 1. Activate the conda environment
conda activate lstm_fcn
# 2. Run the main.py script
python main.pyUpon execution, the script will process all .mat files listed inside it and generate the output plots in the results directory.
This is the core function for data extraction.
- Input: Takes the path to a single
.matfile. - Process:
- It loads the
.matfile into a Python dictionary-like object usingscipy.io.loadmat. - It searches for all variable names (keys) in the file that start with
Untitledand end withX,Y, orZ. This targets the specific sensor data channels. - For each key found, it accesses the nested data structure. Based on the file format, the actual data is located at
mat_data[key][0, 0]. - This
[0, 0]element is a specialnumpy.voidobject, which is how SciPy represents a MATLABstruct. The function checks if this struct contains a field named'Data'. - If the
'Data'field exists, it extracts the numerical array from it. This array contains the actual time-series measurements for that sensor. - The extracted 1D arrays (one for each sensor) are then stacked together as columns to form a single 2D array.
- It loads the
- Output: Returns the 2D NumPy array (
data_array) where each column is a different sensor, and a list of the keys (sensor_keys) that were successfully extracted.
This is the main function that controls the entire workflow.
- Process:
- It defines a list of all
.matfiles to be processed. - It loops through each
file_pathin the list. - For each file, it calls
load_data_from_mat_fileto get the raw data. - It converts the loaded data array into a pandas DataFrame for easier column-based operations. Each column is named after the sensor key it came from (e.g.,
Untitled3204Y). - It then enters a loop to process each
sensor_col(each column) in the DataFrame individually. - Filtering: It applies a high-pass Butterworth filter to remove frequencies below 0.5 Hz.
- Decimation: It decimates the filtered signal by a factor of 20, which significantly reduces the number of data points and smooths the signal.
- Segmenting and Visualizing: It calculates how many 50-second chunks can be created from the decimated data. It then loops, creating one chunk at a time, and immediately generates a plot for that chunk using
matplotlib. - Plot Customization: Each plot is created with a
figsizeof(15, 3)to achieve a 5:1 aspect ratio, and no title is added, as requested. - Saving Output: The generated plot is saved directly as a
.pngfile into a structured output directory.
- It defines a list of all
The script will create a new top-level directory named results. The structure of the output will be:
results/
├── SETUP1/
│ ├── Untitled3204Y/
│ │ ├── chunk_1.png
│ │ ├── chunk_2.png
│ │ └── ...
│ ├── Untitled3104Z/
│ │ ├── chunk_1.png
│ │ └── ...
│ └── ...
├── SETUP2/
│ ├── ...
└── ...
Each .mat file gets its own folder, and inside that, each processed sensor channel gets its own subfolder containing the .png image files of its data chunks.