from collections import Counter
from math import log2

from digestive import Sink


# TODO: stash intermediate histograms in multiple Counters?
# TODO: output as a spark
# TODO: output as plot
class Entropy(Sink):
    def __init__(self):
        super().__init__('entropy')
        self.length = 0
        self.counter = Counter()

    def update(self, data):
        self.length += len(data)
        self.counter.update(data)

    def digest(self):
        # calculate binary entropy as -Σ(1…n) p_i × log₂(p_i)
        entropy = -sum(count / self.length * log2(count / self.length) for count in self.counter.values())
        return '{:.8f}'.format(entropy)

from collections import Counter
from math import log2

from digestive import Sink


class Entropy(Sink):
    def __init__(self):
        super().__init__('entropy')
        self.length = 0
        self.counter = Counter()

    def update(self, data):
        self.length += len(data)
        self.counter.update(data)

    def digest(self):
        # calculate binary entropy as -Σ(1…n) p_i × log₂(p_i)
        entropy = -sum(count / self.length * log2(count / self.length) for count in self.counter.values())
        return '{:.8f}'.format(entropy)

Remove TODO's converted to issues
Remove TODO's converted to issues

  from collections import Counter
  from math import log2
  
  from digestive import Sink
  
  
- # TODO: stash intermediate histograms in multiple Counters?
- # TODO: output as a spark
- # TODO: output as plot
  class Entropy(Sink):
      def __init__(self):
          super().__init__('entropy')
          self.length = 0
          self.counter = Counter()
  
      def update(self, data):
          self.length += len(data)
          self.counter.update(data)
  
      def digest(self):
          # calculate binary entropy as -Σ(1…n) p_i × log₂(p_i)
          entropy = -sum(count / self.length * log2(count / self.length) for count in self.counter.values())
          return '{:.8f}'.format(entropy)
  
Remove TODO's converted to issues
## Code Before:
from collections import Counter
from math import log2

from digestive import Sink


# TODO: stash intermediate histograms in multiple Counters?
# TODO: output as a spark
# TODO: output as plot
class Entropy(Sink):
    def __init__(self):
        super().__init__('entropy')
        self.length = 0
        self.counter = Counter()

    def update(self, data):
        self.length += len(data)
        self.counter.update(data)

    def digest(self):
        # calculate binary entropy as -Σ(1…n) p_i × log₂(p_i)
        entropy = -sum(count / self.length * log2(count / self.length) for count in self.counter.values())
        return '{:.8f}'.format(entropy)

## Instruction:
Remove TODO's converted to issues
## Code After:
from collections import Counter
from math import log2

from digestive import Sink


class Entropy(Sink):
    def __init__(self):
        super().__init__('entropy')
        self.length = 0
        self.counter = Counter()

    def update(self, data):
        self.length += len(data)
        self.counter.update(data)

    def digest(self):
        # calculate binary entropy as -Σ(1…n) p_i × log₂(p_i)
        entropy = -sum(count / self.length * log2(count / self.length) for count in self.counter.values())
        return '{:.8f}'.format(entropy)
