<commit_before># Plot size
width = 1890px
height = 800px

set_default_plot_size(width, height)

# Colors gradients
totals_gradient = Scale.lab_gradient(map(x -> parse(Colorant, x),
                                         ["lightgrey", "red", "orange", "yellow", "green", "blue"])...)
marks_gradient(p) = totals_gradient(1-p)


"""
Create a function which maps a number from [0,1] to a color from the given
gradient.
"""
function color_chooser(gradient)
    return p -> gradient(if p < 1/3; 0.0; else 3*p/2-1/2; end)
end


"""
Save a plot to disk as a PNG file.

#### Arguments

* `dir::AbstractString`: The directory in which the generated image is stored.
* `filename::AbstractString`: The file name of the image.
* `width`: How wide the plot is supposed to be (in cm, pixels, etc.).
* `height`: How high the plot is supposed to be (in cm, pixels, etc.).
* `plot`: The plot to be stored.

#### Examples

    save_plot("/tmp", "image", 800px, 600px, Gadfly.plot(...))

"""
function save_plot(dir::AbstractString, filename::AbstractString, width, height, plot)
    draw(PNG(joinpath(dir, filename * ".png"), width, height), plot)
end


"""
Compute totals for all the students and plot a histogram.
"""
function plot_totals(data::LectureData)
    info("Plotting totals...")
    perfect_score = data.exercises.perfect_score
    totals = data.exercises.totals

    #count_totals = by(totals, :total, df -> DataFrame(count = nrow(df)))
    bins = data.exercises.number_of_assignments

    p = plot(totals, x=:total, color=:Matrikelnummer,
             xintercept=perfect_score/100*[30, 40, 50],
             Geom.vline(color=colorant"red") ,
             Geom.histogram(bincount=bins),
             Guide.colorkey("Matnr."),
             Guide.xlabel("Gesamtpunktzahl"),
             Guide.ylabel("Häufigkeit", orientation=:vertical),
             Guide.xticks(ticks=[0:8:maximum(totals[:total])]),
             Scale.ContinuousColorScale(totals_gradient),
             Theme(background_color=colorant"white"))

    save_plot(data.output_directory, "totals", width, 200px, p)
end

function relative_score(scores, maxs)
    result = copy(scores)
    result[maxs .!= 0] ./= maxs[maxs .!= 0]
    result[maxs .== 0] ./= abs(result[maxs .== 0])
    result[result .< 0] = -0.75
    result
end


"""
Figure out and plot how often every possible score occurred for each of the
assignments.
"""
function plot_overview(data::LectureData)
    info("Plotting overview")
    scores = data.exercises.scores
    num_students = data.exercises.number_of_students<commit_msg>Replace deprecated [a] by collect(a)<commit_after># Plot size
width = 1890px
height = 800px

set_default_plot_size(width, height)

# Colors gradients
totals_gradient = Scale.lab_gradient(map(x -> parse(Colorant, x),
                                         ["lightgrey", "red", "orange", "yellow", "green", "blue"])...)
marks_gradient(p) = totals_gradient(1-p)


"""
Create a function which maps a number from [0,1] to a color from the given
gradient.
"""
function color_chooser(gradient)
    return p -> gradient(if p < 1/3; 0.0; else 3*p/2-1/2; end)
end


"""
Save a plot to disk as a PNG file.

#### Arguments

* `dir::AbstractString`: The directory in which the generated image is stored.
* `filename::AbstractString`: The file name of the image.
* `width`: How wide the plot is supposed to be (in cm, pixels, etc.).
* `height`: How high the plot is supposed to be (in cm, pixels, etc.).
* `plot`: The plot to be stored.

#### Examples

    save_plot("/tmp", "image", 800px, 600px, Gadfly.plot(...))

"""
function save_plot(dir::AbstractString, filename::AbstractString, width, height, plot)
    draw(PNG(joinpath(dir, filename * ".png"), width, height), plot)
end


"""
Compute totals for all the students and plot a histogram.
"""
function plot_totals(data::LectureData)
    info("Plotting totals...")
    perfect_score = data.exercises.perfect_score
    totals = data.exercises.totals

    #count_totals = by(totals, :total, df -> DataFrame(count = nrow(df)))
    bins = data.exercises.number_of_assignments

    p = plot(totals, x=:total, color=:Matrikelnummer,
             xintercept=perfect_score/100*[30, 40, 50],
             Geom.vline(color=colorant"red") ,
             Geom.histogram(bincount=bins),
             Guide.colorkey("Matnr."),
             Guide.xlabel("Gesamtpunktzahl"),
             Guide.ylabel("Häufigkeit", orientation=:vertical),
             Guide.xticks(ticks=collect(0:8:maximum(totals[:total]))),
             Scale.ContinuousColorScale(totals_gradient),
             Theme(background_color=colorant"white"))

    save_plot(data.output_directory, "totals", width, 200px, p)
end

function relative_score(scores, maxs)
    result = copy(scores)
    result[maxs .!= 0] ./= maxs[maxs .!= 0]
    result[maxs .== 0] ./= abs(result[maxs .== 0])
    result[result .< 0] = -0.75
    result
end


"""
Figure out and plot how often every possible score occurred for each of the
assignments.
"""
function plot_overview(data::LectureData)
    info("Plotting overview")
    scores = data.exercises.scores
    num_students = data.exercises.number_of_students
{'fraction_non_alphanumeric': 0.14018691588785046, 'fraction_numerical': 0.01791277258566978, 'mean_word_length': 3.463075586446568, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6455800', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1650, 'n_words': 439}