text:
# Test unit for decomon with Dense layers
from __future__ import absolute_import
import pytest
import numpy as np
import tensorflow.python.keras.backend as K
from decomon.backward_layers.backward_layers import get_backward
from decomon.layers.decomon_layers import DecomonActivation, DecomonPermute, DecomonReshape, DecomonFlatten
from tensorflow.keras.layers import Input, Activation, Flatten, Reshape
from . import (
    get_tensor_decomposition_1d_box,
    get_standart_values_1d_box,
    assert_output_properties_box_linear,
    get_standard_values_multid_box,
    get_tensor_decomposition_multid_box,
)
from tensorflow.keras.models import Model

# Activation

"""
@pytest.mark.parametrize(
    "n, activation, mode, previous, floatx",
    [
        (0, "relu", "hybrid", True, 32),
        (1, "relu", "hybrid", True, 32),
        (2, "relu", "hybrid", True, 32),
        (3, "relu", "hybrid", True, 32),
        (4, "relu", "hybrid", True, 32),
        (5, "relu", "hybrid", True, 32),
        (6, "relu", "hybrid", True, 32),
        (0, "relu", "forward", True, 32),
        (1, "relu", "forward", True, 32),
        (2, "relu", "forward", True, 32),
        (3, "relu", "forward", True, 32),
        (4, "relu", "forward", True, 32),
        (5, "relu", "forward", True, 32),
        (6, "relu", "forward", True, 32),
        (0, "relu", "ibp", True, 32),
        (1, "relu", "ibp", True, 32),
        (2, "relu", "ibp", True, 32),
        (3, "relu", "ibp", True, 32),
        (4, "relu", "ibp", True, 32),
        (5, "relu", "ibp", True, 32),
        (6, "relu", "ibp", True, 32),
        (0, "relu", "hybrid", True, 64),
        (1, "relu", "hybrid", True, 64),
        (2, "relu", "hybrid", True, 64),
        (3, "relu", "hybrid", True, 64),
        (4, "relu", "hybrid", True, 64),
        (5, "relu", "hybrid", True, 64),
        (6, "relu", "hybrid", True, 64),
        (0, "relu", "forward", True, 64),
        (1, "relu", "forward", True, 64),
        (2, "relu", "forward", True, 64),
        (3, "relu", "forward", True, 64),
        (4, "relu", "forward", True, 64),
        (5, "relu", "forward", True, 64),
        (6, "relu", "forward", True, 64),
        (0, "relu", "ibp", True, 64),
        (1, "relu", "ibp", True, 64),
        (2, "relu", "ibp", True, 64),
        (3, "relu", "ibp", True, 64),
        (4, "relu", "ibp", True, 64),
        (5, "relu", "ibp", True, 64),
        (6, "relu", "ibp", True, 64),
        (0, "relu", "hybrid", True, 16),
        (1, "relu", "hybrid", True, 16),
        (2, "relu", "hybrid", True, 16),
        (3, "relu", "hybrid", True, 16),
        (4, "relu", "hybrid", True, 16),
        (5, "relu", "hybrid", True, 16),
        (6, "relu", "hybrid", True, 16),
        (0, "relu", "forward", True, 16),
        (1, "relu", "forward", True, 16),
        (2, "relu", "forward", True, 16),
        (3, "relu", "forward", True, 16),
        (4, "relu", "forward", True, 16),
        (5, "relu", "forward", True, 16),
        (6, "relu", "forward", True, 16),
        (0, "relu", "ibp", True, 16),
        (1, "relu", "ibp", True, 16),
        (2, "relu", "ibp", True, 16),
        (3, "relu", "ibp", True, 16),
        (4, "relu", "ibp", True, 16),
        (5, "relu", "ibp", True, 16),
        (6, "relu", "ibp", True, 16),
        (0, "relu", "hybrid", False, 32),
        (1, "relu", "hybrid", False, 32),
        (2, "relu", "hybrid", False, 32),
        (3, "relu", "hybrid", False, 32),
        (4, "relu", "hybrid", False, 32),
        (5, "relu", "hybrid", False, 32),
        (6, "relu", "hybrid", False, 32),
        (0, "relu", "forward", False, 32),
        (1, "relu", "forward", False, 32),
        (2, "relu", "forward", False, 32),
        (3, "relu", "forward", False, 32),
        (4, "relu", "forward", False, 32),
        (5, "relu", "forward", False, 32),
        (6, "relu", "forward", False, 32),
        (0, "relu", "ibp", False, 32),
        (1, "relu", "ibp", False, 32),
        (2, "relu", "ibp", False, 32),
        (3, "relu", "ibp", False, 32),
        (4, "relu", "ibp", False, 32),
        (5, "relu", "ibp", False, 32),
        (6, "relu", "ibp", False, 32),
        (0, "relu", "hybrid", False, 64),
        (1, "relu", "hybrid", False, 64),
        (2, "relu", "hybrid", False, 64),
        (3, "relu", "hybrid", False, 64),
        (4, "relu", "hybrid", False, 64),
        (5, "relu", "hybrid", False, 64),
        (6, "relu", "hybrid", False, 64),
        (0, "relu", "forward", False, 64),
        (1, "relu", "forward", False, 64),
        (2, "relu", "forward", False, 64),
        (3, "relu", "forward", False, 64),
        (4, "relu", "forward", False, 64),
        (5, "relu", "forward", False, 64),
        (6, "relu", "forward", False, 64),
        (0, "relu", "ibp", False, 64),
        (1, "relu", "ibp", False, 64),
        (2, "relu", "ibp", False, 64),
        (3, "relu", "ibp", False, 64),
        (4, "relu", "ibp", False, 64),
        (5, "relu", "ibp", False, 64),
        (6, "relu", "ibp", False, 64),
        (0, "relu", "hybrid", False, 16),
        (1, "relu", "hybrid", False, 16),
        (2, "relu", "hybrid", False, 16),
        (3, "relu", "hybrid", False, 16),
        (4, "relu", "hybrid", False, 16),
        (5, "relu", "hybrid", False, 16),
        (6, "relu", "hybrid", False, 16),
        (0, "relu", "forward", False, 16),
        (1, "relu", "forward", False, 16),
        (2, "relu", "forward", False, 16),
        (3, "relu", "forward", False, 16),
        (4, "relu", "forward", False, 16),
        (5, "relu", "forward", False, 16),
        (6, "relu", "forward", False, 16),
        (0, "relu", "ibp", False, 16),
        (1, "relu", "ibp", False, 16),
        (2, "relu", "ibp", False, 16),
        (3, "relu", "ibp", False, 16),
        (4, "relu", "ibp", False, 16),
        (5, "relu", "ibp", False, 16),
        (6, "relu", "ibp", False, 16),
        (0, "linear", "hybrid", True, 32),
        (1, "linear", "hybrid", True, 32),
        (2, "linear", "hybrid", True, 32),
        (3, "linear", "hybrid", True, 32),
        (4, "linear", "hybrid", True, 32),
        (5, "linear", "hybrid", True, 32),
        (6, "linear", "hybrid", True, 32),
        (0, "linear", "forward", True, 32),
        (1, "linear", "forward", True, 32),
        (2, "linear", "forward", True, 32),
        (3, "linear", "forward", True, 32),
        (4, "linear", "forward", True, 32),
        (5, "linear", "forward", True, 32),
        (6, "linear", "forward", True, 32),
        (0, "linear", "ibp", True, 32),
        (1, "linear", "ibp", True, 32),
        (2, "linear", "ibp", True, 32),
        (3, "linear", "ibp", True, 32),
        (4, "linear", "ibp", True, 32),
        (5, "linear", "ibp", True, 32),
        (6, "linear", "ibp", True, 32),
        (0, None, "hybrid", True, 32),
        (1, None, "hybrid", True, 32),
        (2, None, "hybrid", True, 32),
        (3, None, "hybrid", True, 32),
        (4, None, "hybrid", True, 32),
        (5, None, "hybrid", True, 32),
        (6, None, "hybrid", True, 32),
        (0, None, "forward", True, 32),
        (1, None, "forward", True, 32),
        (2, None, "forward", True, 32),
        (3, None, "forward", True, 32),
        (4, None, "forward", True, 32),
        (5, None, "forward", True, 32),
        (6, None, "forward", True, 32),
        (0, None, "ibp", True, 32),
        (1, None, "ibp", True, 32),
        (2, None, "ibp", True, 32),
        (3, None, "ibp", True, 32),
        (4, None, "ibp", True, 32),
        (5, None, "ibp", True, 32),
        (6, None, "ibp", True, 32),
    ],
)
def test_Backward_Activation_1D_box_model(n, activation, mode, previous, floatx):
    K.set_floatx("float{}".format(floatx))
    eps = K.epsilon()
    decimal = 5
    if floatx == 16:
        K.set_epsilon(1e-2)
        decimal = 2

    layer = DecomonActivation(activation, dc_decomp=False, mode=mode)

    inputs = get_tensor_decomposition_1d_box(dc_decomp=False)
    inputs_ = get_standart_values_1d_box(n, dc_decomp=False)
    x, y, z_, u_c, W_u, b_u, l_c, W_l, b_l = inputs_

    if mode == "hybrid":
        input_mode = inputs[2:]
        output = layer(input_mode)
        z_0, u_c_0, _, _, l_c_0, _, _ = output
    if mode == "forward":
        input_mode = [inputs[2], inputs[4], inputs[5], inputs[7], inputs[8]]
        output = layer(input_mode)
        z_0, _, _, _, _ = output
    if mode == "ibp":
        input_mode = [inputs[3], inputs[6]]
        output = layer(input_mode)
        u_c_0, l_c_0 = output

    w_out = Input((1, 1))
    b_out = Input((1,))
    # get backward layer
    layer_backward = get_backward(layer, previous=previous)
    if previous:
        w_out_u_, b_out_u_, w_out_l_, b_out_l_ = layer_backward(input_mode + [w_out, b_out, w_out, b_out])
        model = Model(inputs[2:] + [w_out, b_out], [w_out_u_, b_out_u_, w_out_l_, b_out_l_])
        w_u_, b_u_, w_l_, b_l_ = model.predict(
            inputs_[2:]
            + [
                np.ones((len(x), 1, 1)),
                np.zeros(
                    (
                        len(x),
                        1,
                    )
                ),
            ]
        )
    else:
        w_out_u_, b_out_u_, w_out_l_, b_out_l_ = layer_backward(input_mode)
        model = Model(inputs[2:], [w_out_u_, b_out_u_, w_out_l_, b_out_l_])
        w_u_, b_u_, w_l_, b_l_ = model.predict(inputs_[2:])

    assert_output_properties_box_linear(
        x,
        None,
        z_[:, 0],
        z_[:, 1],
        None,
        np.sum(np.maximum(w_u_, 0) * W_u + np.minimum(w_u_, 0) * W_l, 1)[:, :, None],
        b_u_ + np.sum(np.maximum(w_u_, 0) * b_u[:, :, None], 1) + np.sum(np.minimum(w_u_, 0) * b_l[:, :, None], 1),
        None,
        np.sum(np.maximum(w_l_, 0) * W_l + np.minimum(w_l_, 0) * W_u, 1)[:, :, None],
        b_l_ + np.sum(np.maximum(w_l_, 0) * b_l[:, :, None], 1) + np.sum(np.minimum(w_l_, 0) * b_u[:, :, None], 1),
        "activation_{}".format(activation),
        decimal=decimal,
    )

    K.set_floatx("float32")
    K.set_epsilon(eps)

@pytest.mark.parametrize(
    "odd, activation, floatx, mode, previous",
    [
        (0, None, 32, "hybrid", True),
        (1, None, 32, "hybrid", True),
        (0, "linear", 32, "hybrid", True),
        (1, "linear", 32, "hybrid", True),
        (0, "relu", 32, "hybrid", True),
        (0, None, 32, "forward", True),
        (1, None, 32, "forward", True),
        (0, "linear", 32, "forward", True),
        (1, "linear", 32, "forward", True),
        (0, "relu", 32, "forward", True),
        (0, None, 32, "ibp", True),
        (1, None, 32, "ibp", True),
        (0, "linear", 32, "ibp", True),
        (1, "linear", 32, "ibp", True),
        (0, "relu", 32, "ibp", True),
        (0, None, 64, "hybrid", True),
        (1, None, 64, "hybrid", True),
        (0, "linear", 64, "hybrid", True),
        (1, "linear", 64, "hybrid", True),
        (0, "relu", 64, "hybrid", True),
        (0, None, 64, "forward", True),
        (1, None, 64, "forward", True),
        (0, "linear", 64, "forward", True),
        (1, "linear", 64, "forward", True),
        (0, "relu", 64, "forward", True),
        (0, None, 64, "ibp", True),
        (1, None, 64, "ibp", True),
        (0, "linear", 64, "ibp", True),
        (1, "linear", 64, "ibp", True),
        (0, "relu", 64, "ibp", True),
        (0, None, 16, "hybrid", True),
        (1, None, 16, "hybrid", True),
        (0, "linear", 16, "hybrid", True),
        (1, "linear", 16, "hybrid", True),
        (0, "relu", 16, "hybrid", True),
        (0, None, 16, "forward", True),
        (1, None, 16, "forward", True),
        (0, "linear", 16, "forward", True),
        (1, "linear", 16, "forward", True),
        (0, "relu", 16, "forward", True),
        (0, None, 16, "ibp", True),
        (1, None, 16, "ibp", True),
        (0, "linear", 16, "ibp", True),
        (1, "linear", 16, "ibp", True),
        (0, "relu", 16, "ibp", True),
        (0, None, 32, "hybrid", False),
        (1, None, 32, "hybrid", False),
        (0, "linear", 32, "hybrid", False),
        (1, "linear", 32, "hybrid", False),
        (0, "relu", 32, "hybrid", False),
        (0, None, 32, "forward", False),
        (1, None, 32, "forward", False),
        (0, "linear", 32, "forward", False),
        (1, "linear", 32, "forward", False),
        (0, "relu", 32, "forward", False),
        (0, None, 32, "ibp", False),
        (1, None, 32, "ibp", False),
        (0, "linear", 32, "ibp", False),
        (1, "linear", 32, "ibp", False),
        (0, "relu", 32, "ibp", False),
        (0, None, 64, "hybrid", False),
        (1, None, 64, "hybrid", False),
        (0, "linear", 64, "hybrid", False),
        (1, "linear", 64, "hybrid", False),
        (0, "relu", 64, "hybrid", False),
        (0, None, 64, "forward", False),
        (1, None, 64, "forward", False),
        (0, "linear", 64, "forward", False),
        (1, "linear", 64, "forward", False),
        (0, "relu", 64, "forward", False),
        (0, None, 64, "ibp", False),
        (1, None, 64, "ibp", False),
        (0, "linear", 64, "ibp", False),
        (1, "linear", 64, "ibp", False),
        (0, "relu", 64, "ibp", False),
        (0, None, 16, "hybrid", False),
        (1, None, 16, "hybrid", False),
        (0, "linear", 16, "hybrid", False),
        (1, "linear", 16, "hybrid", False),
        (0, "relu", 16, "hybrid", False),
        (0, None, 16, "forward", False),
        (1, None, 16, "forward", False),
        (0, "linear", 16, "forward", False),
        (1, "linear", 16, "forward", False),
        (0, "relu", 16, "forward", False),
        (0, None, 16, "ibp", False),
        (1, None, 16, "ibp", False),
        (0, "linear", 16, "ibp", False),
        (1, "linear", 16, "ibp", False),
        (0, "relu", 16, "ibp", False),
    ],
)
def test_Backward_Activation_multiD_box(odd, activation, floatx, mode, previous):

    K.set_floatx("float{}".format(floatx))
    eps = K.epsilon()
    decimal = 5
    if floatx == 16:
        K.set_epsilon(1e-2)
        decimal = 2

    layer = DecomonActivation(activation, dc_decomp=False, mode=mode)
    inputs = get_tensor_decomposition_multid_box(odd, dc_decomp=False)
    inputs_ = get_standard_values_multid_box(odd, dc_decomp=False)
    x, y, z_, u_c, W_u, b_u, l_c, W_l, b_l = inputs_

    if mode == "hybrid":
        input_mode = inputs[2:]
        output = layer(input_mode)
        z_0, u_c_0, _, _, l_c_0, _, _ = output
    if mode == "forward":
        input_mode = [inputs[2], inputs[4], inputs[5], inputs[7], inputs[8]]
        output = layer(input_mode)
        z_0, _, _, _, _ = output
    if mode == "ibp":
        input_mode = [inputs[3], inputs[6]]
        output = layer(input_mode)
        u_c_0, l_c_0 = output

    # output = layer(inputs[2:])
    # z_0, u_c_0, _, _, l_c_0, _, _ = output

    w_out = Input((1, 1))
    b_out = Input((1,))
    # get backward layer
    layer_backward = get_backward(layer, previous=previous)
    if previous:
        w_out_u, b_out_u, w_out_l, b_out_l = layer_backward(input_mode + [w_out, b_out, w_out, b_out])
        f_dense = K.function(inputs + [w_out, b_out], [w_out_u, b_out_u, w_out_l, b_out_l])
        output_ = f_dense(
            inputs_
            + [
                np.ones((len(x), 1, 1)),
                np.zeros(
                    (
                        len(x),
                        1,
                    )
                ),
            ]
        )
    else:
        w_out_u, b_out_u, w_out_l, b_out_l = layer_backward(input_mode)
        f_dense = K.function(inputs, [w_out_u, b_out_u, w_out_l, b_out_l])
        # import pdb; pdb.set_trace()
        output_ = f_dense(inputs_)
    w_u_, b_u_, w_l_, b_l_ = output_

    assert_output_properties_box_linear(
        x,
        None,
        z_[:, 0],
        z_[:, 1],
        None,
        np.sum(np.maximum(w_u_, 0) * W_u + np.minimum(w_u_, 0) * W_l, 1)[:, :, None],
        b_u_ + np.sum(np.maximum(w_u_, 0) * b_u[:, :, None], 1) + np.sum(np.minimum(w_u_, 0) * b_l[:, :, None], 1),
        None,
        np.sum(np.maximum(w_l_, 0) * W_l + np.minimum(w_l_, 0) * W_u, 1)[:, :, None],
        b_l_ + np.sum(np.maximum(w_l_, 0) * b_l[:, :, None], 1) + np.sum(np.minimum(w_l_, 0) * b_u[:, :, None], 1),
        "dense_{}".format(odd),
        decimal=decimal,
    )
    K.set_floatx("float32")
    K.set_epsilon(eps)

@pytest.mark.parametrize(
    "odd, floatx, mode, previous, data_format",
    [
        (0, 32, "hybrid", True, "channels_last"),
        (1, 32, "hybrid", True, "channels_last"),
        (0, 32, "forward", True, "channels_last"),
        (1, 32, "forward", True, "channels_last"),
        (0, 32, "ibp", True, "channels_last"),
        (1, 32, "ibp", True, "channels_last"),
        (0, 32, "hybrid", True, "channels_first"),
        (1, 32, "hybrid", True, "channels_first"),
        (0, 32, "forward", True, "channels_first"),
        (1, 32, "forward", True, "channels_first"),
        (0, 32, "ibp", True, "channels_first"),
        (1, 32, "ibp", True, "channels_first"),
        (0, 32, "hybrid", False, "channels_last"),
        (1, 32, "hybrid", False, "channels_last"),
        (0, 32, "forward", False, "channels_last"),
        (1, 32, "forward", False, "channels_last"),
        (0, 32, "ibp", False, "channels_last"),
        (1, 32, "ibp", False, "channels_last"),
        (0, 32, "hybrid", False, "channels_first"),
        (1, 32, "hybrid", False, "channels_first"),
        (0, 32, "forward", False, "channels_first"),
        (1, 32, "forward", False, "channels_first"),
        (0, 32, "ibp", False, "channels_first"),
        (1, 32, "ibp", False, "channels_first"),
        (0, 64, "hybrid", True, "channels_last"),
        (1, 64, "hybrid", True, "channels_last"),
        (0, 64, "forward", True, "channels_last"),
        (1, 64, "forward", True, "channels_last"),
        (0, 64, "ibp", True, "channels_last"),
        (1, 64, "ibp", True, "channels_last"),
        (0, 64, "hybrid", True, "channels_first"),
        (1, 64, "hybrid", True, "channels_first"),
        (0, 64, "forward", True, "channels_first"),
        (1, 64, "forward", True, "channels_first"),
        (0, 64, "ibp", True, "channels_first"),
        (1, 64, "ibp", True, "channels_first"),
        (0, 64, "hybrid", False, "channels_last"),
        (1, 64, "hybrid", False, "channels_last"),
        (0, 64, "forward", False, "channels_last"),
        (1, 64, "forward", False, "channels_last"),
        (0, 64, "ibp", False, "channels_last"),
        (1, 64, "ibp", False, "channels_last"),
        (0, 64, "hybrid", False, "channels_first"),
        (1, 64, "hybrid", False, "channels_first"),
        (0, 64, "forward", False, "channels_first"),
        (1, 64, "forward", False, "channels_first"),
        (0, 64, "ibp", False, "channels_first"),
        (1, 64, "ibp", False, "channels_first"),
        (0, 16, "hybrid", True, "channels_last"),
        (1, 16, "hybrid", True, "channels_last"),
        (0, 16, "forward", True, "channels_last"),
        (1, 16, "forward", True, "channels_last"),
        (0, 16, "ibp", True, "channels_last"),
        (1, 16, "ibp", True, "channels_last"),
        (0, 16, "hybrid", True, "channels_first"),
        (1, 16, "hybrid", True, "channels_first"),
        (0, 16, "forward", True, "channels_first"),
        (1, 16, "forward", True, "channels_first"),
        (0, 16, "ibp", True, "channels_first"),
        (1, 16, "ibp", True, "channels_first"),
        (0, 16, "hybrid", False, "channels_last"),
        (1, 16, "hybrid", False, "channels_last"),
        (0, 16, "forward", False, "channels_last"),
        (1, 16, "forward", False, "channels_last"),
        (0, 16, "ibp", False, "channels_last"),
        (1, 16, "ibp", False, "channels_last"),
        (0, 16, "hybrid", False, "channels_first"),
        (1, 16, "hybrid", False, "channels_first"),
        (0, 16, "forward", False, "channels_first"),
        (1, 16, "forward", False, "channels_first"),
        (0, 16, "ibp", False, "channels_first"),
        (1, 16, "ibp", False, "channels_first"),
    ],
)
def test_Backward_Flatten_multiD_box(odd, floatx, mode, previous, data_format):

    if data_format == "channels_first" and not len(K._get_available_gpus()):
        return

    K.set_floatx("float{}".format(floatx))
    eps = K.epsilon()
    decimal = 5
    if floatx == 16:
        K.set_epsilon(1e-2)
        decimal = 2

    layer = DecomonFlatten("channels_last", dc_decomp=False, mode=mode)
    inputs = get_tensor_decomposition_multid_box(odd, dc_decomp=False)
    inputs_ = get_standard_values_multid_box(odd, dc_decomp=False)
    x, y, z_, u_c, W_u, b_u, l_c, W_l, b_l = inputs_

    if mode == "hybrid":
        input_mode = inputs[2:]
        output = layer(input_mode)
        z_0, u_c_0, _, _, l_c_0, _, _ = output
    if mode == "forward":
        input_mode = [inputs[2], inputs[4], inputs[5], inputs[7], inputs[8]]
        output = layer(input_mode)
        z_0, _, _, _, _ = output
    if mode == "ibp":
        input_mode = [inputs[3], inputs[6]]
        output = layer(input_mode)
        u_c_0, l_c_0 = output

    # output = layer(inputs[2:])
    # z_0, u_c_0, _, _, l_c_0, _, _ = output

    w_out = Input((1, 1))
    b_out = Input((1,))
    # get backward layer
    layer_backward = get_backward(layer, previous=previous)
    if previous:
        w_out_u, b_out_u, w_out_l, b_out_l = layer_backward(input_mode + [w_out, b_out, w_out, b_out])
        f_dense = K.function(inputs + [w_out, b_out], [w_out_u, b_out_u, w_out_l, b_out_l])
        output_ = f_dense(
            inputs_
            + [
                np.ones((len(x), 1, 1)),
                np.zeros(
                    (
                        len(x),
                        1,
                    )
                ),
            ]
        )
    else:
        w_out_u, b_out_u, w_out_l, b_out_l = layer_backward(input_mode)
        f_dense = K.function(inputs, [w_out_u, b_out_u, w_out_l, b_out_l])
        # import pdb; pdb.set_trace()
        output_ = f_dense(inputs_)
    w_u_, b_u_, w_l_, b_l_ = output_

    assert_output_properties_box_linear(
        x,
        None,
        z_[:, 0],
        z_[:, 1],
        None,
        np.sum(np.maximum(w_u_, 0) * W_u + np.minimum(w_u_, 0) * W_l, 1)[:, :, None],
        b_u_ + np.sum(np.maximum(w_u_, 0) * b_u[:, :, None], 1) + np.sum(np.minimum(w_u_, 0) * b_l[:, :, None], 1),
        None,
        np.sum(np.maximum(w_l_, 0) * W_l + np.minimum(w_l_, 0) * W_u, 1)[:, :, None],
        b_l_ + np.sum(np.maximum(w_l_, 0) * b_l[:, :, None], 1) + np.sum(np.minimum(w_l_, 0) * b_u[:, :, None], 1),
        "dense_{}".format(odd),
        decimal=decimal,
    )
    K.set_floatx("float32")
    K.set_epsilon(eps)

@pytest.mark.parametrize(
    "odd, floatx, mode, previous, data_format",
    [
        (0, 32, "hybrid", True, "channels_last"),
        (1, 32, "hybrid", True, "channels_last"),
        (0, 32, "forward", True, "channels_last"),
        (1, 32, "forward", True, "channels_last"),
        (0, 32, "ibp", True, "channels_last"),
        (1, 32, "ibp", True, "channels_last"),
        (0, 32, "hybrid", True, "channels_first"),
        (1, 32, "hybrid", True, "channels_first"),
        (0, 32, "forward", True, "channels_first"),
        (1, 32, "forward", True, "channels_first"),
        (0, 32, "ibp", True, "channels_first"),
        (1, 32, "ibp", True, "channels_first"),
        (0, 32, "hybrid", False, "channels_last"),
        (1, 32, "hybrid", False, "channels_last"),
        (0, 32, "forward", False, "channels_last"),
        (1, 32, "forward", False, "channels_last"),
        (0, 32, "ibp", False, "channels_last"),
        (1, 32, "ibp", False, "channels_last"),
        (0, 32, "hybrid", False, "channels_first"),
        (1, 32, "hybrid", False, "channels_first"),
        (0, 32, "forward", False, "channels_first"),
        (1, 32, "forward", False, "channels_first"),
        (0, 32, "ibp", False, "channels_first"),
        (1, 32, "ibp", False, "channels_first"),
        (0, 64, "hybrid", True, "channels_last"),
        (1, 64, "hybrid", True, "channels_last"),
        (0, 64, "forward", True, "channels_last"),
        (1, 64, "forward", True, "channels_last"),
        (0, 64, "ibp", True, "channels_last"),
        (1, 64, "ibp", True, "channels_last"),
        (0, 64, "hybrid", True, "channels_first"),
        (1, 64, "hybrid", True, "channels_first"),
        (0, 64, "forward", True, "channels_first"),
        (1, 64, "forward", True, "channels_first"),
        (0, 64, "ibp", True, "channels_first"),
        (1, 64, "ibp", True, "channels_first"),
        (0, 64, "hybrid", False, "channels_last"),
        (1, 64, "hybrid", False, "channels_last"),
        (0, 64, "forward", False, "channels_last"),
        (1, 64, "forward", False, "channels_last"),
        (0, 64, "ibp", False, "channels_last"),
        (1, 64, "ibp", False, "channels_last"),
        (0, 64, "hybrid", False, "channels_first"),
        (1, 64, "hybrid", False, "channels_first"),
        (0, 64, "forward", False, "channels_first"),
        (1, 64, "forward", False, "channels_first"),
        (0, 64, "ibp", False, "channels_first"),
        (1, 64, "ibp", False, "channels_first"),
        (0, 16, "hybrid", True, "channels_last"),
        (1, 16, "hybrid", True, "channels_last"),
        (0, 16, "forward", True, "channels_last"),
        (1, 16, "forward", True, "channels_last"),
        (0, 16, "ibp", True, "channels_last"),
        (1, 16, "ibp", True, "channels_last"),
        (0, 16, "hybrid", True, "channels_first"),
        (1, 16, "hybrid", True, "channels_first"),
        (0, 16, "forward", True, "channels_first"),
        (1, 16, "forward", True, "channels_first"),
        (0, 16, "ibp", True, "channels_first"),
        (1, 16, "ibp", True, "channels_first"),
        (0, 16, "hybrid", False, "channels_last"),
        (1, 16, "hybrid", False, "channels_last"),
        (0, 16, "forward", False, "channels_last"),
        (1, 16, "forward", False, "channels_last"),
        (0, 16, "ibp", False, "channels_last"),
        (1, 16, "ibp", False, "channels_last"),
        (0, 16, "hybrid", False, "channels_first"),
        (1, 16, "hybrid", False, "channels_first"),
        (0, 16, "forward", False, "channels_first"),
        (1, 16, "forward", False, "channels_first"),
        (0, 16, "ibp", False, "channels_first"),
        (1, 16, "ibp", False, "channels_first"),
    ],
)
def test_Backward_Reshape_multiD_box(odd, floatx, mode, previous, data_format):
    if data_format == "channels_first" and not len(K._get_available_gpus()):
        return

    K.set_floatx("float{}".format(floatx))
    eps = K.epsilon()
    decimal = 5
    if floatx == 16:
        K.set_epsilon(1e-2)
        decimal = 2

    layer = DecomonReshape((-1,), dc_decomp=False, mode=mode)
    inputs = get_tensor_decomposition_multid_box(odd, dc_decomp=False)
    inputs_ = get_standard_values_multid_box(odd, dc_decomp=False)
    x, y, z_, u_c, W_u, b_u, l_c, W_l, b_l = inputs_

    if mode == "hybrid":
        input_mode = inputs[2:]
        output = layer(input_mode)
        z_0, u_c_0, _, _, l_c_0, _, _ = output
    if mode == "forward":
        input_mode = [inputs[2], inputs[4], inputs[5], inputs[7], inputs[8]]
        output = layer(input_mode)
        z_0, _, _, _, _ = output
    if mode == "ibp":
        input_mode = [inputs[3], inputs[6]]
        output = layer(input_mode)
        u_c_0, l_c_0 = output

    # output = layer(inputs[2:])
    # z_0, u_c_0, _, _, l_c_0, _, _ = output

    w_out = Input((1, 1))
    b_out = Input((1,))
    # get backward layer
    layer_backward = get_backward(layer, previous=previous)
    if previous:
        w_out_u, b_out_u, w_out_l, b_out_l = layer_backward(input_mode + [w_out, b_out, w_out, b_out])
        f_dense = K.function(inputs + [w_out, b_out], [w_out_u, b_out_u, w_out_l, b_out_l])
        output_ = f_dense(
            inputs_
            + [
                np.ones((len(x), 1, 1)),
                np.zeros(
                    (
                        len(x),
                        1,
                    )
                ),
            ]
        )
    else:
        w_out_u, b_out_u, w_out_l, b_out_l = layer_backward(input_mode)
        f_dense = K.function(inputs, [w_out_u, b_out_u, w_out_l, b_out_l])
        # import pdb; pdb.set_trace()
        output_ = f_dense(inputs_)
    w_u_, b_u_, w_l_, b_l_ = output_

    assert_output_properties_box_linear(
        x,
        None,
        z_[:, 0],
        z_[:, 1],
        None,
        np.sum(np.maximum(w_u_, 0) * W_u + np.minimum(w_u_, 0) * W_l, 1)[:, :, None],
        b_u_ + np.sum(np.maximum(w_u_, 0) * b_u[:, :, None], 1) + np.sum(np.minimum(w_u_, 0) * b_l[:, :, None], 1),
        None,
        np.sum(np.maximum(w_l_, 0) * W_l + np.minimum(w_l_, 0) * W_u, 1)[:, :, None],
        b_l_ + np.sum(np.maximum(w_l_, 0) * b_l[:, :, None], 1) + np.sum(np.minimum(w_l_, 0) * b_u[:, :, None], 1),
        "dense_{}".format(odd),
        decimal=decimal,
    )
    K.set_floatx("float32")
    K.set_epsilon(eps)
"""

