예제 #1
0
def test_2(path, tmpdir, regtest):

    # same folder as table for peakmaps:

    from emzed.core.data_types import Table

    pm = emzed.io.loadPeakMap(path("data/SHORT_MS2_FILE.mzData"))
    t = emzed.utils.toTable("id", (1, 2, 3), type_=int)
    t.addColumn("peakmap", pm, type_=object)

    p2 = tmpdir.join("with_comp.table").strpath

    t.store(p2, True, True, ".")

    # chekc if pm proxy is next ot
    file_names = [p.basename for p in tmpdir.listdir()]
    print(file_names, file=regtest)

    # force loading of data:.
    print(len(t.peakmap.values[0]), file=regtest)
    s1 = os.stat(p2).st_size
    assert s1 < 1000
    t = Table.load(p2)
    print(len(t.peakmap.values[0]), file=regtest)

    subfolder = tmpdir.join("subfolder")
    os.makedirs(subfolder.strpath)
    for p in file_names:
        shutil.move(tmpdir.join(p).strpath, subfolder.strpath)

    t2 = Table.load(subfolder.join("with_comp.table").strpath)
    print(t2, file=regtest)
    print(len(t2.peakmap.values[0]), file=regtest)
    print(t2, file=regtest)
예제 #2
0
def test_3(path, tmpdir, regtest):

    # relative path for peakmaps:

    from emzed.core.data_types import Table

    pm = emzed.io.loadPeakMap(path("data/SHORT_MS2_FILE.mzData"))
    t = emzed.utils.toTable("id", (1, 2, 3), type_=int)
    t.addColumn("peakmap", pm, type_=object)

    folder = tmpdir.join("subfolder")
    folder.mkdir()

    p2 = folder.join("with_comp.table").strpath
    t.store(p2, True, True, "..")

    for p in tmpdir.listdir():
        # full path varies because the folder is a tmp dir:
        print(p.basename, file=regtest)

    t2 = Table.load(p2)
    # this triggers loading:
    t2.peakmap[0].chromatogram(0, 10, 0, 10)

    t2.peakmap[0]._path == p.strpath

    # now we move and check if proxy still works:
    moved = folder.join("..").join("moved")
    shutil.move(folder.strpath, moved.strpath)

    t2 = Table.load(moved.join("with_comp.table").strpath)
    t2.peakmap[0].chromatogram(0, 10, 0, 10)
예제 #3
0
    def testSplitBy(self):
        t = toTable("a", [1, 1, 3, 4])
        t.addColumn("b", [1, 1, 3, 3])
        t.addColumn("c", [1, 2, 1, 4])
        t._print()
        subts = t.splitBy("a")
        assert len(subts) == 3
        res = Table.mergeTables(subts)
        assert len(res) == len(t)
        subts[0]._print()
        assert res.a.values == t.a.values
        assert res.b.values == t.b.values
        assert res.c.values == t.c.values

        # check if input tables are not altered
        for subt in subts:
            assert subt.getColNames() == ["a", "b", "c"]

        subts = t.splitBy("a", "c")
        assert len(subts) == 4
        res = Table.mergeTables(subts)
        assert res.a.values == t.a.values
        assert res.b.values == t.b.values
        assert res.c.values == t.c.values

        # check if input tables are not altered
        for subt in subts:
            assert subt.getColNames() == ["a", "b", "c"]
예제 #4
0
파일: test_table.py 프로젝트: lowks/emzed2
    def testSplitBy(self):
        t = toTable("a", [1,1,3,4])
        t.addColumn("b", [1,1,3,3])
        t.addColumn("c", [1,2,1,4])
        t._print()
        subts = t.splitBy("a")
        assert len(subts) == 3
        res = Table.mergeTables(subts)
        assert len(res) == len(t)
        subts[0]._print()
        assert res.a.values == t.a.values
        assert res.b.values == t.b.values
        assert res.c.values == t.c.values

        # check if input tables are not altered
        for subt in subts:
            assert subt.getColNames() == [ "a", "b", "c"]

        subts = t.splitBy("a", "c")
        assert len(subts) == 4
        res = Table.mergeTables(subts)
        assert res.a.values == t.a.values
        assert res.b.values == t.b.values
        assert res.c.values == t.c.values

        # check if input tables are not altered
        for subt in subts:
            assert subt.getColNames() == [ "a", "b", "c"]
