-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_pandas.py
More file actions
160 lines (122 loc) · 5.29 KB
/
Copy pathtest_pandas.py
File metadata and controls
160 lines (122 loc) · 5.29 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""
Tests for functions in _pandas submodule.
"""
import numpy as np
import numpy.testing as npt
import pandas as pd
from scyjava import config, jarray, jimport, jinstance, to_java, to_python
config.endpoints.append("org.scijava:scijava-table")
config.enable_headless_mode()
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(object):
def testPandasToTable(self):
columns = ["header1", "header2", "header3", "header4", "header5"]
# Float table.
array = np.random.random(size=(7, 5))
df = pd.DataFrame(array, columns=columns)
table = to_java(df)
assert_same_table(table, df)
assert jinstance(table, "org.scijava.table.DefaultFloatTable")
# Int table.
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 jinstance(table, "org.scijava.table.DefaultIntTable")
# Bool table.
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 jinstance(table, "org.scijava.table.DefaultBoolTable")
# Mixed table.
array = np.random.random(size=(7, 5))
df = pd.DataFrame(array, columns=columns)
# Convert column 0 to integer
df[columns[0]] = (df[columns[0]] * 100).astype("int")
# Convert column 1 to bool
df[columns[1]] = df[columns[1]] > 0.5
# Convert column 2 to string
df[columns[2]] = df[columns[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 jinstance(table, "org.scijava.table.DefaultGenericTable")
def testTabletoPandas(self):
Boolean = jimport("java.lang.Boolean")
Double = jimport("java.lang.Double")
Float = jimport("java.lang.Float")
Integer = jimport("java.lang.Integer")
String = jimport("java.lang.String")
columns = jarray(String, [5])
for i in range(5):
columns[i] = f"header{i + 1}"
# Float table
table = jimport("org.scijava.table.DefaultFloatTable")()
table.appendColumns(columns)
table.setRowCount(7)
array = np.random.random(size=(7, 5))
table = self._fill_table(table, array, lambda v: Float(float(v)))
df = to_python(table)
assert_same_table(table, df)
for col in df.columns:
assert df.dtypes[col] == np.float64
# Int table
table = jimport("org.scijava.table.DefaultIntTable")()
table.appendColumns(columns)
table.setRowCount(7)
array = np.random.random(size=(7, 5)) * 100
array = array.astype("int32")
table = self._fill_table(table, array, lambda v: Integer(int(v)))
df = to_python(table)
assert_same_table(table, df)
for col in df.columns:
assert df.dtypes[col] == np.int64
# Bool table
table = jimport("org.scijava.table.DefaultBoolTable")()
table.appendColumns(columns)
table.setRowCount(7)
array = np.random.random(size=(7, 5)) > 0.5
table = self._fill_table(table, array, lambda v: Boolean(bool(v)))
df = to_python(table)
assert_same_table(table, df)
for col in df.columns:
assert df.dtypes[col] == np.bool_
# Mixed table
table = jimport("org.scijava.table.DefaultGenericTable")()
table.appendColumns(columns)
table.setRowCount(7)
array_float = np.random.random(size=(7, 1))
array_int = np.random.random(size=(7, 1)) * 100
array_int = array_int.astype("int32")
array_bool = np.random.random(size=(7, 1)) > 0.5
array_str = np.array(["foo", "bar", "foobar", "barfoo", "oofrab", "oof", "rab"])
array_double = np.random.random(size=(7, 1))
array_double = array_double.astype("float64")
# fill mixed table
for i in range(table.getRowCount()):
table.set(0, i, Float(float(array_float[i].item())))
table.set(1, i, Integer(int(array_int[i].item())))
table.set(2, i, Boolean(bool(array_bool[i].item())))
table.set(3, i, String(array_str[i]))
table.set(4, i, Double(float(array_double[i].item())))
df = to_python(table)
# Table types cannot be the same here, unless we want to cast.
# assert_same_table(table, df)
assert type(df["header1"][0]) is float
assert type(df["header2"][0]) is int
assert type(df["header3"][0]) is bool
assert type(df["header4"][0]) is str
assert type(df["header5"][0]) is float
def _fill_table(self, table, ndarr, ctor):
for i in range(table.getColumnCount()):
s = ndarr[:, i]
for j in range(table.getRowCount()):
table.setValue(i, j, ctor(s[j]))
return table