Example #1
0
def gamma(*args):
    if len(args) != 3:
        raise my_exceptions.ArgumentoInvalido()
    k = args[0]
    nu = args[1]
    o_instance = args[2]

    if isinstance(k, str):
        if k in dir(o_instance):
            k = getattr(o_instance, k)
        else:
            raise my_exceptions.ReferenciaInvalida()
    if isinstance(nu, str):
        if nu in dir(o_instance):
            nu = getattr(o_instance, nu)
        else:
            raise my_exceptions.ReferenciaInvalida()

    if False in [is_number(arg) for arg in args[0:-1]]:
        raise my_exceptions.ErrorDeTipo()

    def gamma_x(x, k=k, nu=nu):
        if x < 0:
            raise my_exceptions.ErrorMatematico()
        else:
            if k < 1.0:
                raise my_exceptions.ErrorMatematico()
            if type(k) != int:
                raise my_exceptions.ImposibleProcesar()
            f_x = nu**k * x**(k - 1) * e**(-nu * x) / factorial(k - 1)
            return f_x

    return gamma_x
Example #2
0
def normal(*args):
    if len(args) != 3:
        raise my_exceptions.ArgumentoInvalido()
    mu = args[0]
    sigma = args[1]
    o_instance = args[2]

    if isinstance(mu, str):
        if mu in dir(o_instance):
            mu = getattr(o_instance, mu)
        else:
            raise my_exceptions.ReferenciaInvalida()
    if isinstance(sigma, str):
        if sigma in dir(o_instance):
            sigma = getattr(o_instance, sigma)
        else:
            raise my_exceptions.ReferenciaInvalida()

    if False in [is_number(arg) for arg in args[0:-1]]:
        raise my_exceptions.ErrorDeTipo()

    def normal_x(x, mu=mu, sigma=sigma):
        if not is_number(x):
            raise my_exceptions.ErrorDeTipo()
        if sigma == 0:
            raise my_exceptions.ErrorMatematico()
        f_x = (1 / sqrt(2 * pi * sigma**2)) * e**((-1 / 2) *
                                                  ((x - mu) / sigma)**2)
        return f_x

    return normal_x
Example #3
0
 def normal_x(x, mu=mu, sigma=sigma):
     if not is_number(x):
         raise my_exceptions.ErrorDeTipo()
     if sigma == 0:
         raise my_exceptions.ErrorMatematico()
     f_x = (1 / sqrt(2 * pi * sigma**2)) * e**((-1 / 2) *
                                               ((x - mu) / sigma)**2)
     return f_x
def LEN(column, o_instance):
    if isinstance(column, str):
        if column in dir(o_instance):
            column = getattr(o_instance, column)
        else:
            raise my_exceptions.ReferenciaInvalida()
    if not is_iterable(column):
        raise my_exceptions.ErrorDeTipo()
    return len(list(column))
def evaluar(function, start, end, step, o_instance):
    if isinstance(function, str):
        if function in dir(o_instance):
            function = getattr(o_instance, function)
        else:
            raise my_exceptions.ReferenciaInvalida()
    if type(function).__name__ != 'function':
        raise my_exceptions.ErrorDeTipo()
    for i in (function(x) for x in float_range(start, end, step)):
        yield i
def comparar(num_1, symbol, num_2, o_instance):
    if isinstance(num_1, str):
        if num_1 in dir(o_instance):
            num_1 = getattr(o_instance, num_1)
        else:
            raise my_exceptions.ReferenciaInvalida()
    if isinstance(num_2, str):
        if num_2 in dir(o_instance):
            num_2 = getattr(o_instance, num_2)
        else:
            raise my_exceptions.ReferenciaInvalida()
    if not is_number(num_1):
        raise my_exceptions.ErrorDeTipo()
    if not is_number(num_2):
        raise my_exceptions.ErrorDeTipo()
    if not isinstance(symbol, str):
        raise my_exceptions.ErrorDeTipo()

    return to_filter(symbol, num_2)(num_1)
def operar(column, symbol, value, o_instance):
    if isinstance(column, str):
        if column in dir(o_instance):
            column = getattr(o_instance, column)
        else:
            raise my_exceptions.ReferenciaInvalida()
    if isinstance(value, str):
        if value in dir(o_instance):
            value = getattr(o_instance, value)
        else:
            raise my_exceptions.ReferenciaInvalida()
    if not is_iterable(column):
        raise my_exceptions.ImposibleProcesar()
    if not isinstance(symbol, str):
        raise my_exceptions.ErrorDeTipo()
    if not str(value).isnumeric():
        raise my_exceptions.ErrorDeTipo()

    for i in map(to_operation(symbol, value), column):
        yield i
Example #8
0
def generate_distribution(model_name, *args):
    available_models = {
        'NORMAL': normal,
        'GAMMA': gamma,
        'EXPONENTIAL': exponential
    }
    if model_name in available_models:
        function = available_models[model_name](*args)
        return function
    else:
        raise my_exceptions.ErrorDeTipo()
def DESV(column, o_instance):
    if isinstance(column, str):
        if column in dir(o_instance):
            column = getattr(o_instance, column)
        else:
            raise my_exceptions.ReferenciaInvalida()
    if not is_iterable(column):
        raise my_exceptions.ErrorDeTipo()

    l_col = list(column)
    avg = PROM(l_col, o_instance)
    sigma = sqrt(sum((i - avg)**2 for i in l_col) / len(l_col))

    return sigma
