<commit_before>(ns org.micromanager.test.autofocus
  "Use image stacks to test scoring methods for image-based autofocus."
  (:import (ij IJ)))

(defn diff
  "Compute the difference of subsequent points"
  [data]
  (map (fn [[a b]] (- b a)) (partition 2 1 data)))

(defn mean
  "Compute the mean intensity of a processor."
  [processor]
  (.. processor getStatistics mean))

(defn std-dev
  "Compute the normalized standard deviation of a processor."
  [processor]
  (let [stats (.getStatistics processor)]
    (/ (.stdDev stats) (.mean stats))))

(defn edges
  "Compute a score based on the intensity of edges in the image."
  [processor]
  (/ (-> processor .duplicate (doto .findEdges) mean)
     (mean processor)))

(defn sharp-edges
  "Compute a score based on the intensity of sharpened edges in the image."
  [processor]
  (-> processor .duplicate (doto .sharpen .findEdges) mean))

(defn slice-processors
  "Returns a sequence of ImageProcessors for a given slice in a multislice
   ImagePlus. Zero-based."
  [imgp]
  (for [i (range (.getNSlices imgp))]
    (.. imgp getStack (getProcessor (inc i)))))

(defn print-results
  "Prints each item in a sequence preceded by an index integer.
   If starting-index is specified, then the first item will
   have that index; otherwise the first item will be numbered 0."
  ([starting-index data]
  (loop [i starting-index
         data data]
    (println i (first data))
    (when (next data)
      (recur (inc i) (next data)))))
  ([data] (print-results 0 data)))
    
(defn scoring-data
  [scoring-method]
  "Returns the scores using a particular scoring method on the slices
   in the current open ImageJ image stack."
  (map scoring-method (slice-processors (IJ/getImage))))

(defn scoring-test
  "Tests the scoring method on the current open ImageJ image stack."
  [scoring-method]
  (let [data (scoring-data scoring-method)
        diff-data (diff data)]
  (print-results (map vector data (cons nil diff-data)))))<commit_msg>sharp-edges should be normalized; cleanup<commit_after>(ns org.micromanager.test.autofocus
  "Use image stacks to test scoring methods for image-based autofocus."
  (:import (ij IJ)))

(defn slice-processors
  "Returns a sequence of ImageProcessors for a given slice in a multislice
   ImagePlus. Zero-based."
  [imgp]
  (for [i (range (.getNSlices imgp))]
    (.. imgp getStack (getProcessor (inc i)))))

;; scoring methods

(defn mean
  "Compute the mean intensity of a processor."
  [processor]
  (.. processor getStatistics mean))

(defn std-dev
  "Compute the normalized standard deviation of a processor."
  [processor]
  (let [stats (.getStatistics processor)]
    (/ (.stdDev stats) (.mean stats))))

(defn edges
  "Compute a score based on the intensity of edges in the image."
  [processor]
  (/ (-> processor .duplicate (doto .findEdges) mean)
     (mean processor)))

(defn sharp-edges
  "Compute a score based on the intensity of sharpened edges in the image."
  [processor]
  (/ (-> processor .duplicate (doto .sharpen .findEdges) mean)
     (mean processor)))

;; data and printout

(defn diff
  "Compute the difference of subsequent points."
  [data]
  (map (fn [[a b]] (- b a)) (partition 2 1 data)))

(defn print-results
  "Prints each item in a sequence preceded by an index integer.
   If starting-index is specified, then the first item will
   have that index; otherwise the first item will be numbered 0."
  ([starting-index data]
    (loop [i starting-index
           data data]
      (println i (first data))
      (when (next data)
        (recur (inc i) (next data)))))
  ([data] (print-results 0 data)))
    
;; testing

(defn scoring-data
  [scoring-method]
  "Returns the scores using a particular scoring method on the slices
   in the current open ImageJ image stack."
  (map scoring-method (slice-processors (IJ/getImage))))

(defn scoring-test
  "Tests the scoring method on the current open ImageJ image stack."
  [scoring-method]
  (let [data (scoring-data scoring-method)
        diff-data (diff data)]
    (print-results (map vector data (cons nil diff-data)))))
{'fraction_non_alphanumeric': 0.11222739524626317, 'fraction_numerical': 0.0019603038470963, 'mean_word_length': 3.466083150984683, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5318590', 'n_tokens_mistral': 1275, 'n_tokens_neox': 1238, 'n_words': 503}