예제 #5
0
파일: test_table.py 프로젝트: lowks/emzed2
    def testSomePredicates(self):
        #build table
        names="int long float str object array".split()
        types = [int, long, float, str, object, np.ndarray,]
        formats = [ "%3d", "%d", "%.3f", "%s", "%r", "'array%r' % (o.shape,)" ]

        row1 = [ 1, 12323L, 1.0, "hi", { 1: 1 },  np.array((1,2,3)) ]
        row2 = [ 2, 22323L, 2.0, "hi2", [2,3,], np.array(((2,3,4),(1,2,3))) ]
        row3 = [ 3, 32323L, 3.0, "hi3", (3,) , np.array(((3,3,4,5),(1,2,3,4))) ]

        rows = [row1, row2, row3]
        t=Table(names, types, formats, rows, "testtabelle", meta=dict(why=42))

        expr = (t.int + t.float).inRange(-1, 2)
        tn = t.filter(expr)
        assert len(tn) == 1
        assert tn.getValue(tn.rows[0], "int") == 1
        tn = t.filter((t.float+t.int).inRange(-1, 2))
        assert len(tn) == 1
        assert tn.getValue(tn.rows[0], "int") == 1

        tn = t.filter(t.float.approxEqual(1.0, t.int/10))
        tn._print()
        assert len(tn) == 1, len(tn)
        assert tn.getValue(tn.rows[0], "int") == 1
예제 #6
0
    def testDynamicColumnAttributes(self):
        t = Table(["a", "b", "c"], [str, int, int], ["%s", "%d", "%d"], [])
        t.a
        t.b
        t.c
        assert len(t.a.values) == 0
        assert len(t.b.values) == 0
        assert len(t.c.values) == 0

        t.renameColumns(dict(a="aa"))
        assert "a" not in t.getColNames()
        assert "aa" in t.getColNames()
        t.aa
        try:
            t.a
            raise Exception("t.a should be deteted")
        except:
            pass

        col = pickle.loads(pickle.dumps(t.aa))
        assert len(col.values) == 0

        t.dropColumns("aa")
        assert "aa" not in t.getColNames()
        try:
            t.aa
            raise Exception("t.aa should be deteted")
        except:
            pass
예제 #7
0
파일: test_table2.py 프로젝트: lowks/emzed2
def testToOpenMSFeatureMap():
    t = Table("mz rt".split(), [float, float], 2 * ["%.6f"])
    fm = t.toOpenMSFeatureMap()
    assert fm.size() == 0

    t.addRow([1.0, 2.0])
    fm = t.toOpenMSFeatureMap()
    assert fm.size() == 1

    f = fm[0]
    assert f.getMZ() == 1.0  # == ok, as no digits after decimal point
    assert f.getRT() == 2.0  # dito
예제 #8
0
파일: test_table2.py 프로젝트: gmat/emzed2
def test_removePostfixes(regtest):
    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"]

    t = Table._create(["abb__0", "bcb__0", "abb"], [str] * 3, ["%s"] * 3)
    with pytest.raises(Exception) as e:
        t.removePostfixes()

    print(e.value, file=regtest)
예제 #9
0
파일: test_table2.py 프로젝트: kanzy/emzed2
def test_removePostfixes(regtest):
    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"]

    t = Table._create(["abb__0", "bcb__0", "abb"], [str] * 3, ["%s"] * 3)
    with pytest.raises(Exception) as e:
        t.removePostfixes()

    print(e.value, file=regtest)
예제 #10
0
파일: test_table.py 프로젝트: lowks/emzed2
    def testDynamicColumnAttributes(self):
        t = Table(["a", "b", "c"], [str, int, int], ["%s", "%d", "%d"],[])
        t.a
        t.b
        t.c
        assert len(t.a.values) == 0
        assert len(t.b.values) == 0
        assert len(t.c.values) == 0

        t.renameColumns(dict(a="aa"))
        assert "a" not in t.getColNames()
        assert "aa"  in t.getColNames()
        t.aa
        try:
            t.a
            raise Exception("t.a should be deteted")
        except:
            pass

        col = pickle.loads(pickle.dumps(t.aa))
        assert len(col.values) == 0

        t.dropColumns("aa")
        assert "aa" not in t.getColNames()
        try:
            t.aa
            raise Exception("t.aa should be deteted")
        except:
            pass
