def compute(self, data, view=None): """For a given data set, compute the component comp_to given the data associated with each comp_from and the ``using`` function :param data: The data set to use :param view: Optional view (e.g. slice) through the data to use *Returns*: The data associated with comp_to component *Raises*: InvalidAttribute, if the data set doesn't have all the ComponentIDs needed for the transformation """ logger = logging.getLogger(__name__) args = [data[join_component_view(f, view)] for f in self._from] logger.debug("shape of first argument: %s", args[0].shape) result = self._using(*args) logger.debug("shape of result: %s", result.shape) if result.shape != args[0].shape: logger.warn("ComponentLink function %s changed shape. Fixing", self._using.__name__) result.shape = args[0].shape return result
def compute(self, data, view=None): """ For a given data set, compute the component comp_to given the data associated with each comp_from and the ``using`` function This raises an :class:`glue.core.exceptions.IncompatibleAttribute` if the data set doesn't have all the ComponentIDs needed for the transformation Parameters ---------- data : `~glue.core.data.Data` The data set to use view : `None` or `slice` or `tuple` Optional view (e.g. slice) through the data to use Returns ------- result The data associated with comp_to component """ # First we get the values of all the 'from' components. args = [data[join_component_view(f, view)] for f in self._from] # We keep track of the original shape of the arguments original_shape = args[0].shape logger.debug("shape of first argument: %s", original_shape) # We now unbroadcast the arrays to only compute the link with the # smallest number of values we can. This can help for cases where # the link depends only on e.g. pixel components or world coordinates # that themselves only depend on a subset of pixel components. # Unbroadcasting is the act of returning the smallest array that # contains all the information needed to be broadcasted back to its # full value args = [unbroadcast(arg) for arg in args] # We now broadcast these to the smallest common shape in case the # linking functions don't know how to broadcast arrays with different # shapes. args = np.broadcast_arrays(*args) # We call the actual linking function result = self._using(*args) # We call asarray since link functions may return Python scalars in some cases result = np.asarray(result) # In some cases, linking functions return ravelled arrays, so we # fix this here. logger.debug("shape of result: %s", result.shape) if result.shape != args[0].shape: logger.debug("ComponentLink function %s changed shape. Fixing", self._using.__name__) result.shape = args[0].shape # Finally we broadcast the final result to desired shape result = broadcast_to(result, original_shape) return result