def geo_mean(values):
    """
    GIVEN AN ARRAY OF dicts, CALC THE GEO-MEAN ON EACH ATTRIBUTE
    """
    agg = Struct()
    for d in values:
        for k, v in d.items():
            if v != 0:
                agg[k] = nvl(agg[k], ZeroMoment.new_instance()) + Math.log(Math.abs(v))
    return {k: Math.exp(v.stats.mean) for k, v in agg.items()}
def geo_mean(values):
    """
    GIVEN AN ARRAY OF dicts, CALC THE GEO-MEAN ON EACH ATTRIBUTE
    """
    agg = Dict()
    for d in values:
        for k, v in d.items():
            if v != 0:
                agg[k] = coalesce(
                    agg[k], ZeroMoment.new_instance()) + Math.log(Math.abs(v))
    return {k: Math.exp(v.stats.mean) for k, v in agg.items()}
Beispiel #3
0
    def __unicode__(self):
        if not self.milli:
            return "zero"

        output = ""
        rest = (self.milli - (MILLI_VALUES.month * self.month)
                )  # DO NOT INCLUDE THE MONTH'S MILLIS
        isNegative = (rest < 0)
        rest = Math.abs(rest)

        # MILLI
        rem = rest % 1000
        if rem != 0:
            output = "+" + unicode(rem) + "milli" + output
        rest = Math.floor(rest / 1000)

        # SECOND
        rem = rest % 60
        if rem != 0:
            output = "+" + unicode(rem) + "second" + output
        rest = Math.floor(rest / 60)

        # MINUTE
        rem = rest % 60
        if rem != 0:
            output = "+" + unicode(rem) + "minute" + output
        rest = Math.floor(rest / 60)

        # HOUR
        rem = rest % 24
        if rem != 0:
            output = "+" + unicode(rem) + "hour" + output
        rest = Math.floor(rest / 24)

        # DAY
        if (rest < 11 and rest != 7) or rest % 10 == 0:
            rem = rest
            rest = 0
        else:
            rem = rest % 7
            rest = Math.floor(rest / 7)

        if rem != 0:
            output = "+" + unicode(rem) + "day" + output

        # WEEK
        if rest != 0:
            output = "+" + unicode(rest) + "week" + output

        if isNegative:
            output = output.replace("+", "-")

        # MONTH AND YEAR
        if self.month:
            sign = "-" if self.month < 0 else "+"
            month = Math.abs(self.month)

            if month <= 18 and month != 12:
                output = sign + unicode(month) + "month" + output
            else:
                m = month % 12
                if m != 0:
                    output = sign + unicode(m) + "month" + output
                y = Math.floor(month / 12)
                output = sign + unicode(y) + "year" + output

        if output[0] == "+":
            output = output[1::]
        if output[0] == '1' and not Math.is_number(output[1]):
            output = output[1::]
        return output
    def __unicode__(self):
        if not self.milli:
            return "zero"

        output = ""
        rest = (self.milli - (MILLI_VALUES.month * self.month)) # DO NOT INCLUDE THE MONTH'S MILLIS
        isNegative = (rest < 0)
        rest = Math.abs(rest)

        # MILLI
        rem = rest % 1000
        if rem != 0:
            output = "+" + unicode(rem) + "milli" + output
        rest = Math.floor(rest / 1000)

        # SECOND
        rem = rest % 60
        if rem != 0:
            output = "+" + unicode(rem) + "second" + output
        rest = Math.floor(rest / 60)

        # MINUTE
        rem = rest % 60
        if rem != 0:
            output = "+" + unicode(rem) + "minute" + output
        rest = Math.floor(rest / 60)

        # HOUR
        rem = rest % 24
        if rem != 0:
            output = "+" + unicode(rem) + "hour" + output
        rest = Math.floor(rest / 24)

        # DAY
        if (rest < 11 and rest != 7) or rest % 10 == 0:
            rem = rest
            rest = 0
        else:
            rem = rest % 7
            rest = Math.floor(rest / 7)

        if rem != 0:
            output = "+" + unicode(rem) + "day" + output

        # WEEK
        if rest != 0:
            output = "+" + unicode(rest) + "week" + output

        if isNegative:
            output = output.replace("+", "-")

        # MONTH AND YEAR
        if self.month:
            sign = "-" if self.month < 0 else "+"
            month = Math.abs(self.month)

            if month <= 18 and month != 12:
                output = sign + unicode(month) + "month" + output
            else:
                m = month % 12
                if m != 0:
                    output = sign + unicode(m) + "month" + output
                y = Math.floor(month / 12)
                output = sign + unicode(y) + "year" + output

        if output[0] == "+":
            output = output[1::]
        if output[0] == '1' and not Math.is_number(output[1]):
            output = output[1::]
        return output