epyt-flow 0.14.0__py3-none-any.whl → 0.14.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
epyt_flow/VERSION CHANGED
@@ -1 +1 @@
1
- 0.14.0
1
+ 0.14.2
@@ -23,6 +23,10 @@ class ScenarioControlEnv(ABC):
23
23
  autoreset : `bool`, optional
24
24
  If True, environment is automatically reset if terminated.
25
25
 
26
+ The default is False.
27
+ reapply_uncertainties_at_reset : `bool`, optional
28
+ If True, the uncertainties are re-applied to the original properties at each reset.
29
+
26
30
  The default is False.
27
31
 
28
32
  Attributes
@@ -36,7 +40,9 @@ class ScenarioControlEnv(ABC):
36
40
  _hydraulic_scada_data : :class:`~epyt_flow.simulation.scada.scada_data.ScadaData`, protected
37
41
  SCADA data from the hydraulic simulation -- only used if EPANET-MSX is used in the control scenario.
38
42
  """
39
- def __init__(self, scenario_config: ScenarioConfig, autoreset: bool = False, **kwds):
43
+ def __init__(self, scenario_config: ScenarioConfig, autoreset: bool = False,
44
+ reapply_uncertainties_at_reset: bool = False,
45
+ **kwds):
40
46
  if not isinstance(scenario_config, ScenarioConfig):
41
47
  raise TypeError("'scenario_config' must be an instance of " +
42
48
  "'epyt_flow.simulation.ScenarioConfig' " +
@@ -44,12 +50,15 @@ class ScenarioControlEnv(ABC):
44
50
  if not isinstance(autoreset, bool):
45
51
  raise TypeError("'autoreset' must be an instance of 'bool' " +
46
52
  f"but not of '{type(autoreset)}'")
53
+ if not isinstance(reapply_uncertainties_at_reset, bool):
54
+ raise TypeError("")
47
55
 
48
56
  self._scenario_config = scenario_config
49
57
  self._scenario_sim = None
50
58
  self._sim_generator = None
51
59
  self.__autoreset = autoreset
52
60
  self._hydraulic_scada_data = None
61
+ self.__reapply_uncertainties_at_reset = reapply_uncertainties_at_reset
53
62
 
54
63
  super().__init__(**kwds)
55
64
 
@@ -65,6 +74,18 @@ class ScenarioControlEnv(ABC):
65
74
  """
66
75
  return self.__autoreset
67
76
 
77
+ @property
78
+ def reapply_uncertainties_at_reset(self) -> bool:
79
+ """
80
+ True, if the uncertainties are re-applied to the original properties at each reset.
81
+
82
+ Returns
83
+ -------
84
+ `bool`
85
+ True, if the uncertainties are re-applied to the original properties at each reset.
86
+ """
87
+ return self.__reapply_uncertainties_at_reset
88
+
68
89
  def __enter__(self):
69
90
  return self
70
91
 
@@ -117,14 +138,16 @@ class ScenarioControlEnv(ABC):
117
138
  # Run hydraulic simulation first
118
139
  hyd_export = os.path.join(get_temp_folder(), f"epytflow_env_MSX_{uuid.uuid4()}.hyd")
119
140
  sim = self._scenario_sim.run_hydraulic_simulation
120
- self._hydraulic_scada_data = sim(hyd_export=hyd_export)
141
+ self._hydraulic_scada_data = sim(hyd_export=hyd_export,
142
+ reapply_uncertainties=self.__reapply_uncertainties_at_reset)
121
143
 
122
144
  # Run advanced quality analysis (EPANET-MSX) on top of the computed hydraulics
123
145
  gen = self._scenario_sim.run_advanced_quality_simulation_as_generator
124
146
  self._sim_generator = gen(hyd_export, support_abort=True)
125
147
  else:
126
148
  gen = self._scenario_sim.run_hydraulic_simulation_as_generator
127
- self._sim_generator = gen(support_abort=True)
149
+ self._sim_generator = gen(support_abort=True,
150
+ reapply_uncertainties=self.__reapply_uncertainties_at_reset)
128
151
 
