text:
import networkx as nx
import csv
import re
import string
import numpy as np
import os
import sys
import pandas as pd
import warnings

class InfrastructureNode(object):
    """
    This class models a node in an infrastructure network

    Attributes
    ----------
    id : int
        Node id
    net_id : int
        The id of the layer of the network to which the node belong
    local_id : int
        Local node id
    failure_probability : float
        Failure probability of the node
    functionality : bool
        Functionality state of the node
    repaired : bool
        If the node is repaired or not
    reconstruction_cost : float
        Reconstruction cost of the node
    oversupply_penalty : float
        Penalty per supply unit  of the main commodity that is not used for the the node
    undersupply_penalty : float
        Penalty per demand unit  of the main commodity that is not satisfied for the the node
    demand : float
        Demand or supply value of the main commodity assigned to the node
    space : int
        The id of the geographical space where the node is
    resource_usage : dict
        The dictionary that shows how many resource (of each resource type) is employed to repair the node
    extra_com : dict
        The dictionary that shows demand, oversupply_penalty, and undersupply_penalty corresponding to commodities
        other than the main commodity
    """
    def __init__(self, id, net_id, local_id=""):
        self.id = id
        self.net_id = net_id
        if local_id == "":
            self.local_id = id
        self.local_id = local_id
        self.failure_probability = 0.0
        self.functionality = 1.0
        self.repaired = 1.0
        self.reconstruction_cost = 0.0
        self.oversupply_penalty = 0.0
        self.undersupply_penalty = 0.0
        self.demand = 0.0
        self.space = 0
        self.resource_usage = {}
        self.extra_com = {}

    def set_failure_probability(self, failure_probability):
        """
        This function sets the failure probability of the node

        Parameters
        ----------
        failure_probability : float
            Assigned failure probability of node

        Returns
        -------
        None.

        """
        self.failure_probability = failure_probability

    def set_extra_commodity(self, extra_commodity):
        """
        This function initialize the dictionary for the extra commodities

        Parameters
        ----------
        extra_commodity : list
            List of extra commodities

        Returns
        -------
        None.

        """
        for l in extra_commodity:
            self.extra_com[l] = {'demand': 0, 'oversupply_penalty': 0, 'undersupply_penalty': 0}

    def set_resource_usage(self, resource_names):
        """
        This function initialize the dictionary for resource usage per all resource types in the analysis

        Parameters
        ----------
        resource_names : list
            List of resource types

        Returns
        -------
        None.

        """
        for rc in resource_names:
            self.resource_usage[rc] = 0

    def in_space(self, space_id):
        """
        This function checks if the node is in a given space or not

        Parameters
        ----------
        space_id :
            The id of the space that is checked

        Returns
        -------
            : bool
            Returns 1 if the node is in the space, and 0 otherwise.
        """
        if self.space == space_id:
            return 1
        else:
            return 0

class InfrastructureArc(object):
    """
    This class models an arc in an infrastructure network

    Attributes
    ----------
    source : int
        Start (or head) node id
    dest : int
        End (or tail) node id
    layer : int
        The id of the layer of the network to which the arc belong
    failure_probability : float
        Failure probability of the arc
    functionality : bool
        Functionality state of the node
    repaired : bool
        If the arc is repaired or not
    flow_cost : float
        Unit cost of sending the main commodity through the arc
    reconstruction_cost : float
        Reconstruction cost of the arc
    capacity : float
        Maximum volume of the commodities that the arc can carry
    space : int
        The id of the geographical space where the arc is
    resource_usage : dict
        The dictionary that shows how many resource (of each resource type) is employed to repair the arc
    extra_com : dict
        The dictionary that shows flow_cost corresponding to commodities other than the main commodity
    is_interdep : bool
        If arc represent a normal arc (that carry commodity within a single layer) or physical interdependency between
        nodes from different layers
    """
    def __init__(self, source, dest, layer, is_interdep=False):
        self.source = source
        self.dest = dest
        self.layer = layer
        self.failure_probability = 0.0
        self.functionality = 1.0
        self.repaired = 1.0
        self.flow_cost = 0.0
        self.reconstruction_cost = 0.0
        self.resource_usage = {}
        self.capacity = 0.0
        self.space = 0
        self.extra_com = {}
        self.is_interdep = is_interdep

    def set_extra_commodity(self, extra_commodity):
        """
        This function initialize the dictionary for the extra commodities

        Parameters
        ----------
        extra_commodity : list
            List of extra commodities

        Returns
        -------
        None.

        """
        for l in extra_commodity:
            self.extra_com[l] = {'flow_cost': 0}

    def set_resource_usage(self, resource_names):
        """
        This function initialize the dictionary for resource usage per all resource types in the analysis

        Parameters
        ----------
        resource_names : list
            List of resource types

        Returns
        -------
        None.

        """
        for rc in resource_names:
            self.resource_usage[rc] = 0

    def in_space(self, space_id):
        """
        This function checks if the arc is in a given space or not

        Parameters
        ----------
        space_id :
            The id of the space that is checked

        Returns
        -------
            : bool
            Returns 1 if the arc is in the space, and 0 otherwise.
        """
        if self.space == space_id:
            return 1
        else:
            return 0

class InfrastructureInterdepArc(InfrastructureArc):
    """
    This class models a physical interdependency between nodes from two different layers. This class inherits from
    :class:`InfrastructureArc`, where `source` attributes corresponds to the dependee node, and `dest` corresponds to
    the depender node. The depender node is non-functional if the corresponding dependee node is non-functional.

    Attributes
    ----------
    source_layer : int
        The id of the layer where the dependee node is
    dest_layer : int
        The id of the layer where the depender node is
    gamma : float
        The strength of the dependency, which is a number between 0 and 1.
    """
    def __init__(self, source, dest, source_layer, dest_layer, gamma):
        super(InfrastructureInterdepArc, self).__init__(source, dest, source_layer, True)
        self.source_layer = source_layer
        self.dest_layer = dest_layer
        self.gamma = gamma

class InfrastructureSpace(object):
    """
    This class models a geographical space.

    Attributes
    ----------
    id : int
        The id of the space
    cost : float
        The cost of preparing the space for a repair action
    """
    def __init__(self, id, cost):
        self.id = id
        self.cost = cost

class InfrastructureNetwork(object):
    """
    Stores information of the infrastructure network

    Attributes
    ----------
    G : networkx.DiGraph
        The networkx graph object that stores node, arc, and interdependency information
    S : list
        List of geographical spaces on which the network lays
    id : int
        Id of the network
    """
    def __init__(self, id):
        self.G = nx.DiGraph()
        self.S = []
        self.id = id

    def copy(self):
        """
        This function copies the current :class:`InfrastructureNetwork` object

        Returns
        -------
        new_net: :class:`InfrastructureNetwork`
            Copy of the current infrastructure network object
        """
        new_net = InfrastructureNetwork(self.id)
        new_net.G = self.G.copy()
        new_net.S = [s for s in self.S]
        return new_net

    def update_with_strategy(self, player_strategy):
        """
        This function modify the functionality of node and arc per a given strategy

        Parameters
        ----------
        player_strategy : list
            Given strategy, where the first list item shows the functionality of nodes, and the second one is for arcs

        Returns
        -------
        None.

        """
        for q in player_strategy[0]:
            node = q
            strat = player_strategy[0][q]
            self.G.node[q]['data']['inf_data'].repaired = round(strat['repair'])
            self.G.node[q]['data']['inf_data'].functionality = round(strat['w'])
        for q in player_strategy[1]:
            src = q[0]
            dst = q[1]
            strat = player_strategy[1][q]
            self.G[src][dst]['data']['inf_data'].repaired = round(strat['repair'])
            self.G[src][dst]['data']['inf_data'].functionality = round(strat['y'])

    def get_clusters(self, layer):
        """
        This function find the clusters in a layer of the network

        Parameters
        ----------
        layer : int
            The id of the desired layer

        Returns
        -------
            : list
            List of layer components

        """
        g_prime_nodes = [n[0] for n in self.G.nodes(data=True) if
                         n[1]['data']['inf_data'].net_id == layer and n[1]['data']['inf_data'].functionality == 1.0]
        g_prime = nx.DiGraph(self.G.subgraph(g_prime_nodes).copy())
        g_prime.remove_edges_from(
            [(u, v) for u, v, a in g_prime.edges(data=True) if a['data']['inf_data'].functionality == 0.0])
        # print nx.connected_components(g_prime.to_undirected())
        return list(nx.connected_components(g_prime.to_undirected()))

    def gc_size(self, layer):
        """
        This function finds the size of the largest component in a layer of the network

        Parameters
        ----------
        layer : int
            The id of the desired layer

        Returns
        -------
            : int
            Size of the largest component in the layer
        """
        g_prime_nodes = [n[0] for n in self.G.nodes(data=True) if
                         n[1]['data']['inf_data'].net_id == layer and n[1]['data']['inf_data'].functionality == 1.0]
        g_prime = nx.Graph(self.G.subgraph(g_prime_nodes))
        g_prime.remove_edges_from(
            [(u, v) for u, v, a in g_prime.edges(data=True) if a['data']['inf_data'].functionality == 0.0])
        cc = nx.connected_components(g_prime.to_undirected())
        if cc:
            # if len(list(cc)) == 1:
            #    print "I'm here"
            #    return len(list(cc)[0])
            # cc_list=list(cc)
            # print "Length",len(cc_list)
            # if len(cc_list) == 1:
            #    return len(cc_list[0])
            return len(max(cc, key=len))
        else:
            return 0

    def to_game_file(self, layers=None):
        """
        This function writes the multi-defender security games.

        Parameters
        ----------
        layers : list
            List of layers in the game.

        Returns
        -------
        None.

        """
        if layers is None:
            layers = [1, 3]
        with open("../results/indp_gamefile.game") as f:
            num_players = len(layers)
            num_targets = len([n for n in self.G.nodes(data=True) if n[1]['data']['inf_data'].net_id in layers])
            f.write(str(num_players) + "," + str(num_targets) + ",2\n")
            for layer in range(len(layers)):
                layer_nodes = [n for n in self.G.nodes(data=True) if n[1]['data']['inf_data'].net_id == layers[layer]]
                for node in layer_nodes:
                    def_values = [0.0] * len(layers)
                    def_values[layer] = abs(node[1]['data']['inf_data'].demand)
                    atk_values = sum(def_values)
                    f.write(str(layer) + "\n")
                    # f.write("0.0,1.0")
                    for v in def_values:
                        f.write(str(v) + "\n")
                    f.write(str(atk_values) + "\n")

    def to_csv(self, filename="infrastructure_adj.csv"):
        """
        This function writes the object to a csv file

        Parameters
        ----------
        filename : str
            Name of the file to which the network should be written

        Returns
        -------
        None.

        """
        with open(filename, 'w') as f:
            for u, v, a in self.G.edges(data=True):
                f.write(str(u[0]) + "." + str(u[1]) + "," + str(v[0]) + "." + str(v[1]) + "\n")

