Python Hour - 11 August 2014
-
Cim mentioned there is a Python toolbox called x-ray (short for eXtended arRAYs) that is designed to make working with multi-dimensional datasets a lot easier.
xray
allows you to add dimension names and coordinate values to numpy ndarrays. You can then use those dimension names and coordinate values to perform operations over the array. For example, withxray
, you can find the time mean of an array by doing something likex.sum('time')
- rather thanx.sum(axis=0)
, if time was on the first dimension. -
Jake asked about how to set the x-y aspect ratio of a plot. We talked about many ways of doing this. The easiest solution is probably to use the
axes
methodset_aspect
. An minimal working example is provided here. -
Earle talked about different ways of saving Python data to file. Several options were mentioned; for e.g. (most) data can be saved as
.mat
,netcdf
orHDF
files. However, the standard way of saving Python objects to file is to use the pickle module. The syntax for saving objects using pickle ispickle.dump(obj,file,protocol)
. You can find examples of how to use pickle here. By default,pickle.dump(...)
tries to save data in a human-readable format. This is ok for small, simple objects but saving large n-dimensional arrays this way is very slow and results in files that take up a lot of disk space. Theprotocol
parameter allows users to change this behaviour. Settingprotocol = pickle.HIGHEST_PROTOCOL
orprotocol = -1
, tells pickle to use the fastest, most memory efficient option available. By using the highest protocol, you can save files that take up as much space as anetcdf
or.mat
file containing the same data. -
Earle briefly showed how to make a plot with broken axes. An example is given here.
-
We had a pretty lengthy discussion about version control software - git, in particular. JPaul brought up the git rebase command and showed how it can be useful when pushing changes to a group repository. We also tried to understand the difference between
git checkout
andgit reset
. Cim mentioned that bitbucket is an alternative to github and offers free, online private repositories. -
Jake says that PyHOG (PYthon Hour for Oceanographers and Geoscientists) sounds a lot better than GeoPUG (Geoscience Python User Group). So, PyHOG it is...