Skip to content

Commit 1c69007

Browse files
committed
tensor images - improved the statistics section
1 parent cebdf5a commit 1c69007

1 file changed

Lines changed: 33 additions & 16 deletions

File tree

src/dtype_next/image_processing_with_tensors.clj

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
(:require [scicloj.kindly.v4.kind :as kind]
1616
[tech.v3.datatype :as dtype]
1717
[tech.v3.datatype.functional :as dfn]
18+
[tech.v3.datatype.statistics :as stats]
1819
[tech.v3.tensor :as tensor]
1920
[tech.v3.libs.buffered-image :as bufimg]
2021
[tech.v3.dataset.tensor :as ds-tensor]
@@ -567,31 +568,47 @@ flat-tensor
567568

568569
(dtype/shape blue-only)
569570

570-
;; ## Channel Statistics
571+
;; ## Statistical Operations
571572

572-
;; Compute mean, standard deviation, min, max, and percentiles for each channel:
573+
(require '[tech.v3.datatype.statistics :as stats])
574+
575+
;; The [`tech.v3.datatype.statistics`](https://cnuernber.github.io/dtype-next/tech.v3.datatype.statistics.html)
576+
;; namespace provides statistical functions optimized for typed arrays.
577+
;;
578+
;; Key function:
579+
;; - `stats/descriptive-statistics` — returns `:n-elems`, `:min`, `:max`, `:mean`, and `:standard-deviation`
580+
;;
581+
;; This is more efficient than calling individual functions like `dfn/mean`, `dfn/standard-deviation`, etc.,
582+
;; when you need multiple statistics, as it computes them in a single pass over the data.
583+
584+
;; ## Channel Statistics
573585

574586
(defn channel-stats
575587
"Compute statistics for a single channel tensor.
576588
Takes: [H W] tensor
577-
Returns: map with :mean, :std, :min, :max, percentiles"
589+
Returns: map with :mean, :standard-deviation, :min, :max, :n-elems"
590+
[channel]
591+
(stats/descriptive-statistics channel))
592+
593+
(defn channel-percentiles
594+
"Compute percentiles for a single channel tensor.
595+
Takes: [H W] tensor
596+
Returns: map with percentiles"
578597
[channel]
579-
(let [percentiles (dfn/percentiles channel [25 50 75])]
580-
{:mean (dfn/mean channel)
581-
:std (dfn/standard-deviation channel)
582-
:min (dfn/reduce-min channel)
583-
:max (dfn/reduce-max channel)
584-
:q25 (percentiles 0)
585-
:median (percentiles 1)
586-
:q75 (percentiles 2)}))
598+
(zipmap [:q25 :median :q75]
599+
(dfn/percentiles channel [25 50 75])))
587600

588601
;; Apply to our extracted channels:
589602

590-
(->> channels
591-
(map (fn [[k v]]
592-
(merge {:channel k}
593-
(channel-stats v))))
594-
tc/dataset)
603+
(-> (tc/dataset {:channel (keys channels)})
604+
(tc/add-columns (->> channels
605+
vals
606+
(map channel-stats)
607+
tc/dataset))
608+
(tc/add-columns (->> channels
609+
vals
610+
(map channel-percentiles)
611+
tc/dataset)))
595612

596613
;; ## Brightness Analysis
597614

0 commit comments

Comments
 (0)