def load_infrastructure_data(base_dir, ext_interdependency_dir=None,  magnitude=6, sim_number=1, cost_scale=1.0,
                             data_format='shelby_extended', extra_commodity=None):
    """
    This function reads the infrastructure network from file

    Parameters
    ----------
    base_dir : str
        The address of the folder where the basic network information (topology, parameters, etc.) are stored
    ext_interdependency_dir : str
        (only for old format of input data) The address of the file where the interdependency data are stored. The
        default in None.
    magnitude : int
        (only for old format of input data) Magnitude of the initial damage scenario. The default is 6.
    sim_number : int
        (only for old format of input data) The simulation number. The default is 1.
    cost_scale : float
        The factor by which all cost values has to multiplied. The default is 1.0.
    data_format : str
        The format of input data, which is either "shelby_old" (consistent with the output format of Xpress optimizing
        software employed by Andres Gonzalez) or "shelby_extended" (which is devised by Hesam Talebiyan). The default
        is "shelby_extended".
    extra_commodity :
        (only for extended format of input data) List of extra-commodities in the analysis. The default is None, which
        only considers a main commodity.

    Returns
    -------
    G : networkx.DiGraph
        The networkx graph object that stores node, arc, and interdependency information

    """
    if data_format == 'shelby_old':
        G = load_infrastructure_array_format(base_dir=base_dir, ext_interdependency_dir=ext_interdependency_dir,
                                             magnitude=magnitude, sim_number=sim_number, cost_scale=cost_scale)
        return G
    elif data_format == 'shelby_extended':
        G = load_infrastructure_array_format_extended(base_dir=base_dir, cost_scale=cost_scale,
                                                      extra_commodity=extra_commodity)
        return G
    else:
        sys.exit('Error: no interdependent network is found')

def load_interdependencies(net_number, src_net_number, base_dir="../data/INDP_4-12-2016"):
    """
    (only for old format of input data) This function reads interdependency data from file.

    Parameters
    ----------
    net_number : int
        ID of the depender layer.
    src_net_number : int
        ID of the dependee layer.
    base_dir : src
        The address of the folder where the basic network information (topology, parameters, etc.) are stored

    Returns
    -------
    arcs: list
        List of interdependencies as in arc format
    """
    # Variables in CSV format is broken into files:
    #     S<net_number>_adj : Adjacency matrix for <net_number>
    #     S<net_number>_bal : demand/supply of node.
    #     S<net_number>_cst : flow cost ?
    #     S<net_number>_dset : Set of distributors (but this can be calculated with *_bal)
    #     S<net_number>_gset : Set of generators (but this can be calculated with *_bal)
    #     S<net_number>_ub  : Upper bound on arc capacity.
    #     S<net_number>_lb  : Lower bound on arc capacity (typically 0).
    #     S<net_number>_imt : Interdependency matrix for network <net_number>. <=== This method will only load
    #     interdependencies.
    arcs = []
    with open(base_dir + "/csv/S" + str(net_number) + "_imt.csv") as f:
        rows = [[int(y) for y in string.split(x, ",")] for x in f.readlines()]
        for i in range(len(rows)):
            for j in range(len(rows[i])):
                if rows[i][j] > 0:
                    arc = InfrastructureInterdepArc(i + 1, j + 1, src_net_number, net_number, rows[i][j])
                    arcs.append(arc)
    return arcs

def load_infrastructure_array_format(base_dir, ext_interdependency_dir=None, magnitude=6, sim_number=1, cost_scale=1.0):
    """
    This function reads the infrastructure network from file in the old format

    Parameters
    ----------
    base_dir : str
        The address of the folder where the basic network information (topology, parameters, etc.) are stored
    ext_interdependency_dir : str
        The address of the file where the interdependency data are stored. The default in None.
    magnitude : int
        Magnitude of the initial damage scenario. The default is 6.
    sim_number : int
        The simulation number. The default is 1.
    cost_scale : float
        The factor by which all cost values has to multiplied. The default is 1.0.

    Returns
    -------
    G : networkx.DiGraph
        The networkx graph object that stores node, arc, and interdependency information

    """
    variables = ['v', 'c', 'f', 'q', 'Mp', 'Mm', 'g', 'b', 'u', 'h', 'p', 'gamma', 'alpha', 'beta', 'a', 'n']
    entry_regex = r"\(\s*(?P<tuple>(\d+\s*,\s*)*\d+)\s*\)\s*(?P<val>-?\d+)"
    recovery_entry_regex = r"\(\s*(?P<tuple>(\d+\s+)*\d+)\s*\)\s*(?P<val>\d+)"
    var_regex = r"(?P<varname>\D+):\s*\[(?P<entries>[^\]]*)\]"
    pattern = re.compile(var_regex, flags=re.DOTALL)
    files = [base_dir + "/INDP_InputData/MURI_INDP_data.txt"]
    if sim_number > 0:
        files.append(base_dir + "/Failure and recovery scenarios/recoveryM" + str(magnitude) + "v3.txt")
    vars = {}
    for file in files:
        with open(file) as f:
            # print "Opened",file,"."
            lines = f.read()
            vars_strings = {}
            for match in pattern.finditer(lines):
                vars_strings[string.strip(match.group('varname'))] = string.strip(match.group('entries'))
                # print "Varstrings=",vars_strings.keys()
            for v in vars_strings:
                entry_string = vars_strings[v]
                entry_pattern = re.compile(entry_regex, flags=re.DOTALL)
                is_match = False
                for match in entry_pattern.finditer(entry_string):
                    is_match = True
                    tuple_string = match.group('tuple')
                    val_string = match.group('val')
                    tuple_string = string.split(tuple_string, ",")
                    tuple_string = [int(string.strip(x)) for x in tuple_string]
                    val_string = float(string.strip(val_string))
                    # print v,":",tuple_string,"=>",val_string
                    if v not in vars:
                        vars[v] = []
                    vars[v].append((tuple_string, val_string))
                if not is_match:
                    recovery_pattern = re.compile(recovery_entry_regex, flags=re.DOTALL)
                    for match in recovery_pattern.finditer(entry_string):
                        is_match = True
                        tuple_string = match.group('tuple')
                        val_string = match.group('val')
                        tuple_string = string.split(tuple_string, " ")
                        tuple_string = [int(string.strip(x)) for x in tuple_string]
                        val_string = float(string.strip(val_string))
                        # print v,":",tuple_string,"=>",val_string
                        if v not in vars:
                            vars[v] = []
                        vars[v].append((tuple_string, val_string))
    G = InfrastructureNetwork("Test")
    global_index = 1
    for node in vars['n']:
        n = InfrastructureNode(global_index, node[0][1], node[0][0])
        # print "Adding node",node[0][0],"in layer",node[0][1],"."
        G.G.add_node((n.local_id, n.net_id), data={'inf_data': n})
        global_index += 1
    for arc in vars['a']:
        a = InfrastructureArc(arc[0][0], arc[0][1], arc[0][2])
        G.G.add_edge((a.source, a.layer), (a.dest, a.layer), data={'inf_data': a})
    if ext_interdependency_dir:
        print("Loading external interdependencies...")
        interdep_arcs = load_interdependencies(1, 3, base_dir=ext_interdependency_dir)
        for a in interdep_arcs:
            G.G.add_edge((a.source, a.source_layer), (a.dest, a.dest_layer), data={'inf_data': a})
        interdep_arcs = load_interdependencies(3, 1, base_dir=ext_interdependency_dir)
        for a in interdep_arcs:
            G.G.add_edge((a.source, a.source_layer), (a.dest, a.dest_layer), data={'inf_data': a})
    else:
        for arc in vars['gamma']:
            gamma = float(arc[1])
            if gamma > 0.0:
                a = InfrastructureInterdepArc(arc[0][0], arc[0][1], arc[0][2], arc[0][3], gamma)
                G.G.add_edge((a.source, a.source_layer), (a.dest, a.dest_layer), data={'inf_data': a})
    # Arc Attributes.
    for flow_cost in vars['c']:
        G.G[(flow_cost[0][0], flow_cost[0][2])][(flow_cost[0][1], flow_cost[0][2])]['data'][
            'inf_data'].flow_cost = float(flow_cost[1]) * cost_scale
    for rec_cost in vars['f']:
        G.G[(rec_cost[0][0], rec_cost[0][2])][(rec_cost[0][1], rec_cost[0][2])]['data'][
            'inf_data'].reconstruction_cost = float(rec_cost[1]) * cost_scale
    for cap in vars['u']:
        G.G[(cap[0][0], cap[0][2])][(cap[0][1], cap[0][2])]['data']['inf_data'].capacity = cap[1]
    for res in vars['h']:
        # Assume only one kind of resource for now.
        G.G[(res[0][0], res[0][2])][(res[0][1], res[0][2])]['data']['inf_data'].resource_usage = res[1]
    # Space attributes.
    for space in vars['g']:
        G.S.append(InfrastructureSpace(int(space[0][0]), float(space[1])))
    for space in vars['beta']:
        G.G[(space[0][0], space[0][2])][(space[0][1], space[0][2])]['data']['inf_data'].space = int(space[0][3])
    for space in vars['alpha']:
        G.G.node[(space[0][0], space[0][1])]['data']['inf_data'].space = int(space[0][2])
    # Node Attributes.
    for rec_cost in vars['q']:
        G.G.node[(rec_cost[0][0], rec_cost[0][1])]['data']['inf_data'].reconstruction_cost = float(
            rec_cost[1]) * cost_scale
    for sup in vars['Mp']:
        G.G.node[(sup[0][0], sup[0][1])]['data']['inf_data'].oversupply_penalty = float(sup[1]) * cost_scale
    for sup in vars['Mm']:
        G.G.node[(sup[0][0], sup[0][1])]['data']['inf_data'].undersupply_penalty = float(sup[1]) * cost_scale
    for res in vars['p']:
        # Assume only one kind of resource for now.
        G.G.node[(res[0][0], res[0][1])]['data']['inf_data'].resource_usage = res[1]
    for dem in vars['b']:
        G.G.node[(dem[0][0], dem[0][1])]['data']['inf_data'].demand = dem[1]

    # Load failure scenarios.
    if sim_number > 0:
        for func in vars['nresults']:
            if func[0][0] == sim_number:
                G.G.node[(func[0][1], func[0][2])]['data']['inf_data'].functionality = float(func[1])
                G.G.node[(func[0][1], func[0][2])]['data']['inf_data'].repaired = float(func[1])
                # if float(func[1]) == 0.0:
                #    print "Node (",`func[0][1]`+","+`func[0][2]`+") broken."
        for func in vars['aresults']:
            if func[0][0] == sim_number:
                G.G[(func[0][1], func[0][3])][(func[0][2], func[0][3])]['data']['inf_data'].functionality = float(
                    func[1])
                G.G[(func[0][1], func[0][3])][(func[0][2], func[0][3])]['data']['inf_data'].repaired = float(func[1])
                # if float(func[1]) == 0.0:
                #    print "Arc ((",`func[0][1]`+","+`func[0][3]`+"),("+`func[0][2]`+","+`func[0][3]`+")) broken."
    return G

