-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest_spark_histogrammar.py
More file actions
279 lines (234 loc) · 8.86 KB
/
Copy pathtest_spark_histogrammar.py
File metadata and controls
279 lines (234 loc) · 8.86 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from pathlib import Path
import pandas as pd
import pytest
from histogrammar.dfinterface.make_histograms import make_histograms
from histogrammar.dfinterface.spark_histogrammar import SparkHistogrammar
try:
from pyspark import __version__ as pyspark_version
from pyspark.sql import SparkSession
spark_found = True
except (ModuleNotFoundError, AttributeError):
spark_found = False
def get_spark():
if not spark_found:
return None
current_path = Path(__file__).resolve().parent
scala = "2.12" if int(pyspark_version[0]) == 3 else "2.13"
hist_spark_jar = current_path / f"jars/histogrammar-sparksql_{scala}-1.0.30.jar"
hist_jar = current_path / f"jars/histogrammar_{scala}-1.0.30.jar"
return (
SparkSession.builder.master("local")
.appName("histogrammar-pytest")
.config("spark.jars", f"{hist_spark_jar},{hist_jar}")
.config("spark.sql.session.timeZone", "GMT")
.getOrCreate()
)
@pytest.fixture
def spark_co():
""":return: Spark configuration"""
return get_spark()
# @pytest.mark.spark
@pytest.mark.skipif(not spark_found, reason="spark not found")
@pytest.mark.filterwarnings("ignore:createDataFrame attempted Arrow optimization because")
def test_get_histograms(spark_co):
pytest.age["data"]["name"] = "age"
pytest.company["data"]["name"] = "company"
pytest.eyesColor["data"]["name"] = "eyeColor"
pytest.gender["data"]["name"] = "gender"
pytest.isActive["data"]["name"] = "isActive"
pytest.latitude["data"]["name"] = "latitude"
pytest.longitude["data"]["name"] = "longitude"
pytest.transaction["data"]["name"] = "transaction"
pytest.latitude_longitude["data"]["name"] = "latitude:longitude"
pytest.latitude_longitude["data"]["bins:name"] = "unit_func"
spark = spark_co
spark_df = spark.createDataFrame(pytest.test_df)
spark_filler = SparkHistogrammar(
features=[
"date",
"isActive",
"age",
"eyeColor",
"gender",
"company",
"latitude",
"longitude",
["isActive", "age"],
["latitude", "longitude"],
"transaction",
"amount",
],
bin_specs={
"transaction": {"num": 100, "low": -2000, "high": 2000},
"longitude": {"bin_width": 5.0, "bin_offset": 0.0},
"latitude": {"bin_width": 5.0, "bin_offset": 0.0},
},
read_key="input",
store_key="output",
)
# test get_histograms() function call
current_hists = spark_filler.get_histograms(spark_df)
# current_hists = make_histograms(spark_df, features, bin_specs)
assert current_hists["age"].toJson() == pytest.age
assert current_hists["company"].toJson() == pytest.company
assert current_hists["eyeColor"].toJson() == pytest.eyesColor
assert current_hists["gender"].toJson() == pytest.gender
assert current_hists["latitude"].toJson() == pytest.latitude
assert current_hists["longitude"].toJson() == pytest.longitude
assert current_hists["transaction"].toJson() == pytest.transaction
# import json
# with open('tests/popmon/hist/resource/transaction.json', 'w') as outfile:
# json.dump(current_hists["transaction"].toJson(), outfile, indent=4)
# @pytest.mark.spark
@pytest.mark.skipif(not spark_found, reason="spark not found")
@pytest.mark.filterwarnings("ignore:createDataFrame attempted Arrow optimization because")
def test_get_histograms_module(spark_co):
pytest.age["data"]["name"] = "age"
pytest.company["data"]["name"] = "company"
pytest.eyesColor["data"]["name"] = "eyeColor"
pytest.gender["data"]["name"] = "gender"
pytest.isActive["data"]["name"] = "isActive"
pytest.latitude["data"]["name"] = "latitude"
pytest.longitude["data"]["name"] = "longitude"
pytest.latitude_longitude["data"]["name"] = "latitude:longitude"
pytest.latitude_longitude["data"]["bins:name"] = "unit_func"
spark = spark_co
spark_df = spark.createDataFrame(pytest.test_df)
spark_filler = SparkHistogrammar(
features=[
"date",
"isActive",
"age",
"eyeColor",
"gender",
"company",
"latitude",
"longitude",
["isActive", "age"],
["latitude", "longitude"],
"amount",
],
bin_specs={
"longitude": {"bin_width": 5.0, "bin_offset": 0.0},
"latitude": {"bin_width": 5.0, "bin_offset": 0.0},
},
read_key="input",
store_key="output",
)
# test transform() function call
datastore = spark_filler.transform(datastore={"input": spark_df})
assert "output" in datastore
current_hists = datastore["output"]
assert current_hists["age"].toJson() == pytest.age
assert current_hists["company"].toJson() == pytest.company
assert current_hists["eyeColor"].toJson() == pytest.eyesColor
assert current_hists["gender"].toJson() == pytest.gender
assert current_hists["latitude"].toJson() == pytest.latitude
assert current_hists["longitude"].toJson() == pytest.longitude
# assert current_hists['date'].toJson() == pytest.date
# assert current_hists['isActive'].toJson() == pytest.isActive
# assert current_hists['isActive:age'].toJson() == pytest.isActive_age
# assert current_hists['latitude:longitude'].toJson() == pytest.latitude_longitude
# @pytest.mark.spark
@pytest.mark.skipif(not spark_found, reason="spark not found")
@pytest.mark.filterwarnings("ignore:createDataFrame attempted Arrow optimization because")
def test_get_histograms_timestamp(spark_co):
from pyspark.sql.functions import to_timestamp
spark = spark_co
data_date = [
"2018-12-10 00:00:00",
"2018-12-10 00:00:00",
"2018-12-10 00:00:00",
"2018-12-10 00:00:00",
"2018-12-10 00:00:00",
"2018-12-17 00:00:00",
"2018-12-17 00:00:00",
"2018-12-17 00:00:00",
"2018-12-17 00:00:00",
"2018-12-19 00:00:00",
]
df = pd.DataFrame(data_date, columns=["dt"])
sdf = spark.createDataFrame(df).withColumn("dt", to_timestamp("dt", "yyyy-MM-dd HH:mm:ss"))
expected = {
"data": {
"binWidth": 2592000000000000.0,
"bins": {"108": 9.0, "109": 1.0},
"bins:type": "Count",
"entries": 10.0,
"name": "dt",
"nanflow": 0.0,
"nanflow:type": "Count",
"origin": 1.2625632e18,
},
"type": "SparselyBin",
"version": "1.1",
}
filler = SparkHistogrammar(features=["dt"])
current_hists = filler.get_histograms(sdf)
assert current_hists["dt"].toJson() == expected
# @pytest.mark.spark
@pytest.mark.skipif(not spark_found, reason="spark not found")
@pytest.mark.filterwarnings("ignore:createDataFrame attempted Arrow optimization because")
def test_get_histograms_date(spark_co):
from pyspark.sql.functions import to_date
spark = spark_co
data_date = [
"2018-12-10",
"2018-12-10",
"2018-12-10",
"2018-12-10",
"2018-12-10",
"2018-12-17",
"2018-12-17",
"2018-12-17",
"2018-12-17",
"2018-12-19",
]
df = pd.DataFrame(data_date, columns=["dt"])
sdf = spark.createDataFrame(df).withColumn("dt", to_date("dt", "yyyy-MM-dd"))
expected = {
"data": {
"binWidth": 2592000000000000.0,
"bins": {"108": 9.0, "109": 1.0},
"bins:type": "Count",
"entries": 10.0,
"name": "dt",
"nanflow": 0.0,
"nanflow:type": "Count",
"origin": 1.2625632e18,
},
"type": "SparselyBin",
"version": "1.1",
}
filler = SparkHistogrammar(features=["dt"])
current_hists = filler.get_histograms(sdf)
assert current_hists["dt"].toJson() == expected
# @pytest.mark.spark
@pytest.mark.skipif(not spark_found, reason="spark not found")
@pytest.mark.filterwarnings("ignore:createDataFrame attempted Arrow optimization because")
def test_null_histograms(spark_co):
spark = spark_co
data = [
(None, None, None, None),
(1, None, None, 2.0),
(None, True, "Jones", None),
(3, True, "USA", 4.0),
(4, False, "FL", 5.0),
]
columns = ["transaction", "isActive", "eyeColor", "t2"]
sdf = spark.createDataFrame(data=data, schema=columns)
hists = make_histograms(sdf, bin_specs={"transaction": {"num": 40, "low": 0, "high": 10}})
assert "transaction" in hists
assert "isActive" in hists
assert "eyeColor" in hists
assert "t2" in hists
h = hists["transaction"]
assert h.nanflow.entries == 2
h = hists["t2"]
assert h.nanflow.entries == 2
h = hists["isActive"]
assert "NaN" in h.bins
assert h.bins["NaN"].entries == 2
h = hists["eyeColor"]
assert "NaN" in h.bins
assert h.bins["NaN"].entries == 2