Author Archives: DSB

Developing your Julia package

By: DSB

Re-posted from: https://medium.com/coffee-in-a-klein-bottle/developing-your-julia-package-682c1d309507?source=rss-8bd6ec95ab58------2

A Tutorial on how to quickly and easily develop your own Julia package

We’ll show step-by-step how to develop your first package in Julia. To do this, we use the very useful PkgTemplate.jl, and we base our Tutorial on this guide by Quantecon and this video by Chris Rackauckas.

First things first. Start by creating the folder to store your package. In our case, we’ll be creating a package named “VegaGraphs”, which is a package that I’m developing. So, inside this folder, open your Julia REPL and install PkgTemplate.jl.

Installing PkgTemplates

1. Creating your Package Template

First, we need to create a template for our package. This template will include things like Licensing, Plugins, Authors, etc. For our example, we’ll be using a simple template with the MIT License. The plugins used will be GitHub Actions and Codecov. I’ll not be diving into these plugins, but just for the sake of clarity, I’ll briefly explain what they do.

  • GitHub Actions is a plugin that automatically builds a virtual machine and tests your code, hence, you can control the machine configurations necessary to running your package.
  • Codecov is a plugin that analysis your code, and evaluates how much of it is covered in your tests. So, for example, suppose that you wrote 3 different functions, but forgot to write a test for one of them. Then, Codecov will point out that there is no tests for such function.

Still inside the REPL, run the following commands:

t = Template(;user="YourUserNameOnGithub", plugins = [GitHubActions(), Codecov()], manifest = true)
generate("VegaGraphs.jl",t)

The first line of code defines the template, while the second one will generate your package. Note that I didn’t specify a folder, so the package will be created in a default location, which will be ~/.julia/dev (for Linux).

Now, just copy the files from the ~/.julia/dev/VegaGraphs to the folder where you will be working from, and then setup your git repository by running the following commands in the terminal:

# inside the VegaGraphs working folder
git init
git add -A
git commit -m "first commit"
git branch -M master
git remote add origin git@github.com:YOURUSERNAME/VegaGraphs.git
git push -u origin master

Taking a look inside the generated folder, you’ll have two folders and four files:

./src
./test
README.md
LICENSE
Manifest.toml
Project.toml
  • /src : This folder is where you will write the code for your package per se;
  • /test : Here is for storing your tests;
  • Project.toml: This is where you will store information such as the author, dependencies, julia version, etc;
  • Manifest.toml : This is a machine generated file, and you should just leave it be;

The other files are self explanatory.

2. Writing Code

We are ready to start coding our package. Note that inside the /src folder we already have a file named VegaGraphs.jl , which is the main file of our package. We can do all our coding directly inside VegaGraphs.jl , but as our code gets large, this might become messy.

Instead, we can write many different files for organizing our code, and then use VegaGraphs.jl to join everything together. Let’s do an example. We’ll code a very simple function, which we’ll store in another file, called graph_functions.jl , that will also be inside the ./src folder.

Here is some example code:

# code inside graph_functions.jl
function sum_values(x,y)
return x+y
end

The code above is a simple implementation of a function. Below I show how to actually make this function available to users. One just needs to “include” the graph_functions.jl file, and to export the plot_scatter . Once exported, the function is now available to anyone who imports our package.

# code inside VegaGraphs.jl
module VegaGraphs
using VegaLite
export sum_values
include("graph_functions.jl")
end

Note that, besides including our function, I’ve also imported the VegaLite package. Hence, I need to specify VegaLite.jl as a dependency. We’ll do this by using the REPL again.

Go to the root of your package and open the REPL by running the command julia in the terminal.Now, press ] . This will put you on “package mode”. Next, write activate . , which will activate the Julia environment to your current folder. Finally, write add VegaLite , and this will add VegaLite.jl to your dependencies inside the Project.toml file.

Adding dependency to your package

3. Creating and running tests

