Skip to content

Commit b273b3d

Browse files
committed
fix: lint format changes
1 parent 37561eb commit b273b3d

File tree

4 files changed

+87
-96
lines changed

4 files changed

+87
-96
lines changed

soil_id/db.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
# Standard libraries
1717
import logging
18-
import sys
1918

2019
import pandas as pd
2120

@@ -25,12 +24,12 @@
2524
# local libraries
2625
import soil_id.config
2726

28-
import os
2927
from dotenv import load_dotenv
3028

3129
# Load .env file
3230
load_dotenv()
3331

32+
3433
def get_datastore_connection():
3534
"""
3635
Establish a connection to the datastore using app configurations.
@@ -236,6 +235,7 @@ def get_WISE30sec_data(MUGLB_NEW_Select):
236235
if conn:
237236
conn.close()
238237

238+
239239
# global
240240
def get_WRB_descriptions(WRB_Comp_List):
241241
"""
@@ -290,10 +290,10 @@ def getSG_descriptions(WRB_Comp_List):
290290
Fetch WRB descriptions from a PostgreSQL database using wrb2006_to_fao90
291291
and wrb_fao90_desc tables. Returns a pandas DataFrame with columns:
292292
[WRB_tax, Description_en, Management_en, Description_es, ...]
293-
293+
294294
Args:
295295
WRB_Comp_List (list[str]): List of WRB_2006_Full values (e.g. ["Chernozem","Gleysol"]).
296-
296+
297297
Returns:
298298
pandas.DataFrame or None if an error occurs.
299299
"""
@@ -302,13 +302,13 @@ def getSG_descriptions(WRB_Comp_List):
302302
try:
303303
# 1. Get a connection to your datastore (replace with your actual function):
304304
conn = get_datastore_connection()
305-
305+
306306
def execute_query(query, params):
307307
with conn.cursor() as cur:
308308
# Execute the query with the parameters
309309
cur.execute(query, params)
310310
return cur.fetchall()
311-
311+
312312
# 2. Map WRB_2006_Full -> WRB_1984_Full using wrb2006_to_fao90
313313
# Make sure we pass (tuple(WRB_Comp_List),) so psycopg2 can fill IN ('A','B','C')
314314
# Example: WHERE lu.WRB_2006_Full IN ('Chernozem','Gleysol',...)
@@ -321,7 +321,7 @@ def execute_query(query, params):
321321

322322
# Flatten from [(x,), (y,)] => [x, y]
323323
WRB_Comp_List_mapped = [item for (item,) in names]
324-
324+
325325
if not WRB_Comp_List_mapped:
326326
# If no mapping found, return an empty DataFrame or None
327327
logging.warning("No mapped WRB_1984_Full names found for given WRB_2006_Full values.")
@@ -346,7 +346,7 @@ def execute_query(query, params):
346346
WHERE WRB_tax = ANY(%s)
347347
"""
348348
results = execute_query(sql2, ([WRB_Comp_List_mapped],))
349-
349+
350350
# 4. Convert the raw query results to a DataFrame
351351
data = pd.DataFrame(
352352
results,
@@ -371,4 +371,3 @@ def execute_query(query, params):
371371
finally:
372372
if conn:
373373
conn.close()
374-

soil_id/global_soil.py

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,6 @@
1313
# You should have received a copy of the GNU Affero General Public License
1414
# along with this program. If not, see https://www.gnu.org/licenses/.
1515

16-
# Standard libraries
17-
import collections
18-
import csv
19-
import io
20-
import json
21-
import re
22-
import sys
23-
import os
24-
import psycopg
25-
from dataclasses import dataclass
26-
27-
# Load .env file
28-
from dotenv import load_dotenv
29-
load_dotenv()
30-
31-
# Third-party libraries
32-
import numpy as np
33-
import pandas as pd
34-
from scipy.stats import norm
35-
3616
# local libraries
3717
import soil_id.config
3818

@@ -41,17 +21,17 @@
4121
getSG_descriptions,
4222
)
4323

