yirgacheffe 1.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of yirgacheffe might be problematic. Click here for more details.

Files changed (51) hide show
  1. yirgacheffe-1.2.0/LICENSE +7 -0
  2. yirgacheffe-1.2.0/MANIFEST.in +3 -0
  3. yirgacheffe-1.2.0/PKG-INFO +473 -0
  4. yirgacheffe-1.2.0/README.md +449 -0
  5. yirgacheffe-1.2.0/pyproject.toml +38 -0
  6. yirgacheffe-1.2.0/setup.cfg +4 -0
  7. yirgacheffe-1.2.0/tests/test_area.py +21 -0
  8. yirgacheffe-1.2.0/tests/test_auto_windowing.py +250 -0
  9. yirgacheffe-1.2.0/tests/test_base.py +196 -0
  10. yirgacheffe-1.2.0/tests/test_group.py +619 -0
  11. yirgacheffe-1.2.0/tests/test_h3layer.py +230 -0
  12. yirgacheffe-1.2.0/tests/test_intersection.py +191 -0
  13. yirgacheffe-1.2.0/tests/test_multiband.py +70 -0
  14. yirgacheffe-1.2.0/tests/test_operators.py +1306 -0
  15. yirgacheffe-1.2.0/tests/test_optimisation.py +160 -0
  16. yirgacheffe-1.2.0/tests/test_parallel_operators.py +255 -0
  17. yirgacheffe-1.2.0/tests/test_pickle.py +194 -0
  18. yirgacheffe-1.2.0/tests/test_raster.py +271 -0
  19. yirgacheffe-1.2.0/tests/test_rescaling.py +280 -0
  20. yirgacheffe-1.2.0/tests/test_rounding.py +88 -0
  21. yirgacheffe-1.2.0/tests/test_save_with_window.py +70 -0
  22. yirgacheffe-1.2.0/tests/test_sum_with_window.py +89 -0
  23. yirgacheffe-1.2.0/tests/test_uniform_area_layer.py +75 -0
  24. yirgacheffe-1.2.0/tests/test_union.py +136 -0
  25. yirgacheffe-1.2.0/tests/test_vectors.py +456 -0
  26. yirgacheffe-1.2.0/tests/test_window.py +89 -0
  27. yirgacheffe-1.2.0/yirgacheffe/__init__.py +17 -0
  28. yirgacheffe-1.2.0/yirgacheffe/backends/__init__.py +13 -0
  29. yirgacheffe-1.2.0/yirgacheffe/backends/enumeration.py +33 -0
  30. yirgacheffe-1.2.0/yirgacheffe/backends/mlx.py +156 -0
  31. yirgacheffe-1.2.0/yirgacheffe/backends/numpy.py +110 -0
  32. yirgacheffe-1.2.0/yirgacheffe/constants.py +1 -0
  33. yirgacheffe-1.2.0/yirgacheffe/h3layer.py +2 -0
  34. yirgacheffe-1.2.0/yirgacheffe/layers/__init__.py +44 -0
  35. yirgacheffe-1.2.0/yirgacheffe/layers/area.py +91 -0
  36. yirgacheffe-1.2.0/yirgacheffe/layers/base.py +265 -0
  37. yirgacheffe-1.2.0/yirgacheffe/layers/constant.py +41 -0
  38. yirgacheffe-1.2.0/yirgacheffe/layers/group.py +357 -0
  39. yirgacheffe-1.2.0/yirgacheffe/layers/h3layer.py +203 -0
  40. yirgacheffe-1.2.0/yirgacheffe/layers/rasters.py +333 -0
  41. yirgacheffe-1.2.0/yirgacheffe/layers/rescaled.py +94 -0
  42. yirgacheffe-1.2.0/yirgacheffe/layers/vectors.py +380 -0
  43. yirgacheffe-1.2.0/yirgacheffe/operators.py +738 -0
  44. yirgacheffe-1.2.0/yirgacheffe/rounding.py +57 -0
  45. yirgacheffe-1.2.0/yirgacheffe/window.py +141 -0
  46. yirgacheffe-1.2.0/yirgacheffe.egg-info/PKG-INFO +473 -0
  47. yirgacheffe-1.2.0/yirgacheffe.egg-info/SOURCES.txt +49 -0
  48. yirgacheffe-1.2.0/yirgacheffe.egg-info/dependency_links.txt +1 -0
  49. yirgacheffe-1.2.0/yirgacheffe.egg-info/entry_points.txt +2 -0
  50. yirgacheffe-1.2.0/yirgacheffe.egg-info/requires.txt +12 -0
  51. yirgacheffe-1.2.0/yirgacheffe.egg-info/top_level.txt +1 -0
@@ -0,0 +1,7 @@
1
+ ISC License
2
+
3
+ Copyright 2022 Michael Dales <mwd24@cam.ac.uk>
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
@@ -0,0 +1,3 @@
1
+ include LICENSE
2
+ include README.md
3
+ include pyproject.toml
@@ -0,0 +1,473 @@
1
+ Metadata-Version: 2.4
2
+ Name: yirgacheffe
3
+ Version: 1.2.0
4
+ Summary: Abstraction of gdal datasets for doing basic math operations
5
+ Author-email: Michael Dales <mwd24@cam.ac.uk>
6
+ License-Expression: ISC
7
+ Project-URL: Homepage, https://github.com/quantifyearth/yirgacheffe
8
+ Keywords: gdal,numpy,math
9
+ Requires-Python: >=3.10
10
+ Description-Content-Type: text/markdown
11
+ License-File: LICENSE
12
+ Requires-Dist: numpy
13
+ Requires-Dist: gdal[numpy]
14
+ Requires-Dist: scikit-image
15
+ Requires-Dist: torch
16
+ Provides-Extra: dev
17
+ Requires-Dist: mypy; extra == "dev"
18
+ Requires-Dist: pylint; extra == "dev"
19
+ Requires-Dist: pytest; extra == "dev"
20
+ Requires-Dist: h3; extra == "dev"
21
+ Requires-Dist: pytest-cov; extra == "dev"
22
+ Requires-Dist: mlx; extra == "dev"
23
+ Dynamic: license-file
24
+
25
+ # Yirgacheffe: a gdal wrapper that does the tricky bits
26
+
27
+ ## Overview
28
+
29
+ Yirgacheffe is an attempt to wrap raster and polygon geospatial datasets such that you can do computational work on them as a whole or at the pixel level, but without having to do a lot of the grunt work of working out where you need to be in rasters, or managing how much you can load into memory safely.
30
+
31
+ Example common use-cases:
32
+
33
+ * Do the datasets overlap? Yirgacheffe will let you define either the intersection or the union of a set of different datasets, scaling up or down the area as required.
34
+ * Rasterisation of vector layers: if you have a vector dataset then you can add that to your computation and yirgaceffe will rasterize it on demand, so you never need to store more data in memory than necessary.
35
+ * Do the raster layers get big and take up large amounts of memory? Yirgacheffe will let you do simple numerical operations with layers directly and then worry about the memory management behind the scenes for you.
36
+
37
+
38
+ ## Basic usage
39
+
40
+ They main unit of data in Yirgacheffe is a "layer", which wraps either a raster dataset or polygon data, and then you can do work on layers without having to worry (unless you choose to) about how they align - Yirgacheffe will work out all the details around overlapping
41
+
42
+ The motivation for Yirgacheffe layers is to make working with gdal data slightly easier - it just hides some common operations, such as incremental loading to save memory, or letting you align layers to generate either the intersection result or the union result.
43
+
44
+ For example, if we wanted to do a simple [Area of Habitat](https://github.com/quantifyearth/aoh-calculator/) calculation, whereby we find the pixels where a species resides by combining its range polygon, its habitat preferences, and its elevation preferences, the code would be like this:
45
+
46
+ ```python
47
+ from yirgacheffe.layer import RasterLayer, VectorLayer
48
+
49
+ habitat_map = RasterLayer.layer_from_file("habitats.tif")
50
+ elevation_map = RasterLayer.layer_from_file('elevation.tif')
51
+ range_polygon = VectorLayer.layer_from_file('species123.geojson', raster_like=habitat_map)
52
+ area_per_pixel_map = RasterLayer.layer_from_file('area_per_pixel.tif')
53
+
54
+ refined_habitat = habitat_map.isin([...species habitat codes...])
55
+ refined_elevation = (elevation_map >= species_min) && (elevation_map <= species_max)
56
+
57
+ aoh = refined_habitat * refined_elevation * range_polygon * area_per_pixel_map
58
+
59
+ print(f'area for species 123: {aoh.sum()}')
60
+ ```
61
+
62
+ Similarly, you could save the result to a new raster layer:
63
+
64
+ ```python
65
+ with RasterLayer.empty_raster_layer_like(aoh, filename="result.tif") as result:
66
+ aoh.save(result)
67
+ ```
68
+
69
+ Yirgacheffe will automatically infer if you want to do an intersection of maps or a union of the maps based on the operators you use (see below for a full table). You can explicitly override that if you want.
70
+
71
+ ### Lazy loading and evaluation
72
+
73
+ Yirgacheffe uses a technique from computer science called "lazy evaluation", which means that only when you resolve a calculation will Yirgacheffe do any work. So in the first code example given, the work is only calculated when you call the `sum()` method. All the other intermediary results such as `refined_habitat` and `refined_elevation` are not calculated either until that final `sum()` is called. You could easily call sum on those intermediaries if you wanted and get their results, and that would cause them to be evaluated then.
74
+
75
+ Similarly, when you load a layer, be it a raster layer or a vector layer from polygon data, the full data for the file isn't loaded until it's needed for a calculation, and even then only the part of the data necessary will be loaded or rasterized. Furthermore, Yirgacheffe will load the data in chunks, letting you work with rasters bigger than those that would otherwise fit within your computer's memory.
76
+
77
+
78
+ ### Automatic expanding and contracting layers
79
+
80
+ When you load raster layers that aren't of equal geographic area (that is, they have a different origin, size, or both)then Yirgacheffe will do all the math internally to ensure that it aligns the pixels geospatially when doing calculations.
81
+
82
+ If size adjustments are needed, then Yirgacheffe will infer from the calculations you're doing if it needs to either crop or enlarge layers. For instance, if you're summing two rasters it'll expand them to be the union of their two areas before adding them, filling in the missing parts with zeros. If you're multiplying or doing a logical AND of pixels then it'll find the intersection between the two rasters (as areas missing in one would cause the other layer to result in zero anyway).
83
+
84
+ Whilst it is hoped that the default behaviour makes sense in most cases, we can't anticipate all usages, and so if you want to be explicit about the result of any maps you can specify it yourself.
85
+
86
+ For example, to tell Yirgacheffe to make a union of a set of layers you'd write:
87
+
88
+ ```python
89
+ layers = [habitat_map, elevation_map, range_polygon]
90
+ union_area = YirgacheffeLayer.find_union(layers)
91
+ for layer in layers:
92
+ layer.set_window_for_union(union_area)
93
+ ```
94
+
95
+ There is a similar set of methods for using the intersection.
96
+
97
+ If you have set either the intersection window or union window on a layer and you wish to undo that restriction, then you can simply call `reset_window()` on the layer.
98
+
99
+ ### Direct access to data
100
+
101
+ If doing per-layer operations isn't applicable for your application, you can read the pixel values for all layers (including VectorLayers) by calling `read_array` similarly to how you would for gdal. The data you access will be relative to the specified window - that is, if you've called either `set_window_for_intersection` or `set_window_for_union` then `read_array` will be relative to that and Yirgacheffe will clip or expand the data with zero values as necessary.
102
+
103
+
104
+ ### Todo but not supported
105
+
106
+ Yirgacheffe is work in progress, so things planned but not supported currently:
107
+
108
+ * Dynamic pixel scale adjustment - all raster layers must be provided at the same pixel scale currently *NOW IN EXPERIMENTAL TESTING, SEE BELOW*
109
+ * A fold operation
110
+ * CUDA/Metal support via CUPY/MLX
111
+ * Dispatching work across multiple CPUs *NOW IN EXPERIMENTAL TESTING, SEE BELOW*
112
+
113
+
114
+
115
+ ## Layer types
116
+
117
+ ### RasterLayer
118
+
119
+ This is your basic GDAL raster layer, which you load from a geotiff.
120
+
121
+ ```python
122
+ with RasterLayer.layer_from_file('test1.tif') as layer:
123
+ data = layer.read_array(0, 0, 10, 10)
124
+ ...
125
+ ```
126
+
127
+ You can also create empty layers ready for you to store results, either by taking the dimensions from an existing layer. In both these cases you can either provide a filename to which the data will be written, or if you do not provide a filename then the layer will only exist in memory - this will be more efficient if the layer is being used for intermediary results.
128
+
129
+ ```python
130
+ with RasterLayer.empty_raster_layer_like(layer1, "results.tiff") as result:
131
+ ...
132
+ ```
133
+
134
+ Or you can specify the geographic area directly:
135
+
136
+ ```python
137
+ with RasterLayer.empty_raster_layer(
138
+ Area(left=-10.0, top=10.0, right=-5.0, bottom=5.0),
139
+ PixelScale(0.005,-0.005),
140
+ gdal.GDT_Float64,
141
+ "results.tiff"
142
+ ) as result:
143
+ ...
144
+ ```
145
+
146
+ You can also create a new layer that is a scaled version of an existing layer:
147
+
148
+ ```python
149
+ with RasterLayer.layer_from_file('test1.tif') as source:
150
+ scaled = RasterLayer.scaled_raster_from_raster(source, PixelScale(0.0001, -0.0001), 'scaled.tif')
151
+ ```
152
+
153
+
154
+ ### VectorLayer
155
+
156
+ This layer will load vector data and rasterize it on demand as part of a calculation - because it only rasterizes the data when needed, it is memory efficient.
157
+
158
+ Because it will be rasterized you need to specify the pixel scale and map projection to be used when rasterising the data, and the common way to do that is by using one of your other layers.
159
+
160
+ ```python
161
+ with VectorLayer.layer_from_file('range.gpkg', 'id_no == 42', layer1.pixel_scale, layer1.projection) as layer:
162
+ ...
163
+ ```
164
+
165
+ This class was formerly called `DynamicVectorRangeLayer`, a name now deprecated.
166
+
167
+
168
+ ### UniformAreaLayer
169
+
170
+ In certain calculations you find you have a layer where all the rows of data are the same - notably geotiffs that contain the area of a given pixel do this due to how conventional map projections work. It's hugely inefficient to load the full map into memory, so whilst you could just load them as `Layer` types, we recommend you do:
171
+
172
+ ```python
173
+ with UniformAreaLayer('area.tiff') as layer:
174
+ ....
175
+ ```
176
+
177
+ Note that loading this data can still be very slow, due to how image compression works. So if you plan to use area.tiff more than once, we recommend use save an optimised version - this will do the slow uncompression once and then save a minimal file to speed up future processing:
178
+
179
+ ```python
180
+ if not os.path.exists('yirgacheffe_area.tiff'):
181
+ UniformAreaLayer.generate_narrow_area_projection('area.tiff', 'yirgacheffe_area.tiff')
182
+ area_layer = UniformAreaLayer('yirgacheffe_area.tiff')
183
+ ```
184
+
185
+
186
+ ### ConstantLayer
187
+
188
+ This is there to simplify code when you have some optional layers. Rather than littering your code with checks, you can just use a constant layer, which can be included in calculations and will just return an fixed value as if it wasn't there. Useful with 0.0 or 1.0 for sum or multiplication null layers.
189
+
190
+ ```python
191
+ try:
192
+ area_layer = UniformAreaLayer('myarea.tiff')
193
+ except FileDoesNotExist:
194
+ area_layer = ConstantLayer(0.0)
195
+ ```
196
+
197
+
198
+ ### H3CellLayer
199
+
200
+ If you have H3 installed, you can generate a mask layer based on an H3 cell identifier, where pixels inside the cell will have a value of 1, and those outside will have a value of 0.
201
+
202
+ Becuase it will be rasterized you need to specify the pixel scale and map projection to be used when rasterising the data, and the common way to do that is by using one of your other layers.
203
+
204
+ ```python
205
+ hex_cell_layer = H3CellLayer('88972eac11fffff', layer1.pixel_scale, layer1.projection)
206
+ ```
207
+
208
+ ### GroupLayer
209
+
210
+ You can combine several layers into one virtual layer to save you worrying about how to merge them if you don't want to manually add the layers together. Useful when you have tile sets for example. Any area not covered by a layer in the group will return zeros.
211
+
212
+ ```python
213
+ tile1 = RasterLayer.layer_from_file('tile_N10_E10.tif')
214
+ tile2 = RasterLayer.layer_from_file('tile_N20_E10.tif')
215
+ all_tiles = GroupLayer([tile1, tile2])
216
+ ```
217
+
218
+ If you provide tiles that overlap then they will be rendered in reverse one, so in the above example if tile1 and tile2 overlap, then in that region you'd get the data from tile1.
219
+
220
+ To save you specifying each layer, there is a convenience method to let you just load a set of TIFs by filename:
221
+
222
+ ```python
223
+ with GroupLayer.layer_from_files(['tile_N10_E10.tif', 'tile_N20_E10.tif']) as all_tiles:
224
+ ...
225
+ ```
226
+
227
+ Or you can just specify a directory and it'll find the tifs in there (you can also add your own custom file filter too):
228
+
229
+ ```python
230
+ with GroupLayer.layer_from_directory('.') as all_tiles:
231
+ ...
232
+ ```
233
+
234
+ ### TiledGroupLayer
235
+
236
+ This is a specialisation of GroupLayer, which you can use if your layers are all the same size and form a grid, as is often the case with map tiles. In this case the rendering code can be optimised and this class is significantly faster that GroupLayer.
237
+
238
+ ```python
239
+ tile1 = RasterLayer.layer_from_file('tile_N10_E10.tif')
240
+ tile2 = RasterLayer.layer_from_file('tile_N20_E10.tif')
241
+ all_tiles = TiledGroupLayer([tile1, tile2])
242
+ ```
243
+
244
+ Notes:
245
+
246
+ * You can have missing tiles, and these will be filled in with zeros.
247
+ * You can have tiles that overlap, so long as they still conform to the rule that all tiles are the same size and on a grid.
248
+
249
+
250
+ ## Supported operations on layers
251
+
252
+ Once you have two layers, you can perform numerical analysis on them similar to how numpy works:
253
+
254
+ ### Add, subtract, multiple, divide
255
+
256
+ Pixel-wise addition, subtraction, multiplication or division (both true and floor division), and remainder. Either between arrays, or with constants:
257
+
258
+ ```python
259
+ with RasterLayer.layer_from_file('test1.tif') as layer1:
260
+ with RasterLayer.layer_from_file('test2.tif') as layer2:
261
+ with RasterLayer.empty_raster_layer_like(layer1, 'result.tif') as result:
262
+ calc = layer1 + layer2
263
+ calc.save(result)
264
+ ```
265
+
266
+ or
267
+
268
+ ```python
269
+ with RasterLayer.layer_from_file('test1.tif') as layer1:
270
+ with RasterLayer.empty_raster_layer_like(layer1, 'result.tif') as result:
271
+ calc = layer1 * 42.0
272
+ calc.save(result)
273
+ ```
274
+
275
+ ### Boolean testing
276
+
277
+ Testing for equality, less than, less than or equal, greater than, and greater than or equal are supported on layers, along with logical or and logical and, as per this example, where `elevation_upper` and `elevation_lower` are scalar values:
278
+
279
+ ```
280
+ filtered_elevation = (min_elevation_map <= elevation_upper) & (max_elevation_map >= elevation_lower)
281
+ ```
282
+
283
+ ### Power
284
+
285
+ Pixel-wise raising to a constant power:
286
+
287
+ ```python
288
+ with RasterLayer.layer_from_file('test1.tif') as layer1:
289
+ with RasterLayer.empty_raster_layer_like(layer1, 'result.tif') as result:
290
+ calc = layer1 ** 0.65
291
+ calc.save(result)
292
+ ```
293
+
294
+ ### Log, Exp, Clip, etc.
295
+
296
+ The following math operators common to numpy and other libraries are currently supported:
297
+
298
+ * clip
299
+ * exp
300
+ * exp2
301
+ * isin
302
+ * log
303
+ * log2
304
+ * log10
305
+ * maximum
306
+ * minimum
307
+ * nan_to_num
308
+
309
+ Typically these can be invoked either on a layer as a method:
310
+
311
+ ```python
312
+ calc = layer1.log10()
313
+ ```
314
+
315
+ Or via the operators module, as it's sometimes nicer to do it this way when chaining together operations in a single expression:
316
+
317
+ ```python
318
+ import yirgaceffe.operators as yo
319
+
320
+ calc = yo.log10(layer1 / layer2)
321
+ ```
322
+
323
+ ### 2D matrix convolution
324
+
325
+ To facilitate image processing algorithms you can supply a weight matrix to generate a processed image. Currently this support only works for square weight matrices of an odd size.
326
+
327
+ For example, to apply a blur function to a raster:
328
+
329
+ ```python
330
+ blur_filter = np.array([
331
+ [0.0, 0.1, 0.0],
332
+ [0.1, 0.6, 0.1],
333
+ [0.0, 0.1, 0.0],
334
+ ])
335
+ with RasterLayer.layer_from_file('original.tif') as layer1:
336
+ with RasterLayer.empty_raster_layer_like(layer1, 'blurred.tif') as result:
337
+ calc = layer1.conv2d(blur_filter)
338
+ calc.save(result)
339
+ ```
340
+
341
+ ### Apply
342
+
343
+ You can specify a function that takes either data from one layer or from two layers, and returns the processed data. There's two version of this: one that lets you specify a numpy function that'll be applied to the layer data as an array, or one that is more shader like that lets you do pixel wise processing.
344
+
345
+ Firstly the numpy version looks like this:
346
+
347
+ ```python
348
+ def is_over_ten(input_array):
349
+ return numpy.where(input_array > 10.0, 0.0, 1.0)
350
+
351
+ layer1 = RasterLayer.layer_from_file('test1.tif')
352
+ result = RasterLayer.empty_raster_layer_like(layer1, 'result.tif')
353
+
354
+ calc = layer1.numpy_apply(is_over_ten)
355
+
356
+ calc.save(result)
357
+ ```
358
+
359
+ or
360
+
361
+ ```python
362
+ def simple_add(first_array, second_array):
363
+ return first_array + second_array
364
+
365
+ layer1 = RasterLayer.layer_from_file('test1.tif')
366
+ layer2 = RasterLayer.layer_from_file('test2.tif')
367
+ result = RasterLayer.empty_raster_layer_like(layer1, 'result.tif')
368
+
369
+ calc = layer1.numpy_apply(simple_add, layer2)
370
+
371
+ calc.save(result)
372
+ ```
373
+
374
+ If you want to do something specific on the pixel level, then you can also do that, again either on a unary or binary form.
375
+
376
+ ```python
377
+ def is_over_ten(input_pixel):
378
+ return 1.0 if input_pixel > 10 else 0.0
379
+
380
+ layer1 = RasterLayer.layer_from_file('test1.tif')
381
+ result = RasterLayer.empty_raster_layer_like(layer1, 'result.tif')
382
+
383
+ calc = layer1.shader_apply(is_over_ten)
384
+
385
+ calc.save(result)
386
+ ```
387
+
388
+ Note that in general `numpy_apply` is considerably faster than `shader_apply`.
389
+
390
+ ## Getting an answer out
391
+
392
+ There are two ways to store the result of a computation. In all the above examples we use the `save` call, to which you pass a gdal dataset band, into which the results will be written. You can optionally pass a callback to save which will be called for each chunk of data processed and give you the amount of progress made so far as a number between 0.0 and 1.0:
393
+
394
+ ```python
395
+ def print_progress(p)
396
+ print(f"We have made {p * 100} percent progress")
397
+
398
+ ...
399
+
400
+ calc.save(result, callback=print_progress)
401
+ ```
402
+
403
+
404
+ The alternative is to call `sum` which will give you a total:
405
+
406
+ ```python
407
+ with (
408
+ RasterLayer.layer_from_file(...) as area_layer,
409
+ VectorLayer(...) as mask_layer
410
+ ):
411
+ intersection = RasterLayer.find_intersection([area_layer, mask_layer])
412
+ area_layer.set_intersection_window(intersection)
413
+ mask_layer.set_intersection_window(intersection)
414
+
415
+ calc = area_layer * mask_layer
416
+
417
+ total_area = calc.sum()
418
+ ```
419
+
420
+ Similar to sum, you can also call `min` and `max` on a layer or calculation.
421
+
422
+ ## Experimental
423
+
424
+ The following features are considered experimental - they have test cases to show them working in limited circumstances, but they've not yet been tested on a wide range of use cases. We hope that you will try them out and let us know how they work out.
425
+
426
+ ### RescaledRasterLayer
427
+
428
+ The RescaledRasterLayer will take a GeoTIFF and do on demand rescaling in memory to get the layer to match other layers you're working on.
429
+
430
+ ```python
431
+ with RasterLayer.layer_from_file("high_density_file.tif") as high_density:
432
+ with RescaledRasterLayer.layer_from_file("low_density_file.tif", high_density.pixel_scale) as matched_density:
433
+
434
+ # Normally this next line would fail with two RasterLayers as they ahve a different pixel density
435
+ intersection = RasterLayer.find_intersection([high_density, matched_density])
436
+ high_density.set_intersection_window(intersection)
437
+ matched_density.set_intersection_window(intersection)
438
+
439
+ calc = high_density * matched_density
440
+ total = calc.sum()
441
+
442
+ ```
443
+
444
+ ### Parallel saving
445
+
446
+ There is a parallel version of save that can use multiple CPU cores at once to speed up work, that is added as an experimental feature for testing in our wider codebase, which will run concurrently the save over many threads.
447
+
448
+ ```python
449
+ calc.parallel_save(result)
450
+ ```
451
+
452
+ By default it will use as many CPU cores as are available, but if you want to limit that you can pass an extra argument to constrain that:
453
+
454
+ ```python
455
+ calc.parallel_save(result, parallelism=4)
456
+ ```
457
+
458
+ Because of the number of tricks that Python plays under the hood this feature needs a bunch of testing to let us remove the experimental flag, but in order to get that testing we need to put it out there! Hopefully in the next release we can remove the experimental warning.
459
+
460
+ ## GPU support
461
+
462
+ Yirgacheffe has multiple backends, with more planned. Currently you can set the `YIRGACHEFFE_BACKEND` environmental variable to select which one to use. The default is `NUMPY`:
463
+
464
+ * NUMPY: CPU based calculation using [numpy](https://numpy.org/)
465
+ * MLX: Apple/Intel GPU support with CPU fallback based on [MLX](https://ml-explore.github.io/mlx/build/html/index.html)
466
+
467
+ Note that GPU isn't always faster than CPU - it very much depends on the workload, so testing your particular use-case is important.
468
+
469
+ ## Thanks
470
+
471
+ Thanks to discussion and feedback from my colleagues, particularly Alison Eyres, Patrick Ferris, Amelia Holcomb, and Anil Madhavapeddy.
472
+
473
+ Inspired by the work of Daniele Baisero in his AoH library.