Skip to content

Commit 80cb2d2

Browse files
jjmaynardgarobrik
authored andcommitted
fix: refactor sorting and formatting for readability
Improves code readability by reformatting long sort_values and other function calls across the file. No functional changes were made; only code style and formatting were updated for clarity and consistency.
1 parent 4127f98 commit 80cb2d2

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

soil_id/us_soil.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def list_soils(lon, lat):
217217
# Add distance column from mucompdata_pd using cokey link
218218
muhorzdata_pd = pd.merge(
219219
muhorzdata_pd,
220-
mucompdata_pd[["cokey", "distance", "distance_score"]],
220+
mucompdata_pd[["cokey", "distance", "distance_score"]],
221221
on="cokey",
222222
how="left",
223223
)
@@ -234,7 +234,9 @@ def list_soils(lon, lat):
234234
mucompdata_pd = mucompdata_pd[mucompdata_pd["cokey"].isin(comp_key)]
235235

236236
# Sort mucompdata_pd based on 'cond_prob' and 'distance'
237-
mucompdata_pd.sort_values(["cond_prob", "distance", "compname"], ascending=[False, True, True], inplace=True)
237+
mucompdata_pd.sort_values(
238+
["cond_prob", "distance", "compname"], ascending=[False, True, True], inplace=True
239+
)
238240
mucompdata_pd.reset_index(drop=True, inplace=True)
239241

240242
# Duplicate the 'compname' column for grouping purposes
@@ -258,16 +260,13 @@ def list_soils(lon, lat):
258260
component_names = mucompdata_pd["compname"].tolist()
259261
name_counts = collections.Counter(component_names)
260262

261-
# Track which indices have been processed for each name
262-
processed_indices = {}
263-
264263
for name, count in sorted(name_counts.items()): # Sort for deterministic order
265264
if count > 1: # If a component name is duplicated
266265
# Find all indices for this name
267266
indices = [i for i, comp_name in enumerate(component_names) if comp_name == name]
268267
# Sort indices for deterministic order
269268
indices.sort()
270-
269+
271270
# Add suffixes to all occurrences except the first
272271
for i, idx in enumerate(indices):
273272
if i > 0: # Skip the first occurrence (keep original name)
@@ -982,10 +981,11 @@ def list_soils(lon, lat):
982981

983982
for index, group in enumerate(OSDhorzdata_group_cokey):
984983
cokey = group["cokey"].iloc[0] # Get the cokey for this group
985-
984+
986985
# Check if compkind is not in OSD_compkind or if series contains any null values
987986
if (
988-
mucompdata_pd[mucompdata_pd["cokey"] == cokey]["compkind"].iloc[0] not in OSD_compkind
987+
mucompdata_pd[mucompdata_pd["cokey"] == cokey]["compkind"].iloc[0]
988+
not in OSD_compkind
989989
or group["series"].isnull().any()
990990
):
991991
cokey_to_urls[cokey] = {"sde": "", "see": ""}
@@ -998,7 +998,7 @@ def list_soils(lon, lat):
998998
# Create URLs
999999
cokey_to_urls[cokey] = {
10001000
"sde": f"https://casoilresource.lawr.ucdavis.edu/sde/?series={comp}",
1001-
"see": f"https://casoilresource.lawr.ucdavis.edu/see/#{comp}"
1001+
"see": f"https://casoilresource.lawr.ucdavis.edu/see/#{comp}",
10021002
}
10031003

10041004
else:
@@ -1574,7 +1574,7 @@ def rank_soils(
15741574
# Check if list_output_data is a string (error message) instead of expected object
15751575
if isinstance(list_output_data, str):
15761576
return {"error": f"Cannot rank soils: {list_output_data}"}
1577-
1577+
15781578
# ---------------------------------------------------------------------------------------
15791579
# ------ Load in user data --------#
15801580
# Initialize the DataFrame from the input data
@@ -2061,12 +2061,14 @@ def rank_soils(
20612061

20622062
# Concatenate the sorted and ranked groups
20632063
D_final = pd.concat(soilIDList_data).reset_index(drop=True)
2064-
2064+
20652065
# Merge with the Rank_Filter data
20662066
D_final = pd.merge(D_final, Rank_Filter, on="compname", how="left")
20672067

20682068
# Sort dataframe to correctly assign Rank_Data
2069-
D_final = D_final.sort_values(by=["soilID_rank_data", "Score_Data", "compname"], ascending=[False, False, True])
2069+
D_final = D_final.sort_values(
2070+
by=["soilID_rank_data", "Score_Data", "compname"], ascending=[False, False, True]
2071+
)
20702072

20712073
# Assigning rank based on the soilID rank and rank status
20722074
rank_id = 1
@@ -2160,7 +2162,9 @@ def rank_soils(
21602162
soilIDList_out = []
21612163

21622164
for _, group in D_final_loc.groupby("compname_grp", sort=True):
2163-
group = group.sort_values(["Score_Data_Loc", "compname"], ascending=[False, True]).reset_index(drop=True)
2165+
group = group.sort_values(
2166+
["Score_Data_Loc", "compname"], ascending=[False, True]
2167+
).reset_index(drop=True)
21642168
group["soilID_rank_final"] = [True if idx == 0 else False for idx in range(len(group))]
21652169
soilIDList_out.append(group)
21662170

0 commit comments

Comments
 (0)