|
15 | 15 | (:require [scicloj.kindly.v4.kind :as kind] |
16 | 16 | [tech.v3.datatype :as dtype] |
17 | 17 | [tech.v3.datatype.functional :as dfn] |
| 18 | + [tech.v3.datatype.statistics :as stats] |
18 | 19 | [tech.v3.tensor :as tensor] |
19 | 20 | [tech.v3.libs.buffered-image :as bufimg] |
20 | 21 | [tech.v3.dataset.tensor :as ds-tensor] |
@@ -567,31 +568,47 @@ flat-tensor |
567 | 568 |
|
568 | 569 | (dtype/shape blue-only) |
569 | 570 |
|
570 | | -;; ## Channel Statistics |
| 571 | +;; ## Statistical Operations |
571 | 572 |
|
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 |
573 | 585 |
|
574 | 586 | (defn channel-stats |
575 | 587 | "Compute statistics for a single channel tensor. |
576 | 588 | 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" |
578 | 597 | [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]))) |
587 | 600 |
|
588 | 601 | ;; Apply to our extracted channels: |
589 | 602 |
|
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))) |
595 | 612 |
|
596 | 613 | ;; ## Brightness Analysis |
597 | 614 |
|
|
0 commit comments