Julia Fundamentals


  • Basic data types in Julia include integers, strings, and floating-point numbers.
  • Use variable = value to assign a value to a variable.
  • Use println(value) to display output.
  • Julia provides many built-in functions, such as typeof.

Analyzing Patient Data


  • Use Pkg.add("PackageName") to install and using PackageName to load packages in Julia.
  • Load CSV data into a DataFrame with CSV.read("file.csv", DataFrame).
  • Use df[row, column] to access specific values; use df[!, column] to access entire columns.
  • Use size(df), nrow(df), and ncol(df) to inspect DataFrame dimensions.
  • Convert a DataFrame to a matrix using Matrix(df) for numerical operations.
  • Use mean, maximum, minimum, and std to compute statistics on data arrays.
  • Use mean(data, dims=1) for column-wise and dims=2 for row-wise operations.
  • Use diff(data; dims=2) to calculate daily changes per patient.

Visualizing Tabular Data


  • Use the Plots.jl package to create simple and flexible visualizations.

Storing Multiple Values in Vectors


  • [value1, value2, value3, ...] creates a vector.
  • 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] and vec[2:9]), in the same way as strings and arrays.
  • Vectors are mutable (i.e., their values can be changed in place).

Automating Repetition with Loops


  • Use for variable to process the elements of a collection (like a vector) one at a time.
  • The body of a for loop must be placed inside for ... end.
  • The body of a while loop must be placed inside while ... end.
  • Use length(thing) to determine the length of a collection (vector, array, string, etc.).

Analyzing Multiple Files


  • Use glob(pattern, folder) (from Glob.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.

If/Else - Conditional Statements in Julia


  • Use if condition to start a conditional statement, elseif condition to provide additional tests, and else to provide a default.
  • The bodies of the branches of conditional statements must be enclosed within if/elseif/else and end.
  • Use == to test for equality.
  • X && Y is only true if both X and Y are true.
  • X || Y is true if either X or Y, 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=value in the parameter list.
  • Parameters can be passed by position, by name (keyword arguments), or omitted to use their default value.

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 UndefVarError will occur when trying to use a variable that does not exist.

  • Containers like arrays and strings will generate a BoundsError if 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.

Course Conclusion