text:
from numpy.core.fromnumeric import shape
import pinocchio as pin
import crocoddyl
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter, MultipleLocator, AutoMinorLocator

def plotSolutionOneLeg(solver, dt, bounds = True):
    rmodel = solver.problem.runningModels[0].state.pinocchio
    xs, us = solver.xs, solver.us
    data_coef = [-1. ,1. ,-1.] # data adjustments if on real robot direction is reverced

    if bounds:
        us_lb, us_ub = [], []
        xs_lb, xs_ub = [], []
    models = solver.problem.runningModels.tolist() + [solver.problem.terminalModel]
    for m in models:
        us_lb += [m.u_lb]
        us_ub += [m.u_ub]
        xs_lb += [m.state.lb]
        xs_ub += [m.state.ub]

    # Getting the state and control trajectories
    nx, nq, nu = xs[0].shape[0], rmodel.nq, us[0].shape[0]
    X = [0.] * nx
    U = [0.] * nu
    if bounds:
        U_LB = [0.] * nu
        U_UB = [0.] * nu
        X_LB = [0.] * nx
        X_UB = [0.] * nx
    for i in range(nx):
        X[i] = [np.asscalar(x[i]) for x in xs]
        if bounds:
            X_LB[i] = [np.asscalar(x[i]) for x in xs_lb]
            X_UB[i] = [np.asscalar(x[i]) for x in xs_ub]
    for i in range(nu):
        U[i] = [np.asscalar(u[i]) if u.shape[0] != 0 else 0 for u in us]
        if bounds:
            U_LB[i] = [np.asscalar(u[i]) if u.shape[0] != 0 else np.nan for u in us_lb]
            U_UB[i] = [np.asscalar(u[i]) if u.shape[0] != 0 else np.nan for u in us_ub]

    # Plotting the joint positions, velocities and torques
    plt.figure()
    plt.suptitle('Simulated jumping motion - joints trajectory')
    legJointNames = ['Hip', 'Knee', 'Ankle']
    colors = ['royalblue','orange','mediumseagreen']
    # LF foot
    plt.subplot(1, 3, 1)
    # plt.title('Joint position')
    [plt.plot([i*dt for i in range(len(X[k]))], data_coef[i]*np.array(X[k]), label = legJointNames[i]) for i, k in enumerate(range(1, 4))]
    if bounds:
        [plt.plot([i*dt for i in range(len(X_LB[k]))], X_LB[k], linestyle='dashed', color = colors[i]) for i, k in enumerate(range(1, 4))]
        [plt.plot([i*dt for i in range(len(X_UB[k]))], X_UB[k], linestyle='dashed', color = colors[i]) for i, k in enumerate(range(1, 4))]
    plt.ylabel('position [rad]')
    plt.xlabel('time [s]')
    plt.legend(loc='upper left')
    plt.subplot(1, 3, 2)
    # plt.title('Joint velocity')
    [plt.plot([i*dt for i in range(len(X[k]))], data_coef[i]*np.array(X[k]), label=legJointNames[i], color = colors[i]) for i, k in enumerate(range(nq + 1, nq + 4))]
    if bounds:
        [plt.plot([i*dt for i in range(len(X_LB[k]))], X_LB[k], linestyle='dashed', color = colors[i]) for i, k in enumerate(range(nq + 1, nq + 4))]
        [plt.plot([i*dt for i in range(len(X_UB[k]))], X_UB[k], linestyle='dashed', color = colors[i]) for i, k in enumerate(range(nq + 1, nq + 4))]
    plt.ylabel('velocity [rad/s]')
    plt.xlabel('time [s]')
    plt.legend(loc='upper left')
    plt.subplot(1, 3, 3)
    # plt.title('Joint torque')
    [plt.plot([i*dt for i in range(len(U[k]))], data_coef[i]*np.array(U[k]), label=legJointNames[i], color = colors[i]) for i, k in enumerate(range(0, 3))]
    if bounds:
        [plt.plot([i*dt for i in range(len(U_LB[k]))], U_LB[k], linestyle='dashed', color = colors[i]) for i, k in enumerate(range(0, 3))]
        [plt.plot([i*dt for i in range(len(U_UB[k]))], U_UB[k], linestyle='dashed', color = colors[i]) for i, k in enumerate(range(0, 3))]
    plt.ylabel('torque  [N*m]')
    plt.xlabel('time [s]')
    plt.legend(loc='upper left')

    us_lb, us_ub = [], []
    xs_lb, xs_ub = [], []

    plt.figure()
    plt.suptitle('CoM position and body height analysis')
    rdata = rmodel.createData()
    Cx = []
    Cy = []
    Cz = []
    for x in xs:
        q = x[:rmodel.nq]
        c = pin.centerOfMass(rmodel, rdata, q)
        Cx.append(np.asscalar(c[0]))
        Cy.append(np.asscalar(c[1]))
        Cz.append(np.asscalar(c[2]))

    plt.subplot(1, 3, 1)
    plt.plot(Cx, Cy)
    plt.title('CoM xy plane')
    plt.xlabel('x position [m]')
    plt.ylabel('y position [m]')
    plt.grid(True)
    plt.subplot(1, 3, 2)
    plt.plot(Cx, Cz)
    plt.title('CoM xz plane')
    plt.xlabel('x position [m]')
    plt.ylabel('z position [m]')
    plt.grid(True)
    plt.subplot(1, 3, 3)
    plt.plot([i*dt for i in range(len(X[0]))], X[0])
    plt.title('Body height through time')
    plt.ylabel('z position [m]')
    plt.xlabel('time [s]')
    plt.grid(True)
    # plt.show()
    return

def plotSolutionOneLegSeparate(solver, dt, bounds = True):
    rmodel = solver.problem.runningModels[0].state.pinocchio
    xs, us = solver.xs, solver.us
    plot_m = 3
    data_coef = [-1. ,1. ,-1.] # data adjustments if on real robot direction is reverced

    if bounds:
        us_lb, us_ub = [], []
        xs_lb, xs_ub = [], []
        models = solver.problem.runningModels.tolist() + [solver.problem.terminalModel]
        for m in models:
            us_lb += [m.u_lb]
            us_ub += [m.u_ub]
            xs_lb += [m.state.lb]
            xs_ub += [m.state.ub]

    colorst = ['orange','brown','purple']

    # Getting the state and control trajectories
    nx, nq, nu = xs[0].shape[0], rmodel.nq, us[0].shape[0]
    X = [0.] * nx
    U = [0.] * nu
    if bounds:
        U_LB = [0.] * nu
        U_UB = [0.] * nu
        X_LB = [0.] * nx
        X_UB = [0.] * nx
    for i in range(nx):
        X[i] = [np.asscalar(x[i]) for x in xs]
        if bounds:
            X_LB[i] = [np.asscalar(x[i]) for x in xs_lb]
            X_UB[i] = [np.asscalar(x[i]) for x in xs_ub]
    for i in range(nu):
        U[i] = [np.asscalar(u[i]) if u.shape[0] != 0 else 0 for u in us]
        if bounds:
            U_LB[i] = [np.asscalar(u[i]) if u.shape[0] != 0 else np.nan for u in us_lb]
            U_UB[i] = [np.asscalar(u[i]) if u.shape[0] != 0 else np.nan for u in us_ub]

    fig, axs = plt.subplots(plot_m, plot_m, sharex='col')
    legJointNames = ['Hip', 'Knee', 'Ankle']
    colorsb = ['royalblue','orange','mediumseagreen']
    labels = ["Position","Velocity","Torque"]

    # Ploting the positions
    for i, k in enumerate(range(1, 4)):
        axs[0, i].plot([i*dt for i in range(len(X[k]))], data_coef[i]*np.array(X[k]), label = labels[0], color = colorst[0])
        if bounds:
            axs[0, i].plot([i*dt for i in range(len(X_LB[k]))], X_LB[k], linestyle='dashed', color = colorst[0])
            axs[0, i].plot([i*dt for i in range(len(X_UB[k]))], X_UB[k], linestyle='dashed', color = colorst[0])
        axs[0, i].title.set_text('{} Actuator'.format(legJointNames[i]))
        axs[0, i].set_ylabel('rad')
        # axs[0, i].set_ylabel('position [rad]')
        axs[0, i].grid(True)
        axs[0, i].legend(loc='upper left')
    # Plotting the joint positions, velocities and torques

    for i, k in enumerate(range(nq + 1, nq + 4)):
        axs[1, i].plot([i*dt for i in range(len(X[k]))], data_coef[i]*np.array(X[k]), label = labels[1], color = colorst[1])
        if bounds:
            axs[1, i].plot([i*dt for i in range(len(X_LB[k]))], X_LB[k], linestyle='dashed', color = colorst[1])
            axs[1, i].plot([i*dt for i in range(len(X_UB[k]))], X_UB[k], linestyle='dashed', color = colorst[1])
        axs[1, i].set_ylabel('rad/s')
        # axs[1, i].set_ylabel('velocity [rad/s]')
        axs[1, i].grid(True)
        axs[1, i].legend(loc='upper left')

    for i, k in enumerate(range(0, 3)):
        axs[2, i].plot([i*dt for i in range(len(U[k]))], data_coef[i]*np.array(U[k]), label = labels[2], color = colorst[2])
        if bounds:
            axs[2, i].plot([i*dt for i in range(len(U_LB[k]))], U_LB[k], linestyle='dashed', color = colorst[2])
            axs[2, i].plot([i*dt for i in range(len(U_UB[k]))], U_UB[k], linestyle='dashed', color = colorst[2])
        axs[2, i].set_ylabel('N*m')
        # axs[2, i].set_ylabel('torque  [N*m]')
        axs[2, i].grid(True)
        axs[2, i].legend(loc='upper left')
        axs[2, i].set_xlabel('time [s]')

    fig.suptitle('Simulated jumping motion - joints trajectory')
    fig.tight_layout()
    plt.subplots_adjust(wspace=0.2, hspace=0)

    fig, axs = plt.subplots(1, 3)
    fig.suptitle('CoM position and body height analysis')
    rdata = rmodel.createData()
    Cx = []
    Cy = []
    Cz = []
    for x in xs:
        q = x[:rmodel.nq]
        c = pin.centerOfMass(rmodel, rdata, q)
        Cx.append(np.asscalar(c[0]))
        Cy.append(np.asscalar(c[1]))
        Cz.append(np.asscalar(c[2]))

    axs[0].plot(Cx, Cy, label = 'CoM y position')
    axs[0].title.set_text('CoM xy-plane')
    axs[0].set_xlabel('x position [m]')
    axs[0].set_ylabel('m')
    axs[0].grid(True)
    axs[0].xaxis.set_major_locator(MultipleLocator(0.01))
    axs[0].legend(loc='upper left')
    axs[0].xaxis.set_minor_locator(AutoMinorLocator(5))
    axs[0].grid(which='minor', color='#CCCCCC', linestyle=':')
    axs[0].xaxis.set_major_formatter(FormatStrFormatter('%.2f'))

    axs[1].plot(Cx, Cz, label = 'CoM z position')
    axs[1].title.set_text('CoM xz-plane')
    axs[1].set_xlabel('x position [m]')
    axs[1].set_ylabel('m')
    axs[1].grid(True)
    axs[1].xaxis.set_major_locator(MultipleLocator(0.01))
    axs[1].legend(loc='upper left')
    axs[1].xaxis.set_minor_locator(AutoMinorLocator(5))
    axs[1].grid(which='minor', color='#CCCCCC', linestyle=':')
    axs[1].xaxis.set_major_formatter(FormatStrFormatter('%.2f'))

    axs[2].plot([i*dt for i in range(len(X[0]))], X[0], label = 'Body z position')
    axs[2].title.set_text('Body height through time')
    axs[2].legend(loc='upper left')
    axs[2].set_ylabel('m')
    axs[2].set_xlabel('time [s]')
    axs[2].grid(True)
    axs[2].xaxis.set_major_formatter(FormatStrFormatter('%.2f'))

    # plt.show()
    return

def plotConvergence(costs, muLM, muV, gamma, theta, alpha, figIndex=1, show=True, figTitle=""):
    plt.rcParams["pdf.fonttype"] = 42
    plt.rcParams["ps.fonttype"] = 42
    plt.figure(figIndex, figsize=(6.4, 8))

    # Plotting the total cost sequence
    plt.subplot(611)
    plt.ylabel("cost")
    plt.plot(costs)
    plt.title(figTitle, fontsize=14)

    # Ploting mu sequences
    plt.subplot(612)
    plt.ylabel("muLM")
    plt.plot(muLM)
    plt.subplot(613)
    plt.ylabel("muV")
    plt.plot(muV)

    # Plotting the gradient sequence (gamma and theta)
    plt.subplot(614)
    plt.ylabel("gamma")
    plt.plot(gamma)
    plt.subplot(615)
    plt.ylabel("theta")
    plt.plot(theta)

    # Plotting the alpha sequence
    plt.subplot(616)
    plt.ylabel("alpha")
    ind = np.arange(len(alpha))
    plt.bar(ind, alpha)
    plt.xlabel("iteration")
    if show:
        plt.ion()
        plt.show()