@pytest.mark.parametrize(
    "odd, floatx, mode, previous, data_format",
    [
        (0, 32, "hybrid", True, "channels_last"), (1, 32, "hybrid", True, "channels_last"),
        (0, 32, "forward", True, "channels_last"), (1, 32, "forward", True, "channels_last"),
        (0, 32, "ibp", True, "channels_last"), (1, 32, "ibp", True, "channels_last"),
        (0, 32, "hybrid", True, "channels_first"), (1, 32, "hybrid", True, "channels_first"),
        (0, 32, "forward", True, "channels_first"), (1, 32, "forward", True, "channels_first"),
        (0, 32, "ibp", True, "channels_first"), (1, 32, "ibp", True, "channels_first"),
        (0, 32, "hybrid", False, "channels_last"), (1, 32, "hybrid", False, "channels_last"),
        (0, 32, "forward", False, "channels_last"), (1, 32, "forward", False, "channels_last"),
        (0, 32, "ibp", False, "channels_last"), (1, 32, "ibp", False, "channels_last"),
        (0, 32, "hybrid", False, "channels_first"), (1, 32, "hybrid", False, "channels_first"),
        (0, 32, "forward", False, "channels_first"), (1, 32, "forward", False, "channels_first"),
        (0, 32, "ibp", False, "channels_first"), (1, 32, "ibp", False, "channels_first"),
        (0, 64, "hybrid", True, "channels_last"), (1, 64, "hybrid", True, "channels_last"),
        (0, 64, "forward", True, "channels_last"), (1, 64, "forward", True, "channels_last"),
        (0, 64, "ibp", True, "channels_last"), (1, 64, "ibp", True, "channels_last"),
        (0, 64, "hybrid", True, "channels_first"), (1, 64, "hybrid", True, "channels_first"),
        (0, 64, "forward", True, "channels_first"), (1, 64, "forward", True, "channels_first"),
        (0, 64, "ibp", True, "channels_first"), (1, 64, "ibp", True, "channels_first"),
        (0, 64, "hybrid", False, "channels_last"), (1, 64, "hybrid", False, "channels_last"),
        (0, 64, "forward", False, "channels_last"), (1, 64, "forward", False, "channels_last"),
        (0, 64, "ibp", False, "channels_last"), (1, 64, "ibp", False, "channels_last"),
        (0, 64, "hybrid", False, "channels_first"), (1, 64, "hybrid", False, "channels_first"),
        (0, 64, "forward", False, "channels_first"), (1, 64, "forward", False, "channels_first"),
        (0, 64, "ibp", False, "channels_first"), (1, 64, "ibp", False, "channels_first"),
        (0, 16, "hybrid", True, "channels_last"), (1, 16, "hybrid", True, "channels_last"),
        (0, 16, "forward", True, "channels_last"), (1, 16, "forward", True, "channels_last"),
        (0, 16, "ibp", True, "channels_last"), (1, 16, "ibp", True, "channels_last"),
        (0, 16, "hybrid", True, "channels_first"), (1, 16, "hybrid", True, "channels_first"),
        (0, 16, "forward", True, "channels_first"), (1, 16, "forward", True, "channels_first"),
        (0, 16, "ibp", True, "channels_first"), (1, 16, "ibp", True, "channels_first"),
        (0, 16, "hybrid", False, "channels_last"), (1, 16, "hybrid", False, "channels_last"),
        (0, 16, "forward", False, "channels_last"), (1, 16, "forward", False, "channels_last"),
        (0, 16, "ibp", False, "channels_last"), (1, 16, "ibp", False, "channels_last"),
        (0, 16, "hybrid", False, "channels_first"), (1, 16, "hybrid", False, "channels_first"),
        (0, 16, "forward", False, "channels_first"), (1, 16, "forward", False, "channels_first"),
        (0, 16, "ibp", False, "channels_first"), (1, 16, "ibp", False, "channels_first"),

    ]
)
def test_Backward_Permute_multiD_box(odd, floatx, mode, previous, data_format):
    if data_format == 'channels_first' and not len(K._get_available_gpus()):
        return

    K.set_floatx('float{}'.format(floatx))
    eps = K.epsilon()
    decimal = 5
    if floatx == 16:
        K.set_epsilon(1e-2)
        decimal = 2

    inputs = get_tensor_decomposition_multid_box(odd, dc_decomp=False)
    inputs_ = get_standard_values_multid_box(odd, dc_decomp=False)
    x, y, z_, u_c, W_u, b_u, l_c, W_l, b_l = inputs_
    n_dim = len(y.shape) - 1
    target_shape = np.random.permutation(n_dim) + 1
    target_shape_ = tuple([0] + list(target_shape))
    layer = DecomonPermute(target_shape, dc_decomp=False, mode=mode)

    if mode == "hybrid":
        input_mode = inputs[2:]
        output = layer(input_mode)
        z_0, u_c_0, _, _, l_c_0, _, _ = output
    if mode == "forward":
        input_mode = [inputs[2], inputs[4], inputs[5], inputs[7], inputs[8]]
        output = layer(input_mode)
        z_0, _, _, _, _ = output
    if mode == "ibp":
        input_mode = [inputs[3], inputs[6]]
        output = layer(input_mode)
        u_c_0, l_c_0 = output

    # output = layer(inputs[2:])
    # z_0, u_c_0, _, _, l_c_0, _, _ = output
    n_out = y.shape[-1]
    w_out = Input((n_out, n_out))
    b_out = Input((n_out,))

    w_out_ = np.concatenate([np.diag([1]*n_out)[None]]*len(x))
    b_out_ = np.zeros((len(x), n_out))
    # get backward layer
    layer_backward = get_backward(layer, previous=previous)
    if previous:
        w_out_u, b_out_u, w_out_l, b_out_l = layer_backward(input_mode + [w_out, b_out, w_out, b_out])
        f_dense = K.function(inputs + [w_out, b_out], [w_out_u, b_out_u, w_out_l, b_out_l])

        output_ = f_dense(inputs_+[w_out_, b_out_])
        #output_ = f_dense(inputs_ + [np.ones((len(x), 1, 1)), np.zeros((len(x), 1,))])
    else:
        w_out_u, b_out_u, w_out_l, b_out_l = layer_backward(input_mode)
        f_dense = K.function(inputs, [w_out_u, b_out_u, w_out_l, b_out_l])
        # import pdb; pdb.set_trace()
        output_ = f_dense(inputs_)
    w_u_, b_u_, w_l_, b_l_ = output_

    assert_output_properties_box_linear(
        x,
        None,
        z_[:, 0],
        z_[:, 1],
        None,
        np.sum(np.maximum(w_u_, 0) * W_u + np.minimum(w_u_, 0) * W_l, 1)[:, :, None],
        b_u_ + np.sum(np.maximum(w_u_, 0) * b_u[:, :, None], 1) + np.sum(np.minimum(w_u_, 0) * b_l[:, :, None], 1),
        None,
        np.sum(np.maximum(w_l_, 0) * W_l + np.minimum(w_l_, 0) * W_u, 1)[:, :, None],
        b_l_
        + np.sum(np.maximum(w_l_, 0) * b_l[:, :, None], 1)
        + np.sum(np.minimum(w_l_, 0) * b_u[:, :, None], 1),
        "dense_{}".format(odd),
        decimal=decimal
    )

    K.set_floatx("float32")
    K.set_epsilon(eps)

