Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pychunkedgraph/app/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def after_request(response):
# for 9% fewer bytes.
response.data = compression.gzip_compress(response.data, compresslevel=1)
response.headers["Content-Encoding"] = "gzip"
response.headers["Vary"] = "Accept-Encoding"
response.vary.add("Accept-Encoding")
response.headers["Content-Length"] = len(response.data)
return response

Expand Down
85 changes: 50 additions & 35 deletions pychunkedgraph/app/meshing/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
from pychunkedgraph import __version__
from pychunkedgraph.app import app_utils
from pychunkedgraph.graph import chunkedgraph
from pychunkedgraph.graph import exceptions as cg_exceptions
from pychunkedgraph.app.meshing import tasks as meshing_tasks
from pychunkedgraph.meshing import meshgen
from pychunkedgraph.meshing.mesh_meta import MeshMeta
from pychunkedgraph.meshing.manifest import v2
from pychunkedgraph.meshing.manifest import get_highest_child_nodes_with_meshes
from pychunkedgraph.meshing.manifest import get_children_before_start_layer
from pychunkedgraph.meshing.manifest import ManifestCache

from pychunkedgraph.meshing.manifest.dynamic import dynamic_buckets

__meshing_url_prefix__ = os.environ.get("MESHING_URL_PREFIX", "meshing")

Expand Down Expand Up @@ -54,6 +57,11 @@ def handle_valid_frags(table_id, node_id):
## MANIFEST --------------------------------------------------------------------


def _flag(name: str, data: dict[str, object]) -> bool:
"""A manifest request flag, from the query string or the body, false when absent."""
return app_utils.toboolean(request.args.get(name) or data.get(name) or "false")


def handle_get_manifest(table_id, node_id):
current_app.request_type = "manifest"
current_app.table_id = table_id
Expand All @@ -68,13 +76,18 @@ def handle_get_manifest(table_id, node_id):
bounding_box = np.array([b.split("-") for b in bounds.split("_")], dtype=int).T

cg = app_utils.get_cg(table_id)
verify = request.args.get("verify", False)
verify = verify in ["True", "true", "1", True]
return_seg_ids = request.args.get("return_seg_ids", False)
prepend_seg_ids = request.args.get("prepend_seg_ids", False)
return_seg_ids = return_seg_ids in ["True", "true", "1", True]
prepend_seg_ids = prepend_seg_ids in ["True", "true", "1", True]
start_layer = cg.meta.custom_data.get("mesh", {}).get("max_layer", 2)
mm = MeshMeta(cg)
manifest_version = v2.served_manifest_version(request.headers.get("Accept"))
if manifest_version < 2 and mm.needs_v2:
raise cg_exceptions.NotAcceptable(
"This dataset serves meshes from an absolute path; upgrade your "
"client to one that requests "
"'Accept: application/x.cave;manifest_version=2'."
)
verify = _flag("verify", data)
return_seg_ids = _flag("return_seg_ids", data)
prepend_seg_ids = _flag("prepend_seg_ids", data)
start_layer = mm.max_layer
start_layer = int(request.args.get("start_layer", start_layer))
if "start_layer" in data:
start_layer = int(data["start_layer"])
Expand All @@ -91,8 +104,12 @@ def handle_get_manifest(table_id, node_id):
flexible_start_layer,
bounding_box,
data,
manifest_version,
)
return manifest_response(cg, args)
response = make_response(jsonify(manifest_response(cg, args)))
response.headers["X-Manifest-Version"] = str(manifest_version)
response.headers["Vary"] = "Accept"
return response