def load_infrastructure_array_format_extended(base_dir, cost_scale=1.0, extra_commodity=None):
    """
    This function reads the infrastructure network from file in the extended format

    Parameters
    ----------
    base_dir : str
        The address of the folder where the basic network information (topology, parameters, etc.) are stored
    cost_scale : float
        The factor by which all cost values has to multiplied. The default is 1.0.
    extra_commodity :
        (only for extended format of input data) List of extra-commodities in the analysis. The default is None, which
        only considers a main commodity.

    Returns
    -------
    G : networkx.DiGraph
        The networkx graph object that stores node, arc, and interdependency information

    """
    files = [f for f in os.listdir(base_dir) if os.path.isfile(os.path.join(base_dir, f))]
    net_names = {'Water': 1, 'Gas': 2, 'Power': 3, 'Telecommunication': 4}  # !!!
    G = InfrastructureNetwork("Test")
    global_index = 0
    for file in files:
        fname = file[0:-4]
        if fname[-5:] == 'Nodes':
            with open(base_dir + file) as f:
                data = pd.read_csv(f, delimiter=',')
                net = net_names[fname[:-5]]
                for v in data.iterrows():
                    try:
                        node_id = v[1]['ID']
                    except KeyError:
                        node_id = v[1]['nodenwid']
                    n = InfrastructureNode(global_index, net, int(node_id))
                    G.G.add_node((n.local_id, n.net_id), data={'inf_data': n})
                    global_index += 1
                    node_main_data = G.G.nodes[(n.local_id, n.net_id)]['data']['inf_data']
                    node_main_data.reconstruction_cost = float(v[1]['q (complete DS)']) * cost_scale
                    node_main_data.oversupply_penalty = float(v[1]['Mp']) * cost_scale
                    node_main_data.undersupply_penalty = float(v[1]['Mm']) * cost_scale
                    node_main_data.demand = float(v[1]['Demand'])
                    if 'guid' in v[1].index.values:
                        node_main_data.guid = v[1]['guid']
                    resource_names = [x for x in list(v[1].index.values) if x[:2] == 'p_']
                    if len(resource_names) > 0:
                        n.set_resource_usage(resource_names)
                        for rc in resource_names:
                            n.resource_usage[rc] = v[1][rc]
                    else:
                        n.resource_usage['p_'] = 1
                    if extra_commodity:
                        n.set_extra_commodity(extra_commodity[net])
                        for l in extra_commodity[net]:
                            ext_com_data = G.G.nodes[(n.local_id, n.net_id)]['data']['inf_data'].extra_com[l]
                            ext_com_data['oversupply_penalty'] = float(v[1]['Mp_' + l]) * cost_scale
                            ext_com_data['undersupply_penalty'] = float(v[1]['Mm_' + l]) * cost_scale
                            ext_com_data['demand'] = float(v[1]['Demand_' + l])
    for file in files:
        fname = file[0:-4]
        if fname[-4:] == 'Arcs':
            with open(base_dir + file) as f:
                data = pd.read_csv(f, delimiter=',')
                net = net_names[fname[:-4]]
                for v in data.iterrows():
                    try:
                        start_id = v[1]['Start Node']
                        end_id = v[1]['End Node']
                    except KeyError:
                        start_id = v[1]['fromnode']
                        end_id = v[1]['tonode']
                    for duplicate in range(2):
                        if duplicate == 0:
                            a = InfrastructureArc(int(start_id), int(end_id), net)
                        elif duplicate == 1:
                            a = InfrastructureArc(int(end_id), int(start_id), net)
                        G.G.add_edge((a.source, a.layer), (a.dest, a.layer), data={'inf_data': a})
                        arc_main_data = G.G[(a.source, a.layer)][(a.dest, a.layer)]['data']['inf_data']
                        arc_main_data.flow_cost = float(v[1]['c']) * cost_scale
                        arc_main_data.reconstruction_cost = float(v[1]['f']) * cost_scale
                        arc_main_data.capacity = float(v[1]['u'])
                        if 'guid' in v[1].index.values:
                            arc_main_data.guid = v[1]['guid']
                        resource_names = [x for x in list(v[1].index.values) if x[:2] == 'h_']
                        if len(resource_names) > 0:
                            a.set_resource_usage(resource_names)
                            for rc in resource_names:
                                a.resource_usage[rc] = v[1][rc]
                        else:
                            a.resource_usage['h_'] = 1
                        if extra_commodity:
                            a.set_extra_commodity(extra_commodity[net])
                            for l in extra_commodity[net]:
                                ext_com_data = \
                                    G.G[(a.source, a.layer)][(a.dest, a.layer)]['data']['inf_data'].extra_com[l]
                                ext_com_data['flow_cost'] = float(v[1]['c_' + l]) * cost_scale
    for file in files:
        fname = file[0:-4]
        if fname in ['beta', 'alpha', 'g', 'Interdep']:
            with open(base_dir + file) as f:
                data = pd.read_csv(f, delimiter=',')
                for v in data.iterrows():
                    if fname == 'beta':
                        net = net_names[v[1]['Network']]
                        G.G[(int(v[1]['Start Node']), net)][(int(v[1]['End Node']), net)]['data'][
                            'inf_data'].space = int(int(v[1]['Subspace']))
                        G.G[(int(v[1]['End Node']), net)][(int(v[1]['Start Node']), net)]['data'][
                            'inf_data'].space = int(int(v[1]['Subspace']))
                    if fname == 'alpha':
                        net = net_names[v[1]['Network']]
                        G.G.node[(int(v[1]['ID']), net)]['data']['inf_data'].space = int(int(v[1]['Subspace']))
                    if fname == 'g':
                        G.S.append(InfrastructureSpace(int(v[1]['Subspace_ID']), float(v[1]['g'])))
                    if fname == 'Interdep' and v[1]['Type'] == 'Physical':
                        i = int(v[1]['Dependee Node'])
                        net_i = net_names[v[1]['Dependee Network']]
                        j = int(v[1]['Depender Node'])
                        net_j = net_names[v[1]['Depender Network']]
                        a = InfrastructureInterdepArc(i, j, net_i, net_j, gamma=1.0)
                        G.G.add_edge((a.source, a.source_layer), (a.dest, a.dest_layer), data={'inf_data': a})
                        if extra_commodity:
                            a.set_extra_commodity(extra_commodity[net_i])
                            a.set_extra_commodity(extra_commodity[net_j])
    return G

