Skip to content

Commit

Permalink
Avoid relayout when generating description.
Browse files Browse the repository at this point in the history
  • Loading branch information
orenbenkiki committed May 20, 2024
1 parent 2b48647 commit d162b26
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/v0.1.0/.documenter-siteinfo.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"documenter":{"julia_version":"1.10.0","generation_timestamp":"2024-05-20T18:48:31","documenter_version":"1.4.1"}}
{"documenter":{"julia_version":"1.10.0","generation_timestamp":"2024-05-20T20:02:02","documenter_version":"1.4.1"}}
4 changes: 2 additions & 2 deletions src/formats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1051,8 +1051,8 @@ function with_data_read_lock(action::Function, format::FormatReader, what::Abstr
end
end

function has_data_read_lock(format::FormatReader)::Bool
return has_read_lock(format.internal.data_lock)
function has_data_read_lock(format::FormatReader; read_only::Bool = false)::Bool
return has_read_lock(format.internal.data_lock; read_only = read_only)
end

function begin_data_write_lock(format::FormatReader, what::AbstractString...)::Nothing
Expand Down
34 changes: 23 additions & 11 deletions src/readers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,8 @@ function require_matrix(
return nothing
end

struct RequiresRelayoutException <: Exception end

struct UpgradeToWriteLockException <: Exception end

"""
Expand Down Expand Up @@ -587,6 +589,9 @@ function get_matrix(
end
catch exception
if exception isa UpgradeToWriteLockException
if Formats.has_data_read_lock(daf)
throw(RequiresRelayoutException())
end
return Formats.with_data_write_lock(daf, "get_matrix of:", name, "of:", rows_axis, "and:", columns_axis) do
return do_get_matrix(daf, rows_axis, columns_axis, name; default = default, relayout = relayout)
end
Expand Down Expand Up @@ -922,23 +927,30 @@ function matrices_description(
indent::AbstractString,
lines::Vector{String},
)::Nothing
is_first = true
is_first_matrix = true
for rows_axis in axes
for columns_axis in axes
matrices = collect(Formats.get_matrices_set_through_cache(daf, rows_axis, columns_axis))
if !isempty(matrices)
if is_first
push!(lines, "$(indent)matrices:")
is_first = false
end
sort!(matrices)
push!(lines, "$(indent) $(rows_axis),$(columns_axis):")
is_first_axes = true
for matrix in matrices
push!(
lines,
"$(indent) $(matrix): " *
depict(base_array(get_matrix(daf, rows_axis, columns_axis, matrix; relayout = false))),
)
try
data = get_matrix(daf, rows_axis, columns_axis, matrix; relayout = false)
if is_first_matrix
push!(lines, "$(indent)matrices:")
is_first_matrix = false
end
if is_first_axes
push!(lines, "$(indent) $(rows_axis),$(columns_axis):")
is_first_axes = false
end
push!(lines, "$(indent) $(matrix): " * depict(base_array(data)))
catch exception
if !(exception isa RequiresRelayoutException)
rethrow() # untested
end
end
end
end
end
Expand Down
32 changes: 32 additions & 0 deletions test/views.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,36 @@ nested_test("views") do
)
end
end

nested_test("requires_relayout") do
add_axis!(daf, "cell", ["A", "B"])
add_axis!(daf, "gene", ["X", "Y", "Z"])
set_vector!(daf, "cell", "batch", ["U", "V"])
set_vector!(daf, "cell", "age", [-1.0, 2.0])
set_matrix!(daf, "gene", "cell", "UMIs", [1 2; 3 4; 5 6]; relayout = false)
add_axis!(daf, "batch", ["U", "V", "W"])
set_vector!(daf, "batch", "sex", ["Male", "Female", "Male"])

view = viewer(
daf;
name = "view!",
axes = ["obs" => "/ cell", "var" => "/ gene"],
data = [ALL_SCALARS => nothing, ALL_VECTORS => "=", ("obs", "var", "X") => ": UMIs"],
)

@test description(view) == dedent("""
name: view!
type: View MemoryDaf
axes:
obs: 2 entries
var: 3 entries
vectors:
obs:
age: 2 x Float64 (Dense)
batch: 2 x String (Dense)
matrices:
var,obs:
X: 3 x 2 x Int64 in Columns (Dense)
""") * "\n"
end
end

0 comments on commit d162b26

Please sign in to comment.