Commit f773857e4975bbc9f298c96ddbb051cc129c1b5f
- Diff rendering mode:
- inline
- side by side
datamatrix/datamatrix.py
(42 / 15)
|   | |||
| 59 | 59 | - getRowByID - get a row with a specified row name | |
| 60 | 60 | """ | |
| 61 | 61 | ||
| 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): | ||
| 64 | 64 | ||
| 65 | 65 | self._records = dict() | |
| 66 | 66 | self.rownames = list() | |
| … | … | ||
| 118 | 118 | ||
| 119 | 119 | reader = csv.DictReader(fh,fieldnames=heading, | |
| 120 | 120 | delimiter=delimiter,quoting=quoting, | |
| 121 | restval="NA") | ||
| 121 | restval="NA", quotechar=quotechar) | ||
| 122 | 122 | ||
| 123 | 123 | for row in reader: | |
| 124 | 124 | if self.identifier is not None: | |
| … | … | ||
| 191 | 191 | ||
| 192 | 192 | self._records[key] = item | |
| 193 | 193 | ||
| 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 | |||
| 194 | 205 | def iterRows(self,**kwargs): | |
| 195 | 206 | """Iterate over a matrix's rows. | |
| 196 | 207 | ||
| … | … | ||
| 316 | 316 | "You must supply a sequence object to this method")) | |
| 317 | 317 | ||
| 318 | 318 | if len(other) != len(self): | |
| 319 | print "Other", other | ||
| 320 | 319 | raise ValueError(unicode("The column must be of the same length")) | |
| 321 | 320 | column_data = list() | |
| 322 | 321 | for index,item in enumerate(other): | |
| … | … | ||
| 505 | 505 | ||
| 506 | 506 | return dest | |
| 507 | 507 | ||
| 508 | def apply(matrix, func, column): | ||
| 508 | def apply(matrix, func, column, whole=False): | ||
| 509 | 509 | ||
| 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.""" | ||
| 511 | 514 | ||
| 512 | 515 | assert isinstance(matrix, DataMatrix) | |
| 513 | 516 | ||
| 517 | #FIXME: Return a copy, it's safer! | ||
| 518 | |||
| 514 | 519 | if column in matrix: | |
| 515 | 520 | columnData = matrix[column] | |
| 516 | result = func(columnData) | ||
| 521 | if whole: | ||
| 522 | result = func(columnData) | ||
| 523 | else: | ||
| 524 | result = [func(item) for item in columnData] | ||
| 517 | 525 | matrix.replace(result, column) | |
| 518 | else: | ||
| 519 | print "Non existent column - no changes" | ||
| 520 | 526 | ||
| 521 | def elementApply(matrix, func): | ||
| 527 | def elementApply(matrix, func, columns=None): | ||
| 522 | 528 | ||
| 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 | ||
| 524 | 530 | 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.""" | ||
| 526 | 533 | ||
| 527 | 534 | assert isinstance(matrix, DataMatrix) | |
| 528 | 535 | ||
| 536 | if columns: | ||
| 537 | indices = [matrix._colIndex(item) for item in columns] | ||
| 538 | |||
| 529 | 539 | resultMatrix = EmptyMatrix(columns = matrix.columns, | |
| 530 | 540 | identifier=matrix.identifier) | |
| 541 | |||
| 531 | 542 | for row in matrix.iterRows(): | |
| 532 | newRow = [func(item) for item in row[1:]] | ||
| 533 | resultMatrix.appendRow(newRow,row[0]) | ||
| 543 | rowname = row[0] | ||
| 534 | 544 | ||
| 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 | |||
| 535 | 552 | return resultMatrix | |
| 536 | 553 | ||
| 537 | 554 | def matrixApply(matrix, func, what="rows", resultName="Function result"): | |
| … | … | ||
| 665 | 665 | ||
| 666 | 666 | return matrixApply(sourceMatrix, _mean, | |
| 667 | 667 | resultName="Mean value", what="columns") | |
| 668 | |||
| 668 | |||
| 669 | 669 | def meanRows(sourceMatrix): | |
| 670 | 670 | ||
| 671 | 671 | """Convenience function to calculate the mean of the rows of a DataMatrix |
|   | |||
| 5 | 5 | import unittest | |
| 6 | 6 | from StringIO import StringIO | |
| 7 | 7 | ||
| 8 | def upperList(sequence): | ||
| 9 | |||
| 10 | return [toUpper(item) for item in sequence] | ||
| 11 | |||
| 12 | 8 | def toUpper(string): | |
| 13 | 9 | ||
| 14 | 10 | "Simple function required for tests. Returns an uppercase string." | |
| … | … | ||
| 145 | 145 | ||
| 146 | 146 | "Test of the apply function to a column" | |
| 147 | 147 | ||
| 148 | datamatrix.apply(self.matrix, upperList, "Name") | ||
| 148 | datamatrix.apply(self.matrix, toUpper, "Name") | ||
| 149 | 149 | column = self.matrix["Name"] | |
| 150 | 150 | self.assertEqual(column, ["ALBERT", "GROUCHO"]) | |
| 151 | 151 |

