Commit 81ae6d2beb39b9eb251a243236e6f320e4eac9d5

Basic support for sorting rows. It's terribly inefficient. however.
  
457457
458458 del self._records[oldColumn]
459459
460 def sortRows(self, column=None):
460461
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
461493class EmptyMatrix(DataMatrix):
462494
463495 """DataMatrix variant that once instantiated generates an
643643 columnData = matrix[column]
644644 resultData = func(columnData)
645645 result.append(resultData)
646
646
647647 resultMatrix.appendRow(result,resultName)
648648
649649 elif what == "columns":
677677 value = columnData[index]
678678
679679 if func(value):
680 # We need the whole row, not just the selection
680 # We need the whole row, not just the selection
681681 rowData = row[1:] # Get the ID iout
682682 resultMatrix.appendRow(rowData, row[0])
683683 else:
742742 """Convenience function to calculate the mean of the rows of a DataMatrix
743743 instance."""
744744
745 return matrixApply(sourceMatrix, _mean,
745 return matrixApply(sourceMatrix, _mean,
746746 resultName="Mean value", what="rows")
747747
748748def transpose(matrix, identifier="x"):
757757 row_index = index + 1
758758 column = matrix.getRow(row_index, row_name=False)
759759 destination.append(column, matrix.rownames[index])
760
760
761761 return destination
762762
763763def subset(matrix, column_list):
  
3939 header=False,
4040 delimiter=" ",
4141 skip=1)
42 self.rowNameMatrix = datamatrix.DataMatrix(matrixFile, header=True,
43 delimiter=" ", row_names=2)
4244
4345 def testBasics(self):
4446
209209 "Test joining two matrices by rows"
210210
211211 matrixFile = StringIO('Name surname\nIsaac Asimov\nJohn Watson')
212 matrix = datamatrix.DataMatrix(matrixFile, header=True,
212 matrix = datamatrix.DataMatrix(matrixFile, header=True,
213213 delimiter=" ")
214214 new_matrix = datamatrix.rbind(self.matrix, matrix)
215215 self.assertEqual(new_matrix.getRow(3), ["3","Isaac", "Asimov"])
223223 matrixFile = StringIO(
224224 'Job Skills\nscientist relativity\ncomedian humor'
225225 )
226 matrix = datamatrix.DataMatrix(matrixFile, header=True,
226 matrix = datamatrix.DataMatrix(matrixFile, header=True,
227227 delimiter=" ")
228228 new_matrix = datamatrix.cbind(self.matrix, matrix)
229229 self.assertEqual(new_matrix["Job"], ["scientist", "comedian"])
230 self.assertEqual(new_matrix.getRow(2),
230 self.assertEqual(new_matrix.getRow(2),
231231 ["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
232257
233258
234259def runTests():