Skip to content

Commit 3c08b7e

Browse files
committed
feat: corners check for ready chunks
1 parent 6c09823 commit 3c08b7e

File tree

1 file changed

+73
-8
lines changed

1 file changed

+73
-8
lines changed

examples/create_downampled.py

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,60 @@ def process(args):
749749
# with ProcessPoolExecutor(max_workers=max_workers) as executor:
750750
# executor.map(process, coords)
751751

752+
# %% Function to check if a chunk is fully covered by processed bounds
753+
754+
755+
def is_chunk_fully_covered(chunk_bounds, processed_chunks_bounds):
756+
"""
757+
Check if a chunk is fully covered by processed bounds.
758+
759+
Args:
760+
chunk_bounds: [start_coord, end_coord] where each coord is [x, y, z]
761+
processed_chunks_bounds: List of tuples (start, end) where start and end are [x, y, z]
762+
763+
Returns:
764+
bool: True if all 8 corners of the chunk are covered by processed bounds
765+
"""
766+
if not processed_chunks_bounds:
767+
return False
768+
769+
start_coord, end_coord = chunk_bounds
770+
x0, y0, z0 = start_coord
771+
x1, y1, z1 = end_coord
772+
773+
# Generate all 8 corners of the chunk
774+
corners = [
775+
[x0, y0, z0], # min corner
776+
[x1, y0, z0],
777+
[x0, y1, z0],
778+
[x0, y0, z1],
779+
[x1, y1, z0],
780+
[x1, y0, z1],
781+
[x0, y1, z1],
782+
[x1, y1, z1], # max corner
783+
]
784+
785+
# Check if each corner is covered by at least one processed bound
786+
for corner in corners:
787+
corner_covered = False
788+
for start, end in processed_chunks_bounds:
789+
# Check if corner is inside this processed bound
790+
if (
791+
start[0] <= corner[0] < end[0]
792+
and start[1] <= corner[1] < end[1]
793+
and start[2] <= corner[2] < end[2]
794+
):
795+
corner_covered = True
796+
break
797+
798+
# If any corner is not covered, the chunk is not fully covered
799+
if not corner_covered:
800+
return False
801+
802+
# All corners are covered
803+
return True
804+
805+
752806
# %% Function to check the output directory for completed chunks and upload them to GCS
753807

754808
processed_chunks_bounds = []
@@ -791,10 +845,11 @@ def check_and_upload_completed_chunks():
791845
chunk_bounds[1] = [
792846
min(cb, vs) for cb, vs in zip(chunk_bounds[1], volume_size)
793847
]
848+
# Subtract 1 from the end bounds to make them inclusive
849+
chunk_bounds[1] = [cb - 1 for cb in chunk_bounds[1]]
794850
# 2. Check if the chunk is fully covered by the processed bounds
795-
# TODO actually do this check
796-
covered = True
797-
851+
covered = is_chunk_fully_covered(chunk_bounds, processed_chunks_bounds)
852+
798853
if covered:
799854
# 3. If it is, upload it to GCS
800855
relative_path = chunk_file.relative_to(output_path)
@@ -829,6 +884,8 @@ def upload_any_remaining_chunks():
829884
output_path_for_mip = output_path / dir_name
830885
# For each file in the output dir
831886
for chunk_file in output_path_for_mip.glob("**/*"):
887+
if chunk_file in [uf[0] for uf in uploaded_files]:
888+
continue
832889
relative_path = chunk_file.relative_to(output_path)
833890
gcs_chunk_path = (
834891
gcs_output_path.rstrip("/")
@@ -857,12 +914,20 @@ def upload_any_remaining_chunks():
857914
total_uploaded_files += check_and_upload_completed_chunks()
858915
print(f"Total uploaded chunks so far: {total_uploaded_files}")
859916

917+
# Write files that were written before that final upload check
918+
with open(output_path / "processed_chunks.txt", "w") as f:
919+
for local_path, gcs_path in uploaded_files:
920+
f.write(f"{local_path} -> {gcs_path}\n")
860921

861-
# Final upload of any remaining chunks
862-
if use_gcs_output:
863-
print("Processing complete, uploading any remaining chunks...")
864-
total_uploaded_files += upload_any_remaining_chunks()
865-
print(f"Final upload completed: {total_uploaded_files} chunks uploaded")
922+
# Final upload of any remaining chunks - hopefully should be none here, but maybe some failed
923+
print("Processing complete, uploading any remaining chunks...")
924+
total_uploaded_files += upload_any_remaining_chunks()
925+
print(f"Final upload completed: {total_uploaded_files} chunks uploaded")
926+
927+
# Write the list of uploaded files to a text file for reference
928+
with open(output_path / "uploaded_files.txt", "w") as f:
929+
for local_path, gcs_path in uploaded_files:
930+
f.write(f"{local_path} -> {gcs_path}\n")
866931

867932
# %% Serve the dataset to be used in neuroglancer
868933
# vols[0].viewer(port=1337)

0 commit comments

Comments
 (0)