129
152
  return self._next_sim_itr()
130
153
 
@@ -64,7 +64,7 @@ class SpeciesInjectionEvent(SystemEvent, JsonSerializable):
64
64
 
65
65
  self.__species_id = species_id
66
66
  self.__node_id = node_id
67
- self.__profile = profile
67
+ self._profile = profile
68
68
  self.__source_type = source_type
69
69
 
70
70
  super().__init__(**kwds)
@@ -103,7 +103,7 @@ class SpeciesInjectionEvent(SystemEvent, JsonSerializable):
103
103
  `numpy.ndarray`
104
104
  Pattern of the injection.
105
105
  """
106
- return deepcopy(self.__profile)
106
+ return deepcopy(self._profile)
107
107
 
108
108
  @property
109
109
  def source_type(self) -> int:
@@ -125,17 +125,17 @@ class SpeciesInjectionEvent(SystemEvent, JsonSerializable):
125
125
 
126
126
  def get_attributes(self) -> dict:
127
127
  return super().get_attributes() | {"species_id": self.__species_id,
128
- "node_id": self.__node_id, "profile": self.__profile,
128
+ "node_id": self.__node_id, "profile": self._profile,
129
129
  "source_type": self.__source_type}
130
130
 
131
131
  def __eq__(self, other) -> bool:
132
132
  return super().__eq__(other) and self.__species_id == other.species_id and \
133
- self.__node_id == other.node_id and np.all(self.__profile == other.profile) and \
133
+ self.__node_id == other.node_id and np.all(self._profile == other.profile) and \
134
134
  self.__source_type == other.source_type
135
135
 
136
136
  def __str__(self) -> str:
137
137
  return f"{super().__str__()} species_id: {self.__species_id} " +\
138
- f"node_id: {self.__node_id} profile: {self.__profile} source_type: {self.__source_type}"
138
+ f"node_id: {self.__node_id} profile: {self._profile} source_type: {self.__source_type}"
139
139
 
140
140
  def _get_pattern_id(self) -> str:
141
141
  return f"{self.__species_id}_{self.__node_id}"
@@ -160,7 +160,7 @@ class SpeciesInjectionEvent(SystemEvent, JsonSerializable):
160
160
  injection_time_start_idx = int(self.start_time / time_step)
161
161
 
162
162
  injection_pattern = None
163
- if len(self.__profile) == injection_pattern_length:
163
+ if len(self._profile) == injection_pattern_length:
164
164
  injection_pattern = self.profile
165
165
  else:
166
166
  injection_pattern = np.tile(self.profile,
@@ -93,7 +93,7 @@ class SensorFaultConstant(SensorFault, JsonSerializable):
93
93
  raise TypeError("'constant_shift' must be an instance of 'float' but no of " +
94
94
  f"'{type(constant_shift)}'")
95
95
 
96
- self.__constant_shift = constant_shift
96
+ self._constant_shift = constant_shift
97
97
 
98
98
  super().__init__(**kwds)
99
99
 
@@ -107,20 +107,20 @@ class SensorFaultConstant(SensorFault, JsonSerializable):
107
107
  `float`
108
108
  Constant that is added to the sensor reading.
109
109
  """
110
- return self.__constant_shift
110
+ return self._constant_shift
111
111
 
112
112
  def get_attributes(self) -> dict:
113
- return super().get_attributes() | {"constant_shift": self.__constant_shift}
113
+ return super().get_attributes() | {"constant_shift": self._constant_shift}
114
114
 
115
115
  def __eq__(self, other) -> bool:
116
- return super().__eq__(other) and self.__constant_shift == other.constant_shift
116
+ return super().__eq__(other) and self._constant_shift == other.constant_shift
117
117
 
118
118
  def __str__(self) -> str:
119
- return f"{type(self).__name__} {super().__str__()} constant: {self.__constant_shift}"
119
+ return f"{type(self).__name__} {super().__str__()} constant: {self._constant_shift}"
120
120
 