reason: import numpy,from numpy


text:
from epics import *
import numpy as np
import time
import datetime

from ScanFunctions_IEX import setdet

class testgain:
    def __init__(self):
        self._srs = None
        self.value = None
        self.unit = None

class testname:
    def __init__(self):
        self.d3 = None
        self.d4 = None
        self.mcp = None
        self.tey = None
        self.mesh = None

class det:

    def __init__(self):
        self.current = caget('29idKappa:userStringSeq6.STR1')
        self.d3 = None
        self.d4 = None
        self.mcp = None
        self.tey = None
        self.mesh = None

ext: py

reason: import numpy


text:
import numpy as np
import scipy.sparse as sp
import networkx as nx

# Convert sparse matrix to tuple
def sparse_to_tuple(sparse_mx):
    if not sp.isspmatrix_coo(sparse_mx):
        sparse_mx = sparse_mx.tocoo()
    coords = np.vstack((sparse_mx.row, sparse_mx.col)).transpose()
    values = sparse_mx.data
    shape = sparse_mx.shape
    return coords, values, shape

# Get normalized adjacency matrix: A_norm
def preprocess_graph(adj):
    adj = sp.coo_matrix(adj)
    adj_ = adj + sp.eye(adj.shape[0])
    rowsum = np.array(adj_.sum(1))
    degree_mat_inv_sqrt = sp.diags(np.power(rowsum, -0.5).flatten())
    adj_normalized = adj_.dot(degree_mat_inv_sqrt).transpose().dot(degree_mat_inv_sqrt).tocoo()
    return sparse_to_tuple(adj_normalized)

# Prepare feed-dict for Tensorflow session
def construct_feed_dict(adj_normalized, adj, features, placeholders):
    # construct feed dictionary
    feed_dict = dict()
    feed_dict.update({placeholders['features']: features})
    feed_dict.update({placeholders['adj']: adj_normalized})
    feed_dict.update({placeholders['adj_orig']: adj})
    return feed_dict

# Perform train-test split
    # Takes in adjacency matrix in sparse format
    # Returns: adj_train, train_edges, val_edges, val_edges_false,
        # test_edges, test_edges_false
def mask_test_edges(adj, test_frac=.1, val_frac=.05, prevent_disconnect=True, verbose=False):
    # NOTE: Splits are randomized and results might slightly deviate from reported numbers in the paper.

    if verbose == True:
        print 'preprocessing...'

    # Remove diagonal elements
    adj = adj - sp.dia_matrix((adj.diagonal()[np.newaxis, :], [0]), shape=adj.shape)
    adj.eliminate_zeros()
    # Check that diag is zero:
    #assert np.diag(adj.todense()).sum() == 0

    g = nx.from_scipy_sparse_matrix(adj)
    orig_num_cc = nx.number_connected_components(g)

    adj_triu = sp.triu(adj) # upper triangular portion of adj matrix
    adj_tuple = sparse_to_tuple(adj_triu) # (coords, values, shape), edges only 1 way
    edges = adj_tuple[0] # all edges, listed only once (not 2 ways)
    # edges_all = sparse_to_tuple(adj)[0] # ALL edges (includes both ways)
    num_test = int(np.floor(edges.shape[0] * test_frac)) # controls how large the test set should be
    num_val = int(np.floor(edges.shape[0] * val_frac)) # controls how alrge the validation set should be

    # Store edges in list of ordered tuples (node1, node2) where node1 < node2
    edge_tuples = [(min(edge[0], edge[1]), max(edge[0], edge[1])) for edge in edges]
    all_edge_tuples = set(edge_tuples)
    train_edges = set(edge_tuples) # initialize train_edges to have all edges
    test_edges = set()
    val_edges = set()

    if verbose == True:
        print 'generating test/val sets...'

    # Iterate over shuffled edges, add to train/val sets
    np.random.shuffle(edge_tuples)
    for edge in edge_tuples:
        # print edge
        node1 = edge[0]
        node2 = edge[1]

        # If removing edge would disconnect a connected component, backtrack and move on
        g.remove_edge(node1, node2)
        if prevent_disconnect == True:
            if nx.number_connected_components(g) > orig_num_cc:
                g.add_edge(node1, node2)
                continue

        # Fill test_edges first
        if len(test_edges) < num_test:
            test_edges.add(edge)
            train_edges.remove(edge)

        # Then, fill val_edges
        elif len(val_edges) < num_val:
            val_edges.add(edge)
            train_edges.remove(edge)

        # Both edge lists full --> break loop
        elif len(test_edges) == num_test and len(val_edges) == num_val:
            break

    if (len(val_edges) < num_val or len(test_edges) < num_test):
        print "WARNING: not enough removable edges to perform full train-test split!"
        print "Num. (test, val) edges requested: (", num_test, ", ", num_val, ")"
        print "Num. (test, val) edges returned: (", len(test_edges), ", ", len(val_edges), ")"

    if prevent_disconnect == True:
        assert nx.number_connected_components(g) == orig_num_cc

    if verbose == True:
        print 'creating false test edges...'

    test_edges_false = set()
    while len(test_edges_false) < num_test:
        idx_i = np.random.randint(0, adj.shape[0])
        idx_j = np.random.randint(0, adj.shape[0])
        if idx_i == idx_j:
            continue

        false_edge = (min(idx_i, idx_j), max(idx_i, idx_j))

        # Make sure false_edge not an actual edge, and not a repeat
        if false_edge in all_edge_tuples:
            continue
        if false_edge in test_edges_false:
            continue

        test_edges_false.add(false_edge)

    if verbose == True:
        print 'creating false val edges...'

    val_edges_false = set()
    while len(val_edges_false) < num_val:
        idx_i = np.random.randint(0, adj.shape[0])
        idx_j = np.random.randint(0, adj.shape[0])
        if idx_i == idx_j:
            continue

        false_edge = (min(idx_i, idx_j), max(idx_i, idx_j))

        # Make sure false_edge in not an actual edge, not in test_edges_false, not a repeat
        if false_edge in all_edge_tuples or \
            false_edge in test_edges_false or \
            false_edge in val_edges_false:
            continue

        val_edges_false.add(false_edge)

    if verbose == True:
        print 'creating false train edges...'

    train_edges_false = set()
    while len(train_edges_false) < len(train_edges):
        idx_i = np.random.randint(0, adj.shape[0])
        idx_j = np.random.randint(0, adj.shape[0])
        if idx_i == idx_j:
            continue

        false_edge = (min(idx_i, idx_j), max(idx_i, idx_j))

        # Make sure false_edge in not an actual edge, not in test_edges_false,
            # not in val_edges_false, not a repeat
        if false_edge in all_edge_tuples or \
            false_edge in test_edges_false or \
            false_edge in val_edges_false or \
            false_edge in train_edges_false:
            continue

        train_edges_false.add(false_edge)

    if verbose == True:
        print 'final checks for disjointness...'

    # assert: false_edges are actually false (not in all_edge_tuples)
    assert test_edges_false.isdisjoint(all_edge_tuples)
    assert val_edges_false.isdisjoint(all_edge_tuples)
    assert train_edges_false.isdisjoint(all_edge_tuples)

    # assert: test, val, train false edges disjoint
    assert test_edges_false.isdisjoint(val_edges_false)
    assert test_edges_false.isdisjoint(train_edges_false)
    assert val_edges_false.isdisjoint(train_edges_false)

    # assert: test, val, train positive edges disjoint
    assert val_edges.isdisjoint(train_edges)
    assert test_edges.isdisjoint(train_edges)
    assert val_edges.isdisjoint(test_edges)

    if verbose == True:
        print 'creating adj_train...'

    # Re-build adj matrix using remaining graph
    adj_train = nx.adjacency_matrix(g)

    # Convert edge-lists to numpy arrays
    train_edges = np.array([list(edge_tuple) for edge_tuple in train_edges])
    train_edges_false = np.array([list(edge_tuple) for edge_tuple in train_edges_false])
    val_edges = np.array([list(edge_tuple) for edge_tuple in val_edges])
    val_edges_false = np.array([list(edge_tuple) for edge_tuple in val_edges_false])
    test_edges = np.array([list(edge_tuple) for edge_tuple in test_edges])
    test_edges_false = np.array([list(edge_tuple) for edge_tuple in test_edges_false])

    if verbose == True:
        print 'Done with train-test split!'
        print ''

    # NOTE: these edge lists only contain single direction of edge!
    return adj_train, train_edges, train_edges_false, \
        val_edges, val_edges_false, test_edges, test_edges_false

# Perform train-test split
    # Takes in adjacency matrix in sparse format (from a directed graph)
    # Returns: adj_train, train_edges, val_edges, val_edges_false,
        # test_edges, test_edges_false
