def test_sort_dict_by_value(self): # test normal dictionary sorted_dict1 = lcommon.sort_dict_by_value(self.dict1) dict1 = [('d', 10), ('a', 5), ('c', 2), ('b', -1)] self.assertEqual(sorted_dict1, dict1) sorted_dict2 = lcommon.sort_dict_by_value(self.dict2) dict2 = [('sam3', self.sam3), ('sam1', self.sam1), ('sam2', self.sam2)] self.assertEqual(sorted_dict2, dict2) sorted_dict2 = lcommon.sort_dict_by_value(self.dict2, myfunc) dict2 = [('sam2', self.sam2), ('sam1', self.sam1), ('sam3', self.sam3)] self.assertEqual(sorted_dict2, dict2)
def repsize_table(name2sample, outfile, group2avr={}, group2names={}, tex=False): # Sort the samples by group and/or size if len(group2avr) > 0: avrnames = [gavr[0] for gavr in group2avr.values()] sortedsamples = libcommon.sort_objs_by_group(name2sample, group2names, True, group2avr) else: avrnames = [] sortedsamples = libcommon.sort_dict_by_value(name2sample, keyfunc=lambda item: item[0].size) #sortedsamples = sorted(name2sample.values(), key=lambda s: s.size, # reverse=True) # Write to output file if tex: repsize_latex(sortedsamples, outfile, avrnames) else: repsize_text(sortedsamples, outfile)
def table(name2obj, outfile, colfields, group2avr={}, group2names={}, tex=False, keyattr='numclone', caption='', label='', islist=False): # rows=samples; cols=colfields; if not name2obj or not colfields: return # Sort the samples by group and/or size keyfunc = None listattr = None if islist or keyattr not in colfields: listattr = keyattr else: keyfunc = lambda item: item[0][keyattr] if group2avr: sortedobjs = libcommon.sort_objs_by_group(name2obj, group2names, True, group2avr, keyfunc=keyfunc) else: sortedobjs = libcommon.sort_dict_by_value(name2obj, keyfunc=keyfunc) if tex: table_latex(sortedobjs, outfile, colfields, caption, label, listattr) else: table_text(sortedobjs, outfile, colfields, listattr)