Skip to content

Commit 961032b

Browse files
committed
modify hardcoded filepaths in the quadtree to allow transplanting it to new filesystems.
1 parent 9310196 commit 961032b

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

src/natcap/invest/recreation/buffered_numpy_disk_map.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ def _write(self, array_id_list):
109109
where array_id=? LIMIT 1""", [array_id])
110110
array_path = db_cursor.fetchone()
111111
if array_path is not None:
112-
_npy_append(array_path[0], numpy.concatenate(array_deque))
112+
_npy_append(os.path.join(
113+
self.manager_directory, array_path[0]), numpy.concatenate(array_deque))
113114
array_deque = None
114115
# cache gets wiped at end so okay to use same deque
115116
# array_deque.append(numpy.load(array_path[0]))
@@ -122,16 +123,18 @@ def _write(self, array_id_list):
122123
# off the last two characters in the filename
123124
array_filename = uuid.uuid4().hex + '.npy'
124125
# -6:-4 skips the extension and gets the last 2 characters
126+
array_subdirectory = array_filename[-6:-4]
125127
array_directory = os.path.join(
126-
self.manager_directory, array_filename[-6:-4])
128+
self.manager_directory, array_subdirectory)
127129
if not os.path.isdir(array_directory):
128130
os.mkdir(array_directory)
129131
array_path = os.path.join(array_directory, array_filename)
130132
# save the file
131133
array_data = numpy.concatenate(array_deque)
132134
array_deque = None
133135
numpy.save(array_path, array_data)
134-
insert_list.append((array_id, array_path))
136+
# insert_list.append((array_id, array_path))
137+
insert_list.append((array_id, os.path.join(array_subdirectory, array_filename)))
135138
db_connection.close()
136139
return insert_list
137140

@@ -193,7 +196,7 @@ def read(self, array_id):
193196
db_connection.close()
194197

195198
if array_path is not None:
196-
array_data = numpy.load(array_path[0])
199+
array_data = numpy.load(os.path.join(self.manager_directory, array_path[0]))
197200
else:
198201
array_data = numpy.empty(
199202
0, dtype=BufferedNumpyDiskMap._ARRAY_TUPLE_TYPE)
@@ -215,10 +218,11 @@ def delete(self, array_id):
215218
[array_id])
216219
array_path = db_cursor.fetchone()
217220
if array_path is not None:
218-
os.remove(array_path[0])
221+
array_abs_path = os.path.join(self.manager_directory, array_path[0])
222+
os.remove(array_abs_path)
219223
try:
220224
# attempt to remove the directory if it's empty
221-
os.rmdir(os.path.dirname(array_path[0]))
225+
os.rmdir(os.path.dirname(array_abs_path))
222226
except OSError:
223227
# it's not empty, not a big deal
224228
pass

src/natcap/invest/recreation/recmodel_server.py

+34
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ def __init__(
130130
# ooc_qt_picklefilename = os.path.join(
131131
# global_cache, quadtree_pickle_filename)
132132
ooc_qt_picklefilename = quadtree_pickle_filename
133+
global_cache = os.path.dirname(ooc_qt_picklefilename)
133134
else:
134135
raise ValueError(
135136
'Both raw_csv_filename and quadtree_pickle_filename'
@@ -138,6 +139,7 @@ def __init__(
138139
if os.path.isfile(ooc_qt_picklefilename):
139140
LOGGER.info(
140141
'%s quadtree already exists', ooc_qt_picklefilename)
142+
ooc_qt_picklefilename = transplant_quadtree(ooc_qt_picklefilename, cache_workspace)
141143
else:
142144
LOGGER.info(
143145
'%s not found, constructing quadtree', ooc_qt_picklefilename)
@@ -149,6 +151,7 @@ def __init__(
149151
max_points_per_node, max_depth)
150152
self.qt_pickle_filename = ooc_qt_picklefilename
151153
self.local_cache_workspace = os.path.join(cache_workspace, 'local')
154+
# self.global_cache_dir = global_cache
152155
self.min_year = min_year
153156
self.max_year = max_year
154157

@@ -283,8 +286,10 @@ def _calc_aggregated_points_in_aoi(
283286
pud_poly_feature_queue = multiprocessing.Queue(4)
284287
n_polytest_processes = multiprocessing.cpu_count()
285288

289+
LOGGER.info(f'OPENING {self.qt_pickle_filename}')
286290
with open(self.qt_pickle_filename, 'rb') as qt_pickle:
287291
global_qt = pickle.load(qt_pickle)
292+
288293
aoi_layer = aoi_vector.GetLayer()
289294
aoi_extent = aoi_layer.GetExtent()
290295
aoi_ref = aoi_layer.GetSpatialRef()
@@ -1010,3 +1015,32 @@ def _hash_blocks(file_buffer_queue):
10101015
if fast_hash:
10111016
file_hash += '_fast_hash'
10121017
return file_hash
1018+
1019+
def transplant_quadtree(qt_pickle_filepath, workspace):
1020+
storage_dir = os.path.dirname(qt_pickle_filepath)
1021+
pickle_filepath = qt_pickle_filepath
1022+
1023+
def rename_managers(qt):
1024+
if qt.is_leaf:
1025+
qt.node_data_manager.manager_filename = f'{qt_pickle_filepath}.db'
1026+
qt.node_data_manager.manager_directory = os.path.dirname(qt_pickle_filepath)
1027+
qt.quad_tree_storage_dir = storage_dir
1028+
else:
1029+
[rename_managers(qt.nodes[index]) for index in range(4)]
1030+
return qt
1031+
1032+
with open(qt_pickle_filepath, 'rb') as qt_pickle:
1033+
global_qt = pickle.load(qt_pickle)
1034+
1035+
if global_qt.quad_tree_storage_dir != storage_dir:
1036+
LOGGER.info(
1037+
f'setting quadtree node references to the local filesystem '
1038+
f'{storage_dir}')
1039+
new_qt = rename_managers(global_qt)
1040+
pickle_filepath = os.path.join(
1041+
workspace, f'transplant_{os.path.basename(qt_pickle_filepath)}')
1042+
LOGGER.info(
1043+
f'writing new quadtree index to {pickle_filepath}')
1044+
with open(pickle_filepath, 'wb') as qt_pickle:
1045+
pickle.dump(new_qt, qt_pickle)
1046+
return pickle_filepath

0 commit comments

Comments
 (0)