Calculate the entropy of two slices (from the start and end of an axis),
    returning a tuple containing the amount that should be added to the start
    and removed from the end of the axis.
771367
def _compare_entropy(start_slice, end_slice, slice, difference):
    
    start_entropy = utils.image_entropy(start_slice)
    end_entropy = utils.image_entropy(end_slice)
    if end_entropy and abs(start_entropy / end_entropy - 1) < 0.01:
        # Less than 1% difference, remove from both sides.
        if difference >= slice * 2:
            return slice, slice
        half_slice = slice // 2
        return half_slice, slice - half_slice
    if start_entropy > end_entropy:
        return 0, slice
    else:
        return slice, 0
1018896
def total(self, where=None, xbin1=1, xbin2=-2):
        
        nominal, _ = self.sys_hist(None, where=where)
        integral, stat_error = nominal.integral(
            xbin1=xbin1, xbin2=xbin2, error=True)
        ups = [0]
        dns = [0]
        for sys_name in self.sys_names():
            low, high = self.sys_hist(sys_name, where=where)
            up = high.integral(xbin1=xbin1, xbin2=xbin2) - integral
            dn = low.integral(xbin1=xbin1, xbin2=xbin2) - integral
            if up > 0:
                ups.append(up**2)
            else:
                dns.append(up**2)
            if dn > 0:
                ups.append(dn**2)
            else:
                dns.append(dn**2)
        syst_error = (sqrt(sum(ups)), sqrt(sum(dns)))
        return integral, stat_error, syst_error
272402
public static EntropyStats entropy(Matrix m) {
        if (m instanceof SparseMatrix)
            return entropy((SparseMatrix) m);

        double sum = 0;
        double[] colSums = new double[m.columns()];
        double[] rowSums = new double[m.rows()];
        for (int r = 0; r < m.rows(); ++r) {
            for (int c = 0; c < m.columns(); ++c) {
                double v = m.get(r, c);
                sum += v;
                colSums[c] += v;
                rowSums[r] += v;
            }
        }

        double entropy = 0;
        double[] colEntropy = new double[m.columns()];
        double[] rowEntropy = new double[m.rows()];
        for (int r = 0; r < m.rows(); ++r) {
            for (int c = 0; c < m.columns(); ++c) {
                double v = m.get(r, c);
                if (v != 0d) {
                    entropy -= entropy(v, sum);
                    colEntropy[c] -= entropy(v, colSums[c]);
                    rowEntropy[r] -= entropy(v, rowSums[r]);
                }
            }
        }
        return new EntropyStats(entropy, colEntropy, rowEntropy);
    }
113597
def slice(self, start, end):
        

        reverse = False
        if start > end:
            temp = start
            start = end
            end = temp
            reverse = True

        seg = self.copy()
        seg.points = seg.points[start:end+1]
        if reverse:
            seg.points = list(reversed(seg.points))

        return seg
615587
def slice(self, offset=0):
        
        if self.bytes is None:
            start = 0
            stop  = start + self.nbytes
        elif type(self.bytes) is int:
            start = self.bytes
            stop  = start + self.nbytes
        else:
            start = self.bytes[0]
            stop  = self.bytes[1] + 1

        return slice(start + offset, stop + offset)
781058
def entropy_bits_nrange(
        minimum: Union[int, float], maximum: Union[int, float]
) -> float:
    """"""
    # Shannon:
    # d = fabs(maximum - minimum)
    # ent = -(1/d) * log(1/d, 2) * d
    # Aprox form: log10(digits) * log2(10)
    if not isinstance(minimum, (int, float)):
        raise TypeError('minimum can only be int or float')
    if not isinstance(maximum, (int, float)):
        raise TypeError('maximum can only be int or float')
    if minimum < 0:
        raise ValueError('minimum should be greater than 0')
    if maximum < 0:
        raise ValueError('maximum should be greater than 0')

    dif = fabs(maximum - minimum)
    if dif == 0:
        return 0.0

    ent = log10(dif) * 3.321928
    return ent
941994
def entropy_H(self, data):
        """"""

        if not data:
            return 0.0

        occurences = Counter(bytearray(data))

        entropy = 0
        for x in occurences.values():
            p_x = float(x) / len(data)
            entropy -= p_x*math.log(p_x, 2)

        return entropy