max_stars_repo_name: airbus/decomon

max_issues_repo_name: airbus/decomon

max_forks_repo_name: airbus/decomon


text:
import time
import cv2
import matplotlib.pyplot as plt
import numpy as np
import random

np.set_printoptions(threshold=np.inf)
import fusion
# Kinect module
import pyk4a
from helpers import convert_to_bgra_if_required
from pyk4a import Config, PyK4A
from pyk4a import PyK4APlayback
from icp_modules.ICP_algorithm import *
from icp_modules.FramePreprocessing import PointCloud
from helpers import colorize, convert_to_bgra_if_required
from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog, DatasetCatalog

def SEG_model():
    cfg = get_cfg()
    cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
    cfg.MODEL.DEVICE = 'cpu'
    cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
    cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
    predictor = DefaultPredictor(cfg)
    """
    class 중 label 0 이 human
    pred_masks : mask prediction(segmentation)
    """
    return predictor

def panoptic_model():
    cfg = get_cfg()
    cfg.merge_from_file(model_zoo.get_config_file("COCO-PanopticSegmentation/panoptic_fpn_R_101_3x.yaml"))
    cfg.MODEL.DEVICE = 'cpu'
    cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-PanopticSegmentation/panoptic_fpn_R_101_3x.yaml")
    predictor = DefaultPredictor(cfg)
    return predictor

