Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug (with fix?) in month name stitching for aggregate_by() #1183

Closed
niallrobinson opened this issue Jun 19, 2014 · 3 comments
Closed

Bug (with fix?) in month name stitching for aggregate_by() #1183

niallrobinson opened this issue Jun 19, 2014 · 3 comments
Milestone

Comments

@niallrobinson
Copy link
Contributor

There is a bug in a special case of aggregated_by. Here seems to be an example case:
If you aggregate_by season on a cube where there are text month coordinates and the cube only covers one season, then iris fails when it stitches the component month names together as there is only one _slices_by_key and so the iterate fails.

>>> print test_data
air temperature / (K)               (time: 204; projection_y_coordinate: 277; projection_x_coordinate: 349)
     Dimension coordinates:
          time                           x                             -                             -
          projection_y_coordinate        -                             x                             -
          projection_x_coordinate        -                             -                             x
     Auxiliary coordinates:
          month                          x                             -                             -
          season                         x                             -                             -
     Scalar coordinates:
          altitude: 10 m
          forecast_period: 0.0 hours, bound=(0.0, 0.0) hours
          originating_centre: US National Weather Service, National Centres for Environmental Predic...
     Cell methods:
          mean: time

>>>_ = test_data.extract(iris.Constraint(month=['Dec', 'Jan', 'Feb']))
>>>_.aggregated_by('season', iris.analysis.MEAN)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-47-1f9326d6dafb> in <module>()
      1 _ = test_data.extract(iris.Constraint(month=['Dec', 'Jan', 'Feb']))
----> 2 _.aggregated_by('season', iris.analysis.MEAN)

/project/avd/iris/live/testing/iris/cube.pyc in aggregated_by(self, coords, aggregator, **kwargs)
   2951         key = [slice(None, None)] * self.ndim
   2952         # Generate unique index tuple key to maintain monotonicity.
-> 2953         key[dimension_to_groupby] = tuple(range(len(groupby)))
   2954         key = tuple(key)
   2955         aggregateby_cube = self[key]

/project/avd/iris/live/testing/iris/analysis/__init__.pyc in __len__(self)
   1456             value = len(self._slices_by_key)
   1457         else:
-> 1458             value = len([s for s in self.group()])
   1459 
   1460         return value

/project/avd/iris/live/testing/iris/analysis/__init__.pyc in group(self)
   1334                 self._compute_groupby_coords()
   1335                 # Calculate the new shared coordinates.
-> 1336                 self._compute_shared_coords()
   1337             # Generate the group-by slices/groups.
   1338             for groupby_slice in self._slices_by_key.itervalues():

/project/avd/iris/live/testing/iris/analysis/__init__.pyc in _compute_shared_coords(self)
   1402                     new_bounds = None
   1403                     for key_slice in self._slices_by_key.itervalues():
-> 1404                         new_pt = '|'.join(coord.points[i] for i in key_slice)
   1405                         new_points.append(new_pt)
   1406                 else:

TypeError: 'slice' object is not iterable

> /project/avd/iris/live/testing/iris/analysis/__init__.py(1404)_compute_shared_coords()
   1403                     for key_slice in self._slices_by_key.itervalues():
-> 1404                         new_pt = '|'.join(coord.points[i] for i in key_slice)
   1405                         new_points.append(new_pt)

Which disappears as soon as you do this

>>>_ = test_data.extract(iris.Constraint(month=['Dec', 'Jan', 'Feb', 'Mar']))
>>>_.aggregated_by('season', iris.analysis.MEAN)
<iris 'Cube' of air temperature / (K) (-- : 2; projection_y_coordinate: 277; projection_x_coordinate: 349)>

or this

>>>_ = test_data.extract(iris.Constraint(month=['Dec', 'Jan', 'Feb']))
>>>_.remove_coord('month')
>>>_.aggregated_by('season', iris.analysis.MEAN)
<iris 'Cube' of air temperature / (K) (time: 1; projection_y_coordinate: 277; projection_x_coordinate: 349)>

This might be fixed merely by changing the code to this:

       # Create new shared bounded coordinates.
        for coord in self._shared_coords:
            if coord.points.dtype.kind == 'S':
                if coord.bounds is None:
                    new_points = []
                    new_bounds = None
                    for key_slice in self._slices_by_key.itervalues():
                        try:
                            new_pt = '|'.join(coord.points[i] for i in key_slice)
                        except TypeError:
                            new_pt = '|'.join(coord.points[key_slice])
                        new_points.append(new_pt)
                else:
                    msg = ('collapsing the bounded string coordinate {0!r}'
                           ' is not supported'.format(coord.name()))
                    raise ValueError(msg)
            else:
                new_bounds = []
@pelson pelson added this to the v1.7 milestone Jun 25, 2014
@pelson pelson added the bug label Jun 25, 2014
@pelson
Copy link
Member

pelson commented Jun 26, 2014

@niallrobinson - I can't reproduce this. My code is:

import iris
import numpy as np

aux_coords = [(iris.coords.AuxCoord(list('aba'), long_name='foo'), 0)]

cube = iris.cube.Cube(np.arange(12).reshape(3, 4),
                      aux_coords_and_dims=aux_coords)

print cube
print cube.aggregated_by('foo', iris.analysis.MEAN)
print cube[:2].aggregated_by('foo', iris.analysis.MEAN)
print cube[:1].aggregated_by('foo', iris.analysis.MEAN)

Have I missed a key part to this? What version are you using?

@pelson
Copy link
Member

pelson commented Jun 26, 2014

Have I missed a key part to this? What version are you using?

Scrap that. I've just added another coordinate ((iris.coords.AuxCoord(list('aaa'), long_name='bar'), 0),) and I got the exception.

@ajdawson
Copy link
Member

Fixed by #1208.

@QuLogic QuLogic modified the milestones: v1.7.0, v1.7 Sep 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants