/
parse_test.clj
398 lines (342 loc) · 14.3 KB
/
parse_test.clj
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
(ns tech.ml.dataset.parse-test
(:require [clojure.test :refer [deftest is]]
[tech.v2.datatype :as dtype]
[tech.v2.datatype.functional :as dfn]
[tech.ml.dataset.parse :as ds-parse]
[tech.ml.dataset.base :as ds-base]
[tech.ml.dataset :as ds]
[tech.ml.dataset.column :as ds-col]
[clojure.set :as set])
(:import [com.univocity.parsers.csv CsvFormat CsvParserSettings CsvParser]
[java.nio.charset StandardCharsets]))
(def test-file "data/ames-house-prices/train.csv")
(def missing-data
(->> [{:column-name "LotFrontage", :missing-count 259}
{:column-name "Alley", :missing-count 1369}
{:column-name "MasVnrType", :missing-count 8}
{:column-name "MasVnrArea", :missing-count 8}
{:column-name "BsmtQual", :missing-count 37}
{:column-name "BsmtCond", :missing-count 37}
{:column-name "BsmtExposure", :missing-count 38}
{:column-name "BsmtFinType1", :missing-count 37}
{:column-name "BsmtFinType2", :missing-count 38}
{:column-name "Electrical", :missing-count 1}
{:column-name "FireplaceQu", :missing-count 690}
{:column-name "GarageType", :missing-count 81}
{:column-name "GarageYrBlt", :missing-count 81}
{:column-name "GarageFinish", :missing-count 81}
{:column-name "GarageQual", :missing-count 81}
{:column-name "GarageCond", :missing-count 81}
{:column-name "PoolQC", :missing-count 1453}
{:column-name "Fence", :missing-count 1179}
{:column-name "MiscFeature", :missing-count 1406}
]
(map (juxt :column-name :missing-count))
(sort-by first)))
(def datatype-answers
[["1stFlrSF" :int16]
["2ndFlrSF" :int16]
["3SsnPorch" :int16]
["Alley" :string]
["BedroomAbvGr" :int16]
["BldgType" :string]
["BsmtCond" :string]
["BsmtExposure" :string]
["BsmtFinSF1" :int16]
["BsmtFinSF2" :int16]
["BsmtFinType1" :string]
["BsmtFinType2" :string]
["BsmtFullBath" :int16]
["BsmtHalfBath" :int16]
["BsmtQual" :string]
["BsmtUnfSF" :int16]
["CentralAir" :boolean]
["Condition1" :string]
["Condition2" :string]
["Electrical" :string]
["EnclosedPorch" :int16]
["ExterCond" :string]
["ExterQual" :string]
["Exterior1st" :string]
["Exterior2nd" :string]
["Fence" :string]
["FireplaceQu" :string]
["Fireplaces" :int16]
["Foundation" :string]
["FullBath" :int16]
["Functional" :string]
["GarageArea" :int16]
["GarageCars" :int16]
["GarageCond" :string]
["GarageFinish" :string]
["GarageQual" :string]
["GarageType" :string]
["GarageYrBlt" :int16]
["GrLivArea" :int16]
["HalfBath" :int16]
["Heating" :string]
["HeatingQC" :string]
["HouseStyle" :string]
["Id" :int16]
["KitchenAbvGr" :int16]
["KitchenQual" :string]
["LandContour" :string]
["LandSlope" :string]
["LotArea" :int32]
["LotConfig" :string]
["LotFrontage" :int16]
["LotShape" :string]
["LowQualFinSF" :int16]
["MSSubClass" :int16]
["MSZoning" :string]
["MasVnrArea" :int16]
["MasVnrType" :string]
["MiscFeature" :string]
["MiscVal" :int16]
["MoSold" :int16]
["Neighborhood" :string]
["OpenPorchSF" :int16]
["OverallCond" :int16]
["OverallQual" :int16]
["PavedDrive" :string]
["PoolArea" :int16]
["PoolQC" :string]
["RoofMatl" :string]
["RoofStyle" :string]
["SaleCondition" :string]
["SalePrice" :int32]
["SaleType" :string]
["ScreenPorch" :int16]
["Street" :string]
["TotRmsAbvGrd" :int16]
["TotalBsmtSF" :int16]
["Utilities" :string]
["WoodDeckSF" :int16]
["YearBuilt" :int16]
["YearRemodAdd" :int16]
["YrSold" :int16]])
(deftest base-ames-parser-test
(let [result (ds-parse/csv->dataset test-file)
dtypes (->> (vals result)
(map meta)
(sort-by :name)
(mapv (juxt :name :datatype)))]
(is (= (set (map first datatype-answers))
(set (map first dtypes))))
(let [dtype-map (into {} dtypes)
differences (->> datatype-answers
(map (fn [[colname col-dtype]]
(let [detected-dtype (dtype-map colname)]
(when-not (= detected-dtype col-dtype)
{:name colname
:expected-datatype col-dtype
:result-datatype detected-dtype}))))
(remove nil?)
seq)]
(is (nil? differences)
(str differences)))
(let [result-missing-data (->> (vals result)
(map (juxt ds-col/column-name
(comp dtype/ecount ds-col/missing)))
(remove #(= 0 (second %)))
(sort-by first))]
(is (= (set (map first missing-data))
(set (map first result-missing-data))))))
(let [result (ds-parse/csv->dataset
test-file
{:n-records 100
:column-whitelist ["Id" "SalePrice" "YearBuilt"]})]
(is (= 3 (count result)))
;;Header row accounts for one.
(is (= 100 (ds-base/row-count result))))
(let [result (ds-parse/csv->dataset test-file {:n-records 100
:column-blacklist (range 70)})]
(is (= 11 (count result)))
(is (= 100 (ds-base/row-count result)))))
(deftest base-ames-load-test
;;Here we just test that the options correctly pass through ->dataset
(let [result (ds-base/->dataset test-file
{:n-records 100
:column-whitelist ["Id" "SalePrice" "YearBuilt"]})]
(is (= 3 (ds-base/column-count result)))
;;Header row accounts for one.
(is (= 100 (ds-base/row-count result))))
(let [result (ds-base/->dataset test-file {:n-records 100
:column-blacklist (range 70)})]
(is (= 11 (ds-base/column-count result)))
;;Header row accounts for one.
(is (= 100 (ds-base/row-count result)))))
(deftest specify-column-types
;;parse everything as float32
(let [result (ds-base/->dataset
test-file
{:n-records 100
:column-whitelist ["1stFlrSF" "2ndFlrSF" "3SsnPorch"]
:parser-fn :float32})]
(is (= #{:float32}
(set (map dtype/get-datatype (vals result)))))
(is (= 3 (ds-base/column-count result))))
;;Next up is a map of colname->datatype
(let [result (ds-base/->dataset
test-file
{:n-records 100
:column-whitelist ["1stFlrSF" "2ndFlrSF" "3SsnPorch"]
:parser-fn {"1stFlrSF" :float32
"2ndFlrSF" :int32}})]
(is (= #{:float32 :int32 :int16}
(set (map dtype/get-datatype (vals result))))))
;;Or you can implement a function from colname,first-n-strings->parser
(let [parser-fn (fn [_colname _coldata-n-strings]
;;Shortcut to create a full parser from a stateless simple parser
(ds-parse/simple-parser->parser :float32))
result (ds-base/->dataset
test-file
{:n-records 100
:column-whitelist ["1stFlrSF" "2ndFlrSF" "3SsnPorch"]
:parser-fn parser-fn})]
(is (= #{:float32}
(set (map dtype/get-datatype (vals result)))))))
(deftest semi-colon-delimited-file
(let [result (ds-base/->dataset "test/data/sample01.csv"
{:separator \;})]
(is (= 3 (ds-base/column-count result)))))
(deftest tough-file
(let [result (ds-base/->dataset "test/data/essential.csv"
{:n-initial-skip-rows 1
:skip-bad-rows? true})]
(is (= 5 (ds-base/column-count result)))))
(defn- make-essential-csv-parser
[]
(-> (doto (CsvParserSettings.)
(.. getFormat (setLineSeparator "\n"))
(.setHeaderExtractionEnabled true)
(.setIgnoreLeadingWhitespaces true)
(.setIgnoreTrailingWhitespaces true))
(CsvParser.)))
(deftest custom-csv-parser
(let [result (ds-base/->dataset "test/data/essential.csv"
{:csv-parser (make-essential-csv-parser)
:skip-bad-rows? true})]
(is (= 5 (ds-base/column-count result)))))
(deftest simple-write-test
(let [initial-ds (ds-base/->dataset
test-file
{:n-records 20
:column-whitelist ["1stFlrSF" "2ndFlrSF" "3SsnPorch"]})
_ (ds-base/write-csv! initial-ds "test.tsv")
new-ds (ds-base/->dataset "test.tsv")]
(is (dfn/equals (initial-ds "1stFlrSF")
(new-ds "1stFlrSF")))
(is (dfn/equals (initial-ds "2ndFlrSF")
(new-ds "2ndFlrSF"))))
(let [missing-ds (-> (ds-base/->dataset
test-file
{:n-records 20
:column-whitelist ["1stFlrSF" "2ndFlrSF" "3SsnPorch"]})
(ds-base/update-column
"1stFlrSF"
#(ds-col/set-missing % [2 4 7 9])))
_ (ds-base/write-csv! missing-ds "test.tsv")
new-ds (ds-base/->dataset "test.tsv")]
(is (dfn/equals (missing-ds "1stFlrSF")
(new-ds "1stFlrSF")))
(is (= #{2 4 7 9}
(set (ds-col/missing (new-ds "1stFlrSF")))))))
(deftest date-time-format-test-1
(let [stock-ds (ds-base/->dataset "test/data/stocks.csv")]
(is (= :packed-local-date (dtype/get-datatype (stock-ds "date")))))
(let [temp-ds (ds-base/->dataset "test/data/seattle-temps.csv")]
(is (= :zoned-date-time (dtype/get-datatype (temp-ds "date")))))
(let [stock-ds (ds-base/->dataset "test/data/stocks.csv"
{:parser-fn
{"date" :local-date}})]
(is (= :local-date (dtype/get-datatype (stock-ds "date"))))))
(defn verify-relaxed-parse
[ds]
(let [date-col (ds "date")
col-meta (meta date-col)
^List unparsed-data (:unparsed-data col-meta)
^RoaringBitmap unparsed-indexes (:unparsed-indexes col-meta)]
(is (= :packed-local-date (dtype/get-datatype date-col)))
;;Make sure unparsed data came through intact
(is (= #{"hello" "1212"}
(set unparsed-data)))))
(deftest bad-csv-relaxed-1
(let [ds (ds-base/->dataset "test/data/stocks-bad-date.csv")]
(is (= :string (dtype/get-datatype (ds "date"))))
;;Make sure unparsed data came through intact
(is (= #{"hello" "1212"}
(set/intersection #{"hello" "1212"}
(set (ds-col/unique (ds "date"))))))
(let [updated-ds (ds-base/update-column
ds "date" (partial ds-col/parse-column
[:packed-local-date :relaxed?]))]
(verify-relaxed-parse updated-ds))))
(deftest bad-csv-relaxed-2
(let [ds (ds-base/->dataset "test/data/stocks-bad-date.csv"
{:parser-fn
{"date" [:packed-local-date :relaxed?]}})]
(verify-relaxed-parse ds)))
(deftest csv-keyword-colnames
(let [stocks (ds-base/->dataset "test/data/stocks.csv" {:key-fn keyword})]
(is (every? keyword? (ds-base/column-names stocks)))))
(deftest parse-empty-column-name
(let [data (ds-base/->dataset "test/data/rcsv.csv")]
(is (= #{0 "Urban Female" "Urban Male" "Rural Female" "Rural Male"}
(set (ds-base/column-names data))))))
(deftest parse-ip-addrs-as-string
(let [data (ds-base/->dataset "test/data/ip-addrs.csv")]
(is (= :string (dtype/get-datatype (data "ip"))))))
(def arrow-file "test/data/iris.feather")
(def parquet-file "test/data/parquet/userdata1.parquet")
(deftest parse-parquet
(let [ds (ds-base/->dataset parquet-file)]
(is (= 13 (ds-base/column-count ds)))
(is (= 1000 (ds-base/row-count ds)))
(is (= #{:local-date-time :float64 :int32 :string}
(->> (map dtype/get-datatype (vals ds))
set)))))
(deftest parse-ragged
(let [ds (ds-base/->dataset "test/data/ragged.csv"
{:header-row? false
:key-fn keyword})]
(is (= [:column-0 :column-1 :column-2 :column-3 :column-4 :column-5
:column-6 :column-7 :column-8 :column-9 :column-10 :column-11]
(vec (ds/column-names ds))))
(is (= 12 (ds-base/column-count ds)))
(is (= [4 24 31 33 65 67 68 71 75 76 93 97]
(vec ((ds-base/value-reader ds) 4))))
(is (= [10 33 51 66 67 84 nil nil nil nil nil nil]
(vec ((ds-base/value-reader ds) 10))))))
(deftest parse-small-doubles
(let [ds (ds-base/->dataset "test/data/double_parse_test.csv")]
(is (= 197 (count (filter #(not= 0.0 % ) (ds "pvalue")))))))
(deftest string-separators
(let [ds (ds-base/->dataset "test/data/double_parse_test.csv" {:separator ","})]
(is (= 197 (count (filter #(not= 0.0 % ) (ds "pvalue")))))
(is (thrown? Throwable (ds-base/->dataset "test/data/double_parse_test.csv"
{:separator ",n"})))))
(deftest encoded-text
(let [tf "test/data/medical-text.csv"
base-ds (ds-base/->dataset tf)
first-abstract (first (base-ds "abstract"))]
;;As encoded text
(let [default-ds (ds-base/->dataset
tf {:parser-fn {"abstract" :encoded-text}})]
(is (= first-abstract (first (default-ds "abstract")))))
(let [charset-ds (ds-base/->dataset
tf {:parser-fn {"abstract" [:encoded-text
StandardCharsets/ISO_8859_1]}})]
(is (= first-abstract (first (charset-ds "abstract")))))
(let [encdec-ds (ds-base/->dataset
tf {:parser-fn {"abstract"
[:encoded-text "UTF-16LE"]}})]
(is (= first-abstract (first (encdec-ds "abstract")))))))
;; Failing due to apparently invalid iris.feather file
;; (deftest parse-arrow
;; (let [ds (ds-base/->dataset arrow-file)]
;; (is (= 13 (ds-base/column-count ds)))
;; (is (= 1000 (ds-base/row-count ds)))
;; (is (= #{:local-date-time :float64 :int32 :string}
;; (->> (map dtype/get-datatype ds)
;; set)))))