Key Points
Julia Fundamentals
- Basic data types in Julia include integers, strings, and floating-point numbers.
- Use
variable = valueto assign a name to a value. - Use
println(value)or@showto display output. - Julia provides many built-in functions, such as
typeof. - Basic data structures include vectors, dictionaries and tuples.
- Vectors can contain any Julia object, including other vectors (i.e., a vector of vectors).
- Vectors are indexed and sliced with square brackets (e.g.,
vec[1]andvec[2:9]), in the same way as strings and arrays. - Vectors are mutable (i.e., their values can be changed in place).
Analyzing Patient Data
- Use
Pkg.add("PackageName")to install andusing PackageNameto load packages in Julia. - Load CSV data into a Matrix with
CSV.read("file.csv", CSV.Tables.matrix). - Use
size(df)to inspect Matrix dimensions. - Use
mean,maximum,minimum, andstdto compute statistics on data arrays. - Use
mean(data, dims=1)for column-wise anddims=2for row-wise operations. - Use
diff(data; dims=2)to calculate daily changes per patient.
Visualizing Tabular Data
- Use the
Plots.jlpackage to create simple and flexible visualizations.
If/Else - Conditional Statements in Julia
- Use
if conditionto start a conditional statement,elseif conditionto provide additional tests, andelseto provide a default. - The bodies of the branches of conditional statements must be
enclosed within
if/elseif/elseandend. - Use
==to test for equality. -
X && Yis only true if bothXandYare true. -
X || Yis true if eitherXorY, or both, are true.
Creating Functions
- Define a function using
function function_name(parameter)…end. - Call a function using
function_name(value). - Numbers are stored as integers or floating-point numbers.
- Variables defined within a function are local and can only be seen and used inside that function.
- Variables created outside of any function are global.
- Within a function, global variables can be accessed
- Use docstrings (triple-quoted strings
""" ... """) to document a function. - Specify default values for parameters when defining a function using
parameter=valuein the parameter list. - Parameters can be passed by position, by name (keyword arguments), or omitted to use their default value.
Automating Repetition with Loops
- Use
for variableto process the elements of a collection (like a vector) one at a time. - The body of a
forloop must be placed insidefor ... end. - The body of a
whileloop must be placed insidewhile ... end. - Use
length(thing)to determine the length of a collection (vector, array, string, etc.).
Analyzing Multiple Files
- Use
glob(pattern, folder)(fromGlob.jl) to get a vector of files whose names match a given pattern. - In the pattern,
*matches zero or more characters, and?matches exactly one character.
Handling errors
Julia error messages may look intimidating at first, but they contain useful information: what type of error occurred, where it happened, and sometimes hints about why.
An error having to do with the grammar or structure of the program is called a
syntax: ...error.An
UndefVarErrorwill occur when trying to use a variable that does not exist.Containers like arrays and strings will generate a
BoundsErrorif you try to access an element at an index that does not exist.Trying to open a file that does not exist will give you a
SystemError.
Writing Tests
Debugging
- Know what code is supposed to do before trying to debug it.
- Make it fail every time.
- Make it fail fast.
- Change one thing at a time, and for a reason.
- Keep track of what you’ve done.
- Ask for help.