Issue with AlgebraOfGraphics.jl: MethodError when using data(df)

Margot_Geisbauer
Mon, 10 Mar 2025 22:41:15 +0000
General Usage

I’m trying to plot timing data using AlgebraOfGraphics.jl and CairoMakie.jl. My data consists of timestamps (in seconds) and hit types (early, perfect, late). The hit types are mapped to numeric values:

"early" → -1.0

"perfect" → 0.0

"late" → 1.0

I create a DataFrame from the data and pass it to data(df), but I keep getting a MethodError. Here’s my code:

using AlgebraOfGraphics
using CairoMakie
using DataFrames

function load_json(filepath)
    open(filepath, "r") do file
        return JSON.parse(file)
    end
end

function process_data(json_data)
    times = []
    hit_types = []

    for entry in json_data
        for miss in entry["Details"]["miss"]
            time = miss["time"]
            offset = entry["Details"]["offset"]

            if offset < 0
                hit_type = "early"
            elseif offset == 0
                hit_type = "perfect"
            else
                hit_type = "late"
            end

            push!(times, time)
            push!(hit_types, hit_type)
        end
    end
    println(times)
println(hit_types)

    return times, hit_types
end

function plot_hits(times, hit_types)
    hit_mapping = Dict("early" => -1.0, "perfect" => 0.0, "late" => 1.0)
    hit_nums = [hit_mapping[hit] for hit in hit_types]

    times_clean = Float64.(collect(times))
    hit_nums_clean = Float64.(collect(hit_nums))

    df = DataFrame(time=times_clean, hit_type=hit_nums_clean)

    column_table = Tables.columntable(df)
    spec = data(column_table) * mapping(:time, :hit_type) * visual(Lines)

    spec |> draw
end

json_filepath = "Implementierung/src/return.json"
json_data = load_json(json_filepath)
times, hit_types = process_data(json_data)
plot_hits(times, hit_types)

The DataFrame looks fine:
10×2 DataFrame
Row │ time hit_type
─────┼───────────────────
1 │ 0.0 0.0
2 │ 15.0 1.0
3 │ 8.0 1.0
4 │ 14.0 1.0
5 │ 6.0 1.0
6 │ 10.0 1.0
7 │ 7.0 1.0
8 │ 12.0 1.0
9 │ 15.0 1.0
10 │ 9.0 1.0

When I run the function, I get the following error:

ERROR: MethodError: objects of type Vector{Any} are not callable
Use square brackets for indexing an Array.
The object of type Vector{Any} exists, but no method is defined for this combination of argument types when trying to treat it as a callable object.
Stacktrace:
[1] plot_hits(times::Vector{Any}, hit_types::Vector{Any})
@ Main c:\Users\margo\JuliaRepo\j2\Implementierung\src\TimeGraph.jl:55
[2] top-level scope
@ c:\Users\margo\JuliaRepo\j2\Implementierung\src\TimeGraph.jl:63

2 posts - 2 participants