Hướng dẫn create .mat file python

I want to save a list of elements (r) to .mat file format inside python I convert it to array

Nội dung chính

  • Loading .mat files
  • Formatting the data
  • Hello Pandas
  • Can .MAT files be opened in Python?
  • How do I load a .MAT dataset in Python?
  • How do I open a MATLAB data file in Python?
  • How do I read a .MAT file?

r = Lattice([Marker('l000015$start', NumIntSteps=40, length=array(0.)), Marker('ip.1', NumIntSteps=40, length=array(0.)), Drift('drift_0', 2.2002250210956804), Quadrupole('qc1l1.1', 1.2, -0.6451875623775719, NumIntSteps=40), Monitor('BPM0'), Drift('drift_1', 0.08000000000000007), Monitor('BPM1'), Quadrupole('qc1r2.1', 1.0, 0.34045204285588043, NumIntSteps=40), Monitor('BPM2'), Drift('drift_2', 0.08000000000000007), Monitor('BPM3'), Quadrupole('qc1r3.1', 1.0, 0.18818867758026042, NumIntSteps=40), Monitor('BPM4'), Drift('drift_3', 0.2999999999999998), Quadrupole('qc2r1.1', 1.25, 0.026716664923441297, NumIntSteps=40), Monitor('BPM5'), Drift('drift_4', 0.08000000000000096), Monitor('BPM6'), Quadrupole('qc2r2.1', 1.25, -0.02041914442905664, NumIntSteps=40), Monitor('BPM7'),..........



import numpy as np
import scipy.io
arr = np.array(r)

Then i tried to save it using

scipy.io.savemat('file.mat', arr)

I got the error massage

AttributeError: 'numpy.ndarray' object has no attribute 'items'

Could you please clarify to me what is the "items" means,

I imported this list from .mat in python then i added some elements to it inside python and now i want to save it in .mat format.

scipy.io.savemat(file_name, mdict, appendmat=True, format='5', long_field_names=False, do_compression=False, oned_as='row')[source]#

Save a dictionary of names and arrays into a MATLAB-style .mat file.

This saves the array objects in the given dictionary to a MATLAB- style .mat file.

Parametersfile_namestr or file-like object

Name of the .mat file (.mat extension not needed if appendmat == True). Can also pass open file_like object.

mdictdict

Dictionary from which to save matfile variables.

appendmatbool, optional

True (the default) to append the .mat extension to the end of the given filename, if not already present.

format{‘5’, ‘4’}, string, optional

‘5’ (the default) for MATLAB 5 and up (to 7.2), ‘4’ for MATLAB 4 .mat files.

long_field_namesbool, optional

False (the default) - maximum field name length in a structure is 31 characters which is the documented maximum length. True - maximum field name length in a structure is 63 characters which works for MATLAB 7.6+.

do_compressionbool, optional

Whether or not to compress matrices on write. Default is False.

oned_as{‘row’, ‘column’}, optional

If ‘column’, write 1-D NumPy arrays as column vectors. If ‘row’, write 1-D NumPy arrays as row vectors.

Examples

>>> from scipy.io import savemat
>>> a = np.arange(20)
>>> mdic = {"a": a, "label": "experiment"}
>>> mdic
{'a': array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
    17, 18, 19]),
'label': 'experiment'}
>>> savemat("matlab_matrix.mat", mdic)

There is a nice package called mat4py which can easily be installed using

Nội dung chính

  • Loading .mat files
  • Formatting the data
  • Hello Pandas
  • Can .MAT files be opened in Python?
  • How do I load a .MAT dataset in Python?
  • How do I open a MATLAB data file in Python?
  • How do I read a .MAT file?
pip install mat4py

It is straightforward to use (from the website):

Load data from a MAT-file

The function loadmat loads all variables stored in the MAT-file into a simple Python data structure, using only Python’s dict and list objects. Numeric and cell arrays are converted to row-ordered nested lists. Arrays are squeezed to eliminate arrays with only one element. The resulting data structure is composed of simple types that are compatible with the JSON format.

Example: Load a MAT-file into a Python data structure:

from mat4py import loadmat

data = loadmat('datafile.mat')

The variable data is a dict with the variables and values contained in the MAT-file.

Save a Python data structure to a MAT-file

Python data can be saved to a MAT-file, with the function savemat. Data has to be structured in the same way as for loadmat, i.e. it should be composed of simple data types, like dict, list, str, int, and float.

Example: Save a Python data structure to a MAT-file:

from mat4py import savemat

savemat('datafile.mat', data)

The parameter data shall be a dict with the variables.

Matlab is a really popular platform for scientific computing in the academia. I’ve used it my throughout my engineering degree and chances are, you will come across .mat files for datasets released by the universities.

This is a brief post which explains how to load these files using python, the most popular language for machine learning today.

The data

I wanted to build a classifier for detecting cars of different models and makes and so the Stanford Cars Dataset appeared to be a great starting point. Coming from the academia, the annotations for the dataset was in the .mat format. You can get the file used in this post here.

Loading .mat files

