Ejemplo n.º 1
0
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph):
    delimiter = ';;'
    total = 0.0
    numList = []
    nonNumList = []
    inst_vars = alist.instantiated_attributes().keys()
    for c in children:
        for k, v in c.instantiated_attributes().items():
            if k not in inst_vars and k in alist.attributes and k != tt.OP:
                c.instantiate_variable(k, v)

        opVarValue = c.get(c.get(tt.OPVAR))
        if isinstance(opVarValue, str):
            opVarValue = list(map(str, opVarValue.split(delimiter)))
        else:
            opVarValue = [opVarValue]
        for opval in opVarValue:
            if utils.is_numeric(opval):
                total += float(opval)
                numList.append(float(opval))
                if not str(opval).startswith(vx.NESTING):
                    nonNumList.append(str(opval))

            else:
                # if not c.get(c.get(tt.OPVAR)).startswith(vx.NESTING):
                #     nonNumList.append(c.get(c.get(tt.OPVAR)))
                nonNumList.append(opval)

    if numList or nonNumList:
        if len(numList) >= len(nonNumList):
            opVar = alist.get(tt.OPVAR)
            valueToReturn = total / len(children)
            if opVar == alist.get(tt.TIME):
                valueToReturn = str(int(valueToReturn))
            alist.instantiate_variable(opVar, valueToReturn)
        else:
            # # get modal value
            # valueToReturn = max(nonNumList, key=nonNumList.count)
            counts = dict(Counter(nonNumList))
            counts_set = set(counts.values())
            max_val = max(counts_set)
            items = [x for x, y in counts.items() if y == max_val]
            valueToReturn = f'{delimiter} '.join(map(str, set(items)))

            # if len(nonNumList) == 1:
            #     valueToReturn = nonNumList[0]
            # else:
            #     # return list of different values
            #     valueToReturn = ', '.join(map(str,set(nonNumList)))

            alist.instantiate_variable(alist.get(tt.OPVAR), valueToReturn)
    else:
        return None

    alist.instantiate_variable(
        tt.COV,
        estimate_uncertainty(children,
                             len(numList) == len(children), alist.get(tt.OP),
                             len(children)))
    return alist
Ejemplo n.º 2
0
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph):
    y_predict = None
    X = []
    y = []
    data_pts = []
    for c in children:
        opVarValue = c.instantiation_value(c.get(tt.OPVAR))
        if utils.is_numeric(opVarValue) and utils.is_numeric(c.get(tt.TIME)):
            x_val = utils.get_number(c.get(tt.TIME), None)
            y_val = utils.get_number(opVarValue, None)
            X.append([x_val])
            y.append(y_val)
            data_pts.append([x_val, y_val])
    X = np.array(X)
    y = np.array(y)
    reg = LinearRegression().fit(X, y)
    x_predict = utils.get_number(alist.get(tt.TIME), None)
    y_predict = reg.predict(np.array([[x_predict]]))[0]
    prediction = [x_predict, y_predict]
    coeffs = [v for v in reg.coef_]
    coeffs.insert(0, reg.intercept_)
    fnStr = 'LIN;' + ';'.join([str(v) for v in reg.coef_])
    fnAndData = \
        """{{"function":{coeffs}, "data":{data_pts}, "prediction":{prediction}}}""".format(
            coeffs=coeffs, data_pts=data_pts, prediction=prediction)

    alist.instantiate_variable(alist.get(tt.OPVAR), y_predict)
    alist.set(tt.FNPLOT, fnAndData)

    alist.instantiate_variable(
        tt.COV,
        estimate_uncertainty(children,
                             len(data_pts) == len(children), alist.get(tt.OP),
                             len(children)))
    return alist
Ejemplo n.º 3
0
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph):
    data = [str(x.instantiation_value(alist.get(tt.OPVAR))) for x in children]
    data_str = ','.join(data)
    alist.instantiate_variable(alist.get(tt.OPVAR), data_str)

    # TODO: port code for cov calculations
    alist.instantiate_variable(
        tt.COV,
        estimate_uncertainty(children, False, alist.get(tt.OP), len(children)))
    return alist
