Beispiel #1
0
 def roi_compute(self, user_input: OpuUserInput):
     # with automatic input roi, compute number of ones before
     # This case is met only when input is provided to runner creation
     # (i.e. opu.fit with an input)
     #
     # Count number of ones, and then compute ROI with "auto" strategy
     roi = InputRoi(self.s.input_shape, self.s.ones_range)
     n_ones = self._count_ones(user_input.reshape_input(), user_input.traits.packed)
     n_features_s = self._traits.n_features_s
     self._debug("Counted an average of"
                 " {:.2f} ones in input of size {} ({:.2f}%)."
                 .format(n_ones, n_features_s, 100 * n_ones / n_features_s))
     return roi.compute_roi(self.t.input_roi_strategy,
                            self._traits.n_features, n_ones, self.ones_info)
Beispiel #2
0
    def transform(self, input_vectors: OpuUserInput, linear=False):
        """Do the OPU transform of input
        If batch transform, device acquisition must be started
        """
        assert self.device.active and input_vectors.traits == self._traits
        if not self.s.simulated:
            input_vectors.binary_check()
        else:
            # In simulated mode, SimulatedDevice must whether the transform is linear or not
            self.device._linear = linear
        context = self._get_context()
        self._pre_print(self.device, input_vectors.n_samples)
        X = input_vectors.reshape_input()

        if input_vectors.is_batch:
            t0 = time.time()
            nb_retries = 0
            # Batch transform, allocate the result, progress bar, and start acquisition
            # allocation of empty vector for iteration
            n_samples = input_vectors.n_samples_s
            Y = np.empty((n_samples, self._out_size),
                         dtype=self.device.output_dtype)
            self._trace("Y allocated")
            indices = self.indices(input_vectors.n_samples_s)
            with Progress(n_samples, "OPU: transform", self.disable_pbar) as p:
                # iterate over consecutive pairs of indices
                # (https://stackoverflow.com/a/21303286)
                for i, (start, end) in enumerate(zip(indices, indices[1:])):
                    c = self.__batch_transform(X[start:end], Y[start:end], i)
                    p.update(end - start)
                    nb_retries += c
            t1 = time.time()
            # Fill context from opu settings at the end of the transform
            context.from_opu(self.device, dt.datetime.fromtimestamp(t0),
                             dt.datetime.fromtimestamp(t1))
        else:
            Y, nb_retries = self.__single_transform(X)
            context.from_opu(self.device, dt.datetime.now())

        if nb_retries:
            self._print("OPU number of retries: ", nb_retries)
        return ContextArray(Y, context)