Beispiel #1
0
    def binned_to_array(self, key, src_representation, dest_representation):
        """Augmented binned data to array data"""

        logging.trace('Transforming %s binned to array data' % (key))

        self.representation = src_representation
        weights = self[key]

        if not src_representation.is_irregular:
            logging.trace(
                f"Container `{self.name}`: regularized lookup for {key}")
            sample = []
            dimensions = []
            for d in src_representation:
                if d.is_log:
                    self.representation = "log_events"
                    sample.append(self[d.name])
                    dimensions.append(
                        OneDimBinning(d.name,
                                      domain=np.log(d.domain.m),
                                      num_bins=d.num_bins))
                else:
                    self.representation = "events"
                    sample.append(self[d.name])
                    dimensions.append(d)
            hist_binning = MultiDimBinning(dimensions)
        else:
            logging.trace(
                f"Container `{self.name}`: irregular lookup for {key}")
            self.representation = dest_representation
            sample = [self[name] for name in src_representation.names]
            hist_binning = src_representation

        return lookup(sample, weights, hist_binning)
Beispiel #2
0
 def binned_to_array(self, key):
     """Augmented binned data to array data"""
     try:
         binning, hist = self.binned_data[key]
     except KeyError:
         if key in self.array_data:
             logging.debug('No transformation for `%s` array data in container `%s`'%(key, self.name))
             return
         else:
             raise ValueError('Key `%s` does not exist in container `%s`'%(key, self.name))
     logging.debug('Transforming %s binned to array data'%(key))
     sample = [self.array_data[n] for n in binning.names]
     self.add_array_data(key, lookup(sample, hist, binning))
Beispiel #3
0
    def apply(self):
        # DO NOT USE THIS STAGE AS YOUR TEMPLATE IF YOU ARE NEW TO PISA!
        # --------------------------------------------------------------
        #
        # We are overwriting the `apply` method rather than the `apply_function` method
        # because we are manipulating the data binning in a delicate way that doesn't
        # work with automatic rebinning.

        self.data.data_specs = self.input_specs
        if self.scale_errors:
            for container in self.data:
                vectorizer.pow(
                    vals=container["errors"],
                    pwr=2,
                    out=container["variances"],
                )

        input_binvols = SmartArray(
            self.input_specs.weighted_bin_volumes(attach_units=False).ravel())
        output_binvols = SmartArray(
            self.output_specs.weighted_bin_volumes(attach_units=False).ravel())

        for container in self.data:
            self.data.data_specs = self.input_specs
            # we want these to be SmartArrays, so no `.get(WHERE)`
            weights_flat_hist = container["weights"]
            if self.scale_errors:
                vars_flat_hist = container["variances"]
            self.data.data_specs = self.output_specs
            if self.rs_mode == ResampleMode.UP:
                # The `unroll_binning` function returns the midpoints of the bins in the
                # dimension `name`.
                fine_gridpoints = [
                    SmartArray(
                        container.unroll_binning(name, self.output_specs))
                    for name in self.output_specs.names
                ]
                # We look up at which bin index of the input binning the midpoints of
                # the output binning can be found, and assign to each the content of the
                # bin of that index.
                container["weights_resampled"] = translation.lookup(
                    fine_gridpoints,
                    weights_flat_hist,
                    self.input_specs,
                )
                if self.scale_errors:
                    container["vars_resampled"] = translation.lookup(
                        fine_gridpoints,
                        vars_flat_hist,
                        self.input_specs,
                    )
                # These are the volumes of the bins we sample *from*
                origin_binvols = translation.lookup(
                    fine_gridpoints,
                    input_binvols,
                    self.input_specs,
                )
                # Finally, we scale the weights and variances by the ratio of the
                # bin volumes in place:
                vectorizer.imul(output_binvols, container["weights_resampled"])
                vectorizer.itruediv(origin_binvols,
                                    container["weights_resampled"])
                if self.scale_errors:
                    vectorizer.imul(output_binvols,
                                    container["vars_resampled"])
                    vectorizer.itruediv(origin_binvols,
                                        container["vars_resampled"])
            elif self.rs_mode == ResampleMode.DOWN:
                pass  # not yet implemented

            if self.scale_errors:
                vectorizer.sqrt(vals=container["vars_resampled"],
                                out=container["errors_resampled"])