I am running a Python script invoking the DBSCAN tool to cluster feature points. When I open ArcMap, load the feature layer, and run the script, it works the way I expect it to. But if I run it again with the same feature layer still open, I get "AttributeError: NoneType has no attribute issparse".Here is a section of my code:
import sys, osimport arcpyfrom math import sqrtfrom math import atan2from math import atanfrom math import sinfrom math import cosfrom math import tanfrom math import pifrom math import radiansimport numpy as npfrom sklearn.cluster import DBSCANdef llh2enu(lat_dd_orig, lon_dd_orig, ht_orig, lat_dd_pt2, lon_dd_pt2, ht_pt2):...# End function llh2enu definition# Begin processin_featureclass = arcpy.GetParameterAsText(0)in_scan_distance_str = arcpy.GetParameterAsText(1)in_scan_minpts_str = arcpy.GetParameterAsText(2)eps = float(in_scan_distance_str)min_samples = float(in_scan_minpts_str)# Read dataX = []cursor = arcpy.SearchCursor(in_featureclass)firstloop = 0try: for row in cursor: lat_dd = row.getValue("Latitude") lon_dd = row.getValue("Longitude") # Set the origin for the enu coordinate sys as the first point if firstloop == 0: lat_orig = lat_dd lon_orig = lon_dd firstloop = 1 e, n, u = llh2enu(lat_orig, lon_orig, 0.0, lat_dd, lon_dd, 0.0) X.append([e, n]) del cursorexcept arcpy.ExecuteError: arcpy.AddError("Failed reading values") sys.exit()except: arcpy.AddError("Non-tool error occurred") sys.exit()# Make X an array rather than a list.X = np.array(X)# Compute DBSCANdb = DBSCAN(eps, min_samples).fit(X)Here is where the error occurs. Again, it runs just fine the first time the feature layer is open in ArcMap, but it gives me the AttributeError all subsequent times I run it. I have to close ArcMap and reopen it with the feature layer to get it to work again. I even put the following at the end just to be sure to free everything up:
del cursordel Xdel dbI put some diagnostic prints of the values of X in both scenarios, and the values of X are identical.
If I run the script more than once in ArcMap, whether I create the list and array X from a feature class or from a spreadsheet, I get the "AttributeError: NoneType has no attribute issparse" error. If I run the script outside ArcMap from the command line, and create the list and array X from a spreadsheet, I get no errors no matter how many times I run it.
Here is the traceback:
File "P:\Bailey\GaleK Tracks\test_dbscan.py", line 118, in db = DBSCAN(eps, min_samples).fit(X)File "C:\Python27\ArcGIS10.2\lib\site-packages\sklearn\cluster\dbscan_.py", line 248, in fit clust = dbscan(X, **self.get_params())File "C:\Python27\ArcGIS10.2\lib\site-packages\sklearn\cluster\dbscan_.py", line 132, in dbscan X[index], eps, return_distance=False)[0]File "C:\Python27\ArcGIS10.2\lib\site-packages\sklearn\neighbors\base.py", line 507, in radius_neighbors return_distance = return_distance)File "binary_tree.pxi", line 1442, in sklearn.neighbors.dk_tree.BinaryTree.query_radius (sklearn\neighbors\kd_tree.c:11398)File "C:\Python27\ArcGIS10.2\lib\site-packages\sklearn\utils\validation.py", line 117, in array2d if sp.issparse(X):AttributeError: 'NoneType' object has no attribute 'issparse'As you can see, I am running Python 2.7 with ArcGIS 10.2.
أكثر...
import sys, osimport arcpyfrom math import sqrtfrom math import atan2from math import atanfrom math import sinfrom math import cosfrom math import tanfrom math import pifrom math import radiansimport numpy as npfrom sklearn.cluster import DBSCANdef llh2enu(lat_dd_orig, lon_dd_orig, ht_orig, lat_dd_pt2, lon_dd_pt2, ht_pt2):...# End function llh2enu definition# Begin processin_featureclass = arcpy.GetParameterAsText(0)in_scan_distance_str = arcpy.GetParameterAsText(1)in_scan_minpts_str = arcpy.GetParameterAsText(2)eps = float(in_scan_distance_str)min_samples = float(in_scan_minpts_str)# Read dataX = []cursor = arcpy.SearchCursor(in_featureclass)firstloop = 0try: for row in cursor: lat_dd = row.getValue("Latitude") lon_dd = row.getValue("Longitude") # Set the origin for the enu coordinate sys as the first point if firstloop == 0: lat_orig = lat_dd lon_orig = lon_dd firstloop = 1 e, n, u = llh2enu(lat_orig, lon_orig, 0.0, lat_dd, lon_dd, 0.0) X.append([e, n]) del cursorexcept arcpy.ExecuteError: arcpy.AddError("Failed reading values") sys.exit()except: arcpy.AddError("Non-tool error occurred") sys.exit()# Make X an array rather than a list.X = np.array(X)# Compute DBSCANdb = DBSCAN(eps, min_samples).fit(X)Here is where the error occurs. Again, it runs just fine the first time the feature layer is open in ArcMap, but it gives me the AttributeError all subsequent times I run it. I have to close ArcMap and reopen it with the feature layer to get it to work again. I even put the following at the end just to be sure to free everything up:
del cursordel Xdel dbI put some diagnostic prints of the values of X in both scenarios, and the values of X are identical.
If I run the script more than once in ArcMap, whether I create the list and array X from a feature class or from a spreadsheet, I get the "AttributeError: NoneType has no attribute issparse" error. If I run the script outside ArcMap from the command line, and create the list and array X from a spreadsheet, I get no errors no matter how many times I run it.
Here is the traceback:
File "P:\Bailey\GaleK Tracks\test_dbscan.py", line 118, in db = DBSCAN(eps, min_samples).fit(X)File "C:\Python27\ArcGIS10.2\lib\site-packages\sklearn\cluster\dbscan_.py", line 248, in fit clust = dbscan(X, **self.get_params())File "C:\Python27\ArcGIS10.2\lib\site-packages\sklearn\cluster\dbscan_.py", line 132, in dbscan X[index], eps, return_distance=False)[0]File "C:\Python27\ArcGIS10.2\lib\site-packages\sklearn\neighbors\base.py", line 507, in radius_neighbors return_distance = return_distance)File "binary_tree.pxi", line 1442, in sklearn.neighbors.dk_tree.BinaryTree.query_radius (sklearn\neighbors\kd_tree.c:11398)File "C:\Python27\ArcGIS10.2\lib\site-packages\sklearn\utils\validation.py", line 117, in array2d if sp.issparse(X):AttributeError: 'NoneType' object has no attribute 'issparse'As you can see, I am running Python 2.7 with ArcGIS 10.2.
أكثر...