So we’ve implemented a function and added a dependency to our package. The following step is to write a test, to guarantee that our code is indeed working. The code is self-explanatory, we just write our test inside a testset. You can write as many as you like to guarantee that your function is working properly.

# code inside ./test/runtests.jl
using VegaGraphs
using Test
@testset "VegaGraphs.jl" begin
x = 2
y = 2
@test VegaGraphs.sum_values(x,y) == 4
end

Once the test is written, we have to run it and see if everything is working. Again, go to the root of the project and open your REPL. Guarantee that your environment is activated and run the tests as shown in the image below:

Running tests for your package

This will run your tests and see if everything passes. Once everything passes, we can trust that our code is working properly, and we can move on to more implementations.

4. Workflow – Text editor + Jupyter Notebook

With everything shown up until now, you are already ready to develop your package in Julia. Still, you might be interested on how to develop an efficient workflow. There are many possibilities here, such as using IDEs such as Juno and VsCode. I prefer to use Jupyter Notebooks with Vim (my text editor of choice), and doing everything from the terminal.

To use your developing package in your Notebook, you will need to activate your environment in a similar way that we’ve been doing up until now. As you open a new Notebook, run the following code in the very first cell.

Using Jupyter Notebook for developing packages

Note here that besides activating the environment, we also imported a package called Revise. This package is very helpful, and it should be imported right in the beginning of your notebook, before you actually import your own package, otherwise it won’t work properly!

Every time you modify the code in your package, to load the changes to your notebook you would have to restart the kernel. But when you use Revise.jl, you don’t need to restart your kernel, just import your package again, and the modifications will be applied.

Now, my workflow is very simple. I use Vim to modify the code in my package, and use the Notebook for trying things out. Once I get everything working as it should, I write down some tests, and test the whole thing.

5. Registering/Publishing your Package

Finally, suppose that you’ve finished writing your package, and you are ready to share it with the Julia community. Like with other packages, you want users to be able to write a simple Pkg.add("MyPackage") and install it. This process is called registering.

To do this, first go to Registrator.jl and install the app to your Github account.

In the Registrator.jl Gitub page, click in the “install app”

Next, go to your package Github’s page and enter the Issues tab. Create a new issue, an write @JuliaRegistrator register() , as shown in the image below.

Registering your package

Once you’ve done this, the JuliaRegistrator bot will open a pull request for your package to be registered. Unless you actually want your package to be registered, don’t actually submit the issue.

And that’s all.


Developing your Julia package was originally published in Coffee in a Klein Bottle on Medium, where people are continuing the conversation by highlighting and responding to this story.

Analyzing Graphs with Julia

By: DSB

Re-posted from: https://medium.com/coffee-in-a-klein-bottle/analyzing-graphs-with-julia-38e26d1d2f62?source=rss-8bd6ec95ab58------2

A brief tutorial on how to use Julia to analyze graphs using the JuliaGraphs packages

Example of Graph created using LigthsGraphs.jl and VegaLite.jl

First of all, let’s be clear. The goal of this article is to briefly introduce how to use Julia for analyzing graphs. Besides the many types of graphs (undirected, directed, bipartite, weighted…), there are also many methods for analyzing them (degree distribution, centrality measures, clustering measures, visual layouts …). Hence, a comprehensive introduction to Graph Analysis with Julia would be too large of a task.

Therefore, this tutorial focuses on undirected weighted graphs, since they encompass weightless graphs, and are usually more common than directed graphs¹.

JuliaGraphs Project

Almost every package you will need can be found in the JuliaGraphs Project. The project contains specific packages for plotting, network layouts, weighted graphs, and more. In our example, we’ll be using GraphPlot.jl and SimpleWeightedGraphs.jl. The good thing about the project is that these packages work together and are very similar in design. Hence, the functions you use for creating a weighted graph are very similar to the ones you use for creating a simple graph with LightGraphs.jl.

Creating your first Graph

Let’s create a DataFrame using the DataFrames.jl package. Each column will represent a person, and each row will represent an attribute. Therefore, our graph will be composed of nodes (people) and edges (people share the same attribute).

DataFrame used for creating the Graph

After creating the DataFrame, we create the graph. Note here that they are actually separate objects. To create the graph, you only need to specify the number of nodes, which is the number of columns. Below I present the code for the creation of the DataFrame and the Graph.

The nodes were inserted, now we have to create the appropriate edges. This is done by using the command add_edge!() which takes the graph, the nodes that must be connected, and the edge weight.

In our example, the the weight is equal to the number of shared attributes between two columns. For example, the first and second column both have 1’s in rows 1 and 7. Therefore, they share an edge with weight 2:

add_edge!(g,1,2,2) # add_edge!(graph, node_1, node_2, weight)

The following code loops through the data, and adds the edges to the graph.

Visualizing the Graph

With this, our graph is ready to be visualized. This can be easily done with the following command:

gplot(g,nodelabel=names(df),edgelinewidth=ew)
Output from gplot()

There are several different layouts to chose from, just take a look at the GraphPlots.jl page. Here is another example:

gplot(g,nodelabel=names(df),edgelinewidth=ew,layout=circular_layout)
Output of gplot() using another layout

Centrality and Minimum Spanning Tree

Let’s do some analysis in this graph. As I said in the beginning, there are many way to analyze a graph. Two very common methods are studying the centrality of each node, and creating a Minimum Spanning Tree (MST). The goal of this article is not to explain theoretical aspects of graphs, so I’ll assume that the reader knows what I’m talking about.

Here is an example of how to calculate the betweeness centrality and visualizing the results. Note that I added 1 in the first line of code just to enable a better visualization.

Output of code above. The nodes colors represent the centrality.

Finally, one can also easily create a MST. To do so, we must create a new graph, since we’ll be removing edges and adjusting the nodes’ locations. The code below shows how to do this. The only new function here is the kruskal_mst() which will give us the MST. You can choose if the MST is minimizing of maximizing the path. In our case, we want to create a tree the contains only the “strongest” connections, hence, we are maximizing.

MST from the code above.

Conclusion

This is the end of this brief tutorial. There are much more functionalities in the JuliaGraphs project, enabling a much more thorough analysis than the one presented here. Also, one can create more interactive visualizations using VegaLite.jl, but I’ll leave that for another article.

¹ Authors opinion.

² A Jupyter Notebook can also be found on Github.


Analyzing Graphs with Julia was originally published in Coffee in a Klein Bottle on Medium, where people are continuing the conversation by highlighting and responding to this story.

Vim for Julia

By: DSB

Re-posted from: https://medium.com/coffee-in-a-klein-bottle/vim-for-julia-18eba071c654?source=rss-8bd6ec95ab58------2

An easy setup for Vim as Julia IDE (on Ubuntu)

As someone who has been a Vim user for years, I can confidently say that Vim is highly addictive. Once you get used to it’s shortcuts, editing code without it becomes unbearable. Although many tutorials exist on how to setup a Vim IDE for Python and other languages, no tutorial seems to be available for using Vim with Julia. So this might be a first.

By no means this article is comprehensive, I intend to present a very neat and fast way of using Vim with Julia. Note that there are many alternatives to using “pure Vim”, one can code using Juno and install the vim-mode package in Atom. The method I propose is using “pure Vim”, but with the very cool SpaceVim.

If you are new to Vim, and is trying to learn it, you probably realized that Vim has thousands of plugins available, and setting up your “IDEish” Vim may be quite complicated. One has to learn how to install plugins, choose color highlighting themes, and much more. Here is where SpaceVim comes in.

SpaceVim is a Vim distribution that works pretty much like an IDE, with some of the most popular plugins already installed. It also comes with some preset shortcuts, allowing for a more efficient file navigation. And most importantly, it looks beautiful.

Installing Vim and SpaceVim