44-
from .color import(
24+
from .color import (
4525
calculate_deltaE2000,
4626
)
4727

4828
from .services import get_soilgrids_classification_data, get_soilgrids_property_data
29+
4930
from .utils import (
5031
agg_data_layer,
5132
assign_max_distance_scores,
5233
calculate_location_score,
5334
drop_cokey_horz,
54-
extract_values,
5535
extract_WISE_data,
5636
getCF_fromClass,
5737
getClay,
@@ -60,17 +40,36 @@
6040
getTexture,
6141
gower_distances,
6242
pedon_color,
63-
sg_get_and_agg,
6443
silt_calc,
65-
max_comp_depth,
44+
max_comp_depth,
45+
adjust_depth_interval,
6646
)
6747

48+
# Standard libraries
49+
import logging
50+
import collections
51+
import csv
52+
import io
53+
import re
54+
from dataclasses import dataclass
55+
56+
# Third-party libraries
57+
import numpy as np
58+
import pandas as pd
59+
from scipy.stats import norm
60+
61+
# Load .env file
62+
from dotenv import load_dotenv
63+
load_dotenv()
64+
65+
6866
@dataclass
6967
class SoilListOutputData:
7068
soil_list_json: dict
7169
rank_data_csv: str
7270
map_unit_component_data_csv: str
7371

72+
7473
# entry points
7574
# getSoilLocationBasedGlobal
7675
# list_soils
@@ -94,9 +93,9 @@ def list_soils_global(lon, lat):
9493
lon,
9594
lat,
9695
# Temporarily change file path
97-
file_path = '/mnt/c/LandPKS_API_SoilID-master/global/wise30sec_poly_simp_soil.gpkg',
98-
#file_path=config.WISE_PATH,
99-
#layer_name=None,
96+
file_path='/mnt/c/LandPKS_API_SoilID-master/global/wise30sec_poly_simp_soil.gpkg',
97+
# file_path=config.WISE_PATH,
98+
# layer_name=None,
10099
buffer_dist=10000,
101100
)
102101

@@ -106,7 +105,7 @@ def list_soils_global(lon, lat):
106105
mucompdata_pd["distance"] = pd.to_numeric(mucompdata_pd["distance"])
107106
mucompdata_pd["share"] = pd.to_numeric(mucompdata_pd["share"])
108107
mucompdata_pd = mucompdata_pd.drop_duplicates().reset_index(drop=True)
109-
108+
110109
##############################################################################################
111110
# Individual probability
112111
# Based on Fan et al 2018 EQ 1, the conditional probability for each component is calculated
@@ -131,7 +130,7 @@ def list_soils_global(lon, lat):
131130

132131
mucompdata_pd = pd.merge(mucompdata_pd, cond_prob, on="cokey", how="left")
133132
mucompdata_pd = mucompdata_pd.sort_values("distance_score", ascending=False)
134-
133+
135134
mucompdata_pd = mucompdata_pd.reset_index(drop=True)
136135
mucompdata_pd["distance"] = mucompdata_pd["distance"].round(4)
137136
mucompdata_pd["Index"] = mucompdata_pd.index
@@ -214,7 +213,7 @@ def list_soils_global(lon, lat):
214213
# Subset mucompdata_pd by new compname_key and add suffix to name if there are duplicates
215214
mucompdata_pd = mucompdata_pd.loc[mucompdata_pd['cokey'].isin(comp_key)].reset_index(drop=True)
216215
mucompdata_pd["compname_grp"] = mucompdata_pd["compname"]
217-
216+
218217
# Sort by 'distance_score' (descending) and 'distance' (ascending), then reset the index
219218
mucompdata_pd = mucompdata_pd.sort_values(['distance_score', 'distance'], ascending=[False, True]).reset_index(drop=True)
220219

