Skip to content

Commit

Permalink
first working conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
behrica committed Sep 23, 2024
1 parent 93faa31 commit 79866c2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 12 deletions.
40 changes: 29 additions & 11 deletions src/clojisr/v1/impl/java_to_clj.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
(:require [tech.v3.dataset :as ds]
[tech.v3.dataset.column :as col]
[tech.v3.datatype :refer [->reader]]

[tech.v3.tensor :as dtt]
[clojisr.v1.impl.protocols :as prot]
[clojisr.v1.impl.common :refer [tsp->reader first-step->java java->column cartesian-product]]))
[clojisr.v1.impl.common :refer [tsp->reader first-step->java java->column cartesian-product]]
[tech.v3.datatype :as dtype]
[ tech.v3.tensor.dimensions :as tdim]))

(set! *unchecked-math* :warn-on-boxed)

Expand Down Expand Up @@ -92,20 +94,36 @@
(apply ds/concat)))))

(defn multidim? [obj] (prot/attribute obj "dim"))
(defn multidim->dataset
([exp] (multidim->dataset exp nil))
([exp extra]
(let [dims (fix-dims (prot/attribute exp "dim"))
dimnames (fix-dimnames dims (prot/attribute exp "dimnames"))]
(make-nd exp extra dims dimnames))))

(defn- make-tensor [exp dims]

(def exp exp)
(def dims dims)

(dtt/construct-tensor (prot/->clj exp)
(tdim/dimensions dims)))
;; (dtt/->tensor [[7 4 6 8 8 7 5 9 7 8]
;; [4 1 3 6 5 2 3 5 4 2]
;; [3 8 5 1 7 9 3 8 5 2]] :datatype :float64))
(defn multidim->dataset-or-tensor
([exp] (multidim->dataset-or-tensor exp nil nil))
([exp extra] (multidim->dataset-or-tensor exp extra nil))
([exp extra options]


(let [dims (fix-dims (prot/attribute exp "dim"))
dimnames (fix-dimnames dims (prot/attribute exp "dimnames"))]
(if (:as-tensor options)
(make-tensor exp dims)
(make-nd exp extra dims dimnames)))))

(defn mts? [obj] (prot/inherits? obj "mts"))
(defn mts->dataset
[exp]
(let [tsp (-> exp
(prot/attribute "tsp")
(tsp->reader))]
(multidim->dataset exp [tsp :$time])))
(multidim->dataset-or-tensor exp [tsp :$time])))


;; table
Expand Down Expand Up @@ -155,7 +173,7 @@
"Perform high level data conversion"

([exp options]
(println :options options)
;(println :options options)
(def options options)
(let [exp (first-step->java exp)]
(cond
Expand All @@ -164,7 +182,7 @@
(timeseries? exp) (timeseries->dataset exp)
(dist? exp) (dist->dataset exp)
(table? exp) (table->dataset exp)
(multidim? exp) (multidim->dataset exp)
(multidim? exp) (multidim->dataset-or-tensor exp nil options)
:else (prot/->clj exp))))
( [exp] (java->clj exp nil)))

Expand Down
35 changes: 34 additions & 1 deletion src/clojisr/v1/r.clj
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,43 @@

(require-r '[base])

(r "m <- array(rep(1, 365*5*4), dim=c(365, 5, 4))")
(r "m <- array(seq(1, 365*5*4), dim=c(365, 5, 4))")



(-> (r "m")
( r->clj {:as-tensor true}))
;;=> #tech.v3.tensor<object>[365 5 4]
;; [[[ 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]]
;; ...
;; [[7241 7242 7243 7244]
;; [7245 7246 7247 7248]
;; [7249 7250 7251 7252]
;; [7253 7254 7255 7256]
;; [7257 7258 7259 7260]]
;; [[7261 7262 7263 7264]
;; [7265 7266 7267 7268]
;; [7269 7270 7271 7272]
;; [7273 7274 7275 7276]
;; [7277 7278 7279 7280]]
;; [[7281 7282 7283 7284]
;; [7285 7286 7287 7288]
;; [7289 7290 7291 7292]
;; [7293 7294 7295 7296]
;; [7297 7298 7299 7300]]]

)

0 comments on commit 79866c2

Please sign in to comment.