Skip to content

Commit e44818a

Browse files
committed
placed sklearn import in a try block
1 parent 675b197 commit e44818a

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

examples/arcticdem.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"metadata": {},
2121
"outputs": [],
2222
"source": [
23-
"icesat2.init(\"localhost\", verbose=True, organization=None)"
23+
"icesat2.init(\"slideruleearth.io\", verbose=True)"
2424
]
2525
},
2626
{

sliderule/icesat2.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import geopandas
4242
from shapely.geometry.multipolygon import MultiPolygon
4343
from shapely.geometry import Polygon
44-
from sklearn.cluster import KMeans
4544
import sliderule
4645

4746
###############################################################################
@@ -51,6 +50,14 @@
5150
# create logger
5251
logger = logging.getLogger(__name__)
5352

53+
# import cluster support
54+
clustering_enabled = False
55+
try:
56+
from sklearn.cluster import KMeans
57+
clustering_enabled = True
58+
except:
59+
logger.warning("Unable to import sklearn... clustering support disabled")
60+
5461
# profiling times for each major function
5562
profiles = {}
5663

@@ -1327,22 +1334,25 @@ def toregion(source, tolerance=0.0, cellsize=0.01, n_clusters=1):
13271334
# generate clusters
13281335
clusters = []
13291336
if n_clusters > 1:
1330-
# pull out centroids of each geometry object
1331-
if "CenLon" in gdf and "CenLat" in gdf:
1332-
X = numpy.column_stack((gdf["CenLon"], gdf["CenLat"]))
1337+
if clustering_enabled:
1338+
# pull out centroids of each geometry object
1339+
if "CenLon" in gdf and "CenLat" in gdf:
1340+
X = numpy.column_stack((gdf["CenLon"], gdf["CenLat"]))
1341+
else:
1342+
s = gdf.centroid
1343+
X = numpy.column_stack((s.x, s.y))
1344+
# run k means clustering algorithm against polygons in gdf
1345+
kmeans = KMeans(n_clusters=n_clusters, init='k-means++', random_state=5, max_iter=400)
1346+
y_kmeans = kmeans.fit_predict(X)
1347+
k = geopandas.pd.DataFrame(y_kmeans, columns=['cluster'])
1348+
gdf = gdf.join(k)
1349+
# build polygon for each cluster
1350+
for n in range(n_clusters):
1351+
c_gdf = gdf[gdf["cluster"] == n]
1352+
c_poly = __gdf2poly(c_gdf)
1353+
clusters.append(c_poly)
13331354
else:
1334-
s = gdf.centroid
1335-
X = numpy.column_stack((s.x, s.y))
1336-
# run k means clustering algorithm against polygons in gdf
1337-
kmeans = KMeans(n_clusters=n_clusters, init='k-means++', random_state=5, max_iter=400)
1338-
y_kmeans = kmeans.fit_predict(X)
1339-
k = geopandas.pd.DataFrame(y_kmeans, columns=['cluster'])
1340-
gdf = gdf.join(k)
1341-
# build polygon for each cluster
1342-
for n in range(n_clusters):
1343-
c_gdf = gdf[gdf["cluster"] == n]
1344-
c_poly = __gdf2poly(c_gdf)
1345-
clusters.append(c_poly)
1355+
raise sliderule.FatalError("Clustering support not enabled; unable to import sklearn package")
13461356

13471357
# update timing profiles
13481358
profiles[toregion.__name__] = time.perf_counter() - tstart

0 commit comments

Comments
 (0)