Axis names and indices for a tensor. It is an ordered mapping, with keys given by axis name and values given by Axis objects. Duplicate axis names are not allowed.
| 197 | |
| 198 | |
| 199 | class Axes(collections_abc.Mapping): |
| 200 | """Axis names and indices for a tensor. |
| 201 | |
| 202 | It is an ordered mapping, with keys given by axis name and values given |
| 203 | by Axis objects. Duplicate axis names are not allowed. |
| 204 | """ |
| 205 | |
| 206 | @tc.accepts(object, tc.List(AxisLike)) |
| 207 | def __init__(self, axes): |
| 208 | """Construct an Axes. |
| 209 | |
| 210 | Args: |
| 211 | axes: A list of Axis objects or (axis_name, axis_value) tuples. |
| 212 | |
| 213 | Raises: |
| 214 | ValueError: If the user provides empty or duplicate axis names. |
| 215 | """ |
| 216 | self._axes = collections.OrderedDict() |
| 217 | |
| 218 | for axis_data in axes: |
| 219 | axis = as_axis(axis_data) |
| 220 | |
| 221 | name = axis.name |
| 222 | if name in self._axes: |
| 223 | raise ValueError('Duplicate axis name: %s' % name) |
| 224 | |
| 225 | self._axes[name] = axis |
| 226 | |
| 227 | def __iter__(self): |
| 228 | return iter(self._axes) |
| 229 | |
| 230 | @tc.returns(string_types) |
| 231 | def __repr__(self): |
| 232 | # Axes([('x', Dimension(2)), |
| 233 | # ('y', ['a', 'b', 'c']), |
| 234 | # ('z', Dimension(4))]) |
| 235 | cls_name = type(self).__name__ |
| 236 | values = ["('%s', %r)" % (v.name, v.value) for v in self._axes.values()] |
| 237 | values_repr = (',\n' + ' ' * len(cls_name + '([')).join(values) |
| 238 | return '%s([%s])' % (cls_name, values_repr) |
| 239 | |
| 240 | @tc.returns(Axis) |
| 241 | @tc.accepts(object, string_types) |
| 242 | def __getitem__(self, name): |
| 243 | return self._axes[name] |
| 244 | |
| 245 | @tc.returns(bool) |
| 246 | def __contains__(self, name): |
| 247 | return name in self._axes |
| 248 | |
| 249 | @tc.returns(int) |
| 250 | def __len__(self): |
| 251 | return len(self._axes) |
| 252 | |
| 253 | def __hash__(self): |
| 254 | return hash(tuple(self.items())) |
| 255 | |
| 256 | @tc.accepts(object, string_types) |