-
Notifications
You must be signed in to change notification settings - Fork 3
/
slides.html
690 lines (571 loc) · 18.4 KB
/
slides.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
<!DOCTYPE html>
<html>
<head>
<title>Xarray (EGU 2017 PICO)</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="static/css/fonts.css"/>
<link rel="stylesheet" type="text/css" href="static/css/pico_slides_remark.css"/>
</head>
<body>
<textarea id="source">
layout: true
<div class="header-wrap">
<div class='logo-left'><img src="figs/xarray-logo-notext.png" width="65px"/></div>
<div class='title-warp'>
<div class='title'>xarray: multi-dimensional data analysis in Python</div>
<div class='authors'>Stephan Hoyer (1), Joe Hamman (2), Fabien Maussion (3) and Benoît Bovy (4)</div>
<div class='affiliations'>
(1) Google Research, Mountain View, CA, USA
(2) NCAR, Boulder, CO, USA
(3) University of Innsbruck, Austria
(4) GFZ Potsdam, Germany
</div>
</div>
<div class='logo-right'></div>
</div>
<div class="footer-logos">
<span>EGU Vienna 04-2017 (PICO)</span>
<img src="figs/cc-by-license.png" width="60px"/>
</div>
<div class="footer-wrap">
<ul>
<li class='current-nav'>summary</li>
<li>data structures</li>
<li>data analytics</li>
<li>plotting</li>
<li>big data</li>
<li>more features</li>
<li>infos</li>
</ul>
</div>
---
# What is xarray?
.orange[For Python / Numpy users]
- xarray handles N-dimensional arrays with labels (dimension names &
coordinates) and metadata.
.orange[For Python / Pandas users]
- xarray is a powerful, pandas-like toolkit for analytics on
multi-dimensional arrays.
.orange[For scientists familar with the netCDF format]
- xarray implements the netCDF data model with a high level Python API.
.orange[For scientists working with big datasets]
- xarray (with dask) supports efficient, out-of-core computing for datasets
that don't fit in memory.
---
# What is xarray?
If you are dealing with data that...
- is multi-dimensional ;
- is labelled ;
- has (lots of) metadata ;
- is sometimes (very) large ;
<img src="figs/dataset.png" style="margin: 20px 90px;" width="600px"/>
...then you may find xarray very useful!
---
# xarray
- Open source
- Very good integration with other Python libraries for scientific
computing (SciPy / PyData Stack)
- Extensible
- Documentation: http://xarray.pydata.org
- Repository: https://github.com/pydata/xarray
- 60 contributors (still growing)
- Latest release: v0.9.5 (17.04.2017)
- Umbrellas (no funding): Python for Data & NumFOCUS
<img src="figs/pydata-logo.png" style="margin-left: 45px;" width="150px"/>
<img src="figs/numfocus-logo.png" style="margin-left: 45px;" width="150px"/>
---
layout: true
<div class="header-wrap">
<div class='logo-left'><img src="figs/xarray-logo-notext.png" width="65px"/></div>
<div class='title-warp'>
<div class='title'>xarray: multi-dimensional data analysis in Python</div>
<div class='authors'>Stephan Hoyer (1), Joe Hamman (2), Fabien Maussion (3) and Benoît Bovy (4)</div>
<div class='affiliations'>
(1) Google Research, Mountain View, CA, USA
(2) NCAR, Boulder, CO, USA
(3) University of Innsbruck, Austria
(4) GFZ Potsdam, Germany
</div>
</div>
<div class='logo-right'></div>
</div>
<div class="footer-logos">
<span>EGU Vienna 04-2017 (PICO)</span>
<img src="figs/cc-by-license.png" width="60px"/>
</div>
<div class="footer-wrap">
<ul>
<li>summary</li>
<li class='current-nav'>data structures</li>
<li>data analytics</li>
<li>plotting</li>
<li>big data</li>
<li>more features</li>
<li>infos</li>
</ul>
</div>
---
# numpy.array
```python
>>> import numpy as np
>>> a = np.array([[1, 3, 9], [2, 8, 4]])
>>> a
```
```
array([[1, 3, 9],
[2, 8, 4]])
```
```python
>>> a[1, 2] # get the value at 2nd row and 3rd column
```
```
4
```
```python
>>> a.mean(axis=0) # compute the mean column-wise
```
```
array([1.5, 5.5, 6.5])
```
<br/>
Not well supported by numpy:
- array dimensions and indexes often have a meaning, e.g., latitude /
longitude and their coordinates.
---
# xarray.DataArray
```python
>>> import xarray as xr
>>> da = xr.DataArray(a, dims=['latitude', 'longitude'],
coords={'longitude': [11, 12, 13], 'latitude': [1, 2]})
>>> da
```
```
<xarray.DataArray (latitude: 2, longitude: 3)>
array([[1, 3, 9],
[2, 8, 4]])
Coordinates:
* longitude (longitude) int64 11 12 13
* latitude (latitude) int64 1 2
```
```python
>>> da.sel(longitude=13, latitude=2) # easier to work with coordinate values!
```
```
<xarray.DataArray ()>
array(4)
Coordinates:
longitude int64 13
latitude int64 2
```
```python
>>> da.mean(dim='latitude') # easier to remember dimension names!
```
```
<xarray.DataArray (longitude: 3)>
array([ 1.5, 5.5, 6.5])
Coordinates:
* longitude (longitude) int64 11 12 13
```
---
# xarray.Dataset
A collection of `xarray.DataArray`, a netCDF file...
```python
>>> ds = xr.open_dataset('ERA-Interim-MonthlyAvg-TUVP.nc')
>>> ds
```
```
<xarray.Dataset>
Dimensions: (latitude: 241, longitude: 480, time: 457)
Coordinates:
* longitude (longitude) float32 0.0 0.75 1.5 2.25 3.0 3.75 4.5 5.25 6.0 ...
* latitude (latitude) float32 90.0 89.25 88.5 87.75 87.0 86.25 85.5 ...
* time (time) datetime64[ns] 1979-01-01 1979-02-01 1979-03-01 ...
Data variables:
sp (time, latitude, longitude) float64 1.028e+05 1.028e+05 ...
u10 (time, latitude, longitude) float64 -1.857 -1.854 -1.851 ...
v10 (time, latitude, longitude) float64 -0.3266 -0.3056 -0.285 ...
t2m (time, latitude, longitude) float64 242.7 242.7 242.7 242.7 ...
Attributes:
Conventions: CF-1.6
history: 2017-04-19 16:02:16 GMT by grib_to_netcdf-2.1.0: grib_to_ne...
```
---
layout: true
<div class="header-wrap">
<div class='logo-left'><img src="figs/xarray-logo-notext.png" width="65px"/></div>
<div class='title-warp'>
<div class='title'>xarray: multi-dimensional data analysis in Python</div>
<div class='authors'>Stephan Hoyer (1), Joe Hamman (2), Fabien Maussion (3) and Benoît Bovy (4)</div>
<div class='affiliations'>
(1) Google Research, Mountain View, CA, USA
(2) NCAR, Boulder, CO, USA
(3) University of Innsbruck, Austria
(4) GFZ Potsdam, Germany
</div>
</div>
<div class='logo-right'></div>
</div>
<div class="footer-logos">
<span>EGU Vienna 04-2017 (PICO)</span>
<img src="figs/cc-by-license.png" width="60px"/>
</div>
<div class="footer-wrap">
<ul>
<li>summary</li>
<li>data structures</li>
<li class='current-nav'>data analytics</li>
<li>plotting</li>
<li>big data</li>
<li>more features</li>
<li>infos</li>
</ul>
</div>
---
# Analytics
- Advanced selection
```python
>>> # nearest neighbor lookup: no need to provide exact coordinate values
>>> ds.sel(latitude=47.2876, longitude=11.3788, method='nearest')
```
```
<xarray.Dataset>
Dimensions: (time: 457)
Coordinates:
longitude float32 11.25
latitude float32 47.25
* time (time) datetime64[ns] 1979-01-01 1979-02-01 1979-03-01 ...
Data variables:
sp (time) float64 8.419e+04 8.41e+04 8.422e+04 8.441e+04 ...
u10 (time) float64 0.8787 0.1095 0.7023 0.3623 0.3087 0.2992 ...
v10 (time) float64 0.6971 0.5583 1.079 -0.04204 0.6298 -0.2448 ...
t2m (time) float64 265.4 270.6 273.2 273.7 280.6 284.4 284.9 ...
Attributes:
Conventions: CF-1.6
history: 2017-04-19 16:02:16 GMT by grib_to_netcdf-2.1.0: grib_to_ne...
```
---
# Analytics
- Aggregation
```python
>>> global_avg = ds.mean(dim=['latitude', 'longitude'])
>>> global_avg
```
```
<xarray.Dataset>
Dimensions: (time: 457)
Coordinates:
* time (time) datetime64[ns] 1979-01-01 1979-02-01 1979-03-01 ...
Data variables:
sp (time) float64 9.673e+04 9.667e+04 9.668e+04 9.67e+04 9.663e+04 ...
u10 (time) float64 -0.1069 0.02902 -0.1717 -0.1011 0.001793 0.09216 ...
v10 (time) float64 -0.2489 -0.0867 -0.1123 0.0739 0.1937 0.4532 ...
t2m (time) float64 276.7 275.3 276.0 277.0 278.7 280.3 280.3 280.2 ...
```
---
# Analytics
- Groupby (split-apply-combine)
```python
>>> month_avg = ds.groupby('time.month').mean(dim='time')
>>> month_avg
```
```
<xarray.Dataset>
Dimensions: (latitude: 241, longitude: 480, month: 12)
Coordinates:
* longitude (longitude) float32 0.0 0.75 1.5 2.25 3.0 3.75 4.5 5.25 6.0 ...
* latitude (latitude) float32 90.0 89.25 88.5 87.75 87.0 86.25 85.5 ...
* month (month) int64 1 2 3 4 5 6 7 8 9 10 11 12
Data variables:
sp (month, latitude, longitude) float64 1.014e+05 1.014e+05 ...
u10 (month, latitude, longitude) float64 -1.982 -1.987 -1.992 ...
v10 (month, latitude, longitude) float64 -0.7036 -0.678 -0.6526 ...
t2m (month, latitude, longitude) float64 246.1 246.1 246.1 246.1 ...
```
---
# Analytics
- Arithmetic (broadcasting)
```python
>>> time = xr.DataArray([0, 2, 4], dims='time',
... coords={'time': np.arange(3)})
>>> space = xr.DataArray([0, 1, 2, 3], dims='space',
... coords={'space': np.arange(4)})
>>> time + space
```
```
<xarray.DataArray (time: 3, space: 4)>
array([[0, 1, 2, 3],
[2, 3, 4, 5],
[4, 5, 6, 7]])
Coordinates:
* time (time) int64 0 1 2
* space (space) int64 0 1 2 3
```
<img src="figs/broadcast.png" style="margin: 20px 200px;" width="400px"/>
---
# Analytics
- Arithmetic (alignment)
```python
>>> a = xr.DataArray([2, 3, 4, 1, 0, 5], dims='year',
... coords={'year': [2000, 2001, 2002, 2003, 2004, 2005]})
>>> b = xr.DataArray([3, 4, 2, 3, 1, 0, 3], dims='year',
... coords={'year': [2002, 2003, 2004, 2005, 2006, 2007, 2008]})
>>> a + b
```
```
<xarray.DataArray (year: 4)>
array([7, 5, 2, 8])
Coordinates:
* year (year) int64 2002 2003 2004 2005
```
<img src="figs/align.png" style="margin: 20px 200px;" width="300px"/>
---
# Analytics
- More advanced example using both numpy.array and xarray objects
```python
>>> # weighted average on a spherical Earth
>>> weight = np.cos(np.deg2rad(ds.latitude))
>>> weight = weight / weight.sum()
>>> zonal_avg = ds.mean(dim='longitude')
>>> global_avg = (zonal_avg * weight).sum(dim='latitude')
```
```
<xarray.Dataset>
Dimensions: (time: 457)
Coordinates:
* time (time) datetime64[ns] 1979-01-01 1979-02-01 1979-03-01 ...
Data variables:
sp (time) float64 9.852e+04 9.853e+04 9.853e+04 9.853e+04 ...
u10 (time) float64 -0.2679 -0.3043 -0.3953 -0.3146 -0.2558 -0.1829 ...
v10 (time) float64 -0.3623 -0.2423 -0.1939 -0.007289 0.2645 0.5457 ...
t2m (time) float64 285.5 285.4 286.3 287.2 288.2 289.0 289.1 289.1 ...
```
---
layout: true
<div class="header-wrap">
<div class='logo-left'><img src="figs/xarray-logo-notext.png" width="65px"/></div>
<div class='title-warp'>
<div class='title'>xarray: multi-dimensional data analysis in Python</div>
<div class='authors'>Stephan Hoyer (1), Joe Hamman (2), Fabien Maussion (3) and Benoît Bovy (4)</div>
<div class='affiliations'>
(1) Google Research, Mountain View, CA, USA
(2) NCAR, Boulder, CO, USA
(3) University of Innsbruck, Austria
(4) GFZ Potsdam, Germany
</div>
</div>
<div class='logo-right'></div>
</div>
<div class="footer-logos">
<span>EGU Vienna 04-2017 (PICO)</span>
<img src="figs/cc-by-license.png" width="60px"/>
</div>
<div class="footer-wrap">
<ul>
<li>summary</li>
<li>data structures</li>
<li>data analytics</li>
<li class='current-nav'>plotting</li>
<li>big data</li>
<li>more features</li>
<li>infos</li>
</ul>
</div>
---
# Plotting
```python
>>> # plot type, axis labels and colormap inferred from data / labels
>>> global_avg.t2m.plot(figsize=(10, 5))
```
<img src="figs/plot-line.png" width="550px"/>
---
# Plotting: maps
```python
>>> import matplotlib.pyplot as plt
>>> import cartopy.crs as ccrs
>>> ax = plt.axes(projection=ccrs.Robinson())
>>> month_avg.t2m.sel(month=7).plot(ax=ax, transform=ccrs.PlateCarree());
>>> ax.coastlines();
```
<img src="figs/plot-map2.png" width="530px"/>
---
# Plotting: facet plots
```python
>>> season_avg = ds.groupby('time.season').mean(dim='time')
>>> p = season_avg.t2m.plot(x='longitude', y='latitude', col='season',
... col_wrap=2, transform=ccrs.PlateCarree(),
... subplot_kws={'projection': ccrs.PlateCarree()})
>>> for ax in p.axes.flat:
... ax.coastlines()
```
<img src="figs/facet-plot.png" width="600px"/>
---
layout: true
<div class="header-wrap">
<div class='logo-left'><img src="figs/xarray-logo-notext.png" width="65px"/></div>
<div class='title-warp'>
<div class='title'>xarray: multi-dimensional data analysis in Python</div>
<div class='authors'>Stephan Hoyer (1), Joe Hamman (2), Fabien Maussion (3) and Benoît Bovy (4)</div>
<div class='affiliations'>
(1) Google Research, Mountain View, CA, USA
(2) NCAR, Boulder, CO, USA
(3) University of Innsbruck, Austria
(4) GFZ Potsdam, Germany
</div>
</div>
<div class='logo-right'></div>
</div>
<div class="footer-logos">
<span>EGU Vienna 04-2017 (PICO)</span>
<img src="figs/cc-by-license.png" width="60px"/>
</div>
<div class="footer-wrap">
<ul>
<li>summary</li>
<li>data structures</li>
<li>data analytics</li>
<li>plotting</li>
<li class='current-nav'>big data</li>
<li>more features</li>
<li>infos</li>
</ul>
</div>
---
# Out-of-core computing
- dask arrays: divide large arrays in smaller pieces ("chunks") fitting in memory
```python
>>> # split the array into chunks of max. 100 elements on the time dimension
>>> t2m_dask = ds.t2m.chunk({'time': 100})
>>> t2m_dask.data # returns a dask.array instead of a numpy.array
```
```
dask.array<xarray-<this-array>, shape=(457, 241, 480), chunksize=(100, 241, 480)>
```
<img src="figs/dask-array.png" style="margin-left: 230px;" width="260px"/>
---
# Out-of-core computing
- Lazy computation (deferred until requested)
```python
>>> out = t2m_dask.mean(['latitude', 'longitude'])
>>> out # no value computed yet (still a dask.array)
```
```
<xarray.DataArray 't2m' (time: 457)>
dask.array<mean_agg-aggregate, shape=(457,), dtype=float64, chunksize=(100,)>
Coordinates:
* time (time) datetime64[ns] 1979-01-01 1979-02-01 1979-03-01 ...
```
```python
>>> out.compute() # force triggering the computation (returns a numpy.array)
```
```
<xarray.DataArray 't2m' (time: 457)>
array([ 276.727663, 275.325389, 275.960077, ..., 278.9487 , 278.153185,
277.688196])
Coordinates:
* time (time) datetime64[ns] 1979-01-01 1979-02-01 1979-03-01 ...
```
---
# Out-of-core computing
- Computation graph (leverage multi-core processors)
```python
>>> out.data.visualize() # show a graph of the deferred computations
... # (requires graphviz)
```
<img src="figs/dask-graph2.png" width="550px"/>
---
layout: true
<div class="header-wrap">
<div class='logo-left'><img src="figs/xarray-logo-notext.png" width="65px"/></div>
<div class='title-warp'>
<div class='title'>xarray: multi-dimensional data analysis in Python</div>
<div class='authors'>Stephan Hoyer (1), Joe Hamman (2), Fabien Maussion (3) and Benoît Bovy (4)</div>
<div class='affiliations'>
(1) Google Research, Mountain View, CA, USA
(2) NCAR, Boulder, CO, USA
(3) University of Innsbruck, Austria
(4) GFZ Potsdam, Germany
</div>
</div>
<div class='logo-right'></div>
</div>
<div class="footer-logos">
<span>EGU Vienna 04-2017 (PICO)</span>
<img src="figs/cc-by-license.png" width="60px"/>
</div>
<div class="footer-wrap">
<ul>
<li>summary</li>
<li>data structures</li>
<li>data analytics</li>
<li>plotting</li>
<li>big data</li>
<li class='current-nav'>more features</li>
<li>infos</li>
</ul>
</div>
---
# More features...
- Concatenate, merge & combine datasets
- Open multiple files into a single `xarray.Dataset`
- I/O backends: netCDF3/4, GRIB, HDF4... (external dependencies)
- OPeNDAP support
- Import/export from/to `pandas.DataFrame` or `pandas.Series`
- Multi-index coordinates support (stack / unstack)
- Easily extend xarray with domain specific functionality, e.g., signal
processing, image processing, GCM output analytics, GIS processing...
---
layout: true
<div class="header-wrap">
<div class='logo-left'><img src="figs/xarray-logo-notext.png" width="65px"/></div>
<div class='title-warp'>
<div class='title'>xarray: multi-dimensional data analysis in Python</div>
<div class='authors'>Stephan Hoyer (1), Joe Hamman (2), Fabien Maussion (3) and Benoît Bovy (4)</div>
<div class='affiliations'>
(1) Google Research, Mountain View, CA, USA
(2) NCAR, Boulder, CO, USA
(3) University of Innsbruck, Austria
(4) GFZ Potsdam, Germany
</div>
</div>
<div class='logo-right'></div>
</div>
<div class="footer-logos">
<span>EGU Vienna 04-2017 (PICO)</span>
<img src="figs/cc-by-license.png" width="60px"/>
</div>
<div class="footer-wrap">
<ul>
<li>summary</li>
<li>data structures</li>
<li>data analytics</li>
<li>plotting</li>
<li>big data</li>
<li>more features</li>
<li class='current-nav'>infos</li>
</ul>
</div>
---
# xarray
- Open source
- Very good integration with other Python libraries for scientific
computing (SciPy / PyData Stack)
- Extensible
- Documentation: http://xarray.pydata.org
- Repository: https://github.com/pydata/xarray
- 60 contributors (still growing)
- Latest release: v0.9.5 (17.04.2017)
- Umbrellas (no funding): Python for Data & NumFOCUS
<img src="figs/pydata-logo.png" style="margin-left: 45px;" width="150px"/>
<img src="figs/numfocus-logo.png" style="margin-left: 45px;" width="150px"/>
</textarea>
<script src="static/js/remark-latest.min.js">
</script>
<script>
var slideshow = remark.create({
slideNumberFormat: '',
highlightStyle: 'github-gist',
ratio: '4:3'
});
</script>
</body>
</html>