@@ -255,7 +254,7 @@ def list_soils_global(lon, lat):
255254
.drop_duplicates(keep="first")
256255
.reset_index(drop=True)
257256
)
258-
257+
259258
# profile depth
260259
c_very_bottom = max_comp_depth(profile)
261260

@@ -334,7 +333,7 @@ def list_soils_global(lon, lat):
334333
# Concatenate lists to form DataFrames
335334
c_bottom_depths = pd.concat(c_bottom_depths, axis=0)
336335
clay_texture = pd.concat(clay_texture, axis=0)
337-
336+
338337
# Subset mucompdata and muhorzdata DataFrames
339338
mucompdata_pd = mucompdata_pd[mucompdata_pd["cokey"].isin(c_bottom_depths.cokey)]
340339
muhorzdata_pd = muhorzdata_pd[muhorzdata_pd["cokey"].isin(c_bottom_depths.cokey)]
@@ -527,9 +526,9 @@ def convert_to_serializable(obj):
527526
return obj.tolist()
528527
else:
529528
return obj
530-
529+
531530
output_SoilList_cleaned = convert_to_serializable(output_SoilList)
532-
531+
533532
soil_list_json = {
534533
"metadata": {
535534
"location": "us",
@@ -544,8 +543,8 @@ def convert_to_serializable(obj):
544543
"ec": "ds/m",
545544
},
546545
},
547-
#"AWS_PIW90": aws_PIW90,
548-
#"Soil Data Value": var_imp,
546+
# "AWS_PIW90": aws_PIW90,
547+
# "Soil Data Value": var_imp,
549548
"soilList": output_SoilList_cleaned,
550549
}
551550

@@ -554,7 +553,6 @@ def convert_to_serializable(obj):
554553
rank_data_csv=soilIDRank_output_pd.to_csv(index=None, header=True),
555554
map_unit_component_data_csv=mucompdata_cond_prob.to_csv(index=None, header=True),
556555
)
557-
558556

559557

