Skip to content

Commit

Permalink
Merge pull request #582 from BDonnot/master
Browse files Browse the repository at this point in the history
Code cleaning
  • Loading branch information
BDonnot authored Feb 19, 2024
2 parents 35869ff + 944c444 commit 0fc4848
Show file tree
Hide file tree
Showing 25 changed files with 110 additions and 107 deletions.
16 changes: 9 additions & 7 deletions examples/backend_integration/Step1_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
# to serve as an example
import pandapower as pp

ERR_MSG_ELSEWHERE = "Will be detailed in another example script"


class CustomBackend_Step1(Backend):
def load_grid(self,
Expand Down Expand Up @@ -97,25 +99,25 @@ def load_grid(self,
self._compute_pos_big_topo()

def apply_action(self, backendAction: Union["grid2op.Action._backendAction._BackendAction", None]) -> None:
raise NotImplementedError("Will be detailed in another example script")
raise NotImplementedError()

def runpf(self, is_dc : bool=False) -> Tuple[bool, Union[Exception, None]]:
raise NotImplementedError("Will be detailed in another example script")
raise NotImplementedError(ERR_MSG_ELSEWHERE)

def get_topo_vect(self) -> np.ndarray:
raise NotImplementedError("Will be detailed in another example script")
raise NotImplementedError(ERR_MSG_ELSEWHERE)

def generators_info(self)-> Tuple[np.ndarray, np.ndarray, np.ndarray]:
raise NotImplementedError("Will be detailed in another example script")
raise NotImplementedError(ERR_MSG_ELSEWHERE)

def loads_info(self)-> Tuple[np.ndarray, np.ndarray, np.ndarray]:
raise NotImplementedError("Will be detailed in another example script")
raise NotImplementedError(ERR_MSG_ELSEWHERE)

def lines_or_info(self)-> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
raise NotImplementedError("Will be detailed in another example script")
raise NotImplementedError(ERR_MSG_ELSEWHERE)

def lines_ex_info(self)-> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
raise NotImplementedError("Will be detailed in another example script")
raise NotImplementedError(ERR_MSG_ELSEWHERE)


if __name__ == "__main__":
Expand Down
16 changes: 8 additions & 8 deletions examples/backend_integration/Step4_modify_line_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,10 @@ def lines_ex_info(self)-> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
print(f"{q_or = }")
print(f"{v_or = }")
print(f"{a_or = }")
assert p_or[0] == 0.
assert q_or[0] == 0.
assert v_or[0] == 0.
assert a_or[0] == 0.
assert np.abs(p_or[0]) <= 1e-7
assert np.abs(q_or[0]) <= 1e-7
assert np.abs(v_or[0]) <= 1e-7
assert np.abs(a_or[0]) <= 1e-7

# this is how "user" manipute the grid
# in this I reconnect powerline 0
Expand Down Expand Up @@ -280,7 +280,7 @@ def lines_ex_info(self)-> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
print(f"{q_or = }")
print(f"{v_or = }")
print(f"{a_or = }")
assert p_or[line_id] == 0.
assert q_or[line_id] == 0.
assert v_or[line_id] == 0.
assert a_or[line_id] == 0.
assert np.abs(p_or[line_id]) <= 1e-7
assert np.abs(q_or[line_id]) <= 1e-7
assert np.abs(v_or[line_id]) <= 1e-7
assert np.abs(a_or[line_id]) <= 1e-7
2 changes: 1 addition & 1 deletion grid2op/Action/_backendAction.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _change_val_int(self, newvals):
self.values[changed_] = (1 - self.values[changed_]) + 2

def _change_val_float(self, newvals):
changed_ = newvals != 0.0
changed_ = np.abs(newvals) >= 1e-7
self.changed[changed_] = True
self.values[changed_] += newvals[changed_]

Expand Down
50 changes: 25 additions & 25 deletions grid2op/Action/baseAction.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ def _aux_serialize_add_key_change(self, attr_nm, dict_key, res):
res[dict_key] = tmp_

def _aux_serialize_add_key_set(self, attr_nm, dict_key, res):
tmp_ = [(int(id_), int(val)) for id_, val in enumerate(getattr(self, attr_nm)) if val != 0.]
tmp_ = [(int(id_), int(val)) for id_, val in enumerate(getattr(self, attr_nm)) if np.abs(val) >= 1e-7]
if tmp_:
res[dict_key] = tmp_

Expand Down Expand Up @@ -683,7 +683,7 @@ def as_serializable_dict(self) -> dict:
res["redispatch"] = [
(int(id_), float(val))
for id_, val in enumerate(self._redispatch)
if val != 0.0
if np.abs(val) >= 1e-7
]
if not res["redispatch"]:
del res["redispatch"]
Expand All @@ -692,7 +692,7 @@ def as_serializable_dict(self) -> dict:
res["set_storage"] = [
(int(id_), float(val))
for id_, val in enumerate(self._storage_power)
if val != 0.0
if np.abs(val) >= 1e-7
]
if not res["set_storage"]:
del res["set_storage"]
Expand All @@ -701,7 +701,7 @@ def as_serializable_dict(self) -> dict:
res["curtail"] = [
(int(id_), float(val))
for id_, val in enumerate(self._curtail)
if val != -1
if np.abs(val + 1.) >= 1e-7
]
if not res["curtail"]:
del res["curtail"]
Expand Down Expand Up @@ -896,10 +896,10 @@ def _post_process_from_vect(self):
self._modif_set_status = (self._set_line_status != 0).any()
self._modif_change_status = (self._switch_line_status).any()
self._modif_redispatch = (
np.isfinite(self._redispatch) & (self._redispatch != 0.0)
np.isfinite(self._redispatch) & (np.abs(self._redispatch) >= 1e-7)
).any()
self._modif_storage = (self._storage_power != 0.0).any()
self._modif_curtailment = (self._curtail != -1.0).any()
self._modif_storage = (np.abs(self._storage_power) >= 1e-7).any()
self._modif_curtailment = (np.abs(self._curtail + 1.0) >= 1e-7).any()
self._modif_alarm = self._raise_alarm.any()
self._modif_alert = self._raise_alert.any()

Expand All @@ -912,7 +912,7 @@ def _assign_attr_from_name(self, attr_nm, vect):
super()._assign_attr_from_name(attr_nm, vect)
self._post_process_from_vect()
else:
if np.isfinite(vect).any() and (vect != 0.0).any():
if np.isfinite(vect).any() and (np.abs(vect) >= 1e-7).any():
self._dict_inj[attr_nm] = vect

def check_space_legit(self):
Expand Down Expand Up @@ -1539,7 +1539,7 @@ def _aux_iadd_inj(self, other):

def _aux_iadd_redisp(self, other):
redispatching = other._redispatch
if (redispatching != 0.0).any():
if (np.abs(redispatching) >= 1e-7).any():
if "_redispatch" not in self.attr_list_set:
warnings.warn(
type(self).ERR_ACTION_CUT.format("_redispatch")
Expand All @@ -1550,7 +1550,7 @@ def _aux_iadd_redisp(self, other):

def _aux_iadd_curtail(self, other):
curtailment = other._curtail
ok_ind = np.isfinite(curtailment) & (curtailment != -1.0)
ok_ind = np.isfinite(curtailment) & (np.abs(curtailment + 1.0) >= 1e-7)
if ok_ind.any():
if "_curtail" not in self.attr_list_set:
warnings.warn(
Expand All @@ -1564,7 +1564,7 @@ def _aux_iadd_curtail(self, other):

def _aux_iadd_storage(self, other):
set_storage = other._storage_power
ok_ind = np.isfinite(set_storage) & (set_storage != 0.0).any()
ok_ind = np.isfinite(set_storage) & (np.abs(set_storage) >= 1e-7).any()
if ok_ind.any():
if "_storage_power" not in self.attr_list_set:
warnings.warn(
Expand Down Expand Up @@ -2423,7 +2423,7 @@ def _check_for_correct_modif_flags(self):
"You illegally act on the powerline status (using change)"
)

if (self._redispatch != 0.0).any():
if (np.abs(self._redispatch) >= 1e-7).any():
if not self._modif_redispatch:
raise AmbiguousAction(
"A action of type redispatch is performed while the appropriate flag "
Expand All @@ -2434,7 +2434,7 @@ def _check_for_correct_modif_flags(self):
if "redispatch" not in self.authorized_keys:
raise IllegalAction("You illegally act on the redispatching")

if (self._storage_power != 0.0).any():
if (np.abs(self._storage_power) >= 1e-7).any():
if not self._modif_storage:
raise AmbiguousAction(
"A action on the storage unit is performed while the appropriate flag "
Expand All @@ -2445,7 +2445,7 @@ def _check_for_correct_modif_flags(self):
if "set_storage" not in self.authorized_keys:
raise IllegalAction("You illegally act on the storage unit")

if (self._curtail != -1.0).any():
if (np.abs(self._curtail + 1.0) >= 1e-7).any():
if not self._modif_curtailment:
raise AmbiguousAction(
"A curtailment is performed while the action is not supposed to have done so. "
Expand Down Expand Up @@ -2876,8 +2876,8 @@ def _is_curtailment_ambiguous(self):
"units affected"
)

if ((self._curtail < 0.0) & (self._curtail != -1.0)).any():
where_bug = np.nonzero((self._curtail < 0.0) & (self._curtail != -1.0))[0]
if ((self._curtail < 0.0) & (np.abs(self._curtail + 1.0) >= 1e-7)).any():
where_bug = np.nonzero((self._curtail < 0.0) & (np.abs(self._curtail + 1.0) >= 1e-7))[0]
raise InvalidCurtailment(
f"you asked to perform a negative curtailment: "
f"self._curtail[{where_bug}] < 0. "
Expand All @@ -2890,7 +2890,7 @@ def _is_curtailment_ambiguous(self):
f"self._curtail[{where_bug}] > 1. "
f"Curtailment should be a real number between 0.0 and 1.0"
)
if (self._curtail[~cls.gen_renewable] != -1.0).any():
if (np.abs(self._curtail[~cls.gen_renewable] +1.0) >= 1e-7).any():
raise InvalidCurtailment(
"Trying to apply a curtailment on a non renewable generator"
)
Expand Down Expand Up @@ -2989,7 +2989,7 @@ def __str__(self) -> str:
"\t - Modify the generators with redispatching in the following way:"
)
for gen_idx in range(self.n_gen):
if self._redispatch[gen_idx] != 0.0:
if np.abs(self._redispatch[gen_idx]) >= 1e-7:
gen_name = self.name_gen[gen_idx]
r_amount = self._redispatch[gen_idx]
res.append(
Expand All @@ -3005,7 +3005,7 @@ def __str__(self) -> str:
res.append("\t - Modify the storage units in the following way:")
for stor_idx in range(self.n_storage):
amount_ = self._storage_power[stor_idx]
if np.isfinite(amount_) and amount_ != 0.0:
if np.isfinite(amount_) and np.abs(amount_) >= 1e-7:
name_ = self.name_storage[stor_idx]
res.append(
'\t \t - Ask unit "{}" to {} {:.2f} MW (setpoint: {:.2f} MW)'
Expand All @@ -3024,7 +3024,7 @@ def __str__(self) -> str:
res.append("\t - Perform the following curtailment:")
for gen_idx in range(self.n_gen):
amount_ = self._curtail[gen_idx]
if np.isfinite(amount_) and amount_ != -1.0:
if np.isfinite(amount_) and np.abs(amount_ + 1.0) >= 1e-7:
name_ = self.name_gen[gen_idx]
res.append(
'\t \t - Limit unit "{}" to {:.1f}% of its Pmax (setpoint: {:.3f})'
Expand Down Expand Up @@ -3245,9 +3245,9 @@ def impact_on_objects(self) -> dict:

# handle redispatching
redispatch = {"changed": False, "generators": []}
if (self._redispatch != 0.0).any():
if (np.abs(self._redispatch) >= 1e-7).any():
for gen_idx in range(self.n_gen):
if self._redispatch[gen_idx] != 0.0:
if np.abs(self._redispatch[gen_idx]) >= 1e-7:
gen_name = self.name_gen[gen_idx]
r_amount = self._redispatch[gen_idx]
redispatch["generators"].append(
Expand Down Expand Up @@ -3277,7 +3277,7 @@ def impact_on_objects(self) -> dict:
if self._modif_curtailment:
for gen_idx in range(self.n_gen):
tmp = self._curtail[gen_idx]
if np.isfinite(tmp) and tmp != -1:
if np.isfinite(tmp) and np.abs(tmp + 1.) >= 1e-7:
name_ = self.name_gen[gen_idx]
new_max = tmp
curtailment["limit"].append(
Expand Down Expand Up @@ -3540,7 +3540,7 @@ def get_types(self) -> Tuple[bool, bool, bool, bool, bool, bool, bool]:
lines_impacted, subs_impacted = self.get_topological_impact()
topology = subs_impacted.any()
line = lines_impacted.any()
redispatching = (self._redispatch != 0.0).any()
redispatching = (np.abs(self._redispatch) >= 1e-7).any()
storage = self._modif_storage
curtailment = self._modif_curtailment
return injection, voltage, topology, line, redispatching, storage, curtailment
Expand Down Expand Up @@ -6135,7 +6135,7 @@ def limit_curtail_storage(self,
total_storage_consumed = res._storage_power.sum()

# curtailment
gen_curtailed = (res._curtail != -1) & cls.gen_renewable
gen_curtailed = (np.abs(res._curtail + 1) >= 1e-7) & cls.gen_renewable
gen_curtailed &= ( (obs.gen_p > res._curtail * cls.gen_pmax) | (obs.gen_p_before_curtail > obs.gen_p ))
gen_p_after_max = (res._curtail * cls.gen_pmax)[gen_curtailed]

Expand Down
12 changes: 6 additions & 6 deletions grid2op/Action/serializableActionSpace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,7 @@ def _custom_deepcopy_for_copy(self, new_obj):
new_obj._template_act = self.actionClass()

def _aux_get_back_to_ref_state_curtail(self, res, obs):
is_curtailed = obs.curtailment_limit != 1.0
is_curtailed = np.abs(obs.curtailment_limit - 1.0) >= 1e-7
if is_curtailed.any():
res["curtailment"] = []
if not self.supports_type("curtail"):
Expand Down Expand Up @@ -1546,7 +1546,7 @@ def _aux_get_back_to_ref_state_sub(self, res, obs):

def _aux_get_back_to_ref_state_redisp(self, res, obs, precision=1e-5):
# TODO this is ugly, probably slow and could definitely be optimized
notredisp_setpoint = obs.target_dispatch != 0.0
notredisp_setpoint = np.abs(obs.target_dispatch) >= 1e-7
if notredisp_setpoint.any():
need_redisp = np.nonzero(notredisp_setpoint)[0]
res["redispatching"] = []
Expand Down Expand Up @@ -1587,14 +1587,14 @@ def _aux_get_back_to_ref_state_redisp(self, res, obs, precision=1e-5):
continue
if obs.target_dispatch[gen_id] > 0.0:
if nb_act < nb_[gen_id] - 1 or (
rem[gen_id] == 0.0 and nb_act == nb_[gen_id] - 1
np.abs(rem[gen_id]) <= 1e-7 and nb_act == nb_[gen_id] - 1
):
reds[gen_id] = -obs.gen_max_ramp_down[gen_id]
else:
reds[gen_id] = -rem[gen_id]
else:
if nb_act < nb_[gen_id] - 1 or (
rem[gen_id] == 0.0 and nb_act == nb_[gen_id] - 1
np.abs(rem[gen_id]) <= 1e-7 and nb_act == nb_[gen_id] - 1
):
reds[gen_id] = obs.gen_max_ramp_up[gen_id]
else:
Expand Down Expand Up @@ -1658,14 +1658,14 @@ def _aux_get_back_to_ref_state_storage(
continue
if current_state[stor_id] > 0.0:
if nb_act < nb_[stor_id] - 1 or (
rem[stor_id] == 0.0 and nb_act == nb_[stor_id] - 1
np.abs(rem[stor_id]) <= 1e-7 and nb_act == nb_[stor_id] - 1
):
reds[stor_id] = -obs.storage_max_p_prod[stor_id]
else:
reds[stor_id] = -rem[stor_id]
else:
if nb_act < nb_[stor_id] - 1 or (
rem[stor_id] == 0.0 and nb_act == nb_[stor_id] - 1
np.abs(rem[stor_id]) <= 1e-7 and nb_act == nb_[stor_id] - 1
):
reds[stor_id] = obs.storage_max_p_absorb[stor_id]
else:
Expand Down
4 changes: 2 additions & 2 deletions grid2op/Backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,11 +744,11 @@ def set_thermal_limit(self, limits : Union[np.ndarray, Dict["str", float]]) -> N
if el in limits:
try:
tmp = dt_float(limits[el])
except:
except Exception as exc_:
raise BackendError(
'Impossible to convert data ({}) for powerline named "{}" into float '
"values".format(limits[el], el)
)
) from exc_
if tmp <= 0:
raise BackendError(
'New thermal limit for powerlines "{}" is not positive ({})'
Expand Down
2 changes: 1 addition & 1 deletion grid2op/Chronics/GSFFWFWM.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def _fix_maintenance_format(obj_with_maintenance):
)

# there are _maintenance and hazards only if the value in the file is not 0.
obj_with_maintenance.maintenance = obj_with_maintenance.maintenance != 0.0
obj_with_maintenance.maintenance = np.abs(obj_with_maintenance.maintenance) >= 1e-7
obj_with_maintenance.maintenance = obj_with_maintenance.maintenance.astype(dt_bool)

@staticmethod
Expand Down
1 change: 0 additions & 1 deletion grid2op/Chronics/fromOneEpisodeData.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,6 @@ def get_id(self) -> str:
else:
# TODO EpisodeData.path !!!
return ""
raise NotImplementedError()

def shuffle(self, shuffler=None):
# TODO
Expand Down
10 changes: 5 additions & 5 deletions grid2op/Chronics/gridStateFromFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ def _init_attrs(
self.hazards[:, line_id]
)

self.hazards = self.hazards != 0.0
self.hazards = np.abs(self.hazards) >= 1e-7
if maintenance is not None:
self.maintenance = copy.deepcopy(
maintenance.values[:, self._order_maintenance]
Expand All @@ -759,7 +759,7 @@ def _init_attrs(
] = self.get_maintenance_duration_1d(self.maintenance[:, line_id])

# there are _maintenance and hazards only if the value in the file is not 0.
self.maintenance = self.maintenance != 0.0
self.maintenance = np.abs(self.maintenance) >= 1e-7
self.maintenance = self.maintenance.astype(dt_bool)

def done(self):
Expand Down Expand Up @@ -1026,14 +1026,14 @@ def _convert_datetime(self, datetime_beg):
if not isinstance(datetime_beg, datetime):
try:
res = datetime.strptime(datetime_beg, "%Y-%m-%d %H:%M")
except:
except Exception as exc_:
try:
res = datetime.strptime(datetime_beg, "%Y-%m-%d")
except:
except Exception as exc_2:
raise ChronicsError(
'Impossible to convert "{}" to a valid datetime. Accepted format is '
'"%Y-%m-%d %H:%M"'.format(datetime_beg)
)
) from exc_2
return res

def _extract_array(self, nm):
Expand Down
Loading

0 comments on commit 0fc4848

Please sign in to comment.