How do I incorporate Rich into my usual workflow as a Machine Learning Engineer

Manuel Gijón Agudo
4 min readOct 24, 2020

--

This subtitle is generated with Rich!

What is Rich, when to use it, in which context is worth and why should I care?

Rich is a python package for improving the default terminal output or, as the official documentations says:

Rich is a Python library for rich text and beautiful formatting in the terminal.”.

You can use it anytime and anywhere (works on Windows, Linux and MacOs), just install it using pip and your are ready to roll!. Rich even works in Jupyter Notebooks! The only requirement is to work in Python 3.6.1 or later.

pip install rich

Rich is easy to learn and to implement, however, I cannot tell you in which situations it is worth using it in your particular case, that will depend on you. What is in my hands to do is show you how I have been incorporating it to my daily work since I learned it. In order to do that, I will show you two examples of scripts with Rich as an example of task that I have to do frequently.

But at this point the question is still in the air, “why should I care about this?”. Well, this is true. Rich is not an essential tool as pandas or numpy and I am not trying to convince you of the contrary, especially because it is not for me either. But, I will tell you why I care enough to write this article: I care because in the past month I have been incorporating Rich in almost every script that I wrote in some way or another even without notice!. And over all, because I am convinced that Rich manages to combine the simplicity and speed of a script to work with data with the convenience of only using the terminal.

Improving the default Pandas Data Frame print

This is what appears if you print the iris data set using the print function.

It is all about data. Most of the time that we spend developing AI-based solutions we spend cleaning, transforming and aggregating data. Since this process is very iterative, everything that is to make the process easier or more pleasant, if it requires a reasonable investment of time and effort, I adopt it.

Above we can see the standard output of a pandas data frame print. Is very nice, but I write this function to print in a table shape that I think is nicer. Here the result (I also included some logic in the print that in a real example would be adapted to a particular situation, in this case the green floats corresponds with values grater than the mean of the values of each feature).

This table is generated with a custom function that just need a pandas data frame object as parameter.

Adding visual information in statistical test

Let me illustrate with this example of how I use Rich in order to facilitate the interpretability of the results displayed on the screen.

In this example, we have to apply a statistical test, the Shapiro-Wilk, to test if some data comes from a Normal distribution. This is a really common situation when we want to do inference. Also when we might need to check normality when we are working with some linear and generalized linear models.

Custom displayed of the results of a statistical test applied to a set of 7 sets of data.

Conclusions and bonus

For me this package, as I said before, manages to combine the simplicity and speed of a script to work with data with the convenience of only using the terminal. That is why I used so much, as a productivity tool.

I really think that this Rich is a very useful tool to incorporate in your Machine Learning daily practice, and with this article we just scratch the surface of its possibilities (custom tables, custom progress bars, syntax highlight, include markdown in your outputs and the list of options continues).

Example of customized progress bars.

To say goodbye let me say that I really hope that this text has been helpful to someone in someway. If you have any questions or comments about the article or the examples or detect any bugs on the code, do not hesitate to contact me. Any feedback would be welcome.

Thanks for reading!

--

--

Manuel Gijón Agudo

Research Engineer in AI Associate Professor at Polytechnic University of Catalonia's School of Informatics (Data Mining). BSc and MSc in Mathematics.