GitXplorerGitXplorer
M

showyourwork_julia_example

public
7 stars
1 forks
2 issues

Commits

List of commits on branch main.
Verified
7d76fdb113c0087588132d0f432b68cfbd5cfeef

Link git diff in README

MMilesCranmer committed 2 years ago
Verified
251efba01da57b1388d69874fbb4a7f3e6fb0fdd

Screenshot of hyperlink ability

MMilesCranmer committed 2 years ago
Verified
f925181794082cd7a91b1424192fab1dda5faafc

Describe LaTeX integration

MMilesCranmer committed 2 years ago
Verified
3fdf3ca8c3c5ccf815896ca2a5a5dff2f15df590

Re-order README.md

MMilesCranmer committed 2 years ago
Verified
1991309cb5c8c2731373e7a6f40e43f14bd079b4

Describe how `showyourwork.yml` was configured

MMilesCranmer committed 2 years ago
Verified
d651012f258d280c496df6d100ed7445ec42d42f

Proper use of `\script` inside paper tex

MMilesCranmer committed 2 years ago

README

The README file for this repository.

showyourwork

Article status Article tarball Read the article

An open source scientific article created using the showyourwork workflow.

Using Julia

Fork this repo to easily use Julia in showyourwork. The following modifications were made to the default template (You can also see this from the Git diff)

  1. Defined src/scripts/paths.jl, replacing src/scripts/paths.py (just a convenience file which defines paths when you include() it).
  2. Created a Project.toml to define Julia dependencies.
  3. Created two example scripts in src/scripts/:
    • data.jl, to create a dataset and save it to mydata.csv, and
    • plot.jl, to plot the dataset and save it to myplot.png.
  4. Created three Snakemake rules:
    • julia_manifest creates Manifest.toml from the Project.toml.
    • data calls data.jl, and depends on Manifest.toml.
    • plot calls plot.jl, and depends on mydata.csv and Manifest.toml.
  5. Configured showyourwork.yml to map .jl to julia.

The Snakefile also defines the JULIA_PROJECT as ".". These three Julia jobs are dependencies of the final rule, which compiles the LaTeX document using tectonic. The generated PDF and arXiv tarball will contain myplot.png.

For example, the rule plot:

rule plot:
    input:
        "Manifest.toml",
        data="src/data/mydata.csv",
    output: "src/tex/figures/myplot.png"
    script: "src/scripts/plot.jl"

This Julia script is then able to reference the variable snakemake:

using Gadfly
using Cairo
using CSV
using DataFrames

input_fname = snakemake.input["data"]
output_fname = snakemake.output[1]

data = open(input_fname, "r") do io
    CSV.read(io, DataFrame)
end

# Plot x vs y:
p = plot(data, x=:x, y=:y, Geom.line)

# Save:
draw(PNG(output_fname, 10cm, 7.5cm), p)

In ms.tex, we can define the corresponding figure as:

\begin{figure}[h!]
    \centering
    \includegraphics[width=0.5\textwidth]{figures/myplot.png}
    \caption{A figure.}
    \label{fig:fig1}
    \script{../scripts/plot.jl}
\end{figure}

Which will add a hyperlink to the script used to generate the figure:

Screenshot 2023-04-15 at 3 01 17 PM