예제 #11
0
def setupTable():
    names = "int long float str object array".split()
    types = [
        int,
        long,
        float,
        str,
        object,
        np.ndarray,
    ]
    formats = ["%3d", "%d", "%.3f", "%s", "%r", "'array(%r)' % o.shape"]

    row1 = [1, 12323L, 1.0, "hi", {1: 1}, np.array((1, 2, 3))]
    row2 = [2, 22323L, 2.0, "hi2", [
        2,
        3,
    ], np.array(((2, 3, 4), (1, 2, 3)))]
    row3 = [
        3, 32323L, 3.0, "hi3", (3, ),
        np.array(((3, 3, 4, 5), (1, 2, 3, 4)))
    ]

    rows = [row1, row2, row3]
    t = Table(names, types, formats, rows, "testtabelle", meta=dict(why=42))
    t = t.extractColumns("int", "float", "str")
    t.addEnumeration()
    t._name = "t"
    t._print()
    return t
예제 #12
0
 def testDoubleColumnames(self):
     ex = None
     try:
         colnames = ["col0", "col0", "col1", "col1", "col2"]
         Table(colnames, [int] * 5, [""] * 5)
     except Exception, e:
         ex = e.message
예제 #13
0
    def testMerge(self):
        t1 = toTable("a", [1])
        t1.addColumn("b", [2])
        t1.addColumn("c", [3])

        t2 = toTable("a", [1, 2])
        t2.addColumn("c", [1, 3])
        t2.addColumn("d", [1, 4])

        tn = Table.mergeTables([t1, t2])

        assert tn.a.values == (
            1,
            1,
            2,
        )
        assert tn.b.values == (
            2,
            None,
            None,
        )
        assert tn.c.values == (
            3,
            1,
            3,
        )
        assert tn.d.values == (
            None,
            1,
            4,
        )

        # check if input tables are not altered
        assert t1.getColNames() == ["a", "b", "c"]
        assert t2.getColNames() == ["a", "c", "d"]
예제 #14
0
    def testSomePredicates(self):
        #build table
        names = "int long float str object array".split()
        types = [
            int,
            long,
            float,
            str,
            object,
            np.ndarray,
        ]
        formats = ["%3d", "%d", "%.3f", "%s", "%r", "'array%r' % (o.shape,)"]

        row1 = [1, 12323L, 1.0, "hi", {1: 1}, np.array((1, 2, 3))]
        row2 = [
            2, 22323L, 2.0, "hi2", [
                2,
                3,
            ],
            np.array(((2, 3, 4), (1, 2, 3)))
        ]
        row3 = [
            3, 32323L, 3.0, "hi3", (3, ),
            np.array(((3, 3, 4, 5), (1, 2, 3, 4)))
        ]

        rows = [row1, row2, row3]
        t = Table(names,
                  types,
                  formats,
                  rows,
                  "testtabelle",
                  meta=dict(why=42))

        expr = (t.int + t.float).inRange(-1, 2)
        tn = t.filter(expr)
        assert len(tn) == 1
        assert tn.getValue(tn.rows[0], "int") == 1
        tn = t.filter((t.float + t.int).inRange(-1, 2))
        assert len(tn) == 1
        assert tn.getValue(tn.rows[0], "int") == 1

        tn = t.filter(t.float.approxEqual(1.0, t.int / 10))
        tn._print()
        assert len(tn) == 1, len(tn)
        assert tn.getValue(tn.rows[0], "int") == 1
