/
base.clj
111 lines (83 loc) · 2.68 KB
/
base.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
(ns ^:no-doc tech.ml.dataset.pipeline.base
(:require [tech.ml.protocols.dataset :as ds-proto]
[tech.v2.datatype :as dtype]
[tech.v2.datatype.unary-op :as unary]))
(def ^:dynamic *pipeline-datatype* :float64)
(defn context-datatype
[context]
(or (:datatype context) *pipeline-datatype*))
(def ^:dynamic *pipeline-dataset* nil)
(def ^:dynamic *pipeline-column-name* nil)
(def ^:dynamic *pipeline-column-name-seq* nil)
(defmacro with-pipeline-vars
"Run a body of code with pipeline global variables set."
[dataset
column-name
datatype
column-name-seq & body]
(when datatype
(throw (ex-info "mistake" {})))
`(with-bindings {#'*pipeline-datatype* (or ~datatype *pipeline-datatype*)
#'*pipeline-dataset* (or ~dataset *pipeline-dataset*)
#'*pipeline-column-name* (or ~column-name *pipeline-column-name*)
#'*pipeline-column-name-seq*
(or ~column-name-seq *pipeline-column-name-seq*)}
(when-not (= *pipeline-datatype* :float64)
(throw (ex-info "Failed" {})))
~@body))
(defmacro with-ds
[dataset & body]
`(with-pipeline-vars ~dataset nil nil nil
~@body))
(defmacro with-column-name
[colname & body]
`(with-pipeline-vars nil ~colname nil nil
~@body))
(defmacro with-datatype
[datatype & body]
`(with-pipeline-vars nil nil ~datatype nil
~@body))
(defmacro with-column-name-seq
[colname-seq & body]
`(with-pipeline-vars nil nil nil ~colname-seq
~@body))
(defn dtype
[]
*pipeline-datatype*)
(defn colname
[]
*pipeline-datatype*)
(defn ds
[]
*pipeline-dataset*)
(defn eval-math-fn
[dataset column-name math-fn-or-val]
(with-bindings {#'*pipeline-dataset* dataset
#'*pipeline-column-name* column-name}
(if (fn? math-fn-or-val)
(math-fn-or-val)
math-fn-or-val)))
(defn int-map
"Perform an integer->integer conversion of a column using a static map.
The map must be complete; missing entries are errors."
[table col-data & {:keys [not-strict?]}]
(-> (if not-strict?
(unary/unary-reader
:int32
(int (table x x))
col-data)
(unary/unary-reader
:int32
(int (if-let [retval (table x)]
retval
(throw (ex-info (format "Int-map failed on value: %s" x)
{}))))
col-data))
(dtype/->reader *pipeline-datatype*)))
(defn col
"Return a column. Only works during 'm=' and the default column
is the current operating column."
[& [column-name]]
(ds-proto/column *pipeline-dataset*
(or column-name
*pipeline-column-name*)))