Difference between revisions of "CodeBreak 1/9/2021"

m (C.carouge moved page CodeBreak to CodeBreak 1/9/2021 without leaving a redirect)
Line 1: Line 1:
  
= Code Breaks =
+
= Summary of topics: =
 
 
  1/9/2021   Summary of topics:
 
  
 
*How to organise plotting large numbers of plots from heterogenous models in python  
 
*How to organise plotting large numbers of plots from heterogenous models in python  
Line 8: Line 6:
 
*Fast percentile calculation in python  
 
*Fast percentile calculation in python  
  
 
+
= Organising workflows in python =
 
 
== Organising workflows in python ==
 
  
cf-xarray is a useful library for writing general code for a heterogenous set of models that might not share the same names for things like dimensions (e.g. lat/latitude/LAT/) and variables. cf-xarray has code for inferring this information and allows you to refer to them in a general way. It is available in the conda environments and the documentation is here:
+
<span style="color:#c0392b">cf-xarray</span> is a useful library for writing general code for a heterogenous set of models that might not share the same names for things like dimensions (e.g. lat/latitude/LAT/) and variables. cf-xarray has code for inferring this information and allows you to refer to them in a general way. It is available in the conda environments and the documentation is here:
  
 
[https://cf-xarray.readthedocs.io/en/latest/ https://cf-xarray.readthedocs.io/en/latest/]
 
[https://cf-xarray.readthedocs.io/en/latest/ https://cf-xarray.readthedocs.io/en/latest/]
  
The python3 pathlib library is an object oriented path manipulation library that makes working with paths a lot simpler and cleaner, e.g. when opening data files, and saving processed output or plots to disk:
+
The python3<span style="color:#c0392b">pathlib</span> library is an object oriented path manipulation library that makes working with paths a lot simpler and cleaner, e.g. when opening data files, and saving processed output or plots to disk:
  
 
[https://docs.python.org/3/library/pathlib.html https://docs.python.org/3/library/pathlib.html]
 
[https://docs.python.org/3/library/pathlib.html https://docs.python.org/3/library/pathlib.html]
  
As far as how to organise plotting of a large number of different plots from a range of models, there are a range of data structures that might suit this purpose, and it comes down to the specifics of what needs to be done and personal preference, but some options are: &nbsp;
+
As far as <span style="color:#c0392b">how to organise plotting</span> of a large number of different plots from a range of models, there are a range of data structures that might suit this purpose, and it comes down to the specifics of what needs to be done and personal preference, but some options are: &nbsp;
  
 
*[https://realpython.com/python-data-structures/#dict-simple-data-objects python dict]  
 
*[https://realpython.com/python-data-structures/#dict-simple-data-objects python dict]  
Line 26: Line 22:
 
*[https://realpython.com/python-data-classes/ python data class]  
 
*[https://realpython.com/python-data-classes/ python data class]  
  
&nbsp; For more information this is a pretty comprehensive write up of some of the commonly used data structures in python &nbsp; [https://realpython.com/python-data-structures/ https://realpython.com/python-data-structures/] &nbsp;
+
For more information, this is a pretty comprehensive write up of some of the commonly used data structures in python &nbsp; [https://realpython.com/python-data-structures/ https://realpython.com/python-data-structures/] &nbsp;
 +
 
 +
= Subplots in matplotlib =
 +
 
 +
&nbsp; Scott wrote a blog showing a sophisticated <span style="color:#c0392b">use of subplot</span>, but also has some tips for organising the plots by saving references to each in a dictionary named for the plot type: &nbsp; [https://climate-cms.org/2018/04/27/subplots.html https://climate-cms.org/2018/04/27/subplots.html]
  
== Subplots in matplotlib ==
+
= Jupyter Notebook =
  
&nbsp; Scott wrote a blog showing a sophisticated use of subplot, but also has some tips for organising the plots by saving references to each in a dictionary named for the plot type: &nbsp; [https://climate-cms.org/2018/04/27/subplots.html https://climate-cms.org/2018/04/27/subplots.html]<br/> Unrelated to the original topics, but some of the attendees didn' t know it was possible to connect a jupyter notebook directly to gadi compute nodes, which is useful for anyone who must access data on /scratch, or have workloads that are currently too onerous for VDI or ood.nci.org.au (though ood is designed to cater for larger jobs than VDI).&nbsp; This is all covered on our wiki &nbsp; [http://climate-cms.wikis.unsw.edu.au/Running_Jupyter_Notebook#On_Gadi http://climate-cms.wikis.unsw.edu.au/Running_Jupyter_Notebook#On_Gadi] &nbsp; which also covers creating symbolic links to access files in other locations on the file system, e.g. /g/data &nbsp;
+
Unrelated to the original topics, but some of the attendees didn't know it was possible to <span style="color:#c0392b">connect a jupyter notebook directly to gadi</span> compute nodes, which is useful for anyone who must access data on /scratch, or have workloads that are currently too onerous for VDI or ood.nci.org.au (though ood is designed to cater for larger jobs than VDI).&nbsp; This is all covered on our wiki &nbsp; [http://climate-cms.wikis.unsw.edu.au/Running_Jupyter_Notebook#On_Gadi http://climate-cms.wikis.unsw.edu.au/Running_Jupyter_Notebook#On_Gadi] &nbsp; which also covers creating symbolic links to access files in other locations on the file system, e.g. /g/data &nbsp;
  
== Fast percentile calculation in python ==
+
= Fast percentile calculation in python =
  
 
&nbsp; Calculating a percentile climatology where for each day in the year, that day plus 15 days either side are gathered together over all years to make a (dayofyear, lat, lon) dataset of 90th percentiles from each sample &nbsp; Rolling will get a 31 day sample centred around the day of interest (at the start and end of the dataset the values will be NAN) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;tas.rolling(time=31, center=True) &nbsp; Construct will take the rolling samples and convert them to a new dimension, so the data now has dimensions (time, lat, lon, window) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;tas.rolling(time=31, center=True).construct(time='window') &nbsp; Groupby will collect all the equivalent days of the year (remember the 'time' axis is the centre of the sample) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;(tas.rolling(time=31, center=True)
 
&nbsp; Calculating a percentile climatology where for each day in the year, that day plus 15 days either side are gathered together over all years to make a (dayofyear, lat, lon) dataset of 90th percentiles from each sample &nbsp; Rolling will get a 31 day sample centred around the day of interest (at the start and end of the dataset the values will be NAN) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;tas.rolling(time=31, center=True) &nbsp; Construct will take the rolling samples and convert them to a new dimension, so the data now has dimensions (time, lat, lon, window) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;tas.rolling(time=31, center=True).construct(time='window') &nbsp; Groupby will collect all the equivalent days of the year (remember the 'time' axis is the centre of the sample) &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;(tas.rolling(time=31, center=True)

Revision as of 01:45, 6 September 2021

Summary of topics:

  • How to organise plotting large numbers of plots from heterogenous models in python
  • Layout of subplots with matplotlib in python
  • Fast percentile calculation in python

Organising workflows in python

cf-xarray is a useful library for writing general code for a heterogenous set of models that might not share the same names for things like dimensions (e.g. lat/latitude/LAT/) and variables. cf-xarray has code for inferring this information and allows you to refer to them in a general way. It is available in the conda environments and the documentation is here:

https://cf-xarray.readthedocs.io/en/latest/

The python3pathlib library is an object oriented path manipulation library that makes working with paths a lot simpler and cleaner, e.g. when opening data files, and saving processed output or plots to disk:

https://docs.python.org/3/library/pathlib.html

As far as how to organise plotting of a large number of different plots from a range of models, there are a range of data structures that might suit this purpose, and it comes down to the specifics of what needs to be done and personal preference, but some options are:  

For more information, this is a pretty comprehensive write up of some of the commonly used data structures in python   https://realpython.com/python-data-structures/  

Subplots in matplotlib

  Scott wrote a blog showing a sophisticated use of subplot, but also has some tips for organising the plots by saving references to each in a dictionary named for the plot type:   https://climate-cms.org/2018/04/27/subplots.html

Jupyter Notebook

Unrelated to the original topics, but some of the attendees didn't know it was possible to connect a jupyter notebook directly to gadi compute nodes, which is useful for anyone who must access data on /scratch, or have workloads that are currently too onerous for VDI or ood.nci.org.au (though ood is designed to cater for larger jobs than VDI).  This is all covered on our wiki   http://climate-cms.wikis.unsw.edu.au/Running_Jupyter_Notebook#On_Gadi   which also covers creating symbolic links to access files in other locations on the file system, e.g. /g/data  

Fast percentile calculation in python

  Calculating a percentile climatology where for each day in the year, that day plus 15 days either side are gathered together over all years to make a (dayofyear, lat, lon) dataset of 90th percentiles from each sample   Rolling will get a 31 day sample centred around the day of interest (at the start and end of the dataset the values will be NAN)       tas.rolling(time=31, center=True)   Construct will take the rolling samples and convert them to a new dimension, so the data now has dimensions (time, lat, lon, window)       tas.rolling(time=31, center=True).construct(time='window')   Groupby will collect all the equivalent days of the year (remember the 'time' axis is the centre of the sample)       (tas.rolling(time=31, center=True)

  .construct(time='window')

  .groupby(time='dayofyear'))   Normally you can just add a reduction operation (e.g. mean) to the end here, but that doesn't work with percentiles in this case. Instead do a loop:       doy_pct = []

    for doy, sample in (tas

.rolling(time=31,center=True)

.construct(time='window')

.groupby('time.dayofyear')):

        print(doy)

        doy_pct.append(sample.load().quantile(0.9, dim=['time', 'window']))       xarray.concat(doy_pct, dim='dayofyear')   See that we've called `.load()` inside the loop, which avoids a Dask chunking error when doing a percentile.   Try it with just a single point to help understand how this is working   Note the use of the list to gather the percentiles arrays for each day, then concatenate along a new dimension 'dayofyear'.