Performs grid reinforcement measures for all MV and LV grids
        Args:

        Returns:
964814
def reinforce_grid(self):
        
        # TODO: Finish method and enable LV case

        for grid_district in self.mv_grid_districts():

            # reinforce MV grid
            grid_district.mv_grid.reinforce_grid()

            # reinforce LV grids
            for lv_load_area in grid_district.lv_load_areas():
                if not lv_load_area.is_aggregated:
                    for lv_grid_district in lv_load_area.lv_grid_districts():
                        lv_grid_district.lv_grid.reinforce_grid()
1033451
def consistency_check(self):
        


        for c in self.iterate_components(self.one_port_components):
            missing = c.df.index[~c.df.bus.isin(self.buses.index)]
            if len(missing) > 0:
                logger.warning("The following %s have buses which are not defined:\n%s",
                               c.list_name, missing)

        for c in self.iterate_components(self.branch_components):
            for attr in ["bus0","bus1"]:
                missing = c.df.index[~c.df[attr].isin(self.buses.index)]
                if len(missing) > 0:
                    logger.warning("The following %s have %s which are not defined:\n%s",
                                   c.list_name, attr, missing)


        for c in self.iterate_components(self.passive_branch_components):
            for attr in ["x","r"]:
                bad = c.df.index[c.df[attr] == 0.]
                if len(bad) > 0:
                    logger.warning("The following %s have zero %s, which could break the linear load flow:\n%s",
                                   c.list_name, attr, bad)

            bad = c.df.index[(c.df["x"] == 0.) & (c.df["r"] == 0.)]
            if len(bad) > 0:
                logger.warning("The following %s have zero series impedance, which will break the load flow:\n%s",
                               c.list_name, bad)


        for c in self.iterate_components({"Transformer"}):
            bad = c.df.index[c.df["s_nom"] == 0.]
            if len(bad) > 0:
                logger.warning("The following %s have zero s_nom, which is used to define the impedance and will thus break the load flow:\n%s",
                               c.list_name, bad)


        for c in self.iterate_components(self.all_components):
            for attr in c.attrs.index[c.attrs.varying & c.attrs.static]:
                attr_df = c.pnl[attr]

                diff = attr_df.columns.difference(c.df.index)
                if len(diff) > 0:
                    logger.warning("The following %s have time series defined for attribute %s in network.%s_t, but are not defined in network.%s:\n%s",
                                   c.list_name, attr, c.list_name, c.list_name, diff)

                diff = self.snapshots.difference(attr_df.index)
                if len(diff) > 0:
                    logger.warning("In the time-dependent Dataframe for attribute %s of network.%s_t the following snapshots are missing:\n%s",
                                   attr, c.list_name, diff)

                diff = attr_df.index.difference(self.snapshots)
                if len(diff) > 0:
                    logger.warning("In the time-dependent Dataframe for attribute %s of network.%s_t the following snapshots are defined which are not in network.snapshots:\n%s",
                                   attr, c.list_name, diff)

        static_attrs = ['p_nom', 's_nom', 'e_nom']
        varying_attrs = ['p_max_pu', 'e_max_pu']
        for c in self.iterate_components(self.all_components - {'TransformerType'}):
            varying_attr = c.attrs.index[c.attrs.varying].intersection(varying_attrs)
            static_attr = c.attrs.index[c.attrs.static].intersection(static_attrs)

            if len(static_attr):
                diff = (getattr(self, c.list_name)[static_attr[0] + "_max"] -
                        getattr(self, c.list_name)[static_attr[0] + "_min"])
                if not diff[diff < 0].empty:
                    logger.warning("The following %s have smaller maximum than minimum expansion limit which can lead to infeasibilty:\n%s",
                                   c.list_name, diff[diff < 0].index)

            if len(varying_attr):
                max_pu = get_switchable_as_dense(self, c.name, varying_attr[0][0] + "_max_pu")
                min_pu = get_switchable_as_dense(self, c.name, varying_attr[0][0] + "_min_pu")

                # check for NaN values:
                if max_pu.isnull().values.any():
                    for col in max_pu.columns[max_pu.isnull().any()]:
                        logger.warning("The attribute %s of element %s of %s has NaN values for the following snapshots:\n%s",
                                       varying_attr[0][0] + "_max_pu", col, c.list_name, max_pu.index[max_pu[col].isnull()])
                if min_pu.isnull().values.any():
                    for col in min_pu.columns[min_pu.isnull().any()]:
                        logger.warning("The attribute %s of element %s of %s has NaN values for the following snapshots:\n%s",
                                       varying_attr[0][0] + "_min_pu", col, c.list_name, min_pu.index[min_pu[col].isnull()])

                diff = max_pu - min_pu
                diff = diff[diff < 0].dropna(axis=1, how='all')
                for col in diff.columns:
                    logger.warning("The element %s of %s has a smaller maximum than minimum operational limit which can lead to infeasibility for the following snapshots:\n%s",
                                   col, c.list_name, diff[col].dropna().index)

        #check all dtypes of component attributes

        for c in self.iterate_components():

            #first check static attributes

            dtypes_soll = c.attrs.loc[c.attrs["static"], "dtype"].drop("name")
            unmatched = (c.df.dtypes[dtypes_soll.index] != dtypes_soll)

            if unmatched.any():
                logger.warning("The following attributes of the dataframe %s have the wrong dtype:\n%s\n"
                               "They are:\n%s\n"
                               "but should be:\n%s",
                               c.list_name,
                               unmatched.index[unmatched],
                               c.df.dtypes[dtypes_soll.index[unmatched]],
                               dtypes_soll[unmatched])

            #now check varying attributes

            types_soll = c.attrs.loc[c.attrs["varying"], ["typ", "dtype"]]

            for attr, typ, dtype in types_soll.itertuples():
                if c.pnl[attr].empty:
                    continue

                unmatched = (c.pnl[attr].dtypes != dtype)

                if unmatched.any():
                    logger.warning("The following columns of time-varying attribute %s in %s_t have the wrong dtype:\n%s\n"
                                   "They are:\n%s\n"
                                   "but should be:\n%s",
                                   attr,c.list_name,
                                   unmatched.index[unmatched],
                                   c.pnl[attr].dtypes[unmatched],
                                   typ)
1315715
def generate_base_grid(self, vtk_filename=None):
        
        nd, ed, ed_dir = self.gen_grid_fcn(self.data.shape, self.voxelsize)
        self.add_nodes(nd)
        self.add_edges(ed, ed_dir, edge_low_or_high=0)

        if vtk_filename is not None:
            self.write_vtk(vtk_filename)
324043
def set_operation_voltage_level(self):
        

        mv_station_v_level_operation = float(cfg_ding0.get('mv_routing_tech_constraints',
                                                           'mv_station_v_level_operation'))

        self.v_level_operation = mv_station_v_level_operation * self.grid.v_level
367278
def get_grid_dimensions(current_problem_size, params, grid_div, block_size_names):
    """"""
    def get_dimension_divisor(divisor_list, default, params):
        if divisor_list is None:
            if default in params:
                divisor_list = [default]
            else:
                return 1
        return numpy.prod([int(eval(replace_param_occurrences(s, params))) for s in divisor_list])
    divisors = [get_dimension_divisor(d, block_size_names[i], params) for i, d in enumerate(grid_div)]
    return tuple(int(numpy.ceil(float(current_problem_size[i]) / float(d))) for i, d in enumerate(divisors))
1013523
def wrap(vtkdataset):
    
    wrappers = {
        'vtkUnstructuredGrid' : vtki.UnstructuredGrid,
        'vtkRectilinearGrid' : vtki.RectilinearGrid,
        'vtkStructuredGrid' : vtki.StructuredGrid,
        'vtkPolyData' : vtki.PolyData,
        'vtkImageData' : vtki.UniformGrid,
        'vtkStructuredPoints' : vtki.UniformGrid,
        'vtkMultiBlockDataSet' : vtki.MultiBlock,
        }
    key = vtkdataset.GetClassName()
    try:
        wrapped = wrappers[key](vtkdataset)
    except:
        logging.warning('VTK data type ({}) is not currently supported by vtki.'.format(key))
        return vtkdataset # if not supported just passes the VTK data object
    return wrapped
579980
def _q_iteration(self, Q, Bpp_solver, Vm, Va, pq):
        
        dVm = -Bpp_solver.solve(Q)

        # Update voltage.
        Vm[pq] = Vm[pq] + dVm
        V = Vm * exp(1j * Va)

        return V, Vm, Va
363659
def rebuildGrid( self ):
        
        vruler = self.verticalRuler()
        hruler = self.horizontalRuler()
        
        rect   = self._buildData['grid_rect']
        
        # process the vertical ruler
        h_lines     = []
        h_alt       = []
        h_notches   = []
        
        vpstart  = vruler.padStart()
        vnotches = vruler.notches()
        vpend    = vruler.padEnd()
        vcount   = len(vnotches) + vpstart + vpend
        deltay   = rect.height() / max((vcount - 1), 1)
        y        = rect.bottom()
        alt      = False
        
        for i in range(vcount):
            h_lines.append(QLineF(rect.left(), y, rect.right(), y))
            
            # store alternate color
            if ( alt ):
                alt_rect = QRectF(rect.left(), y, rect.width(), deltay)
                h_alt.append(alt_rect)
            
            # store notch information
            nidx = i - vpstart
            if ( 0 <= nidx and nidx < len(vnotches) ):
                notch = vnotches[nidx]
                notch_rect = QRectF(0, y - 3, rect.left() - 3, deltay)
                h_notches.append((notch_rect, notch))
            
            y -= deltay
            alt = not alt
        
        self._buildData['grid_h_lines']     = h_lines
        self._buildData['grid_h_alt']       = h_alt
        self._buildData['grid_h_notches']   = h_notches
        
        # process the horizontal ruler
        v_lines     = []
        v_alt       = []
        v_notches   = []
        
        hpstart     = hruler.padStart()
        hnotches    = hruler.notches()
        hpend       = hruler.padEnd()
        hcount      = len(hnotches) + hpstart + hpend
        deltax      = rect.width() / max((hcount - 1), 1)
        x           = rect.left()
        alt         = False
        
        for i in range(hcount):
            v_lines.append(QLineF(x, rect.top(), x, rect.bottom()))
            
            # store alternate info
            if ( alt ):
                alt_rect = QRectF(x - deltax, rect.top(), deltax, rect.height())
                v_alt.append(alt_rect)
            
            # store notch information
            nidx = i - hpstart
            if ( 0 <= nidx and nidx < len(hnotches) ):
                notch = hnotches[nidx]
                notch_rect = QRectF(x - (deltax / 2.0), 
                                    rect.bottom() + 3, 
                                    deltax, 
                                    13)
                v_notches.append((notch_rect, notch))
            
            x += deltax
            alt = not alt
        
        self._buildData['grid_v_lines']     = v_lines
        self._buildData['grid_v_alt']       = v_alt
        self._buildData['grid_v_notches']   = v_notches
        
        # draw the axis lines
        axis_lines = []
        axis_lines.append(QLineF(rect.left(), 
                                 rect.top(), 
                                 rect.left(), 
                                 rect.bottom()))
                                 
        axis_lines.append(QLineF(rect.left(), 
                                 rect.bottom(), 
                                 rect.right(), 
                                 rect.bottom()))
        
        self._buildData['axis_lines'] = axis_lines
817257
def keep_absolute_impute__r2(X, y, model_generator, method_name, num_fcounts=11):
    
    return __run_measure(measures.keep_impute, X, y, model_generator, method_name, 0, num_fcounts, sklearn.metrics.r2_score)
1163008
def build_grid(self):
        

        # add required transformers
        build_grid.transformer(self)

        # add branches of sectors retail/industrial and agricultural
        build_grid.build_ret_ind_agr_branches(self.grid_district)

        # add branches of sector residential
        build_grid.build_residential_branches(self.grid_district)
764085
def connect_generators(self, debug=False):
        

        self._graph = mv_connect.mv_connect_generators(self.grid_district, self._graph, debug)
1320676
def display_grid_scores(grid_scores, top=None):
    """"""
    grid_scores = sorted(grid_scores, key=lambda x: x[1], reverse=True)
    if top is not None:
        grid_scores = grid_scores[:top]
        
    # Compute a threshold for staring models with overlapping stderr:
    _, best_mean, best_scores = grid_scores[0]
    threshold = best_mean - 2 * sem(best_scores)
    
    for params, mean_score, scores in grid_scores:
        append_star = mean_score + 2 * sem(scores) > threshold
        print(display_scores(params, scores, append_star=append_star))
1337923
async def set_neighbors(self):
        '''
        '''
        t = time.time()
        self.logger.debug("Settings grid neighbors for the multi-environments.")
        tasks = []
        for i in range(len(self.grid)):
            for j in range(len(self.grid[0])):
                addr = self.grid[i][j]
                N, E, S, W = None, None, None, None
                if i != 0:
                    W = self.grid[i-1][j]
                if i != len(self.grid) - 1:
                    E = self.grid[i+1][j]
                if j != 0:
                    N = self.grid[i][j-1]
                if j != len(self.grid[0]) - 1:
                    S = self.grid[i][j+1]
                task = asyncio.ensure_future(self._set_node_neighbors(addr, N, E, S, W))
                tasks.append(task)
        await asyncio.gather(*tasks)

        self.logger.debug("Setting grid neighbors for the slave environments "
                          "and their agents.")
        tasks = []
        for addr in self.addrs:
            task = asyncio.ensure_future(self._set_neighbors(addr))
            tasks.append(task)
        await asyncio.gather(*tasks)
        self.logger.debug("All grid neighbors set in {} seconds."
                          .format(time.time() - t))
        x = self._ngs[0] * self._gs[0] * self._n_slaves
        y = self._ngs[1] * self._gs[1]
        self.logger.info("Initialized a distributed grid with overall size "
                         "({}, {}). Total of {} agents.".format(x, y, x*y))
835615
def extract_tendency_grid(self, model_grid):
        
        var_name = model_grid.variable + "-tendency"
        self.attributes[var_name] = []
        timesteps = np.arange(self.start_time, self.end_time + 1)
        for ti, t in enumerate(timesteps):
            t_index = t - model_grid.start_hour
            self.attributes[var_name].append(
                model_grid.data[t_index, self.i[ti], self.j[ti]] - model_grid.data[t_index - 1, self.i[ti], self.j[ti]]
                )
1003200
def assign_bus_results(grid, bus_data):
    

    # iterate of nodes and assign voltage obtained from power flow analysis
    for node in grid._graph.nodes():
        # check if node is connected to graph
        if (node not in grid.graph_isolated_nodes()
            and not isinstance(node,
                               LVLoadAreaCentreDing0)):
            if isinstance(node, LVStationDing0):
                node.voltage_res = bus_data.loc[node.pypsa_id, 'v_mag_pu']
            elif isinstance(node, (LVStationDing0, LVLoadAreaCentreDing0)):
                if node.lv_load_area.is_aggregated:
                    node.voltage_res = bus_data.loc[node.pypsa_id, 'v_mag_pu']
            elif not isinstance(node, CircuitBreakerDing0):
                node.voltage_res = bus_data.loc[node.pypsa_id, 'v_mag_pu']
            else:
                logger.warning("Object {} has been skipped while importing "
                               "results!")
672801
def _candidates_from_grid(self, n=1000):
        """"""
        used_vectors = set(tuple(v) for v in self.X)

        # if every point has been used before, gridding is done.
        grid_size = self.grid_width ** len(self.tunables)
        if len(used_vectors) == grid_size:
            return None

        all_vectors = set(itertools.product(*self._grid_axes))
        remaining_vectors = all_vectors - used_vectors
        candidates = np.array(list(map(np.array, remaining_vectors)))

        np.random.shuffle(candidates)
        return candidates[0:n]
142028
def get_mv_impedance(grid):
    

    omega = 2 * math.pi * 50

    mv_grid = grid.grid_district.lv_load_area.mv_grid_district.mv_grid
    edges = mv_grid.find_path(grid._station, mv_grid._station, type='edges')
    r_mv_grid = sum([e[2]['branch'].type['R'] * e[2]['branch'].length / 1e3
                     for e in edges])
    x_mv_grid = sum([e[2]['branch'].type['L'] / 1e3 * omega * e[2][
        'branch'].length / 1e3 for e in edges])

    return [r_mv_grid, x_mv_grid]