From 6bfe7dfd8ef9ff7f44c2fae0d96a6079aadc0740 Mon Sep 17 00:00:00 2001 From: Pedro Ribeiro de Almeida Date: Fri, 9 Aug 2024 10:49:59 +1000 Subject: [PATCH 1/4] Fix show_after error when sizeof fails to determine DataType size This error was preventing users from creating YAXArrays of Strings} and Unions that contain Strings. --- src/Cubes/Cubes.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Cubes/Cubes.jl b/src/Cubes/Cubes.jl index 6666cde6..457609c2 100644 --- a/src/Cubes/Cubes.jl +++ b/src/Cubes/Cubes.jl @@ -510,7 +510,11 @@ function DD.show_after(io::IO, mime, c::YAXArray) blockwidth = get(io, :blockwidth, 0) DD.print_block_separator(io, "file size", blockwidth, blockwidth) println(io, " ") - println(io, " file size: ", formatbytes(cubesize(c))) + try + println(io, " file size: ", formatbytes(cubesize(c))) + catch e + e isa ErrorException ? println(" could not determine DataType size.") : rethrow(e) + end DD.print_block_close(io, blockwidth) # And if you want the array data to print: # ndims(c) > 0 && println(io) From 51d5f829f7f3a5d1ac264c1eb996f69058b0f510 Mon Sep 17 00:00:00 2001 From: Pedro Ribeiro de Almeida Date: Thu, 5 Sep 2024 13:57:00 +1000 Subject: [PATCH 2/4] Remove try catch and only print cube size for a few selected types --- src/Cubes/Cubes.jl | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Cubes/Cubes.jl b/src/Cubes/Cubes.jl index 457609c2..854ebb81 100644 --- a/src/Cubes/Cubes.jl +++ b/src/Cubes/Cubes.jl @@ -508,12 +508,10 @@ getCubeDes(::Type{T}) where {T} = string(T) function DD.show_after(io::IO, mime, c::YAXArray) blockwidth = get(io, :blockwidth, 0) - DD.print_block_separator(io, "file size", blockwidth, blockwidth) - println(io, " ") - try - println(io, " file size: ", formatbytes(cubesize(c))) - catch e - e isa ErrorException ? println(" could not determine DataType size.") : rethrow(e) + # This are some types for which sizeof is known to return a meaninful value + if isconcretetype(eltype(c)) && <:(eltype(c),Union{AbstractFloat,Signed,Unsigned,Bool}) + DD.print_block_separator(io, "file size", blockwidth, blockwidth) + println(io, "\n file size: ", formatbytes(cubesize(c))) end DD.print_block_close(io, blockwidth) # And if you want the array data to print: From ffc57e477c606022a8f095ea5d8ade1028a5b455 Mon Sep 17 00:00:00 2001 From: Lazaro Alonso Date: Sun, 22 Sep 2024 12:34:24 +0200 Subject: [PATCH 3/4] eltypes and fallback --- src/Cubes/Cubes.jl | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Cubes/Cubes.jl b/src/Cubes/Cubes.jl index 711ecb1c..5068ec60 100644 --- a/src/Cubes/Cubes.jl +++ b/src/Cubes/Cubes.jl @@ -508,19 +508,25 @@ getCubeDes(::Type{T}) where {T} = string(T) function DD.show_after(io::IO, mime, c::YAXArray) blockwidth = get(io, :blockwidth, 0) - # This are some types for which sizeof is known to return a meaninful value - if isconcretetype(eltype(c)) && <:(eltype(c),Union{AbstractFloat,Signed,Unsigned,Bool}) + # ? sizeof : Check if the element type is a bitstype or a union of bitstypes + if (isconcretetype(eltype(c)) && isbitstype(eltype(c))) || + (eltype(c) isa Union && all(isbitstype, Base.uniontypes(eltype(c)))) + DD.print_block_separator(io, "file size", blockwidth, blockwidth) println(io, "\n file size: ", formatbytes(cubesize(c))) + else # fallback + DD.print_block_separator(io, "memory size", blockwidth, blockwidth) + println(io, "\n summarysize size: ", formatbytes(Base.summarysize(parent(c)))) end DD.print_block_close(io, blockwidth) - # And if you want the array data to print: + # Uncomment to print array data if needed # ndims(c) > 0 && println(io) # DD.print_array(io, mime, c) end + include("TransformedCubes.jl") include("Slices.jl") include("Rechunker.jl") From 773d6271552094e2b3716c6a6a1411bd55ecd64b Mon Sep 17 00:00:00 2001 From: Lazaro Alonso Date: Sun, 22 Sep 2024 13:32:54 +0200 Subject: [PATCH 4/4] examples --- docs/src/UserGuide/faq.md | 22 +++++++++++++++++++++- src/Cubes/Cubes.jl | 2 +- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/docs/src/UserGuide/faq.md b/docs/src/UserGuide/faq.md index 4895303a..cd8d394e 100644 --- a/docs/src/UserGuide/faq.md +++ b/docs/src/UserGuide/faq.md @@ -363,4 +363,24 @@ and voilĂ  ds = Dataset(; (d_keys .=> yax_list)...) ```` -now they are printed together, showing that is exactly the same axis structure for all variables. \ No newline at end of file +now they are printed together, showing that is exactly the same axis structure for all variables. + +## Create a `YAXArray` with unions containing `Strings` + +````@example howdoi +test_x = stack(Vector{Union{Int,String}}[[1, "Test"], [2, "Test2"]]) +yax_string = YAXArray(test_x) +```` + +or simply with an `Any` type + +````@example howdoi +test_bool = ["Test1" 1 false; 2 "Test2" true; 1 2f0 1f2] +yax_bool = YAXArray(test_bool) +```` + +::: warning + +Note that although their creation is allowed, it is not possible to save these types into Zarr or NetCDF. + +::: \ No newline at end of file diff --git a/src/Cubes/Cubes.jl b/src/Cubes/Cubes.jl index 5068ec60..9a047bac 100644 --- a/src/Cubes/Cubes.jl +++ b/src/Cubes/Cubes.jl @@ -516,7 +516,7 @@ function DD.show_after(io::IO, mime, c::YAXArray) println(io, "\n file size: ", formatbytes(cubesize(c))) else # fallback DD.print_block_separator(io, "memory size", blockwidth, blockwidth) - println(io, "\n summarysize size: ", formatbytes(Base.summarysize(parent(c)))) + println(io, "\n summarysize: ", formatbytes(Base.summarysize(parent(c)))) end DD.print_block_close(io, blockwidth) # Uncomment to print array data if needed