def mask_test_edges_directed(adj, test_frac=.1, val_frac=.05,
    prevent_disconnect=True, verbose=False, false_edge_sampling='iterative'):
    if verbose == True:
        print 'preprocessing...'

    # Remove diagonal elements
    adj = adj - sp.dia_matrix((adj.diagonal()[np.newaxis, :], [0]), shape=adj.shape)
    adj.eliminate_zeros()
    # Check that diag is zero:
    assert np.diag(adj.todense()).sum() == 0

    # Convert to networkx graph to calc num. weakly connected components
    g = nx.from_scipy_sparse_matrix(adj, create_using=nx.DiGraph())
    orig_num_wcc = nx.number_weakly_connected_components(g)

    adj_tuple = sparse_to_tuple(adj) # (coords, values, shape)
    edges = adj_tuple[0] # List of ALL edges (either direction)
    edge_pairs = [(edge[0], edge[1]) for edge in edges] # store edges as list of tuples (from_node, to_node)

    num_test = int(np.floor(edges.shape[0] * test_frac)) # controls how large the test set should be
    num_val = int(np.floor(edges.shape[0] * val_frac)) # controls how alrge the validation set should be
    num_train = len(edge_pairs) - num_test - num_val # num train edges

    all_edge_set = set(edge_pairs)
    train_edges = set(edge_pairs) # init train_edges to have all edges
    test_edges = set() # init test_edges as empty set
    val_edges = set() # init val edges as empty set

    ### ---------- TRUE EDGES ---------- ###
    # Shuffle and iterate over all edges
    np.random.shuffle(edge_pairs)

    # get initial bridge edges
    bridge_edges = set(nx.bridges(nx.to_undirected(g)))

    if verbose:
        print('creating true edges...')

    for ind, edge in enumerate(edge_pairs):
        node1, node2 = edge[0], edge[1]

        # Recalculate bridges every ____ iterations to relatively recent
        if ind % 10000 == 0:
            bridge_edges = set(nx.bridges(nx.to_undirected(g)))

        # Don't sample bridge edges to increase likelihood of staying connected
        if (node1, node2) in bridge_edges or (node2, node1) in bridge_edges:
            continue

        # If removing edge would disconnect the graph, backtrack and move on
        g.remove_edge(node1, node2)
        if prevent_disconnect == True:
            if not nx.is_weakly_connected(g):
                g.add_edge(node1, node2)
                continue

        # Fill test_edges first
        if len(test_edges) < num_test:
            test_edges.add(edge)
            train_edges.remove(edge)
            if len(test_edges) % 10000 == 0 and verbose == True:
                print 'Current num test edges: ', len(test_edges)

        # Then, fill val_edges
        elif len(val_edges) < num_val:
            val_edges.add(edge)
            train_edges.remove(edge)
            if len(val_edges) % 10000 == 0 and verbose == True:
                print 'Current num val edges: ', len(val_edges)

        # Both edge lists full --> break loop
        elif len(test_edges) == num_test and len(val_edges) == num_val:
            break

    # Check that enough test/val edges were found
    if (len(val_edges) < num_val or len(test_edges) < num_test):
        print "WARNING: not enough removable edges to perform full train-test split!"
        print "Num. (test, val) edges requested: (", num_test, ", ", num_val, ")"
        print "Num. (test, val) edges returned: (", len(test_edges), ", ", len(val_edges), ")"

    # Print stats for largest remaining WCC
    print 'Num WCC: ', nx.number_weakly_connected_components(g)
    largest_wcc_set = max(nx.weakly_connected_components(g), key=len)
    largest_wcc = g.subgraph(largest_wcc_set)
    print 'Largest WCC num nodes: ', largest_wcc.number_of_nodes()
    print 'Largest WCC num edges: ', largest_wcc.number_of_edges()

    if prevent_disconnect == True:
        assert nx.number_weakly_connected_components(g) == orig_num_cc

    # Fraction of edges with both endpoints in largest WCC
    def frac_edges_in_wcc(edge_set):
        num_wcc_contained_edges = 0.0
        num_total_edges = 0.0
        for edge in edge_set:
            num_total_edges += 1
            if edge[0] in largest_wcc_set and edge[1] in largest_wcc_set:
                num_wcc_contained_edges += 1
        frac_in_wcc = num_wcc_contained_edges / num_total_edges
        return frac_in_wcc

    # Check what percentage of edges have both endpoints in largest WCC
    print 'Fraction of train edges with both endpoints in L-WCC: ', frac_edges_in_wcc(train_edges)
    print 'Fraction of test edges with both endpoints in L-WCC: ', frac_edges_in_wcc(test_edges)
    print 'Fraction of val edges with both endpoints in L-WCC: ', frac_edges_in_wcc(val_edges)

    # Ignore edges with endpoint not in largest WCC
    print 'Removing edges with either endpoint not in L-WCC from train-test split...'
    train_edges = {edge for edge in train_edges if edge[0] in largest_wcc_set and edge[1] in largest_wcc_set}
    test_edges = {edge for edge in test_edges if edge[0] in largest_wcc_set and edge[1] in largest_wcc_set}
    val_edges = {edge for edge in val_edges if edge[0] in largest_wcc_set and edge[1] in largest_wcc_set}

    ### ---------- FALSE EDGES ---------- ###

    # Initialize empty sets
    train_edges_false = set()
    test_edges_false = set()
    val_edges_false = set()

    # Generate candidate false edges (from g-complement) and iterate through them
    if false_edge_sampling == 'iterative':
        if verbose == True:
            print 'preparing complement adjacency matrix...'

        # Sample false edges from G-complement, instead of randomly generating edges
        # g_complement = nx.complement(g)
        adj_complement = 1 - adj.toarray() # flip 0's, 1's in adjacency matrix
        np.fill_diagonal(adj_complement, val=0) # set diagonals to 0

        # 2 numpy arrays indicating x, y coords in adj_complement
            # WARNING: This line can use up a lot of RAM depending on 'adj' size
        idx1, idx2 = np.where(adj_complement == 1)

        edges_false = np.stack((idx1, idx2), axis=-1) # stack arrays into coord pairs.
        edge_pairs_false = [(edge[0], edge[1]) for false_edge in edges_false]

        # Shuffle and iterate over false edges
        np.random.shuffle(edge_pairs_false)
        if verbose == True:
            print 'adding candidate false edges to false edge sets...'
        for false_edge in edge_pairs_false:
            # Fill train_edges_false first
            if len(train_edges_false) < len(train_edges):
                train_edges_false.add(false_edge)
                if len(train_edges_false) % 100000 == 0 and verbose == True:
                    print 'Current num false train edges: ', len(train_edges_false)

            # Fill test_edges_false next
            elif len(test_edges_false) < len(test_edges):
                test_edges_false.add(false_edge)
                if len(test_edges_false) % 100000 == 0 and verbose == True:
                    print 'Current num false test edges: ', len(test_edges_false)

            # Fill val_edges_false last
            elif len(val_edges_false) < len(val_edges):
                val_edges_false.add(false_edge)
                if len(val_edges_false) % 100000 == 0 and verbose == True:
                    print 'Current num false val edges: ', len(val_edges_false)

            # All sets filled --> break
            elif len(train_edges_false) == len(train_edges) and \
                len(test_edges_false) == len(test_edges) and \
                len(val_edges_false) == len(val_edges):
                break

    # Randomly generate false edges (idx_i, idx_j) 1 at a time to save memory
    elif false_edge_sampling == 'random':
        if verbose == True:
            print 'creating false test edges...'

        # FALSE TEST EDGES
        while len(test_edges_false) < len(test_edges):
            idx_i = np.random.randint(0, adj.shape[0])
            idx_j = np.random.randint(0, adj.shape[0])
            if idx_i == idx_j: # no self-loops
                continue

            # Ensure both endpoints are in largest WCC
            if idx_i not in largest_wcc_set or idx_j not in largest_wcc_set:
                continue

            false_edge = (idx_i, idx_j)

            # Make sure false_edge not an actual edge, and not a repeat
            if false_edge in all_edge_set:
                continue
            if false_edge in test_edges_false:
                continue

            test_edges_false.add(false_edge)

            if len(test_edges_false) % 100000 == 0 and verbose == True:
                print 'Current num false test edges: ', len(test_edges_false)

        # FALSE VAL EDGES
        if verbose == True:
            print 'creating false val edges...'

        while len(val_edges_false) < len(val_edges):
            idx_i = np.random.randint(0, adj.shape[0])
            idx_j = np.random.randint(0, adj.shape[0])
            if idx_i == idx_j:
                continue

            false_edge = (idx_i, idx_j)

            # Make sure false_edge in not an actual edge, not in test_edges_false, not a repeat
            if false_edge in all_edge_set or \
                false_edge in test_edges_false or \
                false_edge in val_edges_false:
                continue

            val_edges_false.add(false_edge)

            if len(val_edges_false) % 100000 == 0 and verbose == True:
                print 'Current num false val edges: ', len(val_edges_false)

        # FALSE TRAIN EDGES
        if verbose == True:
            print 'creating false train edges...'

        while len(train_edges_false) < len(train_edges):
            idx_i = np.random.randint(0, adj.shape[0])
            idx_j = np.random.randint(0, adj.shape[0])
            if idx_i == idx_j:
                continue

            false_edge = (idx_i, idx_j)

            # Make sure false_edge in not an actual edge, not in test_edges_false,
                # not in val_edges_false, not a repeat
            if false_edge in all_edge_set or \
                false_edge in test_edges_false or \
                false_edge in val_edges_false or \
                false_edge in train_edges_false:
                continue

            train_edges_false.add(false_edge)

            if len(train_edges_false) % 100000 == 0 and verbose == True:
                print 'Current num false train edges: ', len(train_edges_false)

    ### ---------- FINAL DISJOINTNESS CHECKS ---------- ###
    if verbose == True:
        print 'final checks for disjointness...'

    # assert: false_edges are actually false (not in all_edge_tuples)
    assert test_edges_false.isdisjoint(all_edge_set)
    assert val_edges_false.isdisjoint(all_edge_set)
    assert train_edges_false.isdisjoint(all_edge_set)

    # assert: test, val, train false edges disjoint
    assert test_edges_false.isdisjoint(val_edges_false)
    assert test_edges_false.isdisjoint(train_edges_false)
    assert val_edges_false.isdisjoint(train_edges_false)

    # assert: test, val, train positive edges disjoint
    assert val_edges.isdisjoint(train_edges)
    assert test_edges.isdisjoint(train_edges)
    assert val_edges.isdisjoint(test_edges)

    if verbose == True:
        print 'creating adj_train...'

    # Re-build adj matrix using remaining graph
    adj_train = nx.adjacency_matrix(g)

    # Convert edge-lists to numpy arrays
    train_edges = np.array([list(edge_tuple) for edge_tuple in train_edges])
    train_edges_false = np.array([list(edge_tuple) for edge_tuple in train_edges_false])
    val_edges = np.array([list(edge_tuple) for edge_tuple in val_edges])
    val_edges_false = np.array([list(edge_tuple) for edge_tuple in val_edges_false])
    test_edges = np.array([list(edge_tuple) for edge_tuple in test_edges])
    test_edges_false = np.array([list(edge_tuple) for edge_tuple in test_edges_false])

    if verbose == True:
        print 'Done with train-test split!'
        print 'Num train edges (true, false): (', train_edges.shape[0], ', ', train_edges_false.shape[0], ')'
        print 'Num test edges (true, false): (', test_edges.shape[0], ', ', test_edges_false.shape[0], ')'
        print 'Num val edges (true, false): (', val_edges.shape[0], ', ', val_edges_false.shape[0], ')'
        print ''

    # Return final edge lists (edges can go either direction!)
    return adj_train, train_edges, train_edges_false, \
        val_edges, val_edges_false, test_edges, test_edges_false

reason: import numpy,import scipy,import networkx


text:
#  File:
#    rectilinear_contour_xarray.py
#
#  Synopsis:
#    Demonstrate the use of xarray to read a netCDF file.
#    Create a contour plot.
#
#  Category:
#    xarray
#    netCDF
#    contour
#
#  Author:
#    Karin Meier-Fleischer
#
#  Date of initial publication:
#    January, 2019
#
#  Description:
#    Demonstrate the use of xarray to read a netCDF file.
#    Create a contour plot.
#
#  Effects illustrated:
#    o  Read netCDF file with xarray
#    o  Create contour plot
#
#  Output:
#     A single visualization is produced.
#
'''
  DKRZ Python Example: 	rectilinear_contour_xarray.py

  -  Read netCDF file
  -  Create contour plot
'''
from __future__ import print_function
import os
import numpy as np
import xarray as xr
import Ngl

#-----------------------------------------------------------------------
#-- Function:	add_titles(wks, plot, title, left, center, right, xtitle, ytitle)
#-----------------------------------------------------------------------
def ngl_Strings(wks, plot, title='', left='', center='', right='', xtitle='', ytitle=''):

   vpx = Ngl.get_float(plot,'vpXF')             #-- retrieve value of res.vpXF from plot
   vpy = Ngl.get_float(plot,'vpYF')             #-- retrieve value of res.vpYF from plot
   vpw = Ngl.get_float(plot,'vpWidthF')         #-- retrieve value of res.vpWidthF from plot
   vph = Ngl.get_float(plot,'vpHeightF')        #-- retrieve value of res.vpHeightF from plot

   ymax = vpy+0.08                              #-- we need space for the title and strings
   if(ymax > 0.98):
     print("--> if you can't see the title use res.nglMaximize = False and/or set res.vpYF")

#-- add title
   if(title != ''):
      tires = Ngl.Resources()
      tires.txFontHeightF =  0.016
      tires.txJust        = 'CenterCenter'
      tires.txFont        =  22                     #-- Font 22: Helvetica bold
      if(left != '' or center != '' or right != ''):
         y = vpy + 0.075
      else:
         y = vpy + 0.05
      Ngl.text_ndc(wks, title, 0.5, y, tires)

#-- add left, center and/or right string
   txres = Ngl.Resources()
   txres.txFontHeightF = 0.020                  #-- font size for left, center and right string

   y = vpy + 0.035                              #-- y-position

   if(left != ''):
      txres.txJust = 'CenterLeft'               #-- text justification
      x = vpx                                   #-- x-position
      Ngl.text_ndc(wks, left, x, y, txres)      #-- add text to wks

   if(center != ''):
      txres.txJust = 'CenterCenter'             #-- text justification
      Ngl.text_ndc(wks, center, 0.5, y, txres)  #-- add text to wks

   if(right != ''):
      txres.txJust = 'CenterRight'              #-- text justification
      x = vpx+vpw                               #-- x-position
      Ngl.text_ndc(wks, right, x, y, txres)     #-- add text to wks

#-- add y-axis title string
   txtires = Ngl.Resources()
   txtires.txFontHeightF = 0.024                #-- font size for x-axis title string
   txtires.txAngleF      = 90.0
   txtires.txJust        = 'CenterCenter'       #-- text justification

   y = vpy - vph/2                              #-- y-position
   x = vpx - 0.12

   Ngl.text_ndc(wks, ytitle, x, y, txtires)     #-- add text to wks

#-----------------------------------------------------------------------
#-- Function:   main
#-----------------------------------------------------------------------
def main():

    #-- open file and read variable and time
    home  = os.environ.get('HOME')
    fname = os.path.join(home,'/NCL/PyNGL/User_Guide_examples/rectilinear_grid_2D.nc')

    #-- open file and read variables
    f    = xr.open_dataset(home+fname)
    temp = f['tsurf'][0,::-1,:]                    #-- first time step, reverse latitude
    lat  = f['lat'][::-1]                          #-- reverse latitudes
    lon  = f['lon'][:]                             #-- all longitudes

    if temp.attrs['long_name']:
       lname = temp.attrs['long_name']             #-- retrieve variable long_name

    if temp.attrs['units']:
       units = temp.attrs['units']                 #-- retrieve variable units

    title = 'PyNGL: contour plot'                  #-- set title string

    #-- open a workstation
    wkres            =  Ngl.Resources()            #-- generate an res object for workstation
    wkres.wkColorMap = 'rainbow'                   #-- choose colormap
    wks = Ngl.open_wks('png','plot_rectilinear_cont_xarray',wkres)  #-- open workstation

    #-- set resources
    res                       =  Ngl.Resources()   #-- generate a resource object

    res.nglMaximize           =  False
    res.nglPointTickmarksOutward = True            #-- point tickmarks outward
    res.nglDraw               =  False
    res.nglFrame              =  False

    res.vpXF                  =  0.1               #-- viewport x-position
    res.vpYF                  =  0.8               #-- viewport y-position
    res.vpWidthF              =  0.7               #-- viewport width
    res.vpHeightF             =  0.7               #-- viewport height

    res.cnFillOn              =  True              #-- turn on contour fill
    res.cnLineLabelsOn        =  False             #-- turn off line labels
    res.cnInfoLabelOn         =  False             #-- turn off info label
    res.cnLevelSelectionMode  = 'ManualLevels'     #-- define your own contour levels
    res.cnMinLevelValF        =  250.              #-- minimum contour value
    res.cnMaxLevelValF        =  315.              #-- maximum contour value
    res.cnLevelSpacingF       =  5.                #-- contour increment

    res.lbRightMarginF        =  0.8               #-- move labelbar to the left

    res.sfXArray              =  lon.values        #-- lon as np.ndarray
    res.sfYArray              =  lat.values        #-- lat as np.ndarray

    #-- create the plot
    plot = Ngl.contour_map(wks,temp,res)           #-- draw contours over a map

    #-- add additional strings to plot (like NCL's gsnLeftString and gsnRightString)
    ngl_Strings(wks, plot, title=title, left=lname, right=units, ytitle=lname)

    #-- done
    Ngl.draw(plot)
    Ngl.frame(wks)
    Ngl.end()

