diff --git a/modules/performREC/pyrecodes/PyReCode-REWET-preprocessor.py b/modules/performREC/pyrecodes/PyReCode-REWET-preprocessor.py index fd2489653..c951ec51a 100644 --- a/modules/performREC/pyrecodes/PyReCode-REWET-preprocessor.py +++ b/modules/performREC/pyrecodes/PyReCode-REWET-preprocessor.py @@ -1,4 +1,4 @@ -# Copyright (c) 2024 The Regents of the University of California +# Copyright (c) 2024 The Regents of the University of California # noqa: INP001 # Copyright (c) 2024 Leland Stanford Junior University # # This file is part of whale. @@ -37,19 +37,20 @@ """Provide Interface between REWET and PYReCoDes.""" -import random -import json import copy -import rewet +import json +import random from pathlib import Path -import wntrfr + import pandas as pd -from sklearn.cluster import KMeans +import rewet +import wntrfr from rewet.api import API +from sklearn.cluster import KMeans -TEMP_DIR = "./" -RESULT_DIR = "./rewet_result" -INPUT_FILE_DIR = "./" +TEMP_DIR = './' +RESULT_DIR = './rewet_result' +INPUT_FILE_DIR = './' class REWETPyReCoDes: @@ -105,7 +106,7 @@ def system_state(self, state, damage, inp_file, damage_time=0): # couple building to the demand nodes self.couple_buildings_to_demand_nodes(state) - def system_performance(self, state, damage, current_time, next_time): + def system_performance(self, state, current_time, next_time): """ Assess the system functionality. @@ -150,9 +151,7 @@ def system_performance(self, state, damage, current_time, next_time): self.rewet.save_result() # Get result - building_satisfaction = self.get_building_data_satisfaction( - method="mean" - ) + building_satisfaction = self.get_building_data_satisfaction(method='mean') self.building_satisfaction = building_satisfaction return building_satisfaction @@ -175,7 +174,7 @@ def read_inp_file(self, inp_file): self.inp_file_path = self.inp_file_path.resolve() if not self.inp_file_path.exists(): - raise ValueError(f"There inp file does not exists: {inp_file}") + raise ValueError(f'There inp file does not exists: {inp_file}') # noqa: EM102, TRY003 self.inp_file_path = str(self.inp_file_path) @@ -189,11 +188,9 @@ def read_inp_file(self, inp_file): if node_name not in self.nodes: self.nodes[node_name] = {} - self.nodes[node_name][ - "initial_demand" - ] = node_demand_base_value + self.nodes[node_name]['initial_demand'] = node_demand_base_value - self.nodes[node_name]["coordinates"] = node.coordinates + self.nodes[node_name]['coordinates'] = node.coordinates def set_asset_data(self, state): """ @@ -214,8 +211,8 @@ def set_asset_data(self, state): None. """ - wdn_state = state["WaterDistributionNetwork"] - wdn_state = wdn_state.get("Pipe", []) + wdn_state = state['WaterDistributionNetwork'] + wdn_state = wdn_state.get('Pipe', []) for asset_type, asset_type_data in state.items(): if asset_type not in self.asset_information: @@ -228,23 +225,23 @@ def set_asset_data(self, state): self.asset_information[asset_type][sub_asset_type] = {} for element_key, element_data in sub_asset_type_data.items(): - asset_id = element_data["GeneralInformation"]["AIM_id"] + asset_id = element_data['GeneralInformation']['AIM_id'] if asset_id != element_key: - raise ValueError( - "The rationality behidd the workdflow" - "is that oth aim-id and keys be the" - "same" + raise ValueError( # noqa: TRY003 + 'The rationality behidd the workdflow' # noqa: EM101 + 'is that oth aim-id and keys be the' + 'same' ) - self.asset_information[asset_type][sub_asset_type][ - asset_id - ] = element_data["GeneralInformation"] + self.asset_information[asset_type][sub_asset_type][asset_id] = ( + element_data['GeneralInformation'] + ) - building_state = state["Buildings"]["Building"] + building_state = state['Buildings']['Building'] for building_id, each_building in building_state.items(): - population = each_building["GeneralInformation"]["Population"] - population_ratio = each_building.get("Population_Ratio", None) + population = each_building['GeneralInformation']['Population'] + population_ratio = each_building.get('Population_Ratio', None) if population_ratio is not None: ratio = population_ratio @@ -252,14 +249,14 @@ def set_asset_data(self, state): ratio = 1 cur_building = {} - cur_building["initial_population_ratio"] = ratio - cur_building["population_ratio"] = ratio - cur_building["initial_population"] = population - cur_building["population"] = population + cur_building['initial_population_ratio'] = ratio + cur_building['population_ratio'] = ratio + cur_building['initial_population'] = population + cur_building['population'] = population self.buildings[building_id] = cur_building - def update_state_with_damages(self, damage, damage_time, state): + def update_state_with_damages(self, damage, damage_time, state): # noqa: ARG002 """ Update the state dic with damages. @@ -282,21 +279,19 @@ def update_state_with_damages(self, damage, damage_time, state): None. """ - damage = damage["WaterDistributionNetwork"] - pipe_damage = damage.get("Pipe", []) + damage = damage['WaterDistributionNetwork'] + pipe_damage = damage.get('Pipe', []) for asset_id, damage_location in pipe_damage.items(): - damage_location_info = damage_location["Damage"] + damage_location_info = damage_location['Damage'] aggregate_keys = [ - key for key in damage_location_info if "aggregate-" in key + key for key in damage_location_info if 'aggregate-' in key ] aggregate_keys.sort() - aggregate_results = [ - damage_location_info[key] for key in aggregate_keys - ] + aggregate_results = [damage_location_info[key] for key in aggregate_keys] segment_sizes = len(aggregate_results) segment_step = 1 / segment_sizes @@ -307,13 +302,11 @@ def update_state_with_damages(self, damage, damage_time, state): for damage_val in aggregate_results: if damage_val > 0: if damage_val == 1: - damage_type = "leak" - elif damage_val == 2: - damage_type = "break" + damage_type = 'leak' + elif damage_val == 2: # noqa: PLR2004 + damage_type = 'break' else: - raise ValueError( - "The damage type must be either 1 or 2" - ) + raise ValueError('The damage type must be either 1 or 2') # noqa: EM101, TRY003 else: continue @@ -321,22 +314,18 @@ def update_state_with_damages(self, damage, damage_time, state): cur_pipe_damage_location_list.append(cur_loc) cur_pipe_damage_location_type.append(damage_type) - wdn_state = state["WaterDistributionNetwork"] - pipe_state = wdn_state.get("Pipe") + wdn_state = state['WaterDistributionNetwork'] + pipe_state = wdn_state.get('Pipe') - if "Damage" in pipe_state[asset_id]: - raise ValueError( - f"Damage is already exist for Pipe " f"{asset_id}" - ) + if 'Damage' in pipe_state[asset_id]: + raise ValueError(f'Damage is already exist for Pipe ' f'{asset_id}') # noqa: EM102, TRY003 - pipe_state[asset_id]["Damage"] = dict() - pipe_state[asset_id]["Damage"][ - "Location" - ] = cur_pipe_damage_location_list + pipe_state[asset_id]['Damage'] = dict() # noqa: C408 + pipe_state[asset_id]['Damage']['Location'] = ( + cur_pipe_damage_location_list + ) - pipe_state[asset_id]["Damage"][ - "Type" - ] = cur_pipe_damage_location_type + pipe_state[asset_id]['Damage']['Type'] = cur_pipe_damage_location_type def set_rewet_damage_from_state(self, state, damage_time): """ @@ -359,25 +348,23 @@ def set_rewet_damage_from_state(self, state, damage_time): None. """ - state = state["WaterDistributionNetwork"] - pipe_damage = state.get("Pipe", []) + state = state['WaterDistributionNetwork'] + pipe_damage = state.get('Pipe', []) damage_list = [] for asset_id, pipe_info in pipe_damage.items(): - damage_location_info = pipe_info["Damage"] - damage_location_list = damage_location_info["Location"] - damage_type_list = damage_location_info["Type"] + damage_location_info = pipe_info['Damage'] + damage_location_list = damage_location_info['Location'] + damage_type_list = damage_location_info['Type'] if len(damage_location_list) != len(damage_type_list): - raise ValueError( - "The size of types and locationis not the" " same." - ) + raise ValueError('The size of types and locationis not the same.') # noqa: EM101, TRY003 segment_sizes = len(damage_location_list) - pipe_id = self.asset_information["WaterDistributionNetwork"][ - "Pipe" - ][asset_id]["InpID"] + pipe_id = self.asset_information['WaterDistributionNetwork']['Pipe'][ + asset_id + ]['InpID'] for c in range(segment_sizes): cur_loc = damage_location_list[c] @@ -385,10 +372,10 @@ def set_rewet_damage_from_state(self, state, damage_time): damage_list.append( { - "pipe_id": pipe_id, - "damage_loc": cur_loc, - "type": damage_type, - "Material": "CI", + 'pipe_id': pipe_id, + 'damage_loc': cur_loc, + 'type': damage_type, + 'Material': 'CI', } ) @@ -396,21 +383,19 @@ def set_rewet_damage_from_state(self, state, damage_time): self.pipe_damage = pd.Series( data=damage_list, index=[damage_time for val in damage_list], - dtype="O", + dtype='O', ) - self.node_damage = pd.Series(dtype="O") + self.node_damage = pd.Series(dtype='O') - self.pump_damage = pd.Series(dtype="O") + self.pump_damage = pd.Series(dtype='O') - self.tank_damage = pd.Series(dtype="O") + self.tank_damage = pd.Series(dtype='O') if damage_time in self.damage_state: - raise ValueError( - f"Time {damage_time} still exists in" f" damage state." - ) + raise ValueError(f'Time {damage_time} still exists in damage state.') # noqa: EM102, TRY003 - def couple_buildings_to_demand_nodes(self, state): + def couple_buildings_to_demand_nodes(self, state): # noqa: C901 """ Couple building to the demand nodes based on their coordinates. @@ -424,18 +409,18 @@ def couple_buildings_to_demand_nodes(self, state): None. """ - building_state = state["Buildings"]["Building"] + building_state = state['Buildings']['Building'] building_id_list = [] for building_id, each_building in building_state.items(): - location = each_building["GeneralInformation"]["location"] - coordinate = (location["latitude"], location["longitude"]) + location = each_building['GeneralInformation']['location'] + coordinate = (location['latitude'], location['longitude']) building_id_list.append(building_id) self.building_coordinates.append(coordinate) demand_node_coordinate_list = [ - val["coordinates"] for key, val in self.nodes.items() + val['coordinates'] for key, val in self.nodes.items() ] demand_node_name_list = [key for key, val in self.nodes.items()] @@ -466,17 +451,17 @@ def couple_buildings_to_demand_nodes(self, state): for node_name in self.demand_node_to_building: building_name_list = self.demand_node_to_building[node_name] population_list = [ - self.buildings[bldg_id]["initial_population"] + self.buildings[bldg_id]['initial_population'] for bldg_id in building_name_list ] total_initial_population = sum(population_list) cur_node = self.nodes[node_name] - initial_node_demand = cur_node["initial_demand"] + initial_node_demand = cur_node['initial_demand'] if initial_node_demand == 0 and total_initial_population > 0: - Warning( + Warning( # noqa: PLW0133 f"Initial demand for node {node_name} is 0." f" Thus, the demand ratio in buildidng(s)" f" {repr(building_name_list).strip('[').strip(']')}" @@ -485,7 +470,7 @@ def couple_buildings_to_demand_nodes(self, state): ) if total_initial_population == 0: - Warning( + Warning( # noqa: PLW0133 f"The population assigned to {node_name} is 0." f" Thus, the demand ratio in buildidng(s)" f" {repr(building_name_list).strip('[').strip(']')}" @@ -497,14 +482,12 @@ def couple_buildings_to_demand_nodes(self, state): # For more clarity, we name the variable "initial_population". # It does not mean that there will be ffdifferent population in # the course of recovery - self.nodes[node_name][ - "initial_population" - ] = total_initial_population + self.nodes[node_name]['initial_population'] = total_initial_population - self.nodes[node_name]["initial_node_demand"] = initial_node_demand + self.nodes[node_name]['initial_node_demand'] = initial_node_demand for bldg_id in building_name_list: - pop = self.buildings[bldg_id]["initial_population"] + pop = self.buildings[bldg_id]['initial_population'] if total_initial_population != 0: cur_bldg_initial_demand = ( @@ -513,9 +496,7 @@ def couple_buildings_to_demand_nodes(self, state): else: cur_bldg_initial_demand = None - self.buildings[bldg_id][ - "initial_demand" - ] = cur_bldg_initial_demand + self.buildings[bldg_id]['initial_demand'] = cur_bldg_initial_demand def save_damage(self, state, current_time): """ @@ -533,16 +514,16 @@ def save_damage(self, state, current_time): None. """ - pipe_damage_file_name = "temp_pipe_damage_file.pkl" - node_damage_file_name = "node_pipe_damage_file.pkl" - tank_damage_file_name = "tank_pipe_damage_file.pkl" - pump_damage_file_name = "pump_pipe_damage_file.pkl" + pipe_damage_file_name = 'temp_pipe_damage_file.pkl' + node_damage_file_name = 'node_pipe_damage_file.pkl' + tank_damage_file_name = 'tank_pipe_damage_file.pkl' + pump_damage_file_name = 'pump_pipe_damage_file.pkl' pipe_path = Path(TEMP_DIR) / pipe_damage_file_name node_path = Path(TEMP_DIR) / node_damage_file_name tank_path = Path(TEMP_DIR) / tank_damage_file_name pump_path = Path(TEMP_DIR) / pump_damage_file_name - list_path = Path(TEMP_DIR) / "list.xlsx" + list_path = Path(TEMP_DIR) / 'list.xlsx' pipe_path = str(pipe_path) node_path = str(node_path) @@ -553,15 +534,15 @@ def save_damage(self, state, current_time): self.set_rewet_damage_from_state(state, current_time) if current_time in self.damage_state: - raise ValueError( - f"The time {current_time} is already in " f" damage state." + raise ValueError( # noqa: TRY003 + f'The time {current_time} is already in ' f' damage state.' # noqa: EM102 ) self.damage_state[current_time] = { - "Pipe": self.pipe_damage, - "Node": self.node_damage, - "Pump": self.pump_damage, - "Tank": self.tank_damage, + 'Pipe': self.pipe_damage, + 'Node': self.node_damage, + 'Pump': self.pump_damage, + 'Tank': self.tank_damage, } self.pipe_damage.to_pickle(pipe_path) @@ -570,19 +551,19 @@ def save_damage(self, state, current_time): self.tank_damage.to_pickle(tank_path) scn_postfix_list = random.choices( - ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "L", "M"], k=0 + ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'L', 'M'], k=0 ) - scn_postfix = "" + scn_postfix = '' for p in scn_postfix_list: scn_postfix += p - self.damage_file_list["Scenario Name"] = "SCN_" + scn_postfix - self.damage_file_list["Pipe Damage"] = pipe_damage_file_name - self.damage_file_list["Nodal Damage"] = node_damage_file_name - self.damage_file_list["Pump Damage"] = tank_damage_file_name - self.damage_file_list["Tank Damage"] = pump_damage_file_name - self.damage_file_list["Probability"] = 1 + self.damage_file_list['Scenario Name'] = 'SCN_' + scn_postfix + self.damage_file_list['Pipe Damage'] = pipe_damage_file_name + self.damage_file_list['Nodal Damage'] = node_damage_file_name + self.damage_file_list['Pump Damage'] = tank_damage_file_name + self.damage_file_list['Tank Damage'] = pump_damage_file_name + self.damage_file_list['Probability'] = 1 damage_file_list = pd.DataFrame.from_dict([self.damage_file_list]) damage_file_list.to_excel(list_path) @@ -612,25 +593,23 @@ def run_performance(self, current_time, next_time): """ if current_time > next_time: - raise ValueError( - "Current tiime cannot be bigger than the next" "time" - ) + raise ValueError('Current tiime cannot be bigger than the next' 'time') # noqa: EM101, TRY003 if abs(next_time - current_time) % self.hydraulic_time_step != 0: - raise ValueError( - "next_time - current_time must be a factor of " - "the hydraulci time step." + raise ValueError( # noqa: TRY003 + 'next_time - current_time must be a factor of ' # noqa: EM101 + 'the hydraulci time step.' ) status = self.rewet.initiate(current_time=current_time, debug=True) if status != 0: - raise ValueError(f"There is an error: {status}") + raise ValueError(f'There is an error: {status}') # noqa: EM102, TRY003 self.rewet.apply_damage(current_time, 0.001) time_step = next_time - current_time status = self.rewet.run_hydraulic_simulation(time_step) if status != 0: - raise ValueError(f"There is an error: {status}") + raise ValueError(f'There is an error: {status}') # noqa: EM102, TRY003 return dict @@ -660,31 +639,29 @@ def make_rewet_inputs(self, current_time, next_time): list_file_path = Path(self.list_path) list_file_path = list_file_path.resolve() if not list_file_path.exists(): - raise ValueError( - f"The list file does not exists: " f"{str(list_file_path)}" + raise ValueError( # noqa: TRY003 + f'The list file does not exists: ' f'{list_file_path!s}' # noqa: EM102 ) list_file_path = str(list_file_path) temp_dir = Path(TEMP_DIR).resolve() if not temp_dir.exists(): - raise ValueError( - f"The temp directory does not exists: " f"{str(temp_dir)}" - ) + raise ValueError(f'The temp directory does not exists: ' f'{temp_dir!s}') # noqa: EM102, TRY003 temp_dir = str(temp_dir) - settings["RUN_TIME"] = run_time - settings["minimum_simulation_time"] = run_time - settings["result_directory"] = RESULT_DIR - settings["temp_directory"] = temp_dir - settings["WN_INP"] = self.inp_file_path - settings["pipe_damage_file_list"] = list_file_path - settings["pipe_damage_file_directory"] = temp_dir - settings["Restoration_on"] = False - settings["Pipe_damage_input_method"] = "pickle" - - input_file_path = Path(INPUT_FILE_DIR) / "rewet_input.json" + settings['RUN_TIME'] = run_time + settings['minimum_simulation_time'] = run_time + settings['result_directory'] = RESULT_DIR + settings['temp_directory'] = temp_dir + settings['WN_INP'] = self.inp_file_path + settings['pipe_damage_file_list'] = list_file_path + settings['pipe_damage_file_directory'] = temp_dir + settings['Restoration_on'] = False + settings['Pipe_damage_input_method'] = 'pickle' + + input_file_path = Path(INPUT_FILE_DIR) / 'rewet_input.json' input_file_path = input_file_path.resolve() - with open(input_file_path, "wt") as f: + with open(input_file_path, 'w') as f: # noqa: PTH123 json.dump(settings, f, indent=4) self.input_file_path = str(input_file_path) @@ -711,14 +688,14 @@ def get_building_data_satisfaction(self, method): """ demand_sat = self.rewet.get_satisfied_demand_ratio() - if method.upper() == "MEAN": + if method.upper() == 'MEAN': demand_sat = demand_sat.mean() - elif method.upper() == "MAX": + elif method.upper() == 'MAX': demand_sat = demand_sat.max() - elif method.upper() == "MIN": + elif method.upper() == 'MIN': demand_sat = demand_sat.min() else: - raise ValueError(f"The method is not recognizable: {method}") + raise ValueError(f'The method is not recognizable: {method}') # noqa: EM102, TRY003 building_demand_satisfaction_ratio = {} @@ -729,9 +706,7 @@ def get_building_data_satisfaction(self, method): zip(building_names, [node_demand_ratio] * len(building_names)) ) - building_demand_satisfaction_ratio.update( - cur_satisified_demand_building - ) + building_demand_satisfaction_ratio.update(cur_satisified_demand_building) return building_demand_satisfaction_ratio @@ -750,40 +725,32 @@ def set_new_demand(self, state): """ for node_name in self.demand_node_to_building: - # cur_node = self.nodes[node_name] - total_initial_population = self.nodes[node_name][ - "initial_population" - ] + total_initial_population = self.nodes[node_name]['initial_population'] if not total_initial_population > 0: continue building_name_list = self.demand_node_to_building[node_name] - building = state["Buildings"]["Building"] + building = state['Buildings']['Building'] node_new_demand = 0 for bldg_id in building_name_list: + cur_bldg_initial_demand = self.buildings[bldg_id]['initial_demand'] - cur_bldg_initial_demand = self.buildings[bldg_id][ - "initial_demand" + cur_bldg_deamnd_ratio = building[bldg_id]['GeneralInformation'][ + 'Population_Ratio' ] - cur_bldg_deamnd_ratio = building[bldg_id][ - "GeneralInformation" - ]["Population_Ratio"] - - cur_bldg_new_deamnd = ( - cur_bldg_deamnd_ratio * cur_bldg_initial_demand - ) + cur_bldg_new_deamnd = cur_bldg_deamnd_ratio * cur_bldg_initial_demand - self.buildings[bldg_id]["current_demand"] = cur_bldg_new_deamnd + self.buildings[bldg_id]['current_demand'] = cur_bldg_new_deamnd node_new_demand += cur_bldg_new_deamnd - self.nodes[node_name]["current_demand"] = node_new_demand + self.nodes[node_name]['current_demand'] = node_new_demand node = self.wn.get_node(node_name) node.demand_timeseries_list[0].base_value = node_new_demand @@ -801,17 +768,17 @@ def get_rewet_hydraulic_basic_setting(): settings = rewet.Input.Settings.Settings() settings_dict = settings.process.settings - return settings_dict + return settings_dict # noqa: RET504 -if __name__ == "__main__": - with open("Results_det.json", "rt") as f: +if __name__ == '__main__': + with open('Results_det.json') as f: # noqa: PTH123 state = json.load(f) - with open("Results_0.json", "rt") as f: + with open('Results_0.json') as f: # noqa: PTH123 damage = json.load(f) - inp_file = "waterNetwork.inp" + inp_file = 'waterNetwork.inp' interface = REWETPyReCoDes() interface.system_state(state, damage, inp_file) diff --git a/modules/performREC/pyrecodes/damage_convertor.py b/modules/performREC/pyrecodes/damage_convertor.py index a3aa1aa57..ea4f2f013 100644 --- a/modules/performREC/pyrecodes/damage_convertor.py +++ b/modules/performREC/pyrecodes/damage_convertor.py @@ -1,4 +1,4 @@ -# Copyright (c) 2024 The Regents of the University of California +# Copyright (c) 2024 The Regents of the University of California # noqa: INP001 # Copyright (c) 2024 Leland Stanford Junior University # # This file is part of whale. @@ -39,15 +39,14 @@ import os from pathlib import Path + import pandas as pd import preprocessorIO CBIG_int = int(1e9) -def createPipeDamageInputForREWET( - pipe_damage_data, run_dir, event_time, sc_geojson -): +def createPipeDamageInputForREWET(pipe_damage_data, run_dir, event_time, sc_geojson): # noqa: N802 """ Create REWET-style piep damage file. @@ -73,42 +72,40 @@ def createPipeDamageInputForREWET( REWET-style pipe damage file. """ - pipe_id_list = [key for key in pipe_damage_data] + pipe_id_list = [key for key in pipe_damage_data] # noqa: C416 damage_list = [] damage_time = event_time sc_geojson_file = preprocessorIO.readJSONFile(sc_geojson) pipe_data = [ ss - for ss in sc_geojson_file["features"] - if ss["properties"]["type"] == "Pipe" + for ss in sc_geojson_file['features'] + if ss['properties']['type'] == 'Pipe' ] - pipe_index = [str(ss["id"]) for ss in pipe_data] - pipe_id = [ss["properties"]["InpID"] for ss in pipe_data] + pipe_index = [str(ss['id']) for ss in pipe_data] + pipe_id = [ss['properties']['InpID'] for ss in pipe_data] pipe_index_to_id = dict(zip(pipe_index, pipe_id)) for pipe_id in pipe_id_list: cur_data = pipe_damage_data[pipe_id] - cur_damage = cur_data["Damage"] + cur_damage = cur_data['Damage'] # cur_demand = cur_data["Demand"] aim_data = findAndReadAIMFile( pipe_id, - os.path.join("Results", "WaterDistributionNetwork", "Pipe"), + os.path.join('Results', 'WaterDistributionNetwork', 'Pipe'), # noqa: PTH118 run_dir, ) - material = aim_data["GeneralInformation"].get("Material", None) + material = aim_data['GeneralInformation'].get('Material', None) if material is None: # raise ValueError("Material is none") - material = "CI" + material = 'CI' aggregates_list = [ - cur_agg - for cur_agg in list(cur_damage.keys()) - if "aggregate" in cur_agg + cur_agg for cur_agg in list(cur_damage.keys()) if 'aggregate' in cur_agg ] segment_sizes = len(aggregates_list) segment_step = 1 / segment_sizes @@ -118,11 +115,11 @@ def createPipeDamageInputForREWET( damage_val = cur_damage[cur_agg] if damage_val > 0: if damage_val == 1: - damage_type = "leak" - elif damage_val == 2: - damage_type = "break" + damage_type = 'leak' + elif damage_val == 2: # noqa: PLR2004 + damage_type = 'break' else: - raise ValueError("The damage type must be eother 1 or 2") + raise ValueError('The damage type must be eother 1 or 2') # noqa: EM101, TRY003 else: continue @@ -131,24 +128,24 @@ def createPipeDamageInputForREWET( c += 1 damage_list.append( { - "pipe_id": pipe_index_to_id[pipe_id], - "damage_loc": cur_loc, - "type": damage_type, - "Material": material, + 'pipe_id': pipe_index_to_id[pipe_id], + 'damage_loc': cur_loc, + 'type': damage_type, + 'Material': material, } ) damage_list.reverse() pipe_damage_list = pd.Series( - data=damage_list, index=[damage_time for val in damage_list], dtype="O" + data=damage_list, index=[damage_time for val in damage_list], dtype='O' ) # REWET_input_data["Pipe_damage_list"] = pipe_damage_list # REWET_input_data["AIM"] = aim_data - return pipe_damage_list + return pipe_damage_list # noqa: RET504 -def createNodeDamageInputForREWET(node_damage_data, run_dir, event_time): +def createNodeDamageInputForREWET(node_damage_data, run_dir, event_time): # noqa: N802 """ Create REWET-style node damage file. @@ -167,7 +164,7 @@ def createNodeDamageInputForREWET(node_damage_data, run_dir, event_time): REWET-style node damage file. """ - node_id_list = [key for key in node_damage_data] + node_id_list = [key for key in node_damage_data] # noqa: C416 damage_list = [] damage_time = event_time @@ -175,9 +172,7 @@ def createNodeDamageInputForREWET(node_damage_data, run_dir, event_time): for node_id in node_id_list: cur_damage = node_damage_data[node_id] aggregates_list = [ - cur_agg - for cur_agg in list(cur_damage.keys()) - if "aggregate" in cur_agg + cur_agg for cur_agg in list(cur_damage.keys()) if 'aggregate' in cur_agg ] if len(aggregates_list) == 0: @@ -185,34 +180,34 @@ def createNodeDamageInputForREWET(node_damage_data, run_dir, event_time): cur_data = node_damage_data[node_id] - cur_damage = cur_data["Damage"] + cur_damage = cur_data['Damage'] # cur_demand = cur_data["Demand"] aim_data = findAndReadAIMFile( node_id, - os.path.join("Results", "WaterDistributionNetwork", "Node"), + os.path.join('Results', 'WaterDistributionNetwork', 'Node'), # noqa: PTH118 run_dir, ) - total_length = aim_data["GeneralInformation"].get("Total_length", None) - total_number_of_damages = cur_damage["aggregate"] + total_length = aim_data['GeneralInformation'].get('Total_length', None) + total_number_of_damages = cur_damage['aggregate'] damage_list.append( { - "node_name": node_id, - "number_of_damages": total_number_of_damages, - "node_Pipe_Length": total_length, + 'node_name': node_id, + 'number_of_damages': total_number_of_damages, + 'node_Pipe_Length': total_length, } ) node_damage_list = pd.Series( - data=damage_list, index=[damage_time for val in damage_list], dtype="O" + data=damage_list, index=[damage_time for val in damage_list], dtype='O' ) - return node_damage_list + return node_damage_list # noqa: RET504 -def createPumpDamageInputForREWET(pump_damage_data, REWET_input_data): +def createPumpDamageInputForREWET(pump_damage_data, REWET_input_data): # noqa: N802, N803 """ Create REWET-style pump damage file. @@ -229,16 +224,16 @@ def createPumpDamageInputForREWET(pump_damage_data, REWET_input_data): REWET-style pump damage file. """ - pump_id_list = [key for key in pump_damage_data] + pump_id_list = [key for key in pump_damage_data] # noqa: C416 damage_list = [] - damage_time = REWET_input_data["event_time"] + damage_time = REWET_input_data['event_time'] for pump_id in pump_id_list: cur_data = pump_damage_data[pump_id] - cur_damage = cur_data["Damage"] - cur_repair_time = cur_data["Repair"] + cur_damage = cur_data['Damage'] + cur_repair_time = cur_data['Repair'] if cur_damage == 0: continue # cur_damage_state = 0 means undamaged pump @@ -253,19 +248,19 @@ def createPumpDamageInputForREWET(pump_damage_data, REWET_input_data): # restore_time = getPumpRetsoreTime(cur_damage) damage_list.append( { - "pump_id": pump_id, - "time": damage_time, - "Restore_time": cur_repair_time, + 'pump_id': pump_id, + 'time': damage_time, + 'Restore_time': cur_repair_time, } ) pump_damage_list = pd.Series( index=[damage_time for val in damage_list], data=damage_list ) - return pump_damage_list + return pump_damage_list # noqa: RET504 -def createTankDamageInputForREWET(tank_damage_data, REWET_input_data): +def createTankDamageInputForREWET(tank_damage_data, REWET_input_data): # noqa: N802, N803 """ Create REWET-style Tank damage file. @@ -281,25 +276,25 @@ def createTankDamageInputForREWET(tank_damage_data, REWET_input_data): tank_damage_list : Pandas Series REWET-style tank damage file. """ - tank_id_list = [key for key in tank_damage_data] + tank_id_list = [key for key in tank_damage_data] # noqa: C416 damage_list = [] - damage_time = REWET_input_data["event_time"] + damage_time = REWET_input_data['event_time'] for tank_id in tank_id_list: cur_data = tank_damage_data[tank_id] - cur_damage = cur_data["Damage"] - cur_repair_time = cur_data["Repair"] + cur_damage = cur_data['Damage'] + cur_repair_time = cur_data['Repair'] if cur_damage == 0: continue # cur_damage_state = 0 meeans undamged tank damage_list.append( { - "tank_id": tank_id, - "time": damage_time, - "Restore_time": cur_repair_time, + 'tank_id': tank_id, + 'time': damage_time, + 'Restore_time': cur_repair_time, } ) @@ -307,10 +302,10 @@ def createTankDamageInputForREWET(tank_damage_data, REWET_input_data): index=[damage_time for val in damage_list], data=damage_list ) - return tank_damage_list + return tank_damage_list # noqa: RET504 -def findAndReadAIMFile(asset_id, asset_type, run_dir): +def findAndReadAIMFile(asset_id, asset_type, run_dir): # noqa: N802 """ Find and read the AIM file for an asset. @@ -333,15 +328,15 @@ def findAndReadAIMFile(asset_id, asset_type, run_dir): run_dir, asset_type, str(asset_id), - "templatedir", - f"{asset_id}-AIM.json", + 'templatedir', + f'{asset_id}-AIM.json', ) aim_file_data = preprocessorIO.readJSONFile(str(file_path)) - return aim_file_data + return aim_file_data # noqa: RET504 # Not UsedWE WILL GET IT FROM PELICUN -def getPumpRetsoreTime(damage_state): +def getPumpRetsoreTime(damage_state): # noqa: N802 """ Provide the restore time. @@ -365,7 +360,7 @@ def getPumpRetsoreTime(damage_state): """ if damage_state == 1: restore_time = int(3 * 24 * 3600) - elif damage_state == 2: + elif damage_state == 2: # noqa: PLR2004 restore_time = int(7 * 24 * 3600) else: restore_time = CBIG_int @@ -374,7 +369,7 @@ def getPumpRetsoreTime(damage_state): # NOT USED! WE WILL GET IT FROM PELICUN -def getTankRetsoreTime(tank_type, damage_state): +def getTankRetsoreTime(tank_type, damage_state): # noqa: ARG001, N802 """ Provide the restore time. @@ -400,7 +395,7 @@ def getTankRetsoreTime(tank_type, damage_state): """ if damage_state == 1: restore_time = int(3 * 24 * 3600) - elif damage_state == 2: + elif damage_state == 2: # noqa: PLR2004 restore_time = int(7 * 24 * 3600) else: restore_time = CBIG_int @@ -408,7 +403,7 @@ def getTankRetsoreTime(tank_type, damage_state): return restore_time -def readDamagefile(file_addr, run_dir, event_time, sc_geojson): +def readDamagefile(file_addr, run_dir, event_time, sc_geojson): # noqa: N802 """ Read PELICUN damage files. and create REWET-Style damage. @@ -436,40 +431,40 @@ def readDamagefile(file_addr, run_dir, event_time, sc_geojson): damage_data = preprocessorIO.readJSONFile(file_addr) - wn_damage_data = damage_data["WaterDistributionNetwork"] + wn_damage_data = damage_data['WaterDistributionNetwork'] - if "Pipe" in wn_damage_data: + if 'Pipe' in wn_damage_data: pipe_damage_data = createPipeDamageInputForREWET( - wn_damage_data["Pipe"], run_dir, event_time, sc_geojson + wn_damage_data['Pipe'], run_dir, event_time, sc_geojson ) else: - pipe_damage_data = pd.Series(dtype="O") + pipe_damage_data = pd.Series(dtype='O') - if "Tank" in wn_damage_data: + if 'Tank' in wn_damage_data: tank_damage_data = createTankDamageInputForREWET( - wn_damage_data["Tank"], run_dir, event_time + wn_damage_data['Tank'], run_dir, event_time ) else: - tank_damage_data = pd.Series(dtype="O") + tank_damage_data = pd.Series(dtype='O') - if "Pump" in wn_damage_data: + if 'Pump' in wn_damage_data: pump_damage_data = createPumpDamageInputForREWET( - wn_damage_data["Pump"], run_dir, event_time + wn_damage_data['Pump'], run_dir, event_time ) else: - pump_damage_data = pd.Series(dtype="O") + pump_damage_data = pd.Series(dtype='O') - if "Junction" in wn_damage_data: + if 'Junction' in wn_damage_data: node_damage_data = createNodeDamageInputForREWET( - wn_damage_data["Junction"], run_dir, event_time + wn_damage_data['Junction'], run_dir, event_time ) else: - node_damage_data = pd.Series(dtype="O") + node_damage_data = pd.Series(dtype='O') damage_data = {} - damage_data["Pipe"] = pipe_damage_data - damage_data["Tank"] = tank_damage_data - damage_data["Pump"] = pump_damage_data - damage_data["Node"] = node_damage_data + damage_data['Pipe'] = pipe_damage_data + damage_data['Tank'] = tank_damage_data + damage_data['Pump'] = pump_damage_data + damage_data['Node'] = node_damage_data return damage_data