Skip to content

Instantly share code, notes, and snippets.

@sritchie
Created February 27, 2011 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sritchie/845855 to your computer and use it in GitHub Desktop.
Save sritchie/845855 to your computer and use it in GitHub Desktop.
;; The goal here is to write an aggregator that takes in a sequence of
;; tuples of the form <tperiod, pixel-vector>, and returns tuples of
;; the form <pixel-index, min-time, max-time, timeseries>.
;;
;; We assume that we're receiving chunks for every month within the
;; range. We measure pixel-index as the position inside the chunk.
;;
;; Example:
;; (timeseries [[1 [7 4 2 1]]
;; [2 [1 2 3 4]]
;; [3 [3 4 3 2]]
;; [4 [4 8 7 5]]
;; [5 [1 5 3 2]]])
;;
;; ;=> ([0 1 5 [7 1 3 4 1]]
;; [1 1 5 [4 2 4 8 5]]
;; [2 1 5 [2 3 3 7 3]]
;; [3 1 5 [1 4 2 5 2]])
;;
;; Here's my first attempt:
(defn transpose
"Matrix transpose. Example:
(transpose [[1 2 3] [4 5 6] [7 8 9]])
;=> [[1 4 7] [2 5 8] [3 6 9]]"
[a]
(vec (apply map vector a)))
;; Sample data at 130 months, 24000 pixels per chunk.
(def test-chunks
(for [period (range 130)]
(vector period (range 24000))))
(defn timeseries [chunks]
(let [[periods series] (transpose chunks)]
(map-indexed #(vector %1
(first periods)
(last periods)
%2)
(transpose series))))
;; Why is this lazy?
(defn timeseries [tuples]
(let [[periods chunks] (apply map vector tuples)
periodize (partial vector
(first periods)
(last periods))
tupleize (comp periodize vector)]
(map-indexed cons (apply map tupleize chunks))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment