Category Archives: Julia

Installing Julia and VS Code – A Comprehensive Guide

By: Steven Whitaker

Re-posted from: https://glcs.hashnode.dev/install-julia-and-vscode

Julia is a relatively new,free, and open-source programming language.It has a syntaxsimilar to that of other popular programming languagessuch as MATLAB and Python,but it boasts being able to achieve C-like speeds.

One popular IDE to use with Juliais Visual Studio Code,or VS Code.

In this post,we will learn how to install Julia and VS Code.We will also learn how to configure VS Codeto get it to work with Julia.

The instructions for installing Julia and VS Codevary by operating system.Please use the table of contentsto jump to the instructionsthat are relevant to you.Note that the instructions for configuring VS Codeare the same for all operating systems.

Windows

Installing Julia

  1. Go to https://julialang.org/downloads.
  2. In the table under the heading”Current stable release”,go to the row labeled “Windows”and click the “64-bit (installer)” link.(Of course, click the 32-bit linkif you have a 32-bit machine.)

    Downloading Julia installer on Windows

    (See Installing a Specific Julia Versionfor details on installing an older Julia version.)

  3. Run the installer.
  4. Choose where to install Julia.(The default location works wellunless you have a specific needto install Julia somewhere else.)Then click “Next”.

    Selecting Julia installation directory

  5. Specify some additional options.

    • “Create a Desktop shortcut”:Allows you to run Juliaby clicking the shortcuton your Desktop.
    • “Create a Start Menu entry”:Allows you to pin Julia to your Start Menu.Also allows you to easily search for Juliafrom the Start Menu.
    • “Add Julia to PATH”:(Recommended)Allows you to run Juliafrom the command linewithout specifying the full pathto the Julia executable.Also enables easier integrationwith VS Code.

    Then click “Next”.

    Specifying additional options

  6. Installation is complete!Click “Finish” to exit the installer.

    Installation is complete

Running Julia

There are a few different waysto run Julia on Windows.

  • If you created a Desktop shortcut,you can double-click the shortcutto start Julia.

    Julia Desktop icon in Windows

  • If you created a Start Menu entry,you can run Julia from the Start Menu.First search for “julia”.

    Start menu search bar

    And then click on the appropriate resultto start Julia.

    Start menu Julia search results

  • You can also run Juliafrom the command line(Command Prompt or PowerShell).If Julia was added to PATH,you can run
    C:\Users\user> julia

    Otherwise,you can specify the full (or relative) path.

    C:\Users\user> .\AppData\Local\Programs\Julia-1.9.3\bin\julia.exe

After starting Juliayou will be greeted witha fresh Julia prompt.

Fresh Julia prompt on Windows

Now you know how to install and run Julia on Windows!

Installing VS Code

  1. Go to https://code.visualstudio.com.
  2. Click “Download for Windows”.

    Downloading VS Code installer on Windows

  3. Run the installer.
  4. Click “I accept the agreement”and then click “Next”.

    VS Code license agreement on Windows

  5. Choose where to install VS Code.(The default location works wellunless you have a specific needto install VS Code somewhere else.)Then click “Next”.

    Selecting VS Code installation directory

  6. Specify the Start Menu folder for VS Code.(The default folder is fine.)Then click “Next”.

    Choosing VS Code start menu folder

  7. Specify additional options if desired.(The defaults are fine,but if you want a Desktop shortcutbe sure to select that option.)Then click “Next”.

    Specifying additional options

  8. Click “Install”.

    Confirming installation options for VS Code in Windows

  9. Installation is complete!Click “Finish” to exit the installer.

    Installation is complete

Running VS Code

You can run VS Codeusing any of the methods described abovefor running Julia:

  • using the Desktop shortcut.

    VS Code Desktop icon in Windows

  • using the Start Menu.

    Start menu VS Code search results

  • using the command line.
    C:\Users\user> code

Now you know how to install and run VS Code on Windows!

Jump to Configuring VS Code for Juliato learn how to configure VS Code.

macOS

Installing Julia

  1. Go to https://julialang.org/downloads.
  2. In the table under the heading”Current stable release”,go to the rows labeled “MacOS”and click the “64-bit (.dmg)” link for either Apple Siliconor the Intel or Rosetta link for older machines or special scenarios.

    Downloading Julia .dmg on macOS

    (See Installing a Specific Julia Versionfor details on installing an older Julia version.)

  3. Open the downloads folder and double-click on the Julia Disk Image you just downloaded.

    Open Julia .dmg on macOS

  4. In the new window, drag Julia into the Applications folder.

    Drag Julia

  5. Allow Julia to be copied with your credentials or Touch ID.

    Allow Julia

  6. Eject the disk image, either with the eject buttonor with right-click and selecting “Eject”.

    Eject Julia Disk Image

  7. We recommend you follow these final instructionsto make it easier to open Julia on your Mac.Open a new terminal window and follow these instructions:

    sudo mkdir -p /usr/local/binsudo rm -f /usr/local/bin/juliasudo ln -s /Applications/Julia-1.9.app/Contents/Resources/julia/bin/julia /usr/local/bin/julia

    Original source.

Running Julia

You can start Julia through your normal means of opening applicationsor through typing julia into your terminal (if you did the last step above).See Apple’s support if you need help.

After starting Juliayou will be greeted witha fresh Julia prompt.

Fresh Julia prompt on macOS

Now you know how to install and run Julia on macOS!

Installing VS Code

  1. Go to https://code.visualstudio.com.
  2. Click “Download Mac Universal”.

    Downloading VS Code app on macOS

  3. Open the downloads folder and drag VSCode into the Applications folder.

    Drag VSCode

  4. Allow VSCode to be moved with your credentials or Touch ID.

    Allow VSCode

You can start VS Code through your normal means of opening applications.

Now you know how to install and run VS Code on macOS!

Jump to Configuring VS Code for Juliato learn how to configure VS Code.

Linux

Installing Julia

  1. Go to https://julialang.org/downloads.
  2. In the table under the heading”Current stable release”,go to the row labeled “Generic Linux on x86″and click the “64-bit (glibc)” link.(This link should work for most Linux systems.If you have a different computer architecture,you probably already know to choose a different download link.)

    Downloading Julia installer on Linux

    (See Installing a Specific Julia Versionfor details on installing an older Julia version.)

  3. Open a terminaland navigate to the directorywhere you want to install Julia.In this post,we will install Juliain ~/programs/julia/.(Note that everything up to and including the $is the terminal prompt,so the actual command to runis everything after the $.)
    ~$ mkdir -p ~/programs/julia~$ cd ~/programs/julia
  4. Move the downloaded archiveto the current directory.
    ~/programs/julia$ mv ~/Downloads/julia-1.9.3-linux-x86_64.tar.gz .
  5. Unarchive the file.
    ~/programs/julia$ tar xzf julia-1.9.3-linux-x86_64.tar.gz
  6. Add Julia to your PATHby adding the followingto your .bashrc or .zshrc file.(Remember to change /home/user/programs/juliato the directory where you installed Julia.)
    export PATH="$PATH:/home/user/programs/julia/julia-1.9.3/bin"

    (Restart the terminal to actually update the PATH.)

Julia is now installed!

Running Julia

You can run Julia from the command line.

$ ~/programs/julia/julia-1.9.3/bin/julia

Or, if you added Julia to your PATH:

$ julia

After starting Juliayou will be greeted witha fresh Julia prompt.

Fresh Julia prompt on Linux

Now you know how to install and run Julia on Linux!

Installing VS Code

The following steps were testedon a computer running Ubuntu.They should work as writtenfor any Debian-based Linux distribution,but modifications may be necessaryfor other Linux distributions.Note that you will need admin privilegesfor one of the steps below.

  1. Go to https://code.visualstudio.com.
  2. Click ” .deb”.

    Downloading VS Code installer on Linux

  3. (Requires admin privileges)Either open your file managerand double-click the downloaded file,or install VS Codevia the command line.(Remember to change the file path/name as needed.)
    $ sudo dpkg -i /home/user/Downloads/code_1.81.1-1691620686_amd64.deb

VS Code is now installed!

Running VS Code

You can run VS Code from the command line.

$ code

Now you know how to install and run VS Code on Linux!

Jump to Configuring VS Code for Juliato learn how to configure VS Code.

Installing a Specific Julia Version

If you need a specific version of Julia,you can navigate to the older releases pageand find the appropriate installerfor the version you need.For example,to install Julia 1.0.5 on Windows,you would find “v1.0.5” on the left columnand then click on the download linkto download the installer.

Installing Julia 1.0.5

Otherwise,the installation instructionsshould be basically the same.

Configuring VS Code for Julia

After starting VS Codefor the first timeyou will be greeted with”Get Started with VS Code”.

Get Started with VS Code

Feel free to walk through the options,but for this postwe will ignore themand just click “< Welcome”on the top left of the window.That brings us to the welcome page.

VS Code welcome page

We need to install the Julia extension for VS Code.

  1. Click on the extensions button.

    VS Code extensions button

  2. Search for “julia”.Click “install” on the Julia extension.

    Installing Julia VS Code extension

Now we need to make surethe Julia extension knows where Julia is installed.If you added Julia to PATH,this step can be skipped.However,if you get errors trying to run Julia in VS Codeor if you find the wrong Julia version is being used(if you have multiple versions installed),you can follow these steps.

  1. Open the Julia extension settingsby clicking the settings icon.Click “Extension Settings”.

    Opening Julia extension settings

  2. Search for “executable path”.Then type the path to the Julia executablein the “Julia: Executable Path” extension setting.

    Telling VS Code where the Julia executable is

Now VS Code is ready for Julia development!

Developing Julia Code in VS Code

  1. Click the file icon in the top leftto open the Explorer pane.Then click “Open Folder”.

    Explorer pane in VS Code

  2. Navigate to a folder(or create one)where your Julia code will be saved.In this post,we created a folder called Julia.
  3. Click the “New File” buttonnext to the folder in the Explorer pane.Then type a name for the filewhere you will write Julia code.It should have a .jl extension.In this post,we created a file called script.jl.

    Creating a new file in VS Code

    VS Code will now open a new tabwith a blank fileready for us to edit.

  4. Add the following code to your fileand then save it.
    println("Hello, World!")
  5. To run the code,place your cursor on the line of code to executeand then press Shift+Enter.This command will start a Julia session(if one has not already been started)and then run the code.(The very first time you run codemay take a little whilebecause packages need to precompile.)

    Hello, World from VS Code

  6. Now add more lines of code.

    function plus1(x)    x + 1enda = 3b = plus1(a)b == a + 1
  7. To run all the code in the file,open the command palettewith Ctrl+Shift+Pand search for”Julia: Execute active file in REPL”.Highlight the command and press Enter.

    VS Code command palette

    File execution in VS Code

    Note that two lines of outputwere added to the REPL:the println statementand the value of b == a + 1(the last line that was executed).

  8. Now that some code has run,we can look at the Julia paneon the leftand see the variables and function we definedin the “Workspace” tab.

    Workspace pane in VS Code

  9. We can also use the REPL directly.

    VS Code Julia REPL

And now you know how to write and run Julia codein VS Code!

If you would like to see more tips and tricksfor how to use Julia in VS Code,be sure to comment below!

Summary

In this post,we learned how to install Juliaand VS Codeon different operating systems.We also learned how to configure VS Codefor developing Julia code.

Once you have installed Julia,move on to thenext post to learn about variables and functions!Or,feel free to take a lookat our other Julia tutorial posts!

Dropping columns from a data frame

By: Blog by Bogumił Kamiński

Re-posted from: https://bkamins.github.io/julialang/2023/09/08/dropcols.html

Introduction

One of the common tasks when working with a data frame is dropping some of its columns.
There are two ways to do it. You can either specify
which columns you want to keep or which columns you want to drop.

One of the frequent questions I get is how to do these operations with
[DataFrames.jl][tables] in case the list of columns to keep or drop might not
be a subset of columns of the data frame. This is the topic I want to cover in my today’s post.

The post was tested using Julia 1.9.2 and DataFrames.jl 1.6.1.

Standard column selection

First, create an example data frame:

julia> using DataFrames

julia> df = DataFrame(a=1, b=2, c=3, d=4)
1×4 DataFrame
 Row │ a      b      c      d
     │ Int64  Int64  Int64  Int64
─────┼────────────────────────────
   1 │     1      2      3      4

Now assume I want to keep columns :a and :c from it. You can do it by writing, for example:

julia> select(df, :a, :c)
1×2 DataFrame
 Row │ a      c
     │ Int64  Int64
─────┼──────────────
   1 │     1      3

You could also pass the columns as a variable using e.g., a vector:

julia> keep1 = [:a, :c]
2-element Vector{Symbol}:
 :a
 :c

julia> select(df, keep1)
1×2 DataFrame
 Row │ a      c
     │ Int64  Int64
─────┼──────────────
   1 │     1      3

Now, let us discuss dropping columns. Assume we want to keep all columns except columns :b and :d.
We can achieve this by using the Not command:

julia> select(df, Not(:b, :d))
1×2 DataFrame
 Row │ a      c
     │ Int64  Int64
─────┼──────────────
   1 │     1      3

Also in this case we can use a helper variable:

julia> drop1 = [:b, :d]
2-element Vector{Symbol}:
 :b
 :d

julia> select(df, Not(drop1))
1×2 DataFrame
 Row │ a      c
     │ Int64  Int64
─────┼──────────────
   1 │     1      3

The problematic case: the selected column is not present in a data frame

In some scenarios, we might want to provide a list of columns of which not
all are present in the data frame. For example, assume we want to keep
columns :a and :x. We see that the :x column is not present in our
df data frame.

Before we move forward, let me comment when such a situation occurs most often.
Assume you have 100 data frames that describe your data. Each data frame is similar,
but not identical. For example, a single data frame might represent data from one country
and the list of information for the countries does not have to be identical (for some
countries we might have more information, which results in more columns in a data frame).
When processing such data we might want to write one general condition on which columns
we want to keep or drop, and some of these columns might be present in only a subset of
all data frames.

Now let us go back to our example. Let us try keeping columns :a and :x:

julia> select(df, :a, :x)
ERROR: ArgumentError: column name "x" not found in the data frame; existing most similar names are: "a", "b", "c" and "d"

julia> keep2 = [:a, :x]
2-element Vector{Symbol}:
 :a
 :x

julia> select(df, keep2)
ERROR: ArgumentError: column name "x" not found in the data frame; existing most similar names are: "a", "b", "c" and "d"

We get an error. DataFrames.jl is designed to check, by default, that the operation you want to perform
on your data frame is valid. This is a conscious design decision. The reason is that in production application
settings most often when you say that you want to keep columns :a and :x you assume that they are present in df.
Thus you want to get an error if they would not be all present in it.

The same behavior can be observed for dropping columns. Assume we want to drop columns :b and :x:

julia> select(df, Not(:b, :x))
ERROR: ArgumentError: column name "x" not found in the data frame; existing most similar names are: "a", "b", "c" and "d"

julia> drop2 = [:b, :x]
2-element Vector{Symbol}:
 :b
 :x

julia> select(df, drop2)
ERROR: ArgumentError: column name "x" not found in the data frame; existing most similar names are: "a", "b", "c" and "d"

So what we saw here is a default behavior that was designed to be safe.
In what follows let me discuss how to perform a flexible selection.

Performing column selection when some of them are not present in a data frame

There are several solutions for column selection when some of them are not present in a data frame.
Let me present the one that I find the most convenient.
For this operation I typically use the Cols selector. The reason is that you can pass
a condition function (a predicate) as an argument to Cols that will select columns
whose names meet a passed condition.

Therefore the following operation:

julia> select(df, keep1)
1×2 DataFrame
 Row │ a      c
     │ Int64  Int64
─────┼──────────────
   1 │     1      3

Is the same as:

julia> keep1s = string.(keep1)
2-element Vector{String}:
 "a"
 "c"

julia> select(df, Cols(in(keep1s)))
1×2 DataFrame
 Row │ a      c
     │ Int64  Int64
─────┼──────────────
   1 │     1      3

Note what we did here. The in(keep1s) expression produces a function that checks if a value passed to it is in the keep1s vector.
It is important to note that although column selection in DataFrames.jl accepts both Symbol (like :a) and strings (like "a")
as column names the Cols-based selector will perform the check against strings. Therefore I had to convert the keep1 vector
of symbols to a keep1s vector of strings.

So far the select(df, Cols(in(keep1s))) is more verbose than just writing select(df, keep1). However, the benefit of Cols
is that when the in(keep1s) check is done we can have in keep1s vector whatever values we like, in particular,
they do not have be valid column names of our df.

Therefore to keep columns :a and :x, if we are unsure if these columns are present in df we can write:

julia> select(df, Cols(in(["a", "x"])))
1×1 DataFrame
 Row │ a
     │ Int64
─────┼───────
   1 │     1

Note that this time the operation works without an error. And again, please keep in mind that in this selector we need to pass
column names as strings.

Now you can probably already tell how to pass a list of columns to drop, without requiring that they are present in df.
The only thing to do is to use the !in function (not-in) instead of in. Let us drop columns :b and :x from our
data frame (keeping in mind that :x is not present in it):

julia> select(df, Cols(!in(["b", "x"])))
1×3 DataFrame
 Row │ a      c      d
     │ Int64  Int64  Int64
─────┼─────────────────────
   1 │     1      3      4

All worked as expected – the :b column was dropped and the :x column was ignored in the column dropping operation.

Conclusions

I hope you find the examples I gave today useful.

In general, the whole design of DataFrames.jl is similar to
what was discussed in this post. The default behavior is picked to be safe (as in our example: by default,
select checks if the columns you pass are present in a data frame), but it is possible to switch to an unsafe
mode relatively easily (in our example: using Cols with a predicate function).

Best Frenemies: Julia and Python

By: julia on Abel Soares Siqueira

Re-posted from: https://abelsiqueira.com/blog/2023-09-05-best-frenemies-julia-and-python/

Julia and Python are often seen as competitors, but in this video I want to show the integration between them. Using the PythonCall and the JuliaCall packages we can call Julia from Python and Python from Julia. Check the video out or check edited transcript below.
Don’t forget to like and subscribe.
Download the python-from-julia.jl Pluto notebook.
Download the julia-from-python.ipynb Jupyter notebook.
There are currently two ways to integrate Python in Julia and Julia in Python.