Ejemplo n.º 4
0
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph):
    variables = alist.variables()
    data = [x.instantiation_value(alist.get(tt.OPVAR)) for x in children
            if (x not in list(variables.keys()) and x not in list(variables.values()))]
    alist.instantiate_variable(alist.get(tt.OPVAR), len(data))

    alist.instantiate_variable(tt.COV, estimate_uncertainty(
        children, False, alist.get(tt.OP), len(children)
    ))
    return alist
Ejemplo n.º 5
0
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph):
    data = [
        utils.get_number(x.instantiation_value(alist.get(tt.OPVAR)), 0)
        for x in children
    ]
    alist.instantiate_variable(alist.get(tt.OPVAR), sum(data))

    # TODO: port code for cov calculations
    alist.instantiate_variable(
        tt.COV,
        estimate_uncertainty(children, True, alist.get(tt.OP), len(children)))
    return alist
Ejemplo n.º 6
0
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph):
    data = {x: utils.get_number(x.instantiation_value(
        alist.get(tt.OPVAR)), 999999999999999) for x in children}
    minAlist = min(data, key=data.get)
    minValue = data[minAlist]
    alist.instantiate_variable(alist.get(tt.OPVAR), minValue)

    propagate.projections(alist, (minAlist,))

    alist.instantiate_variable(tt.COV, estimate_uncertainty(
        children, True, alist.get(tt.OP), len(children)
    ))
    return alist
Ejemplo n.º 7
0
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph):
    allNumeric = True
    y_predict = None
    X = []
    y = []
    data_pts = []
    for c in children:
        opVarValue = c.instantiation_value(c.get(tt.OPVAR))
        if utils.is_numeric(opVarValue) and utils.is_numeric(c.get(tt.TIME)):
            x_val = utils.get_number(c.get(tt.TIME), None)
            y_val = utils.get_number(opVarValue, None)
            X.append([x_val])
            y.append(y_val)
            data_pts.append([x_val, y_val])
        else:
            allNumeric = False
    X = np.array(X)
    y = np.array(y)

    x_to_predict = utils.get_number(alist.get(tt.TIME), None)
    if not x_to_predict:
        return None
    else:
        x_to_predict = np.array([x_to_predict])

    gp_prediction = do_gpregress(X, y, x_to_predict,
                                 (np.max(y) - np.min(y))**2, 1)

    if gp_prediction is None:
        return None

    y_predict = gp_prediction[0]['y']
    try:
        prediction = [x_to_predict, y_predict]
        alist.instantiate_variable(alist.get(tt.OPVAR), y_predict)
        alist.instantiate_variable(tt.COV,
                                   gp_prediction[0]['stdev'] / y_predict)

        alist.instantiate_variable(
            tt.COV,
            estimate_uncertainty(children, allNumeric, alist.get(tt.OP),
                                 len(children)))
    except Exception as ex:
        print(ex)
        return None

    return alist
Ejemplo n.º 8
0
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph):
    sum = 0.0
    allNumeric = True
    for c in children:
        for k, v in c.instantiated_attributes().items():
            if k in alist.attributes:
                alist.instantiate_variable(k, v)

        opVarValue = c.get(c.get(tt.OPVAR))
        if utils.is_numeric(opVarValue):
            sum += float(opVarValue)
        else:
            allNumeric = False

    alist.instantiate_variable(alist.get(tt.OPVAR), sum / len(children))

    alist.instantiate_variable(
        tt.COV,
        estimate_uncertainty(children, allNumeric, alist.get(tt.OP),
                             len(children)))
    return alist
Ejemplo n.º 9
0
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph):
    sum = 0.0
    nonNumList = []
    for c in children:
        for k, v in c.instantiated_attributes().items():
            if k in alist.attributes:
                alist.instantiate_variable(k, v)

        opVarValue = c.get(c.get(tt.OPVAR))

        if not c.get(c.get(tt.OPVAR)).startswith(vx.NESTING):
            nonNumList.append(c.get(c.get(tt.OPVAR)))

    # get modal value
    valueToReturn = max(nonNumList, key=nonNumList.count)
    alist.instantiate_variable(alist.get(tt.OPVAR), valueToReturn)

    # todo: propagate projection variables of modal alist to parent

    alist.instantiate_variable(tt.COV, estimate_uncertainty(
        children, len(nonNumList) > 0, 'value', len(children)
    ))
    return alist