#-------------------------------------------------------------
#-- run main
#-------------------------------------------------------------

if __name__ == '__main__':
   main()


text:
import numpy
from handwriting_features.features import HandwritingFeatures
from handwriting_features.features.configuration.mapping import HandwritingFeaturesMapping
from handwriting_features.features.validation import HandwritingFeaturesValidation
from handwriting_features.interface.featurizer.utils import (
    SingleSubjectFeatureUtils,
    MultiSubjectFeatureUtils,
    FeaturesPipelineUtils
)

class BaseFeatureExtractorHandler(object):
    """Base class for the feature extractor handlers"""

    # Features pipeline utils
    pipeline_utils = FeaturesPipelineUtils

    # Features
    features = HandwritingFeatures

    # Feature mapper
    mapping = HandwritingFeaturesMapping

    # Feature validator
    validation = HandwritingFeaturesValidation

class SingleSubjectFeatureExtractorHandler(BaseFeatureExtractorHandler):
    """Class implementing the single-subject features extractor handler"""

    # Feature utils
    utils = SingleSubjectFeatureUtils

    @classmethod
    def extract(cls, data_values, data_labels=None, pipeline=None, preparation=True, **configuration):
        """
        Extracts the features specified in the pipeline for a single subject.

        :param data_values: sample values to extract the features from
        :type data_values: numpy.ndarray
        :param data_labels: labels for data samples, defaults to None
        :type data_labels: list, optional
        :param pipeline: pipeline of the features, defaults to None
        :type pipeline: list, optional
        :param preparation: prepare the pipeline of features, defaults to True
        :type preparation: bool, optional
        :param configuration: common extractor configuration
        :type configuration: **kwargs
        :return: extracted features and labels
        :rtype: dict {"features": ..., "labels": ...}
        """

        # Initialize the handwriting features interface
        features = cls.features.from_numpy_array(data_values, data_labels, **configuration)

        # Initialize the feature mapping
        mapping = cls.mapping(features)

        # Prepare the buffers for extracted feature values/labels
        feature_values = []
        feature_labels = []

        # Prepare the features pipeline
        if preparation:
            pipeline = cls.pipeline_utils.prepare_features_pipeline(pipeline)

        # Extract the features specified in the features pipeline
        for feature in pipeline:

            # Get the validated feature arguments
            arguments = cls.validation.validate(feature.get("name"), feature.get("args"))

            # Extract the feature
            extracted = mapping.map(feature.get("name"))(**arguments)

            # Update the feature values/labels
            feature_values.append(cls.utils.prepare_feature_values(extracted))
            feature_labels.append(cls.utils.prepare_feature_labels(
                extracted,
                feature.get("name"),
                feature.get("args")))

        # Return the extracted feature values/labels
        return {
            "features": feature_values,
            "labels": feature_labels
        }

class MultiSubjectFeatureExtractorHandler(BaseFeatureExtractorHandler):
    """Class implementing the multi-subject features extractor handler"""

    # Feature extractor
    extractor = SingleSubjectFeatureExtractorHandler

    # Feature utils
    utils = MultiSubjectFeatureUtils

    @classmethod
    def extract(cls, data_values, data_labels=None, pipeline=None, **configuration):
        """
        Extracts the features specified in the pipeline for multiple subjects.

        :param data_values: samples values to extract the features from
        :type data_values: numpy.ndarray
        :param data_labels: labels for data samples, defaults to None
        :type data_labels: list, optional
        :param pipeline: pipeline of the features, defaults to None
        :type pipeline: list, optional
        :param configuration: common extractor configuration
        :type configuration: **kwargs
        :return: extracted features and labels
        :rtype: dict {"features": ..., "labels": ...}
        """

        # Prepare the features pipeline
        pipeline = cls.pipeline_utils.prepare_features_pipeline(pipeline)

        # Extract the features specified in the features pipeline for each subject
        extracted = [
            cls.extractor.extract(
                data_values=data_values[sample, ...],
                data_labels=data_labels,
                pipeline=pipeline,
                preparation=False,
                **configuration)
            for sample in range(data_values.shape[0])
        ]

        # Prepare the feature values/labels
        feature_values = cls.utils.prepare_feature_values(extracted, pipeline)
        feature_labels = cls.utils.prepare_feature_labels(extracted, pipeline)

        # Return the extracted feature values/labels
        return {
            "features": feature_values,
            "labels": feature_labels
        }

max_stars_repo_name: BDALab/handwriting-features

max_issues_repo_name: BDALab/handwriting-features

max_forks_repo_name: BDALab/handwriting-features


text:
"""
Copyright MIT and Harvey Mudd College
MIT License
Summer 2020

Defines the interface of the Camera module of the racecar_core library.
"""

import abc
import numpy as np
from nptyping import NDArray

class Camera(abc.ABC):
    """
    Returns the color images and depth images captured by the camera.
    """

    # The dimensions of the image in pixels
    _WIDTH: int = 640
    _HEIGHT: int = 480

    # Maximum range of the depth camera (in cm)
    _MAX_RANGE = 1200

    @abc.abstractmethod
    def get_color_image(self) -> NDArray[(480, 640, 3), np.uint8]:
        """
        Returns the current color image captured by the camera.

        Returns:
            An array representing the pixels in the image, organized as follows
                0th dimension: pixel rows, indexed from top to bottom.
                1st dimension: pixel columns, indexed from left to right.
                2nd dimension: pixel color channels, in the blue-green-red format.

        Note:
            Each color value ranges from 0 to 255.

        Example::

            # Initialize image with the most recent color image captured by the camera
            image = rc.camera.get_color_image()

            # Store the amount of blue in the pixel on row 3, column 5
            blue = image[3][5][0]
        """
        pass

    @abc.abstractmethod
    def get_color_image_async(self) -> NDArray[(480, 640, 3), np.uint8]:
        """
        Returns the current color image without the car in "go" mode.

        Returns:
            An array representing the pixels in the image, organized as follows
                0th dimension: pixel rows, indexed from top to bottom.
                1st dimension: pixel columns, indexed from left to right.
                2nd dimension: pixel color channels, in the blue-green-red format.

        Note:
            Each color value ranges from 0 to 255.

        Warning:
            This function breaks the start-update paradigm and should only be used in
            Jupyter Notebook.

        Example::

            # Initialize image with the most recent color image captured by the camera
            image = rc.camera.get_color_image_async()

            # Store the amount of blue in the pixel on row 3, column 5
            blue = image[3][5][0]
        """
        pass

    @abc.abstractmethod
    def get_depth_image(self) -> NDArray[(480, 640), np.float32]:
        """
        Returns the current depth image captured by the camera.

        Returns:
            A two dimensional array indexed from top left to the bottom right storing
            the distance of each pixel from the car in cm.

        Example::

            # Initialize image with the most recent depth image captured by the camera
            image = rc.camera.get_depth_image()

            # Store the distance of the object at pixel row 3, column 5
            distance = image[3][5]
        """
        pass

    @abc.abstractmethod
    def get_depth_image_async(self) -> NDArray[(480, 640), np.float32]:
        """
        Returns the current depth image without the car in "go" mode.

        Returns:
            A two dimensional array indexed from top left to the bottom right storing
            the distance of each pixel from the car in cm.

        Warning:
            This function breaks the start-update paradigm and should only be used in
            Jupyter Notebook.

        Example::

            # Initialize image with the most recent depth image captured by the camera
            image = rc.camera.get_depth_image_async()

            # Store the distance of the object at pixel row 3, column 5
            distance = image[3][5]
        """
        pass

    def get_width(self) -> int:
        """
        Returns the pixel width of the color and depth images.

        Returns:
            The width (number of pixel columns) in the color and depth images.

        Example::

            image = rc.camera.get_color_image()

            # Access the top right pixel of the image
            top_right_pixel = image[0][rc.camera.get_width() - 1]
        """
        return self._WIDTH

    def get_height(self) -> int:
        """
        Returns the pixel height of the color and depth images.

        Returns:
            The height (number of pixel rows) in the color and depth images.

        Example::

            image = rc.camera.get_color_image()

            # Access the top bottom left pixel of the image
            bottom_left_pixel = image[rc.camera.get_height() - 1][0]
        """
        return self._HEIGHT

    def get_max_range(self) -> float:
        """
        Returns the maximum distance in cm which can be detected by the depth camera.

        Returns:
            The maximum range of the depth camera.

        Example::

            depth_image = rc.camera.get_depth_image()
            center_distance = rc_utils.get_depth_image_center_distance(depth_image)

            # If center_distance is 0.0 (no data), set it to max_range
            if center_distance == 0.0:
                center_distance = rc.camera.get_max_range()
        """
        return self._MAX_RANGE


text:
import numpy as np

__all__ = ['BoundingBox']

class BoundingBox(np.ndarray):

    """Bounding box assuming RAS coordinate system"""
    def __new__(cls, input_array, info=None):
        try:
            if len(input_array) == 6 and np.isscalar(input_array[0]):
                pass
            elif len(input_array) >= 1 and len(input_array[0]) == 3:
                pass
            else:
                raise ValueError(
                    "Bounding box must have 6 components or be a list of points")
        except TypeError:
            raise ValueError(
                "Bounding box must have 6 components or be a list of points")
        except ValueError:
            raise ValueError(
                "Bounding box must have 6 components or be a list of points")

        input_array = np.asanyarray(input_array)

        if len(input_array) != 6 or input_array.ndim > 1:
            input_array = np.r_[
                np.min(input_array, axis=0),
                np.max(input_array, axis=0),
            ]

        array = input_array.view(cls)
        array.setflags(write=False)
        return input_array.view(cls)

    def __str__(self):
        return ('%s:%s' % (
            super(np.ndarray, self[:3]).__str__(),
            super(np.ndarray, self[3:]).__str__()
        )).replace('BoundingBox', '')

    @property
    def volume(self):
        return np.prod(self.side_lengths)

    @property
    def left(self):
        return self[0]

    @property
    def posterior(self):
        return self[1]

    @property
    def inferior(self):
        return self[2]

    @property
    def right(self):
        return self[3]

    @property
    def anterior(self):
        return self[4]

    @property
    def superior(self):
        return self[5]

    @property
    def side_lengths(self):
        return self[3:] - self[:3]

    def union(self, bounding_box):
        if isinstance(bounding_box, BoundingBox):
            _bounding_box = bounding_box
        else:
            _bounding_box = BoundingBox(bounding_box)
        return BoundingBox(np.r_[
            np.minimum(self[:3], _bounding_box[:3]),
            np.maximum(self[3:], _bounding_box[3:]),
        ])

    def contains(self, points):
        points = np.atleast_2d(points)
        lower = np.asarray(self[:3])[None, :]
        upper = np.asarray(self[3:])[None, :]

        self_min_ge_point = (points <= upper).all(1)
        self_max_le_point = (lower <= points).all(1)

        return self_min_ge_point * self_max_le_point

    def intersection(self, bounding_box):
        bounding_boxes = np.atleast_2d(np.asarray(bounding_box))
        lower = np.asarray(self[:3])[None, :]
        upper = np.asarray(self[3:])[None, :]

        self_min_ge_other_min = (bounding_boxes[:, :3] <= lower).any(1)
        self_min_le_other_max = (lower <= bounding_boxes[:, 3:]).any(1)

        self_max_ge_other_min = (bounding_boxes[:, :3] <= upper).any(1)
        self_max_le_other_max = (lower <= bounding_boxes[:, 3:]).any(1)

        other_min_ge_self_min = (lower <= bounding_boxes[:, :3]).any(1)
        other_min_le_self_max = (bounding_boxes[:, :3] <= upper).any(1)

        other_max_ge_self_min = (lower <= bounding_boxes[:, 3:]).any(1)
        other_max_le_self_max = (bounding_boxes[:, 3:] <= upper).any(1)

        one_point_self_inside_other = (
            self_min_ge_other_min * self_min_le_other_max +
            self_max_ge_other_min * self_max_le_other_max
        )

        one_point_other_inside_self = (
            other_min_ge_self_min * other_min_le_self_max +
            other_max_ge_self_min * other_max_le_self_max
        )

        intersection_exists = (
            one_point_self_inside_other +
            one_point_other_inside_self
        )

        intersection = np.c_[
            np.maximum(bounding_boxes[:, :3], lower),
            np.minimum(bounding_boxes[:, 3:], upper),
        ]

        if bounding_box.ndim == 1:
            if any(intersection_exists):
                return BoundingBox(intersection[0])
            else:
                return None
        else:
            return intersection_exists, intersection

    def old_intersection(self, bounding_box):
        one_point_self_inside_bounding_box = np.all(
            (self[:3] >= bounding_box[:, :3]) *
            (self[3:] <= bounding_box[:, 3:]),
            1
        )
        one_point_bounding_box_inside_self = np.all(
            (self[:3] <= bounding_box[:, :3]) *
            (self[3:] >= bounding_box[:, 3:]),
            1
        )
        if one_point_self_inside_bounding_box or one_point_bounding_box_inside_self:
            return BoundingBox(np.r_[
                np.maximum(self[:3], bounding_box[:, :3]),
                np.minimum(self[3:], bounding_box[:, 3:]),
            ])
        else:
            return None

    def collides_width(self, bounding_box):
        return self.intersection(bounding_box) is not None

    def split(self, axis):
        box_minor = self.copy()
        box_major = self.copy()
        half_length = (self[axis + 3] - self[axis]) / 2.

        box_minor.setflags(write=True)
        box_major.setflags(write=True)

        box_minor[axis + 3] -= half_length
        box_major[axis] += half_length

        box_minor.setflags(write=False)
        box_major.setflags(write=False)
        return box_minor, box_major

    def __contains__(self, point):
        point = np.atleast_2d(point)
        return (self[:3] <= point).prod(1) * (point <= self[:3]).prod(1)

