-
Notifications
You must be signed in to change notification settings - Fork 1
/
NamedMatrix.py
274 lines (204 loc) · 10.7 KB
/
NamedMatrix.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# -*- coding: utf-8 -*-
"""
NamedMatrix (filename = None, delimiter = ',', npMatrix = None, colnames = None, rownames = None))
A wrapper class enable access data matrix elements by col and row names.
This class is capable of parsing csv files and keep track the row and column names.
Elements such as rows and columns or a block of matrix according to names of row and column
as well as index arrays
Created on Sun Aug 25 08:40:33 2013
@author: xinghualu
"""
import numpy as np
import sys
from StringIO import StringIO
import gc
class NamedMatrix:
## Constructor
# @param filename=None A string point to a text matrix file
# @param delimiter=',' A string indicate the delimiter separating fields in txt
# @param npMatrix=None A reference to a numpy matrix
# @colnames A string array of column names
# @rownames A string array of rownames
def __init__(self, filename = None, delimiter = ',', npMatrix = None, colnames = None, rownames = None):
if filename and npMatrix:
raise Exception ("Cannot create a NamedMatrix with both 'npMatrix' and 'filename' arguments set")
if not filename and npMatrix == None:
raise Exception ("Attempt to create a NameMatrix without 'filename' or an 'npMatrix'")
if filename:
print "Extracting matrix file " + filename
try:
f = open(filename, 'r')
lines = f.readlines()
f.close()
except IOError:
print "Fail to read file " + filename
return
if len(lines) == 1: # Mac version csv, end with "\r" instead of "\n" as return
lines = lines[0].split("\r")
self.colnames = lines.pop(0).strip().split(',') # split header and extract colnames
map(lambda x: x.strip(), lines) # remove the "\r"
lines = "\n".join(lines) # use "\n" to join lines
else:
self.colnames = lines.pop(0).strip().split(',')
self.colnames.pop(0)
self.colnames = [x.translate(None, '"') for x in self.colnames]
lines = "".join(lines)
# extract condition name
self.rownames = list()
for l in lines.split("\n"):
self.rownames.append(l.split(',')[0])
if lines[-1] == '\n':
self.rownames.pop()
# read in data and generate a numpy data matrix
self.data = np.genfromtxt(StringIO(lines), delimiter = ",", usecols=tuple(range(1, len(self.colnames)+1)))
# except:
# print "Problem generating data from matrix file. Please check data file to make sure matrix is formatted properly."
# return None
if self.data.shape[0] != len(self.rownames):
print "Name matrix: When parsing %s, The size of matrix does not match the length of rownames" %filename
print self.rownames
print self.data.shape
raise Exception ()
if self.data.shape[1] != len(self.colnames):
print "Name matrix: When parsing %s, The size of matrix does not match the length of colnames" %filename
raise Exception()
if npMatrix != None:
self.data = npMatrix
nrow, ncol = np.shape(self.data)
if colnames:
if len(colnames) == ncol:
self.colnames = colnames
else:
raise Exception("Dimensions of input colnames and matrix do not agree")
else:
self.colnames = list()
for c in range(ncol):
self.colnames.append('c' + str(c))
if rownames:
if len(rownames) == nrow:
self.rownames = rownames
else:
raise Exception("Dimensions of input rownames and matrix do not agree")
else:
self.rownames = list()
for r in range(nrow):
self.rownames.append('r' + str(r))
self.nrows, self.ncols = np.shape(self.data)
self.rownames = [x.replace("\"", "") for x in self.rownames]
self.colnames = [x.replace("\"", "") for x in self.colnames]
# force garbage collection to clean the read in text
gc.collect()
def setColnames(self, colnames):
## set the column names
if len(colnames) == len(self.colnames):
self.colnames = colnames
elif len(colnames) == self.data.shape[1]:
self.colnames = colnames
else:
raise Exception("New colnames vector has differnt dimension as the original colnames")
def getColnames(self):
return self.colnames
def setRownames(self, rownames):
if len(rownames) == len(self.rownames):
self.rownames = rownames
elif len(rownames) == self.data.shape[0]:
self.rownames = rownames
else:
raise Exception("New rownames vector has differnt dimension as the original colnames")
def getRownames(self):
return self.rownames
def getValuesByCol(self, colnames):
if isinstance (colnames, list):
if not set(colnames) <= set(self.colnames):
raise Exception("Try to access nonexisting columns")
else:
colIndx = map(lambda x: self.colnames.index(x), colnames)
ixgrid = np.ix_(range(self.nrows), colIndx)
return self.data[ixgrid]
if isinstance(colnames, basestring):
if colnames not in self.colnames:
raise Exception ("Try to access non-existing column")
else:
return self.data[:, self.colnames.index(colnames)]
def getValuesByRow(self, rownames):
if isinstance (rownames, list):
if not set(rownames) <= set(self.rownames):
raise Exception("Try to access nonexisting rows")
else:
rowIndx = map(lambda x: self.rownames.index(x), rownames)
ixgrid = np.ix_(rowIndx, range(self.ncols))
return self.data[ixgrid]
if isinstance(rownames, basestring):
if rownames not in self.rownames:
raise Exception ("Try to access non-existing row")
else:
return self.data[self.rownames.index(rownames), :]
def setValuesByColName(self, values, col):
self.data[:,self.colnames.index(col)] = values
def shape(self):
if self.data != None:
return np.shape(self.data)
else:
return None
## Return the position indices of colnames
def findColIndices(self, colnames):
if isinstance (colnames, list):
if not set(colnames) <= set(self.colnames):
raise Exception("Try to access nonexisting columns")
else:
colIndx = map(lambda x: self.colnames.index(x), colnames)
return colIndx
if isinstance(colnames, basestring):
if colnames not in self.colnames:
raise Exception ("Try to access non-existing column")
else:
return self.colnames.index(colnames)
def getSubMatrixByCols(self, columnNames):
if isinstance (columnNames, list):
if not set(columnNames) <= set(self.colnames):
raise Exception("Try to access nonexisting columns")
else:
colIndx = map(lambda x: self.colnames.index(x), columnNames)
ixgrid = np.ix_(range(self.nrows), colIndx)
#print str(ixgrid)
return NamedMatrix(npMatrix = self.data[ixgrid], colnames = columnNames, rownames = self.rownames)
if isinstance(columnNames, basestring):
if columnNames not in self.colnames:
raise Exception ("Try to access non-existing column")
else:
return NamedMatrix(npMatrix = self.data[:, self.colnames.index(columnNames)], colnames = columnNames, rownames = self.rownames)
## Return the position indices of rownames
def findRowIndices(self, rownames):
if set(rownames) - set(self.rownames):
raise Exception("Unknown column name is used to query index")
return [lambda x: self.rownames.index(x) for x in rownames]
def setCellValue(self, rowname, colname, value):
value = np.float(value) # force it into a np.float
self.data[self.rownames.index(rowname), self.colnames.index(colname)] = value
## Output matrix to a text file
def writeToText(self, filename, delimiter=',', filePath=None,):
if not filePath:
try:
outMatrix = open(filename, "w") #Open file containing the output matrix
except:
print "Could not find filepath to output file: " + filename + ". Please ensure you have given an existing filepath."
sys.exit()
else:
try:
outMatrix = open(filePath + "/" + filename, "w") #Open file containing the output matrix
except:
print "Could not open output file: " + filePath + "/" + filename +". Please ensure you have given an existing filepath."
sys.exit()
#Writing out the column header. Iterate through colnames in our class, and write
# them out to the first line of the file, seperated by the given delimiter.
outMatrix.write("Sample") #Write "Sample" as the first cell of our matrix. Properly aligns the rows and columns.
for colName in self.colnames: #Write out rest of column headers
outMatrix.write(delimiter + colName)
outMatrix.write("\n")
#Write out each row of our matrix
for i in range(self.shape()[0]):
outMatrix.write(self.rownames[i]) #Write out the rowName for the particular row
for j in self.data[i]:
outMatrix.write(delimiter + str(round(j, 4))) #Write out each cell of data for that row, separated by the given delimiter
outMatrix.write("\n")
outMatrix.close() #Done writing matrix, close file