File size: 3,203 Bytes
83913eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sys
from config import *
from scipy.io import loadmat
import numpy as np

# load .mat data
class DataSet:
    def __init__(self, matfile) -> None:
        
        assert os.path.exists(matfile), f'{matfile} not exist'
        
        _ , ext = os.path.splitext(matfile)
        assert ext == '.mat', 'not reading mat file'
        
        matdat = loadmat(matfile,mat_dtype=True)

        self.arraynames = []
        for key, value in matdat.items():
            
            if key.startswith("__"):
                # skip meta data
                continue
            if isinstance(value,np.ndarray):
                if value.dtype.kind in {'f','i','u'}:
                    # convert to float or double
                    value = value.astype(DTYPE)
                
                if value.shape[0]> 1:
                    self.arraynames.append(key)

                if value.size == 1:
                    # if singleton, get number
                    value = value.item()
                
                setattr(self, key, value)

        self.dim = self.X_res.shape[1]
        self.xdim = self.X_res.shape[1]-1
        
        # collection of attributes not callable
        self.attr = [a for a in dir(self) if not a.startswith("__") and not callable(getattr(self,a))]
        

    def print(self, attr=None):
        '''print data set

        '''
        # if attr is not None, print attr
        
        attr_to_print = self.attr if attr is None else attr
        # if attr is None:
        for a in attr_to_print:
            x = getattr(self, a)
            print(f"{a} {x}")
            if isinstance(x, np.ndarray):
                print(f"{x.shape} {x.dtype}")
    
    def downsample(self,n, names=None):
        ''' downsample data size

        '''
        names = self.arraynames if names is None else names
        # get variable name in .mat, remove meta info
        for a in names:
            # check if attribute exist
            if not hasattr(self, a):
                continue
            x = getattr(self, a)
            # only work on variables with more than one rows
            print(f'downsample {a} from {x.shape[0]} to {n} ')
            x = x[:n,:]
            setattr(self, a, x)

    def subsample(self, idx, names):
        ''' subsample data set

        '''
        names = self.arraynames if names is None else names
        for a in names:
            x = getattr(self, a)
            # only work on variables with more than one rows
            print(f'subsample {a} from {x.shape[0]} to {len(idx)} ')
            x = x[idx,:]
            setattr(self, a, x)
    
    
    def shuffle(self):
        ''' permute data set

        '''
        idx = np.random.permutation(self.X_res.shape[0])
        for a in self.arraynames:
            x = getattr(self, a)
            x = x[idx,:]
            setattr(self, a, x)

    


if __name__ == "__main__":
    # read mat file and print dataset
    filename  = sys.argv[1]
    vars2print = sys.argv[2:] if len(sys.argv) > 2 else None

    dataset = DataSet(filename)
    dataset.print(vars2print)