def human_masking(p_model, color_image, depth_image):
    panoptic_seg, segments_info = p_model(color_image)["panoptic_seg"]
    panop = panoptic_seg.numpy()
    panop[panop != 1] = 0
    panop_3 = np.array([panop, panop, panop])
    panop_3 = panop_3.transpose(1, 2, 0)
    masked_color = np.multiply(color_image, panop_3)
    masked_depth = np.multiply(depth_image, panop)
    return masked_color, masked_depth

def filter_human(output):
    classes = output["instances"].pred_classes
    human = list(np.nonzero(np.where(classes.numpy() == 0, 1, 0))[0])
    boxes = output["instances"].pred_boxes
    if len(boxes.area().numpy()[human]) > 0:
        focus = boxes.area().numpy()[human].argmax()
        mask = output["instances"].pred_masks[human[focus]]
        x, y = np.nonzero(1 - mask.numpy())
        human = True
        return x, y, human
    else:
        human = False
        x = None
        y = None
        return x, y, human

def filter_joint(output, color_img, depth_img):
    joints = output["instances"].pred_keypoints[0][:, :2].numpy()    # 17x2
    H, W, C = color_img.shape
    color_filtered = []
    depth_filtered = []
    for i in range(len(joints)):
        joint_xy = joints[i]
        xx = int(np.round(joint_xy[0]))
        yy = int(np.round(joint_xy[1]))
        filter_color = np.zeros((H, W, 3))
        filter_color[yy, xx] = color_img[yy, xx]
        color_filtered.append(filter_color)
        filter_depth = np.zeros((H, W))
        filter_depth[yy, xx] = depth_img[yy, xx]
        depth_filtered.append(filter_depth)
    return joints, color_filtered, depth_filtered

def vol_to_joint(vols):
    result = []
    for i in range(len(vols)):
        joints, _a, _b, _c = vols[i].get_mesh()
        temp = joints.reshape(-1, 3)
        result.append(np.mean(temp, axis=0))
    result = np.array(result)
    print(result.shape)
    return result

def Joint_model():
    cfg = get_cfg()
    cfg.merge_from_file(model_zoo.get_config_file("COCO-Keypoints/keypoint_rcnn_R_50_FPN_3x.yaml"))
    cfg.MODEL.DEVICE = 'cpu'
    cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7
    cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-Keypoints/keypoint_rcnn_R_50_FPN_3x.yaml")
    predictor = DefaultPredictor(cfg)
    return predictor

def info(playback: PyK4APlayback):
    print(f"Record length: {playback.length / 1000000: 0.2f} sec")
    return int(playback.length / 1000000)

if __name__ == "__main__":
    model = SEG_model()
    joint_model = Joint_model()
    # panoptic = panoptic_model()

    # Open kinect camera by realtime
    k4a = PyK4A(
        Config(
            color_resolution=pyk4a.ColorResolution.RES_720P,
            depth_mode=pyk4a.DepthMode.NFOV_UNBINNED,
            color_format=pyk4a.ImageFormat.COLOR_MJPG
        )
    )

    # Load video file
    filename = r'C:\Users\82106\PycharmProjects\dino_lib\python_kinect_fusion\tsdf-fusion-python-master\yuna2.mkv'
    save_dir = r'C:\Users\82106\PycharmProjects\dino_lib\python_kinect_fusion\tsdf-fusion-python-master' \
               r'\segmentation_check'
    # n_frames = 5

    k4a = PyK4APlayback(filename)
    k4a.open()
    video_frames = info(k4a) * 30   # 30fps

    # Load Kinect's intrinsic parameter
    cam_intr = k4a.calibration.get_camera_matrix(pyk4a.calibration.CalibrationType.COLOR)

    # List 생성
    list_depth_im = []
    list_color_im = []
    # vol_bnds 생성
    vol_bnds = np.zeros((3, 2))
    voxel_size = 0.01
    # while True:
    for i in range(video_frames):
        capture = k4a.get_next_capture()
        if capture.depth is not None and capture.color is not None: #and i%2==0:
            if i < 553:
                continue
            print(f"==========={i}==========")
            # Read depth and color image
            depth_im = capture.transformed_depth.astype(float)
            depth_im /= 1000.  ## depth is saved in 16-bit PNG in millimeters
            depth_im[depth_im >= 2] = 0  # set invalid depth to 0 (specific to 7-scenes dataset) 65.535=2^16/1000
            color_capture = convert_to_bgra_if_required(k4a.configuration["color_format"], capture.color)
            color_im = cv2.cvtColor(color_capture, cv2.COLOR_BGR2RGB)

            filtered_depth = cv2.bilateralFilter(depth_im.astype(np.float32), 5, 35, 35)
            filename = str(save_dir) + '\depth' + str(i) + '.jpg'
            cv2.imwrite(filename, filtered_depth)
            # color_im = cv2.bilateralFilter(color_im, 10, 50, 50)
            # sharpening_2 = np.array([[-1, -1, -1, -1, -1],
            #                          [-1, 2, 2, 2, -1],
            #                          [-1, 2, 9, 2, -1],
            #                          [-1, 2, 2, 2, -1],
            #                          [-1, -1, -1, -1, -1]]) / 9.0
            # color_im = cv2.filter2D(color_im, -1, sharpening_2)
            #
            # # Segmentaion human
            # output = model(color_im)
            # not_valid_x, not_valid_y, human = filter_human(output)
            # if not human:
            #     continue
            # for not_x, not_y in zip(not_valid_x, not_valid_y):
            #     depth_im[not_x, not_y] = 0
            #     color_im[not_x, not_y] = 0
            # # cv2.imshow('color', color_im)
            # # cv2.waitKey(0)
            # # list_depth_im.append(depth_im)
            # # list_color_im.append(color_im)
            # name = str(save_dir) + '\seg' + str(i) + '.jpg'
            # print(name)
            # cv2.imwrite(name, color_im)

ext: py

reason: import numpy


text:
"""
Test functions for tradaboost module.
"""

import numpy as np
from sklearn.linear_model import LinearRegression, LogisticRegression

from adapt.instance_based import (TrAdaBoost,
                                  TrAdaBoostR2,
                                  TwoStageTrAdaBoostR2)

np.random.seed(0)
Xs = np.concatenate((
    np.random.randn(50)*0.1,
    np.random.randn(50)*0.1 + 1.,
)).reshape(-1, 1)
Xt = (np.random.randn(100) * 0.1).reshape(-1, 1)
ys_reg = np.array([1. * x if x<0.5 else
                   10 for x in Xs.ravel()]).reshape(-1, 1)
yt_reg = np.array([1. * x if x<0.5 else
                   10 for x in Xt.ravel()]).reshape(-1, 1)
ys_classif = np.array(
    [x<0 if x<0.5 else x<1 for x in Xs.ravel()]
).astype(float)
yt_classif = np.array(
    [x<0 if x<0.5 else x<1 for x in Xt.ravel()]
).astype(float)

def test_tradaboost_fit():
    np.random.seed(0)
    model = TrAdaBoost(LogisticRegression(penalty='none',
                       solver='lbfgs'),
                       n_estimators=20)
    model.fit(Xs, ys_classif, Xt[:10], yt_classif[:10])
    assert len(model.sample_weights_src_[0]) == 100
    assert (model.sample_weights_src_[0][:50].sum() ==
            model.sample_weights_src_[0][50:].sum())
    assert len(model.sample_weights_tgt_[-1]) == 10
    assert model.sample_weights_tgt_[-1].sum() > 0.3
    assert (model.predict(Xt).ravel() == yt_classif).sum() > 90

def test_tradaboostr2_fit():
    np.random.seed(0)
    model = TrAdaBoostR2(LinearRegression(fit_intercept=False),
                         n_estimators=100)
    model.fit(Xs, ys_reg, Xt[:10], yt_reg[:10])
    assert np.abs(model.estimators_[-1].coef_[0] - 1.) < 1
    assert np.abs(model.sample_weights_src_[-1][:50].sum() /
            model.sample_weights_src_[-1][50:].sum()) > 10
    assert model.sample_weights_tgt_[-1].sum() > 0.7
    assert np.abs(model.predict(Xt) - yt_reg).sum() < 1

def test_twostagetradaboostr2_fit():
    np.random.seed(0)
    model = TwoStageTrAdaBoostR2(LinearRegression(fit_intercept=False),
                         n_estimators=10)
    model.fit(Xs, ys_reg, Xt[:10], yt_reg[:10])
    assert np.abs(model.estimators_[-1].estimators_[-1].coef_[0]
           - 1.) < 1
    assert np.abs(model.sample_weights_src_[-1][:50].sum() /
            model.sample_weights_src_[-1][50:].sum()) > 10
    assert model.sample_weights_tgt_[-1].sum() > 0.7
    assert np.abs(model.predict(Xt) - yt_reg).sum() < 1

max_stars_repo_name: antoinedemathelin/adapt

max_issues_repo_name: antoinedemathelin/adapt

max_forks_repo_name: antoinedemathelin/adapt


text:
import re
from collections import namedtuple
from pathlib import Path

import gemmi
import numpy as numpy
from gemmi.cif import Loop, Document, Style

Limit = namedtuple('Limit', 'h_max, h_min, k_max, k_min, l_max, l_min')

