Skip to content

Commit

Permalink
ruff reformat for v0.6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
zsarnoczay committed Dec 4, 2024
1 parent 1182432 commit 09782ba
Showing 1 changed file with 67 additions and 67 deletions.
134 changes: 67 additions & 67 deletions modules/performRegionalMapping/NearestNeighborEvents/NNE.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def find_neighbors( # noqa: C901, D103
process_id = 0
run_parallel = False

if do_parallel == "True":
mpi_spec = importlib.util.find_spec("mpi4py")
if do_parallel == 'True':
mpi_spec = importlib.util.find_spec('mpi4py')
found = mpi_spec is not None
if found:
from mpi4py import MPI
Expand All @@ -74,7 +74,7 @@ def find_neighbors( # noqa: C901, D103
num_processes = comm.Get_size()
process_id = comm.Get_rank()
if num_processes < 2: # noqa: PLR2004
do_parallel = "False"
do_parallel = 'False'
run_parallel = False
num_processes = 1
process_id = 0
Expand All @@ -87,18 +87,18 @@ def find_neighbors( # noqa: C901, D103
# Check if the file is a CSV or a GIS file
file_extension = Path(event_grid_file).suffix.lower()

if file_extension == ".csv":
if file_extension == '.csv':
# Existing code for CSV files
grid_df = pd.read_csv(event_dir / event_grid_file, header=0)

# store the locations of the grid points in grid_locations
lat_e = grid_df["Latitude"]
lon_e = grid_df["Longitude"]
lat_e = grid_df['Latitude']
lon_e = grid_df['Longitude']
grid_locations = np.array([[lo, la] for lo, la in zip(lon_e, lat_e)])

if filter_label == "":
if filter_label == '':
grid_extra_keys = list(
grid_df.drop(["GP_file", "Longitude", "Latitude"], axis=1).columns
grid_df.drop(['GP_file', 'Longitude', 'Latitude'], axis=1).columns
)

else:
Expand All @@ -110,59 +110,59 @@ def find_neighbors( # noqa: C901, D103
gdf = gdf.to_crs(epsg=4326) # Convert to WGS84

# Extract coordinates from the geometry
gdf["Longitude"] = gdf.geometry.x
gdf["Latitude"] = gdf.geometry.y
gdf['Longitude'] = gdf.geometry.x
gdf['Latitude'] = gdf.geometry.y

# store the locations of the grid points in grid_locations
lat_e = gdf["Latitude"]
lon_e = gdf["Longitude"]
lat_e = gdf['Latitude']
lon_e = gdf['Longitude']
grid_locations = np.array([[lo, la] for lo, la in zip(lon_e, lat_e)])

if filter_label == "":
if filter_label == '':
grid_extra_keys = list(
gdf.drop(["geometry", "Longitude", "Latitude"], axis=1).columns
gdf.drop(['geometry', 'Longitude', 'Latitude'], axis=1).columns
)

# Convert GeoDataFrame to regular DataFrame for consistency with the rest of the code
grid_df = pd.DataFrame(gdf.drop(columns="geometry"))
grid_df = pd.DataFrame(gdf.drop(columns='geometry'))

# prepare the tree for the nearest neighbor search
if filter_label != "" or len(grid_extra_keys) > 0:
if filter_label != '' or len(grid_extra_keys) > 0:
neighbors_to_get = min(neighbors * 10, len(lon_e))
else:
neighbors_to_get = neighbors

nbrs = NearestNeighbors(n_neighbors=neighbors_to_get, algorithm="ball_tree").fit(
nbrs = NearestNeighbors(n_neighbors=neighbors_to_get, algorithm='ball_tree').fit(
grid_locations
)

# load the building data file
with open(asset_file, encoding="utf-8") as f: # noqa: PTH123
with open(asset_file, encoding='utf-8') as f: # noqa: PTH123
asset_dict = json.load(f)

# prepare a dataframe that holds asset filenames and locations
aim_df = pd.DataFrame(
columns=["Latitude", "Longitude", "file"], index=np.arange(len(asset_dict))
columns=['Latitude', 'Longitude', 'file'], index=np.arange(len(asset_dict))
)

count = 0
for i, asset in enumerate(asset_dict):
if run_parallel == False or (i % num_processes) == process_id: # noqa: E712
with open(asset["file"], encoding="utf-8") as f: # noqa: PTH123
with open(asset['file'], encoding='utf-8') as f: # noqa: PTH123
asset_data = json.load(f)

asset_loc = asset_data["GeneralInformation"]["location"]
asset_loc = asset_data['GeneralInformation']['location']
aim_id = aim_df.index[count]
aim_df.loc[aim_id, "Longitude"] = asset_loc["longitude"]
aim_df.loc[aim_id, "Latitude"] = asset_loc["latitude"]
aim_df.loc[aim_id, "file"] = asset["file"]
aim_df.loc[aim_id, 'Longitude'] = asset_loc['longitude']
aim_df.loc[aim_id, 'Latitude'] = asset_loc['latitude']
aim_df.loc[aim_id, 'file'] = asset['file']
count = count + 1

# store building locations in bldg_locations
bldg_locations = np.array(
[
[lo, la]
for lo, la in zip(aim_df["Longitude"], aim_df["Latitude"])
for lo, la in zip(aim_df['Longitude'], aim_df['Latitude'])
if not np.isnan(lo) and not np.isnan(la)
]
)
Expand All @@ -185,14 +185,14 @@ def find_neighbors( # noqa: C901, D103
):
# open the AIM file
aim_index_id = aim_df.index[aim_id]
asst_file = aim_df.loc[aim_index_id, "file"]
asst_file = aim_df.loc[aim_index_id, 'file']

with open(asst_file, encoding="utf-8") as f: # noqa: PTH123
with open(asst_file, encoding='utf-8') as f: # noqa: PTH123
asset_data = json.load(f)

if filter_label != "":
if filter_label != '':
# soil type of building
asset_label = asset_data["GeneralInformation"][filter_label]
asset_label = asset_data['GeneralInformation'][filter_label]
# soil types of all initial neighbors
grid_label = grid_df[filter_label][ind_list]

Expand All @@ -212,13 +212,13 @@ def find_neighbors( # noqa: C901, D103

if len(grid_extra_keys) > 0:
filter_labels = []
for key in asset_data["GeneralInformation"].keys(): # noqa: SIM118
for key in asset_data['GeneralInformation'].keys(): # noqa: SIM118
if key in grid_extra_keys:
filter_labels.append(key) # noqa: PERF401

filter_list = [True for i in dist_list]
for filter_label in filter_labels: # noqa: PLR1704
asset_label = asset_data["GeneralInformation"][filter_label]
asset_label = asset_data['GeneralInformation'][filter_label]
grid_label = grid_df[filter_label][ind_list]
filter_list_i = (grid_label == asset_label).values # noqa: PD011
filter_list = filter_list and filter_list_i
Expand All @@ -245,8 +245,8 @@ def find_neighbors( # noqa: C901, D103
nbr_samples = np.where(rng.multinomial(1, weights, samples) == 1)[1]

# this is the preferred behavior, the else clause is left for legacy inputs
if file_extension == ".csv":
if grid_df.iloc[0]["GP_file"][-3:] == "csv":
if file_extension == '.csv':
if grid_df.iloc[0]['GP_file'][-3:] == 'csv':
# We assume that every grid point has the same type and number of
# event data. That is, you cannot mix ground motion records and
# intensity measures and you cannot assign 10 records to one point
Expand All @@ -256,12 +256,12 @@ def find_neighbors( # noqa: C901, D103
# information. GM grids have GM record filenames defined in the
# grid point files.
first_file = pd.read_csv(
event_dir / grid_df.iloc[0]["GP_file"], header=0
event_dir / grid_df.iloc[0]['GP_file'], header=0
)
if first_file.columns[0] == "TH_file":
event_type = "timeHistory"
if first_file.columns[0] == 'TH_file':
event_type = 'timeHistory'
else:
event_type = "intensityMeasure"
event_type = 'intensityMeasure'
event_count = first_file.shape[0]

# collect the list of events and scale factors
Expand All @@ -277,9 +277,9 @@ def find_neighbors( # noqa: C901, D103
nbr_index = ind_list[nbr]

# if the grid has ground motion records...
if event_type == "timeHistory":
if event_type == 'timeHistory':
# load the file for the selected grid point
event_collection_file = grid_df.iloc[nbr_index]["GP_file"]
event_collection_file = grid_df.iloc[nbr_index]['GP_file']
event_df = pd.read_csv(
event_dir / event_collection_file, header=0
)
Expand All @@ -294,10 +294,10 @@ def find_neighbors( # noqa: C901, D103
scale_list.append(1.0)

# if the grid has intensity measures
elif event_type == "intensityMeasure":
elif event_type == 'intensityMeasure':
# save the collection file name and the IM row id
event_list.append(
grid_df.iloc[nbr_index]["GP_file"] + f"x{event_j}"
grid_df.iloc[nbr_index]['GP_file'] + f'x{event_j}'
)

# IM collections are not scaled
Expand All @@ -308,20 +308,20 @@ def find_neighbors( # noqa: C901, D103
event_list = []
for e, i in zip(nbr_samples, ind_list):
event_list += [
grid_df.iloc[i]["GP_file"],
grid_df.iloc[i]['GP_file'],
] * e

scale_list = np.ones(len(event_list))
else:
event_list = []
scale_list = []
event_type = "intensityMeasure"
event_type = 'intensityMeasure'

# Determine event_count (number of IMs per grid point)
im_columns = [
col
for col in grid_df.columns
if col not in ["geometry", "Longitude", "Latitude"]
if col not in ['geometry', 'Longitude', 'Latitude']
]
# event_count = len(im_columns)
event_count = 1
Expand All @@ -335,7 +335,7 @@ def find_neighbors( # noqa: C901, D103
nbr_index = ind_list[nbr]

# For GIS files, create a new CSV file
csv_filename = f"Site_{nbr_index}.csv"
csv_filename = f'Site_{nbr_index}.csv'

csv_path = event_dir / csv_filename

Expand All @@ -345,7 +345,7 @@ def find_neighbors( # noqa: C901, D103
im_columns = [
col
for col in grid_df.columns
if col not in ["geometry", "Longitude", "Latitude"]
if col not in ['geometry', 'Longitude', 'Latitude']
]

im_data = pd.DataFrame(
Expand All @@ -357,7 +357,7 @@ def find_neighbors( # noqa: C901, D103

im_data.to_csv(csv_path, index=False)
# save the collection file name and the IM row id
event_list.append(csv_filename + f"x{event_j}")
event_list.append(csv_filename + f'x{event_j}')

# IM collections are not scaled
scale_list.append(1.0)
Expand All @@ -371,42 +371,42 @@ def find_neighbors( # noqa: C901, D103
# "factor": scale_list[e_i],
# #"type": event_type
# })
event_list_json.append([f"{event}x{e_i:05d}", scale_list[e_i]])
event_list_json.append([f'{event}x{e_i:05d}', scale_list[e_i]])

# save the event dictionary to the AIM
# TODO: we assume there is only one event # noqa: TD002
# handling multiple events will require more sophisticated inputs

if "Events" not in asset_data:
asset_data["Events"] = [{}]
elif len(asset_data["Events"]) == 0:
asset_data["Events"].append({})
if 'Events' not in asset_data:
asset_data['Events'] = [{}]
elif len(asset_data['Events']) == 0:
asset_data['Events'].append({})

asset_data["Events"][0].update(
asset_data['Events'][0].update(
{
# "EventClassification": "Earthquake",
"EventFolderPath": str(event_dir),
"Events": event_list_json,
"type": event_type,
'EventFolderPath': str(event_dir),
'Events': event_list_json,
'type': event_type,
# "type": "SimCenterEvents"
}
)

with open(asst_file, "w", encoding="utf-8") as f: # noqa: PTH123
with open(asst_file, 'w', encoding='utf-8') as f: # noqa: PTH123
json.dump(asset_data, f, indent=2)


if __name__ == "__main__":
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--assetFile")
parser.add_argument("--filenameEVENTgrid")
parser.add_argument("--samples", type=int)
parser.add_argument("--neighbors", type=int)
parser.add_argument("--filter_label", default="")
parser.add_argument("--doParallel", default="False")
parser.add_argument("-n", "--numP", default="8")
parser.add_argument("-m", "--mpiExec", default="mpiexec")
parser.add_argument("--seed", type=int, default=None)
parser.add_argument('--assetFile')
parser.add_argument('--filenameEVENTgrid')
parser.add_argument('--samples', type=int)
parser.add_argument('--neighbors', type=int)
parser.add_argument('--filter_label', default='')
parser.add_argument('--doParallel', default='False')
parser.add_argument('-n', '--numP', default='8')
parser.add_argument('-m', '--mpiExec', default='mpiexec')
parser.add_argument('--seed', type=int, default=None)
args = parser.parse_args()

find_neighbors(
Expand Down

0 comments on commit 09782ba

Please sign in to comment.