; @Dataset(label="Red blood cell channel") rbc
; @OUTPUT Dataset velocity-field

(ns zebrafish-vasculature.optical-flow
  (:require [fun.imagej.img :as img]
            [fun.imagej.img.cursor :as cursor]
            [fun.imagej.img.shape :as shape]
            [fun.imagej.img.type :as tpe]
            [fun.imagej.core :as ij]
            [fun.imagej.ops :as ops]
            [fun.imagej.img.utils :as img-utils]
            [fun.imagej.mesh :as msh]
            [fun.imagej.segmentation.fast-segmentation :as fseg]
            [clojure.java.io :as io]
            [clojure.string :as string]
            [zebrafish-vasculature.skeleton :as skeleton]
            [zebrafish-vasculature.training-utils :as tutils]
            [fun.imagej.algorithm.optical-flow :as opt-flow]))

(defn -main [& args]
  (let [;; First put everything into a map
        argmap (apply hash-map
                      (mapcat #(vector (read-string (first %)) (second %) #_(read-string (second %)))
                              (partition 2 args)))
        ;; Then read-string on *some* args, but ignore others
        argmap (apply hash-map
                      (apply concat
                             (for [[k v] argmap]
                               [k (cond (= k :output-directory) v
                                        (= k :cache-directory) v
                                        (= k :directory) v
                                        (= k :basename) v
                                        :else (read-string v))])))
        arg-params (merge {:num-positive-samples 1000
                           :num-negative-samples 1000
                           :label-rbc true
                           :generate-dataset true
                           :solve-segmentation true
                           :save-segmentation-config true
                           ;:cache-directory "/projects/VirtualFish/kyle/"
                           ;:cache-directory "/Users/kharrington/Data/Daetwyler_Stephan/test_ISVs/cache/"
                           :segmentation-config-filename "segmentation_config.clj"
                           :write-segmentation true
                           :segmentation-filename "segmentationMap.tif"
                           ;:basename "161122_angle001_t0188_registered"
                           ;:basename "t00161_angle002_crop_001"
                           ;:directory "/Users/kharrington/Data/Daetwyler_Stephan/consecutiveData/t00161/"
                           :directory "/Users/kharrington/Data/Daetwyler_Stephan/movie_stack/"
                           ;:basename "head_vasculature_crop_003"
                           ;:basename "head_vasculature_crop_001"
                           :basename "head_vasculature_composite_3steps"
                           :verbose true
                           ; consider adding a tag
                           }; default params
                          argmap)
        basename (str (:directory arg-params)
                      (:basename arg-params))
        weight-filename (str basename "_weights.csv")]
    (println args)
    (println argmap)
    (println arg-params)
    (println (:basename arg-params) (:directory arg-params) basename)

    (def full-image (fun.imagej.ops.convert/float32 (ij/open-img (str basename ".tif"))))
    ;(def vasculature (img/normalize (img/hyperslice full-image 2 0)))
    (def rbc (img/normalize (img/hyperslice full-image 2 1)))

    (def time-start 0)
    (def time-stop 2)
    (def time-dimension 2)

    (def velocities (img/concat-imgs
                      (map #(opt-flow/velocity-img-2d % time-dimension time-start time-stop)
                           (img/dimension-split rbc 2))))

    ;(ij/show velocities "Velocities")

    ;(ij/save-img (fun.imagej.ops.convert/float32 velocities) (str basename "_velocities.tif"))

    (ij/save-img (fun.imagej.ops.convert/float32 (img/hyperslice velocities 2 0)) (str basename "_vx.tif"))
    (ij/save-img (fun.imagej.ops.convert/float32 (img/hyperslice velocities 2 1)) (str basename "_vy.tif"))
    ))

;(-main)
