Commit 81ae6d2beb39b9eb251a243236e6f320e4eac9d5
- Diff rendering mode:
- inline
- side by side
datamatrix/datamatrix.py
(36 / 4)
|   | |||
| 457 | 457 | ||
| 458 | 458 | del self._records[oldColumn] | |
| 459 | 459 | ||
| 460 | def sortRows(self, column=None): | ||
| 460 | 461 | ||
| 462 | "Sorts the rows. Works only if rownames are set, at the moment" | ||
| 463 | |||
| 464 | if column: | ||
| 465 | self._records[column].sort(key=lambda x: x[1]) | ||
| 466 | sorted_rownames = [item[0] for item in self._records[column]] | ||
| 467 | # FIXME: Inefficient! | ||
| 468 | sorted_indices = [self.rownames.index(item) for item in sorted_rownames] | ||
| 469 | |||
| 470 | for record in self: | ||
| 471 | |||
| 472 | if record == column: | ||
| 473 | continue | ||
| 474 | |||
| 475 | value = self._records[record] | ||
| 476 | value = [value[index] for index in sorted_indices] | ||
| 477 | self._records[record] = value | ||
| 478 | |||
| 479 | self.rownames = sorted_rownames | ||
| 480 | |||
| 481 | else: | ||
| 482 | for record in self._records: | ||
| 483 | self._records[record].sort(key=lambda x: x[0]) | ||
| 484 | self.rownames.sort() | ||
| 485 | |||
| 486 | def sortColumns(self): | ||
| 487 | |||
| 488 | "Sorts columns." | ||
| 489 | |||
| 490 | self.columns.sort() | ||
| 491 | |||
| 492 | |||
| 461 | 493 | class EmptyMatrix(DataMatrix): | |
| 462 | 494 | ||
| 463 | 495 | """DataMatrix variant that once instantiated generates an | |
| … | … | ||
| 643 | 643 | columnData = matrix[column] | |
| 644 | 644 | resultData = func(columnData) | |
| 645 | 645 | result.append(resultData) | |
| 646 | |||
| 646 | |||
| 647 | 647 | resultMatrix.appendRow(result,resultName) | |
| 648 | 648 | ||
| 649 | 649 | elif what == "columns": | |
| … | … | ||
| 677 | 677 | value = columnData[index] | |
| 678 | 678 | ||
| 679 | 679 | if func(value): | |
| 680 | # We need the whole row, not just the selection | ||
| 680 | # We need the whole row, not just the selection | ||
| 681 | 681 | rowData = row[1:] # Get the ID iout | |
| 682 | 682 | resultMatrix.appendRow(rowData, row[0]) | |
| 683 | 683 | else: | |
| … | … | ||
| 742 | 742 | """Convenience function to calculate the mean of the rows of a DataMatrix | |
| 743 | 743 | instance.""" | |
| 744 | 744 | ||
| 745 | return matrixApply(sourceMatrix, _mean, | ||
| 745 | return matrixApply(sourceMatrix, _mean, | ||
| 746 | 746 | resultName="Mean value", what="rows") | |
| 747 | 747 | ||
| 748 | 748 | def transpose(matrix, identifier="x"): | |
| … | … | ||
| 757 | 757 | row_index = index + 1 | |
| 758 | 758 | column = matrix.getRow(row_index, row_name=False) | |
| 759 | 759 | destination.append(column, matrix.rownames[index]) | |
| 760 | |||
| 760 | |||
| 761 | 761 | return destination | |
| 762 | 762 | ||
| 763 | 763 | def subset(matrix, column_list): |
datamatrix/test_datamatrix.py
(30 / 3)
|   | |||
| 39 | 39 | header=False, | |
| 40 | 40 | delimiter=" ", | |
| 41 | 41 | skip=1) | |
| 42 | self.rowNameMatrix = datamatrix.DataMatrix(matrixFile, header=True, | ||
| 43 | delimiter=" ", row_names=2) | ||
| 42 | 44 | ||
| 43 | 45 | def testBasics(self): | |
| 44 | 46 | ||
| … | … | ||
| 209 | 209 | "Test joining two matrices by rows" | |
| 210 | 210 | ||
| 211 | 211 | matrixFile = StringIO('Name surname\nIsaac Asimov\nJohn Watson') | |
| 212 | matrix = datamatrix.DataMatrix(matrixFile, header=True, | ||
| 212 | matrix = datamatrix.DataMatrix(matrixFile, header=True, | ||
| 213 | 213 | delimiter=" ") | |
| 214 | 214 | new_matrix = datamatrix.rbind(self.matrix, matrix) | |
| 215 | 215 | self.assertEqual(new_matrix.getRow(3), ["3","Isaac", "Asimov"]) | |
| … | … | ||
| 223 | 223 | matrixFile = StringIO( | |
| 224 | 224 | 'Job Skills\nscientist relativity\ncomedian humor' | |
| 225 | 225 | ) | |
| 226 | matrix = datamatrix.DataMatrix(matrixFile, header=True, | ||
| 226 | matrix = datamatrix.DataMatrix(matrixFile, header=True, | ||
| 227 | 227 | delimiter=" ") | |
| 228 | 228 | new_matrix = datamatrix.cbind(self.matrix, matrix) | |
| 229 | 229 | self.assertEqual(new_matrix["Job"], ["scientist", "comedian"]) | |
| 230 | self.assertEqual(new_matrix.getRow(2), | ||
| 230 | self.assertEqual(new_matrix.getRow(2), | ||
| 231 | 231 | ["2", "Groucho", "Marx", "comedian", "humor"]) | |
| 232 | |||
| 233 | def testRowNames(self): | ||
| 234 | |||
| 235 | "Test row names assignment" | ||
| 236 | |||
| 237 | self.assertEqual(self.rowNameMatrix.identifier, "surname") | ||
| 238 | self.assertEqual(self.rowNameMatrix.rownames, ["Einstein", "Marx"]) | ||
| 239 | |||
| 240 | def testSortRow(self): | ||
| 241 | |||
| 242 | "Test row sorting" | ||
| 243 | |||
| 244 | self.rowNameMatrix.appendRow(["Isaac"], "Asimov") | ||
| 245 | self.rowNameMatrix.sortRows() | ||
| 246 | |||
| 247 | self.assertEqual(self.rowNameMatrix.rownames, ["Asimov", "Einstein", | ||
| 248 | "Marx"]) | ||
| 249 | self.assertEqual(self.rowNameMatrix["Name"], ["Isaac", "Albert", | ||
| 250 | "Groucho"]) | ||
| 251 | |||
| 252 | def testSortColumns(self): | ||
| 253 | |||
| 254 | "Test column sorting...TODO" | ||
| 255 | |||
| 256 | pass | ||
| 232 | 257 | ||
| 233 | 258 | ||
| 234 | 259 | def runTests(): |