def manifest_response(cg, args):
Expand All @@ -107,34 +124,40 @@ def manifest_response(cg, args):
flexible_start_layer,
bounding_box,
data,
manifest_version,
) = args
resp = {}
seg_ids = []
if not verify:
seg_ids, resp["fragments"] = speculative_manifest_sharded(
seg_ids, fragments = speculative_manifest_sharded(
cg, node_id, start_layer=start_layer, bounding_box=bounding_box
)

else:
seg_ids, resp["fragments"] = get_highest_child_nodes_with_meshes(
seg_ids, fragments = get_highest_child_nodes_with_meshes(
cg,
np.uint64(node_id),
start_layer=start_layer,
bounding_box=bounding_box,
)
if prepend_seg_ids:
resp["fragments"] = [f"~{i}:{f}" for i, f in zip(seg_ids, resp["fragments"])]
if return_seg_ids:
resp["seg_ids"] = seg_ids

if manifest_version >= 2:
# Seg ids travel on the fragments and nowhere else: a top level list cannot be lined up
# with fragments split across buckets, and duplicates what the prefix already carries.
# Every row carries its seg id, so neither seg id flag changes a v2 response.
mm = MeshMeta(cg)
initial, dynamic = v2.to_v2_groups(seg_ids, fragments, verified=verify)
resp = v2.assemble(mm.initial_path, initial, dynamic_buckets(cg, dynamic))
else:
resp = {"fragments": fragments}
if prepend_seg_ids:
resp["fragments"] = [f"~{i}:{f}" for i, f in zip(seg_ids, fragments)]
if return_seg_ids:
resp["seg_ids"] = seg_ids
return _check_post_options(cg, resp, data, seg_ids)


def _check_post_options(cg, resp, data, seg_ids):
if app_utils.toboolean(data.get("return_seg_ids", "false")):
resp["seg_ids"] = seg_ids
if app_utils.toboolean(data.get("return_seg_id_layers", "false")):
if _flag("return_seg_id_layers", data):
resp["seg_id_layers"] = cg.get_chunk_layers(seg_ids)
if app_utils.toboolean(data.get("return_seg_chunk_coordinates", "false")):
if _flag("return_seg_chunk_coordinates", data):
resp["seg_chunk_coordinates"] = [
cg.get_chunk_coordinates(seg_id) for seg_id in seg_ids
]
Expand Down Expand Up @@ -180,22 +203,14 @@ def handle_remesh(table_id):

def _remeshing(serialized_cg_info, lvl2_nodes):
cg = chunkedgraph.ChunkedGraph(**serialized_cg_info)
cv_mesh_dir = cg.meta.dataset_info["mesh"]
cv_unsharded_mesh_dir = cg.meta.dataset_info["mesh_metadata"]["unsharded_mesh_dir"]
cv_unsharded_mesh_path = os.path.join(
cg.meta.data_source.WATERSHED, cv_mesh_dir, cv_unsharded_mesh_dir
)
mesh_data = cg.meta.custom_data["mesh"]

# TODO: stop_layer and mip should be configurable by dataset
mm = MeshMeta(cg)
meshgen.remeshing(
cg,
lvl2_nodes,
stop_layer=mesh_data["max_layer"],
mip=mesh_data["mip"],
max_err=mesh_data["max_error"],
cv_sharded_mesh_dir=cv_mesh_dir,
cv_unsharded_mesh_path=cv_unsharded_mesh_path,
stop_layer=mm.max_layer,
mip=mm.mip,
max_err=mm.max_error,
cv_unsharded_mesh_path=mm.dynamic_path,
)

return Response(status=200)
Expand Down
23 changes: 7 additions & 16 deletions pychunkedgraph/app/meshing/tasks.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
from pychunkedgraph.app import app_utils
from pychunkedgraph.meshing import meshgen, meshgen_utils
from pychunkedgraph.meshing import meshgen
from pychunkedgraph.meshing.mesh_meta import MeshMeta
import numpy as np
import os


def remeshing(table_id, lvl2_nodes):
lvl2_nodes = np.array(lvl2_nodes, dtype=np.uint64)
cg = app_utils.get_cg(table_id, skip_cache=True)

cv_mesh_dir = cg.meta.dataset_info["mesh"]
cv_unsharded_mesh_dir = cg.meta.dataset_info["mesh_metadata"]["unsharded_mesh_dir"]
cv_unsharded_mesh_path = os.path.join(
cg.meta.data_source.WATERSHED, cv_mesh_dir, cv_unsharded_mesh_dir
)
mesh_data = cg.meta.custom_data["mesh"]

# TODO: stop_layer and mip should be configurable by dataset
mm = MeshMeta(cg)
meshgen.remeshing(
cg,
lvl2_nodes,
stop_layer=mesh_data["max_layer"],
mip=mesh_data["mip"],
max_err=mesh_data["max_error"],
cv_sharded_mesh_dir=cv_mesh_dir,
cv_unsharded_mesh_path=cv_unsharded_mesh_path,
stop_layer=mm.max_layer,
mip=mm.mip,
max_err=mm.max_error,
cv_unsharded_mesh_path=mm.dynamic_path,
)
12 changes: 7 additions & 5 deletions pychunkedgraph/app/segmentation/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from pychunkedgraph import __version__
from pychunkedgraph.app import app_utils
from pychunkedgraph.meshing.mesh_meta import MeshMeta
from pychunkedgraph.graph import (
attributes,
cutting,
Expand Down Expand Up @@ -106,11 +107,12 @@ def handle_info(table_id):
combined_info["verify_mesh"] = cg.meta.custom_data.get("mesh", {}).get(
"verify", False
)
mesh_dir = cg.meta.custom_data.get("mesh", {}).get("dir", None)
if mesh_dir is not None:
combined_info["mesh_dir"] = mesh_dir
elif combined_info.get("mesh_dir", None) is not None:
combined_info["mesh_dir"] = "graphene_meshes"
# A table without a mesh dir has no meshes, so it advertises none.
if cg.meta.custom_data.get("mesh", {}).get("dir") is not None:
mm = MeshMeta(cg)
combined_info["mesh_dir"] = mm.dir
combined_info["dynamic_mesh_dir"] = mm.client_dynamic_dir
combined_info["mesh_metadata"] = mm.client_metadata
return jsonify(combined_info)


Expand Down
6 changes: 6 additions & 0 deletions pychunkedgraph/graph/basetypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Compat shim: ``basetypes`` lives at ``graph/utils/basetypes.py`` on this line
but at ``graph/basetypes.py`` on the pcgv3 line. Re-export so the shorter import
path resolves identically on both, keeping meshing code branch-agnostic.
"""

from .utils.basetypes import * # noqa: F401,F403
33 changes: 22 additions & 11 deletions pychunkedgraph/graph/chunkedgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ def __init__(
self._cache_service = None
self.mock_edges = None # hack for unit tests

# A copied table carries its source's meta; its first open under the new id
# rewrites and saves it once, and a later open matches the id.
if graph_id != self.graph_id:
self.update_meta(self.meta.for_copied_graph(graph_id), overwrite=True)

@property
def meta(self) -> ChunkedGraphMeta:
return self._meta
Expand Down Expand Up @@ -112,9 +117,13 @@ def range_read_chunk(
"""Read all nodes in a chunk."""
layer = self.get_chunk_layer(chunk_id)
root_chunk = layer == self.meta.layer_count
max_node_id = self.id_client.get_max_node_id(chunk_id=chunk_id, root_chunk=root_chunk)
max_node_id = self.id_client.get_max_node_id(
chunk_id=chunk_id, root_chunk=root_chunk
)
if layer == 1:
max_node_id = chunk_id | self.get_segment_id_limit(chunk_id) # pylint: disable=unsupported-binary-operation
max_node_id = chunk_id | self.get_segment_id_limit(
chunk_id
) # pylint: disable=unsupported-binary-operation

return self.client.read_nodes(
start_id=self.get_node_id(np.uint64(0), chunk_id=chunk_id),
Expand Down Expand Up @@ -283,9 +292,11 @@ def _get_children_multiple(
node_ids=node_ids, properties=attributes.Hierarchy.Child
)
return {
x: node_children_d[x][0].value
if x in node_children_d
else types.empty_1d.copy()
x: (
node_children_d[x][0].value
if x in node_children_d
else types.empty_1d.copy()
)
for x in node_ids
}
return self.cache.children_multiple(node_ids)
Expand Down Expand Up @@ -692,19 +703,19 @@ def get_l2_agglomerations(
sv_parent_d.update(dict(zip(svs.tolist(), [l2id] * len(svs))))

in_edges, out_edges, cross_edges = edge_utils.categorize_edges_v2(
self.meta,
all_chunk_edges,
sv_parent_d
self.meta, all_chunk_edges, sv_parent_d
)

agglomeration_d = get_agglomerations(
l2id_children_d, in_edges, out_edges, cross_edges, sv_parent_d
)
return (
agglomeration_d,
(self.mock_edges,)
if self.mock_edges is not None
else (in_edges, out_edges, cross_edges),
(
(self.mock_edges,)
if self.mock_edges is not None
else (in_edges, out_edges, cross_edges)
),
)

def get_node_timestamps(
Expand Down
5 changes: 5 additions & 0 deletions pychunkedgraph/graph/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class Forbidden(ClientError):
status_code = http_client.FORBIDDEN


class NotAcceptable(ClientError):
"""Exception mapping a ``406 Not Acceptable`` response."""
status_code = http_client.NOT_ACCEPTABLE


class Conflict(ClientError):
"""Exception mapping a ``409 Conflict`` response."""
status_code = http_client.CONFLICT
Expand Down
Loading
Loading