def add_failure_scenario(G, dam_dir, magnitude=6, v=3, sim_number=1):
    """
    This function reads initial damage data from file in the ANDRES format, and apply it to the infrastructure network

    Parameters
    ----------
    G : networkx.DiGraph
        The networkx graph object that stores node, arc, and interdependency information
    dam_dir : str
        The address of the folder where the initial damage data are stored
    magnitude : int
        Magnitude of the initial damage scenario. The default is 6.
    v : int
        Number of the resources in the analysis. The default is 3.
    sim_number : int
        The simulation number. The default is 1.

    Returns
    -------
    None.

    """
    print("Initialize Damage...")
    if sim_number == "INF":
        # Destroy all nodes!!!!
        for n, d in G.G.nodes(data=True):
            G.G.node[n]['data']['inf_data'].functionality = 0.0
            G.G.node[n]['data']['inf_data'].repaired = 0.0
        for u, v, a in G.G.edges(data=True):
            if not a['data']['inf_data'].is_interdep:
                G.G[u][v]['data']['inf_data'].functionality = 0.0
                G.G[u][v]['data']['inf_data'].repaired = 0.0
    elif sim_number > 0:
        variables = ['v', 'c', 'f', 'q', 'Mp', 'Mm', 'g', 'b', 'u', 'h', 'p', 'gamma', 'alpha', 'beta', 'a', 'n']
        entry_regex = r"\(\s*(?P<tuple>(\d+\s*,\s*)*\d)\s*\)\s*(?P<val>-?\d+)"
        recovery_entry_regex = r"\(\s*(?P<tuple>(\d+\s+)*\d)\s*\)\s*(?P<val>\d+)"
        var_regex = r"(?P<varname>\D+):\s*\[(?P<entries>[^\]]*)\]"
        pattern = re.compile(var_regex, flags=re.DOTALL)
        file = dam_dir + "/Failure and recovery scenarios/recoveryM" + str(magnitude) + "v3.txt"
        vars = {}
        with open(file) as f:
            # print "Opened",file,"."
            lines = f.read()
            vars_strings = {}
            for match in pattern.finditer(lines):
                vars_strings[string.strip(match.group('varname'))] = string.strip(match.group('entries'))
            # print "Varstrings=",vars_strings.keys()
            for v in vars_strings:
                entry_string = vars_strings[v]
                entry_pattern = re.compile(entry_regex, flags=re.DOTALL)
                is_match = False
                for match in entry_pattern.finditer(entry_string):
                    is_match = True
                    tuple_string = match.group('tuple')
                    val_string = match.group('val')
                    tuple_string = string.split(tuple_string, ",")
                    tuple_string = [int(string.strip(x)) for x in tuple_string]
                    val_string = float(string.strip(val_string))
                    # print v,":",tuple_string,"=>",val_string
                    if v not in vars:
                        vars[v] = []
                    vars[v].append((tuple_string, val_string))
                if not is_match:
                    recovery_pattern = re.compile(recovery_entry_regex, flags=re.DOTALL)
                    for match in recovery_pattern.finditer(entry_string):
                        is_match = True
                        tuple_string = match.group('tuple')
                        val_string = match.group('val')
                        tuple_string = string.split(tuple_string, " ")
                        tuple_string = [int(string.strip(x)) for x in tuple_string]
                        val_string = float(string.strip(val_string))
                        # print v,":",tuple_string,"=>",val_string
                        if v not in vars:
                            vars[v] = []
                        vars[v].append((tuple_string, val_string))
        # Load failure scenarios.
        for func in vars['nresults']:
            if func[0][0] == sim_number:
                G.G.node[(func[0][1], func[0][2])]['data']['inf_data'].functionality = float(func[1])
                G.G.node[(func[0][1], func[0][2])]['data']['inf_data'].repaired = float(func[1])
                # if float(func[1]) == 0.0:
                #    print "Node (",`func[0][1]`+","+`func[0][2]`+") broken."
        for func in vars['aresults']:
            if func[0][0] == sim_number:
                G.G[(func[0][1], func[0][3])][(func[0][2], func[0][3])]['data']['inf_data'].functionality = float(
                    func[1])
                G.G[(func[0][1], func[0][3])][(func[0][2], func[0][3])]['data']['inf_data'].repaired = float(func[1])
                # if float(func[1]) == 0.0:
                #    print "Arc ((",`func[0][1]`+","+`func[0][3]`+"),("+`func[0][2]`+","+`func[0][3]`+")) broken."

def add_from_csv_failure_scenario(G, sample, dam_dir=""):
    """
    This function reads initial damage data from file in the from_csv format, and apply it to the infrastructure
    network. This format only consider one magnitude value (0), and there can be as many as samples form that magnitude.

    Parameters
    ----------
    G : networkx.DiGraph
        The networkx graph object that stores node, arc, and interdependency information
    sample : int
        Sample number of the initial damage scenario,
    dam_dir : str
        The address of the folder where the initial damage data are stored

    Returns
    -------
    None.

    """
    # print("Initialize Random Damage...")
    with open(dam_dir + 'Initial_node.csv') as csv_file:
        data = csv.reader(csv_file, delimiter=',')
        for row in data:
            raw_n = row[0]
            raw_n = raw_n.split(',')
            n = (int(raw_n[0].strip(' )(')), int(raw_n[1].strip(' )(')))
            state = float(row[sample + 1])
            G.G.nodes[n]['data']['inf_data'].functionality = state
            G.G.nodes[n]['data']['inf_data'].repaired = state

    with open(dam_dir + 'Initial_link.csv') as csv_file:
        data = csv.reader(csv_file, delimiter=',')
        for row in data:
            raw_uv = row[0]
            raw_uv = raw_uv.split(',')
            u = (int(raw_uv[0].strip(' )(')), int(raw_uv[1].strip(' )(')))
            v = (int(raw_uv[2].strip(' )(')), int(raw_uv[3].strip(' )(')))
            state = float(row[sample + 1])
            if state == 0.0:
                G.G[u][v]['data']['inf_data'].functionality = state
                G.G[u][v]['data']['inf_data'].repaired = state

                G.G[v][u]['data']['inf_data'].functionality = state
                G.G[v][u]['data']['inf_data'].repaired = state

def add_wu_failure_scenario(G, dam_dir, no_set=0, no_sce=0):
    """
    This function reads initial damage data from file in the WU format, and apply it to the infrastructure network. The
    damage data for this dataset comes in a format similar to the hazard maps from Jason Wu :cite:`Wu2017`, which
    consist of N sets (= samples) of M damage scenarios (= magnitudes). For shelby county, for example, N=50 and M=96.

    Parameters
    ----------
    G : networkx.DiGraph
        The networkx graph object that stores node, arc, and interdependency information
    dam_dir : str
        The address of the folder where the initial damage data are stored
    no_set : int
        The number of the set of the initial damage scenario. The default is 0.
    no_sce : int
        The number of the scenario of the initial damage scenario The default is 0.

    Returns
    -------
    None.

    """
    print("Initialize Wu failure scenarios...")
    dam_nodes = {}
    dam_arcs = {}
    folder_dir = dam_dir + 'Set%d/Sce%d/' % (no_set, no_sce)
    net_names = {1: 'Water', 2: 'Gas', 3: 'Power', 4: 'Telecommunication'}
    offset = 0  # set to 1 if node IDs start from 1
    # Load failure scenarios.
    if os.path.exists(folder_dir):
        for k in range(1, len(net_names.keys()) + 1):
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                dam_nodes[k] = np.loadtxt(folder_dir + 'Net_%s_Damaged_Nodes.txt' \
                                          % net_names[k]).astype('int')
                dam_arcs[k] = np.loadtxt(folder_dir + 'Net_%s_Damaged_Arcs.txt' \
                                         % net_names[k]).astype('int')
        for k in range(1, len(net_names.keys()) + 1):
            if dam_nodes[k].size != 0:
                if dam_nodes[k].size == 1:
                    dam_nodes[k] = [dam_nodes[k]]
                for v in dam_nodes[k]:
                    G.G.nodes[(v + offset, k)]['data']['inf_data'].functionality = 0.0
                    G.G.nodes[(v + offset, k)]['data']['inf_data'].repaired = 0.0
                    # print("Node (",`v+offset`+","+`k`+") broken.")
            if dam_arcs[k].size != 0:
                if dam_arcs[k].size == 2:
                    dam_arcs[k] = [dam_arcs[k]]
                for a in dam_arcs[k]:
                    G.G[(a[0] + offset, k)][(a[1] + offset, k)]['data']['inf_data'].functionality = 0.0
                    G.G[(a[0] + offset, k)][(a[1] + offset, k)]['data']['inf_data'].repaired = 0.0
                    # print("Arc ((",`a[0]+offset`+","+`k`+"),("+`a[1]+offset`+","+`k`+")) broken.")

                    G.G[(a[1] + offset, k)][(a[0] + offset, k)]['data']['inf_data'].functionality = 0.0
                    G.G[(a[1] + offset, k)][(a[0] + offset, k)]['data']['inf_data'].repaired = 0.0
                    # print("Arc ((",`a[1]+offset`+","+`k`+"),("+`a[0]+offset`+","+`k`+")) broken.")
    else:
        pass  # Un-damaging scenarios are not presented with any file or folder in the datasets

def load_synthetic_network(base_dir, topology='Random', config=6, sample=0, cost_scale=1.0):
    """
    This function reads a synthetic network from file

    Parameters
    ----------
    base_dir : str
        The address of the folder where the basic network information (topology, parameters, etc.) are stored
    topology : str
        Topology of the layers of the synthetic network. The default in 'Random'.
    config : int
        Configuration number of the initial damage scenario. The default is 6.
    sample : int
        Sample number of the initial damage scenario. The default is 0.
    cost_scale : float
        The factor by which all cost values has to multiplied. The default is 1.0.

    Returns
    -------
    G : networkx.DiGraph
        The networkx graph object that stores node, arc, and interdependency information
    no_resource : int
        Number of resources available for restoration
    : list
        List of layers of the synthetic network
    """
    print("Initialize Damage...")
    net_dir = base_dir + '/' + topology + 'Networks/'
    topo_initial = {'Random': 'RN', 'ScaleFree': 'SFN', 'Grid': 'GN', 'General': 'GEN'}
    with open(net_dir + 'List_of_Configurations.txt') as f:
        config_data = pd.read_csv(f, delimiter='\t')
        config_data = config_data.rename(columns=lambda x: x.strip())
    config_param = config_data.iloc[config]
    no_layers = int(config_param.loc['No. Layers'])
    no_resource = int(config_param.loc['Resource Cap'])

    file_dir = net_dir + topo_initial[topology] + 'Config_' + str(config) + '/Sample_' + str(sample) + '/'
    G = InfrastructureNetwork("Test")
    global_index = 0
    files = [f for f in os.listdir(file_dir) if os.path.isfile(os.path.join(file_dir, f))]
    for k in range(1, no_layers + 1):
        for file in files:
            if file == 'N' + str(k) + '_Nodes.txt':
                with open(file_dir + file) as f:
                    # print "Opened",file,"."
                    data = pd.read_csv(f, delimiter='\t', header=None)
                    for v in data.iterrows():
                        n = InfrastructureNode(global_index, k, int(v[1][0]))
                        # print "Adding node",node[0][0],"in layer",node[0][1],"."
                        G.G.add_node((n.local_id, n.net_id), data={'inf_data': n})
                        global_index += 1
                        G.G.nodes[(n.local_id, n.net_id)]['data']['inf_data'].reconstruction_cost = float(
                            v[1][7]) * cost_scale
                        G.G.nodes[(n.local_id, n.net_id)]['data']['inf_data'].oversupply_penalty = float(
                            v[1][5]) * cost_scale
                        G.G.nodes[(n.local_id, n.net_id)]['data']['inf_data'].undersupply_penalty = float(
                            v[1][6]) * cost_scale
                        # !!! Assume only one kind of resource for now and one resource for each repaired element.
                        G.G.nodes[(n.local_id, n.net_id)]['data']['inf_data'].resource_usage['p_'] = 1
                        G.G.nodes[(n.local_id, n.net_id)]['data']['inf_data'].demand = float(v[1][4])
        for file in files:
            if file == 'N' + str(k) + '_Arcs.txt':
                with open(file_dir + file) as f:
                    # print "Opened",file,"."
                    data = pd.read_csv(f, delimiter='\t', header=None)
                    for v in data.iterrows():
                        for duplicate in range(2):
                            if duplicate == 0:
                                a = InfrastructureArc(int(v[1][0]), int(v[1][1]), k)
                            elif duplicate == 1:
                                a = InfrastructureArc(int(v[1][1]), int(v[1][0]), k)
                            G.G.add_edge((a.source, a.layer), (a.dest, a.layer), data={'inf_data': a})
                            G.G[(a.source, a.layer)][(a.dest, a.layer)]['data']['inf_data'].flow_cost = float(
                                v[1][2]) * cost_scale
                            G.G[(a.source, a.layer)][(a.dest, a.layer)]['data']['inf_data'].reconstruction_cost = float(
                                v[1][4]) * cost_scale
                            G.G[(a.source, a.layer)][(a.dest, a.layer)]['data']['inf_data'].capacity = float(v[1][2])
                            # !!! Assume only one kind of resource for now and one resource for each repaired element.
                            G.G[(a.source, a.layer)][(a.dest, a.layer)]['data']['inf_data'].resource_usage['h_'] = 1
    for k in range(1, no_layers + 1):
        for kt in range(1, no_layers + 1):
            if k != kt:
                for file in files:
                    if file == 'Interdependent_Arcs_' + str(k) + '_' + str(kt) + '.txt':
                        with open(file_dir + file) as f:
                            #                print "Opened",file,"."
                            try:
                                data = pd.read_csv(f, delimiter='\t', header=None)
                                for v in data.iterrows():
                                    i = int(v[1][0])
                                    net_i = k
                                    j = int(v[1][1])
                                    net_j = kt
                                    a = InfrastructureInterdepArc(i, j, net_i, net_j, gamma=1.0)
                                    G.G.add_edge((a.source, a.source_layer), (a.dest, a.dest_layer),
                                                 data={'inf_data': a})
                                    # import random
                                    # if net_i == 1 and random.uniform(0, 1) > 0.5:
                                    #     pass
                                    # else:
                                    #     a = InfrastructureInterdepArc(i, j, net_i, net_j, gamma=1.0)
                                    #     G.G.add_edge((a.source, a.source_layer), (a.dest, a.dest_layer),
                                    #                  data={'inf_data': a})
                            except:
                                print('Empty file: ' + file)
    # add subspace data #.. todo: Add geographical spaces to synthetic networks
    # for v in data.iterrows():
    #    if fname=='beta':
    #        net = netNames[v[1]['Network']]
    #        G.G[(int(v[1]['Start Node']),net)][(int(v[1]['End Node']),net)]['data']['inf_data'].space=int(int(v[1]['Subspace']))
    #        G.G[(int(v[1]['End Node']),net)][(int(v[1]['Start Node']),net)]['data']['inf_data'].space=int(int(v[1]['Subspace']))
    #    if fname=='alpha':
    #        net = netNames[v[1]['Network']]
    #        G.G.node[(int(v[1]['ID']),net)]['data']['inf_data'].space=int(int(v[1]['Subspace']))
    #    if fname=='g':
    #        G.S.append(InfrastructureSpace(int(v[1]['Subspace_ID']),float(v[1]['g'])))

    return G, no_resource, range(1, no_layers + 1)

def add_synthetic_failure_scenario(G, dam_dir, topology='Random', config=0, sample=0):
    """
    This function reads initial damage data from file for a synthetic network.

    Parameters
    ----------
    G : networkx.DiGraph
        The networkx graph object that stores node, arc, and interdependency information.
    dam_dir : str
        The address of the folder where the initial damage data are stored.
    topology : str
        Topology of the layers of the synthetic network. The default in 'Random'.
    config : int
        Configuration number of the initial damage scenario. The default is 6.
    sample : int
        Sample number of the initial damage scenario. The default is 0.

    Returns
    -------
    None.

    """
    net_dir = dam_dir + '/' + topology + 'Networks/'
    topo_initial = {'Random': 'RN', 'ScaleFree': 'SFN', 'Grid': 'GN', 'General': 'GEN'}
    with open(net_dir + 'List_of_Configurations.txt') as f:
        config_data = pd.read_csv(f, delimiter='\t')
        config_data = config_data.rename(columns=lambda x: x.strip())
    config_param = config_data.iloc[config]
    no_layers = int(config_param.loc['No. Layers'])
    # noResource = int(config_param.loc['Resource Cap'])
    file_dir = net_dir + topo_initial[topology] + 'Config_' + str(config) + '/Sample_' + str(sample) + '/'
    files = [f for f in os.listdir(file_dir) if os.path.isfile(os.path.join(file_dir, f))]
    for k in range(1, no_layers + 1):
        for file in files:
            if file == 'N' + str(k) + '_Damaged_Nodes.txt':
                with open(file_dir + file) as f:
                    try:
                        data = pd.read_csv(f, delimiter='\t', header=None)
                        for v in data.iterrows():
                            G.G.nodes[(v[1][0], k)]['data']['inf_data'].functionality = 0.0
                            G.G.nodes[(v[1][0], k)]['data']['inf_data'].repaired = 0.0
                            # print("Node (",`int(v[1][0])`+","+`k`+") broken.")
                    except:
                        print('Empty file: ' + file)

        for file in files:
            if file == 'N' + str(k) + '_Damaged_Arcs.txt':
                with open(file_dir + file) as f:
                    try:
                        data = pd.read_csv(f, delimiter='\t', header=None)
                        for a in data.iterrows():
                            G.G[(a[1][0], k)][(a[1][1], k)]['data']['inf_data'].functionality = 0.0
                            G.G[(a[1][0], k)][(a[1][1], k)]['data']['inf_data'].repaired = 0.0
                            # print "Arc ((",`int(a[1][0])`+","+`k`+"),("+`int(a[1][1])`+","+`k`+")) broken."

                            G.G[(a[1][1], k)][(a[1][0], k)]['data']['inf_data'].functionality = 0.0
                            G.G[(a[1][1], k)][(a[1][0], k)]['data']['inf_data'].repaired = 0.0
                            # print "Arc ((",`int(a[1][1])`+","+`k`+"),("+`int(a[1][0])`+","+`k`+")) broken."
                    except:
                        print('Empty file: ' + file)

reason: import numpy,import networkx


text:
from mltoolkit.mldp.steps.transformers import BaseTransformer
from mltoolkit.mlutils.helpers.general import argsort
import numpy as np

class ChunkSorter(BaseTransformer):
    """Sorts data-chunks by one field."""

    def __init__(self, field_name, order='descending', fields_to_sort=None,
                 **kwargs):
        """
        :param field_name: name of the target/key field, based on which other
                           fields will be sorted.
        :param order: the order of sorting.
        :param fields_to_sort: if a list of field names is provided, will only
                               sort those specific fields. All otherwise.
        """
        if order not in ['ascending', 'descending']:
            raise ValueError("Please provide a valid 'order' param"
                             " ('ascending' or 'descending').")
        if fields_to_sort is not None and not isinstance(fields_to_sort, list):
            raise TypeError("Please provide a 'list' of field names to be "
                            "sorted based on the target field. Otherwise set "
                            "'fields_to_sort' to 'None'.")
        super(ChunkSorter, self).__init__(**kwargs)
        self.field_name = field_name
        self.order = order
        self.fields_to_sort = fields_to_sort
        if isinstance(self.fields_to_sort, list):
            self.fields_to_sort.append(self.field_name)

    def _transform(self, data_chunk):
        indxs = argsort(data_chunk[self.field_name], order=self.order)
        field_names = self.fields_to_sort if self.fields_to_sort else data_chunk.keys()
        for field_name in field_names:
            fv = data_chunk[field_name]
            if isinstance(fv, np.ndarray):
                fv = fv[indxs]
            elif isinstance(fv, list):
                fv = [fv[indx] for indx in indxs]
            else:
                raise TypeError("All field values must be of 'list' or 'array'"
                                " type.")
            data_chunk[field_name] = fv
        return data_chunk

ext: py

reason: import numpy


text:
#!/usr/bin/env python
# coding: utf-8

# In[28]:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

def splitPlusMinus(x, index=0):
    N = len(x)
    vector = np.asarray(np.zeros(N))
    error = np.asarray(np.zeros(N))
    for i in range(index, N+index):
        value = x[i].split(u'\xb1')
        vector[i-index] = (float(value[0]))
        error[i-index] = (float(value[1]))
        #print float(x[i].split(u'\xb1')[0])
    return vector, error

df = pd.read_excel (r'C:\Users\observer\Desktop\Ensaios_e_Caracterizacoes\Planilhas\GAPs_sem_salvar_imagem.xlsx')
columns = pd.DataFrame(df)
#print columns

GAP    = {}
GAP['value'] = [1, 10, 20, 40, 80]
GAP['GAP'] = [columns['#1'][19], columns['#10'][19], columns['#20'][19], columns['#40'][19], columns['#80'][19]]
GAP['error'] = [columns['#1'][20], columns['#10'][20], columns['#20'][20], columns['#40'][20], columns['#80'][20]]

fontsize = 15
plt.errorbar(GAP['value'], GAP['GAP'], GAP['error'], marker='o', c='blue',linewidth=1.0)
plt.xlabel(r'$\mathtt{N \acute{u} mero \quad de \quad imagens}$', size=fontsize)
plt.ylabel(r'$\mathtt{GAP \quad (s)}$', size=fontsize)
plt.title(r'$\mathtt{Gr \acute{a}fico \quad do \quad GAP \quad entre \quad cubos \quad de \quad imagens \quad sem \quad salvar}$'+'\n', size=fontsize)
#plt.ylim(1.8, 2.3)
plt.xlim(-5, 85)
plt.show()


text:
from data_acquirers import SimulatedDataAcquirer
import unittest
import numpy as np

class Test(unittest.TestCase):
    def test_simulate_signals(self, repeat=1):
        t, sigA, sigB = SimulatedDataAcquirer().acquire()
        self.assertIs(type(sigA), np.ndarray)
        self.assertIs(type(sigB), np.ndarray)
        self.assertIs(type(t), np.ndarray)
        self.assertEqual(len(sigA), len(t))
        self.assertEqual(len(sigB), len(t))

if __name__ == '__main__':
    unittest.main()

max_stars_repo_name: malgorzatakim/polarimeter

max_issues_repo_name: malgorzatakim/polarimeter

max_forks_repo_name: malgorzatakim/polarimeter


text:
"""
this pre processor selects one every "skip_count" images in the sample
for example if the sample has 100 images, out put will have 20 for skip_count =5

test samples will have only one image chosen at random
"""
from src.preprocessing.preprocessor import preprocessor
import numpy as np
import cv2
from numpy import array, zeros
import os
from glob import  glob
import cv2
import json

class EveryOther ( preprocessor ):

    def __init__(self, exportPath, trainingPath , testPath , images_size=[640,640], importPath = None , skip_count =5):
        self.exportPath = exportPath
        self.trainingPath = trainingPath
        self.testPath = testPath
        self.image_size = images_size
        self.importPath = importPath
        self.skip_count = skip_count
        self.name = "EveryOther_" + str(skip_count)
        self.x_test = None
        self.y_train = None
        self.x_train = None

    def preprocess(self):
        """
        this funciton preopricess the imagaes into three arays, test_x tarin_x , train_y
        :return:
        """
        train_x = [] # None #np.array([])
        train_y = []  # None # np.array([])

        # create the trainig set
        if( not  self.trainingPath is None):
            for sample in sorted(os.listdir(self.trainingPath)) :

                mask_path = os.path.join( self.trainingPath, sample + '/mask.png')

                # load train_y
                y = self.change_size(cv2.imread( mask_path, 0))
                y= np.expand_dims( y, axis=0 )
                y=( y==2 ).astype(int)

                # take under account the skip count and lod the images
                t = [  self.change_size(cv2.imread(os.path.join(self.trainingPath, "%s/frame%04d.png" % (sample, i)),0))  for i in range(0, 99, self.skip_count) ]
                t = [ np.expand_dims(x, axis=0)  for x in t ]
                train_x.extend(t)
                for i in range( len(t)):
                    train_y.append(y)

        # create the test set
        # test_x = []
        test_dic = {}
        test_size_ref = {}
        if not self.testPath is None:
            for sample in sorted(os.listdir(self.testPath)):
                # image = cv2.imread(os.path.join(self.testPath, "%s/frame0050.png" % sample),0) #/ 255
                # test_size_ref[sample]= image.shape
                # image = self.change_size(image)
                # image = image.reshape(image.shape + (1,))
                # test_dic[sample] = np.expand_dims(image, axis=0)
                print (os.path.join(self.testPath, "%s/frame%04d.png" % (sample, i)))
                if  '.DS_Store' in sample : continue

                t = [cv2.imread(os.path.join(self.testPath, "%s/frame%04d.png" % (sample, i)), 0)
                     for i in range(0, 99, 3)]

                test_size_ref[sample] = t[0].shape

                t = [ self.change_size(x) for x in  t ]

                t = [np.expand_dims(x.reshape(x.shape + (1,)), axis=0)  for x in t]

                test_dic[sample] = np.vstack(t)

                # test_x.append(np.expand_dims(image, axis=0))

        train_x = np.vstack(train_x)
        train_y = np.vstack(train_y)
        # test_x = np.vstack(test_x)

        train_x = train_x.reshape(train_x.shape + (1,))
        train_y = train_y.reshape(train_y.shape + (1,))
        #test_x = test_x.reshape(test_x.shape + (1,))

        print(train_x.shape)
        print(train_y.shape)
        # print(test_x.shape)

        self.x_train = train_x
        # self.x_test = test_x
        self.y_train = train_y

        # if( not self.exportPath is None):
        #     self.save_to_file()

        return train_x , train_y , test_dic, test_size_ref

max_stars_repo_name: hitennirmal/goucher

reason: import numpy,from numpy


text:
#! /usr/bin/env python
#coding=utf-8
import numpy as np
import sys,os
# 设置当前的工作环境在caffe下
caffe_root = '/home/dandan/DL/caffe/'
# 我们也把caffe/python也添加到当前环境
sys.path.insert(0, caffe_root + 'python')
import caffe
os.chdir(caffe_root)#更换工作目录

# 设置网络结构
#net_file=caffe_root + 'models/bvlc_reference_caffenet/deploy.prototxt'
net_file=caffe_root + 'models/vgg/VGG_ILSVRC_16_layers_deploy.prototxt'
# 添加训练之后的参数
#caffe_model=caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'
caffe_model=caffe_root + 'models/vgg/vgg_sexy_train_iter_15000.caffemodel'
# 均值文件
mean_file=caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy'

# 这里对任何一个程序都是通用的，就是处理图片
# 把上面添加的两个变量都作为参数构造一个Net
net = caffe.Net(net_file,caffe_model,caffe.TEST)
# 得到data的形状，这里的图片是默认matplotlib底层加载的
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
# matplotlib加载的image是像素[0-1],图片的数据格式[weight,high,channels]，RGB
# caffe加载的图片需要的是[0-255]像素，数据格式[channels,weight,high],BGR，那么就需要转换

# channel 放到前面
transformer.set_transpose('data', (2,0,1))

#transformer.set_mean('data', np.load(mean_file).mean(1).mean(1))
transformer.set_mean('data', np.array([104, 117, 123]))
# 图片像素放大到[0-255]
transformer.set_raw_scale('data', 255)
# RGB-->BGR 转换
transformer.set_channel_swap('data', (2,1,0))

# 这里才是加载图片
im=caffe.io.load_image(caffe_root+'examples/images/5.jpg')
# 用上面的transformer.preprocess来处理刚刚加载图片
net.blobs['data'].data[...] = transformer.preprocess('data',im)

for layer_name, blob in net.blobs.iteritems():
    print layer_name + '\t' + str(blob.data.shape)

#注意,网络开始向前传播啦
out = net.forward()

print net.blobs['prob'].data[0]

result=net.blobs['prob'].data[0].flatten()
print ("sexy:%.2f"%result[0])
print ("norm:%.2f"%result[1])

#imagenet_labels_filename = caffe_root + 'data/ilsvrc12/synset_words.txt'
#labels = np.loadtxt(imagenet_labels_filename, str, delimiter='\t')
#top_k = net.blobs['prob'].data[0].flatten().argsort()[-1:-2:-1]
#for i in np.arange(top_k.size):
#    print top_k[i] , net.blobs['prob'].data[0][top_k[i]]

max_stars_repo_name: shenlinqijing/predl

max_issues_repo_name: shenlinqijing/predl

max_forks_repo_name: shenlinqijing/predl


text:
r"""
Defines ``Bank``, a class-based interface for managing template banks.
"""
import os
from typing import Callable, Optional, Set, Tuple, Union

import jax
import jax.numpy as jnp
from jax import random

from .metric import get_density, get_g, get_gam
from .utils import (
    Array,
    PRNGKeyArray,
    est_ratio_max,
    gen_bank_random,
    gen_bank_stochastic,
    gen_template_rejection,
    gen_templates_rejection,
    get_bank_effectualness,
    get_eff_pads,
    get_match,
    get_top_matches_templates,
)

class Bank:
    r"""
    Class representing a template bank.
    """

    computed_vars: Set[str] = set(
        [
            "ratio_max",
            "n_templates",
            "templates",
            "effectualness_points",
            "effectualnesses",
            "eta_est",
            "eta_est_err",
            "_eff_pad_low",
            "_eff_pad_high",
            "_dim",
        ]
    )
    """
    The names of attributes that are computed in the course of working with a
    bank. For example, this includes ``"templates"``.
    """
    provided_vars: Set[str] = set(
        [
            "fs",
            "m_star",
            "eta",
            "name",
        ]
    )
    """
    The names of attributes containing data the user provides when initialing a
    bank
    """

    def __init__(
        self,
        amp: Callable[[Array, Array], Array],
        Psi: Callable[[Array, Array], Array],
        fs: Array,
        Sn: Callable[[Array], Array],
        m_star: Union[float, Array],
        eta: Union[float, Array],
        sample_base: Callable[[PRNGKeyArray, int], Array],
        density_fun_base: Callable[[Array], Array] = lambda _: jnp.array(1.0),
        name: str = "test",
    ):
        r"""
        Initializes a :class:`Bank` instance.

        Args:
            amp: function returning the amplitude of the Fourier-domain waveform
                as a function of frequency and parameters
            Psi: function returning the phase of the Fourier-domain waveform as
                a function of frequency and parameters
            fs: uniformly-spaced grid of frequencies used to compute the
                noise-weighted inner product or match
            Sn: power spectral density of the detector noise
            m_star: the target maximum mismatch for the bank (equal to
                ``1 - minimum_match``)
            eta: target fraction of parameter space to cover with the bank
            sample_base: ``jit``-able base sampler for rejection sampling from
                the metric density. Takes a key and number of samples as
                arguments.
            density_fun_base: ``jit``-able density function for ``sample_base``.
                Need not be normalized.
            name: an identifier for the bank.

        Raises:
            ValueError: if ``len(fs) <= 2`` or ``m_star`` or ``eta`` are outside
                the range (0, 1)
        """
        # Validation
        if len(fs) <= 2:
            # Required for padding to work
            raise ValueError("length of frequency array must be at least three")
        if m_star > 1 or m_star < 0:
            raise ValueError("m_star must be in (0, 1)")
        if eta > 1 or eta < 0:
            raise ValueError("eta must be in (0, 1)")

        self.amp = amp
        self.Psi = Psi
        self.fs = fs
        self.Sn = Sn
        self.m_star = m_star
        self.eta = eta
        self.sample_base = sample_base
        self.density_fun_base = density_fun_base
        self.name = name

        self.ratio_max: Optional[Array] = None
        self.n_templates: Optional[int] = None
        self.templates: Optional[Array] = None
        self.effectualness_points: Optional[Array] = None
        self.effectualnesses: Optional[Array] = None
        self.eta_est: Optional[Array] = None
        self.eta_est_err: Optional[Array] = None

        # Padding for accurate effectualness calculation
        # Length of padded array
        self._eff_pad_low, self._eff_pad_high = get_eff_pads(self.fs)

        # Key doesn't matter
        self._dim = self.sample_base(random.PRNGKey(1), 1).shape[-1]

    def __str__(self):
        r"""Gets a string representation of the bank."""
        return f"Bank(m_star={float(self.m_star)}, eta={float(self.eta)}, dim={self.dim}, name='{self.name}')"

    def __repr__(self):
        return str(self)  # for now

    @property
    def dim(self) -> int:
        r"""
        The dimensionality of the bank's parameter space.
        """
        return self._dim

    def match_fun(
        self,
        theta1: Array,
        theta2: Array,
        amp2: Optional[Callable[[Array, Array], Array]] = None,
        Psi2: Optional[Callable[[Array, Array], Array]] = None,
    ) -> Array:
        r"""
        Gets the match between a point from the bank's waveform model and a different
        point, potentially with a distinct waveform model. See :meth:`diffbank.utils.get_match`.

        Args:
            theta1: the point from the same waveform model as the bank
            theta2: the other point
            amp2: amplitude function for the ``theta2``. If not provided, it is
                assumed ``theta2`` has the same waveform model as the bank.
            Psi2: phase function for the ``theta2``. If not provided, it is
                assumed ``theta2`` has the same waveform model as the bank.

        Returns:
            The match between the waveforms for the two points.
        """
        return get_match(
            theta1,
            theta2,
            self.amp,
            self.Psi,
            self.amp if amp2 is None else amp2,
            self.Psi if Psi2 is None else Psi2,
            self.fs,
            self.Sn,
            self._eff_pad_low,
            self._eff_pad_high,
        )

    def density_fun(self, theta: Array) -> Array:
        """
        Computes the determinant of the metric over the intrinsic of the binary,
        maximized over the time and phase at coalescence.
        See :meth:`diffbank.metric.get_density`.
        """
        return get_density(theta, self.amp, self.Psi, self.fs, self.Sn)

    def g_fun(self, theta) -> Array:
        """
        Computes the metric for the intrinsic binary parameters of the binary,
        maximized over the time and phase at coalescence. See
        :meth:`diffbank.metric.get_density`.
        """
        return get_g(theta, self.amp, self.Psi, self.fs, self.Sn)

    def gam_fun(self, theta) -> Array:
        """
        Computes the metric for the time of coalescence and intrinsic parameters
        of the binary. See :meth:`diffbank.metric.get_gam`.
        """
        return get_gam(theta, self.amp, self.Psi, self.fs, self.Sn)

    def est_ratio_max(
        self,
        key: PRNGKeyArray,
        n_iter: int = 1000,
        n_init: int = 200,
        show_progress: bool = True,
    ) -> Tuple[Array, Array]:
        r"""
        Estimate maximum of the ratio of target to base density using empirical
        supremum rejection sampling. See :meth:`diffbank.utils.est_ratio_max`.
        """
        return est_ratio_max(
            key,
            self.density_fun,
            self.sample_base,
            self.density_fun_base,
            n_iter,
            n_init,
            show_progress,
        )

    def gen_template_rejection(self, key: PRNGKeyArray) -> Array:
        r"""
        Generates a single template using rejection sampling. See
        :meth:`diffbank.utils.gen_template_rejection`.

        Raises:
            RuntimeError: if ``ratio_max`` is not set
        """
        if self.ratio_max is None:
            raise RuntimeError(
                "must set bank's 'ratio_max' attribute to an estimate of the"
                " maximum value of sqrt(|g|)"
            )
        return gen_template_rejection(
            key,
            self.ratio_max,
            self.density_fun,
            self.sample_base,
            self.density_fun_base,
        )

    def gen_templates_rejection(self, key: PRNGKeyArray, n_templates) -> Array:
        r"""
        Generates multiple templates using rejection sampling. See
        :meth:`diffbank.utils.gen_template_rejection`.

        Raises:
            RuntimeError: if ``ratio_max`` is not set
        """
        if self.ratio_max is None:
            raise RuntimeError(
                "must set bank's 'ratio_max' attribute to an estimate of the"
                " maximum value of sqrt(|g|)"
            )
        return gen_templates_rejection(
            key, n_templates, self.ratio_max, self.density_fun, self.sample_base
        )

    def fill_bank(
        self,
        key: PRNGKeyArray,
        method="random",
        n_eff: int = 1000,
        show_progress: bool = True,
    ):
        """
        Fills the bank with the required number of templates using the random or
        stochastic sampling methods from Coogan et al 2022. Updates the
        ``templates`` and ``n_templates`` attributes. See
        :meth:`diffbank.utils.gen_bank_random` and
        :meth:`diffbank.utils.gen_bank_stochastic`.

        Raises:
            RuntimeError: if ``ratio_max`` is not set
        """
        if self.ratio_max is None:
            raise RuntimeError(
                "must set bank's 'ratio_max' attribute to an estimate of the"
                " maximum value of sqrt(|g|)"
            )

        if method == "random":
            self.templates, _ = gen_bank_random(
                key,
                1 - self.m_star,
                self.eta,
                self.match_fun,
                self.ratio_max,
                self.density_fun,
                self.sample_base,
                self.density_fun_base,
                n_eff=n_eff,
                show_progress=show_progress,
            )
            self.n_templates = len(self.templates)
        elif method == "stochastic":
            propose_template = jax.jit(self.gen_template_rejection)
            self.templates, _ = gen_bank_stochastic(
                key,
                1 - self.m_star,
                self.eta,
                self.match_fun,
                propose_template,
                propose_template,
                n_eff=n_eff,
                show_progress=show_progress,
            )
            self.n_templates = len(self.templates)

    def get_top_matches_templates(
        self,
        theta2s: Array,
        amp2: Optional[Callable[[Array, Array], Array]] = None,
        Psi2: Optional[Callable[[Array, Array], Array]] = None,
        n_top: int = 1,
        show_progress: bool = True,
    ) -> Tuple[Array, Array]:
        """
        Finds best-matching templates for a set of points for a waveform model distinct
        from the bank's. See :meth:`diffbank.utils.get_top_matches_templates`.

        Raises:
            RuntimeError: if the bank does not contain templates
        """
        if self.templates is None:
            raise RuntimeError("cannot calculate effectualness of an empty bank")

        match_fun = lambda template, point: self.match_fun(template, point, amp2, Psi2)
        return get_top_matches_templates(
            self.templates, match_fun, theta2s, n_top, show_progress
        )

    def calc_bank_effectualness(
        self, key: PRNGKeyArray, n: int, show_progress: bool = True
    ):
        """
        Computes effectualness of bank at points sampled from the metric
        density. See :meth:`diffbank.utils.get_bank_effectualness`.

        Raises:
            RuntimeError: if the bank does not contain templates
        """
        if self.templates is None:
            raise RuntimeError("cannot calculate effectualness of an empty bank")

        sample_eff_pt = jax.jit(self.gen_template_rejection)
        (
            self.effectualnesses,
            self.effectualness_points,
            self.eta_est,
            self.eta_est_err,
        ) = get_bank_effectualness(
            key,
            1 - self.m_star,
            self.templates,
            self.match_fun,
            sample_eff_pt,
            n,
            show_progress,
        )

    def save(self, path: str = ""):
        """
        Saves template bank non-function attributes to a ``npz`` file. The
        file name is the bank's name plus the ``.npz`` extension.

        Args:
            path: directory in which to save bank
        """
        d = {k: getattr(self, k) for k in self.provided_vars | self.computed_vars}
        jnp.savez(os.path.join(path, f"{self.name}.npz"), bank=d)

    @classmethod
    def load(
        cls,
        path: str,
        amp: Callable[[Array, Array], Array],
        Psi: Callable[[Array, Array], Array],
        Sn: Callable[[Array], Array],
        sample_base: Callable[[PRNGKeyArray, int], Array],
        ignore_key_errors: bool = False,
    ):
        """
        Loads template bank's non-function attributes from a ``npz`` file. The
        other required attributes must be provided as arguments to this
        function.

        Args:
            path: path to ``npz`` file generated with ``Bank.save``
            amp: function returning the amplitude of the Fourier-domain waveform
                as a function of frequency and parameters
            Psi: function returning the phase of the Fourier-domain waveform as
                a function of frequency and parameters
            Sn: power spectral density of the detector noise
            sample_base: ``jit``-able base sampler for rejection sampling from
                the metric density. Takes a key and number of samples as
                arguments.
            ignore_key_errors: if ``True``, will try to load the bank even if
                there are extra or missing keys in the ``npz`` file.

        Raises:
            ValueError: if the bank being loaded is missing or contains extra keys,
                and ``ignore_key_errors`` is ``False``. This should only happen
                when trying to load old banks after ``diffbank`` goes through major
                updates.

        Returns:
            A new ``Bank``
        """
        d = jnp.load(path, allow_pickle=True)["bank"].item()
        if d.keys() != cls.provided_vars | cls.computed_vars and not ignore_key_errors:
            raise ValueError("missing or extra keys in bank file")

        # Instantiate with provided variables and functions
        fn_kwargs = {
            "amp": amp,
            "Psi": Psi,
            "Sn": Sn,
            "sample_base": sample_base,
        }

        try:
            bank = cls(**{**fn_kwargs, **{name: d[name] for name in cls.provided_vars}})

            # Set computed variables
            for name in cls.computed_vars:
                setattr(bank, name, d[name])
        except KeyError as e:
            if not ignore_key_errors:
                raise e

        return bank  # type: ignore

max_stars_repo_name: adam-coogan/diffbank

max_issues_repo_name: adam-coogan/diffbank

max_forks_repo_name: adam-coogan/diffbank

reason: import jax,from jax


text:
from manim import *
from numpy import array, array_equal

# Helper Function for Title
def Title(scene, t0, t1, wait=5, scale=0.3, move=(3, 6)):
    # Title Scene
    t0 = Text(t0)
    t1 = Text(t1)
    tg = VGroup(t0, t1).arrange(direction=DOWN)
    scene.add(tg)
    scene.wait(wait)
    scene.play(tg.animate.scale(scale).move_to(move[0] * DOWN + move[1] * RIGHT))
    return tg

# Helper function for multi-line explanation
def Explanation(scene, text, font='', wait=3, scale=0.7, fade=True, aligned_edge=ORIGIN):
    # Explanation

    el = [MarkupText(x, font=font) for x in text]
    eg = VGroup(*el).scale(scale).arrange(DOWN, aligned_edge=aligned_edge)

    for _el in el:
        scene.play(AddTextLetterByLetter(_el, time_per_letter=1))
    if wait:
        scene.wait(wait)
    if fade:
        scene.play(FadeOut(eg))

    return eg

# Helper function to display text message
def DisplayText(scene, text, font='', scale=1, move=(0, 0), wait=5, fade=True):
    t = MarkupText(text, font=font).scale(scale)
    scene.play(FadeIn(t.move_to(move[0] * DOWN + move[1] * RIGHT)))
    scene.wait(wait)
    if fade:
        scene.play(FadeOut(t))
    return t

# Helper function for Sutra display
# Inputs
#     sutra     - Sutra string
#     viccheda  - List of sandhi split segments
#   translation - List of translation segments, matching viccheda
def Sutra(scene, sutra, viccheda, translation, font='', wait=3, scale=1, move=(3, 5), fade=False, dir1=RIGHT, dir2=RIGHT):
    sg = VGroup(Text("सूत्रम्"), Text("विच्छेदः"),
                Text("Translation")).arrange(direction=DOWN,
                                                                                aligned_edge=RIGHT).set_color(
        BLUE).set_opacity(0.5).scale(scale)
    t0 = MarkupText(sutra).scale(scale)
    t1 = [MarkupText(x).scale(scale) for x in viccheda]
    t2 = [MarkupText(x, font=font).scale(scale) for x in translation]
    assert len(viccheda) == len(translation), "Sandhi split (viccheda) and translation must be of equal length"
    a1 =  UP if array_equal(dir1, RIGHT) else LEFT
    a2 =  UP if array_equal(dir2, RIGHT) else LEFT
    t1g = VGroup(*t1).arrange(direction=dir1, aligned_edge=a1).set_opacity(0)
    t2g = VGroup(*t2).arrange(direction=dir2, aligned_edge=a2).set_opacity(0)
    tg = VGroup(t0, t1g, t2g).arrange(direction=DOWN, aligned_edge=LEFT)

    tg.move_to(UP + RIGHT)
    sg[0].next_to(t0, LEFT)
    sg[1].next_to(t1g, LEFT)
    sg[2].next_to(t2g, LEFT)

    scene.play(FadeIn(sg))
    scene.play(Write(t0.set_color(ORANGE)))
    scene.wait(2)
    scene.play(t1g.animate.set_opacity(1))
    scene.wait(1)
    for i in range(len(translation)):
        scene.play(t1g[i].animate.set_color(YELLOW),
                   t2g[i].animate.set_color(YELLOW).set_opacity(1))
        scene.wait(3)

        scene.play(t1g[i].animate.set_opacity(0.25),
                   t2g[i].animate.set_opacity(0.25))
    t1g.set_opacity(1)
    t2g.set_opacity(1)
    scene.wait(3)
    scene.play(FadeOut(sg, t0))
    if move is not None:
        scene.play(t1g.set_color(WHITE).animate.scale(0.5).move_to(move[0] * UP + move[1] * RIGHT))
        scene.play(t2g.set_color(WHITE).animate.scale(0.5).move_to((move[0] - 0.5) * UP + move[1] * RIGHT))
        scene.wait(1)
    elif fade:
        scene.play(FadeOut(t1g, t2g))
    return (t1g, t2g)

def ShowOp(scene, sn1, sn2, sop, sr, move=(0, 0), wait=3, play=True, fade=True, oplen=0):
    ''' Helper function to display a single operation '''
    n1 = MarkupText(str(sn1), font='Cambria Math')
    n2 = MarkupText(str(sn2), font='Cambria Math')
    if oplen == 0:
        oplen = len(str(sn1))
    ln = Line(start=array([-1 * oplen / 2, 0, 0]), end=array([0, 0, 0])).set_color(YELLOW)
    op = MarkupText(str(sop), font='Cambria Math')
    res = MarkupText(str(sr), font='Cambria Math')
    g1 = VGroup(n1, n2, ln, res).arrange(DOWN, aligned_edge=RIGHT)
    g = VGroup(g1, op).arrange(RIGHT, aligned_edge=UP)

    if play:
        scene.play(Write(g.move_to(UP * move[0] + RIGHT * move[1])))
        if wait:
            scene.wait(wait)
        if fade:
            scene.play(FadeOut(g))
    return g

def Span(t, **kwargs):
    ''' Helper function for generating text with span tag '''
    return "<span " + " ".join([f"{k}='{v}'" for k,v in kwargs.items()]) + f">{t}</span>"

reason: from numpy