class HKL():
    """
    loop_
      _refln_index_h
      _refln_index_k
      _refln_index_l
      _refln_F_squared_meas
      _refln_F_squared_sigma
      _refln_scale_group_code
    """

    def __init__(self, hkl_file: str, block_name: str, hklf_type: int = 4):
        self._hkl_file = hkl_file
        self.hklf_type = hklf_type
        self._doc: Document = gemmi.cif.Document()
        self._doc.add_new_block(block_name)
        self.block = self._doc.sole_block()
        self._get_hkl_as_block()

    @property
    def hkl_as_cif(self) -> str:
        return self._doc.as_string(style=Style.Simple)

    def _get_hkl_as_block(self):
        loop_header = ['index_h',
                       'index_k',
                       'index_l',
                       'F_squared_meas' if self.hklf_type != 3 else 'F_meas',
                       'F_squared_sigma' if self.hklf_type != 3 else 'F_sigma',
                       'scale_group_code']
        loop: Loop = self.block.init_loop('_refln_', self._trim_header_to_hkl_width(loop_header))
        zero_reflection_pattern = re.compile(r'^\s+0\s+0\s+0\s+0.*')
        for line in self._hkl_file.splitlines(keepends=False):
            splitline = line.split()
            if not splitline:
                continue
            # Do not use data after the 0 0 0 reflection
            if zero_reflection_pattern.match(line):
                loop.add_row(splitline)
                break
            try:
                loop.add_row(splitline[:len(loop_header)])
            # RuntimeError ist from gemmi.cif.add_row:
            except (IndexError, RuntimeError):
                continue

    def __repr__(self) -> str:
        return self.hkl_as_cif[:250]

    def _trim_header_to_hkl_width(self, loop_header):
        hkl_with = self._get_hkl_with()
        trimmed_header = loop_header[:hkl_with]
        return trimmed_header

    def _get_hkl_with(self) -> int:
        first_lines = self._hkl_file[:150].strip().splitlines(keepends=False)
        if len(first_lines) > 1:
            return len(first_lines[1].split())
        return len(first_lines[0].split())

    def get_hkl_min_max(self) -> Limit:
        hkl: gemmi.ReflnBlock = gemmi.hkl_cif_as_refln_block(self.block)
        miller = hkl.make_miller_array()
        h_max, k_max, l_max = numpy.max(miller, axis=0)
        h_min, k_min, l_min = numpy.min(miller, axis=0)
        return Limit(h_max=h_max, h_min=h_min, k_max=k_max, k_min=k_min, l_max=l_max, l_min=l_min)

if __name__ == '__main__':
    h = HKL(Path('tests/examples/work/test_hkl_file.txt').read_text(), '123234')
    # print(h.hkl_as_cif[:250])
    m = h.get_hkl_min_max()
    print(m)

max_stars_repo_name: dkratzert/FinalCif

max_issues_repo_name: dkratzert/FinalCif

max_forks_repo_name: dkratzert/FinalCif


text:
import cv2
import os
import numpy as np
from PIL import Image
import time
import matplotlib.pyplot as plt
from matplotlib.pyplot import cm
import matplotlib.dates as mdates
import pandas as pd
from datetime import datetime
from . import constants

