-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_pandas.py
More file actions
76 lines (53 loc) · 2.38 KB
/
test_pandas.py
File metadata and controls
76 lines (53 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import numpy as np
import numpy.testing as npt
import pandas as pd
import unittest
from scyjava import config, jimport, to_java
config.endpoints.append('org.scijava:scijava-table')
config.add_option('-Djava.awt.headless=true')
def assert_same_table(table, df):
assert len(table.toArray()) == df.shape[1]
assert len(table.toArray()[0].toArray()) == df.shape[0]
for i, column in enumerate(table.toArray()):
npt.assert_array_almost_equal(df.iloc[:, i].values, column.toArray())
assert table.getColumnHeader(i) == df.columns[i]
class TestPandas(unittest.TestCase):
def testPandasToTable(self):
# Float table.
columns = ["header1", "header2", "header3", "header4", "header5"]
array = np.random.random(size=(7, 5))
df = pd.DataFrame(array, columns=columns)
table = to_java(df)
assert_same_table(table, df)
assert type(table) == jimport('org.scijava.table.DefaultFloatTable')
# Int table.
columns = ["header1", "header2", "header3", "header4", "header5"]
array = np.random.random(size=(7, 5)) * 100
array = array.astype('int')
df = pd.DataFrame(array, columns=columns)
table = to_java(df)
assert_same_table(table, df)
assert type(table) == jimport('org.scijava.table.DefaultIntTable')
# Bool table.
columns = ["header1", "header2", "header3", "header4", "header5"]
array = np.random.random(size=(7, 5)) > 0.5
df = pd.DataFrame(array, columns=columns)
table = to_java(df)
assert_same_table(table, df)
assert type(table) == jimport('org.scijava.table.DefaultBoolTable')
# Mixed table.
columns = ["header1", "header2", "header3", "header4", "header5"]
array = np.random.random(size=(7, 5))
df = pd.DataFrame(array, columns=columns)
# Convert column 0 to integer
df.iloc[:, 0] = (df.iloc[:, 0] * 100).astype('int')
# Convert column 1 to bool
df.iloc[:, 1] = df.iloc[:, 1] > 0.5
# Convert column 2 to string
df.iloc[:, 2] = df.iloc[:, 2].to_string(index=False).split('\n')
table = to_java(df)
# Table types cannot be the same here, unless we want to cast.
# assert_same_table(table, df)
assert type(table) == jimport('org.scijava.table.DefaultGenericTable')
if __name__ == '__main__':
unittest.main()