This repository was archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathmatlabmat_file.py
111 lines (92 loc) · 3.53 KB
/
matlabmat_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
Input/output class for the matlab .mat fileformat
"""
import numpy as np
import pandas as pd
import os
import scipy.io
try:
from .file import File, WrongFormatError, BrokenFormatError
except:
File=dict
EmptyFileError = type('EmptyFileError', (Exception,),{})
WrongFormatError = type('WrongFormatError', (Exception,),{})
BrokenFormatError = type('BrokenFormatError', (Exception,),{})
class MatlabMatFile(File):
"""
Read/write a mat file. The object behaves as a dictionary.
Main methods
------------
- read, write, toDataFrame, keys
Examples
--------
f = MatlabMatFile('file.mat')
print(f.keys())
print(f.toDataFrame().columns)
"""
@staticmethod
def defaultExtensions():
""" List of file extensions expected for this fileformat"""
return ['.mat']
@staticmethod
def formatName():
""" Short string (~100 char) identifying the file format"""
return 'Matlab mat file'
@staticmethod
def priority(): return 60 # Priority in weio.read fileformat list between 0=high and 100:low
def __init__(self, filename=None, **kwargs):
""" Class constructor. If a `filename` is given, the file is read. """
self.filename = filename
if filename:
self.read(**kwargs)
def read(self, filename=None, **kwargs):
""" Reads the file self.filename, or `filename` if provided """
# --- Standard tests and exceptions (generic code)
if filename:
self.filename = filename
if not self.filename:
raise Exception('No filename provided')
if not os.path.isfile(self.filename):
raise OSError(2,'File not found:',self.filename)
if os.stat(self.filename).st_size == 0:
raise EmptyFileError('File is empty:',self.filename)
mfile = scipy.io.loadmat(self.filename)
def write(self, filename=None):
""" Rewrite object to file, or write object to `filename` if provided """
if filename:
self.filename = filename
if not self.filename:
raise Exception('No filename provided')
#with open(self.filename,'w') as f:
# f.write(self.toString)
raise NotImplementedError()
def toDataFrame(self):
""" Returns object into one DataFrame, or a dictionary of DataFrames"""
# --- Example (returning one DataFrame):
# return pd.DataFrame(data=np.zeros((10,2)),columns=['Col1','Col2'])
# --- Example (returning dict of DataFrames):
#dfs={}
#cols=['Alpha_[deg]','Cl_[-]','Cd_[-]','Cm_[-]']
#dfs['Polar1'] = pd.DataFrame(data=..., columns=cols)
#dfs['Polar1'] = pd.DataFrame(data=..., columns=cols)
# return dfs
raise NotImplementedError()
# --- Optional functions
def __repr__(self):
""" String that is written to screen when the user calls `print()` on the object.
Provide short and relevant information to save time for the user.
"""
s='<{} object>:\n'.format(type(self).__name__)
s+='|Main attributes:\n'
s+='| - filename: {}\n'.format(self.filename)
# --- Example printing some relevant information for user
#s+='|Main keys:\n'
#s+='| - ID: {}\n'.format(self['ID'])
#s+='| - data : shape {}\n'.format(self['data'].shape)
s+='|Main methods:\n'
s+='| - read, write, toDataFrame, keys'
return s
def toString(self):
""" """
s=''
return s