예제 #15
0
파일: test_table2.py 프로젝트: kanzy/emzed2
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), [None] * 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"]
예제 #16
0
파일: test_table2.py 프로젝트: lowks/emzed2
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"]
예제 #17
0
def test_pandas(regtest):
    import pandas
    from emzed.core.data_types import Table
    data = dict(a=[1, 2, 3], b=[1.0, 2.0, None], c=["a", "b", "c"], d=[1.0, 2.0, 3.0],
                e=[None, None, None])
    df = pandas.DataFrame(data, columns=sorted(data.keys()))
    t = Table.from_pandas(df, formats={"b": "%.2f", float: "%.1f", object: "%s", None: "%s"})

    t.print_()
    t.print_(out=regtest)
예제 #18
0
파일: test_table2.py 프로젝트: lowks/emzed2
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"
예제 #19
0
파일: test_table2.py 프로젝트: kanzy/emzed2
def testToOpenMSFeatureMap():
    t = Table("mz rt".split(), [float, float], 2 * ["%.6f"])
    fm = t.toOpenMSFeatureMap()
    assert fm.size() == 0

    t.addRow([1.0, 2.0])
    fm = t.toOpenMSFeatureMap()
    assert fm.size() == 1

    f = fm[0]
    assert f.getMZ() == 1.0  # == ok, as no digits after decimal point
    assert f.getRT() == 2.0  # dito
예제 #20
0
def setupTable():
    names="int long float str object array".split()
    types = [int, long, float, str, object, np.ndarray,]
    formats = [ "%3d", "%d", "%.3f", "%s", "%r", "'array(%r)' % o.shape" ]

    row1 = [ 1, 12323L, 1.0, "hi", { 1: 1 },  np.array((1,2,3)) ]
    row2 = [ 2, 22323L, 2.0, "hi2", [2,3,], np.array(((2,3,4),(1,2,3))) ]
    row3 = [ 3, 32323L, 3.0, "hi3", (3,) , np.array(((3,3,4,5),(1,2,3,4))) ]

    rows = [row1, row2, row3]
    t=Table(names, types, formats, rows, "testtabelle", meta=dict(why=42))
    t = t.extractColumns("int", "float", "str")
    t.addEnumeration()
    t._name = "t"
    t._print()
    return t
예제 #21
0
def testMixedRows():
    names = """rt rtmin rtmax rt__1 rtmin__1 rtmax__1
               mz mzmin mzmax mz__1 mzmin__1 mzmax__1
               rt__0 rtmin__0 rtmax__0
               mz__0 mzmin__0 mzmax__0
               rt__2 rtmin__2""".split()

    names = [n.strip() for n in names]

    tab = Table._create(names, [float]*len(names), ["%f"] * len(names))

    model = TableModel(tab, None)

    #assert model.postfixes == [ "", "__0", "__1", "__2"], model.postfixes

    assert model.table.supportedPostfixes(["rtmin", "rt"]) == ["", "__0", "__1", "__2"]
    assert model.table.supportedPostfixes(["mz", "rt", "mzmin"]) == ["", "__0", "__1"]
    assert model.table.supportedPostfixes(["mz", "rt", "x"]) == []
예제 #22
0
파일: load_utils.py 프로젝트: kanzy/emzed2
def loadExcel(path=None, sheetname=0, types=None, formats=None):
    """`sheetname` is either an intger or string for indicating the sheet which will be extracted
    from the .xls or .xlsx file. The index 0 refers to the first sheet.

    `types` is either None or a dictionary mapping column names to their types.

    `formats` is either None or a dictionary mapping column names to formats.
    """
    path = _prepare_path(path, extensions=["xls", "xlsx"])
    if path is None:
        return None

    from emzed.core.data_types import Table
    import pandas

    # sheetname is reuqired for pandas < 0.14.0, later versions have default 0
    df = pandas.read_excel(path, sheetname=sheetname)
    return Table.from_pandas(df, types=types, formats=formats)
예제 #23
0
def test_pandas(regtest):
    import pandas
    from emzed.core.data_types import Table
    data = dict(a=[1, 2, 3],
                b=[1.0, 2.0, None],
                c=["a", "b", "c"],
                d=[1.0, 2.0, 3.0],
                e=[None, None, None])
    df = pandas.DataFrame(data, columns=sorted(data.keys()))
    t = Table.from_pandas(df,
                          formats={
                              "b": "%.2f",
                              float: "%.1f",
                              object: "%s",
                              None: "%s"
                          })

    t.print_()
    t.print_(out=regtest)
예제 #24
0
파일: test_table.py 프로젝트: lowks/emzed2
    def testMerge(self):
        t1 = toTable("a", [1])
        t1.addColumn("b", [2])
        t1.addColumn("c", [3])

        t2 = toTable("a", [1,2])
        t2.addColumn("c", [1,3])
        t2.addColumn("d", [1,4])

        tn = Table.mergeTables([t1, t2])

        assert tn.a.values == (1, 1, 2,)
        assert tn.b.values == (2, None, None,)
        assert tn.c.values == (3, 1, 3,)
        assert tn.d.values == (None, 1, 4,)

        # check if input tables are not altered
        assert t1.getColNames() == [ "a", "b", "c"]
        assert t2.getColNames() == [ "a", "c", "d"]
예제 #25
0
def test_pandas():
    import pandas
    import cStringIO
    from emzed.core.data_types import Table
    data = dict(a=[1, 2, 3], b=[1.0, 2.0, None], c=["a", "b", "c"], d=[1.0, 2.0, 3.0],
                e=[None, None, None])
    df = pandas.DataFrame(data, columns=sorted(data.keys()))
    t = Table.from_pandas(df, formats={"b": "%.2f", float: "%.1f", object: "%s", None: "%s"})
    out = cStringIO.StringIO()

    t.print_()
    t.print_(out=out)
    lines = out.getvalue().split("\n")

    assert len(lines) == 7
    assert lines[0].strip() == "a        b        c        d        e"
    assert lines[1].strip() == "int      float    str      float    None"
    assert lines[2].strip() == "------   ------   ------   ------   ------"
    assert lines[3].strip() == "1        1.00     a        1.0      -"
    assert lines[4].strip() == "2        2.00     b        2.0      -"
    assert lines[5].strip() == "3        -        c        3.0      -"
예제 #26
0
파일: test_table2.py 프로젝트: kanzy/emzed2
def testIllegalRows():
    with pytest.raises(Exception):
        Table(["a", "b"], [float, float], ["%f", "%f"], [(1, 2)])
예제 #27
0
파일: test_table.py 프로젝트: lowks/emzed2
    def testRunnerTable(self):


        def run(t, colnames, rows):
            t = t.copy()
            ixs = set()
            for i, name in enumerate(colnames):
                ixs.add(t.getIndex(name))
            assert not None in ixs
            assert len(ixs) == len(colnames)

            # test iteration
            content = []
            for row in t.rows:
                for (cell, formatter) in zip(row, t.colFormatters):
                    content.append( formatter(cell))
                break

            # test formatting
            assert content[0] == "  1"
            assert content[1] == "12323"
            assert content[2] == "1.000"
            assert content[3] == "hi"
            assert content[4] == repr({1:1})
            assert content[5] in ("array((3,))", "array((3L,))"), content[5]

            assert set(t.getVisibleCols()) == { 'int', 'long', 'float', 'str',
                                                'object', 'array' }


            expr = t.str.contains("hi")
            tn = t.filter(expr)
            assert len(tn) == 3
            tn = t.filter(~ t.str.contains("hi"))
            assert len(tn) == 0

            tn = t.filter(t.str.contains("2"))
            assert len(tn) == 1



            # test requireColumn
            for name in colnames:
                t.requireColumn(name)

            ex = None
            try:
                t.requireColumn("asdfkl?dsflkjaldfkja?sdlfjal?djf")
            except Exception, e:
                ex = e
            assert ex is not None

            # test other fields
            assert t.meta["why"] == 42
            assert t.title == "testtabelle"

            t.sortBy("int", ascending=False)

            # restrct cols
            tn = t.extractColumns("int", "long")
            assert len(tn.getColNames()) == 2, len(t.getColNames())
            assert len(tn.getColTypes()) == 2
            assert len(tn.getColFormats()) == 2

            assert len(tn) == len(t)

            assert tn.meta["why"] == 42
            assert tn.title == "testtabelle"

            tn.addEnumeration()
            assert set(tn.getVisibleCols()) == { 'int', 'long', 'id' }
            assert tn.getColNames()[0]=="id"
            assert list(tn.id) == range(len(t))

            tn.renameColumns(int='iii')
            assert set(tn.getVisibleCols()) == { 'iii', 'long', 'id' }

            tn.addColumn('x', 'hi', str, '%s')
            assert set(tn.getVisibleCols()) == { 'iii', 'long', 'id', 'x' }
            assert tn.getColNames()[-1]=="x"

            assert list(tn.x) == ["hi"]*len(tn)

            import os.path

            def j(name):
                return os.path.join(self.temp_output, name)

            before = set(os.listdir(self.temp_output))
            tn.storeCSV(j("x.csv"))

            tnre  = Table.loadCSV(j("x.csv"))
            assert len(tnre) == len(tn)
            assert tnre.getColNames() == tn.getColNames()
            assert tnre.id.values == tn.id.values
            assert tnre.iii.values == tn.iii.values
            assert tnre.long.values == tn.long.values
            assert tnre.x.values == tn.x.values



            tn.storeCSV(j("x.csv"), onlyVisibleColumns=False)
            after = set(os.listdir(self.temp_output))
            # file written twice !
            assert len(after-before) == 2
            for n in after-before:
                # maybe we have some x.csv.? from previous run of this
                #function so we can not assume that we find x.csv and
                #x.csv.1
                assert re.match("x.csv(.\d+)?", n)

            ex = None
            with self.assertRaises(Exception):
                # wrong file extension
                tn.storeCSV(j("x.dat"))

            # computed by exrpression
            tn.print_()
            tn.addColumn("computed", tn.long / (tn.iii + 1))
            # computed by callback:
            tn.addColumn("squared", lambda t,r,n: t.getValue(r, "iii")**2)


            assert tn.getColumn("computed").values == (8080, 7441, 6161), tn.computed.values
            assert tn.getColumn("squared").values == (9, 4, 1)


            tn.replaceColumn("squared", tn.squared+1)
            assert tn.getColumn("squared").values == (10, 5, 2)
            assert len(tn.getColNames())  == 6

            tx  = tn.copy()
            tx.dropColumns("squared", "computed")
            assert tx.getColNames() == ["id", "iii", "long", "x"]
            assert len(tx) == 3

            tn.dropColumns("computed", "squared")
            assert tn.getColNames() == ["id", "iii", "long", "x"]
            assert len(tn) == 3

            tn.dropColumns("id", "x")
            t2 = tn.copy()
            res = tn.leftJoin(t2, tn.iii == tn.long)
            assert len(res) == len(t2)
            res = tn.leftJoin(t2, tn.iii == tn.iii)
            assert len(res) == len(t2)**2
            res = tn.leftJoin(t2, (tn.iii == tn.iii) & (t2.long==32323))
            assert len(res) == len(t2)

            res = tn.join(t2, tn.iii == tn.long)
            assert len(res) == 0
            res = tn.join(t2, tn.long == tn.iii)
            assert len(res) == 0
            res = tn.join(t2, tn.iii == tn.iii)
            assert len(res) == len(t2)**2, len(res)
            res = tn.join(t2, (tn.iii == tn.iii) & (t2.long==32323))
            assert len(res) == len(t2), len(res)

            tx = tn.filter(tn.iii.isIn([1,4]))
            tx._print()
            assert len(tx) == 1
            assert tx.iii.values == (1,)

            tn.addColumn("li", [1,2,3])
            assert len(tn) == 3
            assert len(tn.getColNames()) == 3
            assert "li" in tn.getColNames()

            tn.addRow([1, 1, 1])
            assert len(tn) == 4

            ex = None
            try:
                tn.addRow([1,2,3,2])
            except Exception, e:
                ex = e
예제 #28
0
파일: test_table.py 프로젝트: lowks/emzed2
            assert ex is not None

        with self.assertRaises(Exception):
            Table(["a"],[np.float32],["%f"],[[32.0]])

        #build table
        names="int long float str object array".split()
        types = [int, long, float, str, object, np.ndarray,]
        formats = [ "%3d", "%d", "%.3f", "%s", "%r", "'array(%r)' % (o.shape,)" ]

        row1 = [ 1, 12323L, 1.0, "hi", { 1: 1 },  np.array((1,2,3)) ]
        row2 = [ 2, 22323L, 2.0, "hi2", [2,3,], np.array(((2,3,4),(1,2,3))) ]
        row3 = [ 3, 32323L, 3.0, "hi3", (3,) , np.array(((3,3,4,5),(1,2,3,4))) ]

        rows = [row1, row2, row3]
        t=Table(names, types, formats, rows, "testtabelle", meta=dict(why=42))


        run(t, names, [row1, row2, row3])
        # test pickle
        dat = pickle.dumps(t)
        t = pickle.loads(dat)
        run(t, names, [row1, row2, row3])

        def j(name):
            import os.path
            return os.path.join(self.temp_output, name)

        t.store(j("test.table"))
        try:
            Table.storeTable(t, "temp_output/test.table")
예제 #29
0
파일: test_table.py 프로젝트: lowks/emzed2
 def testIfThenElse(self):
     t = Table(["a", "b", "c"], [str, int, int], ["%s", "%d", "%d"],[])
     t.rows.append(["0", 1, 2])
     t.rows.append([None, 2, 1])
     t._print()
     t.addColumn("x", (t.a.isNotNone()).thenElse(t.b, t.c))
     assert t.getColNames()==["a", "b", "c", "x"]
     print
     t._print()
     t.addColumn("y", (t.a.isNotNone()).thenElse("ok", "not ok"))
     t._print()
     assert t.y.values == ("ok", "not ok")
