A package for reading MATLAB .mat files, with support for MATLAB datatypes like table and string
Project description
Mat-IO Module
The mat-io module provides tools for load & save with .mat files, particularly perfomring read-write operations on user-defined objects or MATLAB datatypes such as datetime, table and string. It uses a wrapper built around scipy.io to read-write subsystem data in MAT-files, which is where MATLAB stores serialized object data. MAT-file versions v7 to v7.3 are supported.
mat-io can load and save almost all types of objects from MAT-files, including user-defined objects and handle class objects. Additionally, it includes utilities to convert the following MATLAB datatypes into their respective Pythonic objects, and vice versa:
stringdatetime,durationandcalendarDurationtableandtimetablecontainers.Mapanddictionarycategorical- Enumeration Instance Arrays
Note: load_from_mat() uses a modified version of scipy. The modifications include a few minor changes to scipy.io to process mxOPAQUE_CLASS variables. You can view the changes under patches/ and apply it manually. Note that you might need to rebuild as parts of the Cython code was modified. Follow the instructions on the official SciPy documentation.
Usage
Install using pip
pip install mat-io
Available Commands
To read MATLAB objects from a .mat file:
from matio import load_from_mat
file_path = "path/to/your/file.mat"
data = load_from_mat(
file_path,
raw_data=False,
add_table_attrs=False,
mdict=None,
variable_names=None,
**kwargs
)
print(data)
MATLAB Opaque objects are returned as an instance of class MatioOpaque with the following attributes:
classname: The class name, including namespace qualifiers (if any).type_system: An interal MATLAB type identifier. UsuallyMCOS, but could also bejavaorhandle.properties: A dictionary containing the property names and property values.
These objects are contained within numpy.ndarray in case of object arrays. If the raw_data parameter is set to False, then load_from_mat converts these objects into a corresponding Pythonic datatype.
To write Python objects to a .mat file:
from matio import save_to_mat
file_path = "path/to/your/file.mat"
mdict = {"var1": data1, "var2": data2}
save_to_mat(
file_path,
mdict=mdict,
version="v7",
do_compression=True,
global_vars=None,
oned_as="row",
)
When writing objects, matio tries to guess the class name of the object based on its datatype. For example, pandas.DataFrames could be read in as table or timetable. You can also explicitly mention the object by wrapping your data around an instance of MatioOpaque as follows:
from matio import MatioOpaque, save_to_mat
df = some_dataframe
mat_df = MatioOpaque(properties=df, classname="table")
mdict = {"table1": mat_df}
data = save_to_mat(file_path="temp.mat", mdict=mdict)
For user-defined classes, a dictionary of property name-value pairs must be wrapped around a MatioOpaque instance. In case of arrays, these objects should be contained within a numpy.ndarray with dtype=object and each MatioOpaque instance should be flagged with the is_array attribute. All user-defined classes default to the MCOS type system.
Notes
-
Extra keyword arguments (
**kwargs) are passed directly toscipy.io.loadmatorscipy.io.savemat. -
For conversion rules between MATLAB and Python datatypes, see the documentation.
-
To differentiate between MATLAB character and string arrays, it would be ideal to wrap a numpy string array around a
MatioOpaqueinstance. However, this is not feasible all the time, especially as MATLAB seems to be using strings in most of their new datatypes. Hence, this module uses Scipy's convention of converting Pythonstr,list(str)ornumpy.array(dtype=<U1/S1)to a character array. Numpy string arrays with multiple strings is converted to a MATLAB string
Contribution
Feel free to create a PR if you'd like to add something, or open up an issue if you'd like to discuss! I've also opened an issue with scipy.io detailing some of the workflow, as well as a PR to develop this iteratively. Please feel free to contribute there as well!
Acknowledgement
Big thanks to mahalex for their detailed breakdown of MAT-files. A lot of this wouldn't be possible without it.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mat_io-0.4.1.tar.gz.
File metadata
- Download URL: mat_io-0.4.1.tar.gz
- Upload date:
- Size: 158.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4140c98d8c495751f63a5d0bdf46792806b5bd919b79e5a82bf6d15cee42c6cb
|
|
| MD5 |
8db2096fcd6db801237cebc67e44f2f4
|
|
| BLAKE2b-256 |
8f7d606b284791b533284dbb909140a366716d9ae7b7288972da37a71bc7cb95
|
Provenance
The following attestation bundles were made for mat_io-0.4.1.tar.gz:
Publisher:
python-publish.yml on foreverallama/matio
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mat_io-0.4.1.tar.gz -
Subject digest:
4140c98d8c495751f63a5d0bdf46792806b5bd919b79e5a82bf6d15cee42c6cb - Sigstore transparency entry: 417264103
- Sigstore integration time:
-
Permalink:
foreverallama/matio@61d519e9513ff8f92ad319047c74926cc9a1daab -
Branch / Tag:
refs/tags/v0.4.1 - Owner: https://github.com/foreverallama
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@61d519e9513ff8f92ad319047c74926cc9a1daab -
Trigger Event:
release
-
Statement type:
File details
Details for the file mat_io-0.4.1-py3-none-any.whl.
File metadata
- Download URL: mat_io-0.4.1-py3-none-any.whl
- Upload date:
- Size: 170.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
00ab63dcab3fda17775775b6d3c15f732cb8110eb034af906a6d9ef4210abd0c
|
|
| MD5 |
9c19fd86028cba62f0e32841e07825dc
|
|
| BLAKE2b-256 |
a5b554baca561a5bf4b29f815786ce22865762f5415ccfb925b18cadb0ea351e
|
Provenance
The following attestation bundles were made for mat_io-0.4.1-py3-none-any.whl:
Publisher:
python-publish.yml on foreverallama/matio
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mat_io-0.4.1-py3-none-any.whl -
Subject digest:
00ab63dcab3fda17775775b6d3c15f732cb8110eb034af906a6d9ef4210abd0c - Sigstore transparency entry: 417264126
- Sigstore integration time:
-
Permalink:
foreverallama/matio@61d519e9513ff8f92ad319047c74926cc9a1daab -
Branch / Tag:
refs/tags/v0.4.1 - Owner: https://github.com/foreverallama
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@61d519e9513ff8f92ad319047c74926cc9a1daab -
Trigger Event:
release
-
Statement type: