def nan_to_num(x, copy=True): """ Replace NaN with zero and infinity with large finite numbers. If `x` is inexact, NaN is replaced by zero, and infinity and -infinity replaced by the respectively largest and most negative finite floating point values representable by ``x.dtype``. For complex dtypes, the above is applied to each of the real and imaginary components of `x` separately. If `x` is not inexact, then no replacements are made. Parameters ---------- x : scalar or array_like Input data. copy : bool, optional Whether to create a copy of `x` (True) or to replace values in-place (False). The in-place operation only occurs if casting to an array does not require a copy. Default is True. .. versionadded:: 1.13 Returns ------- out : ndarray `x`, with the non-finite values replaced. If `copy` is False, this may be `x` itself. See Also -------- isinf : Shows which elements are positive or negative infinity. isneginf : Shows which elements are negative infinity. isposinf : Shows which elements are positive infinity. isnan : Shows which elements are Not a Number (NaN). isfinite : Shows which elements are finite (not NaN, not infinity) Notes ----- NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity. Examples -------- >>> np.nan_to_num(np.inf) 1.7976931348623157e+308 >>> np.nan_to_num(-np.inf) -1.7976931348623157e+308 >>> np.nan_to_num(np.nan) 0.0 >>> x = np.array([np.inf, -np.inf, np.nan, -128, 128]) >>> np.nan_to_num(x) array([ 1.79769313e+308, -1.79769313e+308, 0.00000000e+000, # may vary -1.28000000e+002, 1.28000000e+002]) >>> y = np.array([complex(np.inf, np.nan), np.nan, complex(np.nan, np.inf)]) array([ 1.79769313e+308, -1.79769313e+308, 0.00000000e+000, # may vary -1.28000000e+002, 1.28000000e+002]) >>> np.nan_to_num(y) array([ 1.79769313e+308 +0.00000000e+000j, # may vary 0.00000000e+000 +0.00000000e+000j, 0.00000000e+000 +1.79769313e+308j]) """ x = _nx.array(x, subok=True, copy=copy) xtype = x.dtype.type isscalar = (x.ndim == 0) if not issubclass(xtype, _nx.inexact): return x[()] if isscalar else x iscomplex = issubclass(xtype, _nx.complexfloating) dest = (x.real, x.imag) if iscomplex else (x,) maxf, minf = _getmaxmin(x.real.dtype) for d in dest: _nx.copyto(d, 0.0, where=isnan(d)) _nx.copyto(d, maxf, where=isposinf(d)) _nx.copyto(d, minf, where=isneginf(d)) return x[()] if isscalar else x
def nan_to_num(x): """ Replace nan with zero and inf with finite numbers. Returns an array or scalar replacing Not a Number (NaN) with zero, (positive) infinity with a very large number and negative infinity with a very small (or negative) number. Parameters ---------- x : array_like Input data. Returns ------- out : ndarray New Array with the same shape as `x` and dtype of the element in `x` with the greatest precision. If `x` is inexact, then NaN is replaced by zero, and infinity (-infinity) is replaced by the largest (smallest or most negative) floating point value that fits in the output dtype. If `x` is not inexact, then a copy of `x` is returned. See Also -------- isinf : Shows which elements are negative or negative infinity. isneginf : Shows which elements are negative infinity. isposinf : Shows which elements are positive infinity. isnan : Shows which elements are Not a Number (NaN). isfinite : Shows which elements are finite (not NaN, not infinity) Notes ----- Numpy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity. Examples -------- >>> np.set_printoptions(precision=8) >>> x = np.array([np.inf, -np.inf, np.nan, -128, 128]) >>> np.nan_to_num(x) array([ 1.79769313e+308, -1.79769313e+308, 0.00000000e+000, -1.28000000e+002, 1.28000000e+002]) """ x = _nx.array(x, subok=True) xtype = x.dtype.type if not issubclass(xtype, _nx.inexact): return x iscomplex = issubclass(xtype, _nx.complexfloating) isscalar = (x.ndim == 0) x = x[None] if isscalar else x dest = (x.real, x.imag) if iscomplex else (x,) maxf, minf = _getmaxmin(x.real.dtype) for d in dest: _nx.copyto(d, 0.0, where=isnan(d)) _nx.copyto(d, maxf, where=isposinf(d)) _nx.copyto(d, minf, where=isneginf(d)) return x[0] if isscalar else x
def nan_to_num(x, copy=True): """ Replace NaN with zero and infinity with large finite numbers. If `x` is inexact, NaN is replaced by zero, and infinity and -infinity replaced by the respectively largest and most negative finite floating point values representable by ``x.dtype``. For complex dtypes, the above is applied to each of the real and imaginary components of `x` separately. If `x` is not inexact, then no replacements are made. Parameters ---------- x : scalar or array_like Input data. copy : bool, optional Whether to create a copy of `x` (True) or to replace values in-place (False). The in-place operation only occurs if casting to an array does not require a copy. Default is True. .. versionadded:: 1.13 Returns ------- out : ndarray `x`, with the non-finite values replaced. If `copy` is False, this may be `x` itself. See Also -------- isinf : Shows which elements are positive or negative infinity. isneginf : Shows which elements are negative infinity. isposinf : Shows which elements are positive infinity. isnan : Shows which elements are Not a Number (NaN). isfinite : Shows which elements are finite (not NaN, not infinity) Notes ----- NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity. Examples -------- >>> np.nan_to_num(np.inf) 1.7976931348623157e+308 >>> np.nan_to_num(-np.inf) -1.7976931348623157e+308 >>> np.nan_to_num(np.nan) 0.0 >>> x = np.array([np.inf, -np.inf, np.nan, -128, 128]) >>> np.nan_to_num(x) array([ 1.79769313e+308, -1.79769313e+308, 0.00000000e+000, -1.28000000e+002, 1.28000000e+002]) >>> y = np.array([complex(np.inf, np.nan), np.nan, complex(np.nan, np.inf)]) >>> np.nan_to_num(y) array([ 1.79769313e+308 +0.00000000e+000j, 0.00000000e+000 +0.00000000e+000j, 0.00000000e+000 +1.79769313e+308j]) """ x = _nx.array(x, subok=True, copy=copy) xtype = x.dtype.type isscalar = (x.ndim == 0) if not issubclass(xtype, _nx.inexact): return x[()] if isscalar else x iscomplex = issubclass(xtype, _nx.complexfloating) dest = (x.real, x.imag) if iscomplex else (x,) maxf, minf = _getmaxmin(x.real.dtype) for d in dest: _nx.copyto(d, 0.0, where=isnan(d)) _nx.copyto(d, maxf, where=isposinf(d)) _nx.copyto(d, minf, where=isneginf(d)) return x[()] if isscalar else x
def nan_to_num(x): """ Replace nan with zero and inf with finite numbers. Returns an array or scalar replacing Not a Number (NaN) with zero, (positive) infinity with a very large number and negative infinity with a very small (or negative) number. Parameters ---------- x : array_like Input data. Returns ------- out : ndarray New Array with the same shape as `x` and dtype of the element in `x` with the greatest precision. If `x` is inexact, then NaN is replaced by zero, and infinity (-infinity) is replaced by the largest (smallest or most negative) floating point value that fits in the output dtype. If `x` is not inexact, then a copy of `x` is returned. See Also -------- isinf : Shows which elements are negative or negative infinity. isneginf : Shows which elements are negative infinity. isposinf : Shows which elements are positive infinity. isnan : Shows which elements are Not a Number (NaN). isfinite : Shows which elements are finite (not NaN, not infinity) Notes ----- Numpy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity. Examples -------- >>> np.set_printoptions(precision=8) >>> x = np.array([np.inf, -np.inf, np.nan, -128, 128]) >>> np.nan_to_num(x) array([ 1.79769313e+308, -1.79769313e+308, 0.00000000e+000, -1.28000000e+002, 1.28000000e+002]) """ x = _nx.array(x, subok=True) xtype = x.dtype.type if not issubclass(xtype, _nx.inexact): return x iscomplex = issubclass(xtype, _nx.complexfloating) isscalar = (x.ndim == 0) x = x[None] if isscalar else x dest = (x.real, x.imag) if iscomplex else (x, ) maxf, minf = _getmaxmin(x.real.dtype) for d in dest: _nx.copyto(d, 0.0, where=isnan(d)) _nx.copyto(d, maxf, where=isposinf(d)) _nx.copyto(d, minf, where=isneginf(d)) return x[0] if isscalar else x
def _build_gen_opf(net, ppc): ''' Takes the empty ppc network and fills it with the gen values. The gen datatype will be float afterwards. **INPUT**: **net** -The pandapower format network **ppc** - The PYPOWER format network to fill in values ''' bus_lookup = net["_pd2ppc_lookups"]["bus"] calculate_voltage_angles = net["_options"]["calculate_voltage_angles"] if len(net.dcline) > 0: ppc["dcline"] = net.dcline[["loss_kw", "loss_percent"]].values # get in service elements _is_elements = net["_is_elements"] eg_is = net["ext_grid"][_is_elements['ext_grid']] gen_is = net["gen"][_is_elements['gen']] sg_is = net.sgen[(net.sgen.in_service & net.sgen.controllable) == True] \ if "controllable" in net.sgen.columns else DataFrame() l_is = net.load[(net.load.in_service & net.load.controllable) == True] \ if "controllable" in net.load.columns else DataFrame() _is_elements["sgen_controllable"] = sg_is _is_elements["load_controllable"] = l_is eg_end = len(eg_is) gen_end = eg_end + len(gen_is) sg_end = gen_end + len(sg_is) l_end = sg_end + len(l_is) q_lim_default = 1e9 # which is 1000 TW - should be enough for distribution grids. p_lim_default = 1e9 # changes must be considered in check_opf_data delta = net["_options"]["delta"] # initialize generator matrix ppc["gen"] = zeros(shape=(l_end, 21), dtype=float) ppc["gen"][:] = array([0, 0, 0, q_lim_default, -q_lim_default, 1., 1., 1, p_lim_default, -p_lim_default, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) # add sgens first so pv bus types won't be overwritten if sg_end > gen_end: ppc["gen"][gen_end:sg_end, GEN_BUS] = bus_lookup[sg_is["bus"].values] ppc["gen"][gen_end:sg_end, PG] = - sg_is["p_kw"].values * 1e-3 * sg_is["scaling"].values ppc["gen"][gen_end:sg_end, QG] = sg_is["q_kvar"].values * 1e-3 * sg_is["scaling"].values # set bus values for generator buses gen_buses = bus_lookup[sg_is["bus"].values] ppc["bus"][gen_buses, BUS_TYPE] = PQ # set constraints for PV generators if "min_q_kvar" in sg_is.columns: ppc["gen"][gen_end:sg_end, QMAX] = - (sg_is["min_q_kvar"].values * 1e-3 - delta) max_q_kvar = ppc["gen"][gen_end:sg_end, [QMIN]] ncn.copyto(max_q_kvar, -q_lim_default, where=isnan(max_q_kvar)) ppc["gen"][gen_end:sg_end, [QMIN]] = max_q_kvar if "max_q_kvar" in sg_is.columns: ppc["gen"][gen_end:sg_end, QMIN] = - (sg_is["max_q_kvar"].values * 1e-3 + delta) min_q_kvar = ppc["gen"][gen_end:sg_end, [QMAX]] ncn.copyto(min_q_kvar, q_lim_default, where=isnan(min_q_kvar)) ppc["gen"][gen_end:sg_end, [QMAX]] = min_q_kvar - 1e-10 if "max_p_kw" in sg_is.columns: ppc["gen"][gen_end:sg_end, PMIN] = - (sg_is["max_p_kw"].values * 1e-3 + delta) max_p_kw = ppc["gen"][gen_end:sg_end, [PMIN]] ncn.copyto(max_p_kw, -p_lim_default, where=isnan(max_p_kw)) ppc["gen"][gen_end:sg_end, [PMIN]] = max_p_kw if "min_p_kw" in sg_is.columns: ppc["gen"][gen_end:sg_end, PMAX] = - (sg_is["min_p_kw"].values * 1e-3 - delta) min_p_kw = ppc["gen"][gen_end:sg_end, [PMAX]] ncn.copyto(min_p_kw, p_lim_default, where=isnan(min_p_kw)) ppc["gen"][gen_end:sg_end, [PMAX]] = min_p_kw # add controllable loads if l_end > sg_end: load_buses = bus_lookup[l_is["bus"].values] ppc["gen"][sg_end:l_end, GEN_BUS] = load_buses ppc["gen"][sg_end:l_end, PG] = - l_is["p_kw"].values * 1e-3 * l_is["scaling"].values ppc["gen"][sg_end:l_end, QG] = l_is["q_kvar"].values * 1e-3 * l_is["scaling"].values # set bus values for controllable loads ppc["bus"][load_buses, BUS_TYPE] = PQ # set constraints for controllable loads if "min_q_kvar" in l_is.columns: ppc["gen"][sg_end:l_end, QMAX] = - (l_is["min_q_kvar"].values * 1e-3 - delta) max_q_kvar = ppc["gen"][sg_end:l_end, [QMIN]] ncn.copyto(max_q_kvar, -q_lim_default, where=isnan(max_q_kvar)) ppc["gen"][sg_end:l_end, [QMIN]] = max_q_kvar if "max_q_kvar" in l_is.columns: ppc["gen"][sg_end:l_end, QMIN] = - (l_is["max_q_kvar"].values * 1e-3 + delta) min_q_kvar = ppc["gen"][sg_end:l_end, [QMAX]] ncn.copyto(min_q_kvar, q_lim_default, where=isnan(min_q_kvar)) ppc["gen"][sg_end:l_end, [QMAX]] = min_q_kvar if "min_p_kw" in l_is.columns: ppc["gen"][sg_end:l_end, PMIN] = - (l_is["max_p_kw"].values * 1e-3 + delta) max_p_kw = ppc["gen"][sg_end:l_end, [PMIN]] ncn.copyto(max_p_kw, -p_lim_default, where=isnan(max_p_kw)) ppc["gen"][sg_end:l_end, [PMIN]] = max_p_kw if "max_p_kw" in l_is.columns: ppc["gen"][sg_end:l_end, PMAX] = - (l_is["min_p_kw"].values * 1e-3 - delta) min_p_kw = ppc["gen"][sg_end:l_end, [PMAX]] ncn.copyto(min_p_kw, p_lim_default, where=isnan(min_p_kw)) ppc["gen"][sg_end:l_end, [PMAX]] = min_p_kw # add ext grid / slack data ppc["gen"][:eg_end, GEN_BUS] = bus_lookup[eg_is["bus"].values] ppc["gen"][:eg_end, VG] = eg_is["vm_pu"].values ppc["gen"][:eg_end, GEN_STATUS] = eg_is["in_service"].values if "max_p_kw" in eg_is.columns: ppc["gen"][:eg_end, PMIN] = - (eg_is["max_p_kw"].values * 1e-3 - delta) max_p_kw = ppc["gen"][:eg_end, [PMIN]] ncn.copyto(max_p_kw, -p_lim_default, where=isnan(max_p_kw)) ppc["gen"][:eg_end, [PMIN]] = max_p_kw if "min_p_kw" in eg_is.columns: ppc["gen"][:eg_end, PMAX] = - (eg_is["min_p_kw"].values * 1e-3 + delta) min_p_kw = ppc["gen"][:eg_end, [PMAX]] ncn.copyto(min_p_kw, p_lim_default, where=isnan(min_p_kw)) ppc["gen"][:eg_end, [PMAX]] = min_p_kw if "min_q_kvar" in eg_is.columns: ppc["gen"][:eg_end, QMAX] = - (eg_is["min_q_kvar"].values * 1e-3 - delta) max_q_kvar = ppc["gen"][:eg_end, [QMIN]] ncn.copyto(max_q_kvar, -q_lim_default, where=isnan(max_q_kvar)) ppc["gen"][:eg_end, [QMIN]] = max_q_kvar if "max_q_kvar" in eg_is.columns: ppc["gen"][:eg_end, QMIN] = - (eg_is["max_q_kvar"].values * 1e-3 + delta) min_q_kvar = ppc["gen"][:eg_end, [QMAX]] ncn.copyto(min_q_kvar, q_lim_default, where=isnan(min_q_kvar)) ppc["gen"][:eg_end, [QMAX]] = min_q_kvar - 1e-10 # set bus values for external grid buses eg_buses = bus_lookup[eg_is["bus"].values] if calculate_voltage_angles: ppc["bus"][eg_buses, VA] = eg_is["va_degree"].values ppc["bus"][eg_buses, BUS_TYPE] = REF ppc["bus"][eg_buses, VM] = eg_is["vm_pu"].values # REF busses don't have flexible voltages by definition: ppc["bus"][eg_buses, VMAX] = ppc["bus"][ppc["bus"][:, BUS_TYPE] == REF, VM] ppc["bus"][eg_buses, VMIN] = ppc["bus"][ppc["bus"][:, BUS_TYPE] == REF, VM] # add generator / pv data if gen_end > eg_end: ppc["gen"][eg_end:gen_end, GEN_BUS] = bus_lookup[gen_is["bus"].values] ppc["gen"][eg_end:gen_end, PG] = - gen_is["p_kw"].values * 1e-3 * gen_is["scaling"].values ppc["gen"][eg_end:gen_end, VG] = gen_is["vm_pu"].values # set bus values for generator buses gen_buses = bus_lookup[gen_is["bus"].values] ppc["bus"][gen_buses, BUS_TYPE] = PV ppc["bus"][gen_buses, VM] = gen_is["vm_pu"].values # set constraints for PV generators _copy_q_limits_to_ppc(net, ppc, eg_end, gen_end, _is_elements['gen']) _copy_p_limits_to_ppc(net, ppc, eg_end, gen_end, _is_elements['gen']) _replace_nans_with_default_q_limits_in_ppc(ppc, eg_end, gen_end, q_lim_default) _replace_nans_with_default_p_limits_in_ppc(ppc, eg_end, gen_end, p_lim_default)
def nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None): """ Replace NaN with zero and infinity with large finite numbers (default behaviour) or with the numbers defined by the user using the `nan`, `posinf` and/or `neginf` keywords. If `x` is inexact, NaN is replaced by zero or by the user defined value in `nan` keyword, infinity is replaced by the largest finite floating point values representable by ``x.dtype`` or by the user defined value in `posinf` keyword and -infinity is replaced by the most negative finite floating point values representable by ``x.dtype`` or by the user defined value in `neginf` keyword. For complex dtypes, the above is applied to each of the real and imaginary components of `x` separately. If `x` is not inexact, then no replacements are made. Parameters ---------- x : scalar or array_like Input data. copy : bool, optional Whether to create a copy of `x` (True) or to replace values in-place (False). The in-place operation only occurs if casting to an array does not require a copy. Default is True. .. versionadded:: 1.13 nan : int, float, optional Value to be used to fill NaN values. If no value is passed then NaN values will be replaced with 0.0. .. versionadded:: 1.17 posinf : int, float, optional Value to be used to fill positive infinity values. If no value is passed then positive infinity values will be replaced with a very large number. .. versionadded:: 1.17 neginf : int, float, optional Value to be used to fill negative infinity values. If no value is passed then negative infinity values will be replaced with a very small (or negative) number. .. versionadded:: 1.17 Returns ------- out : ndarray `x`, with the non-finite values replaced. If `copy` is False, this may be `x` itself. See Also -------- isinf : Shows which elements are positive or negative infinity. isneginf : Shows which elements are negative infinity. isposinf : Shows which elements are positive infinity. isnan : Shows which elements are Not a Number (NaN). isfinite : Shows which elements are finite (not NaN, not infinity) Notes ----- NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity. Examples -------- >>> np.nan_to_num(np.inf) 1.7976931348623157e+308 >>> np.nan_to_num(-np.inf) -1.7976931348623157e+308 >>> np.nan_to_num(np.nan) 0.0 >>> x = np.array([np.inf, -np.inf, np.nan, -128, 128]) >>> np.nan_to_num(x) array([ 1.79769313e+308, -1.79769313e+308, 0.00000000e+000, # may vary -1.28000000e+002, 1.28000000e+002]) >>> np.nan_to_num(x, nan=-9999, posinf=33333333, neginf=33333333) array([ 3.3333333e+07, 3.3333333e+07, -9.9990000e+03, -1.2800000e+02, 1.2800000e+02]) >>> y = np.array([complex(np.inf, np.nan), np.nan, complex(np.nan, np.inf)]) array([ 1.79769313e+308, -1.79769313e+308, 0.00000000e+000, # may vary -1.28000000e+002, 1.28000000e+002]) >>> np.nan_to_num(y) array([ 1.79769313e+308 +0.00000000e+000j, # may vary 0.00000000e+000 +0.00000000e+000j, 0.00000000e+000 +1.79769313e+308j]) >>> np.nan_to_num(y, nan=111111, posinf=222222) array([222222.+111111.j, 111111. +0.j, 111111.+222222.j]) """ x = _nx.array(x, subok=True, copy=copy) xtype = x.dtype.type isscalar = (x.ndim == 0) if not issubclass(xtype, _nx.inexact): return x[()] if isscalar else x iscomplex = issubclass(xtype, _nx.complexfloating) dest = (x.real, x.imag) if iscomplex else (x, ) maxf, minf = _getmaxmin(x.real.dtype) if posinf is not None: maxf = posinf if neginf is not None: minf = neginf for d in dest: idx_nan = isnan(d) idx_posinf = isposinf(d) idx_neginf = isneginf(d) _nx.copyto(d, nan, where=idx_nan) _nx.copyto(d, maxf, where=idx_posinf) _nx.copyto(d, minf, where=idx_neginf) return x[()] if isscalar else x