1041313
def integer_based_slice(self, ts):
        

        if isinstance(ts, slice):
            try:
                start = Seconds(0) if ts.start is None else ts.start
                if start < Seconds(0):
                    start = self.end + start
                stop = self.end if ts.stop is None else ts.stop
                if stop < Seconds(0):
                    stop = self.end + stop
                duration = stop - start
                ts = TimeSlice(start=start, duration=duration)
            except (ValueError, TypeError):
                pass

        if not isinstance(ts, TimeSlice):
            return ts

        diff = self.duration - self.frequency
        start_index = \
            max(0, np.floor((ts.start - diff) / self.frequency))
        end = self.end if ts.duration is None else ts.end


        # KLUDGE: This is basically arbitrary, but the motivation is that we'd
        # like to differentiate between cases where the slice
        # actually/intentionally overlaps a particular sample, and cases where
        # the slice overlaps the sample by a tiny amount, due to rounding or
        # lack of precision (e.g. Seconds(1) / SR44100().frequency).
        ratio = np.round(end / self.frequency, 2)


        stop_index = np.ceil(ratio)
        return slice(int(start_index), int(stop_index))
607746
def shannon(self, data):
        '''
        
        '''
        entropy = 0

        if data:
            length = len(data)

            seen = dict(((chr(x), 0) for x in range(0, 256)))
            for byte in data:
                seen[byte] += 1

            for x in range(0, 256):
                p_x = float(seen[chr(x)]) / length
                if p_x > 0:
                    entropy -= p_x * math.log(p_x, 2)

        return (entropy / 8)
606478
def get_gain(self, attr_name):
        
        subset_entropy = 0.0
        for value in iterkeys(self._attr_value_counts[attr_name]):
            value_prob = self.get_value_prob(attr_name, value)
            e = self.get_entropy(attr_name, value)
            subset_entropy += value_prob * e
        return (self.main_entropy - subset_entropy)
432676
def maxEntropy(n,k):
  

  s = float(k)/n
  if s > 0.0 and s < 1.0:
    entropy = - s * math.log(s,2) - (1 - s) * math.log(1 - s,2)
  else:
    entropy = 0

  return n*entropy
71264
def entropy_H(self, data):
        """"""
        
        if len(data) == 0:
            return 0.0
        
        occurences = array.array('L', [0]*256)
        
        for x in data:
            occurences[ord(x)] += 1
        
        entropy = 0
        for x in occurences:
            if x:
                p_x = float(x) / len(data)
                entropy -= p_x*math.log(p_x, 2)
        
        return entropy
981341
def slices(self):
        

        return (slice(self.origin[1], self.origin[1] + self.shape[1]),
                slice(self.origin[0], self.origin[0] + self.shape[0]))
900180
def slices_from_global_coords(self, slices):
    
    bbox = self.bbox_to_mip(slices, 0, self.mip)
    return bbox.to_slices()
1040270
def information_coefficient(total1,total2,intersect):
    '''
    '''
    total = total1 + total2
    return 2.0*len(intersect) / total
215660
def _combine_ranges_on_length(self, data_len, first, second):
        '''
        
        '''
        first = get_true_slice(first, data_len)
        second = get_true_slice(second, data_len)
        final_start, final_step, final_stop = (None, None, None)

        # Get our start
        if first.start == None and second.start == None:
            final_start = None
        else:
            final_start = (first.start if first.start else 0)+(second.start if second.start else 0)

        # Get our stop
        if second.stop == None:
            final_stop = first.stop
        elif first.stop == None:
            final_stop = (first.start if first.start else 0) + second.stop
        else:
            final_stop = min(first.stop, (first.start if first.start else 0) + second.stop)

        # Get our step
        if first.step == None and second.step == None:
            final_step = None
        else:
            final_step = (first.step if first.step else 1)*(second.step if second.step else 1)

        # If we have a start above our stop, set them to be equal
        if final_start > final_stop:
            final_start = final_stop

        return slice(final_start, final_stop, final_step)
511638
def cross_entropy_calc(TOP, P, POP):
    
    try:
        result = 0
        for i in TOP.keys():
            reference_likelihood = P[i] / POP[i]
            response_likelihood = TOP[i] / POP[i]
            if response_likelihood != 0 and reference_likelihood != 0:
                result += reference_likelihood * \
                    math.log(response_likelihood, 2)
        return -result
    except Exception:
        return "None"