Skip to content

Commit d8986e6

Browse files
authored
Merge pull request #22 from ai2es/code_refactor
Code refactor
2 parents 296d8b4 + de6a3b0 commit d8986e6

26 files changed

+11552
-2459
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,12 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
# Generated figures
132+
133+
media/OUTPUT_IMME
134+
notebooks/experiments_Imme_Apr_4.ipynb
135+
notebooks/experiments_Imme_July_5_2024.ipynb
136+
notebooks/experiments_imme_v2_active_for_Lander.ipynb
137+
notebooks/time_experiment.ipynb
138+
.vscode/settings.json

README.md

Lines changed: 5 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# ai2es-sharpness
2-
This is a repository containing implementations and utility functions for a variety of metrics that can be used to analyze the sharpness of meteorological image, as well as transform functions and a selection of synthetic and real data for use in examples. This is work conducted under the umbrella of [The NSF AI Institute for Research on Trustworthy AI in Weather, Climate, and Coastal Oceanography (AI2ES)](https://www.ai2es.org/). To find out more about the metrics included and how they can be used, see the [accompanying paper](https://www.ai2es.org/sharpness/), and if you make use of this repository, please cite that paper.
2+
This is a repository containing implementations and utility functions for a variety of metrics that can be used to analyze the sharpness of meteorological image, as well as transform functions and a selection of synthetic and real data for use in examples. This is work conducted under the umbrella of [The NSF AI Institute for Research on Trustworthy AI in Weather, Climate, and Coastal Oceanography (AI2ES)](https://www.ai2es.org/). To find out more about the metrics included and how they can be used, see the [accompanying preprint](https://www.ai2es.org/sharpness/), and if you make use of this repository, please cite that paper. A peer-reviewed version is expected soon.
33

44
## Installation
55

@@ -11,118 +11,14 @@ To install with the specific versions used in the creation of this package, use
1111

1212
## Examples of use
1313

14-
There are a number of example notebooks in the [notebooks](/notebooks) folder, which serve as a set of examples for utilizing the low-level interface of this package. For a simple introduction to how to run experiments using transforms, see [experiment_demo.ipynb](/notebooks/experiment_demo.ipynb), and for more in-depth experiments see [experiments_imme_v2.ipynb](/notebooks/experiments_imme_v2.ipynb).
14+
There are a number of example notebooks in the [notebooks](/notebooks) folder, which serve as a set of examples for utilizing the low-level interface of this package. For a simple introduction to how to run experiments using transforms, see [experiment_demo.ipynb](/notebooks/experiment_demo.ipynb), and for more in-depth experiments (including all those described in the accompanying paper,) see [paper_experiments.ipynb](/notebooks/paper_experiments.ipynb).
1515

1616
## Python interface
1717

18-
The principal utilities offered by this package can be found in [`src/__init__.py`](/src/__init__.py), and are the functions `compute_all_metrics_globally`, `compute_metric_globally`, `compute_all_metrics_locally`, `compute_metric_locally`, all of which take in a pair of images `X` and `T` and either compute all metrics, or just a specified single metric. In either case, computing "globally" means that the metric will be applied to the whole image (or pair of images) and a single number will be returned, while computing "locally" means that a heatmap of local metric values will be computed.
19-
20-
## Benchmark
21-
22-
This is a command line interface to compute and plot evaluations from different metrics and transformations on real or synthetic datasets.
18+
The principal utilities offered by this package can be found in [`src/sharpness/high_level_functions.py`](/src/sharpness/high_level_functions.py) and made available as a base-level import with the sharpness package. These are the functions `compute_all_metrics_globally`, `compute_metric_globally`, `compute_all_metrics_locally`, `compute_metric_locally`, all of which take in a pair of images `X` and `T` and either compute all metrics, or just a specified single metric. In either case, computing "globally" means that the metric will be applied to the whole image (or pair of images) and a single number will be returned, while computing "locally" means that a heatmap of local metric values will be computed.
2319

2420
#### Input requirements
2521

26-
At this point, all functions (including the `benchmark.py` script) take as their input a single `n x m` grayscale image. If using these methods on multi-channel imagery, either convert the image to grayscale (if it is an RGB image) or loop over the channels and compute sharpness statistics individually.
27-
28-
Input data should be of float type; if the data are not already floats, certain metrics which require float type inputs will internally convert the data to floats.
29-
30-
#### Example CLI Usage
31-
32-
From within the `src` directory:
33-
34-
```bash
35-
$ python benchmark.py -h
36-
usage: benchmark.py [-h] [-s {sinusoidal,gaussian,bw,xor}] [-i INPUT] [-t {vflip,hflip,blur,noise,brightness,crop}] [-m {all,mse,mae,rmse,grad}] [--visualize] [-o OUTPUT]
37-
38-
Sharpness Benchmarks
39-
40-
optional arguments:
41-
-h, --help show this help message and exit
42-
-s {sinusoidal,gaussian,bw,xor}, --synthetic {sinusoidal,gaussian,bw,xor}
43-
generate synthetic data
44-
-i INPUT, --input INPUT
45-
name of input file to load data from
46-
-t {vflip,hflip,blur,noise,brightness,crop}, --transformation {vflip,hflip,blur,noise,brightness,crop}
47-
transformation to perform on data
48-
-m {all,mse,mae,rmse,grad}, --metric {all,mse,mae,rmse,grad,s1,psnr,ncc,grad-ds,grad-rmse,laplace-rmse,hist-int,hog-pearson,fourier-similarity,wavelet-similarity,tv,grad-tv,fourier-tv,wavelet-tv}
49-
evaluation metric to compute
50-
--heatmap compute sharpness heatmap(s) rather than global metric
51-
--visualize visualize and save the operations
52-
--overlay only relevant if both heatmap and visualize are true; plots heatmaps on top of input data
53-
-o OUTPUT, --output OUTPUT
54-
name of output file visualization
55-
```
56-
57-
Note that with the `--heatmap` option, each metric will be computed on small, overlapping tiles across the image; by default, these tiles will be square with side length equal to ~1/8th the width of the input image, and the stride for these tiles will be 1/4 the side length of the tile. The image will also be padded using the "reflect" method by a number of pixels equal to ~1/16th the width of the input image. These parameters are adjustable by editing the appropriate function in `__init__.py`.
58-
59-
#### Examples
60-
61-
Generate synthetic data, apply a blurring transformation, compute all metrics, and visualize/save the output.
62-
63-
```bash
64-
$ python benchmark.py -s xor -t blur -m all --visualize -o ../media/synthetic.png
65-
=> mse: 150.1562378666429
66-
=> mae: 7.141086141494917
67-
=> rmse: 12.25382543806802
68-
=> s1: (0.00339528769955455, 1.7763568394002505e-15)
69-
=> psnr: 26.36536982276365
70-
=> ncc: 0.9965476066368607
71-
=> mgm: (44.19561294970306, 29.452084668360147)
72-
=> grad-ds: 0.49620562145179087
73-
=> grad-rmse: 71.91002205102615
74-
=> laplace-rmse: 26.700794614133223
75-
=> hist-int: 0.6448973445108177
76-
=> hog-pearson: 0.5533226275817607
77-
=> fourier-rmse: 278977716.3719768
78-
=> wavelet-similarity: 0.485842832185395
79-
=> tv: (524288.0, 272784.50942777185)
80-
=> grad-tv: (3153888.0, 2149503.1447823923)
81-
=> fourier-tv: (120947473.46739776, 120612974.58877043)
82-
=> wavelet-tv: (4194304.0, 4248385.269167363)
83-
```
84-
![](media/synthetic.png)
85-
86-
We can re-run the above example, but with local computations of heatmaps overlaid on top of input data instead of global metrics.
87-
88-
```bash
89-
$ python benchmark.py -s xor -t blur -m all --heatmap --visualize --overlay -o ../media/synthetic_heatmaps.png
90-
Heatmap will be computed with blocks of size 32, and has image padding of length 16
91-
=> mse average: 39.881929874420166
92-
=> mae average: 111.61909246444702
93-
=> rmse average: 6.259764454401335
94-
=> s1 averages: (2.7966713163144616, 4.416437443298626)
95-
=> psnr average: 27.90985194547143
96-
=> ncc average: 1.0957310987654965
97-
=> grad averages: (0.025264954381721993, 0.019396214612508057)
98-
=> grad-ds average: 0.4854998901219907
99-
=> grad-rmse average: 54.209045009739974
100-
=> laplace-rmse average: 20.67599301023201
101-
=> hist-int average: 0.5660052760923319
102-
=> hog-pearson average: 0.6746634025245817
103-
=> fourier-similarity average: 1.0
104-
=> wavelet-similarity average: 0.3293994978185485
105-
=> tv averages: (253952.0, 176779.0)
106-
=> grad-tv averages: (40465.7734375, 30567.400390625)
107-
=> fourier-tv averages: (358239.7522150265, 318628.08900317416)
108-
=> wavelet-tv averages: (65607.43359375, 65929.7275390625)
109-
```
110-
![](media/synthetic_heatmaps.png)
111-
112-
Load the default data example, apply a vertical transformation, compute only the root-mean-square error, and visualize/save the output to the default name.
113-
114-
```bash
115-
$ python benchmark.py -t vflip -m rmse --visualize
116-
Loading data from ../data/kh_ABI_C13.nc (sample 0)
117-
=> rmse: 10.005649078875036
118-
```
119-
![](media/output.png)
120-
121-
Generate synthetic data again, but only compute total variation as a heatmap.
122-
```bash
123-
$ python benchmark.py -s='xor' -t='blur' -m='tv' -o='../media/synth_tv.png' --heatmap --visualize
124-
Heatmap will be computed with blocks of size 32, and has image padding of length 16
125-
=> tv averages: (253952.0, 176779.0)
126-
```
22+
All functions take as their input a single `n x m` grayscale image. If using these methods on multi-channel imagery, either convert the image to grayscale (if it is an RGB image) or loop over the channels and compute sharpness statistics individually.
12723

128-
![](media/synth_tv.png)
24+
Input data should be of float type; if the data are not already floats, certain metrics which require float type inputs will internally convert the data to floats.

0 commit comments

Comments
 (0)