def PROM(column, o_instance):
    if isinstance(column, str):
        if column in dir(o_instance):
            column = getattr(o_instance, column)
        else:
            raise my_exceptions.ReferenciaInvalida()
    if not is_iterable(column):
        raise my_exceptions.ErrorDeTipo()
    enum = [(i, v) for i, v in enumerate(column)]
    n = len(enum)
    if n == 0:
        raise my_exceptions.ErrorMatematico()
    Sum = sum(j for i, j in enum)
    return Sum / n
Example #11
0
def graficar(column, option, o_instance):
    if isinstance(column, str):
        if column in dir(o_instance):
            column = getattr(o_instance, column)
        else:
            raise my_exceptions.ReferenciaInvalida()
    if not is_iterable(column):
        raise my_exceptions.ImposibleProcesar()
    if not isinstance(option, str):
        raise my_exceptions.ImposibleProcesar()

    available_options = ['numerico', 'normalizado']
    y = np.array(list(column))

    if len(y) == 0:
        raise my_exceptions.ErrorDeTipo()

    if option in available_options:
        if option == 'numerico':
            x = np.array([i for i in range(0, len(y))])
        elif option == 'normalizado':
            suma = sum(y)
            x = np.array([i for i in range(0, len(y))]) / suma
    elif 'rango' in option:
        my_range = to_range(option)
        x = np.array([i for i in my_range])
        if len(x) != len(y):
            raise my_exceptions.ImposibleProcesar()
    elif isinstance(option, str):
        if option in dir(o_instance):
            option = getattr(o_instance, option)
            if is_iterable(option):
                x = np.array(list(option))
                if len(x) != len(y):
                    raise my_exceptions.ImposibleProcesar()
            else:
                raise my_exceptions.ImposibleProcesar()
    elif is_iterable(option):
        x = np.array(list(option))
        if len(x) != len(y):
            raise my_exceptions.ImposibleProcesar()
    else:
        raise my_exceptions.ImposibleProcesar()

    plt.plot(x, y)
    plt.xlabel('X')
    plt.ylabel('Y')
    plt.show()

    return 'Graficando'
def get_column(file_name, column_name, o_instance):
    if not isinstance(file_name, str):
        raise my_exceptions.ErrorDeTipo()
    if not isinstance(column_name, str):
        raise my_exceptions.ErrorDeTipo()
    if not isfile(file_name + '.csv'):
        raise my_exceptions.ReferenciaInvalida()
    with open(file_name + '.csv') as f:
        reader = my_csv.reader(f, delimiter=';')
        header = next(reader)
        header_wo_types = [i.split(':')[0] for i in header]
        df = {header_wo_types[i]: i for i in range(len(header))}

        if column_name not in df:
            raise my_exceptions.ReferenciaInvalida()

        for row in reader:
            v = row[df[column_name]]
            if not is_number(v):
                if v in dir(o_instance):
                    v = getattr(o_instance, v)
                else:
                    raise my_exceptions.ReferenciaInvalida()
            yield float(v)
Example #13
0
def to_operation(symbol, value):
    value = float(value)
    available_symbols = ['*', '/', '+', '-', '>=<']
    if symbol not in available_symbols:
        raise my_exceptions.ErrorDeTipo()
    else:
        if symbol == '*':
            return lambda x: x * value
        elif symbol == '/':
            return lambda x: x / value
        elif symbol == '+':
            return lambda x: x + value
        elif symbol == '-':
            return lambda x: x - value
        elif symbol == '>=<':
            return lambda x: round(x, value)
Example #14
0
def to_filter(symbol, value):
    value = float(value)
    available_symbols = ['==', '>', '<', '>=', '<=', '!=']
    if symbol not in available_symbols:
        raise my_exceptions.ErrorDeTipo()
    else:
        if symbol == '==':
            return lambda x: x == value
        elif symbol == '>':
            return lambda x: x > value
        elif symbol == '<':
            return lambda x: x < value
        elif symbol == '>=':
            return lambda x: x >= value
        elif symbol == '<=':
            return lambda x: x <= value
        elif symbol == '!=':
            return lambda x: x != value
def MEDIAN(column, o_instance):
    if isinstance(column, str):
        if column in dir(o_instance):
            column = getattr(o_instance, column)
        else:
            raise my_exceptions.ReferenciaInvalida()
    if not is_iterable(column):
        raise my_exceptions.ErrorDeTipo()
    l_col = list(column)
    n = len(l_col)
    if n == 0:
        raise my_exceptions.ImposibleProcesar()
    if n % 2 == 0:
        m_1 = l_col[int(n / 2)]
        m_2 = l_col[int(n / 2 - 1)]
        return (m_1 + m_2) / 2
    else:
        m = l_col[int(n / 2)]
        return m
Example #16
0
def exponential(*args):
    if len(args) != 2:
        raise my_exceptions.ArgumentoInvalido()
    nu = args[0]
    o_instance = args[1]

    if isinstance(nu, str):
        if nu in dir(o_instance):
            nu = getattr(o_instance, nu)
        else:
            raise my_exceptions.ReferenciaInvalida()

    if False in [is_number(arg) for arg in args[0:-1]]:
        raise my_exceptions.ErrorDeTipo()

    def exponential_x(x, nu=nu):
        if x < 0:
            raise my_exceptions.ErrorMatematico()
        else:
            f_x = nu * e**(-nu * x)
            return f_x

    return exponential_x