def testSupportedPostfixes(): names = "mz mzmin mzmax mz0 mzmin0 mzmax0 mz1 mzmax1 mzmin__0 mzmax__0 mz__0 "\ "mzmax3 mz4 mzmin4".split() t = Table._create(names, [float] * len(names), []) assert len(t.supportedPostfixes(["mz"])) == len(names) assert t.supportedPostfixes(["mz", "mzmin"]) == ["", "0", "4", "__0"] assert t.supportedPostfixes(["mz", "mzmin", "mzmax"]) == ["", "0", "__0"]
def test_removePostfixes(): t = Table._create(["abb__0", "bcb__0"], [str] * 2, ["%s"] * 2) assert t.getColNames() == ["abb__0", "bcb__0"] t.removePostfixes() assert t.getColNames() == ["abb", "bcb"] t.removePostfixes("bb", "cb") assert t.getColNames() == ["a", "b"] try: t.print_() t.removePostfixes("a", "b") t.print_() except: pass else: assert False, "expected exception"
def loadCSV(path=None, sep=";", keepNone = False, **specialFormats): # local import in order to keep namespaces clean import ms import csv, os.path, sys, re from libms.DataStructures.Table import (Table, common_type_for,\ bestConvert, guessFormatFor) if isinstance(path, unicode): path = path.encode(sys.getfilesystemencoding()) elif path is None: path = ms.askForSingleFile(extensions=["csv"]) if path is None: return None with open(path,"r") as fp: # remove clutter at right margin reader = csv.reader(fp, delimiter=sep) # reduce multiple spaces to single underscore colNames = [ re.sub(" +", "_", n.strip()) for n in reader.next()] if keepNone: conv = bestConvert else: conv = lambda v: None if v=="None" else bestConvert(v) rows = [ [conv(c.strip()) for c in row] for row in reader] columns = [[row[i] for row in rows] for i in range(len(colNames))] types = [common_type_for(col) for col in columns] #defaultFormats = {float: "%.2f", str: "%s", int: "%d"} formats = dict([(name, guessFormatFor(name,type_)) for (name, type_)\ in zip(colNames, types)]) formats.update(specialFormats) formats = [formats[n] for n in colNames] title = os.path.basename(path) meta = dict(loaded_from=os.path.abspath(path)) return Table._create(colNames, types, formats, rows, title, meta)
def _build_starttable(tables, force_merge): colname_orders = [] for table in tables: colname_orders.append(table._colNames) colum_names = _topo_sort_with_in_order(colname_orders) if colum_names is None: raise Exception("could not combine all column names to a "\ "consistent order. you have to provide a reference table") types = dict() for table in tables: for name in table._colNames: type_ = table.getType(name) if types.get(name, type_) != type_: if not force_merge: raise Exception("type conflictfor column %s" % name) print "type conflict:",name, types.get(name, type_), type_ types[name] = type_ formats = dict() for table in tables: for name in table._colNames: format_ = table.getFormat(name) if formats.get(name, format_) != format_: if not force_merge: raise Exception("format conflict for column %s" % name) print "format conflict:", name, formats.get(name, format_), format_ formats[name] = format_ final_types = [types.get(n) for n in colum_names] final_formats = [formats.get(n) for n in colum_names] prototype = Table._create(colum_names, final_types, final_formats) return prototype, colum_names