560558
##############################################################################################
@@ -686,12 +684,11 @@ def rank_soils_global(
686684
p_sandpct_intpl = adjust_depth_interval(p_sandpct_intpl)
687685
p_claypct_intpl = adjust_depth_interval(p_claypct_intpl)
688686
p_cfg_intpl = adjust_depth_interval(p_cfg_intpl)
689-
p_lab_intpl = adjust_depth_interval(p_lab_intpl)
690687

691688
# Construct final dataframe with adjusted data
692689
p_compname = pd.Series("sample_pedon", index=np.arange(len(p_sandpct_intpl)))
693690
p_hz_data = pd.concat(
694-
[p_compname, p_sandpct_intpl, p_claypct_intpl, p_cfg_intpl, p_lab_intpl], axis=1
691+
[p_compname, p_sandpct_intpl, p_claypct_intpl, p_cfg_intpl], axis=1
695692
)
696693
p_hz_data.columns = [
697694
"compname",
@@ -720,22 +717,20 @@ def rank_soils_global(
720717
p_cfg_intpl = []
721718

722719
# Initialize lab interpolation data with NaNs
723-
p_lab_intpl = pd.DataFrame(np.nan, index=np.arange(1), columns=np.arange(3))
724720
cr_df = pd.Series([np.nan])
725-
721+
726722
# Set default bottom depth data
727723
if bedrock is not None:
728724
p_bottom_depth = pd.DataFrame([-999, "sample_pedon", bedrock]).T
729725
else:
730726
p_bottom_depth = pd.DataFrame([-999, "sample_pedon", 0]).T
731727
p_bottom_depth.columns = ["cokey", "compname", "bottom_depth"]
732728

733-
734729
# --------------------------------------------------------------------------------------------------------------------------------------
735730
# Load in component data from soilIDList
736731
soilIDRank_output_pd = pd.read_csv(io.StringIO(list_output_data.rank_data_csv))
737732
mucompdata_pd = pd.read_csv(io.StringIO(list_output_data.map_unit_component_data_csv))
738-
733+
739734
# Create soil depth DataFrame and subset component depths based on max user depth
740735
# if no bedrock specified
741736
c_bottom_depths = mucompdata_pd[["compname", "c_very_bottom"]].rename(
@@ -1139,7 +1134,7 @@ def rank_soils_global(
11391134
Score_Data_Loc = (D_final_loc[["Score_Data", "distance_score"]].sum(axis=1)) / (
11401135
D_final_loc.weight + location_weight
11411136
)
1142-
1137+
11431138
D_final_loc["Score_Data_Loc"] = Score_Data_Loc
11441139

11451140
# Rule-based final score adjustment
@@ -1251,7 +1246,7 @@ def rank_soils_global(
12511246
].fillna(
12521247
0.0
12531248
)
1254-
1249+
12551250
# Construct the output format
12561251
Rank = [
12571252
{
@@ -1288,15 +1283,17 @@ def rank_soils_global(
12881283
##################################################################################################
12891284
# getSoilGridsGlobal #
12901285
##################################################################################################
1286+
1287+
12911288
def sg_list(lon, lat):
12921289
"""
12931290
Query the SoilGrids API (via get_soilgrids_property_data) and post-process
12941291
the returned JSON into a structured dictionary that includes:
1295-
1292+
12961293
1. Soil horizons data (sand, clay, cfvo, pH, cec, silt, texture) at multiple depths.
12971294
2. Classification probabilities and descriptions (WRB taxonomy).
12981295
3. Summarized or aggregated soil variables, with depth tracking.
1299-
1296+
13001297
Args:
13011298
lon (float): Longitude in decimal degrees (WGS84).
13021299
lat (float): Latitude in decimal degrees (WGS84).
@@ -1354,7 +1351,7 @@ def sg_list(lon, lat):
13541351
df_top_depth = pd.DataFrame(top_depths, columns=["top_depth"])
13551352
df_bottom_depth = pd.DataFrame(bottom_depths, columns=["bottom_depth"])
13561353
df_values = pd.DataFrame(values, columns=["value"])
1357-
1354+
13581355
# The code assumes each property repeats over the same set of depths.
13591356
n_depths = len(df_top_depth)
13601357
if len(names) % n_depths != 0:
@@ -1374,9 +1371,9 @@ def sg_list(lon, lat):
13741371
# 6. Pivot the data into a wide form with each property as a column.
13751372
try:
13761373
sg_data_w = sg_data.pivot_table(
1377-
index="bottom_depth",
1378-
columns="prop",
1379-
values="value",
1374+
index="bottom_depth",
1375+
columns="prop",
1376+
values="value",
13801377
aggfunc="first" # Change the aggregator if needed
13811378
)
13821379
except Exception as e:
@@ -1461,7 +1458,7 @@ def sg_list(lon, lat):
14611458
sand_pd = sg_data_w["sand"]
14621459
clay_pd = sg_data_w["clay"]
14631460
rfv_pd = sg_data_w["cfvo"]
1464-
pH_pd = sg_data_w["phh2o"]
1461+
pH_pd = sg_data_w["phh2o"]
14651462
cec_pd = sg_data_w["cec"]
14661463
# 13. Additional texture calculations from aggregated values
14671464
texture_pd = pd.DataFrame({
@@ -1536,4 +1533,4 @@ def sg_list(lon, lat):
15361533
return {
15371534
"metadata": metadata,
15381535
"soilGrids": SoilGrids
1539-
}
1536+
}

soil_id/services.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@
1313
# You should have received a copy of the GNU Affero General Public License
1414
# along with this program. If not, see https://www.gnu.org/licenses/.
1515

16-
import json
1716
import logging
1817
import re
1918

2019
import pandas as pd
2120
import requests
2221

23-
from .db import save_soilgrids_output
24-
2522

2623
def get_elev_data(lon, lat):
2724
"""

0 commit comments

Comments
 (0)