-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.clj
344 lines (306 loc) · 10.2 KB
/
utils.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
(ns clojask.utils
(:require [clojure.core.async :refer [chan sliding-buffer >!! close!]]
[clojure.java.io :refer [resource]]
[onyx.plugin.core-async :refer [take-segments!]]
;; [tech.v3.dataset :as ds]
[clojure.string :as str]
[clojure.java.io :as io])
(:import (java.util Date)
(java.time LocalDate)
(java.time LocalDateTime)
(java.time.format DateTimeFormatter)))
"Utility function used in dataframe"
(defn gets
"unlike core/get, get elements from indices"
[coll indices]
(mapv #(nth coll %) indices)
)
(defn gets-format
"gets with format"
[coll indices formatters]
(mapv (fn [_]
(let [val (nth coll _)]
(if-let [formatter (get formatters _)]
(formatter val)
val))) indices)
)
(defn get-key
[row types key-index key]
(let [index (get key-index key)]
(if (contains? types index)
((get types index) (.get row index))
(.get row index))))
(defn get-val
[row types index]
(map (fn [_] (if-let [parser (get types _)]
(parser (nth row _))
(nth row _)))
index))
(defn eval-res
[row types formats operations index]
;; (spit "resources/debug.txt" (str row "\n") :append true)
;; (spit "resources/debug.txt" (str types) :append true)
;; (spit "resources/debug.txt" operations :append true)
;; (spit "resources/debug.txt" (str index "\n") :append true)
;; (println opr-vec)
(let [opr-vec (get operations index)
vals (get-val row types (first opr-vec))]
;; (println [vals])
(loop [res vals oprs (rest opr-vec)]
(if (= (count oprs) 0)
(first res)
(let [opr (first oprs)
rest (rest oprs)]
(recur [(apply opr res)] rest))))))
(defn eval-res-ne
[row types formats operations index]
;; (spit "resources/debug.txt" (str row "\n") :append true)
;; (spit "resources/debug.txt" (str types) :append true)
;; (spit "resources/debug.txt" operations :append true)
;; (spit "resources/debug.txt" (str index "\n") :append true)
;; (println opr-vec)
(try
(let [opr-vec (get operations index)
vals (get-val row types (first opr-vec))]
;; (println [vals])
(loop [res vals oprs (rest opr-vec)]
(if (= (count oprs) 0)
(first res)
(let [opr (first oprs)
rest (rest oprs)]
(recur [(apply opr res)] rest)))))
(catch Exception e nil)))
(defn filter-check
[filters types row]
;; (loop [filters filters]
;; (let [filter (first filters)
;; rem (rest filters)]
;; (if (= filter nil)
;; true
;; (if (not= (filter row) true)
;; false
;; (recur rem)))))
(if (= filters [])
true
(loop [filters filters]
(let [com (first filters)
rem (rest filters)]
;; (println com)
(if (= com nil)
true
(do
(if (apply (first com) (get-val row types (nth com 1)))
(recur rem)
false)))))))
(def toInt
(atom (fn [string]
(try
(Integer/parseInt string)
(catch Exception e nil)))))
(def toDouble
(atom (fn [string]
(try
(Double/parseDouble string)
(catch Exception e nil)))))
(def toString
(atom (fn [string]
string)))
(def fromString
(atom (fn [_] (str _))))
;; (def toDate
;; (atom (fn [string]
;; (try
;; (LocalDate/parse string (DateTimeFormatter/ofPattern "yyyy-MM-dd"))
;; (catch Exception e (throw e))))))
;; (def fromDate
;; (atom (fn [date]
;; (if (= (type date) java.time.LocalDate)
;; (.format date (DateTimeFormatter/ofPattern "yyyy-MM-dd"))
;; date))))
;; (def toDateTime
;; (atom (fn [string]
;; (try
;; (LocalDateTime/parse string (DateTimeFormatter/ofPattern "yyyy-MM-dd HH:mm:ss"))
;; (catch Exception e (throw e))))))
;; (def fromDateTime
;; (atom (fn [date]
;; (if (= (type date) java.time.LocalDateTime)
;; (.format date (DateTimeFormatter/ofPattern "yyyy-MM-dd HH:mm:ss"))
;; date))))
;; (defn set-format-string
;; [string]
;; (if (or (str/starts-with? string "date:") (str/starts-with? string "datetime:"))
;; (let [format-string (subs string (inc (str/index-of string ":")))]
;; (reset! toDate
;; (fn [string]
;; (try
;; (LocalDate/parse string (DateTimeFormatter/ofPattern format-string))
;; (catch Exception e (throw e)))))
;; (reset! fromDate
;; (fn [date]
;; (if (= (type date) java.time.LocalDate)
;; (.format date (DateTimeFormatter/ofPattern format-string))
;; date)))
;; (reset! toDateTime
;; (fn [string]
;; (try
;; (LocalDateTime/parse string (DateTimeFormatter/ofPattern format-string))
;; (catch Exception e (throw e)))))
;; (reset! fromDateTime
;; (fn [date]
;; (if (= (type date) java.time.LocalDateTime)
;; (.format date (DateTimeFormatter/ofPattern format-string))
;; date))))
;; ))
;; ;; (def operation-type-map
;; ;; {toInt "int"
;; ;; toDouble "double"
;; ;; toString "string"
;; ;; toDate "date"})
;; (def type-operation-map
;; {"int" [toInt fromString]
;; "double" [toDouble fromString]
;; "string" [toString fromString]
;; "date" [toDate fromDate]
;; "datetime" [toDateTime fromDateTime]})
(def toDate
(atom (fn [string]
(try
(.parse (java.text.SimpleDateFormat. "yyyy-MM-dd") string)
;; (catch Exception e (throw e))
(catch Exception e nil)
))))
(def fromDate
(atom (fn [date]
(if (= (type date) java.util.Date)
(.format (java.text.SimpleDateFormat. "yyyy-MM-dd") date)
date))))
(defn set-format-string
[string]
(if (or (str/starts-with? string "date:") (str/starts-with? string "datetime:"))
(let [format-string (subs string (inc (str/index-of string ":")))]
(reset! toDate
(fn [string]
(try
(.parse (java.text.SimpleDateFormat. format-string) string)
(catch Exception e (throw e))
(catch Exception e nil)
)))
(reset! fromDate
(fn [date]
(if (= (type date) java.util.Date)
(.format (java.text.SimpleDateFormat. format-string) date)
date))))
(do
(reset! toDate
(fn [string]
(try
(.parse (java.text.SimpleDateFormat. "yyyy-MM-dd") string)
;; (catch Exception e (throw e))
(catch Exception e nil)
)))
(reset! fromDate
(fn [date]
(if (= (type date) java.util.Date)
(.format (java.text.SimpleDateFormat. "yyyy-MM-dd") date)
date))))))
(def type-operation-map
{"int" [toInt fromString]
"double" [toDouble fromString]
"string" [toString fromString]
"date" [toDate fromDate]
"datetime" [toDate fromDate]})
(defn type-detection
[file]
(let [sample (take 5 file)]))
(defn is-in
[col dataframe]
(if (contains? (.getKeyIndex (:col-info dataframe)) col)
true
false))
(defn is-out
[col dataframe]
(if (contains? (.getKeyIndex (:col-info dataframe)) col)
false
true))
(defn are-in
"return should be [] if all in"
[cols dataframe]
(filter (fn [col] (is-out col dataframe)) cols))
(defn are-out
"return should be [] if all out"
[cols dataframe]
(filter (fn [col] (is-in col dataframe)) cols))
(defn init-file
[out-dir header]
(io/delete-file out-dir true)
(doseq [file (rest (file-seq (io/file "./.clojask/grouped/")))]
(try
(io/delete-file file)
(catch Exception e nil)))
(doseq [file (rest (file-seq (io/file "./.clojask/join/")))]
(try
(io/delete-file file)
(catch Exception e nil)))
(io/make-parents "./.clojask/grouped/a.txt")
(io/make-parents "./.clojask/join/a/a.txt")
(io/make-parents "./.clojask/join/b/a.txt")
(io/make-parents "./.clojask/sort/a.txt")
(if (not= header nil)
(with-open [wrtr (io/writer out-dir)]
(.write wrtr (str (str/join "," header) "\n")))))
(defn get-type-string
[x]
(if (not= x nil)
(subs (str (type x)) 6)
"nil"))
(defn get-type-string-vec
[col]
(let [types (mapv get-type-string col)
types (sort (vec (set types)))]
(str/join " & " types)))
(defn check-duplicate-col
"Check for duplicated column names and return a column names list w/o duplicates"
[colNames]
(if (not= (count (distinct colNames)) (count colNames))
(do
(println "WARNING: Duplicated columns found")
(let [colNames-var (atom colNames)
duplicate-list (into (sorted-map) (clojure.core/filter #(> (last %) 1) (frequencies (deref colNames-var))))
counter (atom {})]
(doseq [duplicate-col duplicate-list]
(swap! counter assoc (first duplicate-col) (atom 0)))
(doseq [col colNames]
(if (contains? duplicate-list col)
(reset! colNames-var (map #(if (= % col)
(do
(swap! (get @counter col) inc)
(str % (deref (get @counter col))))
%) (deref colNames-var)))))
(deref colNames-var)))
colNames))
(defn proc-groupby-key-each
[pair]
(if (coll? pair)
(if (and (= 2 (count pair)) (fn? (first pair)) (string? (nth pair 1)))
pair
(if (and (= 1 (count pair)) (string? (first pair)))
[nil pair]
(throw (Exception.))))
(if (string? pair)
[nil pair]
(throw (Exception.)))))
(defn proc-groupby-key
[input]
(try
(if (coll? input)
;; it is a collection
(if (fn? (first input))
(if (= 2 (count input))
[input]
nil)
(mapv proc-groupby-key-each input))
(if (string? input)
[[nil input]]
nil))
(catch Exception e nil)))