class AABBTree:
    tree = None
    leaves = None
    forwardMap = None
    reverseMap = None

    def __init__(self, allboxes, indices=None):
        self.tree, self.leaves = self.buildTree(allboxes, indices=indices)
        self.forwardMap = np.arange(len(allboxes))
        self.reverseMap = np.arange(len(allboxes))

    def intersect(self, box):
        return self._intersect(self.tree, box)

    def _intersect(self, tree, box):
        treeBox = tree.box
        if all(
              (box[::2] <= treeBox[1::2]) *
              (box[1::2] >= treeBox[::2])
        ):
            if isinstance(tree, self.leaf):
                return [tree]
            else:
                return self._intersect(tree.left, box) + self._intersect(tree.right, box)
        else:
            return []

    def build_tree(self, allboxes, indices=None, leafPointers=None, parent=None):
        if indices == None:
            indices = np.arange(len(allboxes), dtype=np.int)
            boxes = allboxes
        else:
            if len(indices) == 1:
                return self.leaf(allboxes[indices[0], :], indices[0], parent=parent)
            boxes = allboxes[indices, :]

    def buildTree_old(self, allboxes, indices=None, leafPointers=None, parent=None, verbose=False):
        """
        Each element of the box list is a 6-ple b where
          b = [x-,x+,y-,y+,z-,z+]
        """

        if indices == None:
            indices = np.arange(len(allboxes), dtype=np.int)
            boxes = allboxes
        else:
            if len(indices) == 1:
                return self.leaf(allboxes[indices[0], :], indices[0], parent=parent)
            boxes = allboxes[indices, :]

        if verbose:
            print('*******************************************')

        dimensions = len(boxes[0]) / 2

        box = np.empty(2 * dimensions)
        np.min(boxes[:, ::2], axis=0, out=box[::2])
        np.max(boxes[:, 1::2], axis=0, out=box[1::2])
        boxes[:, ::2].min(0, out=box[::2])
        boxes[:, 1::2].max(0, out=box[1::2])

        lengths = box[1::2] - box[::2]
        largestDimension = lengths.argmax()
        largestDimensionLength = lengths[largestDimension]
        cuttingPlaneAt = largestDimensionLength / 2.

        halfLength = (
            boxes[:, 2 * largestDimension] +
            (
                boxes[:, 2 * largestDimension + 1] - boxes[:, 2 * largestDimension]
            ) / 2.
        )

        halfLengthSortedIndices = halfLength.argsort()
        halfLengthSorted = halfLength[halfLengthSortedIndices]
        division = halfLengthSorted.searchsorted(cuttingPlaneAt)

        leftIndices = indices[halfLengthSortedIndices[:division]]
        rightIndices = indices[halfLengthSortedIndices[division:]]

        if len(leftIndices) == 0 or len(rightIndices) == 0:
            n = len(indices) / 2
            leftIndices = indices[:n]
            rightIndices = indices[n:]

        if verbose:
            print(("Left: ", leftIndices))
            print(("Right: ", rightIndices))
        n = self.node(box, indices.copy(), parent=parent)
        n.left = self.buildTree(
            allboxes, leftIndices, parent=n, leafPointers=leafPointers)
        n.right = self.buildTree(
            allboxes, rightIndices, parent=n, leafPointers=leafPointers)
        if parent != None:
            return n
        else:
            return n, leafPointers

    class node:
        box = None
        indices = None
        left = None
        right = None
        parent = None

        def __init__(self, box, indices, left=None, right=None, parent=None):
            self.box = box
            self.indices = indices
            self.left = left
            self.right = right
            self.parent = parent

        def __str__(self):
            return """
      box = %s
      indices = %s
      """ % (
                self.box,
                self.indices,
            )

        def __repr__(self):
            return self.__str__()

    class leaf:
        box = None
        indices = None
        parent = None

        def __init__(self, box, indices, parent=None):
            self.box = box
            self.indices = indices
            self.parent = parent

        def __str__(self):
            return """
      box = %s
      indices = %s
      """ % (
                self.box,
                self.indices,
            )

        def __repr__(self):
            return self.__str__()


text:
import cocos.device
import cocos.numerics as cn
import numpy as np
import pytest

test_data = [np.array([[1, 2, 3],
                       [4, 5, 6],
                       [7, 8, 20]],
                      dtype=np.int32),
             np.array([[0.2, 1.0, 0.5],
                       [0.4, 0.5, 0.6],
                       [0.7, 0.2, 0.25]],
                      dtype=np.float32),
             np.array([[0.5, 2.3, 3.1],
                       [4, 5.5, 6],
                       [7 - 9j, 8 + 1j, 2 + 10j]],
                      dtype=np.complex64)]

@pytest.mark.parametrize("A_numpy", test_data)
def test_tril_triu(A_numpy):
    cocos.device.init()
    A_cocos = cn.array(A_numpy)

    # conduct tests
    assert np.allclose(np.tril(A_numpy), cn.tril(A_cocos))
    assert np.allclose(np.triu(A_numpy), cn.triu(A_cocos))

max_stars_repo_name: michaelnowotny/cocos

max_issues_repo_name: michaelnowotny/cocos

max_forks_repo_name: michaelnowotny/cocos


text:
import numpy as np
import random
import geohash2 as Geohash
from TaxiDirectory import TaxiDirectory
from Taxi_Location import Taxi_Location
import post_request

if __name__== "__main__":

    taxis = []
    for i in range(1000):
        taxis.append(Taxi_Location())

    directory = TaxiDirectory()
    for i in range(60):
        for t in taxis:
            t.randomise()
            directory.put(t.id,t.latitude,t.longitude)
    a = directory.aggregate()
    print(a)
    print(post_request.post_to_app(a))


text:
# -*- coding: utf-8 -*-
"""01_preprocess_data.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/github/zhihanyang2022/super_mario_as_a_string/blob/master/01_preprocess_data.ipynb
"""

import os
import json
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm_notebook

dir_1 = "data_raw/super_mario_bros"
dir_2 = "data_raw/super_mario_bros_2_japan"
annot_txts = [
    os.path.join(dir_1, fn) for fn in os.listdir(dir_1) if fn.split('.')[-1] == 'txt'
] + [
    os.path.join(dir_2, fn) for fn in os.listdir(dir_2) if fn.split('.')[-1] == 'txt'
]
annot_txts

def load_txt(txt):
    with open(txt, 'r') as txt_f:
        return txt_f.readlines()

with open('data_preprocessed/mario.txt', 'w+') as txt_f:

    for i, fp in enumerate(annot_txts):

        infile = load_txt(fp)

        lines = []
        for line in infile:
            lines.append(list(line.rstrip()))

        infile_transposed = np.array(lines).T

        for line in infile_transposed:  # each line represents a column
            num_chars_to_add = 16 - len(lines)
            txt_f.write("".join(['-'] * num_chars_to_add + list(line)))
            txt_f.write("\n")

        if i+1 == len(annot_txts):  # seperate each level with the ")" character
            txt_f.write(")")
        else:
            txt_f.write(")\n")

max_stars_repo_name: inzva/procedural-pattern-generation

max_issues_repo_name: inzva/procedural-pattern-generation

max_forks_repo_name: inzva/procedural-pattern-generation


text:
#!/home/wiseman/anaconda3/bin/python
import numpy as np
import pandas as pd
import subprocess
import glob
import matplotlib.pyplot as plt
import seaborn as sns
from astropy.coordinates import SkyCoord
import logging
from astropy.table import Table
import astropy.io.fits as fits
import os
from astropy import units as u
from astropy.cosmology import FlatLambdaCDM
from astropy import wcs
from des_stacks import des_stack, stack_all
from des_stacks.utils import stack_tools,source_tools
from des_stacks.analysis import astro
%matplotlib notebook
sns.set_color_codes(palette='colorblind')
import time
import _pickle as cpickle
import itertools
import sys
import os
import numpy as np
import math as m

#####################   DEFINE GLOBAL VARIABLES   #################################################

rad  = np.pi/180                   # convert deg to rad
pix_arcsec = 0.264                 # pixel scale (arcsec per pixel)
pix2_arcsec2 = 0.264**2            # pix^2 to arcsec^2 conversion factor
pix2_deg2 = pix2_arcsec2/(3600**2) # pix^2 to deg^2 conversion factor

def get_DLR_ABT(RA_SN, DEC_SN, RA, DEC, A_IMAGE, B_IMAGE, THETA_IMAGE, angsep):
    # inputs are arrays

    global numFailed
    rPHI = np.empty_like(angsep)
    d_DLR = np.empty_like(angsep)

    # convert from IMAGE units (pixels) to WORLD (arcsec^2)
    A_ARCSEC = A_IMAGE*pix_arcsec
    B_ARCSEC = B_IMAGE*pix_arcsec

    # angle between RA-axis and SN-host vector
    GAMMA = np.arctan((DEC_SN - DEC)/(np.cos(DEC_SN*rad)*(RA_SN - RA)))

    # angle between semi-major axis of host and SN-host vector
    PHI = np.radians(THETA_IMAGE) + GAMMA # angle between semi-major axis of host and SN-host vector

    rPHI = A_ARCSEC*B_ARCSEC/np.sqrt((A_ARCSEC*np.sin(PHI))**2 +
                                     (B_ARCSEC*np.cos(PHI))**2)

    # directional light radius
    #  where 2nd moments are bad, set d_DLR = 99.99
    d_DLR = angsep/rPHI

    return [d_DLR, A_ARCSEC, B_ARCSEC, rPHI]

sngals = pd.read_csv('/media/data3/wiseman/des/coadding/catalogs/sngals2.csv',index_col=0)
sngals['DLR_reprod']=''
sngals['DLR_diff']=''
sngals['DLR_RANK_new']=''
n=0
galindex = []
for i in sngals.index:
    n+=1
    print ('Doing %s of %s'%(n,len(sngals)))
    snra,sndec,sn_name = specIas[['RA','DEC','TRANSIENT_NAME']].loc[i]
    sn_name = sn_name.strip(' ')
    match = sngals[sngals['TRANSIENT_NAME']==sn_name]
    sncoord = SkyCoord(ra=snra*u.degree,dec=sndec*u.degree)
    galcoords = SkyCoord(ra=match['RA'].values*u.deg,dec=match['DEC'].values*u.deg)
    dist = sncoord.separation(galcoords)
    dists = np.array([float(dist[i].to_string(unit=u.arcsec,decimal=True)) for i in range(len(dist))])
    new_dlr = get_DLR_ABT(snra,sndec,match['RA'].values,match['DEC'].values,
            match['A_IMAGE'].values,match['B_IMAGE'].values,
           match['THETA_IMAGE'].values,dists)[0]
    if len(new_dlr)>3:
        print (sn_name, len)

    for ind in match.index:
        galindex.append(ind)
    sngals['DLR_reprod'].loc[match.index]=new_dlr
    new_rank = sngals['DLR_reprod'].loc[match.index].rank()
    for r in range(len(new_rank)):
        if new_dlr[r]>4:
            new_rank.iloc[r]=new_rank.iloc[r]*-1
    sngals['DLR_RANK_new'].loc[match.index]=new_rank
    sngals['DLR_diff'].loc[match.index]=sngals['DLR'].loc[match.index]-new_dlr
