Commit f773857e4975bbc9f298c96ddbb051cc129c1b5f

Changes:

- Support quoting characters in __init__
- Change "apply" to also work if you want to use the whole column as
  function parameter
- fix the *apply functions
- Docstrings update
  
5959 - getRowByID - get a row with a specified row name
6060 """
6161
62 def __init__(self,fh=None,row_names=None,header=True, delimiter="\t",
63 quoting=csv.QUOTE_MINIMAL, fixR=False):
62 def __init__(self, fh=None, row_names=None, header=True, delimiter="\t",
63 quoting=csv.QUOTE_MINIMAL, quotechar="'", fixR=False):
6464
6565 self._records = dict()
6666 self.rownames = list()
118118
119119 reader = csv.DictReader(fh,fieldnames=heading,
120120 delimiter=delimiter,quoting=quoting,
121 restval="NA")
121 restval="NA", quotechar=quotechar)
122122
123123 for row in reader:
124124 if self.identifier is not None:
191191
192192 self._records[key] = item
193193
194 def _colIndex(self, column):
195
196 "Returns the index of a column. Returns -1 if not found."
197
198 if column in self:
199 for index, item in enumerate(self.columns):
200 if item == column:
201 return index
202 else:
203 return -1
204
194205 def iterRows(self,**kwargs):
195206 """Iterate over a matrix's rows.
196207
316316 "You must supply a sequence object to this method"))
317317
318318 if len(other) != len(self):
319 print "Other", other
320319 raise ValueError(unicode("The column must be of the same length"))
321320 column_data = list()
322321 for index,item in enumerate(other):
505505
506506 return dest
507507
508def apply(matrix, func, column):
508def apply(matrix, func, column, whole=False):
509509
510 "Applies a function to a specific column. Rows are not supported (yet)."
510 """Applies a function to a specific column. Rows are not supported (yet).
511 The operation is performed in-place. If the parameter "whole" is specified,
512 the function is applied to all the column at once, rather than to each of
513 its members."""
511514
512515 assert isinstance(matrix, DataMatrix)
513516
517 #FIXME: Return a copy, it's safer!
518
514519 if column in matrix:
515520 columnData = matrix[column]
516 result = func(columnData)
521 if whole:
522 result = func(columnData)
523 else:
524 result = [func(item) for item in columnData]
517525 matrix.replace(result, column)
518 else:
519 print "Non existent column - no changes"
520526
521def elementApply(matrix, func):
527def elementApply(matrix, func, columns=None):
522528
523 """Applies a function to each element of rows or columns, and outputs a new
529 """Applies a function to each column for each row, and outputs a new
524530 matrix as result. If the function requires any type conversion, that must be
525 done by the user."""
531 done by the user. If the columns parameter is not None, the function is
532 applied only to specific columns."""
526533
527534 assert isinstance(matrix, DataMatrix)
528535
536 if columns:
537 indices = [matrix._colIndex(item) for item in columns]
538
529539 resultMatrix = EmptyMatrix(columns = matrix.columns,
530540 identifier=matrix.identifier)
541
531542 for row in matrix.iterRows():
532 newRow = [func(item) for item in row[1:]]
533 resultMatrix.appendRow(newRow,row[0])
543 rowname = row[0]
534544
545 if columns is not None:
546 for index in indices:
547 row[index+1] = func(row[index+1])
548 else:
549 row = [func(item) for item in row[1:]]
550 resultMatrix.appendRow(row, rowname)
551
535552 return resultMatrix
536553
537554def matrixApply(matrix, func, what="rows", resultName="Function result"):
665665
666666 return matrixApply(sourceMatrix, _mean,
667667 resultName="Mean value", what="columns")
668
668
669669def meanRows(sourceMatrix):
670670
671671 """Convenience function to calculate the mean of the rows of a DataMatrix
  
55import unittest
66from StringIO import StringIO
77
8def upperList(sequence):
9
10 return [toUpper(item) for item in sequence]
11
128def toUpper(string):
139
1410 "Simple function required for tests. Returns an uppercase string."
145145
146146 "Test of the apply function to a column"
147147
148 datamatrix.apply(self.matrix, upperList, "Name")
148 datamatrix.apply(self.matrix, toUpper, "Name")
149149 column = self.matrix["Name"]
150150 self.assertEqual(column, ["ALBERT", "GROUCHO"])
151151