The first thing to do is to install Vim… Well, actually, NeoVim. If you are not familiar with NeoVim, it is pretty much an updated Vim. So why use it? The only reason is that in my experience, it just works better with SpaceVim and Julia, and also, I like the cursor better. But feel free to go with Vim if you prefer it. The commands below install NeoVim and then SpaceVim.

sudo apt install neovim
curl -sLf https://spacevim.org/install.sh | bash

To access NeoVim from the terminal, you just need to write

nvim “example.txt”

If like me, you prefer to use the common “vim” command, then you can create an alias.

If you are using bash (the standard shel), open the file ~/.bashrc and write alias vim="nvim $argv".

If you are using fish, then on the terminal write

function vim
nvim $argv
end

This will create a function called “vim” in the fish shell. Then run the command funcsave vim , to save the function so it becomes permanant.

When you run Vim for the first time after installing SpaceVim, you will note that some plugins will be installed. By the end of the installation, you might get an error message about “vimproc”. To fix this, go into the command line of Vim by typing : , then write VimProcInstall .

Example on running VimProcInstall to fix possible error messages.

Setting up Julia

To set up Julia now is incredibly simple. If a plugin is already available in the SpaceVim “ecosystem”, you just need to add that plugin to your init.toml file, which is located in your home directory inside the folder .SpaceVim.d , and SpaceVim will install it for you.

Hence, to install the packages for dealing with Julia, just open ~/.SpaceVim.d/init.toml and write the following in the end of the file

[[layer]]
name= "lang#julia"

You might also want to add

[[layer]]
name= "colorscheme"

The code above will enable different colorschemes, which can be chosen by altering the attribute colorscheme.

This is it. You now have a Vim IDE for Julia!

Example of file ~/.SpaceVim.d/init.toml — Note that the ‘autocomplete’ layer is disabled. This is due to some incompatibility between the Julia packages and the autocomplete package. At the end of this article I present a fix for this problem.

Tips on using SpaceVim

There are tons of tutorials on how to use Vim, so I will not go into that. But there is not as much information on using SpaceVim. So I’ll give you some tips on how to get started.

  • Installing Plugins: I’ve already talked about how to install different plugins. You just have to pretty much add a layer to your init.toml file. Here is a list of the available plugins for SpaceVim;
  • Navigating file: the NerdTree plugin is already shipped with SpaceVim, so one just needs to press F3 to open the navigation menu. You can press l to go inside a folder, or h to go out of the folder;
  • <Leader> and SPC: If you read the documentation of SpaceVim, you will likely encounter shortcuts such as SPC <Tab> . The SPC stands for “space bar”. The <Leader> default key is \ ;
  • Commenting line: To comment a line, just do SPC c l (space bar + c +l), and SPC c l ;
  • Copy to system clipboad: A common difficulty to people starting with Vim is the fact that you cannot easily copy text from Vim to a another file not opened on the same Vim window (e.g your Browser). SpaceVim solves this to you, just use <Leader> y to copy to your system clipboard, and <Leader> p ;
  • Moving from Buffers (tabs): If you are working on a file, then press F3 and open another file, a new buffer will become active. You can then press SPC <Tab> to change buffers, therefore, easily going from one file to another. Amazingly, the buffers will be shown on the top of Vim, and you can actually just click on them if you want (but if you are using Vim, you probably don’t want to be using your mouse).
Image showing the different buffers of files opened.

If you like this article or has suggestions on how it could be improved, consider “Clapping” or leaving a comment, so I can get some feedback.

UPDATE: When using SpaceVim with Julia autocompletion, you might experience some freezing when pressing the Tab . This is due some incompatibility between the julia-vim package and the other autocompletion packages present on SpaceVim. To solve this issue, you may disable the Latex unicode tab command in the julia-vim package. To do this, open the file ~./SpaceVim/autoload/SpaceVim/layers/lang/julia.vim . And disable the following command.

let g:latex_to_unicode_tab = 0


Vim for Julia was originally published in Coffee in a Klein Bottle on Medium, where people are continuing the conversation by highlighting and responding to this story.