Scipy is a really popular python library used for scientific computing and quite naturally, they have a method which lets you read in .mat files. Reading them in is definitely the easy part. You can get it done in one line of code:

from scipy.io import loadmat
annots = loadmat('cars_train_annos.mat')

Well, it’s really that simple. But let’s go on and actually try to get the data we need out of this dictionary.

Formatting the data

The loadmat method returns a more familiar data structure, a python dictionary. If we peek into the keys, we’ll see how at home we feel now compared to dealing with a .mat file:

annots.keys()
> dict_keys(['__header__', '__version__', '__globals__', 'annotations'])

Looking at the documentation for this dataset, we’ll get to learn what this is really made of. The README.txt gives us the following information:

This file gives documentation for the cars 196 dataset.
(http://ai.stanford.edu/~jkrause/cars/car_dataset.html)
— — — — — — — — — — — — — — — — — — — —
Metadata/Annotations
— — — — — — — — — — — — — — — — — — — —
Descriptions of the files are as follows:
-cars_meta.mat:
Contains a cell array of class names, one for each class.
-cars_train_annos.mat:
Contains the variable ‘annotations’, which is a struct array of length
num_images and where each element has the fields:
bbox_x1: Min x-value of the bounding box, in pixels
bbox_x2: Max x-value of the bounding box, in pixels
bbox_y1: Min y-value of the bounding box, in pixels
bbox_y2: Max y-value of the bounding box, in pixels
class: Integral id of the class the image belongs to.
fname: Filename of the image within the folder of images.
-cars_test_annos.mat:
Same format as ‘cars_train_annos.mat’, except the class is not provided.
— — — — — — — — — — — — — — — — — — — —
Submission file format
— — — — — — — — — — — — — — — — — — — —
Files for submission should be .txt files with the class prediction for
image M on line M. Note that image M corresponds to the Mth annotation in
the provided annotation file. An example of a file in this format is
train_perfect_preds.txt
Included in the devkit are a script for evaluating training accuracy,
eval_train.m. Usage is:
(in MATLAB)
>> [accuracy, confusion_matrix] = eval_train(‘train_perfect_preds.txt’)
If your training predictions work with this function then your testing
predictions should be good to go for the evaluation server, assuming
that they’re in the same format as your training predictions.

Our interest is in the 'annotations' variable, as it contains our class labels and bounding boxes. It’s a struct, a data type very familiar to folks coming from a strongly typed language like a flavour of C or java.

A little digging into the object gives us some interesting things to work with:

type(annots[‘annotations’]),annots[‘annotations’].shape
>(numpy.ndarray, (1, 8144))
type(annots['annotations'][0][0]),annots['annotations'][0][0].shape
>(numpy.void, ())

The annotations are stored in a numpy.ndarray format, however the data type for the items inside this array is numpy.void and numpy doesn’t really seem to know the shape of them.

The documentation page for the loadmat method tells us how it loads matlab structs into numpy structured arrays.You can access the members of the structs using the keys:

annots[‘annotations’][0][0][‘bbox_x1’], annots[‘annotations’][0][0][‘fname’]> (array([[39]], dtype=uint8), array(['00001.jpg'], dtype='<U9'))

So now that we know how to access the members of the struct, we can iterate through all of them and store them in a list:

[item.flat[0] for item in annots[‘annotations’][0][0]]> [39, 116, 569, 375, 14, '00001.jpg']

Here, we can use the flat method to squeeze the value out of the array.

Hello Pandas

Now that we know how to deal with matlab files in python, let’s convert it into a pandas data frame. We can do so easily using a list of lists:

data = [[row.flat[0] for row in line] for line in annots[‘annotations’][0]]columns = [‘bbox_x1’, ‘bbox_y1’, ‘bbox_x2’, ‘bbox_y2’, ‘class’, ‘fname’]
df_train = pd.DataFrame(data, columns=columns)

Finally, familiar territory!

The code for this post can be found here.

Can .MAT files be opened in Python?

Matlab 7.3 and greater Beginning at release 7.3 of Matlab, mat files are actually saved using the HDF5 format by default (except if you use the -vX flag at save time, see in Matlab). These files can be read in Python using, for instance, the PyTables or h5py package.

How do I load a .MAT dataset in Python?

Just do as follows:.

Install the package: pip install pymatreader..

Import the relevant function of this package: from pymatreader import read_mat..

Use the function to read the matlab struct: data = read_mat('matlab_struct. mat').

use data. keys() to locate where the data is actually stored..

How do I open a MATLAB data file in Python?

Read Matlab mat Files in Python.

Use the scipy.io Module to Read .mat Files in Python..

Use the NumPy Module to Read mat Files in Python..

Use the mat4py Module to Read mat Files in Python..

Use the matlab.engine Module to Read mat Files in Python..

How do I read a .MAT file?

How to Open an MAT File. MAT files that are Microsoft Access Shortcut files can be created by dragging a table out of Access and to the desktop or into another folder. Microsoft Access needs to be installed in order to use them. MATLAB from MathWorks can open MAT files that are used by that program.