-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpysmat.py
executable file
·119 lines (110 loc) · 3.88 KB
/
pysmat.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
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python
######################################################
#Python engine for csv & mat conversion
#Created by: Feng Xue 09/20/2018 @UCSD
######################################################
import sys
import os
import csv
import time
#################################################################################################
#For matlab 7.3 format support, we should use hdf5storage.savemat and hdf5storage.loadmat instead
#################################################################################################
if len(sys.argv)!=5:
print "Usage: "+ os.path.basename(sys.argv[0])+' mode file_in file_out variable'
print " mode 1: csv2mat"
print " mode 2: mat2csv"
os._exit(0)
mode=int(sys.argv[1])
file_in=sys.argv[2]
file_out=sys.argv[3]
var=sys.argv[4]
if not os.path.isfile(file_in):
print "Error: input file " + file_in + " doesn't exist"
os._exit(-1)
t = time.time()
if mode==1:
################
#csv to mat
################
from numpy.core.records import fromarrays
from scipy.io import savemat
f = open(file_in, 'rb')
reader = csv.reader(f)
headers = reader.next()
column = {}
for colidx in range(len(headers)):
column[colidx] = []
for row in reader:
for h, v in zip(headers, row):
colidx=headers.index(h)
try:
column[colidx].append(int(v))
except:
try:
column[colidx].append(float(v))
except:
column[colidx].append(v)
f.close()
data=[]
for idx in range(len(headers)):
data.append(column[idx])
abcd_info = fromarrays(data, names=headers)
savemat(file_out, {var: abcd_info}, oned_as='column')
elif mode==2:
################
#mat to csv
################
import scipy.io
import numpy as np
from itertools import chain
data = scipy.io.loadmat(file_in)
abcd_info = data[var]
abcd_info_new = []
for idx in range(len(abcd_info)):
tmp = list(chain.from_iterable(abcd_info[idx]))
for subidx in range(len(tmp)):
if isinstance(tmp[subidx],np.ndarray):
if len(tmp[subidx])==1:
tmp[subidx] = tmp[subidx][0]
if isinstance(tmp[subidx],np.ndarray):
if len(tmp[subidx])==1:
tmp[subidx] = tmp[subidx].flatten().tolist()[0]
elif len(tmp[subidx])==0:
tmp[subidx] = None
else:
print "Length is bigger than 1"
print tmp[subidx]
elif isinstance(tmp[subidx],np.unicode):
tmp[subidx] = tmp[subidx].astype(str)
else:
print "unknow type:"
type(tmp[subidx])
elif len(tmp[subidx])==0:
tmp[subidx] = None
else:
print "Length is bigger than 1"
print tmp[subidx]
else:
print "type exception:"
type(tmp[subidx])
print tmp[subidx]
if not isinstance(tmp[subidx],int) and not isinstance(tmp[subidx],float):
try:
tmp[subidx] = int(tmp[subidx])
except:
try:
tmp[subidx] = float(tmp[subidx])
except:
pass
abcd_info_new.append(tmp)
with open(file_out, 'wb') as fp:
writer = csv.writer(fp, quoting=csv.QUOTE_NONNUMERIC)
writer.writerow(abcd_info.dtype.names)
writer.writerows(abcd_info_new)
fp.close()
else:
print "Error: Mode should be 1 or 2"
os._exit(-1)
elapsed = time.time() - t
print ('Elapsed: '+ str(elapsed))