import dascore as dc
patch = dc.get_example_patch()
# Data of the same shape reuses the existing coords and attrs.
out = dc.Patch.from_parts(patch.data * 2, patch.coords, patch.attrs)
# If the coords change, the attrs must be rebuilt from them.
coords = patch.coords.update(time=patch.get_coord("time")[::2])
out = dc.Patch.from_parts(
patch.data[..., ::2], coords, patch.attrs.update(coords=coords)
)from_parts
from_parts(
data: <class ‘object ,
coords: CoordManager ,
attrs: PatchAttrs ,
)-> ’Self’
Create a patch from parts which already conform to each other.
This is a fast alternative to the normal constructor for code which has already built a coordinate manager and matching attributes. It skips recomputing the coordinate summaries stored on attrs, which the constructor does unconditionally.
Parameters
| Parameter | Description |
|---|---|
| data | The array data. Its shape must match coords. |
| coords | A CoordManager describing the data’s dimensions. |
| attrs | A PatchAttrs whose coordinate summaries were derived from coords. |
Raises
PatchCoordinateError If coords and attrs disagree about dimension names. CoordDataError If the data shape does not match coords.
The data shape and dimension names are validated, but the coordinate summaries on attrs are trusted. Passing attrs whose summaries came from a different coordinate manager creates a Patch whose attrs disagree with its coords.
Every Patch conforms by construction, so patch.attrs is always safe to pass alongside patch.coords. If the coords have changed, rebuild the attrs first with patch.attrs.update(coords=new_coords). Note that attrs built with an empty coords (a common way to let the constructor repopulate them) do not conform, and the dimension check will not catch it, since clearing coords leaves dims in place.