Rename Map.from_images to from_stack
@@ -1367,45 +1367,51 @@ class Map(abc.ABC):
self.quantity = np.nan_to_num(normed)
@classmethod
- def from_images(cls, images, axis=None):
+ def from_stack(cls, maps, axis=None, axis_name=None):
"""Create Map from list of images and a non-spatial axis.
- If the images have a non-spatial axis of length 1 a new axes is generated
- from by merging the individual axes. The image geometries must be aligned.
+ The image geometries must be aligned, except for the axis that is stacked.
Parameters
----------
- images : list of `Map` objects
- Images
+ maps : list of `Map` objects
+ List of maps
axis : `MapAxis`
- Map axis
+ If a `MapAxis` is provided the maps are stacked along the last data
+ axis and the new axis is introduced.
+ axis_name : str
+ If an axis name is as string the given the maps are stacked along
+ the given axis name.
Returns
-------
map : `Map`
Map with additional non-spatial axis.
-
"""
- geom_ref = images[0].geom.to_image()
+ geom = maps[0].geom
+
+ if axis_name is None and axis is None:
+ axis_name = geom.axes.names[-1]
+
+ if axis_name:
+ axis = MapAxis.from_stack(axes=[m.geom.axes[axis_name] for m in maps])
+ geom = geom.drop(axis_name=axis_name)
data = []
- for image in images:
- if not image.geom.to_image() == geom_ref:
- raise ValueError("Image geometries not aligned")
- data.append(image.data)
+ for m in maps:
+ if axis_name:
+ m_geom = m.geom.drop(axis_name=axis_name)
+ else:
+ m_geom = m.geom
- if axis is None:
- try:
- axis = MapAxis.from_stack(axes=[image.geom.axes[0] for image in images])
- except IndexError:
- ValueError(
- "Images don't have a non-spatial axis. Please provide"
- " the axis separately"
- )
+ if not m_geom == geom:
+ raise ValueError(f"Image geometries not aligned: {m.geom} and {geom}")
+
+ data.append(m.quantity.to_value(maps[0].unit))
return cls.from_geom(
- data=np.stack(data), geom=geom_ref.to_cube(axes=[axis]), unit=images[0].unit
+ data=np.stack(data), geom=geom.to_cube(axes=[axis]), unit=maps[0].unit
)
def to_cube(self, axes):
