テキストファイルを追加モードで保存するためのネストされた「最終」関数があります。コンソール モード (PyCharm を使用) で選択を実行すると、このメッセージが表示されます
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 2721, in run_code
exec code_obj in self.user_global_ns, self.user_ns
File "<ipython-input-62-9c259f95cff2>", line 1, in <module>
f.write(line)
ValueError: I/O operation on closed file
0
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 2721, in run_code
exec code_obj in self.user_global_ns, self.user_ns
File "<ipython-input-63-f015ac76386b>", line 12, in <module>
f.write(line)
ValueError: I/O operation on closed file
私の機能は次のとおりです。
def segmentation_accuracy(reference, segmented, output, method="ke", threshold=0.1, sep="space", header=True):
if not check_suffix(reference) or not check_suffix(segmented):
raise ValueError('the input files need to be ESRI shapefile (*.shp)')
ref_shp = ogr.Open(os.path.abspath(reference))
if ref_shp is None:
raise SystemExit('Unable to open %s' % reference)
seg_shp = ogr.Open(os.path.abspath(segmented))
if seg_shp is None:
raise SystemExit('Unable to open %s' % segmented)
if method == "ke" or method == "pu":
if threshold is None:
raise AccuracyException("threshold need to be set with a value between 0.0 and 1.0")
ref_layer = ref_shp.GetLayer()
ref_layer_num = ref_layer.GetFeatureCount()
seg_layer = seg_shp.GetLayer()
seg_layer_num = seg_layer.GetFeatureCount()
if ref_layer.GetGeomType() != 3:
raise ValueError("s% is not a single-part polygons" % (os.path.split(reference)[1]))
if ref_layer.GetGeomType() != 3:
raise ValueError("s% is not a single-part polygons" % (os.path.split(segmented)[1]))
# get shapely polygons
ref_list = polygon_list(ref_layer)
seg_list = polygon_list(seg_layer)
# create text file
ref_path = os.path.split(os.path.abspath(reference))[0]
filename = set_outputfile(output, path=ref_path)
with open(filename, "a") as f:
if header is True:
line = ["FID", "centroidX", "centroidY", "area", "perimeter", "segments", "ra_or", "ra_os", "over_seg", \
"under_seg", "over_merge", "under_merge", "sim_size", "sim_size_std", "AFI", "qr", "seg_error", \
"d_sr", "d_max", "rp_sr"]
line = sepType[sep].join([str(e) for e in line])+ "\n"
f.write(line)
for FID, ref in enumerate(ref_list):
print FID
seg_overlap = list()
for seg in seg_list:
if ref.intersects(seg):
if threshold_type[method](ref, seg, threshold):
seg_overlap.append(seg)
if len(seg_overlap) != 0:
accuracy = Accuracy(ref, seg_overlap)
line = [FID] + accuracy.data
line = sepType[sep].join([str(e) for e in line])+ "\n"
f.write(line)
else:
accuracy = Accuracy(ref)
line = [FID] + accuracy.data
line = sepType[sep].join([str(e) for e in line])+ "\n"
f.write(line)