sngals.to_csv('/media/data3/wiseman/coadding/catalogs/sngals_updated.csv')

reason: import numpy,import astropy,from astropy


text:
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 12 21:38:01 2021

@author: T. Katemann
@description: AoC 2021 12 with Pydroid 3
"""

import numpy as np

def fp(r0,pth0,cnt0):
	cnt0+=1
	if bVerb:
		print("lvl:",cnt0,pth0,r0)

	if np.mod(len(pth),1000) == 0:
		print("ActLen",len(pth))

	for i in r0:
		if i == "end":
			# end reached
			pth1 = pth0[:]
			pth1.append(i)
			if bVerb:
				print("End:",pth0, pth1)

			if pth0 in pth:
				pth.remove(pth0)

			# append global
			pth.append(pth1)

		if i in p.keys():
			# remove ncomplete
			if pth0 in pth:
				pth.remove(pth0)

			pth1=[]
			r1 = p[i]
			if bVerb:
				print(i,r1)

			pth1 = pth0[:]

			if (i in pth1) and i.islower():
				cntlow = 0
				for it1 in pth1:
					if it1 != "start":
						if it1.islower():
							cntlow=max([cntlow,
								pth1.count(it1)])
				if cntlow <= 1:
					if pth1.count(i) >= numfrst:
						if bVerb:
							print("skip",i,pth1.count(i))
						continue
				else:
					if pth1.count(i) >= 1:
						if bVerb:
							print("skip",i,pth1.count(i))
						continue

			pth1.append(i)
			if bVerb:
				print(pth0, pth1)

			# append global
			pth.append(pth1)

			if cnt0 <= 5000:
				fp(r1,pth1,cnt0)

f = open("aoc_2112_data.dat", "r")
data=(f.read().splitlines())
print(data)
db = {}
for it in data:
	a1,a2 = it.split("-")
	if a1 != "end" and a2 != "start":
		if a1 not in db.keys():
			db[a1] = [a2]
		else:
			da = db[a1]
			da.append(a2)

	if a2 != "end" and a1 != "start":
		if a2 not in db.keys():
			db[a2] = [a1]
		else:
			da = db[a2]
			da.append(a1)

print(db)

p= db
bVerb=0
numfrst = 2

if 1:
	# main
	cnt1=0
	ks="start"
	pth =[[ks]]

	if ks in p.keys():
		r=p[ks]
		print(ks,r)
		fp(r,pth[0],cnt1)
	else:
		print("wrongkey")

	print()

	for pth2 in pth:
		print(pth2)
	print("num path:",len(pth))


text:
### Progressive hedging requires a model with specified first and second stage decisions.
### The premise of this constructor is to construct a model where first and second stage decisions are specified

import sys
import os
from os.path import abspath, dirname
sys.path.insert(0, dirname(dirname(dirname(dirname(abspath(__file__))))))
from pyomo.environ import *
from pyomo.dae import *
import pdb

def construct_model(sets,params,success, Fixed_Variables, Constrained_Variables):

	m = ConcreteModel()

	##Sets##
	m.I = Set(initialize=sets['Product'])
	m.J = Set(initialize=sets['Trial'])
	m.T = Set(initialize=sets['Time_Step'])
	m.R = Set(initialize=sets['resource_type'])

	def FS_IDX_init(model):
		return ((i,j,t) for i in m.I for j in m.J for t in m.T if (i,j,t) in Constrained_Variables)
	m.FS_IDX = set(within = m.I * m.J * m.T, initialize = FS_IDX_init)

	def SS_IDX_init(model):
		return ((i,j,t) for i in m.I for j in m.J for t in m.T if (i,j,t) not in Constrained_Variables and not in Fixed_Variables)
	m.SS_IDX = set(within = m.I * m.J * m.T, initialize = SS_IDX_init)

	### Fixed Parameters###
	m.Resource_Max = Param(initialize=params['resource_max'])
	m.GammaL = Param(initialize = params['gammaL'])
	m.GammaD = Param(initialize = params['gammaD'])
	m.Duration = Param(initialize = params['duration'])
	m.Trial_Cost = Param(initialize = params['trial_cost'])
	m.Resources_Required = Param(initialize = params['resource_required'])
	m.Revenue_Max = Param(initialize = params['revenue_max'])
	m.LastTimeStep = Param(initialize = params['last_time_step'])
	m.Last_Trial = Param(initialize = params['last_trial'])
	m.RR = Param(initialize = params['Running_Revenue'])
	m.OR = Param(initialize = params['Open_Revenue'])
	m.DF = Param(initialize = params['Discounting_Factor'])

	### Scenario Specific Parameters###
	m.Success = Param(initialize = success, muteable = True)

	### Variables ###
	m.FS_X = Var(m.FS_IDX, bounds=(0,1))
	m.SS_X = Var(m.SS_IDX, bounds=(0,1))
	m.Y = Var(m.I,m.J,m.T, bounds=(0,1))
	m.Z = Var(m.I,m.J,m.T, bounds= (0,1))

	##############################################################################
	### Constraints
	##############################################################################

	### Constraint-- The trial is complete if the trial was started a duration ago
	def Trial_Finish_rule(m,i,j,t):
		past_duration = t - m.Duration[i,j]
		if t==1:
			return model.Decision_Y[i,j,t] == 0
		elif  t > 1 and  past_duration < 1 :
			return model.Decision_Y[i,j,t] == model.Decision_Y[i,j,t-1]
		else:
			if (i,j,t) in Constrained_Variables:
				return m.Y[i,j,t] == m.Y[i,j,t-1] + m.FS_X[i,j,past_duration]
			else:
				return m.Y[i,j,t] == m.Y[i,j,t-1] + m.SS_X[i,j,past_duration]
	m.Trial_Finish = Constraint(m.I, m.J, m.T, rule=Trial_Finish_rule)

	def Calculate_Z_rule(m,i,j,t):
		if (i,j,t) in Fixed_Variables:
			decX = Fixed_Variables[i,j,t]
			if j > 1:
				pd = t - m.duration[i,j-1,t]
				if pd > 0 and (i,j-1,pd) in Fixed_Variables:
					decX_pd = Fixed_Variables[(i,j,pd)]
				elif pd > 0 and (i,j-1,pd) in Constrained_Variables:
					decX_pd = m.FS_X[i,j,pd]
				elif pd > 0:
					decX_pd = m.SS_X[i,j,pd]

		elif (i,j,t) in Constrained_Variables
			decX = m.FS_X[(i,j,t)]
			if j > 1:
				pd = t - m.duration[i,j-1,t]
				if pd > 0 and (i,j-1,pd) in Fixed_Variables:
					decX_pd = Fixed_Variables[(i,j,pd)]
				elif pd > 0 and (i,j-1,pd) in Constrained_Variables:
					decX_pd = m.FS_X[i,j,pd]
				elif pd > 0:
					decX_pd = m.SS_X[i,j,pd]
		else:
			decX = m.SS_X[(i,j,t)]
			if j > 1:
				pd = t - m.duration[i,j-1,t]
				if pd > 0 and (i,j-1,pd) in Fixed_Variables:
					decX_pd = Fixed_Variables[(i,j,pd)]
				elif pd > 0 and (i,j-1,pd) in Constrained_Variables:
					decX_pd = m.FS_X[i,j,pd]
				elif pd > 0:
					decX_pd = m.SS_X[i,j,pd]

		if j == 1:
			if t > 1:
				return m.Z[i,j,t] == m.Z[i,1,t-1] - decX
			else:
				return m.Z[i,j,1] == 1 - decX
		else:
			if t- m.Duration[i,j-1] >0 and t>1:
				return m.Z[i,j,t] == m.Z[i,j,t-1] + decX_pd - decX
			elif t-m.Duration[i,j-1] <= 0 and t > 1:
				return m.Z[i,j,t] == m.Z[i,j,t-1] - decX
			elif t==1:
				return m.Z[i,j,t] == -decX
	m.Calculate_Z = Constraint(m.I,m.J,m.T, rule= Calculate_Z_rule)

	### Constraint--You can only start each trial once
	def Constraint_3_rule(model,i,j):
		exp = 0
		for t in m.T:
			if (i,j,t) in Fixed_Variables:
				exp += Fixed_Variables[i,j,t]
			elif (i,j,t) in Constrained_Variables:
				exp += m.FS_X[i,j,t]
			else:
				exp += m.SS_X[i,j,t]
 		return exp <= 1
	model.Constraint_3 = Constraint(m.I,m.J,rule=Constraint_3_rule)

	### Constraint-- Must Start Previous Trial Before it can be finished
	def Constraint_4_rule(model,i,j,t):
		if j > 1:
			previous_trial = j-1
			LHS = 0
			for tprime in m.T:
				if tprime =< t:
					if (i,j,tprime) in Fixed_Variables:
						LHS += Fixed_Variables[(i,j,tprime)]
					elif (i,j,tprime) in Constrained_Variables:
						LHS += m.FS_X[i,j,tprime]
					else:
						LHS += m.SS_X[i,j,tprime]
			return LHS <= model.Decision_Y[i,previous_trial,t]
		else:
			return Constraint.Skip
	model.Constraint_4 = Constraint(m.I, m.J, m.T, rule=Constraint_4_rule)

	### Constraint-- Ensures resources are managed correctly
	def Resource_Constraint_rule(model,r,t):
		LHS = 0
		for i in m.I:
			for j in m.J:
				for tprime in m.T:
					if tprime > (t - m.Duration[i,j]) and tprime <= t:
						if (i,j,tprime) in Fixed_Variables:
							LHS += Fixed_Variables[i,j,tprime] * m.RR[i,j,r]
						elif (i,j,tprime) in Constrained_Variables:
							LHS += m.FS_X[i,j,tprime]
						else:
							LHS += m.SS_X[i,j,tprime]
		return LHS <= model.Resource_Max[r]
	model.Resource_Constraint = Constraint(m.R, m.T, rule= Resource_Constraint_rule)

	def ComputeFirstStageCosts_rule():
		cst = 0
		### Costs of Starting First Stage Trials
		cst += -sum((1 - (0.025 * (t - 1))) * m.Trial_Cost[i,j] * m.FS_X[i,j,t] for (i,j,t) in m.FS_IDX)

		cst += -sum((1 - (0.025 * (t - 1))) * m.Trial_Cost[i,j] * Fixed_Variables[(i,j,t)] for (i,j,t) in Fixed_Variables)

		### Revenue From Starting Final Trials
		cst += sum(m.Success[i]*(m.Revenue_Max[i] * m.FS_X[i,j,t]) for (i,j,t) in m.FS_IDX if j == m.last_trial)

		cst += sum(m.Success[i]*(m.Revenue_Max[i] * Fixed_Variables[(i,j,t)]) for (i,j,t) in Fixed_Variables if j == m.last_trial)

		### Losses From Not Starting Trials
		cst += sum(-model.Success[i]*model.GammaL[i]*(t + m.Duration[i, j]) * m.FS_X[i,j,t] for (i,j,t) in m.FS_IDX if j == m.last_trial)

		cst += sum(-model.Success[i]*model.GammaL[i]*(t + m.Duration[i, j]) * Fixed_Variables[(i,j,t)] for (i,j,t) in Fixed_Variables if j == m.last_trial)

		### Free Revenue From Products Started But Not Yet Finished
		cst	+= sum(m.Success[i] * m.RR[i,j,t] * m.DF[i,j+1]*m.FS_X[i,j,t] for (i,j,t) in m.FS_IDX if j < m.Last_Trial for t in m.T if t > m.LastTimeStep - m.Duration[i,j])

		cst	+= sum(m.Success[i] * m.RR[i,j,t] * m.DF[i,j+1]*Fixed_Variables[(i,j,t)] for (i,j,t) in Fixed_Variables if j < m.Last_Trial for t in m.T if t > m.LastTimeStep - m.Duration[i,j])

		return -cst
	model.FirstStageCost = Expression(rule=ComputeFirstStageCost_rule)

	def ComputeSecondStageCost_rule():
		cst = 0

		### Costs of Starting First Stage Trials
		cst += -sum((1 - (0.025 * (t - 1))) * model.Trial_Cost[i,j] * m.SS_X[i,j,t] for (i,j,t) in m.SS_IDX)

		### Revenue From Starting Final Trials
		cst += sum(m.Success[i]*(m.Revenue_Max[i] * m.SS_X[i,j,t]) for (i,j,t) in m.SS_IDX if j == m.last_trial)

		### Losses For Decisions Not Made
		cst += sum(-m.Success[i]*m.GammaD[i] * sum(m.Z[i,j,t] for j in m.J if j > 1) for i in m.I for t in m.T)

		### Losses for the time to development
		cst += sum(-m.Success[i]*m.GammaL[i]*(t + m.Duration[i, j]) * m.SS_X[i,j,t] for (i,j,t) in m.SS_IDX if j == m.Last_Trial)

		### Revenue Yet to Be Realized
		cst += sum(m.Success[i] * m.OR[i,j] * m.DF[i,j]* m.Z[i,j,m.LastTimeStep] for i in m.I for j in m.J)

		### Revenue for Started Products Not Yet Realized
		cst += sum(m.Success[i] * m.RR[i,j,t] * m.DF[i,j+1]*m.SS_X[i,j,t] for (i,j,t) in m.SS_IDX if j < m.Last_Trial for t in m.T if t > m.LastTimeStep - m.Duration[i,j])

		return -cst
	model.SecondStageCost = Expression(rule=ComputeSecondStageCost_rule)

	return model

def multistage_constructor(Tree_Object, FV, CD):

	m = ConcreteModel()

	##Sets##
	m.I = Set(initialize=sets['Product'])
	m.J = Set(initialize=sets['Trial'])
	m.T = Set(initialize=sets['Time_Step'])
	m.R = Set(initialize=sets['resource_type'])

	def IDX_init(model,indicies):
		return ((i,j,t) for i in m.I for j in m.J for t in m.T if (i,j,t) in indicies)

	for node_name in Tree_Object.nodes:
		m.add_component(node_name, Set(within = m.I * m.J * m.T, initialize = IDX_init(node_name))

	### Fixed Parameters###
	m.Resource_Max = Param(initialize=params['resource_max'])
	m.GammaL = Param(initialize = params['gammaL'])
	m.GammaD = Param(initialize = params['gammaD'])
	m.Duration = Param(initialize = params['duration'])
	m.Trial_Cost = Param(initialize = params['trial_cost'])
	m.Resources_Required = Param(initialize = params['resource_required'])
	m.Revenue_Max = Param(initialize = params['revenue_max'])
	m.LastTimeStep = Param(initialize = params['last_time_step'])
	m.Last_Trial = Param(initialize = params['last_trial'])
	m.RR = Param(initialize = params['Running_Revenue'])
	m.OR = Param(initialize = params['Open_Revenue'])
	m.DF = Param(initialize = params['Discounting_Factor'])

	### Scenario Specific Parameters###
	m.Success = Param(initialize = success, muteable = True)

	### Variables ###
	m.X = Var(m.FS_IDX, bounds=(0,1))
	m.XX = Var(m.SS_IDX, bounds=(0,1))
	mvars = [m.X, m.XX]
	m.Y = Var(m.I,m.J,m.T, bounds=(0,1))
	m.Z = Var(m.I,m.J,m.T, bounds= (0,1))

	def ComputeStageCost_rule():

		cst = 0

		for i in range(2):
			decvar = mvars[i]

			### Costs of Starting First Stage Trials
			cst += -sum((1 - (0.025 * (t - 1))) * model.Trial_Cost[i,j] * decvar[i,j,t] for (i,j,t) in m.SS_IDX ### this should be the stageX indicies)

			### Revenue From Starting Final Trials
			cst += sum(m.Success[i]*(m.Revenue_Max[i] * decvar[i,j,t]) for (i,j,t) in m.SS_IDX if j == m.last_trial)

			### Losses for the time to development
			cst += sum(-m.Success[i]*m.GammaL[i]*(t + m.Duration[i, j]) * decvar[i,j,t] for (i,j,t) in m.SS_IDX if j == m.Last_Trial)

			### Revenue for Started Products Not Yet Realized
			cst += sum(m.Success[i] * m.RR[i,j,t] * m.DF[i,j+1]*decvar[i,j,t] for (i,j,t) in m.SS_IDX if j < m.Last_Trial for t in m.T if t > m.LastTimeStep - m.Duration[i,j])

		### Losses For Decisions Not Made
		cst += sum(-m.Success[i]*m.GammaD[i] * sum(m.Z[i,j,t] for j in m.J if j > 1) for i in m.I for t in m.T)

		### Revenue Yet to Be Realized
		cst += sum(m.Success[i] * m.OR[i,j] * m.DF[i,j]* m.Z[i,j,m.LastTimeStep] for i in m.I for j in m.J)

		return -cst

reason: from pyomo


text:
# coding: utf-8

# In[1]:

import numpy as np
import pandas as pd

# ## Preprocessing Operations

# In[2]:

def preprocess_chemicals(dataset_name = "chemicals.csv", yearbound=2000):
    df = pd.read_csv(dataset_name, encoding = "utf-8")
    for i, c in enumerate(df["contaminant_level"].unique()):
        df.loc[df["contaminant_level"]==c, "contaminant_level"] = i
    return df

# In[3]:

def preprocess_droughts(dataset_name='droughts', year_bound=2000):
    dataset_path = dataset_name + '.csv'
    df = pd.read_csv(dataset_path, encoding = "utf-8")
    valid_start = list(df['valid_start'])
    valid_start = list(map(lambda x: int(x[0:4]), valid_start))
    df['valid_start'] = valid_start
    valid_end = list(df['valid_end'])
    valid_end = list(map(lambda x: int(x[0:4]), valid_end))
    df['valid_end'] = valid_end
    valid_dates = df['valid_start'] >= year_bound
    df = df[valid_dates]
    df.to_csv(dataset_name + '_processed.csv', index=False)
    df['year'] = list(df['valid_start'])
    return df

# In[4]:

def preprocess_earnings(dataset_name='earnings', year_bound=2000):
    dataset_path = dataset_name + '.csv'
    df = pd.read_csv(dataset_path, encoding = "utf-8")
    df_mod = pd.DataFrame({'fips':df['fips'], 'county':df['county'], 'total_med':df['total_med'], 'year':df['year']})
    df.to_csv(dataset_name + '_processed.csv', index=False)
    possible_years = df['year'] >= year_bound
    df = df[possible_years]
    return df

# In[5]:

def preprocess_education_attainment(dataset_name="education_attainment", yearbound=2000):
    df = pd.read_csv(dataset_name + '.csv', encoding = "utf-8")
    df = df.loc[df["year"]>=yearbound]
    return df

# In[6]:

def preprocess_industry_occupation(dataset_name='industry_occupation', year_bound=2000):
    dataset_path = dataset_name + '.csv'
    df = pd.read_csv(dataset_path, encoding = "latin-1")
    df = df[df['year'] > year_bound]
    df.to_csv(dataset_name + '_processed.csv', index=False)
    return df

# In[7]:

def preprocess_water_usage(dataset_name = "water_usage", yearbound=2000):
    df = pd.read_csv("water_usage.csv")
    df = df.drop(['ps_groundwater', 'ps_surfacewater'], axis=1)
    return df

# In[8]:

preprocess_chemicals().to_csv("chemicals_processed.csv")
preprocess_droughts().to_csv("droughts_processed.csv")
preprocess_earnings().to_csv("earnings_processed.csv")
preprocess_education_attainment().to_csv("education_attainment_processed.csv")
preprocess_industry_occupation().to_csv("industry_occupation_processed.csv")
preprocess_water_usage().to_csv("water_usage_processed.csv")

# ## Joining Operations

# In[9]:

'''
@param df : pandas dataframe
@param other_df : another pandas dataframe
@reurn merge of two dataframes
'''
def join_two_datasets(df, other_df, fields):

    df['fips'] = df['fips'].astype(int)
    other_df['fips'] = other_df['fips'].astype(int)
    return pd.merge(df, other_df, on=fields)

'''
@return dictionary mapping name to pandas data frame
'''
def generate_dfs():
    all_dfs = {}
    all_dfs['water_usage'] = preprocess_water_usage()
    all_dfs['industry_occupation'] = preprocess_industry_occupation()
    all_dfs['chemicals'] = preprocess_chemicals()
    all_dfs['droughts'] = preprocess_droughts()
    all_dfs['earnings'] = preprocess_earnings()
    all_dfs['education_attainment'] = preprocess_education_attainment()
    return all_dfs


text:
# cec2017.simple
# Author: Duncan Tilley
# Simple function definitions, f1 to f10

import numpy as np

from . import basic, transforms

def f1(x, rotation=None, shift=None):
    """
    Shifted and Rotated Bent Cigar Function

    Args:
        x (array): Input vector of dimension 2, 10, 20, 30, 50 or 100.
        rotation (matrix): Optional rotation matrix. If None (default), the
            official matrix from the benchmark suite will be used.
        shift (array): Optional shift vector. If None (default), the official
            vector from the benchmark suite will be used.
    """
    nx = len(x)
    if rotation is None:
        rotation = transforms.rotations[nx][0]
    if shift is None:
        shift = transforms.shifts[0][:nx]
    x_transformed = np.matmul(rotation, x - shift)
    return basic.bent_cigar(x_transformed) + 100.0

def f2(x, rotation=None, shift=None):
    """
    (Deprecated) Shifted and Rotated Sum of Different Power Function

    Args:
        x (array): Input vector of dimension 2, 10, 20, 30, 50 or 100.
        rotation (matrix): Optional rotation matrix. If None (default), the
            official matrix from the benchmark suite will be used.
        shift (array): Optional shift vector. If None (default), the official
            vector from the benchmark suite will be used.
    """
    if "warned" not in f2.__dict__:
        f2.warned = True
        print("WARNING: f2 has been deprecated from the CEC 2017 benchmark suite")

    nx = len(x)
    if rotation is None:
        rotation = transforms.rotations[nx][1]
    if shift is None:
        shift = transforms.shifts[1][:nx]
    x_transformed = np.matmul(rotation, x - shift)
    return basic.sum_diff_pow(x_transformed) + 200.0

def f3(x, rotation=None, shift=None):
    """
    Shifted and Rotated Zakharov Function

    Args:
        x (array): Input vector of dimension 2, 10, 20, 30, 50 or 100.
        rotation (matrix): Optional rotation matrix. If None (default), the
            official matrix from the benchmark suite will be used.
        shift (array): Optional shift vector. If None (default), the official
            vector from the benchmark suite will be used.
    """
    nx = len(x)
    if rotation is None:
        rotation = transforms.rotations[nx][2]
    if shift is None:
        shift = transforms.shifts[2][:nx]
    x_transformed = np.matmul(rotation, x - shift)
    return basic.zakharov(x_transformed) + 300.0

def f4(x, rotation=None, shift=None):
    """
    Shifted and Rotated Rosenbrock’s Function

    Args:
        x (array): Input vector of dimension 2, 10, 20, 30, 50 or 100.
        rotation (matrix): Optional rotation matrix. If None (default), the
            official matrix from the benchmark suite will be used.
        shift (array): Optional shift vector. If None (default), the official
            vector from the benchmark suite will be used.
    """
    nx = len(x)
    if rotation is None:
        rotation = transforms.rotations[nx][3]
    if shift is None:
        shift = transforms.shifts[3][:nx]
    x_transformed = np.matmul(rotation, (x - shift))
    return basic.rosenbrock(x_transformed) + 400.0

def f5(x, rotation=None, shift=None):
    """
    Shifted and Rotated Rastrigin's Function

    Args:
        x (array): Input vector of dimension 2, 10, 20, 30, 50 or 100.
        rotation (matrix): Optional rotation matrix. If None (default), the
            official matrix from the benchmark suite will be used.
        shift (array): Optional shift vector. If None (default), the official
            vector from the benchmark suite will be used.
    """
    nx = len(x)
    if rotation is None:
        rotation = transforms.rotations[nx][4]
    if shift is None:
        shift = transforms.shifts[4][:nx]
    x_transformed = np.matmul(rotation, (x - shift))
    return basic.rastrigin(x_transformed) + 500.0

def f6(x, rotation=None, shift=None):
    """
    Shifted and Rotated Schaffer’s F7 Function

    Args:
        x (array): Input vector of dimension 2, 10, 20, 30, 50 or 100.
        rotation (matrix): Optional rotation matrix. If None (default), the
            official matrix from the benchmark suite will be used.
        shift (array): Optional shift vector. If None (default), the official
            vector from the benchmark suite will be used.
    """
    nx = len(x)
    if rotation is None:
        rotation = transforms.rotations[nx][5]
    if shift is None:
        shift = transforms.shifts[5][:nx]
    x_transformed = np.matmul(rotation, (x - shift))
    return basic.schaffers_f7(x_transformed) + 600.0

def f7(x, rotation=None, shift=None):
    """
    Shifted and Rotated Lunacek Bi-Rastrigin’s Function

    Args:
        x (array): Input vector of dimension 2, 10, 20, 30, 50 or 100.
        rotation (matrix): Optional rotation matrix. If None (default), the
            official matrix from the benchmark suite will be used.
        shift (array): Optional shift vector. If None (default), the official
            vector from the benchmark suite will be used.
    """
    nx = len(x)
    if rotation is None:
        rotation = transforms.rotations[nx][6]
    if shift is None:
        shift = transforms.shifts[6][:nx]
    # pass the shift and rotation directly to the function
    return basic.lunacek_bi_rastrigin(x, shift, rotation) + 700.0

def f8(x, rotation=None, shift=None):
    """
    Shifted and Rotated Non-Continuous Rastrigin’s Function

    Args:
        x (array): Input vector of dimension 2, 10, 20, 30, 50 or 100.
        rotation (matrix): Optional rotation matrix. If None (default), the
            official matrix from the benchmark suite will be used.
        shift (array): Optional shift vector. If None (default), the official
            vector from the benchmark suite will be used.
    """
    nx = len(x)
    if rotation is None:
        rotation = transforms.rotations[nx][7]
    if shift is None:
        shift = transforms.shifts[7][:nx]
    # pass the shift and rotation directly to the function
    return basic.non_cont_rastrigin(x, shift, rotation) + 800.0

def f9(x, rotation=None, shift=None):
    """
    Shifted and Rotated Levy Function

    Args:
        x (array): Input vector of dimension 2, 10, 20, 30, 50 or 100.
        rotation (matrix): Optional rotation matrix. If None (default), the
            official matrix from the benchmark suite will be used.
        shift (array): Optional shift vector. If None (default), the official
            vector from the benchmark suite will be used.
    """
    nx = len(x)
    if rotation is None:
        rotation = transforms.rotations[nx][8]
    if shift is None:
        shift = transforms.shifts[8][:nx]
    x_transformed = np.matmul(rotation, (x - shift))
    return basic.levy(x_transformed) + 900.0

def f10(x, rotation=None, shift=None):
    """
    Shifted and Rotated Schwefel’s Function

    Args:
        x (array): Input vector of dimension 2, 10, 20, 30, 50 or 100.
        rotation (matrix): Optional rotation matrix. If None (default), the
            official matrix from the benchmark suite will be used.
        shift (array): Optional shift vector. If None (default), the official
            vector from the benchmark suite will be used.
    """
    nx = len(x)
    if rotation is None:
        rotation = transforms.rotations[nx][9]
    if shift is None:
        shift = transforms.shifts[9][:nx]
    x_transformed = np.matmul(rotation, (x - shift))
    return basic.modified_schwefel(x_transformed) + 1000.0

all_functions = [f1, f2, f3, f4, f5, f6, f7, f8, f9, f10]

max_stars_repo_name: Evelkos/CellularEvolutionaryAlgorithm

max_issues_repo_name: Evelkos/CellularEvolutionaryAlgorithm

max_forks_repo_name: Evelkos/CellularEvolutionaryAlgorithm


text:
#!/usr/bin/env python

__author__ = "C. Clayton Violand"
__copyright__ = "Copyright 2017"

## Preps cifar-10 input data (1000) for system by subtracting specified mean.
##

import caffe
import numpy as np
import sys

def main():
    MEAN_IMAGE = 'helper/cifar-10_tools/mean.binaryproto'

    READ_FILE = 'helper/cifar-10_tools/cifar-10_1000/cifar-10_1000.csv'
    WRITE_FILE = 'helper/cifar-10_tools/mean_image_reduced/cifar-10_img_norm_1000.csv'

    blob = caffe.proto.caffe_pb2.BlobProto()
    data = open(MEAN_IMAGE, 'rb').read()
    blob.ParseFromString(data)

    data = np.array(blob.data)
    arr = np.array(caffe.io.blobproto_to_array(blob))
    mean_image = arr[0].reshape(1, 3072)

    data = np.genfromtxt(READ_FILE, delimiter=',')

    images = data[:, 1:]
    labels = data[:, 0].reshape(1000, 1)

    result = images - mean_image

    results = np.hstack((labels, result))
    np.savetxt(WRITE_FILE, results, delimiter=',')

if __name__ == "__main__":
    main()


text:
import numpy as np
import os
import cv2

import tensorflow as tf
import tensorflow.contrib.slim as slim
import random
from glob import glob
from tqdm import tqdm

class Image_data:

    def __init__(self, img_height, img_width, channels, dataset_path, label_list, augment_flag):
        self.img_height = img_height
        self.img_width = img_width
        self.channels = channels
        self.augment_flag = augment_flag

        self.label_list = label_list
        self.dataset_path = dataset_path

        self.label_onehot_list = []
        self.image = []
        self.label = []

    def image_processing(self, filename, label):
        x = tf.read_file(filename)
        x_decode = tf.image.decode_jpeg(x, channels=self.channels, dct_method='INTEGER_ACCURATE')
        img = tf.image.resize_images(x_decode, [self.img_height, self.img_width])
        img = tf.cast(img, tf.float32) / 127.5 - 1

        if self.augment_flag :
            augment_height_size = self.img_height + (30 if self.img_height == 256 else int(self.img_height * 0.1))
            augment_width_size = self.img_width + (30 if self.img_width == 256 else int(self.img_width * 0.1))

            img = tf.cond(pred=tf.greater_equal(tf.random_uniform(shape=[], minval=0.0, maxval=1.0), 0.5),
                          true_fn=lambda : augmentation(img, augment_height_size, augment_width_size),
                          false_fn=lambda : img)

        return img, label

    def preprocess(self):
        # self.label_list = ['tiger', 'cat', 'dog', 'lion']

        v = 0

        for label in self.label_list : # fabric
            label_one_hot = list(get_one_hot(v, len(self.label_list))) # [1, 0, 0, 0, 0]
            self.label_onehot_list.append(label_one_hot)
            v = v+1

            image_list = glob(os.path.join(self.dataset_path, label) + '/*.png') + glob(os.path.join(self.dataset_path, label) + '/*.jpg')
            label_one_hot = [label_one_hot] * len(image_list)

            self.image.extend(image_list)
            self.label.extend(label_one_hot)

class ImageData_celebA:

    def __init__(self, img_height, img_width, channels, dataset_path, label_list, augment_flag):
        self.img_height = img_height
        self.img_width = img_width
        self.channels = channels
        self.augment_flag = augment_flag
        self.label_list = label_list

        self.dataset_path = os.path.join(dataset_path, 'train')
        self.file_name_list = [os.path.basename(x) for x in glob(self.dataset_path + '/*.png')]
        self.lines = open(os.path.join(dataset_path, 'list_attr_celeba.txt'), 'r').readlines()

        self.image = []
        self.label = []

        self.test_image = []
        self.test_label = []

        self.attr2idx = {}
        self.idx2attr = {}

        self.train_label_onehot_list = []
        self.test_label_onehot_list = []

    def image_processing(self, filename, label, fix_label):
        x = tf.read_file(filename)
        x_decode = tf.image.decode_jpeg(x, channels=self.channels, dct_method='INTEGER_ACCURATE')
        img = tf.image.resize_images(x_decode, [self.img_height, self.img_width])
        img = tf.cast(img, tf.float32) / 127.5 - 1

        if self.augment_flag :
            augment_height = self.img_height + (30 if self.img_height == 256 else int(self.img_height * 0.1))
            augment_width = self.img_width + (30 if self.img_width == 256 else int(self.img_width * 0.1))

            img = tf.cond(pred=tf.greater_equal(tf.random_uniform(shape=[], minval=0.0, maxval=1.0), 0.5),
                          true_fn=lambda: augmentation(img, augment_height, augment_width),
                          false_fn=lambda: img)

        return img, label, fix_label

    def preprocess(self, phase):

        all_attr_names = self.lines[1].split()
        for i, attr_name in enumerate(all_attr_names):
            self.attr2idx[attr_name] = i
            self.idx2attr[i] = attr_name

        lines = self.lines[2:]
        random.seed(1234)
        random.shuffle(lines)

        for i, line in enumerate(tqdm(lines)):
            split = line.split()
            if split[0] in self.file_name_list:
                filename = os.path.join(self.dataset_path, split[0])
                values = split[1:]

                label = []

                for attr_name in self.label_list:
                    idx = self.attr2idx[attr_name]

                    if values[idx] == '1':
                        label.append(1.0)
                    else:
                        label.append(0.0)

                if i < 2000:
                    self.test_image.append(filename)
                    self.test_label.append(label)
                else:
                    if phase == 'test' :
                        break
                    self.image.append(filename)
                    self.label.append(label)
                # ['./dataset/celebA/train/019932.png', [1, 0, 0, 0, 1]]

        print()

        self.test_label_onehot_list = create_labels(self.test_label, self.label_list)
        if phase == 'train' :
            self.train_label_onehot_list = create_labels(self.label, self.label_list)

        print('\n Finished preprocessing the CelebA dataset...')

def load_test_image(image_path, img_width, img_height, img_channel):

    if img_channel == 1 :
        img = cv2.imread(image_path, flags=cv2.IMREAD_GRAYSCALE)
    else :
        img = cv2.imread(image_path, flags=cv2.IMREAD_COLOR)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    img = cv2.resize(img, dsize=(img_width, img_height))

    if img_channel == 1 :
        img = np.expand_dims(img, axis=0)
        img = np.expand_dims(img, axis=-1)
    else :
        img = np.expand_dims(img, axis=0)

    img = img/127.5 - 1

    return img

def load_one_hot_vector(label_list, target_label) :
    label_onehot_dict = {}

    v = 0
    for label_name in label_list :
        label_one_hot = list(get_one_hot(v, len(label_list)))
        label_onehot_dict[label_name] = [label_one_hot]

    x = label_onehot_dict[target_label]

    return x

def label2onehot(label_list) :
    v = 0
    label_onehot_list = []
    for _ in label_list:  # fabric
        label_one_hot = list(get_one_hot(v, len(label_list)))  # [1, 0, 0, 0, 0]
        label_onehot_list.append(label_one_hot)
        v = v + 1

    return label_onehot_list

def augmentation(image, augment_height, augment_width):
    seed = random.randint(0, 2 ** 31 - 1)
    ori_image_shape = tf.shape(image)
    image = tf.image.random_flip_left_right(image, seed=seed)
    image = tf.image.resize_images(image, [augment_height, augment_width])
    image = tf.random_crop(image, ori_image_shape, seed=seed)
    return image

def save_images(images, size, image_path):
    return imsave(inverse_transform(images), size, image_path)

def inverse_transform(images):
    return ((images+1.) / 2) * 255.0

def imsave(images, size, path):
    images = merge(images, size)
    images = cv2.cvtColor(images.astype('uint8'), cv2.COLOR_RGB2BGR)

    return cv2.imwrite(path, images)

def merge(images, size):
    h, w = images.shape[1], images.shape[2]
    img = np.zeros((h * size[0], w * size[1], 3))
    for idx, image in enumerate(images):
        i = idx % size[1]
        j = idx // size[1]
        img[h*j:h*(j+1), w*i:w*(i+1), :] = image

    return img

def return_images(images, size) :
    x = merge(images, size)

    return x

def check_folder(log_dir):
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)
    return log_dir

def show_all_variables():
    model_vars = tf.trainable_variables()
    slim.model_analyzer.analyze_vars(model_vars, print_info=True)

def str2bool(x):
    return x.lower() in ('true')

def get_one_hot(targets, nb_classes):

    x = np.eye(nb_classes)[targets]

    return x

def create_labels(c_org, selected_attrs=None):
    """Generate target domain labels for debugging and testing."""
    # Get hair color indices.
    c_org = np.asarray(c_org)
    hair_color_indices = []
    for i, attr_name in enumerate(selected_attrs):
        if attr_name in ['Black_Hair', 'Blond_Hair', 'Brown_Hair', 'Gray_Hair']:
            hair_color_indices.append(i)

    c_trg_list = []

    for i in range(len(selected_attrs)):
        c_trg = c_org.copy()

        if i in hair_color_indices:  # Set one hair color to 1 and the rest to 0.
            c_trg[:, i] = 1.0
            for j in hair_color_indices:
                if j != i:
                    c_trg[:, j] = 0.0
        else:
            c_trg[:, i] = (c_trg[:, i] == 0)  # Reverse attribute value.

        c_trg_list.append(c_trg)

    c_trg_list = np.transpose(c_trg_list, axes=[1, 0, 2]) # [bs, c_dim, c_dim]

    return c_trg_list

def pytorch_xavier_weight_factor(gain=0.02, uniform=False) :

    if uniform :
        factor = gain * gain
        mode = 'FAN_AVG'
    else :
        factor = (gain * gain) / 1.3
        mode = 'FAN_AVG'

    return factor, mode, uniform

def pytorch_kaiming_weight_factor(a=0.0, activation_function='relu', uniform=False) :

    if activation_function == 'relu' :
        gain = np.sqrt(2.0)
    elif activation_function == 'leaky_relu' :
        gain = np.sqrt(2.0 / (1 + a ** 2))
    elif activation_function =='tanh' :
        gain = 5.0 / 3
    else :
        gain = 1.0

    if uniform :
        factor = gain * gain
        mode = 'FAN_IN'
    else :
        factor = (gain * gain) / 1.3
        mode = 'FAN_IN'

    return factor, mode, uniform