<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://climate-cms.wikis.unsw.edu.au/index.php?action=history&amp;feed=atom&amp;title=CodeBreak_30%2F03%2F2022</id>
	<title>CodeBreak 30/03/2022 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://climate-cms.wikis.unsw.edu.au/index.php?action=history&amp;feed=atom&amp;title=CodeBreak_30%2F03%2F2022"/>
	<link rel="alternate" type="text/html" href="http://climate-cms.wikis.unsw.edu.au/index.php?title=CodeBreak_30/03/2022&amp;action=history"/>
	<updated>2026-10-15T07:42:42Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.0</generator>
	<entry>
		<id>http://climate-cms.wikis.unsw.edu.au/index.php?title=CodeBreak_30/03/2022&amp;diff=3466&amp;oldid=prev</id>
		<title>C.carouge at 01:04, 12 April 2022</title>
		<link rel="alternate" type="text/html" href="http://climate-cms.wikis.unsw.edu.au/index.php?title=CodeBreak_30/03/2022&amp;diff=3466&amp;oldid=prev"/>
		<updated>2022-04-12T01:04:53Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 01:04, 12 April 2022&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot; &gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;div&amp;gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;= Error in Python =&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;= Error in Python =&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l54&quot; &gt;Line 54:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 53:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;A question was to count how many events with positive values there are for each grid point given an array like &amp;lt;code&amp;gt;anom(event, lat, lon)&amp;lt;/code&amp;gt;. xarray has a function &amp;lt;code&amp;gt;count()&amp;lt;/code&amp;gt;. This will count the number of valid values along the given dimensions. So we need to filter out the negative values using &amp;lt;code&amp;gt;where()&amp;lt;/code&amp;gt; and then &amp;lt;code&amp;gt;count()&amp;lt;/code&amp;gt; the number of values left along the &amp;quot;event&amp;quot; dimension.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;A question was to count how many events with positive values there are for each grid point given an array like &amp;lt;code&amp;gt;anom(event, lat, lon)&amp;lt;/code&amp;gt;. xarray has a function &amp;lt;code&amp;gt;count()&amp;lt;/code&amp;gt;. This will count the number of valid values along the given dimensions. So we need to filter out the negative values using &amp;lt;code&amp;gt;where()&amp;lt;/code&amp;gt; and then &amp;lt;code&amp;gt;count()&amp;lt;/code&amp;gt; the number of values left along the &amp;quot;event&amp;quot; dimension.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;pre&amp;gt;positive_count = anom.where(anom &amp;gt; 0).count(dim=’event’)&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;pre&amp;gt;positive_count = anom.where(anom &amp;gt; 0).count(dim=’event’)&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/div&amp;gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>C.carouge</name></author>
		
	</entry>
	<entry>
		<id>http://climate-cms.wikis.unsw.edu.au/index.php?title=CodeBreak_30/03/2022&amp;diff=3465&amp;oldid=prev</id>
		<title>C.carouge: Created page with &quot;&lt;div&gt; = Error in Python =  The person was trying to reproduce an example Python program. It output the error: &lt;pre&gt;AttributeError: 'function' object has no attribute ‘comput...&quot;</title>
		<link rel="alternate" type="text/html" href="http://climate-cms.wikis.unsw.edu.au/index.php?title=CodeBreak_30/03/2022&amp;diff=3465&amp;oldid=prev"/>
		<updated>2022-04-12T00:37:39Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;&amp;lt;div&amp;gt; = Error in Python =  The person was trying to reproduce an example Python program. It output the error: &amp;lt;pre&amp;gt;AttributeError: &amp;#039;function&amp;#039; object has no attribute ‘comput...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;div&amp;gt;&lt;br /&gt;
= Error in Python =&lt;br /&gt;
&lt;br /&gt;
The person was trying to reproduce an example Python program. It output the error:&lt;br /&gt;
&amp;lt;pre&amp;gt;AttributeError: 'function' object has no attribute ‘compute’  &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This happened because the person had omitted the parentheses on a previous call to another function. For example, the code below will return the same error:&lt;br /&gt;
&amp;lt;pre&amp;gt;a=xr.DataArray([1,2])&lt;br /&gt;
b=a.mean&lt;br /&gt;
b.mean()&lt;br /&gt;
&lt;br /&gt;
Traceback (most recent call last):&lt;br /&gt;
  File &amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;, line 1, in &amp;lt;module&amp;gt;&lt;br /&gt;
AttributeError: 'function' object has no attribute 'mean'&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the second line, &amp;lt;code&amp;gt;b=a.mean&amp;lt;/code&amp;gt; will return a reference to the meanfunction associated with the DataArray &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;. In other words, &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is the mean function, not its result. That is why the error message starts with &amp;lt;code&amp;gt;‘function’ object&amp;lt;/code&amp;gt;.&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
To get the results from the function, you need to call the function which is done with the &amp;lt;code&amp;gt;()&amp;lt;/code&amp;gt; and any argument you need to give.&lt;br /&gt;
&lt;br /&gt;
= Avoiding repetition in python through code organisation =&lt;br /&gt;
&lt;br /&gt;
It is quite common when starting to write a code to just copy and paste a group of lines if we want, for example, to execute the same calculation on a different variable. If you find that you are doing that a lot, it might be time to go back and re-organise your code. A better organised code is not only more readable but is easier to fix should you find a bug. A well-organised code will limit the number of copied lines so you only have to fix your code in one point.&lt;br /&gt;
&lt;br /&gt;
One simple step is to try to create a function for every block of code you are repeating, using only the variables and parameters that might change between calls as arguments. &amp;amp;nbsp; &amp;amp;nbsp;&lt;br /&gt;
&amp;lt;pre&amp;gt;def myfunction( row, array ):&lt;br /&gt;
&lt;br /&gt;
       param = …  # this doesn’t change no need to use it as an argument for the function&lt;br /&gt;
       … Your calculation …&lt;br /&gt;
&lt;br /&gt;
       return newarray  &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the case we looked at, one function was applied to each row of a pandas dataframe. To do so we can use the pandas dataframe &amp;lt;code&amp;gt;itterrows()&amp;lt;/code&amp;gt; method and append each result to a list.&amp;amp;nbsp; &amp;amp;nbsp;&lt;br /&gt;
&amp;lt;pre&amp;gt;results = []&lt;br /&gt;
for index, row in df.iterrows():&lt;br /&gt;
    results.append( myfunction(row, array) )   &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Basically &amp;lt;code&amp;gt;&amp;lt;kbd&amp;gt;iterrows()&amp;lt;/kbd&amp;gt;&amp;lt;/code&amp;gt; loops across each row of the dataframe and returns the index and the row itself.&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
The results list can then be concatenated into one dataArray&lt;br /&gt;
&amp;lt;pre&amp;gt;final = xarray.concat(results, dim='mydim')  &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another example we looked at was a function to create a subplot, which was then called on different variables, each representing a different region. In such a case, to handle different titles and parameters it can be handy to use a dictionary. &amp;amp;nbsp;&lt;br /&gt;
&amp;lt;pre&amp;gt;regions = {'east': [mslp_east, 1, 'Eastern Australian NRM region'],&lt;br /&gt;
           ‘north’: [mslp_north, 2, 'Northern Australian NRM region'], … }   &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then you can use the key/values pair to call the same function on each region: &amp;amp;nbsp;&lt;br /&gt;
&amp;lt;pre&amp;gt;for k in regions.keys():&lt;br /&gt;
    fig = subplot(regions[k][0], regions[k][1], regions[k][2])  &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The dictionary method &amp;lt;code&amp;gt;keys()&amp;lt;/code&amp;gt; here works in a similar way to &amp;lt;code&amp;gt;iterrows()&amp;lt;/code&amp;gt; but returns only the key. Use &amp;lt;code&amp;gt;items() &amp;lt;/code&amp;gt;if you want the key,value pairs and &amp;lt;code&amp;gt;values()&amp;lt;/code&amp;gt; if you want only the values.&lt;br /&gt;
&lt;br /&gt;
= Count the number of elements that follow a given condition =&lt;br /&gt;
&lt;br /&gt;
A question was to count how many events with positive values there are for each grid point given an array like &amp;lt;code&amp;gt;anom(event, lat, lon)&amp;lt;/code&amp;gt;. xarray has a function &amp;lt;code&amp;gt;count()&amp;lt;/code&amp;gt;. This will count the number of valid values along the given dimensions. So we need to filter out the negative values using &amp;lt;code&amp;gt;where()&amp;lt;/code&amp;gt; and then &amp;lt;code&amp;gt;count()&amp;lt;/code&amp;gt; the number of values left along the &amp;quot;event&amp;quot; dimension.&lt;br /&gt;
&amp;lt;pre&amp;gt;positive_count = anom.where(anom &amp;gt; 0).count(dim=’event’)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;/div&gt;</summary>
		<author><name>C.carouge</name></author>
		
	</entry>
</feed>