def _perchannel(self, x: np.ndarray) -> np.ndarray:
        """
        Apply thermometer encoding to one channel.

        :param x: Sample to encode with shape `(batch_size, width, height)`.
        :return: Encoded sample with shape `(batch_size, width, height, num_space)`.
        """
        pos = np.zeros(shape=x.shape)
        for i in range(1, self.num_space):
            pos[x > float(i) / self.num_space] += 1

        onehot_rep = to_categorical(pos.reshape(-1), self.num_space)

        for i in range(self.num_space - 1):
            onehot_rep[:, i] += np.sum(onehot_rep[:, i + 1 :], axis=1)

        return onehot_rep.flatten()

        Apply thermometer encoding to one channel.

        :param x: Sample to encode with shape `(batch_size, width, height)`.
        :return: Encoded sample with shape `(batch_size, width, height, num_space)`.
        
art/defences/preprocessor/thermometer_encoding.py