121
121
  def apply_sensor_fault(self, cur_multiplier: float, sensor_reading: float,
122
122
  cur_time: int) -> float:
123
- return sensor_reading + cur_multiplier * self.__constant_shift
123
+ return sensor_reading + cur_multiplier * self._constant_shift
124
124
 
125
125
 
126
126
  @serializable(SENSOR_FAULT_DRIFT_ID, ".epytflow_sensorfault_drift")
@@ -134,7 +134,7 @@ class SensorFaultDrift(SensorFault, JsonSerializable):
134
134
  Coefficient of the drift.
135
135
  """
136
136
  def __init__(self, coef: float, **kwds):
137
- self.__coef = coef
137
+ self._coef = coef
138
138
 
139
139
  super().__init__(**kwds)
140
140
 
@@ -148,20 +148,20 @@ class SensorFaultDrift(SensorFault, JsonSerializable):
148
148
  `float`
149
149
  Coefficient of the drift.
150
150
  """
151
- return self.__coef
151
+ return self._coef
152
152
 
153
153
  def get_attributes(self) -> dict:
154
- return super().get_attributes() | {"coef": self.__coef}
154
+ return super().get_attributes() | {"coef": self._coef}
155
155
 
156
156
  def __eq__(self, other) -> bool:
157
- return super().__eq__(other) and self.__coef == other.coef
157
+ return super().__eq__(other) and self._coef == other.coef
158
158
 
159
159
  def __str__(self) -> str:
160
- return f"{type(self).__name__} {super().__str__()} coef: {self.__coef}"
160
+ return f"{type(self).__name__} {super().__str__()} coef: {self._coef}"
161
161
 
162
162
  def apply_sensor_fault(self, cur_multiplier: float, sensor_reading: float,
163
163
  cur_time: int) -> float:
164
- return sensor_reading + cur_multiplier * (self.__coef * (cur_time - self.start_time))
164
+ return sensor_reading + cur_multiplier * (self._coef * (cur_time - self.start_time))
165
165
 
166
166
 
167
167
  @serializable(SENSOR_FAULT_GAUSSIAN_ID, ".epytflow_sensorfault_gaussian")
@@ -179,7 +179,7 @@ class SensorFaultGaussian(SensorFault, JsonSerializable):
179
179
  if not isinstance(std, float) or not std > 0:
180
180
  raise ValueError("'std' must be an instance of 'float' and be greater than 0")
181
181
 
182
- self.__std = std
182
+ self._std = std
183
183
 
184
184
  super().__init__(**kwds)
185
185
 
@@ -193,20 +193,20 @@ class SensorFaultGaussian(SensorFault, JsonSerializable):
193
193
  `float`
194
194
  Standard deviation of the Gaussian noise.
195
195
  """
196
- return self.__std
196
+ return self._std
197
197
 
198
198
  def get_attributes(self) -> dict:
199
- return super().get_attributes() | {"std": self.__std}
199
+ return super().get_attributes() | {"std": self._std}
200
200
 
201
201
  def __eq__(self, other) -> bool:
202
- return super().__eq__(other) and self.__std == other.std
202
+ return super().__eq__(other) and self._std == other.std
203
203
 
204
204
  def __str__(self) -> str:
205
- return f"{type(self).__name__} {super().__str__()} std: {self.__std}"
205
+ return f"{type(self).__name__} {super().__str__()} std: {self._std}"
206
206
 
207
207
  def apply_sensor_fault(self, cur_multiplier: float, sensor_reading: float,
208
208
  cur_time: int) -> float:
209
- return sensor_reading + cur_multiplier * np.random.normal(loc=0, scale=self.__std)
209
+ return sensor_reading + cur_multiplier * np.random.normal(loc=0, scale=self._std)
210
210
 
211
211
 
212
212
  @serializable(SENSOR_FAULT_PERCENTAGE_ID, ".epytflow_sensorfault_percentage",)
@@ -223,7 +223,7 @@ class SensorFaultPercentage(SensorFault, JsonSerializable):
223
223
  if not isinstance(coef, float) or not coef > 0:
224
224
  raise ValueError("'coef' must be an instance of 'float' and be greater than zero.")
225
225
 
226
- self.__coef = coef
226
+ self._coef = coef
227
227
 
228
228
  super().__init__(**kwds)
229
229
 
@@ -237,20 +237,20 @@ class SensorFaultPercentage(SensorFault, JsonSerializable):
237
237
  `float`
238
238
  Coefficient (percentage) of the shift.
239
239
  """
240
- return self.__coef
240
+ return self._coef
241
241
 
242
242
  def get_attributes(self) -> dict:
243
- return super().get_attributes() | {"coef": self.__coef}
243
+ return super().get_attributes() | {"coef": self._coef}
244
244
 
245
245
  def __eq__(self, other) -> bool:
246
- return super().__eq__(other) and self.__coef == other.coef
246
+ return super().__eq__(other) and self._coef == other.coef
247
247
 
248
248
  def __str__(self) -> str:
249
- return f"{type(self).__name__} {super().__str__()} coef: {self.__coef}"
249
+ return f"{type(self).__name__} {super().__str__()} coef: {self._coef}"
250
250
 
251
251
  def apply_sensor_fault(self, cur_multiplier: float, sensor_reading: float,
252
252
  cur_time: int) -> float:
253
- return sensor_reading + cur_multiplier * self.__coef * sensor_reading
253
+ return sensor_reading + cur_multiplier * self._coef * sensor_reading
254
254
 
255
255
 
256
256
  @serializable(SENSOR_FAULT_STUCKATZERO_ID, ".epytflow_sensorfault_zero")
@@ -13,7 +13,7 @@ class SystemEvent(Event):
13
13
  """
14
14
  def __init__(self, **kwds):
15
15
  self._epanet_api = None
16
- self.__exit_called = False
16
+ self._exit_called = False
17
17
 
18
18
  super().__init__(**kwds)
19
19
 
@@ -52,9 +52,9 @@ class SystemEvent(Event):
52
52
  if self.start_time <= cur_time < self.end_time:
53
53
  self.apply(cur_time)
54
54
  elif cur_time > self.end_time:
55
- if self.__exit_called is False:
55
+ if self._exit_called is False:
56
56
  self.exit(cur_time)
57
- self.__exit_called = True
57
+ self._exit_called = True
58
58
 
59
59
  def reset(self) -> None:
60
60
  """
@@ -167,7 +167,7 @@ class ScadaData(Serializable):
167
167
  raise TypeError("'sensor_readings_time' must be an instance of 'numpy.ndarray' " +
168
168
  f"but not of '{type(sensor_readings_time)}'")
169
169
  if warnings_code is None:
170
- warnings_code = [0] * len(sensor_readings_time)
170
+ warnings_code = np.array([0] * len(sensor_readings_time))
171
171
  else:
172
172
  if not isinstance(warnings_code, np.ndarray):
