<commit_before>"""Shannan entropy calculation. For more info see https://rdrr.io/cran/lidR/man/entropy.html"""

import numpy as np
from laserchicken import keys
from laserchicken.feature_extractor.abc import AbstractFeatureExtractor


class EntropyFeatureExtractor(AbstractFeatureExtractor):
    # TODO: make this settable from command line
    layer_thickness = 0.5
    z_min = None
    z_max = None

    @classmethod
    def requires(cls):
        return []

    @classmethod
    def provides(cls):
        return ['z_entropy']

    def get_params(self):
        p = [self.layer_thickness]
        if self.z_min is not None:
            p.append(self.z_min)
        if self.z_max is not None:
            p.append(self.z_max)
        return p

    def extract(self, source_pc, neighborhood, target_pc, target_index, volume_description):
        z = source_pc[keys.point]["z"]["data"][neighborhood]
        _z_min = np.min(z) if self.z_min is None else self.z_min
        _z_max = np.max(z) if self.z_max is None else self.z_max
        n_bins = int(np.ceil((_z_max - _z_min) / self.layer_thickness))<commit_msg>When z_min equal z_max return entropy 0.<commit_after>"""Shannan entropy calculation. For more info see https://rdrr.io/cran/lidR/man/entropy.html"""

import numpy as np
from laserchicken import keys
from laserchicken.feature_extractor.abc import AbstractFeatureExtractor


class EntropyFeatureExtractor(AbstractFeatureExtractor):
    # TODO: make this settable from command line
    layer_thickness = 0.5
    z_min = None
    z_max = None

    @classmethod
    def requires(cls):
        return []

    @classmethod
    def provides(cls):
        return ['z_entropy']

    def get_params(self):
        p = [self.layer_thickness]
        if self.z_min is not None:
            p.append(self.z_min)
        if self.z_max is not None:
            p.append(self.z_max)
        return p

    def extract(self, source_pc, neighborhood, target_pc, target_index, volume_description):
        z = source_pc[keys.point]["z"]["data"][neighborhood]
        _z_min = np.min(z) if self.z_min is None else self.z_min
        _z_max = np.max(z) if self.z_max is None else self.z_max
        if (_z_min == _z_max):
          return 0
        n_bins = int(np.ceil((_z_max - _z_min) / self.layer_thickness))
{'fraction_non_alphanumeric': 0.09639600521059488, 'fraction_numerical': 0.0026052974381241857, 'mean_word_length': 2.8528428093645486, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '825939', 'n_tokens_mistral': 772, 'n_tokens_neox': 751, 'n_words': 192}