|
| 1 | +# Copyright Iris contributors |
| 2 | +# |
| 3 | +# This file is part of Iris and is released under the BSD license. |
| 4 | +# See LICENSE in the root of the repository for full licensing details. |
| 5 | +"""Benchmarks relating to :meth:`iris.cube.CubeList.merge` and ``concatenate``.""" |
| 6 | + |
| 7 | +import warnings |
| 8 | + |
| 9 | +import numpy as np |
| 10 | + |
| 11 | +from iris import analysis, coords, cube |
| 12 | +from iris.warnings import IrisVagueMetadataWarning |
| 13 | + |
| 14 | +from .generate_data.stock import realistic_4d_w_everything |
| 15 | + |
| 16 | + |
| 17 | +class AggregationMixin: |
| 18 | + params = [[False, True]] |
| 19 | + param_names = ["Lazy operations"] |
| 20 | + |
| 21 | + def setup(self, lazy_run: bool): |
| 22 | + warnings.filterwarnings("ignore", message="Ignoring a datum") |
| 23 | + warnings.filterwarnings("ignore", category=IrisVagueMetadataWarning) |
| 24 | + cube = realistic_4d_w_everything(lazy=lazy_run) |
| 25 | + |
| 26 | + for cm in cube.cell_measures(): |
| 27 | + cube.remove_cell_measure(cm) |
| 28 | + for av in cube.ancillary_variables(): |
| 29 | + cube.remove_ancillary_variable(av) |
| 30 | + |
| 31 | + agg_mln_data = np.arange(0, 70, 10) |
| 32 | + agg_mln_repeat = np.repeat(agg_mln_data, 10) |
| 33 | + |
| 34 | + cube = cube[..., :10, :10] |
| 35 | + |
| 36 | + self.mln_aux = "aggregatable" |
| 37 | + self.mln = "model_level_number" |
| 38 | + agg_mln_coord = coords.AuxCoord(points=agg_mln_repeat, long_name=self.mln_aux) |
| 39 | + |
| 40 | + if lazy_run: |
| 41 | + agg_mln_coord.points = agg_mln_coord.lazy_points() |
| 42 | + cube.add_aux_coord(agg_mln_coord, 1) |
| 43 | + self.cube = cube |
| 44 | + |
| 45 | + |
| 46 | +class Aggregation(AggregationMixin): |
| 47 | + def time_aggregated_by_MEAN(self, _): |
| 48 | + _ = self.cube.aggregated_by(self.mln_aux, analysis.MEAN).data |
| 49 | + |
| 50 | + def time_aggregated_by_COUNT(self, _): |
| 51 | + _ = self.cube.aggregated_by( |
| 52 | + self.mln_aux, analysis.COUNT, function=lambda values: values > 280 |
| 53 | + ).data |
| 54 | + |
| 55 | + def time_aggregated_by_GMEAN(self, _): |
| 56 | + _ = self.cube.aggregated_by(self.mln_aux, analysis.GMEAN).data |
| 57 | + |
| 58 | + def time_aggregated_by_HMEAN(self, _): |
| 59 | + _ = self.cube.aggregated_by(self.mln_aux, analysis.HMEAN).data |
| 60 | + |
| 61 | + def time_aggregated_by_MAX_RUN(self, _): |
| 62 | + _ = self.cube.aggregated_by( |
| 63 | + self.mln_aux, analysis.MAX_RUN, function=lambda values: values > 280 |
| 64 | + ).data |
| 65 | + |
| 66 | + def time_aggregated_by_MAX(self, _): |
| 67 | + _ = self.cube.aggregated_by(self.mln_aux, analysis.MAX).data |
| 68 | + |
| 69 | + def time_aggregated_by_MEDIAN(self, _): |
| 70 | + _ = self.cube.aggregated_by(self.mln_aux, analysis.MEDIAN).data |
| 71 | + |
| 72 | + def time_aggregated_by_MIN(self, _): |
| 73 | + _ = self.cube.aggregated_by(self.mln_aux, analysis.MIN).data |
| 74 | + |
| 75 | + def time_aggregated_by_PEAK(self, _): |
| 76 | + _ = self.cube.aggregated_by(self.mln_aux, analysis.PEAK).data |
| 77 | + |
| 78 | + def time_aggregated_by_PERCENTILE(self, _): |
| 79 | + _ = self.cube.aggregated_by( |
| 80 | + self.mln_aux, analysis.PERCENTILE, percent=[10, 50, 90] |
| 81 | + ).data |
| 82 | + |
| 83 | + def time_aggregated_by_FAST_PERCENTILE(self, _): |
| 84 | + _ = self.cube.aggregated_by( |
| 85 | + self.mln_aux, |
| 86 | + analysis.PERCENTILE, |
| 87 | + mdtol=0, |
| 88 | + percent=[10, 50, 90], |
| 89 | + fast_percentile_method=True, |
| 90 | + ).data |
| 91 | + |
| 92 | + def time_aggregated_by_PROPORTION(self, _): |
| 93 | + _ = self.cube.aggregated_by( |
| 94 | + self.mln_aux, |
| 95 | + analysis.PROPORTION, |
| 96 | + function=lambda values: values > 280, |
| 97 | + ).data |
| 98 | + |
| 99 | + def time_aggregated_by_STD_DEV(self, _): |
| 100 | + _ = self.cube.aggregated_by(self.mln_aux, analysis.STD_DEV).data |
| 101 | + |
| 102 | + def time_aggregated_by_VARIANCE(self, _): |
| 103 | + _ = self.cube.aggregated_by(self.mln_aux, analysis.VARIANCE).data |
| 104 | + |
| 105 | + def time_aggregated_by_RMS(self, _): |
| 106 | + _ = self.cube.aggregated_by(self.mln_aux, analysis.RMS).data |
| 107 | + |
| 108 | + def time_collapsed_by_MEAN(self, _): |
| 109 | + _ = self.cube.collapsed(self.mln, analysis.MEAN).data |
| 110 | + |
| 111 | + def time_collapsed_by_COUNT(self, _): |
| 112 | + _ = self.cube.collapsed( |
| 113 | + self.mln, analysis.COUNT, function=lambda values: values > 280 |
| 114 | + ).data |
| 115 | + |
| 116 | + def time_collapsed_by_GMEAN(self, _): |
| 117 | + _ = self.cube.collapsed(self.mln, analysis.GMEAN).data |
| 118 | + |
| 119 | + def time_collapsed_by_HMEAN(self, _): |
| 120 | + _ = self.cube.collapsed(self.mln, analysis.HMEAN).data |
| 121 | + |
| 122 | + def time_collapsed_by_MAX_RUN(self, _): |
| 123 | + _ = self.cube.collapsed( |
| 124 | + self.mln, analysis.MAX_RUN, function=lambda values: values > 280 |
| 125 | + ).data |
| 126 | + |
| 127 | + def time_collapsed_by_MAX(self, _): |
| 128 | + _ = self.cube.collapsed(self.mln, analysis.MAX).data |
| 129 | + |
| 130 | + def time_collapsed_by_MEDIAN(self, _): |
| 131 | + _ = self.cube.collapsed(self.mln, analysis.MEDIAN).data |
| 132 | + |
| 133 | + def time_collapsed_by_MIN(self, _): |
| 134 | + _ = self.cube.collapsed(self.mln, analysis.MIN).data |
| 135 | + |
| 136 | + def time_collapsed_by_PEAK(self, _): |
| 137 | + _ = self.cube.collapsed(self.mln, analysis.PEAK).data |
| 138 | + |
| 139 | + def time_collapsed_by_PERCENTILE(self, _): |
| 140 | + _ = self.cube.collapsed( |
| 141 | + self.mln, analysis.PERCENTILE, percent=[10, 50, 90] |
| 142 | + ).data |
| 143 | + |
| 144 | + def time_collapsed_by_FAST_PERCENTILE(self, _): |
| 145 | + _ = self.cube.collapsed( |
| 146 | + self.mln, |
| 147 | + analysis.PERCENTILE, |
| 148 | + mdtol=0, |
| 149 | + percent=[10, 50, 90], |
| 150 | + fast_percentile_method=True, |
| 151 | + ).data |
| 152 | + |
| 153 | + def time_collapsed_by_PROPORTION(self, _): |
| 154 | + _ = self.cube.collapsed( |
| 155 | + self.mln, analysis.PROPORTION, function=lambda values: values > 280 |
| 156 | + ).data |
| 157 | + |
| 158 | + def time_collapsed_by_STD_DEV(self, _): |
| 159 | + _ = self.cube.collapsed(self.mln, analysis.STD_DEV).data |
| 160 | + |
| 161 | + def time_collapsed_by_VARIANCE(self, _): |
| 162 | + _ = self.cube.collapsed(self.mln, analysis.VARIANCE).data |
| 163 | + |
| 164 | + def time_collapsed_by_RMS(self, _): |
| 165 | + _ = self.cube.collapsed(self.mln, analysis.RMS).data |
| 166 | + |
| 167 | + |
| 168 | +class WeightedAggregation(AggregationMixin): |
| 169 | + def setup(self, lazy_run): |
| 170 | + super().setup(lazy_run) |
| 171 | + |
| 172 | + weights = np.linspace(0, 1, 70) |
| 173 | + weights = np.broadcast_to(weights, self.cube.shape[:2]) |
| 174 | + weights = np.broadcast_to(weights.T, self.cube.shape[::-1]) |
| 175 | + weights = weights.T |
| 176 | + |
| 177 | + self.weights = weights |
| 178 | + |
| 179 | + ## currently has problems with indexing weights |
| 180 | + # def time_w_aggregated_by_WPERCENTILE(self, _): |
| 181 | + # _ = self.cube.aggregated_by( |
| 182 | + # self.mln_aux, analysis.WPERCENTILE, weights=self.weights, percent=[10, 50, 90] |
| 183 | + # ).data |
| 184 | + |
| 185 | + def time_w_aggregated_by_SUM(self, _): |
| 186 | + _ = self.cube.aggregated_by( |
| 187 | + self.mln_aux, analysis.SUM, weights=self.weights |
| 188 | + ).data |
| 189 | + |
| 190 | + def time_w_aggregated_by_RMS(self, _): |
| 191 | + _ = self.cube.aggregated_by( |
| 192 | + self.mln_aux, analysis.RMS, weights=self.weights |
| 193 | + ).data |
| 194 | + |
| 195 | + def time_w_aggregated_by_MEAN(self, _): |
| 196 | + _ = self.cube.aggregated_by( |
| 197 | + self.mln_aux, analysis.MEAN, weights=self.weights |
| 198 | + ).data |
| 199 | + |
| 200 | + def time_w_collapsed_by_WPERCENTILE(self, _): |
| 201 | + _ = self.cube.collapsed( |
| 202 | + self.mln, analysis.WPERCENTILE, weights=self.weights, percent=[10, 50, 90] |
| 203 | + ).data |
| 204 | + |
| 205 | + def time_w_collapsed_by_SUM(self, _): |
| 206 | + _ = self.cube.collapsed(self.mln, analysis.SUM, weights=self.weights).data |
| 207 | + |
| 208 | + def time_w_collapsed_by_RMS(self, _): |
| 209 | + _ = self.cube.collapsed(self.mln, analysis.RMS, weights=self.weights).data |
| 210 | + |
| 211 | + def time_w_collapsed_by_MEAN(self, _): |
| 212 | + _ = self.cube.collapsed(self.mln, analysis.MEAN, weights=self.weights).data |
0 commit comments