class Graphics():
    """
    Use to process and generate graphics
    """

    def __init__(self):
        self.output_dir = os.getcwd()+f"/{constants.OUTPUT_FOLDER}/"
        if not os.path.exists(self.output_dir):
            os.mkdir(self.output_dir)

    def crop_center(self, pil_img, crop_width, crop_height):
        """
        Crops image with inputs relative to center
        """
        img_width, img_height = pil_img.size
        return pil_img.crop(((img_width - crop_width) // 2,
                            (img_height - crop_height) // 2,
                            (img_width + crop_width) // 2,
                            (img_height + crop_height) // 2))

    def crop_max_square(self, image_filename):
        """
        Crops max square from image
        """

        pil_img = Image.open(image_filename)
        cropped_image = self.crop_center(pil_img, min(pil_img.size), min(pil_img.size))
        cropped_image.save(image_filename, quality=95)

    def graph_trendline(self, username, ts_list, like_list, ts_peak, like_peak, title, lower_ts, upper_ts):
        """
        Creates a trendline image with the identified 9 peaks
        """
        str_dates = pd.date_range(lower_ts, upper_ts).tolist()
        year_dates = [date.to_pydatetime() for date in str_dates]
        year_y_vals = [-100]*len(year_dates)

        fig = plt.figure(figsize=(8, 6), dpi=300)
        sid = 60*60*24
        xmin, xmax = xlim = time.mktime(lower_ts.timetuple())/sid, time.mktime(upper_ts.timetuple())/sid
        ymin, ymax = ylim = 0, max(like_list)*1.2
        ax = fig.add_subplot(111, xlim=xlim, ylim=ylim,
                            autoscale_on=False)
        X = [[0, 0], [0.1, 0.1]]
        ax.imshow(X, interpolation='bicubic', cmap=cm.gnuplot2,
                extent=(xmin, xmax, ymin-1100, ymax+1200), alpha=1)

        ax.plot_date(year_dates, year_y_vals)
        ax.xaxis.set_major_locator(mdates.MonthLocator())
        ax.xaxis.set_major_formatter(mdates.DateFormatter("%b"))
        ax.plot_date(ts_list, like_list, ls='--', color='white')
        ax.plot_date(ts_peak, like_peak, color='red')

        ax.set_title(str(title))
        # ax.grid(ls='dotted', color='blue')
        ax.set_aspect('auto')
        fig.savefig(self.output_dir+username.replace(".",""))
        plt.close()

    def generate_image_matrix(self, username, filename_peak):
        """
        Generates image matrix based on list of image filenames
        """
        image_list = []
        for jpg in filename_peak:
            self.crop_max_square(jpg)
            image = cv2.imread(jpg)
            image_list.append(cv2.resize(image,(800,800)))

        hor1 = (np.hstack([image_list[0],image_list[1],image_list[2]]))
        hor2 = (np.hstack([image_list[3],image_list[4],image_list[5]]))
        hor3 = (np.hstack([image_list[6],image_list[7],image_list[8]]))

        image_matrix=np.vstack([hor1,hor2,hor3])

        cv2.imwrite(self.output_dir+username.replace(".","")+".jpg", image_matrix)


text:
import csv
import logging
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import random
import statistics as stat
import plotly.graph_objects as go

logger = logging.getLogger(__name__)

def set_size(w,h, ax=None):
    """ w, h: width, height in inches """
    if not ax: ax=plt.gca()
    l = ax.figure.subplotpars.left
    r = ax.figure.subplotpars.right
    t = ax.figure.subplotpars.top
    b = ax.figure.subplotpars.bottom
    figw = float(w)/(r-l)
    figh = float(h)/(t-b)
    ax.figure.set_size_inches(figw, figh)

def main():

    horizon = [1,2,5,10,25,50,75,100,200]

    # maximum computation times
    ct_67_1 = [0.664, 1.053, 1.887]
    ct_67_2 = [0.644, 1.221, 1.964, 5.811]
    ct_60_2 = [0.653, 0.951, 1.577]
    ct_50_1 = [0.325, 0.338, 0.987, 1.398, 11.17, 54.95]
    ct_50_2 = [0.418, 0.465, 0.643, 1.87, 38.67, 103.788]
    ct_40_1 = [0.39, 0.488, 0.82, 1.26, 4.61]
    ct_40_2 = [0.292, 0.516, 0.54, 1.12, 4.57, 17.87]
    ct_30_1 = [0.071, 0.0806, 0.184, 0.341, 0.447, 0.609, 0.65, 0.622]
    ct_30_2 = [0.065, 0.11, 0.14, 0.255, 0.595, 1.148]
    COUNT = 5

    colors = plt.cm.rainbow( np.arange(COUNT)/(COUNT-1) )
    fig, ax =plt.subplots()

    ax.plot(horizon[0:len(ct_67_1)], ct_67_1, color=colors[0], linestyle='--', marker='o', label="70")
    ax.plot(horizon[0:len(ct_60_2)], ct_60_2, color=colors[1], linestyle='--', marker='o', label="60")
    ax.plot(horizon[0:len(ct_50_1)], ct_50_1, color=colors[2], linestyle='--', marker='o', label="50")
    ax.plot(horizon[0:len(ct_40_1)], ct_40_1, color=colors[3], linestyle='--', marker='o', label="40")
    ax.plot(horizon[0:len(ct_30_1)], ct_30_1, color=colors[4], linestyle='--', marker='o', label="30")

    ax.grid()
    plt.legend(title="# AGVs")
    set_size(w=5,h=3,ax=ax)
    plt.xlabel("Horizon length")
    plt.ylabel("Peak computation time [s]")
    plt.show()

if __name__ == "__main__":
    main()

max_stars_repo_name: alexberndt/mobile-AGV-optimization

max_issues_repo_name: alexberndt/mobile-AGV-optimization

max_forks_repo_name: alexberndt/mobile-AGV-optimization


text:
import re
import numpy as np
import pandas as pd
from text_pre_processing.method.qam import QA_M
from text_pre_processing.method.qab import QA_B
from text_pre_processing.method.nlim import NLI_M
from text_pre_processing.method.nlib import NLI_B
import progressbar

class AuxiliarySentenceMaker(object):
    def __init__(self, domain):
        self.domain = domain
        print("---auxiliarySentenceMaker initialized---")

    def make_auxiliary_sentences(self, dataframe, filename):
        qa_m_generator = QA_M(self.domain)
        qa_b_generator = QA_B(self.domain)
        nli_m_generator = NLI_M(self.domain)
        nli_b_generator = NLI_B(self.domain)

        bar = progressbar.ProgressBar(maxval=100, \
            widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])
        bar.start()

        qa_m = qa_m_generator.generate_data_frame(dataframe)
        bar.update(25)

        qa_b = qa_b_generator.generate_data_frame(dataframe)
        bar.update(50)

        nli_m = nli_m_generator.generate_data_frame(dataframe)
        bar.update(75)

        nli_b = nli_b_generator.generate_data_frame(dataframe)
        bar.update(100)

        print(filename)

        return {
            'qa_m' : qa_m,
            'qa_b' : qa_b,
            'nli_m' : nli_m,
            'nli_b' : nli_b,
        }


text:
import numpy as np
import inspect # Used for storing the input

class Element:
    def __init__(self, model, nparam=1, nunknowns=0, layers=0, \
                 tsandbc=[(0, 0)], type='z', name='', label=None):
        '''Types of elements
        'g': strength is given through time
        'v': boundary condition is variable through time
        'z': boundary condition is zero through time
        Definition of nlayers, Ncp, Npar, nunknowns:
        nlayers: Number of layers that the element is screened in,
        as set in Element
        Ncp: Number of control points along the element
        nparam: Number of parameters, commonly nlayers * Ncp
        nunknowns: Number of unknown parameters, commonly zero or Npar
        '''
        self.model = model
        self.aq = None # Set in the initialization function
        self.nparam = nparam  # Number of parameters
        self.nunknowns = nunknowns
        self.layers = np.atleast_1d(layers)
        self.nlayers = len(self.layers)
        #
        tsandbc = np.atleast_2d(tsandbc).astype('d')
        tsandbc_error = "tsandQ or tsandh need to be 2D lists" + \
                        " or arrays, like [(0, 1), (2, 5), (8, 0)] "
        assert tsandbc.shape[1] == 2, tsandbc_error
        self.tstart, self.bcin = tsandbc[:,0] - self.model.tstart, tsandbc[:,1]
        if self.tstart[0] > 0:
            self.tstart = np.hstack((np.zeros(1), self.tstart))
            self.bcin = np.hstack((np.zeros(1), self.bcin))
        #
        # 'z' boundary condition thru time or 'v' boundary condition thru time
        self.type = type
        self.name = name
        self.label = label
        if self.label is not None:
            assert self.label not in self.model.elementdict.keys(), \
                   "TTim error: label " + self.label + " already exists"
        self.rzero = 30

    def setbc(self):
        if len(self.tstart) > 1:
            self.bc = np.zeros_like(self.bcin)
            self.bc[0] = self.bcin[0]
            self.bc[1:] = self.bcin[1:] - self.bcin[:-1]
        else:
            self.bc = self.bcin.copy()
        self.ntstart = len(self.tstart)

    def initialize(self):
        '''Initialization of terms that cannot be initialized before other
        elements or the aquifer is defined.
        As we don't want to require a certain order of entering elements,
        these terms are initialized when Model.solve is called
        The initialization class needs to be overloaded
        by all derived classes'''
        pass

    def potinf(self, x, y, aq=None):
        '''Returns complex array of size (nparam, naq, npval)'''
        raise 'Must overload Element.potinf()'

    def potential(self, x, y, aq=None):
        '''Returns complex array of size (ngvbc, naq, npval)'''
        if aq is None:
            aq = self.model.aq.find_aquifer_data(x, y)
        return np.sum(self.parameters[:, :, np.newaxis, :] * \
                      self.potinf(x, y, aq), 1)

    def unitpotential(self, x, y, aq=None):
        '''Returns complex array of size (naq, npval)
        Can be more efficient for given elements'''
        if aq is None:
            aq = self.model.aq.find_aquifer_data(x, y)
        return np.sum(self.potinf(x, y, aq), 0)

    def unitpotentialone(self, x, y, jtime, aq=None):
        '''Returns complex array of size (naq, npval)
        Can be more efficient for given elements'''
        if aq is None:
            aq = self.model.aq.find_aquifer_data(x, y)
        return np.sum(self.potinfone(x, y, jtime, aq), 0)

    def disvecinf(self, x, y, aq=None):
        '''Returns 2 complex arrays of size (nparam, naq, npval)'''
        raise 'Must overload Element.disvecinf()'

    def disvec(self, x, y, aq=None):
        '''Returns 2 complex arrays of size (ngvbc, naq, npval)'''
        if aq is None:
            aq = self.model.aq.find_aquifer_data(x, y)
        qx, qy = self.disvecinf(x, y, aq)
        return np.sum(self.parameters[:, :, np.newaxis, :] * qx, 1), \
               np.sum(self.parameters[:, :, np.newaxis, :] * qy, 1)

    def unitdisvec(self, x, y, aq=None):
        '''Returns 2 complex arrays of size (naq, npval)
        Can be more efficient for given elements'''
        if aq is None:
            aq = self.model.aq.find_aquifer_data(x, y)
        qx, qy = self.disvecinf(x, y, aq)
        return np.sum(qx, 0), np.sum(qy, 0)

    # Functions used to build equations
    def potinflayers(self, x, y, layers=0, aq=None):
        '''layers can be scalar, list, or array.
        returns array of size (len(layers),nparam,npval)
        only used in building equations'''
        if aq is None:
            aq = self.model.aq.find_aquifer_data(x, y)
        pot = self.potinf(x, y, aq)
        rv = np.sum(pot[:, np.newaxis, :, :] * aq.eigvec, 2)
        # first axis needs to be the number of layers
        rv = rv.swapaxes(0, 1)
        return rv[layers, :]

    def potentiallayers(self, x, y, layers=0, aq=None):
        '''Returns complex array of size (ngvbc, len(layers),npval)
        only used in building equations'''
        if aq is None:
            aq = self.model.aq.find_aquifer_data(x, y)
        pot = self.potential(x, y, aq)
        phi = np.sum(pot[:, np.newaxis, :, :] * aq.eigvec, 2)
        return phi[:, layers, :]

    def unitpotentiallayers(self, x, y, layers=0, aq=None):
        '''Returns complex array of size (len(layers), npval)
        only used in building equations'''
        if aq is None:
            aq = self.model.aq.find_aquifer_data(x, y)
        pot = self.unitpotential(x, y, aq)
        phi = np.sum(pot[np.newaxis, :, :] * aq.eigvec, 1)
        return phi[layers, :]

    def disvecinflayers(self, x, y, layers=0, aq=None):
        '''layers can be scalar, list, or array.
        returns 2 arrays of size (len(layers),nparam,npval)
        only used in building equations'''
        if aq is None:
            aq = self.model.aq.find_aquifer_data(x, y)
        qx, qy = self.disvecinf(x, y, aq)
        rvx = np.sum(qx[:, np.newaxis, :, :] * aq.eigvec, 2)
        rvy = np.sum(qy[:, np.newaxis, :, :] * aq.eigvec, 2)
        # first axis needs to be the number of layers
        rvx = rvx.swapaxes(0, 1)
        rvy = rvy.swapaxes(0, 1)
        return rvx[layers, :], rvy[layers, :]

    def disveclayers(self, x, y, layers=0, aq=None):
        '''Returns 2 complex array of size (ngvbc, len(layers), npval)
        only used in building equations'''
        if aq is None:
            aq = self.model.aq.find_aquifer_data(x, y)
        qx, qy = self.disvec(x, y, aq)
        rvx = np.sum(qx[:, np.newaxis, :, :] * aq.eigvec, 2)
        rvy = np.sum(qy[:, np.newaxis, :, :] * aq.eigvec, 2)
        return rvx[:, layers, :], rvy[:, layers, :]

    def unitdisveclayers(self, x, y, layers=0, aq=None):
        '''Returns complex array of size (len(layers), npval)
        only used in building equations'''
        if aq is None:
            aq = self.model.aq.find_aquifer_data(x, y)
        qx, qy = self.unitdisvec(x, y, aq)
        rvx = np.sum(qx[np.newaxis, :, :] * aq.eigvec, 1)
        rvy = np.sum(qy[np.newaxis, :, :] * aq.eigvec, 1)
        return rvx[layers, :], rvy[layers, :]

    # Other functions
    def discharge(self, t, derivative=0):
        """The discharge in each layer

        Parameters
        ----------
        t : scalar, list or array
            times at which discharge is computed.
            t must be ordered and tmin <= t <= tmax

        Returns
        -------
        array of discharges (nlayers,len(t))
            Discharge in each screen with zeros for layers that are not
            screened

        """
        # Could potentially be more efficient if s is pre-computed for
        # all elements, but may not be worthwhile to store as it is quick now
        time = np.atleast_1d(t).astype('d')
        if (time[0] < self.model.tmin) or (time[-1] > self.model.tmax):
            print('Warning, some of the times are smaller than tmin or' + \
                  'larger than tmax; zeros are substituted')
        rv = np.zeros((self.nlayers, np.size(time)))
        if self.type == 'g':
            s = self.dischargeinflayers * self.model.p ** derivative
            for itime in range(self.ntstart):
                time -=  self.tstart[itime]
                for i in range(self.nlayers):
                    rv[i] += self.bc[itime] * \
                             self.model.inverseLapTran(s[i], time)
        else:
            s = np.sum(self.parameters[:, :, np.newaxis, :] *
                       self.dischargeinf, 1)
            s = np.sum(s[:, np.newaxis, :, :] * self.aq.eigvec, 2)
            s = s[:, self.layers, :] * self.model.p ** derivative
            for k in range(self.model.ngvbc):
                e = self.model.gvbclist[k]
                for itime in range(e.ntstart):
                    t = time - e.tstart[itime]
                    if t[-1] >= self.model.tmin:  # Otherwise all zero
                        for i in range(self.nlayers):
                            rv[i] += e.bc[itime] * \
                                     self.model.inverseLapTran(s[k, i], t)
        return rv

    def headinside(self, t):
        print("This function not implemented for this element")
        return

    def storeinput(self, frame):
        self.inputargs, _, _, self.inputvalues = inspect.getargvalues(frame)

    def write(self):
        rv = self.name + '(' + self.model.modelname + ',\n'
        for key in self.inputargs[2:]:  # The first two are ignored
            if isinstance(self.inputvalues[key],np.ndarray):
                rv += key + ' = ' + np.array2string(self.inputvalues[key],
                                                    separator=',') + ',\n'
            elif isinstance(self.inputvalues[key],str):
                rv += key + " = '" + self.inputvalues[key] + "',\n"
            else:
                rv += key + ' = ' + str(self.inputvalues[key]) + ',\n'
        rv += ')\n'
        return rv

    def run_after_solve(self):
        '''function to run after a solution is completed.
        for most elements nothing needs to be done,
        but for strings of elements some arrays may need to be filled'''
        pass

    def plot(self):
        pass

max_stars_repo_name: Huite/ttim

max_issues_repo_name: Huite/ttim

max_forks_repo_name: Huite/ttim


text:
# Data header -     UserID (f.key to User) | ItemID (f.key to Item) | Rating | TimeStamp

# Item Header:      movie id | movie title | release date | video release date |
#                   IMDb URL | unknown | Action | Adventure | Animation |
#                   Children's | Comedy | Crime | Documentary | Drama | Fantasy |
#                   Film-Noir | Horror | Musical | Mystery | Romance | Sci-Fi |
#                   Thriller | War | Western |

# User Header -     UserID | Age | Gender | Occupation | Zip code

# - base files are 80k long approximately.
# - test files are 20k long.

import operator
import numpy as np
import math

#Makes it easy to access, even array indexes are base, uneven are test.
datafiles = ["u1.base", "u1.test", "u2.base", "u2.test", "u3.base", "u3.test", "u4.base", "u4.test", "u5.base", "u5.test"]
itemfile = "u.item"
userfile = "u.user"

seperatorline = "|" #oddly some files don't use "\t"
seperatortab = "\t"

def isint(val):
    try:
        int(val)
        return True
    except:
        return False

def file_to_dict(filename, cut_at_item = 0, seperator = seperatorline, index_limit = 0, key_index_mode = 0):
    res_dict = {}
    f = open(filename, "r")

    for index, line in enumerate(f.readlines()):
        items = line.split(seperator)

        #strings to integer values
        for index, item in enumerate(items):
            if isint(item):
                items[index] = int(item)

        #cut data off, useful to avoid date time
        if cut_at_item != 0:
            items = items[:cut_at_item]

        if key_index_mode == 0:
            res_dict[items[0]] = items[1:]
        else:
            res_dict[tuple(items[:key_index_mode])] = int(items[key_index_mode:][0])
        if index_limit != 0 and index_limit == index:
            break
    return res_dict

def calculate_averages(rating_dict):
    movies = {}
    users = {}

    total = 0
    for index, item in enumerate(rating_dict.keys()):
        userid = item[0]
        movieid = item[1]
        total += rating_dict[item]

        #Wet code, create function
        if userid in users:
            updateval = tuple(map(operator.add, users[userid], (rating_dict[item], 1)))
            users[userid] = updateval
        else:
            users[userid] = (rating_dict[item], 1)

        if movieid in movies:
            updateval = tuple(map(operator.add, movies[movieid], (rating_dict[item], 1)))
            movies[movieid] = updateval
        else:
            movies[movieid] = (rating_dict[item], 1)

    total = total / len(rating_dict)

    #Averaging - create function
    for item in movies.keys():
        movies[item] = movies[item][0] / movies[item][1]

    #Averaging.
    for item in users.keys():
        users[item] = users[item][0] / users[item][1]

    return total, movies, users

def preprocess_values(values, total, movies, users):
    for item in values.keys():
        values[item] = values[item] - users[item[0]] - movies[item[1]] + total

def get_movie_genre_values(movie_item):
    return movie_item[4:]

#Amount = items, factors = factor pr item, value = initial value.
def latent_value_initializer(amount, factors = 17, movie = True):
    if movie:
        return np.random.rand(amount,factors)
    else:
        return np.random.rand(factors, amount)

def add_general_factors(movie_general_factors):
    return 0

def predict(movie_latent_values, user_latent_values, movie, user, movie_avg = 0, user_avg = 0, base = 0):
    generalized = np.dot(movie_latent_values[movie -1,:], user_latent_values[:,user -1])
    #TODO: add in general factors
    generalized -= base
    generalized += movie_avg + user_avg
    return generalized

def main():
    #dict creation
    users = file_to_dict(userfile)
    items = file_to_dict(itemfile)

    #c) Construct a matrix factorization CF model (a.k.a. “Funk-SVD”) for this training data. Use between 10 and 50 latent factors.
    user_latent_values = latent_value_initializer(len(users), movie = False, factors = 100)
    movie_latent_values = latent_value_initializer(len(items), movie = True, factors = 100)

    #RMSE's
    # 10 x 5 runs med 10 factors: RMSE: 0.8022170561522384
    # 10 x 5 runs med 50 factors: RMSE: 0.689057330202062
    # 10 x 5 runs med 100 factors: RMSE 0.54
    for i in range(0,10):
        user_latent_values, movie_latent_values = train(user_latent_values, movie_latent_values, datafiles[0], datafiles[1], baselimit= 80000)
        user_latent_values, movie_latent_values = train(user_latent_values, movie_latent_values, datafiles[2], datafiles[3], baselimit= 80000)
        user_latent_values, movie_latent_values = train(user_latent_values, movie_latent_values, datafiles[4], datafiles[5], baselimit= 80000)
        user_latent_values, movie_latent_values = train(user_latent_values, movie_latent_values, datafiles[6], datafiles[7], baselimit= 80000)
        user_latent_values, movie_latent_values = train(user_latent_values, movie_latent_values, datafiles[8], datafiles[9], baselimit= 80000)

def train(user_latent_values, movie_latent_values, basepath, testpath, baselimit = 10000, testlimit = 200):
    #load base and test
    base1 = file_to_dict(basepath, 3, seperatortab, baselimit, 2)
    test1 = file_to_dict(testpath, 3, seperatortab, testlimit, 2)

    # Look up with base1[1,1] - base1[UserID, MovieID]

    #pre processing - b) Subtract the movie and user means from the training data (the pre-processing step from the slides).
    total, movies_avg, users_avg = calculate_averages({**base1, **test1})

    #Make a copy of both the training and testing set
    base1_unbiased = base1.copy()
    test1_unbiased = test1.copy()

    #Then, change their value to the averages.
    preprocess_values(base1_unbiased, total, movies_avg, users_avg)
    preprocess_values(test1_unbiased, total, movies_avg, users_avg)

    #prediction example

    #I slides er A movies og b er users - Am er en specifik movie column og Bu er en specifik user row.
    for index, item in enumerate(base1.keys()):
        n = 0.001
        unbiased_score = base1_unbiased[item] #R_mu

        #Splitting key.
        user_index = item[0]
        movie_index = item[1]

        movie_avg = movies_avg[movie_index]
        user_avg = users_avg[user_index]

        predict_val = predict(movie_latent_values, user_latent_values, movie_index, user_index)

        #Get the correct row and column
        user_column = user_latent_values[:,user_index -1] #B_ku
        movie_row = movie_latent_values[movie_index -1, :] #A_mk

        #This is refering to slide 32 - I'm not sure why you want to multiply a column or row onto the value you are working with.
        mrow_update_vector = n * (unbiased_score - predict_val) * user_column
        ucol_update_vector = n * movie_row * (unbiased_score - predict_val)

        user_latent_values[:,user_index -1] = np.add(user_column, ucol_update_vector)
        movie_latent_values[movie_index -1, :] = np.add(movie_row, mrow_update_vector)

    #test
    miss_val = 0
    for index, item in enumerate(test1.keys()):
        real_score = test1[item] #R_mu

        user_index = item[0]
        movie_index = item[1]

        movie_avg = movies_avg[movie_index]
        user_avg = users_avg[user_index]

        predict_val = predict(movie_latent_values, user_latent_values, movie_index, user_index, movie_avg, user_avg, total)

        #RMSE
        miss_val += (predict_val - real_score) ** 2

        #print("predicted: " + str(predict_val) + " real score was: " + str(real_score) + " Error is: " + str(real_score - predict_val) )

    miss_val /= len(test1)
    miss_val = math.sqrt(miss_val)
    print("RMSE: " + str(miss_val))
    return user_latent_values, movie_latent_values

if __name__ == "__main__":
    main()

max_stars_repo_name: Mollnitz/Portfolio

max_issues_repo_name: Mollnitz/Portfolio

max_forks_repo_name: Mollnitz/Portfolio


text:
#!/usr/bin/python
# Software License Agreement (BSD License)
#
# Copyright (c) 2013, Juergen Sturm, TUM
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above
#    copyright notice, this list of conditions and the following
#    disclaimer in the documentation and/or other materials provided
#    with the distribution.
#  * Neither the name of TUM nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Requirements:
# sudo apt-get install python-argparse

#
# FYI - Modified to add functionality.
#

"""
This script computes the absolute trajectory error from the ground truth
trajectory and the estimated trajectory.
"""

import sys
import numpy
import argparse
import associate

class EvaluateATE:

    def __init__(self, groundtruthfile, resultsfile):
        self.resultsfile = resultsfile
        self.groundtruthfile = groundtruthfile

    def associate(self, offset = 0.0, max_difference = 0.02):
        self.first_list = associate.read_file_list(self.groundtruthfile)
        self.second_list = associate.read_file_list(self.resultsfile)

        self.matches = associate.associate(self.first_list, self.second_list,float(offset),float(max_difference))
        if len(self.matches)<2:
            sys.exit("Couldn't find matching timestamp pairs between groundtruth and estimated trajectory! Did you choose the correct sequence?")

    def associate_identitiy(self):
        self.first_list = associate.read_file_list(self.resultsfile)
        self.second_list = associate.read_file_list(self.groundtruthfile)

        self.matches = zip(self.first_list, self.second_list)

    #
    # Remove constant offset, uses first point to provide the offset.
    # Multiplier is the hack to fix the ICL-NUIM inverse y axis problem.
    #
    def remove_const_offset(self, multiplier):
        firstGround = self.second_list[self.matches[0][1]]

        for i in range(0, len(self.matches)):
            indexF, indexS = self.matches[i]
            self.first_list[indexF] = (multiplier[0] * (float(self.first_list[indexF][0]) + float(firstGround[0])), multiplier[1] * (float(self.first_list[indexF][1]) + float(firstGround[1]) ), multiplier[2] * (float(self.first_list[indexF][2]) + float(firstGround[2]) ))

    #
    # Direct pairwise ATE evaluation.
    #
    def calculate_no_align(self):
        self.trans_error = []

        for i in range(0, len(self.matches)):
            indexF, indexS = self.matches[i]

            calc_traj = ((float(self.first_list[indexF][0])), (float(self.first_list[indexF][1])), (float(self.first_list[indexF][2])))
            ground_traj = ((float(self.second_list[indexS][0])), (float(self.second_list[indexS][1])), (float(self.second_list[indexS][2])))

            err = (abs(calc_traj[0] - ground_traj[0]), abs(calc_traj[1] - ground_traj[1]), abs(calc_traj[2] - ground_traj[2]))

            err_2 = (err[0] * err[0], err[1] * err[1],err[2] * err[2])
            self.trans_error.append(numpy.sqrt(sum(err_2)))

    def calculate_for_scale(self, scale = 1.0):
        self.first_xyz = numpy.matrix([[float(value) for value in self.first_list[a][0:3]] for a,b in self.matches]).transpose()
        second_xyz = numpy.matrix([[float(value)*float(scale) for value in self.second_list[b][0:3]] for a,b in self.matches]).transpose()
        rot,trans,self.trans_error = align(second_xyz,self.first_xyz)
        self.second_xyz_aligned = rot * second_xyz + trans

        self.first_stamps = self.first_list.keys()
        self.first_stamps.sort()
        self.first_xyz_full = numpy.matrix([[float(value) for value in self.first_list[b][0:3]] for b in self.first_stamps]).transpose()

        self.second_stamps = self.second_list.keys()
        self.second_stamps.sort()
        second_xyz_full = numpy.matrix([[float(value)*float(scale) for value in self.second_list[b][0:3]] for b in self.second_stamps]).transpose()
        self.second_xyz_full_aligned = rot * second_xyz_full + trans

    def latest_ate(self):
        return numpy.sqrt(numpy.dot(self.trans_error,self.trans_error) / len(self.trans_error))

    def latest_print(self, verbose = False):
        if verbose:
            print "compared_pose_pairs %d pairs"%(len(self.trans_error))

            print "absolute_translational_error.rmse %f m"%self.latest_ate()
            print "absolute_translational_error.mean %f m"%numpy.mean(self.trans_error)
            print "absolute_translational_error.median %f m"%numpy.median(self.trans_error)
            print "absolute_translational_error.std %f m"%numpy.std(self.trans_error)
            print "absolute_translational_error.min %f m"%numpy.min(self.trans_error)
            print "absolute_translational_error.max %f m"%numpy.max(self.trans_error)
        else:
            print "%f"%self.latest_ate()

    def latest_save_associations(self, path):
        file = open(path,"w")
        file.write("\n".join(["%f %f %f %f %f %f %f %f"%(a,x1,y1,z1,b,x2,y2,z2) for (a,b),(x1,y1,z1),(x2,y2,z2) in zip(self.matches,self.first_xyz.transpose().A,self.second_xyz_aligned.transpose().A)]))
        file.close()

    def latest_save(self, path):
        file = open(path,"w")
        file.write("\n".join(["%f "%stamp+" ".join(["%f"%d for d in line]) for stamp,line in zip(self.second_stamps,self.second_xyz_full_aligned.transpose().A)]))
        file.close()

    def latest_plot(self, path):
        import matplotlib
        matplotlib.use('Agg')
        import matplotlib.pyplot as plt
        fig = plt.figure()
        ax = fig.add_subplot(111)
        plot_traj(ax, self.first_stamps,
                  self. first_xyz_full.transpose().A, '-',
                  "black", "ground truth")

        plot_traj(ax, self.second_stamps,
                  self.second_xyz_full_aligned.transpose().A, '-',
                  "blue", "estimated")

        label="difference"
        for (a,b),(x1,y1,z1),(x2,y2,z2) in zip(self.matches,self.first_xyz.transpose().A,self.second_xyz_aligned.transpose().A):
            ax.plot([x1,x2],[y1,y2],'-',color="red",label=label)
            label=""

        ax.legend()

        ax.set_xlabel('x [m]')
        ax.set_ylabel('y [m]')
        plt.savefig(path,dpi=90)

    def latest_ate_mean(self):
        return numpy.mean(self.trans_error)

    def latest_ate_median(self):
        return numpy.median(self.trans_error)

    def latest_ate_std(self):
        return numpy.std(self.trans_error)

    def latest_ate_min(self):
        return numpy.min(self.trans_error)

    def latest_ate_max(self):
        return numpy.max(self.trans_error)

def align(model,data):
    """Align two trajectories using the method of Horn (closed-form).

    Input:
    model -- first trajectory (3xn)
    data -- second trajectory (3xn)

    Output:
    rot -- rotation matrix (3x3)
    trans -- translation vector (3x1)
    trans_error -- translational error per point (1xn)

    """
    numpy.set_printoptions(precision=3,suppress=True)
    model_zerocentered = model - model.mean(1)
    data_zerocentered = data - data.mean(1)

    W = numpy.zeros( (3,3) )
    for column in range(model.shape[1]):
        W += numpy.outer(model_zerocentered[:,column],data_zerocentered[:,column])
    U,d,Vh = numpy.linalg.linalg.svd(W.transpose())
    S = numpy.matrix(numpy.identity( 3 ))
    if(numpy.linalg.det(U) * numpy.linalg.det(Vh)<0):
        S[2,2] = -1
    rot = U*S*Vh
    trans = data.mean(1) - rot * model.mean(1)

    model_aligned = rot * model + trans
    alignment_error = model_aligned - data

    # for i in numpy.transpose(alignment_error):
    #     print i

    trans_error = numpy.sqrt(numpy.sum(numpy.multiply(alignment_error,alignment_error),0)).A[0]

    return rot,trans,trans_error

def plot_traj(ax,stamps,traj,style,color,label):
    """
    Plot a trajectory using matplotlib.

    Input:
    ax -- the plot
    stamps -- time stamps (1xn)
    traj -- trajectory (3xn)
    style -- line style
    color -- line color
    label -- plot legend

    """
    stamps.sort()
    interval = numpy.median([s-t for s,t in zip(stamps[1:],stamps[:-1])])
    x = []
    y = []
    last = stamps[0]
    for i in range(len(stamps)):
        if stamps[i]-last < 2*interval:
            x.append(traj[i][0])
            y.append(traj[i][1])
        elif len(x)>0:
            ax.plot(x,y,style,color=color,label=label)
            label=""
            x=[]
            y=[]
        last= stamps[i]
    if len(x)>0:
        ax.plot(x,y,style,color=color,label=label)

if __name__=="__main__":
    # parse command line
    parser = argparse.ArgumentParser(description='''
    This script computes the absolute trajectory error from the ground truth trajectory and the estimated trajectory.
    ''')
    parser.add_argument('first_file', help='ground truth trajectory (format: timestamp tx ty tz qx qy qz qw)')
    parser.add_argument('second_file', help='estimated trajectory (format: timestamp tx ty tz qx qy qz qw)')
    parser.add_argument('--offset', help='time offset added to the timestamps of the second file (default: 0.0)',default=0.0)
    parser.add_argument('--scale', help='scaling factor for the second trajectory (default: 1.0)',default=1.0)
    parser.add_argument('--max_difference', help='maximally allowed time difference for matching entries (default: 0.02)',default=0.02)
    parser.add_argument('--save', help='save aligned second trajectory to disk (format: stamp2 x2 y2 z2)')
    parser.add_argument('--save_associations', help='save associated first and aligned second trajectory to disk (format: stamp1 x1 y1 z1 stamp2 x2 y2 z2)')
    parser.add_argument('--plot', help='plot the first and the aligned second trajectory to an image (format: png)')
    parser.add_argument('--verbose', help='print all evaluation data (otherwise, only the RMSE absolute translational error in meters after alignment will be printed)', action='store_true')
    args = parser.parse_args()

    evaluated = EvaluateATE(args.first_file, args.second_file)
    evaluated.associate(offset = 0.0, max_difference = 0.02)
    evaluated.calculate_for_scale(args.scale)

    evaluated.latest_print(args.verbose)

    if args.save_associations:
        evaluated.latest_save_associations(args.save_associations)

    if args.save:
        evaluated.latest_save(args.save)

    if args.plot:
        evaluated.latest_plot(args.plot)

max_stars_repo_name: ori-drs/supereight

max_issues_repo_name: sotpapathe/supereight

max_forks_repo_name: sotpapathe/supereight


text:
import math
import numpy as np
import random,pickle
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

Nc = 2                  # number of cycle
cyclelength = 14        # cycle length

result_f1 = np.array(pickle.load(open('case1.p', 'rb')))    # no butyrate
result_f2 = np.array(pickle.load(open('case2.p', 'rb')))    # with butyrate + carley
result_f3 = np.array(pickle.load(open('case3.p', 'rb')))    # with butyrate + carley + osteoblast proliferation
result_f4 = np.array(pickle.load(open('case4.p', 'rb')))    # with butyrate + carley + butyrate increase pre-osteoblast to osteoblast differentiation
result_f5 = np.array(pickle.load(open('case5.p', 'rb')))    # with butyrate + carley + butyrate increase wnt10b dependent pre-osteoblast to osteoblast differentiation
result_f6 = np.array(pickle.load(open('case6.p', 'rb')))    # with butyrate + carley + butyrate inhibiting osteoclast by osteoclast death
result_f7 = np.array(pickle.load(open('case7.p', 'rb')))    # with butyrate + carley + butyrate inhibiting osteoclast differentiation
result_f8 = np.array(pickle.load(open('case8.p', 'rb')))    # with butyrate + carley + butyrate increase osteoblast mediated bone formation
result_f9 = np.array(pickle.load(open('case9.p', 'rb')))    # with butyrate + carley + butyrate reduce osteoclast mediated bone resorption
T1 = np.arange(0.0, Nc*cyclelength, 0.001)

# Osteoblast/Osteoclast area under curve
AUC1 = (np.trapz(y=result_f1[:,13], x=T1))/(np.trapz(y=result_f1[:,14], x=T1))
AUC2 = (np.trapz(y=result_f2[:,13], x=T1))/(np.trapz(y=result_f2[:,14], x=T1))
AUC3 = (np.trapz(y=result_f3[:,13], x=T1))/(np.trapz(y=result_f3[:,14], x=T1))
AUC4 = (np.trapz(y=result_f4[:,13], x=T1))/(np.trapz(y=result_f4[:,14], x=T1))
AUC5 = (np.trapz(y=result_f5[:,13], x=T1))/(np.trapz(y=result_f5[:,14], x=T1))
AUC6 = (np.trapz(y=result_f6[:,13], x=T1))/(np.trapz(y=result_f6[:,14], x=T1))
AUC7 = (np.trapz(y=result_f7[:,13], x=T1))/(np.trapz(y=result_f7[:,14], x=T1))
AUC8 = (np.trapz(y=result_f8[:,13], x=T1))/(np.trapz(y=result_f8[:,14], x=T1))
AUC9 = (np.trapz(y=result_f9[:,13], x=T1))/(np.trapz(y=result_f9[:,14], x=T1))

AUCratio = [AUC1,AUC2, AUC3,AUC4,AUC5,AUC6,AUC7,AUC8,AUC9]

plt.rcParams.update({'font.size': 25})
r1 = np.arange(9)
prop_iter = iter(plt.rcParams['axes.prop_cycle'])
for i in range(0,len(r1)):
  plt.bar(r1[i],AUCratio[i],color=next(prop_iter)['color'])

plt.xticks([r for r in range(9)], ['1','2','3','4', '5', '6', '7', '8', '9'])
plt.ylabel('Osteoblast/Osteoclast AUC')
plt.show()

# Pre-Osteoblast/Osteoblast area under curve
AUC1 = (np.trapz(y=result_f1[:,12], x=T1))/(np.trapz(y=result_f1[:,13], x=T1))
AUC2 = (np.trapz(y=result_f2[:,12], x=T1))/(np.trapz(y=result_f2[:,13], x=T1))
AUC3 = (np.trapz(y=result_f3[:,12], x=T1))/(np.trapz(y=result_f3[:,13], x=T1))
AUC4 = (np.trapz(y=result_f4[:,12], x=T1))/(np.trapz(y=result_f4[:,13], x=T1))
AUC5 = (np.trapz(y=result_f5[:,12], x=T1))/(np.trapz(y=result_f5[:,13], x=T1))
AUC6 = (np.trapz(y=result_f6[:,12], x=T1))/(np.trapz(y=result_f6[:,13], x=T1))
AUC7 = (np.trapz(y=result_f7[:,12], x=T1))/(np.trapz(y=result_f7[:,13], x=T1))
AUC8 = (np.trapz(y=result_f8[:,12], x=T1))/(np.trapz(y=result_f8[:,13], x=T1))
AUC9 = (np.trapz(y=result_f9[:,12], x=T1))/(np.trapz(y=result_f9[:,13], x=T1))

AUCratio = [AUC1,AUC2, AUC3,AUC4,AUC5,AUC6,AUC7,AUC8,AUC9]

plt.rcParams.update({'font.size': 25})
r1 = np.arange(9)
prop_iter = iter(plt.rcParams['axes.prop_cycle'])
for i in range(0,len(r1)):
  plt.bar(r1[i],AUCratio[i],color=next(prop_iter)['color'])

plt.xticks([r for r in range(9)], ['1','2','3','4', '5', '6', '7', '8', '9'])
plt.ylabel('Pre-Osteoblast/Osteoblast AUC')
plt.show()

# change in osteoblast from baseline
AUC1 = ((np.trapz(y=result_f1[:,13], x=T1))-(np.trapz(y=result_f1[:,13], x=T1)))/(np.trapz(y=result_f1[:,13], x=T1))
AUC2 = ((np.trapz(y=result_f2[:,13], x=T1))-(np.trapz(y=result_f1[:,13], x=T1)))/(np.trapz(y=result_f1[:,13], x=T1))
AUC3 = ((np.trapz(y=result_f3[:,13], x=T1))-(np.trapz(y=result_f1[:,13], x=T1)))/(np.trapz(y=result_f1[:,13], x=T1))
AUC4 = ((np.trapz(y=result_f4[:,13], x=T1))-(np.trapz(y=result_f1[:,13], x=T1)))/(np.trapz(y=result_f1[:,13], x=T1))
AUC5 = ((np.trapz(y=result_f5[:,13], x=T1))-(np.trapz(y=result_f1[:,13], x=T1)))/(np.trapz(y=result_f1[:,13], x=T1))
AUC6 = ((np.trapz(y=result_f6[:,13], x=T1))-(np.trapz(y=result_f1[:,13], x=T1)))/(np.trapz(y=result_f1[:,13], x=T1))
AUC7 = ((np.trapz(y=result_f7[:,13], x=T1))-(np.trapz(y=result_f1[:,13], x=T1)))/(np.trapz(y=result_f1[:,13], x=T1))
AUC8 = ((np.trapz(y=result_f8[:,13], x=T1))-(np.trapz(y=result_f1[:,13], x=T1)))/(np.trapz(y=result_f1[:,13], x=T1))
AUC9 = ((np.trapz(y=result_f9[:,13], x=T1))-(np.trapz(y=result_f1[:,13], x=T1)))/(np.trapz(y=result_f1[:,13], x=T1))

AUCratio = [AUC1,AUC2, AUC3,AUC4,AUC5,AUC6,AUC7,AUC8,AUC9]

plt.rcParams.update({'font.size': 25})
r1 = np.arange(9)
prop_iter = iter(plt.rcParams['axes.prop_cycle'])
for i in range(0,len(r1)):
  plt.bar(r1[i],AUCratio[i]*100,color=next(prop_iter)['color'])

plt.xticks([r for r in range(9)], ['1','2','3','4', '5', '6', '7', '8', '9'])
plt.ylabel('Change in Osteoblast AUC')
plt.show()

# change in osteoclast from baseline
AUC1 = ((np.trapz(y=result_f1[:,14], x=T1))-(np.trapz(y=result_f1[:,14], x=T1)))/(np.trapz(y=result_f1[:,14], x=T1))
AUC2 = ((np.trapz(y=result_f2[:,14], x=T1))-(np.trapz(y=result_f1[:,14], x=T1)))/(np.trapz(y=result_f1[:,14], x=T1))
AUC3 = ((np.trapz(y=result_f3[:,14], x=T1))-(np.trapz(y=result_f1[:,14], x=T1)))/(np.trapz(y=result_f1[:,14], x=T1))
AUC4 = ((np.trapz(y=result_f4[:,14], x=T1))-(np.trapz(y=result_f1[:,14], x=T1)))/(np.trapz(y=result_f1[:,14], x=T1))
AUC5 = ((np.trapz(y=result_f5[:,14], x=T1))-(np.trapz(y=result_f1[:,14], x=T1)))/(np.trapz(y=result_f1[:,14], x=T1))
AUC6 = ((np.trapz(y=result_f6[:,14], x=T1))-(np.trapz(y=result_f1[:,14], x=T1)))/(np.trapz(y=result_f1[:,14], x=T1))
AUC7 = ((np.trapz(y=result_f7[:,14], x=T1))-(np.trapz(y=result_f1[:,14], x=T1)))/(np.trapz(y=result_f1[:,14], x=T1))
AUC8 = ((np.trapz(y=result_f8[:,14], x=T1))-(np.trapz(y=result_f1[:,14], x=T1)))/(np.trapz(y=result_f1[:,14], x=T1))
AUC9 = ((np.trapz(y=result_f9[:,14], x=T1))-(np.trapz(y=result_f1[:,14], x=T1)))/(np.trapz(y=result_f1[:,14], x=T1))

AUCratio = [AUC1,AUC2, AUC3,AUC4,AUC5,AUC6,AUC7,AUC8,AUC9]

plt.rcParams.update({'font.size': 25})
r1 = np.arange(9)
prop_iter = iter(plt.rcParams['axes.prop_cycle'])
for i in range(0,len(r1)):
  plt.bar(r1[i],AUCratio[i]*100,color=next(prop_iter)['color'])

plt.xticks([r for r in range(9)], ['1','2','3','4', '5', '6', '7', '8', '9'])
plt.ylabel('Change in Osteoclast AUC')
plt.show()

plt.xticks(np.arange(0, 30, step=7))
plt.plot(T1, result_f1[:,15],T1, result_f2[:,15],T1, result_f3[:,15], T1, result_f4[:,15],T1, result_f5[:,15],T1, result_f6[:,15],T1, result_f7[:,15],T1, result_f8[:,15],T1, result_f9[:,15], linewidth=3)
plt.xlabel('Time (days)')
plt.ylabel('Relative bone volume (%)')
plt.errorbar([28],[136.6], yerr=[6.47], fmt='o',color='r', elinewidth=3, markersize=10, capsize=6, capthick=3, barsabove= False)
plt.show()

plt.xticks(np.arange(0, 30, step=7))
plt.plot(T1, result_f1[:,11],T1, result_f2[:,11],T1, result_f3[:,11], T1, result_f4[:,11],T1, result_f5[:,11],T1, result_f6[:,11],T1, result_f7[:,11],T1, result_f8[:,11],T1, result_f9[:,11], linewidth=3)
plt.xlabel('Time (days)')
plt.ylabel('Osteocyte cells')
plt.show()

plt.xticks(np.arange(0, 30, step=7))
plt.plot(T1, result_f1[:,12],T1, result_f2[:,12],T1, result_f3[:,12], T1, result_f4[:,12],T1, result_f5[:,12],T1, result_f6[:,12],T1, result_f7[:,12],T1, result_f8[:,12],T1, result_f9[:,12], linewidth=3)
plt.xlabel('Time (days)')
plt.ylabel('Pre-osteoblast cells')
plt.show()

plt.xticks(np.arange(0, 30, step=7))
plt.plot(T1, result_f1[:,13],T1, result_f2[:,13],T1, result_f3[:,13], T1, result_f4[:,13],T1, result_f5[:,13],T1, result_f6[:,13],T1, result_f7[:,13],T1, result_f8[:,13],T1, result_f9[:,13], linewidth=3)
plt.xlabel('Time (days)')
plt.ylabel('Osteoblast cells')
plt.show()

plt.xticks(np.arange(0, 30, step=7))
plt.plot(T1, result_f1[:,14],T1, result_f2[:,14],T1, result_f3[:,14], T1, result_f4[:,14],T1, result_f5[:,14],T1, result_f6[:,14],T1, result_f7[:,14],T1, result_f8[:,14],T1, result_f9[:,14], linewidth=3)
plt.xlabel('Time (days)')
plt.ylabel('Osteoclast cells')
plt.show()

plt.xticks(np.arange(0, 30, step=7))
plt.plot(T1, result_f1[:,10],'--',color = 'magenta', linewidth=3)
plt.plot(T1, result_f2[:,10],color = 'purple',  linewidth=3)
plt.xlabel('Time (days)')
plt.ylabel('Wnt10b fold change')
plt.legend(['without butyrate','with butyrate'])
plt.show()

max_stars_repo_name: ashleefv/butyrateGutBoneAxis

max_issues_repo_name: ashleefv/butyrateGutBoneAxis

max_forks_repo_name: ashleefv/butyrateGutBoneAxis