예제 #30
0
            ex = None
            try:
                tn.addRow([1, 2, 3, 2])
            except Exception, e:
                ex = e
            assert ex is not None

            ex = None
            try:
                tn.addRow(["a", 1, 2])
            except Exception, e:
                ex = e
            assert ex is not None

        with self.assertRaises(Exception):
            Table(["a"], [np.float32], ["%f"], [[32.0]])

        #build table
        names = "int long float str object array".split()
        types = [
            int,
            long,
            float,
            str,
            object,
            np.ndarray,
        ]
        formats = ["%3d", "%d", "%.3f", "%s", "%r", "'array(%r)' % (o.shape,)"]

        row1 = [1, 12323L, 1.0, "hi", {1: 1}, np.array((1, 2, 3))]
        row2 = [
예제 #31
0
    def testRunnerTable(self):
        def run(t, colnames, rows):
            t = t.copy()
            ixs = set()
            for i, name in enumerate(colnames):
                ixs.add(t.getIndex(name))
            assert not None in ixs
            assert len(ixs) == len(colnames)

            # test iteration
            content = []
            for row in t.rows:
                for (cell, formatter) in zip(row, t.colFormatters):
                    content.append(formatter(cell))
                break

            # test formatting
            assert content[0] == "  1"
            assert content[1] == "12323"
            assert content[2] == "1.000"
            assert content[3] == "hi"
            assert content[4] == repr({1: 1})
            assert content[5] in ("array((3,))", "array((3L,))"), content[5]

            assert set(t.getVisibleCols()) == {
                'int', 'long', 'float', 'str', 'object', 'array'
            }

            expr = t.str.contains("hi")
            tn = t.filter(expr)
            assert len(tn) == 3
            tn = t.filter(~t.str.contains("hi"))
            assert len(tn) == 0

            tn = t.filter(t.str.contains("2"))
            assert len(tn) == 1

            # test requireColumn
            for name in colnames:
                t.requireColumn(name)

            ex = None
            try:
                t.requireColumn("asdfkl?dsflkjaldfkja?sdlfjal?djf")
            except Exception, e:
                ex = e
            assert ex is not None

            # test other fields
            assert t.meta["why"] == 42
            assert t.title == "testtabelle"

            t.sortBy("int", ascending=False)

            # restrct cols
            tn = t.extractColumns("int", "long")
            assert len(tn.getColNames()) == 2, len(t.getColNames())
            assert len(tn.getColTypes()) == 2
            assert len(tn.getColFormats()) == 2

            assert len(tn) == len(t)

            assert tn.meta["why"] == 42
            assert tn.title == "testtabelle"

            tn.addEnumeration()
            assert set(tn.getVisibleCols()) == {'int', 'long', 'id'}
            assert tn.getColNames()[0] == "id"
            assert list(tn.id) == range(len(t))

            tn.renameColumns(int='iii')
            assert set(tn.getVisibleCols()) == {'iii', 'long', 'id'}

            tn.addColumn('x', 'hi', str, '%s')
            assert set(tn.getVisibleCols()) == {'iii', 'long', 'id', 'x'}
            assert tn.getColNames()[-1] == "x"

            assert list(tn.x) == ["hi"] * len(tn)

            import os.path

            def j(name):
                return os.path.join(self.temp_output, name)

            before = set(os.listdir(self.temp_output))
            tn.storeCSV(j("x.csv"))

            tnre = Table.loadCSV(j("x.csv"))
            assert len(tnre) == len(tn)
            assert tnre.getColNames() == tn.getColNames()
            assert tnre.id.values == tn.id.values
            assert tnre.iii.values == tn.iii.values
            assert tnre.long.values == tn.long.values
            assert tnre.x.values == tn.x.values

            with self.assertRaises(Exception):
                # wrong file extension
                tn.storeCSV(j("x.dat"))

            # computed by exrpression
            tn.print_()
            tn.addColumn("computed", tn.long / (tn.iii + 1))
            # computed by callback:
            tn.addColumn("squared", lambda t, r, n: t.getValue(r, "iii")**2)

            assert tn.getColumn("computed").values == (
                8080, 7441, 6161), tn.computed.values
            assert tn.getColumn("squared").values == (9, 4, 1)

            tn.replaceColumn("squared", tn.squared + 1)
            assert tn.getColumn("squared").values == (10, 5, 2)
            assert len(tn.getColNames()) == 6

            tx = tn.copy()
            tx.dropColumns("squared", "computed")
            assert tx.getColNames() == ["id", "iii", "long", "x"]
            assert len(tx) == 3

            tn.dropColumns("computed", "squared")
            assert tn.getColNames() == ["id", "iii", "long", "x"]
            assert len(tn) == 3

            tn.dropColumns("id", "x")
            t2 = tn.copy()
            res = tn.leftJoin(t2, tn.iii == tn.long)
            assert len(res) == len(t2)
            res = tn.leftJoin(t2, tn.iii == tn.iii)
            assert len(res) == len(t2)**2
            res = tn.leftJoin(t2, (tn.iii == tn.iii) & (t2.long == 32323))
            assert len(res) == len(t2)

            res = tn.join(t2, tn.iii == tn.long)
            assert len(res) == 0
            res = tn.join(t2, tn.long == tn.iii)
            assert len(res) == 0
            res = tn.join(t2, tn.iii == tn.iii)
            assert len(res) == len(t2)**2, len(res)
            res = tn.join(t2, (tn.iii == tn.iii) & (t2.long == 32323))
            assert len(res) == len(t2), len(res)

            tx = tn.filter(tn.iii.isIn([1, 4]))
            tx._print()
            assert len(tx) == 1
            assert tx.iii.values == (1, )

            tn.addColumn("li", [1, 2, 3])
            assert len(tn) == 3
            assert len(tn.getColNames()) == 3
            assert "li" in tn.getColNames()

            tn.addRow([1, 1, 1])
            assert len(tn) == 4

            ex = None
            try:
                tn.addRow([1, 2, 3, 2])
            except Exception, e:
                ex = e
예제 #32
0
 def testDetectionOfUnallowdColumnNames(self):
     ex = None
     try:
         Table(["__init__"], [int], ["%d"])
     except Exception, e:
         ex = e.message
예제 #33
0
 def testIfThenElse(self):
     t = Table(["a", "b", "c"], [str, int, int], ["%s", "%d", "%d"], [])
     t.rows.append(["0", 1, 2])
     t.rows.append([None, 2, 1])
     t._print()
     t.addColumn("x", (t.a.isNotNone()).thenElse(t.b, t.c))
     assert t.getColNames() == ["a", "b", "c", "x"]
     print
     t._print()
     t.addColumn("y", (t.a.isNotNone()).thenElse("ok", "not ok"))
     t._print()
     assert t.y.values == ("ok", "not ok")