173
173
  raise TypeError("'warnings_code' must be an instance of 'numpy.ndarray' " +
@@ -1713,7 +1713,7 @@ class ScadaData(Serializable):
1713
1713
  self.__sensor_reading_events = sensor_reading_events
1714
1714
  self.__init()
1715
1715
 
1716
- def extract_time_window(self, start_time: int, end_time: int):
1716
+ def extract_time_window(self, start_time: int, end_time: int = None):
1717
1717
  """
1718
1718
  Extracts a time window of SCADA data from this SCADA data instance --
1719
1719
  i.e. creating a new SCADA data instance containing data from the requested
@@ -1966,9 +1966,12 @@ class ScenarioSimulator():
1966
1966
  self._adapt_to_network_changes()
1967
1967
 
1968
1968
  if self._model_uncertainty is not None:
1969
- if self.__uncertainties_applied is True and reapply_uncertainties is True:
1969
+ if self.__uncertainties_applied is False:
1970
1970
  self._model_uncertainty.apply(self.epanet_api)
1971
1971
  self.__uncertainties_applied = True
1972
+ elif self.__uncertainties_applied is True and reapply_uncertainties is True:
1973
+ self._model_uncertainty.undo(self.epanet_api)
1974
+ self._model_uncertainty.apply(self.epanet_api)
1972
1975
 
1973
1976
  for event in self._system_events:
1974
1977
  event.reset()
@@ -2006,8 +2009,8 @@ class ScenarioSimulator():
2006
2009
  with the hydraulic simulation.
2007
2010
 
2008
2011
  The default is False.
2009
- reapply_uncertainties: bool = False : `bool`, optional
2010
- If True, the uncertainties are re-applied.
2012
+ reapply_uncertainties: `bool`, optional
2013
+ If True, the uncertainties are re-applied on the original properties.
2011
2014
 
2012
2015
  The default is False.
2013
2016
 
@@ -2089,8 +2092,8 @@ class ScenarioSimulator():
2089
2092
  with the hydraulic simulation.
2090
2093
 
2091
2094
  The default is False.
2092
- reapply_uncertainties: bool = False : `bool`, optional
2093
- If True, the uncertainties are re-applied.
2095
+ reapply_uncertainties : `bool`, optional
2096
+ If True, the uncertainties are re-applied on the original properties.
2094
2097
 
2095
2098
  The default is False.
2096
2099
 
@@ -2511,8 +2514,8 @@ class ScenarioSimulator():
2511
2514
  will be stored -- this usually leads to a significant reduction in memory consumption.
2512
2515
 
2513
2516
  The default is False.
2514
- reapply_uncertainties: bool = False : `bool`, optional
2515
- If True, the uncertainties are re-applied.
2517
+ reapply_uncertainties : `bool`, optional
2518
+ If True, the uncertainties are re-applied on the original properties.
2516
2519
 
2517
2520
  The default is False.
2518
2521
 
@@ -2596,8 +2599,8 @@ class ScenarioSimulator():
2596
2599
  will be stored -- this usually leads to a significant reduction in memory consumption.
2597
2600
 
2598
2601
  The default is False.
2599
- reapply_uncertainties: bool = False : `bool`, optional
2600
- If True, the uncertainties are re-applied.
2602
+ reapply_uncertainties : `bool`, optional
2603
+ If True, the uncertainties are re-applied on the original properties.
2601
2604
 
2602
2605
  The default is False.
2603
2606
 
@@ -2778,8 +2781,8 @@ class ScenarioSimulator():
2778
2781
  will be stored -- this usually leads to a significant reduction in memory consumption.
2779
2782
 
2780
2783
  The default is False.
2781
- reapply_uncertainties: bool = False : `bool`, optional
2782
- If True, the uncertainties are re-applied.
2784
+ reapply_uncertainties: `bool`, optional
2785
+ If True, the uncertainties are re-applied on the original properties.
2783
2786
 
2784
2787
  The default is False.
2785
2788
 
epyt_flow/topology.py CHANGED
@@ -500,8 +500,15 @@ class NetworkTopology(nx.Graph, JsonSerializable):
500
500
  raise TypeError("Can not compare 'NetworkTopology' instance to " +
501
501
  f"'{type(other)}' instance")
502
502
 
503
+ adj_matrix = self.get_adj_matrix()
504
+ other_adj_matrix = other.get_adj_matrix()
505
+
503
506
  return super().__eq__(other) and \
504
- self.get_all_nodes() == other.get_all_nodes() \
507
+ self.name == other.name \
508
+ and not np.any(adj_matrix.data != other_adj_matrix.data) \
509
+ and not np.any(adj_matrix.indices != other_adj_matrix.indices) \
510
+ and not np.any(adj_matrix.indptr != other_adj_matrix.indptr) \
511
+ and self.get_all_nodes() == other.get_all_nodes() \
505
512
  and all(link_a[0] == link_b[0] and link_a[1] == link_b[1]
506
513
  for link_a, link_b in zip(self.get_all_links(), other.get_all_links())) \
507
514
  and self.__units == other.units \