Skip to content

Commit

Permalink
jq: add zarr functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bogovicj committed Nov 10, 2023
1 parent 2d7f5d4 commit b9ebe69
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/resources/n5.jq
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ def scaleTransform( $scales ): { "type" : "scale", "scale" : $scales };

def toTreePath: ltrimstr( "/") | split("/") | map_values( ["children", . ] ) | flatten;

def fromTreePath: if (length == 0) then "" else [.[range(1;length;2)]] | join("/") end;

def getSubTree( $path ): getpath( $path | toTreePath );

def moveSubTree( $srcPath; $dstPath ): getSubTree( $srcPath ) as $subTree | setpath( $dstPath | toTreePath; $subTree )
Expand Down
56 changes: 56 additions & 0 deletions src/main/resources/zarr.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
def cleanPath: . | sub("^\\.(?<x>.*)\\/.+"; "\(.x)"; "g");

def containsExactString($query): reduce .[] as $i (false; . or ($i == $query));

def hasAllKeys($queryKeys): keys as $keys |
reduce ($queryKeys | .[] ) as $k (true; . and ( $keys | containsExactString($k)));

def inExact($arr): reduce ($arr | .[]) as $v ([., false]; [.[0], .[1] or ($v == .[0])]) | .[1];

def zarrArrayKeys: ["zarr_format", "shape", "chunks", "dtype", "compressor", "fill_value", "order",
"dimension_separator", "filters"];

def zarrGroupKeys: ["zarr_format"];

def isZarrArray: type == "object" and hasAllKeys( zarrArrayKeys );

def zarrArrayObj: zarrArrayKeys as $za | with_entries(select(.key | inExact( $za )));

def zarrArrayAttrs: zarrArrayKeys as $za | with_entries(select(.key | inExact( $za ) | not));

def zarrGroupObj: { "zarr_format" : .zarr_format };

def zarrGroupAttrs: zarrGroupKeys as $zg | with_entries(select(.key | inExact( $zg ) | not));

def zarrAttrs: . as $obj |
if ($obj | isZarrArray) then
{
".zarray" : ($obj | zarrArrayObj),
".zattrs" : ($obj | zarrArrayAttrs)
}
else
{
".zgroup" : ($obj | zarrGroupObj),
".zattrs" : ($obj | zarrGroupAttrs)
}
end;

def hasAttributes: type == "object" and has("attributes");

def zomTreePath: ltrimstr( "/") | split("/") | map_values( ["members", . ] ) | flatten;

def zomAdd( $path; $attrs; $suffix ):
($path | zomTreePath | . + $suffix) as $p |
getpath( $p ) as $currentAttrs |
setpath( $p; $currentAttrs + $attrs );

def zomAddFiles:
reduce inputs as $i ({};
input_filename as $f |
($f | cleanPath) as $p |
if ($f | endswith(".zattrs")) then
zomAdd( ($p | cleanPath); $i; ["attributes"] )
else
zomAdd( ($p | cleanPath); $i; [] )
end);

0 comments on commit b9ebe69

Please sign in to comment.